Improved code for AI attack location randomization.

git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@9221 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
Sevenfm
2021-12-21 08:09:41 +00:00
parent 210101463e
commit ad044f38c6
+22 -25
View File
@@ -5876,43 +5876,40 @@ INT32 RandomizeLocation(INT32 sSpot, INT8 bLevel, UINT8 ubTimes, SOLDIERTYPE *pS
UINT8 ubDirection; UINT8 ubDirection;
UINT8 ubMovementCost; UINT8 ubMovementCost;
INT32 sTempSpot; INT32 sTempSpot;
INT32 sSpotArray[NUM_WORLD_DIRECTIONS + 1];
UINT8 ubSpots;
std::vector<INT32> spots;
std::vector<INT32>::iterator iter;
// store original location
spots.push_back(sSpot);
// find all spots reachable within ubTimes steps
for (UINT8 ubCnt = 0; ubCnt < ubTimes; ubCnt++) for (UINT8 ubCnt = 0; ubCnt < ubTimes; ubCnt++)
{ {
// store original location for (iter = spots.begin(); iter != spots.end(); iter++)
ubSpots = 1;
sSpotArray[0] = sSpot;
// find adjacent locations
for (ubDirection = 0; ubDirection < NUM_WORLD_DIRECTIONS; ubDirection++)
{ {
sTempSpot = NewGridNo(sSpot, DirectionInc(ubDirection)); // find adjacent locations
for (ubDirection = 0; ubDirection < NUM_WORLD_DIRECTIONS; ubDirection++)
if (sTempSpot != sSpot)
{ {
ubMovementCost = gubWorldMovementCosts[sTempSpot][ubDirection][bLevel]; sTempSpot = NewGridNo(*iter, DirectionInc(ubDirection));
if (ubMovementCost < TRAVELCOST_BLOCKED && if (sTempSpot != *iter)
IsLocationSittableExcludingPeople(sTempSpot, bLevel) &&
(!pSightSoldier || SoldierToVirtualSoldierLineOfSightTest(pSightSoldier, sTempSpot, bLevel, ANIM_STAND, TRUE, NO_DISTANCE_LIMIT)))
{ {
sSpotArray[ubSpots] = sTempSpot; ubMovementCost = gubWorldMovementCosts[sTempSpot][ubDirection][bLevel];
ubSpots++;
if (ubMovementCost < TRAVELCOST_BLOCKED &&
std::find(spots.begin(), spots.end(), sTempSpot) == spots.end() &&
IsLocationSittableExcludingPeople(sTempSpot, bLevel) &&
(!pSightSoldier || SoldierToVirtualSoldierLineOfSightTest(pSightSoldier, sTempSpot, bLevel, ANIM_STAND, TRUE, NO_DISTANCE_LIMIT)))
{
spots.push_back(sTempSpot);
}
} }
} }
} }
// find random location
sSpot = sSpotArray[Random(ubSpots)];
// stop if could not find any adjacent spot
if (ubSpots < 2)
{
break;
}
} }
return sSpot; return spots[Random(spots.size())];
} }
INT32 RandomizeOpponentLocation(INT32 sSpot, SOLDIERTYPE *pOpponent, INT16 sMaxDistance) INT32 RandomizeOpponentLocation(INT32 sSpot, SOLDIERTYPE *pOpponent, INT16 sMaxDistance)