Pārlūkot izejas kodu

Refactor destructibleEnemyTurns

MichalZr6 10 mēneši atpakaļ
vecāks
revīzija
e3516120d8

+ 3 - 2
lib/battle/AccessibilityInfo.cpp

@@ -22,9 +22,10 @@ bool AccessibilityInfo::tileAccessibleWithGate(BattleHex tile, BattleSide side)
 
 	if(accessibility == EAccessibility::ALIVE_STACK)
 	{
-		auto destructible = destructibleEnemyTurns.find(tile);
+		if(!destructibleEnemyTurns)
+			return false;
 
-		return destructible != destructibleEnemyTurns.end();
+		return destructibleEnemyTurns->at(tile.hex) >= 0;
 	}
 
 	if(accessibility != EAccessibility::ACCESSIBLE)

+ 2 - 2
lib/battle/AccessibilityInfo.h

@@ -27,7 +27,7 @@ enum class EAccessibility
 	DESTRUCTIBLE_WALL,
 	GATE, //sieges -> gate opens only for defender stacks
 	UNAVAILABLE, //indestructible wall parts, special battlefields (like boat-to-boat)
-	SIDE_COLUMN //used for first and last columns of hexes that are unavailable but wat machines can stand there
+	SIDE_COLUMN //used for first and last columns of hexes that are unavailable but war machines can stand there
 };
 
 
@@ -35,7 +35,7 @@ using TAccessibilityArray = std::array<EAccessibility, GameConstants::BFIELD_SIZ
 
 struct DLL_LINKAGE AccessibilityInfo : TAccessibilityArray
 {
-	std::map<BattleHex, ui8> destructibleEnemyTurns;
+	const std::array<int8_t, GameConstants::BFIELD_SIZE> * destructibleEnemyTurns = nullptr;
 
 	public:
 		bool accessible(BattleHex tile, const battle::Unit * stack) const; //checks for both tiles if stack is double wide

+ 14 - 17
lib/battle/CBattleInfoCallback.cpp

@@ -1083,28 +1083,25 @@ ReachabilityInfo CBattleInfoCallback::makeBFS(const AccessibilityInfo &accessibi
 
 		for(BattleHex neighbour : BattleHexArray::neighbouringTilesCache[curHex.hex])
 		{
-			if(neighbour.isValid())
+			auto additionalCost = 0;
+
+			if(params.bypassEnemyStacks)
 			{
-				auto additionalCost = 0;
+				auto enemyToBypass = params.destructibleEnemyTurns.at(neighbour);
 
-				if(params.bypassEnemyStacks)
+				if(enemyToBypass >= 0)
 				{
-					auto enemyToBypass = params.destructibleEnemyTurns.find(neighbour);
-
-					if(enemyToBypass != params.destructibleEnemyTurns.end())
-					{
-						additionalCost = enemyToBypass->second;
-					}
+					additionalCost = enemyToBypass;
 				}
+			}
 
-				const int costFoundSoFar = ret.distances[neighbour.hex];
+			const int costFoundSoFar = ret.distances[neighbour.hex];
 
-				if(accessibleCache[neighbour.hex] && costToNeighbour + additionalCost < costFoundSoFar)
-				{
-					hexq.push(neighbour);
-					ret.distances[neighbour.hex] = costToNeighbour + additionalCost;
-					ret.predecessors[neighbour.hex] = curHex;
-				}
+			if(accessibleCache[neighbour.hex] && costToNeighbour + additionalCost < costFoundSoFar)
+			{
+				hexq.push(neighbour);
+				ret.distances[neighbour.hex] = costToNeighbour + additionalCost;
+				ret.predecessors[neighbour.hex] = curHex;
 			}
 		}
 	}
@@ -1280,7 +1277,7 @@ ReachabilityInfo CBattleInfoCallback::getReachability(const ReachabilityInfo::Pa
 	{
 		auto accessibility = getAccessibility(params.knownAccessible);
 
-		accessibility.destructibleEnemyTurns = params.destructibleEnemyTurns;
+		accessibility.destructibleEnemyTurns = & params.destructibleEnemyTurns;
 
 		return makeBFS(accessibility, params);
 	}

+ 1 - 0
lib/battle/ReachabilityInfo.cpp

@@ -22,6 +22,7 @@ ReachabilityInfo::Parameters::Parameters(const battle::Unit * Stack, BattleHex S
 	flying(Stack->hasBonusOfType(BonusType::FLYING))
 {
 	knownAccessible = battle::Unit::getHexes(startPosition, doubleWide, side);
+	destructibleEnemyTurns.fill(-1);
 }
 
 ReachabilityInfo::ReachabilityInfo()

+ 5 - 2
lib/battle/ReachabilityInfo.h

@@ -33,12 +33,15 @@ struct DLL_LINKAGE ReachabilityInfo
 		bool ignoreKnownAccessible = false; //Ignore obstacles if it is in accessible hexes
 		bool bypassEnemyStacks = false; // in case of true will count amount of turns needed to kill enemy and thus move forward
 		BattleHexArray knownAccessible; //hexes that will be treated as accessible, even if they're occupied by stack (by default - tiles occupied by stack we do reachability for, so it doesn't block itself)
-		std::map<BattleHex, ui8> destructibleEnemyTurns; // hom many turns it is needed to kill enemy on specific hex
+		std::array<int8_t, GameConstants::BFIELD_SIZE> destructibleEnemyTurns; // how many turns it is needed to kill enemy on specific hex (index <=> hex)
 
 		BattleHex startPosition; //assumed position of stack
 		BattleSide perspective = BattleSide::ALL_KNOWING; //some obstacles (eg. quicksands) may be invisible for some side
 
-		Parameters() = default;
+		Parameters()
+		{
+			destructibleEnemyTurns.fill(-1);
+		}
 		Parameters(const battle::Unit * Stack, BattleHex StartPosition);
 	};