From ad044f38c6089fa901e5dde09d2e787951c71e09 Mon Sep 17 00:00:00 2001 From: Sevenfm Date: Tue, 21 Dec 2021 08:09:41 +0000 Subject: [PATCH] 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 --- TacticalAI/AIUtils.cpp | 47 ++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/TacticalAI/AIUtils.cpp b/TacticalAI/AIUtils.cpp index c577b762..cb7e9a20 100644 --- a/TacticalAI/AIUtils.cpp +++ b/TacticalAI/AIUtils.cpp @@ -5876,43 +5876,40 @@ INT32 RandomizeLocation(INT32 sSpot, INT8 bLevel, UINT8 ubTimes, SOLDIERTYPE *pS UINT8 ubDirection; UINT8 ubMovementCost; INT32 sTempSpot; - INT32 sSpotArray[NUM_WORLD_DIRECTIONS + 1]; - UINT8 ubSpots; + std::vector spots; + std::vector::iterator iter; + + // store original location + spots.push_back(sSpot); + + // find all spots reachable within ubTimes steps for (UINT8 ubCnt = 0; ubCnt < ubTimes; ubCnt++) { - // store original location - ubSpots = 1; - sSpotArray[0] = sSpot; - - // find adjacent locations - for (ubDirection = 0; ubDirection < NUM_WORLD_DIRECTIONS; ubDirection++) + for (iter = spots.begin(); iter != spots.end(); iter++) { - sTempSpot = NewGridNo(sSpot, DirectionInc(ubDirection)); - - if (sTempSpot != sSpot) + // find adjacent locations + for (ubDirection = 0; ubDirection < NUM_WORLD_DIRECTIONS; ubDirection++) { - ubMovementCost = gubWorldMovementCosts[sTempSpot][ubDirection][bLevel]; + sTempSpot = NewGridNo(*iter, DirectionInc(ubDirection)); - if (ubMovementCost < TRAVELCOST_BLOCKED && - IsLocationSittableExcludingPeople(sTempSpot, bLevel) && - (!pSightSoldier || SoldierToVirtualSoldierLineOfSightTest(pSightSoldier, sTempSpot, bLevel, ANIM_STAND, TRUE, NO_DISTANCE_LIMIT))) + if (sTempSpot != *iter) { - sSpotArray[ubSpots] = sTempSpot; - ubSpots++; + ubMovementCost = gubWorldMovementCosts[sTempSpot][ubDirection][bLevel]; + + 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)