BattleInfo.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * BattleInfo.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../int3.h"
  12. #include "../bonuses/Bonus.h"
  13. #include "../bonuses/CBonusSystemNode.h"
  14. #include "CBattleInfoCallback.h"
  15. #include "IBattleState.h"
  16. #include "SiegeInfo.h"
  17. #include "SideInBattle.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class CStack;
  20. class CStackInstance;
  21. class CStackBasicDescriptor;
  22. class BattleField;
  23. class DLL_LINKAGE BattleInfo : public CBonusSystemNode, public CBattleInfoCallback, public IBattleState
  24. {
  25. public:
  26. enum BattleSide
  27. {
  28. ATTACKER = 0,
  29. DEFENDER
  30. };
  31. std::array<SideInBattle, 2> sides; //sides[0] - attacker, sides[1] - defender
  32. si32 round, activeStack;
  33. const CGTownInstance * town; //used during town siege, nullptr if this is not a siege (note that fortless town IS also a siege)
  34. int3 tile; //for background and bonuses
  35. bool creatureBank; //auxilary field, do not serialize
  36. bool replayAllowed;
  37. std::vector<CStack*> stacks;
  38. std::vector<std::shared_ptr<CObstacleInstance> > obstacles;
  39. SiegeInfo si;
  40. BattleField battlefieldType; //like !!BA:B
  41. TerrainId terrainType; //used for some stack nativity checks (not the bonus limiters though that have their own copy)
  42. ui8 tacticsSide; //which side is requested to play tactics phase
  43. ui8 tacticDistance; //how many hexes we can go forward (1 = only hexes adjacent to margin line)
  44. template <typename Handler> void serialize(Handler &h, const int version)
  45. {
  46. h & sides;
  47. h & round;
  48. h & activeStack;
  49. h & town;
  50. h & tile;
  51. h & stacks;
  52. h & obstacles;
  53. h & si;
  54. h & battlefieldType;
  55. h & terrainType;
  56. h & tacticsSide;
  57. h & tacticDistance;
  58. h & static_cast<CBonusSystemNode&>(*this);
  59. if (version > 824)
  60. h & replayAllowed;
  61. else
  62. replayAllowed = false;
  63. }
  64. //////////////////////////////////////////////////////////////////////////
  65. BattleInfo();
  66. virtual ~BattleInfo();
  67. //////////////////////////////////////////////////////////////////////////
  68. // IBattleInfo
  69. int32_t getActiveStackID() const override;
  70. TStacks getStacksIf(TStackFilter predicate) const override;
  71. battle::Units getUnitsIf(battle::UnitFilter predicate) const override;
  72. BattleField getBattlefieldType() const override;
  73. TerrainId getTerrainType() const override;
  74. ObstacleCList getAllObstacles() const override;
  75. PlayerColor getSidePlayer(ui8 side) const override;
  76. const CArmedInstance * getSideArmy(ui8 side) const override;
  77. const CGHeroInstance * getSideHero(ui8 side) const override;
  78. ui8 getTacticDist() const override;
  79. ui8 getTacticsSide() const override;
  80. const CGTownInstance * getDefendedTown() const override;
  81. EWallState getWallState(EWallPart partOfWall) const override;
  82. EGateState getGateState() const override;
  83. uint32_t getCastSpells(ui8 side) const override;
  84. int32_t getEnchanterCounter(ui8 side) const override;
  85. const IBonusBearer * getBonusBearer() const override;
  86. uint32_t nextUnitId() const override;
  87. int64_t getActualDamage(const DamageRange & damage, int32_t attackerCount, vstd::RNG & rng) const override;
  88. //////////////////////////////////////////////////////////////////////////
  89. // IBattleState
  90. void nextRound(int32_t roundNr) override;
  91. void nextTurn(uint32_t unitId) override;
  92. void addUnit(uint32_t id, const JsonNode & data) override;
  93. void moveUnit(uint32_t id, BattleHex destination) override;
  94. void setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta) override;
  95. void removeUnit(uint32_t id) override;
  96. void updateUnit(uint32_t id, const JsonNode & data) override;
  97. void addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  98. void updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  99. void removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  100. void setWallState(EWallPart partOfWall, EWallState state) override;
  101. void addObstacle(const ObstacleChanges & changes) override;
  102. void updateObstacle(const ObstacleChanges& changes) override;
  103. void removeObstacle(uint32_t id) override;
  104. static void addOrUpdateUnitBonus(CStack * sta, const Bonus & value, bool forceAdd);
  105. //////////////////////////////////////////////////////////////////////////
  106. CStack * getStack(int stackID, bool onlyAlive = true);
  107. using CBattleInfoEssentials::battleGetArmyObject;
  108. CArmedInstance * battleGetArmyObject(ui8 side) const;
  109. using CBattleInfoEssentials::battleGetFightingHero;
  110. CGHeroInstance * battleGetFightingHero(ui8 side) const;
  111. std::pair< std::vector<BattleHex>, int > getPath(BattleHex start, BattleHex dest, const battle::Unit * stack); //returned value: pair<path, length>; length may be different than number of elements in path since flying creatures jump between distant hexes
  112. void calculateCasualties(std::map<ui32,si32> * casualties) const; //casualties are array of maps size 2 (attacker, defeneder), maps are (crid => amount)
  113. CStack * generateNewStack(uint32_t id, const CStackInstance & base, ui8 side, const SlotID & slot, BattleHex position);
  114. CStack * generateNewStack(uint32_t id, const CStackBasicDescriptor & base, ui8 side, const SlotID & slot, BattleHex position);
  115. const CGHeroInstance * getHero(const PlayerColor & player) const; //returns fighting hero that belongs to given player
  116. void localInit();
  117. static BattleInfo * setupBattle(const int3 & tile, TerrainId, const BattleField & battlefieldType, const CArmedInstance * armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance * town);
  118. ui8 whatSide(const PlayerColor & player) const;
  119. protected:
  120. #if SCRIPTING_ENABLED
  121. scripting::Pool * getContextPool() const override;
  122. #endif
  123. };
  124. class DLL_LINKAGE CMP_stack
  125. {
  126. int phase; //rules of which phase will be used
  127. int turn;
  128. uint8_t side;
  129. public:
  130. bool operator()(const battle::Unit * a, const battle::Unit * b) const;
  131. CMP_stack(int Phase = 1, int Turn = 0, uint8_t Side = BattleSide::ATTACKER);
  132. };
  133. VCMI_LIB_NAMESPACE_END