mirror of
https://github.com/1dot13/source.git
synced 2026-08-12 14:10:23 +02:00
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:
+50
-27
@@ -1714,7 +1714,7 @@ void GetTargetWorldPositions( SOLDIERTYPE *pSoldier, INT32 sTargetGridNo, FLOAT
|
||||
FLOAT dTargetZ;
|
||||
SOLDIERTYPE* pTargetSoldier = NULL;
|
||||
INT8 bStructHeight;
|
||||
INT16 sXMapPos, sYMapPos;
|
||||
INT16 sXMapPos, sYMapPos, sX, sY;
|
||||
UINT32 uiRoll;
|
||||
INT8 bTargetCubeLevel = 0;
|
||||
INT8 bTargetLevel = 0;
|
||||
@@ -1731,8 +1731,9 @@ void GetTargetWorldPositions( SOLDIERTYPE *pSoldier, INT32 sTargetGridNo, FLOAT
|
||||
{
|
||||
// SAVE OPP ID
|
||||
pSoldier->ubOppNum = pTargetSoldier->ubID;
|
||||
dTargetX = (FLOAT) CenterX( pTargetSoldier->sGridNo );
|
||||
dTargetY = (FLOAT) CenterY( pTargetSoldier->sGridNo );
|
||||
ConvertGridNoToCenterCellXY(pTargetSoldier->sGridNo, &sX, &sY);
|
||||
dTargetX = (FLOAT) sX;
|
||||
dTargetY = (FLOAT) sY;
|
||||
if (pSoldier->bAimShotLocation == AIM_SHOT_RANDOM)
|
||||
{
|
||||
uiRoll = PreRandom( 100 );
|
||||
@@ -1773,9 +1774,11 @@ void GetTargetWorldPositions( SOLDIERTYPE *pSoldier, INT32 sTargetGridNo, FLOAT
|
||||
if ( gGameExternalOptions.fAllowTargetHeadAndLegIfProne && gAnimControl[pTargetSoldier->usAnimState].ubHeight == ANIM_PRONE )
|
||||
{
|
||||
INT32 viewdirectiongridno = NewGridNo( pTargetSoldier->sGridNo, DirectionInc( pTargetSoldier->ubDirection ) );
|
||||
INT16 sX, sY;
|
||||
ConvertGridNoToCenterCellXY(viewdirectiongridno, &sX, &sY);
|
||||
|
||||
dTargetX = (0.3f * dTargetX + 0.7f * (FLOAT)CenterX( viewdirectiongridno )) / 1.0f;
|
||||
dTargetY = (0.3f * dTargetY + 0.7f * (FLOAT)CenterY( viewdirectiongridno )) / 1.0f;
|
||||
dTargetX = (0.3f * dTargetX + 0.7f * (FLOAT)sX) / 1.0f;
|
||||
dTargetY = (0.3f * dTargetY + 0.7f * (FLOAT)sY) / 1.0f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -1789,9 +1792,11 @@ void GetTargetWorldPositions( SOLDIERTYPE *pSoldier, INT32 sTargetGridNo, FLOAT
|
||||
if ( gGameExternalOptions.fAllowTargetHeadAndLegIfProne && gAnimControl[pTargetSoldier->usAnimState].ubHeight == ANIM_PRONE )
|
||||
{
|
||||
INT32 viewdirectiongridno = NewGridNo( pTargetSoldier->sGridNo, DirectionInc( gOppositeDirection[pTargetSoldier->ubDirection] ) );
|
||||
INT16 sX, sY;
|
||||
ConvertGridNoToCenterCellXY(viewdirectiongridno, &sX, &sY);
|
||||
|
||||
dTargetX = (0.3f * dTargetX + 0.7f * (FLOAT)CenterX( viewdirectiongridno )) / 1.0f;
|
||||
dTargetY = (0.3f * dTargetY + 0.7f * (FLOAT)CenterY( viewdirectiongridno )) / 1.0f;
|
||||
dTargetX = (0.3f * dTargetX + 0.7f * (FLOAT)sX) / 1.0f;
|
||||
dTargetY = (0.3f * dTargetY + 0.7f * (FLOAT)sY) / 1.0f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -2718,11 +2723,14 @@ BOOLEAN UseGunNCTH( SOLDIERTYPE *pSoldier , INT32 sTargetGridNo )
|
||||
{
|
||||
if ( WillExplosiveWeaponFail( pSoldier, pObjHand ) )
|
||||
{
|
||||
INT16 sX, sY;
|
||||
ConvertGridNoToCenterCellXY(pSoldier->sGridNo, &sX, &sY);
|
||||
|
||||
if ( Item[usUBItem].singleshotrocketlauncher )
|
||||
{
|
||||
CreateItem( Item[usItemNum].discardedlauncheritem , (*pObjHand)[0]->data.objectStatus, pObjHand );
|
||||
|
||||
// Flugente: why would we keep a piece of scrap in our ahnds in the first place? just drop it to the ground
|
||||
// Flugente: why would we keep a piece of scrap in our hands in the first place? just drop it to the ground
|
||||
AddItemToPool( pSoldier->sGridNo, pObjHand, 1, pSoldier->pathing.bLevel, 0, -1 );
|
||||
|
||||
// Delete object
|
||||
@@ -2730,20 +2738,21 @@ BOOLEAN UseGunNCTH( SOLDIERTYPE *pSoldier , INT32 sTargetGridNo )
|
||||
|
||||
DirtyMercPanelInterface( pSoldier, DIRTYLEVEL2 );
|
||||
|
||||
|
||||
if ( Item[usUBItem].usBuddyItem != 0 && Item[Item[usUBItem].usBuddyItem].usItemClass & IC_EXPLOSV )
|
||||
{
|
||||
IgniteExplosion( pSoldier->ubID, CenterX( pSoldier->sGridNo ), CenterY( pSoldier->sGridNo ), 0, pSoldier->sGridNo, Item[usUBItem].usBuddyItem, pSoldier->pathing.bLevel );
|
||||
IgniteExplosion( pSoldier->ubID, sX, sY, 0, pSoldier->sGridNo, Item[usUBItem].usBuddyItem, pSoldier->pathing.bLevel );
|
||||
}
|
||||
else
|
||||
{
|
||||
IgniteExplosion( pSoldier->ubID, CenterX( pSoldier->sGridNo ), CenterY( pSoldier->sGridNo ), 0, pSoldier->sGridNo, C1, pSoldier->pathing.bLevel );
|
||||
IgniteExplosion( pSoldier->ubID, sX, sY, 0, pSoldier->sGridNo, C1, pSoldier->pathing.bLevel );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String("StructureHit: RPG7 item: %d, Ammo: %d", usUBItem , (*pObjHand)[0]->data.gun.usGunAmmoItem ) );
|
||||
|
||||
IgniteExplosion( pSoldier->ubID, CenterX( pSoldier->sGridNo ), CenterY( pSoldier->sGridNo ), 0, pSoldier->sGridNo, (*pObjHand)[0]->data.gun.usGunAmmoItem, pSoldier->pathing.bLevel );
|
||||
IgniteExplosion( pSoldier->ubID, sX, sY, 0, pSoldier->sGridNo, (*pObjHand)[0]->data.gun.usGunAmmoItem, pSoldier->pathing.bLevel );
|
||||
pSoldier->inv[pSoldier->ubAttackingHand ][0]->data.gun.usGunAmmoItem = NONE;
|
||||
}
|
||||
// Reduce again for attack end 'cause it has been incremented for a normal attack
|
||||
@@ -2893,8 +2902,8 @@ BOOLEAN UseGunNCTH( SOLDIERTYPE *pSoldier , INT32 sTargetGridNo )
|
||||
{
|
||||
sTempGridNo = NewGridNo(sTempGridNo, DirectionInc(pSoldier->ubDirection));
|
||||
}
|
||||
INT16 sX = CenterX(sTempGridNo);
|
||||
INT16 sY = CenterY(sTempGridNo);
|
||||
INT16 sX, sY;
|
||||
ConvertGridNoToCenterCellXY(sTempGridNo, &sX, &sY);
|
||||
|
||||
AniParams.sGridNo = pSoldier->sGridNo;
|
||||
AniParams.ubLevelID = ANI_TOPMOST_LEVEL;
|
||||
@@ -3643,6 +3652,9 @@ BOOLEAN UseGun( SOLDIERTYPE *pSoldier , INT32 sTargetGridNo )
|
||||
{
|
||||
if ( WillExplosiveWeaponFail( pSoldier, pObjUsed ) )
|
||||
{
|
||||
INT16 sX, sY;
|
||||
ConvertGridNoToCenterCellXY(pSoldier->sGridNo, &sX, &sY);
|
||||
|
||||
if ( Item[usUBItem].singleshotrocketlauncher )
|
||||
{
|
||||
CreateItem( Item[usUBItem].discardedlauncheritem , (*pObjUsed)[0]->data.objectStatus, pObjUsed );
|
||||
@@ -3655,20 +3667,21 @@ BOOLEAN UseGun( SOLDIERTYPE *pSoldier , INT32 sTargetGridNo )
|
||||
|
||||
DirtyMercPanelInterface( pSoldier, DIRTYLEVEL2 );
|
||||
|
||||
|
||||
if ( Item[usUBItem].usBuddyItem != 0 && Item[Item[usUBItem].usBuddyItem].usItemClass & IC_EXPLOSV )
|
||||
{
|
||||
IgniteExplosion( pSoldier->ubID, CenterX( pSoldier->sGridNo ), CenterY( pSoldier->sGridNo ), 0, pSoldier->sGridNo, C1, pSoldier->pathing.bLevel );
|
||||
IgniteExplosion( pSoldier->ubID, sX, sY, 0, pSoldier->sGridNo, C1, pSoldier->pathing.bLevel );
|
||||
}
|
||||
else
|
||||
{
|
||||
IgniteExplosion( pSoldier->ubID, CenterX( pSoldier->sGridNo ), CenterY( pSoldier->sGridNo ), 0, pSoldier->sGridNo, C1, pSoldier->pathing.bLevel );
|
||||
IgniteExplosion( pSoldier->ubID, sX, sY, 0, pSoldier->sGridNo, C1, pSoldier->pathing.bLevel );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String("StructureHit: RPG7 item: %d, Ammo: %d", usUBItem , (*pObjUsed)[0]->data.gun.usGunAmmoItem ) );
|
||||
|
||||
IgniteExplosion( pSoldier->ubID, CenterX( pSoldier->sGridNo ), CenterY( pSoldier->sGridNo ), 0, pSoldier->sGridNo, (*pObjUsed)[0]->data.gun.usGunAmmoItem, pSoldier->pathing.bLevel );
|
||||
IgniteExplosion( pSoldier->ubID, sX, sY, 0, pSoldier->sGridNo, (*pObjUsed)[0]->data.gun.usGunAmmoItem, pSoldier->pathing.bLevel );
|
||||
|
||||
OBJECTTYPE * pLaunchable = FindLaunchableAttachment( pObjUsed, usUBItem );
|
||||
if(pLaunchable)
|
||||
@@ -3708,8 +3721,8 @@ BOOLEAN UseGun( SOLDIERTYPE *pSoldier , INT32 sTargetGridNo )
|
||||
{
|
||||
sTempGridNo = NewGridNo(sTempGridNo, DirectionInc(pSoldier->ubDirection));
|
||||
}
|
||||
INT16 sX = CenterX(sTempGridNo);
|
||||
INT16 sY = CenterY(sTempGridNo);
|
||||
INT16 sX, sY;
|
||||
ConvertGridNoToCenterCellXY(sTempGridNo, &sX, &sY);
|
||||
|
||||
AniParams.sGridNo = pSoldier->sGridNo;
|
||||
AniParams.ubLevelID = ANI_TOPMOST_LEVEL;
|
||||
@@ -5167,7 +5180,9 @@ BOOLEAN UseLauncher( SOLDIERTYPE *pSoldier, INT32 sTargetGridNo )
|
||||
|
||||
// So we still should have ABC > 0
|
||||
// Begin explosion due to failure...
|
||||
IgniteExplosion( pSoldier->ubID, CenterX( pSoldier->sGridNo ), CenterY( pSoldier->sGridNo ), 0, pSoldier->sGridNo, Launchable.usItem, pSoldier->pathing.bLevel );
|
||||
INT16 sX, sY;
|
||||
ConvertGridNoToCenterCellXY(pSoldier->sGridNo, &sX, &sY);
|
||||
IgniteExplosion( pSoldier->ubID, sX, sY, 0, pSoldier->sGridNo, Launchable.usItem, pSoldier->pathing.bLevel );
|
||||
|
||||
// Reduce again for attack end 'cause it has been incremented for a normal attack
|
||||
// Nope, not anymore.
|
||||
@@ -5676,15 +5691,18 @@ void StructureHit( INT32 iBullet, UINT16 usWeaponIndex, INT16 bWeaponStatus, UIN
|
||||
// HEADROCK HAM 5: Fragments fired by such weapons should not explode.
|
||||
if ( pBullet->fFragment == false)
|
||||
{
|
||||
INT16 sX, sY;
|
||||
ConvertGridNoToCenterCellXY(pSoldier->sGridNo, &sX, &sY);
|
||||
|
||||
if ( Item[usWeaponIndex].singleshotrocketlauncher )
|
||||
{
|
||||
if ( Item[usWeaponIndex].usBuddyItem != 0 && Item[Item[usWeaponIndex].usBuddyItem].usItemClass & IC_EXPLOSV )
|
||||
{
|
||||
IgniteExplosion( ubAttackerID, CenterX( sGridNo ), CenterY( sGridNo ), 0, sGridNo, Item[usWeaponIndex].usBuddyItem, bLevel, usDirection );
|
||||
IgniteExplosion( ubAttackerID, sX, sY, 0, sGridNo, Item[usWeaponIndex].usBuddyItem, bLevel, usDirection );
|
||||
}
|
||||
else
|
||||
{
|
||||
IgniteExplosion( ubAttackerID, CenterX( sGridNo ), CenterY( sGridNo ), 0, sGridNo, C1, bLevel, usDirection );
|
||||
IgniteExplosion( ubAttackerID, sX, sY, 0, sGridNo, C1, bLevel, usDirection );
|
||||
}
|
||||
}
|
||||
// changed too to use 2 flag to determine
|
||||
@@ -5692,7 +5710,7 @@ void StructureHit( INT32 iBullet, UINT16 usWeaponIndex, INT16 bWeaponStatus, UIN
|
||||
//there shouldn't be a way to enter here with an UnderBarrel weapon, so retaining original code :JMich
|
||||
{
|
||||
DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String("StructureHit: RPG7 item: %d, Ammo: %d",pAttacker->inv[HANDPOS].usItem , pAttacker->inv[HANDPOS][0]->data.gun.usGunAmmoItem ) );
|
||||
IgniteExplosion( ubAttackerID, CenterX( sGridNo ), CenterY( sGridNo ), 0, sGridNo, pAttacker->inv[pAttacker->ubAttackingHand][0]->data.gun.usGunAmmoItem, bLevel, usDirection );
|
||||
IgniteExplosion( ubAttackerID, sX, sY, 0, sGridNo, pAttacker->inv[pAttacker->ubAttackingHand][0]->data.gun.usGunAmmoItem, bLevel, usDirection );
|
||||
|
||||
//This is just to make multishot launchers work in semi auto. It's not really a permanent solution because it still doesn't allow autofire, but it will do for now.
|
||||
OBJECTTYPE * pLaunchable = FindLaunchableAttachment( &(pAttacker->inv[pAttacker->ubAttackingHand ]), pAttacker->inv[pAttacker->ubAttackingHand ].usItem );
|
||||
@@ -5708,7 +5726,7 @@ void StructureHit( INT32 iBullet, UINT16 usWeaponIndex, INT16 bWeaponStatus, UIN
|
||||
else if ( AmmoTypes[(*pObj)[0]->data.gun.ubGunAmmoType].explosionSize > 1)
|
||||
{
|
||||
// re-routed the Highexplosive value to define exposion type
|
||||
IgniteExplosion( ubAttackerID, CenterX( sGridNo ), CenterY( sGridNo ), 0, sGridNo, AmmoTypes[(*pObj)[0]->data.gun.ubGunAmmoType].highExplosive, bLevel, usDirection );
|
||||
IgniteExplosion( ubAttackerID, sX, sY, 0, sGridNo, AmmoTypes[(*pObj)[0]->data.gun.ubGunAmmoType].highExplosive, bLevel, usDirection );
|
||||
// pSoldier->inv[pSoldier->ubAttackingHand ][0]->data.gun.usGunAmmoItem = NONE;
|
||||
}
|
||||
}
|
||||
@@ -5726,7 +5744,9 @@ void StructureHit( INT32 iBullet, UINT16 usWeaponIndex, INT16 bWeaponStatus, UIN
|
||||
//FreeUpAttacker( ubAttackerID );
|
||||
|
||||
// HEADROCK HAM 5 TODO: Tank shell!!
|
||||
IgniteExplosion( ubAttackerID, CenterX( sGridNo ), CenterY( sGridNo ), 0, sGridNo, TANK_SHELL, bLevel, usDirection );
|
||||
INT16 sX, sY;
|
||||
ConvertGridNoToCenterCellXY(sGridNo, &sX, &sY);
|
||||
IgniteExplosion( ubAttackerID, sX, sY, 0, sGridNo, TANK_SHELL, bLevel, usDirection );
|
||||
//FreeUpAttacker( (UINT8) ubAttackerID );
|
||||
|
||||
// Moved here to keep ABC >0 as long as possible
|
||||
@@ -6250,7 +6270,7 @@ UINT32 CalcNewChanceToHitGun(SOLDIERTYPE *pSoldier, INT32 sGridNo, INT16 ubAimTi
|
||||
// Basic defines
|
||||
SOLDIERTYPE * pTarget;
|
||||
INT32 iRange, iSightRange; //, minRange;
|
||||
|
||||
INT16 sX, sY, sX2, sY2;
|
||||
UINT16 usInHand;
|
||||
OBJECTTYPE * pInHand;
|
||||
INT16 sDistVis, sDistVisNoScope;
|
||||
@@ -6270,8 +6290,11 @@ UINT32 CalcNewChanceToHitGun(SOLDIERTYPE *pSoldier, INT32 sGridNo, INT16 ubAimTi
|
||||
|
||||
// calculate actual range (in units, 10 units = 1 tile)
|
||||
iRange = GetRangeInCellCoordsFromGridNoDiff( pSoldier->sGridNo, sGridNo );
|
||||
FLOAT dDeltaX = (FLOAT)(CenterX( pSoldier->sGridNo ) - CenterX( sGridNo ));
|
||||
FLOAT dDeltaY = (FLOAT)(CenterY( pSoldier->sGridNo ) - CenterY( sGridNo ));
|
||||
|
||||
ConvertGridNoToCenterCellXY(pSoldier->sGridNo, &sX, &sY);
|
||||
ConvertGridNoToCenterCellXY(sGridNo, &sX2, &sY2);
|
||||
FLOAT dDeltaX = (FLOAT)( sX - sX2 );
|
||||
FLOAT dDeltaY = (FLOAT)( sY - sY2 );
|
||||
FLOAT d2DDistance = sqrt((dDeltaX*dDeltaX)+(dDeltaY*dDeltaY));
|
||||
|
||||
// Find a target in the tile
|
||||
|
||||
Reference in New Issue
Block a user