Răsfoiți Sursa

use class scope static instead of function scope

MichalZr6 10 luni în urmă
părinte
comite
794595184e
3 a modificat fișierele cu 44 adăugiri și 62 ștergeri
  1. 31 58
      lib/battle/BattleHexArray.cpp
  2. 12 3
      lib/battle/BattleHexArray.h
  3. 1 1
      lib/battle/Unit.cpp

+ 31 - 58
lib/battle/BattleHexArray.cpp

@@ -74,80 +74,50 @@ BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::calculateNeighbouringTile
 	return ret;
 }
 
-BattleHexArray BattleHexArray::generateNeighbouringTiles(BattleHex hex)
-{
-	BattleHexArray ret;
-	for(auto dir : BattleHex::hexagonalDirections())
-		ret.checkAndPush(hex.cloneInDirection(dir, false));
-	
-	return ret;
-}
-
-const BattleHexArray & BattleHexArray::getNeighbouringTilesDblWide(BattleHex pos, BattleSide side)
+BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::calculateNeighbouringTilesDblWide(BattleSide side)
 {
-	static std::array<ArrayOfBattleHexArrays, 2> ret;		// 2 -> only two valid sides: ATTACKER and DEFENDER
-	size_t sideIdx = static_cast<size_t>(side);
-	static bool initialized[2] = { false, false };
+	ArrayOfBattleHexArrays ret;
 
-	if(!initialized[sideIdx])
+	for(BattleHex hex = 0; hex < GameConstants::BFIELD_SIZE; hex.hex++)
 	{
-		// first run, need to initialize
+		BattleHexArray hexes;
 
-		for(BattleHex hex = 0; hex < GameConstants::BFIELD_SIZE; hex.hex++)
+		if(side == BattleSide::ATTACKER)
 		{
-			BattleHexArray hexes;
+			const BattleHex otherHex = hex - 1;
 
-			if(side == BattleSide::ATTACKER)
-			{
-				const BattleHex otherHex = hex - 1;
+			for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
+				hexes.checkAndPush(hex.cloneInDirection(dir, false));
 
-				for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
-					hexes.checkAndPush(hex.cloneInDirection(dir, false));
-
-				hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
-				hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::LEFT, false));
-				hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
-			}
-			else if(side == BattleSide::DEFENDER)
-			{
-				const BattleHex otherHex = hex + 1;
+			hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
+			hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::LEFT, false));
+			hexes.checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
+		}
+		else if(side == BattleSide::DEFENDER)
+		{
+			const BattleHex otherHex = hex + 1;
 
-				hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
+			hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false));
 
-				for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
-					hexes.checkAndPush(otherHex.cloneInDirection(dir, false));
+			for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
+				hexes.checkAndPush(otherHex.cloneInDirection(dir, false));
 
-				hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
-				hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::LEFT, false));
-			}
-			ret[sideIdx][hex.hex] = std::move(hexes);
+			hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
+			hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::LEFT, false));
 		}
-		initialized[sideIdx] = true;
+		ret[hex.hex] = std::move(hexes);
 	}
 
-	return ret[sideIdx][pos.hex];
+	return ret;
 }
 
-const BattleHexArray & BattleHexArray::getClosestTilesCache(BattleHex pos, BattleSide side)
+BattleHexArray BattleHexArray::generateNeighbouringTiles(BattleHex hex)
 {
-	assert(!neighbouringTilesCache.empty());
-
-	static std::array<BattleHexArray, 2> ret;
-	static bool initialized = false;
-	size_t sideIdx = static_cast<size_t>(side);
-
-	if(!initialized)
-	{
-		ret[sideIdx].resize(GameConstants::BFIELD_SIZE);
-
-		for(si16 hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
-		{
-			ret[sideIdx].set(hex, neighbouringTilesCache[hex].getClosestTile(BattleSide::ATTACKER, hex));
-		}
-		initialized = true;
-	}
-
-	return ret[sideIdx];
+	BattleHexArray ret;
+	for(auto dir : BattleHex::hexagonalDirections())
+		ret.checkAndPush(hex.cloneInDirection(dir, false));
+	
+	return ret;
 }
 
 void BattleHexArray::merge(const BattleHexArray & other) noexcept
@@ -177,5 +147,8 @@ void BattleHexArray::clear() noexcept
 }
 
 const BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::neighbouringTilesCache = calculateNeighbouringTiles();
+const std::map<BattleSide, BattleHexArray::ArrayOfBattleHexArrays> BattleHexArray::neighbouringTilesDblWide = 
+	{ { BattleSide::ATTACKER, calculateNeighbouringTilesDblWide(BattleSide::ATTACKER) },
+	{ BattleSide::DEFENDER, calculateNeighbouringTilesDblWide(BattleSide::DEFENDER) } };
 
 VCMI_LIB_NAMESPACE_END

+ 12 - 3
lib/battle/BattleHexArray.h

@@ -36,6 +36,7 @@ public:
 	using ArrayOfBattleHexArrays = std::array<BattleHexArray, GameConstants::BFIELD_SIZE>;
 
 	static const ArrayOfBattleHexArrays neighbouringTilesCache;
+	static const std::map<BattleSide, ArrayOfBattleHexArrays> neighbouringTilesDblWide;
 
 	BattleHexArray() noexcept
 	{
@@ -136,13 +137,20 @@ public:
 		return internalStorage.insert(pos, hex);
 	}
 
-	static const BattleHexArray & getClosestTilesCache(BattleHex pos, BattleSide side);
-	static const BattleHexArray & getNeighbouringTilesDblWide(BattleHex pos, BattleSide side);
-
 	BattleHex getClosestTile(BattleSide side, BattleHex initialPos) const;
 
 	void merge(const BattleHexArray & other) noexcept;
 
+	template <typename Container, typename = std::enable_if_t<
+		std::is_convertible_v<typename Container::value_type, BattleHex>>>
+		void merge(const Container & container) noexcept
+	{
+		for(auto value : container)
+		{
+			insert(value);
+		}
+	}
+
 	void clear() noexcept;
 	inline void erase(size_type index) noexcept
 	{
@@ -306,6 +314,7 @@ private:
 
 	/// returns all valid neighbouring tiles
 	static BattleHexArray::ArrayOfBattleHexArrays calculateNeighbouringTiles();
+	static BattleHexArray::ArrayOfBattleHexArrays calculateNeighbouringTilesDblWide(BattleSide side);
 	static BattleHexArray generateNeighbouringTiles(BattleHex hex);
 };
 

+ 1 - 1
lib/battle/Unit.cpp

@@ -65,7 +65,7 @@ const BattleHexArray & Unit::getSurroundingHexes(BattleHex position, bool twoHex
 	if(!twoHex)
 		return BattleHexArray::neighbouringTilesCache[position];
 
-	return BattleHexArray::getNeighbouringTilesDblWide(position, side);
+	return BattleHexArray::neighbouringTilesDblWide.at(side).at(position);
 }
 
 BattleHexArray Unit::getAttackableHexes(const Unit * attacker) const