فهرست منبع

Fix regressions from battlehex PR (mostly related to towers)

Ivan Savenko 11 ماه پیش
والد
کامیت
5bfc837a8f
4فایلهای تغییر یافته به همراه38 افزوده شده و 20 حذف شده
  1. 14 8
      lib/battle/BattleHexArray.h
  2. 3 8
      lib/battle/CBattleInfoCallback.cpp
  3. 18 2
      lib/battle/Unit.cpp
  4. 3 2
      lib/spells/effects/UnitEffect.cpp

+ 14 - 8
lib/battle/BattleHexArray.h

@@ -113,11 +113,11 @@ public:
 	}
 
 	void clear() noexcept;
-	inline void erase(size_type index) noexcept
+	inline void erase(BattleHex target) noexcept
 	{
-		assert(index < totalSize);
-		internalStorage[index] = BattleHex::INVALID;
-		presenceFlags[index] = 0;
+		assert(contains(target));
+		vstd::erase(internalStorage, target);
+		presenceFlags[target.toInt()] = 0;
 	}
 	void erase(iterator first, iterator last) noexcept;
 	inline void pop_back() noexcept
@@ -160,17 +160,23 @@ public:
 	/// get (precomputed) all possible surrounding tiles
 	static const BattleHexArray & getAllNeighbouringTiles(BattleHex hex) noexcept
 	{
-		assert(hex.isValid());
+		static const BattleHexArray invalid;
 
-		return allNeighbouringTiles[hex.toInt()];
+		if (hex.isValid())
+			return allNeighbouringTiles[hex.toInt()];
+		else
+			return invalid;
 	}
 
 	/// get (precomputed) only valid and available surrounding tiles
 	static const BattleHexArray & getNeighbouringTiles(BattleHex hex) noexcept
 	{
-		assert(hex.isValid());
+		static const BattleHexArray invalid;
 
-		return neighbouringTiles[hex.toInt()];
+		if (hex.isValid())
+			return neighbouringTiles[hex.toInt()];
+		else
+			return invalid;
 	}
 
 	/// get (precomputed) only valid and available surrounding tiles for double wide creatures

+ 3 - 8
lib/battle/CBattleInfoCallback.cpp

@@ -1353,14 +1353,9 @@ AttackableTiles CBattleInfoCallback::getPotentiallyAttackableHexes(
 	if(attacker->hasBonusOfType(BonusType::WIDE_BREATH))
 	{
 		BattleHexArray hexes = destinationTile.getNeighbouringTiles();
-		for(int i = 0; i < hexes.size(); i++)
-		{
-			if(hexes.at(i) == attackOriginHex)
-			{
-				hexes.erase(i);
-				i = 0;
-			}
-		}
+		if (hexes.contains(attackOriginHex))
+			hexes.erase(attackOriginHex);
+
 		for(BattleHex tile : hexes)
 		{
 			//friendly stacks can also be damaged by Dragon Breath

+ 18 - 2
lib/battle/Unit.cpp

@@ -134,8 +134,24 @@ const BattleHexArray & Unit::getHexes(BattleHex assumedPos, bool twoHex, BattleS
 		precomputeUnitHexes(BattleSide::DEFENDER, true),
 	};
 
-	int index = side == BattleSide::ATTACKER ? 0 : 2;
-	return precomputed[index + twoHex][assumedPos.toInt()];
+	static const std::array<BattleHexArray, 5> invalidHexes = {
+		BattleHexArray({BattleHex( 0)}),
+		BattleHexArray({BattleHex(-1)}),
+		BattleHexArray({BattleHex(-2)}),
+		BattleHexArray({BattleHex(-3)}),
+		BattleHexArray({BattleHex(-4)})
+	};
+
+	if (assumedPos.isValid())
+	{
+		int index = side == BattleSide::ATTACKER ? 0 : 2;
+		return precomputed[index + twoHex][assumedPos.toInt()];
+	}
+	else
+	{
+		// Towers and such
+		return invalidHexes.at(-assumedPos.toInt());
+	}
 }
 
 BattleHex Unit::occupiedHex() const

+ 3 - 2
lib/spells/effects/UnitEffect.cpp

@@ -223,7 +223,8 @@ EffectTarget UnitEffect::transformTargetByChain(const Mechanics * m, const Targe
 			effectTarget.emplace_back();
 
 		for(auto hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
-			possibleHexes.erase(hex.toInt());
+			if (possibleHexes.contains(hex))
+				possibleHexes.erase(hex);
 
 		if(possibleHexes.empty())
 			break;
@@ -278,4 +279,4 @@ void UnitEffect::serializeJsonEffect(JsonSerializeFormat & handler)
 }
 }
 
-VCMI_LIB_NAMESPACE_END
+VCMI_LIB_NAMESPACE_END