Reduce code duplication (#233)

* Use ConvertGridNoToCenterCellXY instead of CenterX & CenterY

* Add direction utility function

We have a lot of places in the code that calculate direction based on center cell coordinates, but lack a utility function for it similar to other direction methods.

* Use utility function for calculating direction

* Use ConvertGridNoToCenterCellXY

* Use utility function for direction

* Use ConvertGridNoToCenterCellXY instead of CenterX & CenterY

* Use ConvertGridNoToCenterCellXY instead of CenterX & CenterY

* Remove CenterX & CenterY functions

Use ConvertGridNoToCenterCellXY instead

* Remove CenterX & CenterY calls from UB configuration

* Address review feedback

* Remove duplicate function

ConvertGridNoToCenterCellXY and ConvertMapPosToWorldTileCenter do the exact same thing

* Use PythSpacesAway instead of GetRangeFromGridNoDiff

Both functions calculate the same thing

* Remove GetRangeFromGridNoDiff

* Remove calls to abs()

The values end up being squared anyways making these unnecessary
This commit is contained in:
Asdow
2023-10-07 15:14:15 +03:00
committed by GitHub
parent b68f206969
commit b8a870df02
37 changed files with 389 additions and 382 deletions
+24 -11
View File
@@ -698,8 +698,7 @@ INT8 ExplosiveDamageStructureAtGridNo( STRUCTURE * pCurrent, STRUCTURE **ppNextC
#endif
// Get xy
sX = CenterX( sGridNo );
sY = CenterY( sGridNo );
ConvertGridNoToCenterCellXY(sGridNo, &sX, &sY);
// ATE: Continue if we are only looking for walls
if ( fOnlyWalls && !( pCurrent->fFlags & STRUCTURE_WALLSTUFF ) )
@@ -1305,7 +1304,9 @@ INT8 ExplosiveDamageStructureAtGridNo( STRUCTURE * pCurrent, STRUCTURE **ppNextC
// Make secondary explosion if eplosive....
if ( fExplosive )
{
InternalIgniteExplosion( ubOwner, CenterX( sBaseGridNo ), CenterY( sBaseGridNo ), 0, sBaseGridNo, STRUCTURE_EXPLOSION, FALSE, bLevel );
INT16 sX, sY;
ConvertGridNoToCenterCellXY(sBaseGridNo, &sX, &sY);
InternalIgniteExplosion( ubOwner, sX, sY, 0, sBaseGridNo, STRUCTURE_EXPLOSION, FALSE, bLevel );
}
}
@@ -1477,7 +1478,9 @@ void ExplosiveDamageGridNo( INT32 sGridNo, INT16 sWoundAmt, UINT32 uiDist, BOOLE
}
{
InternalIgniteExplosion( ubOwner, CenterX( sNewGridNo2 ), CenterY( sNewGridNo2 ), 0, sNewGridNo2, RDX, FALSE, bLevel );
INT16 sX, sY;
ConvertGridNoToCenterCellXY(sNewGridNo2, &sX, &sY);
InternalIgniteExplosion( ubOwner, sX, sY, 0, sNewGridNo2, RDX, FALSE, bLevel );
}
fToBreak = TRUE;
@@ -4163,7 +4166,10 @@ void HandleExplosionQueue( void )
// no preplaced (owner=NOBODY) tripwire with explosive attachments allowed
if ( gGameExternalOptions.bAllowExplosiveAttachments && (*pObj)[0]->data.misc.ubBombOwner > 1 )
{
fAttFound=HandleAttachedExplosions( (UINT8) ((*pObj)[0]->data.misc.ubBombOwner - 2), CenterX( sGridNo ), CenterY( sGridNo ), 0,
INT16 sX, sY;
ConvertGridNoToCenterCellXY(sGridNo, &sX, &sY);
fAttFound=HandleAttachedExplosions( (UINT8) ((*pObj)[0]->data.misc.ubBombOwner - 2), sX, sY, 0,
sGridNo, (*pObj)[0]->data.misc.usBombItem, FALSE, ubLevel, (*pObj)[0]->data.ubDirection, pObj);
}
}
@@ -4236,15 +4242,18 @@ void HandleExplosionQueue( void )
CheckForBuriedBombsAndRemoveFlags( sGridNo, ubLevel);
// BOOM!
INT16 sX, sY;
ConvertGridNoToCenterCellXY(sGridNo, &sX, &sY);
// bomb objects only store the SIDE who placed the bomb! :-(
if ( (*pObj)[0]->data.misc.ubBombOwner > 1 )
{
IgniteExplosion( (UINT8) ((*pObj)[0]->data.misc.ubBombOwner - 2), CenterX( sGridNo ), CenterY( sGridNo ), 0, sGridNo, (*pObj)[0]->data.misc.usBombItem, ubLevel, (*pObj)[0]->data.ubDirection, pObj);
IgniteExplosion( (UINT8) ((*pObj)[0]->data.misc.ubBombOwner - 2), sX, sY, 0, sGridNo, (*pObj)[0]->data.misc.usBombItem, ubLevel, (*pObj)[0]->data.ubDirection, pObj);
}
else
{
// pre-placed
IgniteExplosion( NOBODY, CenterX( sGridNo ), CenterY( sGridNo ), 0, sGridNo, (*pObj)[0]->data.misc.usBombItem, ubLevel, (*pObj)[0]->data.ubDirection );
IgniteExplosion( NOBODY, sX, sY, 0, sGridNo, (*pObj)[0]->data.misc.usBombItem, ubLevel, (*pObj)[0]->data.ubDirection );
}
}
/* if ( FindWorldItemForBuriedBombInGridNo(sGridNo, ubLevel) != -1 )
@@ -5530,8 +5539,8 @@ void FireFragmentsTrapGun( SOLDIERTYPE* pThrower, INT32 gridno, INT16 sZ, OBJECT
INT16 horizontalarc = 5;
INT16 verticalarc = 0;
INT16 sX = CenterX(gridno);
INT16 sY = CenterY(gridno);
INT16 sX, sY;
ConvertGridNoToCenterCellXY(gridno, &sX, &sY);
AssertMsg( ubFragRange > 0 , "Fragmentation data lacks range property!" );
@@ -6331,7 +6340,9 @@ void RoofDestruction( INT32 sGridNo, BOOLEAN fWithExplosion )
static UINT16 usRoofCollapseExplosionIndex = 1727;
if ( HasItemFlag( usRoofCollapseExplosionIndex, ROOF_COLLAPSE_ITEM ) || GetFirstItemWithFlag( &usRoofCollapseExplosionIndex, ROOF_COLLAPSE_ITEM ) )
{
InternalIgniteExplosion( NOBODY, CenterX( sGridNo ), CenterY( sGridNo ), 0, sGridNo, usRoofCollapseExplosionIndex, FALSE, 0 );
INT16 sX, sY;
ConvertGridNoToCenterCellXY(sGridNo, &sX, &sY);
InternalIgniteExplosion( NOBODY, sX, sY, 0, sGridNo, usRoofCollapseExplosionIndex, FALSE, 0 );
}
if ( Chance( 15 ) )
@@ -6340,7 +6351,9 @@ void RoofDestruction( INT32 sGridNo, BOOLEAN fWithExplosion )
if ( debrissmokeitem )
{
InternalIgniteExplosion( NOBODY, CenterX( sGridNo ), CenterY( sGridNo ), 0, sGridNo, debrissmokeitem, FALSE, 0 );
INT16 sX, sY;
ConvertGridNoToCenterCellXY(sGridNo, &sX, &sY);
InternalIgniteExplosion( NOBODY, sX, sY, 0, sGridNo, debrissmokeitem, FALSE, 0 );
}
}
}
+3 -64
View File
@@ -708,27 +708,6 @@ BOOLEAN GetMouseMapPos( INT32 *psMapPos )
}
BOOLEAN ConvertMapPosToWorldTileCenter( INT32 usMapPos, INT16 *psXPos, INT16 *psYPos )
{
INT16 sWorldX, sWorldY;
INT16 sCellX, sCellY;
// Get X, Y world GRID Coordinates
sWorldY = ( usMapPos / WORLD_COLS );
sWorldX = usMapPos - ( sWorldY * WORLD_COLS );
// Convert into cell coords
sCellY = sWorldY * CELL_Y_SIZE;
sCellX = sWorldX * CELL_X_SIZE;
// Add center tile positions
*psXPos = sCellX + ( CELL_X_SIZE / 2 );
*psYPos = sCellY + ( CELL_Y_SIZE / 2 );
return( TRUE );
}
void GetScreenXYWorldCoords( INT16 sScreenX, INT16 sScreenY, INT16 *psWorldX, INT16 *psWorldY )
{
INT16 sOffsetX, sOffsetY;
@@ -931,7 +910,7 @@ void ConvertGridNoToCellXY( INT32 sGridNo, INT16 *sXPos, INT16 *sYPos )
*sXPos = ( *sXPos * CELL_X_SIZE );
}
void ConvertGridNoToCenterCellXY( INT32 sGridNo, INT16 *sXPos, INT16 *sYPos )
void ConvertGridNoToCenterCellXY( const INT32 sGridNo, INT16 *sXPos, INT16 *sYPos )
{
*sYPos = ( sGridNo / WORLD_COLS );
*sXPos = ( sGridNo - ( *sYPos * WORLD_COLS ) );
@@ -940,22 +919,6 @@ void ConvertGridNoToCenterCellXY( INT32 sGridNo, INT16 *sXPos, INT16 *sYPos )
*sXPos = ( *sXPos * CELL_X_SIZE ) + ( CELL_X_SIZE / 2 );
}
INT32 GetRangeFromGridNoDiff( INT32 sGridNo1, INT32 sGridNo2 )
{
INT32 uiDist;
INT16 sXPos, sYPos, sXPos2, sYPos2;
// Convert our grid-not into an XY
ConvertGridNoToXY( sGridNo1, &sXPos, &sYPos );
// Convert our grid-not into an XY
ConvertGridNoToXY( sGridNo2, &sXPos2, &sYPos2 );
uiDist = (INT32)(sqrt((double) ( sXPos2 - sXPos )*( sXPos2 - sXPos ) + ( sYPos2 - sYPos ) * ( sYPos2 - sYPos ) ));
return( uiDist );
}
INT32 GetRangeInCellCoordsFromGridNoDiff( INT32 sGridNo1, INT32 sGridNo2 )
{
INT16 sXPos, sYPos, sXPos2, sYPos2;
@@ -1002,8 +965,8 @@ INT16 PythSpacesAway(INT32 sOrigin, INT32 sDest)
{
INT16 sRows,sCols,sResult;
sRows = abs((sOrigin / MAXCOL) - (sDest / MAXCOL));
sCols = abs((sOrigin % MAXROW) - (sDest % MAXROW));
sRows = (sOrigin / MAXCOL) - (sDest / MAXCOL);
sCols = (sOrigin % MAXROW) - (sDest % MAXROW);
// apply Pythagoras's theorem for right-handed triangle:
@@ -1234,30 +1197,6 @@ INT16 ExtQuickestDirection(INT16 origin, INT16 dest)
}
// Returns the (center ) cell coordinates in X
INT16 CenterX( INT32 sGridNo )
{
INT32 sYPos, sXPos;
sYPos = sGridNo / WORLD_COLS;
sXPos = ( sGridNo - ( sYPos * WORLD_COLS ) );
return( ( sXPos * CELL_X_SIZE ) + ( CELL_X_SIZE / 2 ) );
}
// Returns the (center ) cell coordinates in Y
INT16 CenterY( INT32 sGridNo )
{
INT32 sYPos, sXPos;
sYPos = sGridNo / WORLD_COLS;
sXPos = ( sGridNo - ( sYPos * WORLD_COLS ) );
return( ( sYPos * CELL_Y_SIZE ) + ( CELL_Y_SIZE / 2 ) );
}
INT16 MapX( INT32 sGridNo )
{
INT32 sYPos, sXPos;
+2 -11
View File
@@ -45,7 +45,7 @@ extern UINT8 gPurpendicularDirection[ NUM_WORLD_DIRECTIONS ][ NUM_WORLD_DIRECTIO
void ConvertDirectionToVectorInXY(UINT8 ubDirection, INT16* sXDir, INT16* sYDir);
void ConvertGridNoToXY( INT32 sGridNo, INT16 *sXPos, INT16 *sYPos );
void ConvertGridNoToCellXY( INT32 sGridNo, INT16 *sXPos, INT16 *sYPos );
void ConvertGridNoToCenterCellXY( INT32 sGridNo, INT16 *sXPos, INT16 *sYPos );
void ConvertGridNoToCenterCellXY( const INT32 sGridNo, INT16 *sXPos, INT16 *sYPos );
// GRID NO MANIPULATION FUNCTIONS
@@ -88,11 +88,8 @@ BOOLEAN GridNoOnVisibleWorldTile( INT32 sGridNo );
BOOLEAN GridNoOnVisibleWorldTileGivenYLimits( INT32 sGridNo );
BOOLEAN GridNoOnEdgeOfMap( INT32 sGridNo, INT8 * pbDirection );
BOOLEAN ConvertMapPosToWorldTileCenter( INT32 usMapPos, INT16 *psXPos, INT16 *psYPos );
BOOLEAN CellXYToScreenXY(INT16 sCellX, INT16 sCellY, INT16 *sScreenX, INT16 *sScreenY);
INT32 GetRangeFromGridNoDiff( INT32 sGridNo1, INT32 sGridNo2 );
INT32 GetRangeInCellCoordsFromGridNoDiff( INT32 sGridNo1, INT32 sGridNo2 );
BOOLEAN IsPointInScreenRect( INT16 sXPos, INT16 sYPos, SGPRect *pRect );
@@ -110,12 +107,6 @@ INT16 QuickestDirection(INT16 origin, INT16 dest);
INT16 ExtQuickestDirection(INT16 origin, INT16 dest);
// Returns the (center ) cell coordinates in X
INT16 CenterX( INT32 sGridNo );
// Returns the (center ) cell coordinates in Y
INT16 CenterY( INT32 sGridNo );
INT16 MapX( INT32 sGridNo );
INT16 MapY( INT32 sGridNo );
BOOLEAN FindFenceJumpDirection( SOLDIERTYPE *pSoldier, INT32 sGridNo, INT8 bStartingDir, INT8 *pbDirection );
@@ -157,4 +148,4 @@ BOOLEAN FindWindowJumpDirection( SOLDIERTYPE *pSoldier, INT32 sGridNo, INT8 bSta
// Flugente: is this gridno near a player merc?
BOOLEAN GridNoNearPlayerMercs( INT32 sGridNo, INT16 sRadius );
#endif
#endif
+4 -1
View File
@@ -84,7 +84,10 @@ void UpdateLightingSprite( LIGHTEFFECT *pLight )
LightSpritePower( pLight->iLight, TRUE );
// LightSpriteFake( pLight->iLight );
LightSpritePosition( pLight->iLight, (INT16)( CenterX( pLight->sGridNo ) / CELL_X_SIZE ), (INT16)( CenterY( pLight->sGridNo ) / CELL_Y_SIZE ) );
INT16 sX, sY;
ConvertGridNoToCenterCellXY(pLight->sGridNo, &sX, &sY);
LightSpritePosition( pLight->iLight, (INT16)( sX / CELL_X_SIZE ), (INT16)( sY / CELL_Y_SIZE ) );
}
// Flugente: create a pure light, worry about updating sight in other functions
+5 -3
View File
@@ -534,9 +534,11 @@ void AddSmokeEffectToTile( INT32 iSmokeEffectID, INT8 bType, INT32 sGridNo, INT8
AniParams.uiFlags = ANITILE_CACHEDTILE | ANITILE_FORWARD | ANITILE_SMOKE_EFFECT | ANITILE_LOOPING | ANITILE_ALWAYS_TRANSLUCENT;
}
AniParams.sX = CenterX( sGridNo );
AniParams.sY = CenterY( sGridNo );
AniParams.sZ = (INT16)0;
INT16 sX, sY;
ConvertGridNoToCenterCellXY(sGridNo, &sX, &sY);
AniParams.sX = sX;
AniParams.sY = sY;
AniParams.sZ = (INT16)0;
// Use the right graphic based on type..
switch( bType )
+1 -3
View File
@@ -1093,7 +1093,6 @@ void AddSnakeAmim( INT32 sGridno, UINT8 usDirection )
if ( !TileIsOutOfBounds( sGridno ) )
{
ANITILE_PARAMS AniParams;
memset( &AniParams, 0, sizeof(ANITILE_PARAMS) );
AniParams.sGridNo = sGridno;
@@ -1101,8 +1100,7 @@ void AddSnakeAmim( INT32 sGridno, UINT8 usDirection )
AniParams.sDelay = 100;
AniParams.sStartFrame = 0;
AniParams.uiFlags = ANITILE_CACHEDTILE | ANITILE_FORWARD | ANITILE_USE_DIRECTION_FOR_START_FRAME;//| ANITILE_LOOPING;
AniParams.sX = CenterX( sGridno );
AniParams.sY = CenterY( sGridno );
ConvertGridNoToCenterCellXY(sGridno, &AniParams.sX, &AniParams.sY);
AniParams.sZ = 0;
strcpy( AniParams.zCachedFile, "TILECACHE\\WATERSNAKE_MOVE.sti" );
+1 -1
View File
@@ -1995,7 +1995,7 @@ void CalculateLaunchItemBasicParams( SOLDIERTYPE *pSoldier, OBJECTTYPE *pItem, I
{
// bad news - i can't throw item at myself
// so use dir incrementer
UINT8 ubDir = atan8( CenterX(pSoldier->sGridNo), CenterY(pSoldier->sGridNo), CenterX(sGridNo), CenterY(sGridNo) );
UINT8 ubDir = GetDirectionFromCenterCellXYGridNo(pSoldier->sGridNo, sGridNo);
sInterGridNo += DirIncrementer[ubDir];
}
}
+1 -1
View File
@@ -614,7 +614,7 @@ void ShowRiotShield( SOLDIERTYPE* pSoldier, UINT16 *pBuffer, UINT32 uiDestPitchB
// try to keep the shield 'moving' alongside the soldier. This won't work perfectly, but it's better than nothing
INT16 base_x = 0;
INT16 base_y = 0;
ConvertMapPosToWorldTileCenter( pSoldier->sGridNo, &base_x, &base_y );
ConvertGridNoToCenterCellXY( pSoldier->sGridNo, &base_x, &base_y );
INT16 dx = pSoldier->sX - base_x;
INT16 dy = pSoldier->sY - base_y;