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,6 +74,43 @@ BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::calculateNeighbouringTile
return ret;
}
BattleHexArray::ArrayOfBattleHexArrays BattleHexArray::calculateNeighbouringTilesDblWide(BattleSide side)
{
ArrayOfBattleHexArrays ret;
for(BattleHex hex = 0; hex < GameConstants::BFIELD_SIZE; hex.hex++)
{
BattleHexArray hexes;
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));
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));
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[hex.hex] = std::move(hexes);
}
return ret;
}
BattleHexArray BattleHexArray::generateNeighbouringTiles(BattleHex hex)
{
BattleHexArray ret;
@ -83,73 +120,6 @@ BattleHexArray BattleHexArray::generateNeighbouringTiles(BattleHex hex)
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++)
{
BattleHexArray hexes;
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));
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));
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);
}
initialized[sideIdx] = true;
}
return ret[sideIdx][pos.hex];
}
const BattleHexArray & BattleHexArray::getClosestTilesCache(BattleHex pos, BattleSide side)
{
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];
}
void BattleHexArray::merge(const BattleHexArray & other) noexcept
{
for(auto hex : other)
@ -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

View File

@ -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);
};

View File

@ -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