Major (but partly) cleanup of actionUpdateDroid:

* Move variables into a more local scope
  - I don't think I screwed up start-at-block-declaration rules for MSVC, but given the size of these changes I can't be sure.
 * Remove a large amount of low level bit manipulation and replace it with arrays of boolean values instead
  - Yes bit shift and bitwise and where used; bitwise or was replaced with addition though
 * Don't use *WORD/*BYTE when we don't need the size guarantee (and the size guarantee is in fact an optimisation prone to introduce bugs)
 * "Concatenate" nested if statements into a single condition in a single if-statement
  - Less nested code to convey exactly the same meaning
 * Remove unused variable moveAction
  - It was only ever written to, but never read from

We don't need the size guarantee for WEAPON::nStat, so substitute "unsigned int" for "UDWORD"

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@6634 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2009-02-11 22:00:47 +00:00
parent 29bb57948c
commit 0711b342ed
2 changed files with 153 additions and 154 deletions

View File

@ -995,28 +995,17 @@ static void actionHomeBasePos(SDWORD player, SDWORD *px, SDWORD *py)
// Update the action state for a droid // Update the action state for a droid
void actionUpdateDroid(DROID *psDroid) void actionUpdateDroid(DROID *psDroid)
{ {
UDWORD droidX,droidY;
UDWORD tlx,tly;
STRUCTURE *psStruct;
STRUCTURE_STATS *psStructStats;
BASE_OBJECT *psTarget; BASE_OBJECT *psTarget;
WEAPON_STATS *psWeapStats;
SDWORD targetDir, dirDiff, pbx,pby;
SDWORD xdiff,ydiff, rangeSq;
SECONDARY_STATE state;
PROPULSION_STATS *psPropStats; PROPULSION_STATS *psPropStats;
BOOL bChaseBloke;
FEATURE *psNextWreck;
BOOL (*actionUpdateFunc)(DROID *psDroid) = NULL; BOOL (*actionUpdateFunc)(DROID *psDroid) = NULL;
SDWORD moveAction; signed int i;
BOOL bDoHelpBuild; unsigned int j;
MAPTILE *psTile;
UBYTE i = 0;
//this is a bit field //this is a bit field
UBYTE num_weapons = 0; bool nonNullWeapon[DROID_MAXWEAPS] = { false };
BASE_OBJECT *psTargets[DROID_MAXWEAPS]; BASE_OBJECT *psTargets[DROID_MAXWEAPS];
UBYTE j,iVisible = 1; bool hasVisibleTarget = false;
BOOL bHasTarget; bool targetVisibile[DROID_MAXWEAPS] = { false };
bool bHasTarget;
CHECK_DROID(psDroid); CHECK_DROID(psDroid);
@ -1064,32 +1053,21 @@ void actionUpdateDroid(DROID *psDroid)
} }
} }
if (psDroid->numWeaps > 1) for (i = 0; i < psDroid->numWeaps; ++i)
{
for(i = 0;i < psDroid->numWeaps;i++)
{ {
if (psDroid->asWeaps[i].nStat > 0) if (psDroid->asWeaps[i].nStat > 0)
{ {
num_weapons += (1 << (i+1)); nonNullWeapon[i] = true;
} }
} }
}
else // HACK: Apparently we can't deal with a droid that only has NULL weapons ?
{ // FIXME: Find out whether this is really necessary
num_weapons = 2; if (psDroid->numWeaps <= 1)
} nonNullWeapon[0] = true;
psTarget = psDroid->psTarget; psTarget = psDroid->psTarget;
if (psDroid->asWeaps[0].nStat > 0)
{
psWeapStats = asWeaponStats + psDroid->asWeaps[0].nStat;
}
else
{
psWeapStats = NULL;
}
switch (psDroid->action) switch (psDroid->action)
{ {
case DACTION_NONE: case DACTION_NONE:
@ -1127,6 +1105,8 @@ void actionUpdateDroid(DROID *psDroid)
{ {
if ((SDWORD)(mission.ETA - (gameTime - missionGetReinforcementTime())) <= 0) if ((SDWORD)(mission.ETA - (gameTime - missionGetReinforcementTime())) <= 0)
{ {
UDWORD droidX, droidY;
if (!droidRemove(psDroid, mission.apsDroidLists)) if (!droidRemove(psDroid, mission.apsDroidLists))
{ {
ASSERT( false, "actionUpdate: Unable to remove transporter from mission list" ); ASSERT( false, "actionUpdate: Unable to remove transporter from mission list" );
@ -1175,19 +1155,21 @@ void actionUpdateDroid(DROID *psDroid)
{ {
for(i = 0;i < psDroid->numWeaps;i++) for(i = 0;i < psDroid->numWeaps;i++)
{ {
if ( (num_weapons & (1 << (i+1))) ) if (nonNullWeapon[i])
{ {
BASE_OBJECT *psTemp = NULL; BASE_OBJECT *psTemp = NULL;
//Watermelon:I moved psWeapStats flag update there //Watermelon:I moved psWeapStats flag update there
psWeapStats = asWeaponStats + psDroid->asWeaps[i].nStat; WEAPON_STATS* const psWeapStats = &asWeaponStats[psDroid->asWeaps[i].nStat];
if (!isVtolDroid(psDroid) && if (!isVtolDroid(psDroid)
psDroid->asWeaps[i].nStat > 0 && && psDroid->asWeaps[i].nStat > 0
psWeapStats->rotate && && psWeapStats->rotate
psWeapStats->fireOnMove != FOM_NO && && psWeapStats->fireOnMove != FOM_NO
CAN_UPDATE_NAYBORS(psDroid) && && CAN_UPDATE_NAYBORS(psDroid)
(aiBestNearestTarget(psDroid, &psTemp, i) >= 0)) && aiBestNearestTarget(psDroid, &psTemp, i) >= 0)
{ {
SECONDARY_STATE state;
if (secondaryGetState(psDroid, DSO_ATTACK_LEVEL, &state)) if (secondaryGetState(psDroid, DSO_ATTACK_LEVEL, &state))
{ {
if (state == DSS_ALEV_ALWAYS) if (state == DSS_ALEV_ALWAYS)
@ -1226,7 +1208,7 @@ void actionUpdateDroid(DROID *psDroid)
bHasTarget = false; bHasTarget = false;
//loop through weapons and look for target for each weapon //loop through weapons and look for target for each weapon
for (i = 0;i < psDroid->numWeaps;i++) for (i = 0; i < psDroid->numWeaps; ++i)
{ {
if (psDroid->psActionTarget[i] == NULL) if (psDroid->psActionTarget[i] == NULL)
{ {
@ -1239,10 +1221,11 @@ void actionUpdateDroid(DROID *psDroid)
} }
} }
if (psDroid->psActionTarget[i] && if (psDroid->psActionTarget[i]
visibleObject((BASE_OBJECT*)psDroid, psDroid->psActionTarget[i], false)) && visibleObject((BASE_OBJECT*)psDroid, psDroid->psActionTarget[i], false))
{ {
iVisible += (1 << (i+1)); hasVisibleTarget = true;
targetVisibile[i] = true;
} }
} }
@ -1252,6 +1235,8 @@ void actionUpdateDroid(DROID *psDroid)
if (psDroid->psActionTarget[j] != NULL && if (psDroid->psActionTarget[j] != NULL &&
validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[j], j)) validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[j], j))
{ {
SECONDARY_STATE state;
// firing on something while moving // firing on something while moving
if (DROID_STOPPED(psDroid)) if (DROID_STOPPED(psDroid))
{ {
@ -1259,9 +1244,10 @@ void actionUpdateDroid(DROID *psDroid)
psDroid->action = DACTION_NONE; psDroid->action = DACTION_NONE;
break; break;
} }
else if ((psDroid->psActionTarget[j] == NULL) || else if (psDroid->psActionTarget[j] == NULL
!validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[j], j) || || !validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[j], j)
(secondaryGetState(psDroid, DSO_ATTACK_LEVEL, &state) && (state != DSS_ALEV_ALWAYS))) || (secondaryGetState(psDroid, DSO_ATTACK_LEVEL, &state)
&& state != DSS_ALEV_ALWAYS))
{ {
if (j == (psDroid->numWeaps - 1) && !bHasTarget) if (j == (psDroid->numWeaps - 1) && !bHasTarget)
{ {
@ -1287,23 +1273,23 @@ void actionUpdateDroid(DROID *psDroid)
} }
else else
{ {
if (iVisible == 1 && !bHasTarget && j == (psDroid->numWeaps - 1)) if (!hasVisibleTarget
&& !bHasTarget
&& j == (psDroid->numWeaps - 1))
{ {
// lost the target // lost the target
psDroid->action = DACTION_MOVE; psDroid->action = DACTION_MOVE;
for (i = 0;i < psDroid->numWeaps;i++) for (i = 0; i < psDroid->numWeaps;i++)
{ {
setDroidActionTarget(psDroid, NULL, i); setDroidActionTarget(psDroid, NULL, i);
} }
} }
if (iVisible & (1 << (j+1))) if (targetVisibile[j])
{ {
bHasTarget = true; bHasTarget = true;
//Watermelon:I moved psWeapStats flag update there
psWeapStats = asWeaponStats + psDroid->asWeaps[j].nStat;
//Watermelon:to fix a AA-weapon attack ground unit exploit //Watermelon:to fix a AA-weapon attack ground unit exploit
if ( (num_weapons & (1 << (j+1))) ) if (nonNullWeapon[j])
{ {
BASE_OBJECT* psActionTarget; BASE_OBJECT* psActionTarget;
@ -1316,20 +1302,16 @@ void actionUpdateDroid(DROID *psDroid)
psActionTarget = psDroid->psActionTarget[0]; psActionTarget = psDroid->psActionTarget[0];
} }
if (validTarget((BASE_OBJECT *)psDroid, psActionTarget, j)) if (validTarget((BASE_OBJECT *)psDroid, psActionTarget, j)
{ && actionTargetTurret((BASE_OBJECT*)psDroid, psActionTarget, &psDroid->asWeaps[j]))
if (actionTargetTurret((BASE_OBJECT*)psDroid, psActionTarget, &psDroid->asWeaps[j]))
{ {
// In range - fire !!! // In range - fire !!!
combFire(&psDroid->asWeaps[j], (BASE_OBJECT *)psDroid, combFire(&psDroid->asWeaps[j], (BASE_OBJECT *)psDroid,
psActionTarget, j); psActionTarget, j);
} }
} else if (targetVisibile[0]
else && validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], j)
{ && actionTargetTurret((BASE_OBJECT*)psDroid, psDroid->psActionTarget[0], &psDroid->asWeaps[j]))
if ((iVisible & 2) && validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], j))
{
if (actionTargetTurret((BASE_OBJECT*)psDroid, psDroid->psActionTarget[0], &psDroid->asWeaps[j]))
{ {
// In range - fire !!! // In range - fire !!!
combFire(&psDroid->asWeaps[j], (BASE_OBJECT *)psDroid, combFire(&psDroid->asWeaps[j], (BASE_OBJECT *)psDroid,
@ -1340,8 +1322,6 @@ void actionUpdateDroid(DROID *psDroid)
} }
} }
} }
}
}
//check its a VTOL unit since adding Transporter's into multiPlayer //check its a VTOL unit since adding Transporter's into multiPlayer
/* check vtol attack runs */ /* check vtol attack runs */
@ -1396,26 +1376,26 @@ void actionUpdateDroid(DROID *psDroid)
psActionTarget = psDroid->psActionTarget[0]; psActionTarget = psDroid->psActionTarget[0];
} }
if ( (num_weapons & (1 << (i+1))) && if (nonNullWeapon[i]
actionVisibleTarget(psDroid, psActionTarget, i) && && actionVisibleTarget(psDroid, psActionTarget, i)
actionInRange(psDroid, psActionTarget, i)) && actionInRange(psDroid, psActionTarget, i))
{ {
bHasTarget = true; bHasTarget = true;
psWeapStats = asWeaponStats + psDroid->asWeaps[i].nStat; WEAPON_STATS* const psWeapStats = &asWeaponStats[psDroid->asWeaps[i].nStat];
if (validTarget((BASE_OBJECT *)psDroid, psActionTarget, i)) if (validTarget((BASE_OBJECT *)psDroid, psActionTarget, i))
{ {
int dirDiff = 0;
if (!psWeapStats->rotate) if (!psWeapStats->rotate)
{ {
// no rotating turret - need to check aligned with target // no rotating turret - need to check aligned with target
targetDir = (SDWORD)calcDirection( const int targetDir = calcDirection(psDroid->pos.x,
psDroid->pos.x,psDroid->pos.y, psDroid->pos.y,
psActionTarget->pos.x, psActionTarget->pos.y); psActionTarget->pos.x,
dirDiff = labs(targetDir - (SDWORD)psDroid->direction); psActionTarget->pos.y);
} dirDiff = labs(targetDir - psDroid->direction);
else
{
dirDiff = 0;
} }
if (dirDiff > FIXED_TURRET_DIR) if (dirDiff > FIXED_TURRET_DIR)
{ {
if (psDroid->sMove.Status != MOVESHUFFLE) if (psDroid->sMove.Status != MOVESHUFFLE)
@ -1436,10 +1416,12 @@ void actionUpdateDroid(DROID *psDroid)
if (!bHasTarget) if (!bHasTarget)
{ {
SECONDARY_STATE state;
if (((psDroid->order == DORDER_ATTACKTARGET if (((psDroid->order == DORDER_ATTACKTARGET
|| psDroid->order == DORDER_FIRESUPPORT) || psDroid->order == DORDER_FIRESUPPORT)
&& secondaryGetState(psDroid, DSO_HALTTYPE, &state) && secondaryGetState(psDroid, DSO_HALTTYPE, &state)
&& (state == DSS_HALT_HOLD)) && state == DSS_HALT_HOLD)
|| (!isVtolDroid(psDroid) || (!isVtolDroid(psDroid)
&& (psTarget = orderStateObj(psDroid, DORDER_FIRESUPPORT)) && (psTarget = orderStateObj(psDroid, DORDER_FIRESUPPORT))
&& psTarget->type == OBJ_STRUCTURE)) && psTarget->type == OBJ_STRUCTURE))
@ -1456,6 +1438,9 @@ void actionUpdateDroid(DROID *psDroid)
break; break;
case DACTION_VTOLATTACK: case DACTION_VTOLATTACK:
{
WEAPON_STATS* psWeapStats = NULL;
//Watermelon:uses vtResult //Watermelon:uses vtResult
if (psDroid->psActionTarget[0] != NULL && if (psDroid->psActionTarget[0] != NULL &&
validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], 0)) validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], 0))
@ -1473,11 +1458,11 @@ void actionUpdateDroid(DROID *psDroid)
for(i = 0;i <psDroid->numWeaps;i++) for(i = 0;i <psDroid->numWeaps;i++)
{ {
if ( (num_weapons & (1 << (i+1))) && if (nonNullWeapon[i]
validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], i) ) && validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], i))
{ {
//Watermelon:I moved psWeapStats flag update there //Watermelon:I moved psWeapStats flag update there
psWeapStats = asWeaponStats + psDroid->asWeaps[i].nStat; psWeapStats = &asWeaponStats[psDroid->asWeaps[i].nStat];
if (actionVisibleTarget(psDroid, psDroid->psActionTarget[0], i)) if (actionVisibleTarget(psDroid, psDroid->psActionTarget[0], i))
{ {
if ( actionInRange(psDroid, psDroid->psActionTarget[0], i) ) if ( actionInRange(psDroid, psDroid->psActionTarget[0], i) )
@ -1521,25 +1506,25 @@ void actionUpdateDroid(DROID *psDroid)
else else
{ {
// if the vtol is close to the target, go around again // if the vtol is close to the target, go around again
xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x; const int xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x;
ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y; const int ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y;
rangeSq = xdiff*xdiff + ydiff*ydiff; const int rangeSq = xdiff * xdiff + ydiff * ydiff;
if (rangeSq < (VTOL_ATTACK_TARDIST*VTOL_ATTACK_TARDIST)) if (rangeSq < (VTOL_ATTACK_TARDIST*VTOL_ATTACK_TARDIST))
{ {
// don't do another attack run if already moving away from the target // don't do another attack run if already moving away from the target
xdiff = (SDWORD)psDroid->sMove.DestinationX - (SDWORD)psDroid->psActionTarget[0]->pos.x; const int xdiff = (SDWORD)psDroid->sMove.DestinationX - (SDWORD)psDroid->psActionTarget[0]->pos.x;
ydiff = (SDWORD)psDroid->sMove.DestinationY - (SDWORD)psDroid->psActionTarget[0]->pos.y; const int ydiff = (SDWORD)psDroid->sMove.DestinationY - (SDWORD)psDroid->psActionTarget[0]->pos.y;
if ((xdiff*xdiff + ydiff*ydiff) < (VTOL_ATTACK_TARDIST*VTOL_ATTACK_TARDIST)) if ((xdiff*xdiff + ydiff*ydiff) < (VTOL_ATTACK_TARDIST*VTOL_ATTACK_TARDIST))
{ {
actionAddVtolAttackRun( psDroid ); actionAddVtolAttackRun( psDroid );
} }
} }
// if the vtol is far enough away head for the target again // if the vtol is far enough away head for the target again
else if (rangeSq > (SDWORD)(psWeapStats->longRange*psWeapStats->longRange)) else if (rangeSq > (SDWORD)(psWeapStats->longRange * psWeapStats->longRange))
{ {
// don't do another attack run if already heading for the target // don't do another attack run if already heading for the target
xdiff = (SDWORD)psDroid->sMove.DestinationX - (SDWORD)psDroid->psActionTarget[0]->pos.x; const int xdiff = (SDWORD)psDroid->sMove.DestinationX - (SDWORD)psDroid->psActionTarget[0]->pos.x;
ydiff = (SDWORD)psDroid->sMove.DestinationY - (SDWORD)psDroid->psActionTarget[0]->pos.y; const int ydiff = (SDWORD)psDroid->sMove.DestinationY - (SDWORD)psDroid->psActionTarget[0]->pos.y;
if ((xdiff*xdiff + ydiff*ydiff) > (VTOL_ATTACK_TARDIST*VTOL_ATTACK_TARDIST)) if ((xdiff*xdiff + ydiff*ydiff) > (VTOL_ATTACK_TARDIST*VTOL_ATTACK_TARDIST))
{ {
moveDroidToDirect(psDroid, psDroid->psActionTarget[0]->pos.x,psDroid->psActionTarget[0]->pos.y); moveDroidToDirect(psDroid, psDroid->psActionTarget[0]->pos.x,psDroid->psActionTarget[0]->pos.y);
@ -1547,7 +1532,7 @@ void actionUpdateDroid(DROID *psDroid)
} }
} }
break; break;
}
case DACTION_MOVETOATTACK: case DACTION_MOVETOATTACK:
// don't wan't formations for this one // don't wan't formations for this one
if (psDroid->sMove.psFormation) if (psDroid->sMove.psFormation)
@ -1582,28 +1567,29 @@ void actionUpdateDroid(DROID *psDroid)
{ {
for(i = 0;i < psDroid->numWeaps;i++) for(i = 0;i < psDroid->numWeaps;i++)
{ {
if( (num_weapons & (1 << (i+1))) && if (nonNullWeapon[i]
validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], i) && && validTarget((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], i)
actionVisibleTarget(psDroid, psDroid->psActionTarget[0], i) ) && actionVisibleTarget(psDroid, psDroid->psActionTarget[0], i))
{ {
psWeapStats = asWeaponStats + psDroid->asWeaps[i].nStat; bool chaseBloke = false;
WEAPON_STATS* const psWeapStats = &asWeaponStats[psDroid->asWeaps[i].nStat];
if (psWeapStats->rotate) if (psWeapStats->rotate)
{ {
actionTargetTurret((BASE_OBJECT*)psDroid, psDroid->psActionTarget[0], &psDroid->asWeaps[i]); actionTargetTurret((BASE_OBJECT*)psDroid, psDroid->psActionTarget[0], &psDroid->asWeaps[i]);
} }
bChaseBloke = false;
if (!isVtolDroid(psDroid) && if (!isVtolDroid(psDroid) &&
psDroid->psActionTarget[0]->type == OBJ_DROID && psDroid->psActionTarget[0]->type == OBJ_DROID &&
((DROID *)psDroid->psActionTarget[0])->droidType == DROID_PERSON && ((DROID *)psDroid->psActionTarget[0])->droidType == DROID_PERSON &&
psWeapStats->fireOnMove != FOM_NO) psWeapStats->fireOnMove != FOM_NO)
{ {
bChaseBloke = true; chaseBloke = true;
} }
if ( (actionInAttackRange(psDroid, psDroid->psActionTarget[0], i)) && if (actionInAttackRange(psDroid, psDroid->psActionTarget[0], i)
!bChaseBloke) && !chaseBloke)
{ {
/* Stop the droid moving any closer */ /* Stop the droid moving any closer */
// ASSERT( psDroid->pos.x != 0 && psDroid->pos.y != 0, // ASSERT( psDroid->pos.x != 0 && psDroid->pos.y != 0,
@ -1656,9 +1642,12 @@ void actionUpdateDroid(DROID *psDroid)
/* Stopped moving but haven't reached the target - possibly move again */ /* Stopped moving but haven't reached the target - possibly move again */
//Watermelon:'hack' to make the droid to check the primary turrent instead of all //Watermelon:'hack' to make the droid to check the primary turrent instead of all
psWeapStats = asWeaponStats + psDroid->asWeaps[0].nStat; WEAPON_STATS* const psWeapStats = &asWeaponStats[psDroid->asWeaps[0].nStat];
SECONDARY_STATE state;
if (psDroid->order == DORDER_ATTACKTARGET && secondaryGetState(psDroid, DSO_HALTTYPE, &state) && (state == DSS_HALT_HOLD)) if (psDroid->order == DORDER_ATTACKTARGET
&& secondaryGetState(psDroid, DSO_HALTTYPE, &state)
&& state == DSS_HALT_HOLD)
{ {
psDroid->action = DACTION_NONE; // on hold, give up. psDroid->action = DACTION_NONE; // on hold, give up.
} }
@ -1666,6 +1655,8 @@ void actionUpdateDroid(DROID *psDroid)
{ {
if ( proj_Direct( psWeapStats ) ) if ( proj_Direct( psWeapStats ) )
{ {
SDWORD pbx, pby;
// try and extend the range // try and extend the range
actionCalcPullBackPoint((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], &pbx,&pby); actionCalcPullBackPoint((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], &pbx,&pby);
moveDroidTo(psDroid, (UDWORD)pbx, (UDWORD)pby); moveDroidTo(psDroid, (UDWORD)pbx, (UDWORD)pby);
@ -1733,16 +1724,16 @@ void actionUpdateDroid(DROID *psDroid)
(SDWORD)psDroid->orderX,(SDWORD)psDroid->orderY, psDroid->psTarStats)) (SDWORD)psDroid->orderX,(SDWORD)psDroid->orderY, psDroid->psTarStats))
{ {
moveStopDroid(psDroid); moveStopDroid(psDroid);
bDoHelpBuild = false; bool helpBuild = false;
// Got to destination - start building // Got to destination - start building
psStructStats = (STRUCTURE_STATS*)psDroid->psTarStats; STRUCTURE_STATS* const psStructStats = (STRUCTURE_STATS*)psDroid->psTarStats;
if (psDroid->order == DORDER_BUILD && psDroid->psTarget == NULL) if (psDroid->order == DORDER_BUILD && psDroid->psTarget == NULL)
{ {
// Starting a new structure // Starting a new structure
// calculate the top left of the structure // calculate the top left of the structure
tlx = (SDWORD)psDroid->orderX - (SDWORD)(psStructStats->baseWidth * TILE_UNITS)/2; const UDWORD tlx = (SDWORD)psDroid->orderX - (SDWORD)(psStructStats->baseWidth * TILE_UNITS)/2;
tly = (SDWORD)psDroid->orderY - (SDWORD)(psStructStats->baseBreadth * TILE_UNITS)/2; const UDWORD tly = (SDWORD)psDroid->orderY - (SDWORD)(psStructStats->baseBreadth * TILE_UNITS)/2;
//need to check if something has already started building here? //need to check if something has already started building here?
//unless its a module! //unless its a module!
@ -1754,12 +1745,12 @@ void actionUpdateDroid(DROID *psDroid)
else if (TileHasStructure(mapTile(map_coord(psDroid->orderX), map_coord(psDroid->orderY)))) else if (TileHasStructure(mapTile(map_coord(psDroid->orderX), map_coord(psDroid->orderY))))
{ {
// structure on the build location - see if it is the same type // structure on the build location - see if it is the same type
psStruct = getTileStructure(map_coord(psDroid->orderX), map_coord(psDroid->orderY)); STRUCTURE* const psStruct = getTileStructure(map_coord(psDroid->orderX), map_coord(psDroid->orderY));
if (psStruct->pStructureType == (STRUCTURE_STATS *)psDroid->psTarStats) if (psStruct->pStructureType == (STRUCTURE_STATS *)psDroid->psTarStats)
{ {
// same type - do a help build // same type - do a help build
setDroidTarget(psDroid, (BASE_OBJECT *)psStruct); setDroidTarget(psDroid, (BASE_OBJECT *)psStruct);
bDoHelpBuild = true; helpBuild = true;
} }
else if ((psStruct->pStructureType->type == REF_WALL || else if ((psStruct->pStructureType->type == REF_WALL ||
psStruct->pStructureType->type == REF_WALLCORNER) && psStruct->pStructureType->type == REF_WALLCORNER) &&
@ -1802,7 +1793,7 @@ void actionUpdateDroid(DROID *psDroid)
psStructStats->type == REF_DEFENSE)) psStructStats->type == REF_DEFENSE))
{ {
// building a wall. // building a wall.
psTile = mapTile(map_coord(psDroid->orderX), map_coord(psDroid->orderY)); MAPTILE* const psTile = mapTile(map_coord(psDroid->orderX), map_coord(psDroid->orderY));
if (psDroid->psTarget == NULL if (psDroid->psTarget == NULL
&& (TileHasStructure(psTile) && (TileHasStructure(psTile)
|| TileHasFeature(psTile))) || TileHasFeature(psTile)))
@ -1810,12 +1801,12 @@ void actionUpdateDroid(DROID *psDroid)
if (TileHasStructure(psTile)) if (TileHasStructure(psTile))
{ {
// structure on the build location - see if it is the same type // structure on the build location - see if it is the same type
psStruct = getTileStructure(map_coord(psDroid->orderX), map_coord(psDroid->orderY)); STRUCTURE* const psStruct = getTileStructure(map_coord(psDroid->orderX), map_coord(psDroid->orderY));
if (psStruct->pStructureType == (STRUCTURE_STATS *)psDroid->psTarStats) if (psStruct->pStructureType == (STRUCTURE_STATS *)psDroid->psTarStats)
{ {
// same type - do a help build // same type - do a help build
setDroidTarget(psDroid, (BASE_OBJECT *)psStruct); setDroidTarget(psDroid, (BASE_OBJECT *)psStruct);
bDoHelpBuild = true; helpBuild = true;
} }
else if ((psStruct->pStructureType->type == REF_WALL || psStruct->pStructureType->type == REF_WALLCORNER) && else if ((psStruct->pStructureType->type == REF_WALL || psStruct->pStructureType->type == REF_WALLCORNER) &&
((STRUCTURE_STATS *)psDroid->psTarStats)->type == REF_DEFENSE) ((STRUCTURE_STATS *)psDroid->psTarStats)->type == REF_DEFENSE)
@ -1853,10 +1844,10 @@ void actionUpdateDroid(DROID *psDroid)
} }
else else
{ {
bDoHelpBuild = true; helpBuild = true;
} }
if (bDoHelpBuild) if (helpBuild)
{ {
// continuing a partially built structure (order = helpBuild) // continuing a partially built structure (order = helpBuild)
if (droidStartBuild(psDroid)) if (droidStartBuild(psDroid))
@ -1877,7 +1868,9 @@ void actionUpdateDroid(DROID *psDroid)
if (actionDroidOnBuildPos(psDroid, if (actionDroidOnBuildPos(psDroid,
(SDWORD)psDroid->orderX,(SDWORD)psDroid->orderY, psDroid->psTarStats)) (SDWORD)psDroid->orderX,(SDWORD)psDroid->orderY, psDroid->psTarStats))
{ {
actionHomeBasePos(psDroid->player, &pbx,&pby); SDWORD pbx, pby;
actionHomeBasePos(psDroid->player, &pbx, &pby);
if (pbx == 0 || pby == 0) if (pbx == 0 || pby == 0)
{ {
debug(LOG_NEVER, "DACTION_MOVETOBUILD: No HQ, cannot move in that direction."); debug(LOG_NEVER, "DACTION_MOVETOBUILD: No HQ, cannot move in that direction.");
@ -1970,7 +1963,9 @@ void actionUpdateDroid(DROID *psDroid)
if (actionDroidOnBuildPos(psDroid, if (actionDroidOnBuildPos(psDroid,
(SDWORD)psDroid->actionX,(SDWORD)psDroid->actionY, psDroid->psTarStats)) (SDWORD)psDroid->actionX,(SDWORD)psDroid->actionY, psDroid->psTarStats))
{ {
actionHomeBasePos(psDroid->player, &pbx,&pby); SDWORD pbx, pby;
actionHomeBasePos(psDroid->player, &pbx, &pby);
if (pbx == 0 || pby == 0) if (pbx == 0 || pby == 0)
{ {
debug(LOG_NEVER, "No HQ - cannot move in that direction."); debug(LOG_NEVER, "No HQ - cannot move in that direction.");
@ -1994,19 +1989,19 @@ void actionUpdateDroid(DROID *psDroid)
switch (psDroid->action) switch (psDroid->action)
{ {
case DACTION_DEMOLISH: case DACTION_DEMOLISH:
moveAction = DACTION_MOVETODEMOLISH; // DACTION_MOVETODEMOLISH;
actionUpdateFunc = droidUpdateDemolishing; actionUpdateFunc = droidUpdateDemolishing;
break; break;
case DACTION_REPAIR: case DACTION_REPAIR:
moveAction = DACTION_MOVETOREPAIR; // DACTION_MOVETOREPAIR;
actionUpdateFunc = droidUpdateRepair; actionUpdateFunc = droidUpdateRepair;
break; break;
case DACTION_CLEARWRECK: case DACTION_CLEARWRECK:
moveAction = DACTION_MOVETOCLEAR; // DACTION_MOVETOCLEAR;
actionUpdateFunc = droidUpdateClearing; actionUpdateFunc = droidUpdateClearing;
break; break;
case DACTION_RESTORE: case DACTION_RESTORE:
moveAction = DACTION_MOVETORESTORE; // DACTION_MOVETORESTORE;
actionUpdateFunc = droidUpdateRestore; actionUpdateFunc = droidUpdateRestore;
break; break;
default: default:
@ -2036,8 +2031,8 @@ void actionUpdateDroid(DROID *psDroid)
else if (psDroid->action == DACTION_CLEARWRECK) else if (psDroid->action == DACTION_CLEARWRECK)
{ {
//see if there is any other wreckage in the area //see if there is any other wreckage in the area
FEATURE* const psNextWreck = checkForWreckage(psDroid);
psDroid->action = DACTION_NONE; psDroid->action = DACTION_NONE;
psNextWreck = checkForWreckage(psDroid);
if (psNextWreck) if (psNextWreck)
{ {
orderDroidObj(psDroid, DORDER_CLEARWRECK, (BASE_OBJECT *)psNextWreck); orderDroidObj(psDroid, DORDER_CLEARWRECK, (BASE_OBJECT *)psNextWreck);
@ -2080,10 +2075,10 @@ void actionUpdateDroid(DROID *psDroid)
case DACTION_BUILD_FOUNDATION: case DACTION_BUILD_FOUNDATION:
//building a structure's foundation - flattening the ground for now //building a structure's foundation - flattening the ground for now
{ {
psTile = mapTile(map_coord(psDroid->orderX), map_coord(psDroid->orderY)); MAPTILE* const psTile = mapTile(map_coord(psDroid->orderX), map_coord(psDroid->orderY));
psStructStats = (STRUCTURE_STATS*)psDroid->psTarStats; STRUCTURE_STATS* const psStructStats = (STRUCTURE_STATS*)psDroid->psTarStats;
tlx = (SDWORD)psDroid->orderX - (SDWORD)(psStructStats->baseWidth * TILE_UNITS)/2; const UDWORD tlx = (SDWORD)psDroid->orderX - (SDWORD)(psStructStats->baseWidth * TILE_UNITS)/2;
tly = (SDWORD)psDroid->orderY - (SDWORD)(psStructStats->baseBreadth * TILE_UNITS)/2; const UDWORD tly = (SDWORD)psDroid->orderY - (SDWORD)(psStructStats->baseBreadth * TILE_UNITS)/2;
if ((psDroid->psTarget == NULL) && if ((psDroid->psTarget == NULL) &&
(TileHasStructure(psTile) || (TileHasStructure(psTile) ||
TileHasFeature(psTile))) TileHasFeature(psTile)))
@ -2091,7 +2086,7 @@ void actionUpdateDroid(DROID *psDroid)
if (TileHasStructure(psTile)) if (TileHasStructure(psTile))
{ {
// structure on the build location - see if it is the same type // structure on the build location - see if it is the same type
psStruct = getTileStructure(map_coord(psDroid->orderX), map_coord(psDroid->orderY)); STRUCTURE* const psStruct = getTileStructure(map_coord(psDroid->orderX), map_coord(psDroid->orderY));
if (psStruct->pStructureType == (STRUCTURE_STATS *)psDroid->psTarStats) if (psStruct->pStructureType == (STRUCTURE_STATS *)psDroid->psTarStats)
{ {
// same type - do a help build // same type - do a help build
@ -2140,12 +2135,12 @@ void actionUpdateDroid(DROID *psDroid)
else else
{ {
// make sure the target is within sensor range // make sure the target is within sensor range
xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x; const int xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x;
ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y; const int ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y;
rangeSq = droidSensorRange(psDroid); int rangeSq = droidSensorRange(psDroid);
rangeSq = rangeSq * rangeSq; rangeSq = rangeSq * rangeSq;
if (!visibleObject((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], false) || if (!visibleObject((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], false)
xdiff*xdiff + ydiff*ydiff >= rangeSq) || xdiff * xdiff + ydiff * ydiff >= rangeSq)
{ {
psDroid->action = DACTION_MOVETOOBSERVE; psDroid->action = DACTION_MOVETOOBSERVE;
moveDroidTo(psDroid, psDroid->psActionTarget[0]->pos.x, psDroid->psActionTarget[0]->pos.y); moveDroidTo(psDroid, psDroid->psActionTarget[0]->pos.x, psDroid->psActionTarget[0]->pos.y);
@ -2159,9 +2154,9 @@ void actionUpdateDroid(DROID *psDroid)
if (visibleObject((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], false)) if (visibleObject((BASE_OBJECT *)psDroid, psDroid->psActionTarget[0], false))
{ {
// make sure the target is within sensor range // make sure the target is within sensor range
xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x; const int xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x;
ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y; const int ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y;
rangeSq = droidSensorRange(psDroid); int rangeSq = droidSensorRange(psDroid);
rangeSq = rangeSq * rangeSq; rangeSq = rangeSq * rangeSq;
if ((xdiff*xdiff + ydiff*ydiff < rangeSq) && if ((xdiff*xdiff + ydiff*ydiff < rangeSq) &&
!DROID_STOPPED(psDroid)) !DROID_STOPPED(psDroid))
@ -2187,9 +2182,9 @@ void actionUpdateDroid(DROID *psDroid)
(psDroid->psTarget->type != OBJ_STRUCTURE)) (psDroid->psTarget->type != OBJ_STRUCTURE))
{ {
//move droids to within short range of the sensor now!!!! //move droids to within short range of the sensor now!!!!
xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psTarget->pos.x; int xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psTarget->pos.x;
ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psTarget->pos.y; int ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psTarget->pos.y;
rangeSq = asWeaponStats[psDroid->asWeaps[0].nStat].shortRange; int rangeSq = asWeaponStats[psDroid->asWeaps[0].nStat].shortRange;
rangeSq = rangeSq * rangeSq; rangeSq = rangeSq * rangeSq;
if (xdiff*xdiff + ydiff*ydiff < rangeSq) if (xdiff*xdiff + ydiff*ydiff < rangeSq)
{ {
@ -2209,8 +2204,10 @@ void actionUpdateDroid(DROID *psDroid)
if (DROID_STOPPED(psDroid) || if (DROID_STOPPED(psDroid) ||
(xdiff*xdiff + ydiff*ydiff > rangeSq)) (xdiff*xdiff + ydiff*ydiff > rangeSq))
{ {
if (secondaryGetState(psDroid, DSO_HALTTYPE, &state) && ( SECONDARY_STATE state;
state == DSS_HALT_HOLD))
if (secondaryGetState(psDroid, DSO_HALTTYPE, &state)
&& state == DSS_HALT_HOLD)
{ {
// droid on hold, don't allow moves. // droid on hold, don't allow moves.
psDroid->action = DACTION_NONE; psDroid->action = DACTION_NONE;
@ -2234,15 +2231,16 @@ void actionUpdateDroid(DROID *psDroid)
} }
else else
{ {
debug(LOG_DEATH, "actionUpdateDroid: Droid %d destructed", (int)psDroid->id); debug(LOG_DEATH, "Droid %d destructed", (int)psDroid->id);
destroyDroid(psDroid); destroyDroid(psDroid);
} }
} }
break; break;
case DACTION_MOVETODROIDREPAIR: case DACTION_MOVETODROIDREPAIR:
{
// moving to repair a droid // moving to repair a droid
xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x; const int xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x;
ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y; const int ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y;
if ( xdiff*xdiff + ydiff*ydiff < REPAIR_RANGE ) if ( xdiff*xdiff + ydiff*ydiff < REPAIR_RANGE )
{ {
// Got to destination - start repair // Got to destination - start repair
@ -2268,6 +2266,7 @@ void actionUpdateDroid(DROID *psDroid)
moveDroidTo(psDroid, psDroid->actionX, psDroid->actionY); moveDroidTo(psDroid, psDroid->actionX, psDroid->actionY);
} }
break; break;
}
case DACTION_DROIDREPAIR: case DACTION_DROIDREPAIR:
//if not doing self-repair //if not doing self-repair
//Watermelon:use 0 for repair droid //Watermelon:use 0 for repair droid
@ -2277,8 +2276,8 @@ void actionUpdateDroid(DROID *psDroid)
} }
//check still next to the damaged droid //check still next to the damaged droid
xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x; const int xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x;
ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y; const int ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y;
if ( xdiff*xdiff + ydiff*ydiff > REPAIR_RANGE ) if ( xdiff*xdiff + ydiff*ydiff > REPAIR_RANGE )
{ {
/*once started - don't allow the Repair droid to follow the /*once started - don't allow the Repair droid to follow the
@ -2354,7 +2353,7 @@ void actionUpdateDroid(DROID *psDroid)
{ {
// got close to the rearm pad - now find a clear one // got close to the rearm pad - now find a clear one
objTrace(psDroid->id, "Seen rearm pad - searching for available one"); objTrace(psDroid->id, "Seen rearm pad - searching for available one");
psStruct = findNearestReArmPad(psDroid, (STRUCTURE *)psDroid->psActionTarget[0], true); STRUCTURE* const psStruct = findNearestReArmPad(psDroid, (STRUCTURE *)psDroid->psActionTarget[0], true);
if (psStruct != NULL) if (psStruct != NULL)
{ {
// found a clear landing pad - go for it // found a clear landing pad - go for it
@ -2368,8 +2367,8 @@ void actionUpdateDroid(DROID *psDroid)
if (DROID_STOPPED(psDroid) || if (DROID_STOPPED(psDroid) ||
(psDroid->action == DACTION_WAITFORREARM)) (psDroid->action == DACTION_WAITFORREARM))
{ {
droidX = psDroid->psActionTarget[0]->pos.x; UDWORD droidX = psDroid->psActionTarget[0]->pos.x;
droidY = psDroid->psActionTarget[0]->pos.y; UDWORD droidY = psDroid->psActionTarget[0]->pos.y;
if (!actionVTOLLandingPos(psDroid, &droidX, &droidY)) if (!actionVTOLLandingPos(psDroid, &droidX, &droidY))
{ {
// totally bunged up - give up // totally bunged up - give up

View File

@ -30,7 +30,7 @@ typedef struct _weapon
* Index into the global @c asWeaponStats array; thus a "reference" of * Index into the global @c asWeaponStats array; thus a "reference" of
* some kind to the associated stats. * some kind to the associated stats.
*/ */
UDWORD nStat; unsigned int nStat;
UDWORD ammo; UDWORD ammo;