From 794595184eb2782909116f8efd01ae09d0bd0b8c Mon Sep 17 00:00:00 2001 From: MichalZr6 Date: Fri, 6 Dec 2024 19:59:05 +0100 Subject: [PATCH] use class scope static instead of function scope --- lib/battle/BattleHexArray.cpp | 107 +++++++++++++--------------------- lib/battle/BattleHexArray.h | 15 ++++- lib/battle/Unit.cpp | 2 +- 3 files changed, 53 insertions(+), 71 deletions(-) diff --git a/lib/battle/BattleHexArray.cpp b/lib/battle/BattleHexArray.cpp index f7bd751e8..00927227b 100644 --- a/lib/battle/BattleHexArray.cpp +++ b/lib/battle/BattleHexArray.cpp @@ -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(0); dir <= static_cast(4); dir = static_cast(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(0); dir <= static_cast(4); dir = static_cast(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 ret; // 2 -> only two valid sides: ATTACKER and DEFENDER - size_t sideIdx = static_cast(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(0); dir <= static_cast(4); dir = static_cast(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(0); dir <= static_cast(4); dir = static_cast(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 ret; - static bool initialized = false; - size_t sideIdx = static_cast(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 BattleHexArray::neighbouringTilesDblWide = + { { BattleSide::ATTACKER, calculateNeighbouringTilesDblWide(BattleSide::ATTACKER) }, + { BattleSide::DEFENDER, calculateNeighbouringTilesDblWide(BattleSide::DEFENDER) } }; VCMI_LIB_NAMESPACE_END \ No newline at end of file diff --git a/lib/battle/BattleHexArray.h b/lib/battle/BattleHexArray.h index f876100f6..8b0a2f960 100644 --- a/lib/battle/BattleHexArray.h +++ b/lib/battle/BattleHexArray.h @@ -36,6 +36,7 @@ public: using ArrayOfBattleHexArrays = std::array; static const ArrayOfBattleHexArrays neighbouringTilesCache; + static const std::map 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 >> + 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); }; diff --git a/lib/battle/Unit.cpp b/lib/battle/Unit.cpp index 18a634eef..ba16a1af7 100644 --- a/lib/battle/Unit.cpp +++ b/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