1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-19 21:10:12 +02:00

use class scope static instead of function scope

This commit is contained in:
MichalZr6 2024-12-06 19:59:05 +01:00
parent 7a8edff419
commit 794595184e
3 changed files with 53 additions and 71 deletions

View File

@ -74,24 +74,9 @@ BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::calculateNeighbouringTile
return ret; return ret;
} }
BattleHexArray BattleHexArray::generateNeighbouringTiles(BattleHex hex) BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::calculateNeighbouringTilesDblWide(BattleSide side)
{ {
BattleHexArray ret; ArrayOfBattleHexArrays ret;
for(auto dir : BattleHex::hexagonalDirections())
ret.checkAndPush(hex.cloneInDirection(dir, false));
return ret;
}
const BattleHexArray & BattleHexArray::getNeighbouringTilesDblWide(BattleHex pos, 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 };
if(!initialized[sideIdx])
{
// first run, need to initialize
for(BattleHex hex = 0; hex < GameConstants::BFIELD_SIZE; hex.hex++) for(BattleHex hex = 0; hex < GameConstants::BFIELD_SIZE; hex.hex++)
{ {
@ -120,34 +105,19 @@ const BattleHexArray & BattleHexArray::getNeighbouringTilesDblWide(BattleHex pos
hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false)); hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false));
hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::LEFT, false)); hexes.checkAndPush(hex.cloneInDirection(BattleHex::EDir::LEFT, false));
} }
ret[sideIdx][hex.hex] = std::move(hexes); ret[hex.hex] = std::move(hexes);
}
initialized[sideIdx] = true;
} }
return ret[sideIdx][pos.hex]; return ret;
} }
const BattleHexArray & BattleHexArray::getClosestTilesCache(BattleHex pos, BattleSide side) BattleHexArray BattleHexArray::generateNeighbouringTiles(BattleHex hex)
{ {
assert(!neighbouringTilesCache.empty()); BattleHexArray ret;
for(auto dir : BattleHex::hexagonalDirections())
ret.checkAndPush(hex.cloneInDirection(dir, false));
static std::array<BattleHexArray, 2> ret; return 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];
} }
void BattleHexArray::merge(const BattleHexArray & other) noexcept void BattleHexArray::merge(const BattleHexArray & other) noexcept
@ -177,5 +147,8 @@ void BattleHexArray::clear() noexcept
} }
const BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::neighbouringTilesCache = calculateNeighbouringTiles(); 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 VCMI_LIB_NAMESPACE_END

View File

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

View File

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