BattleInfo.h 5.8 KB

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