From 7421fabf2cf4ddff507a5699f8e773d0b5d93b38 Mon Sep 17 00:00:00 2001 From: krs Date: Sat, 25 Feb 2023 00:54:48 +0200 Subject: [PATCH] BattlePhases enum moved to Unit.h --- lib/battle/CBattleInfoCallback.cpp | 35 +++++++++++------------------- lib/battle/CUnitState.cpp | 19 +++++----------- lib/battle/CUnitState.h | 2 +- lib/battle/Unit.h | 12 +++++++++- 4 files changed, 30 insertions(+), 38 deletions(-) diff --git a/lib/battle/CBattleInfoCallback.cpp b/lib/battle/CBattleInfoCallback.cpp index 53fe2db27..59c6ecb9c 100644 --- a/lib/battle/CBattleInfoCallback.cpp +++ b/lib/battle/CBattleInfoCallback.cpp @@ -355,15 +355,6 @@ battle::Units CBattleInfoCallback::battleAliveUnits(ui8 side) const }); } -enum BattlePhases -{ - SIEGE, // [0] - turrets/catapult, - NORMAL, // [1] - normal (unmoved) creatures, other war machines, - WAIT_MORALE, // [2] - waited creatures that had morale, - WAIT, // [3] - rest of waited creatures - MAX_NO_OF_PHASES // [4] - number of phases. -}; - //T is battle::Unit descendant template const T * takeOneUnit(std::vector & allUnits, const int turn, int8_t & sideThatLastMoved, int phase) @@ -387,7 +378,7 @@ const T * takeOneUnit(std::vector & allUnits, const int turn, int8_t & switch(phase) { - case NORMAL: // Faster first, attacker priority, higher slot first + case battle::NORMAL: // Faster first, attacker priority, higher slot first if(returnedUnit == nullptr || currentUnitInitiative > returnedUnitInitiative) { returnedUnit = currentUnit; @@ -409,8 +400,8 @@ const T * takeOneUnit(std::vector & allUnits, const int turn, int8_t & } } break; - case WAIT_MORALE: // Slower first, higher slot first - case WAIT: + case battle::WAIT_MORALE: // Slower first, higher slot first + case battle::WAIT: if(returnedUnit == nullptr || currentUnitInitiative < returnedUnitInitiative) { returnedUnit = currentUnit; @@ -462,11 +453,11 @@ void CBattleInfoCallback::battleGetTurnOrder(std::vector & turns, turns.emplace_back(); // We'll split creatures with remaining movement to 4 buckets - // [0] - turrets/catapult, - // [1] - normal (unmoved) creatures, other war machines, - // [2] - waited creatures that had morale, - // [3] - rest of waited creatures - std::array phases; + // [0] SIEGE - turrets/catapult, + // [1] NORMAL - normal (unmoved) creatures, other war machines, + // [2] WAIT_MORALE - waited creatures that had morale, + // [3] WAIT - rest of waited creatures + std::array phases; // Access using BattlePhases enum const battle::Unit * activeUnit = battleActiveUnit(); @@ -512,17 +503,17 @@ void CBattleInfoCallback::battleGetTurnOrder(std::vector & turns, phases[unitPhase].push_back(unit); } - boost::sort(phases[SIEGE], CMP_stack(SIEGE, actualTurn, sideThatLastMoved)); - std::copy(phases[SIEGE].begin(), phases[SIEGE].end(), std::back_inserter(turns.back())); + boost::sort(phases[battle::SIEGE], CMP_stack(battle::SIEGE, actualTurn, sideThatLastMoved)); + std::copy(phases[battle::SIEGE].begin(), phases[battle::SIEGE].end(), std::back_inserter(turns.back())); if(turnsIsFull()) return; - for(int phase = NORMAL; phase < MAX_NO_OF_PHASES; phase++) + for(int phase = battle::NORMAL; phase < battle::MAX_NO_OF_PHASES; phase++) boost::sort(phases[phase], CMP_stack(phase, actualTurn, sideThatLastMoved)); - int phase = NORMAL; - while(!turnsIsFull() && phase < MAX_NO_OF_PHASES) + int phase = battle::NORMAL; + while(!turnsIsFull() && phase < battle::MAX_NO_OF_PHASES) { const battle::Unit * currentUnit = nullptr; if(phases[phase].empty()) diff --git a/lib/battle/CUnitState.cpp b/lib/battle/CUnitState.cpp index 92792ec33..e3bc59371 100644 --- a/lib/battle/CUnitState.cpp +++ b/lib/battle/CUnitState.cpp @@ -609,31 +609,22 @@ bool CUnitState::waited(int turn) const return false; } -int CUnitState::battleQueuePhase(int turn) const +BattlePhases CUnitState::battleQueuePhase(int turn) const { - enum BattlePhases - { - SIEGE, // [0] - turrets/catapult, - NORMAL, // [1] - normal (unmoved) creatures, other war machines, - WAIT_MORALE, // [2] - waited creatures that had morale, - WAIT, // [3] - rest of waited creatures - MAX_NO_OF_PHASES // [4] - number of phases. Can be used in for loops. - }; - if(turn <= 0 && waited()) //consider waiting state only for ongoing round { if(hadMorale) - return WAIT_MORALE; + return battle::WAIT_MORALE; else - return WAIT; + return battle::WAIT; } else if(creatureIndex() == CreatureID::CATAPULT || isTurret()) //catapult and turrets are first { - return SIEGE; + return battle::SIEGE; } else { - return NORMAL; + return battle::NORMAL; } } diff --git a/lib/battle/CUnitState.h b/lib/battle/CUnitState.h index 2c9dee6a2..4a2271248 100644 --- a/lib/battle/CUnitState.h +++ b/lib/battle/CUnitState.h @@ -225,7 +225,7 @@ public: std::shared_ptr acquire() const override; std::shared_ptr acquireState() const override; - int battleQueuePhase(int turn) const override; + BattlePhases battleQueuePhase(int turn) const override; int getTotalAttacks(bool ranged) const override; diff --git a/lib/battle/Unit.h b/lib/battle/Unit.h index 0eb7ad497..4b831e4cc 100644 --- a/lib/battle/Unit.h +++ b/lib/battle/Unit.h @@ -25,6 +25,16 @@ class JsonSerializeFormat; namespace battle { + +enum BattlePhases +{ + SIEGE, // [0] - turrets/catapult, + NORMAL, // [1] - normal (unmoved) creatures, other war machines, + WAIT_MORALE, // [2] - waited creatures that had morale, + WAIT, // [3] - rest of waited creatures + MAX_NO_OF_PHASES // [4] - number of phases. +}; + class CUnitState; class DLL_LINKAGE Unit : public IUnitInfo, public spells::Caster, public virtual IBonusBearer @@ -79,7 +89,7 @@ public: virtual std::shared_ptr acquire() const = 0; virtual std::shared_ptr acquireState() const = 0; - virtual int battleQueuePhase(int turn) const = 0; + virtual BattlePhases battleQueuePhase(int turn) const = 0; virtual std::string getDescription() const;