StackWithBonuses.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * StackWithBonuses.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 <vstd/RNG.h>
  12. #include <vcmi/Environment.h>
  13. #include <vcmi/ServerCallback.h>
  14. #include "../../lib/bonuses/Bonus.h"
  15. #include "../../lib/battle/BattleProxy.h"
  16. #include "../../lib/battle/CUnitState.h"
  17. class HypotheticBattle;
  18. ///Fake random generator, used by AI to evaluate random server behavior
  19. class RNGStub final : public vstd::RNG
  20. {
  21. public:
  22. int nextInt() override
  23. {
  24. return 0;
  25. }
  26. int nextBinomialInt(int coinsCount, double coinChance) override
  27. {
  28. return coinsCount * coinChance;
  29. }
  30. int nextInt(int lower, int upper) override
  31. {
  32. return (lower + upper) / 2;
  33. }
  34. int64_t nextInt64(int64_t lower, int64_t upper) override
  35. {
  36. return (lower + upper) / 2;
  37. }
  38. double nextDouble(double lower, double upper) override
  39. {
  40. return (lower + upper) / 2;
  41. }
  42. int nextInt(int upper) override
  43. {
  44. return upper / 2;
  45. }
  46. int64_t nextInt64(int64_t upper) override
  47. {
  48. return upper / 2;
  49. }
  50. double nextDouble(double upper) override
  51. {
  52. return upper / 2;
  53. }
  54. };
  55. class StackWithBonuses : public battle::CUnitState, public virtual IBonusBearer
  56. {
  57. public:
  58. std::vector<Bonus> bonusesToAdd;
  59. std::vector<Bonus> bonusesToUpdate;
  60. std::set<std::shared_ptr<Bonus>> bonusesToRemove;
  61. int treeVersionLocal;
  62. StackWithBonuses(const HypotheticBattle * Owner, const battle::CUnitState * Stack);
  63. StackWithBonuses(const HypotheticBattle * Owner, const battle::Unit * Stack);
  64. StackWithBonuses(const HypotheticBattle * Owner, const battle::UnitInfo & info);
  65. virtual ~StackWithBonuses();
  66. StackWithBonuses & operator= (const battle::CUnitState & other);
  67. ///IUnitInfo
  68. const CCreature * unitType() const override;
  69. int32_t unitBaseAmount() const override;
  70. uint32_t unitId() const override;
  71. BattleSide unitSide() const override;
  72. PlayerColor unitOwner() const override;
  73. SlotID unitSlot() const override;
  74. ///IBonusBearer
  75. TConstBonusListPtr getAllBonuses(const CSelector & selector, const CSelector & limit,
  76. const std::string & cachingStr = "") const override;
  77. int64_t getTreeVersion() const override;
  78. void addUnitBonus(const std::vector<Bonus> & bonus);
  79. void updateUnitBonus(const std::vector<Bonus> & bonus);
  80. void removeUnitBonus(const std::vector<Bonus> & bonus);
  81. void removeUnitBonus(const CSelector & selector);
  82. void spendMana(ServerCallback * server, const int spellCost) const override;
  83. std::string getDescription() const override;
  84. private:
  85. const IBonusBearer * origBearer;
  86. const HypotheticBattle * owner;
  87. const CCreature * type;
  88. ui32 baseAmount;
  89. uint32_t id;
  90. BattleSide side;
  91. PlayerColor player;
  92. SlotID slot;
  93. };
  94. class HypotheticBattle : public BattleProxy, public battle::IUnitEnvironment
  95. {
  96. public:
  97. std::map<uint32_t, std::shared_ptr<StackWithBonuses>> stackStates;
  98. const Environment * env;
  99. HypotheticBattle(const Environment * ENV, Subject realBattle);
  100. bool unitHasAmmoCart(const battle::Unit * unit) const override;
  101. PlayerColor unitEffectiveOwner(const battle::Unit * unit) const override;
  102. std::shared_ptr<StackWithBonuses> getForUpdate(uint32_t id);
  103. BattleID getBattleID() const override;
  104. int32_t getActiveStackID() const override;
  105. battle::Units getUnitsIf(const battle::UnitFilter & predicate) const override;
  106. void nextRound() override;
  107. void nextTurn(uint32_t unitId) override;
  108. void addUnit(uint32_t id, const JsonNode & data) override;
  109. void setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta) override;
  110. void moveUnit(uint32_t id, BattleHex destination) override;
  111. void removeUnit(uint32_t id) override;
  112. void updateUnit(uint32_t id, const JsonNode & data) override;
  113. void addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  114. void updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  115. void removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  116. void setWallState(EWallPart partOfWall, EWallState state) override;
  117. void addObstacle(const ObstacleChanges & changes) override;
  118. void updateObstacle(const ObstacleChanges& changes) override;
  119. void removeObstacle(uint32_t id) override;
  120. uint32_t nextUnitId() const override;
  121. int64_t getActualDamage(const DamageRange & damage, int32_t attackerCount, vstd::RNG & rng) const override;
  122. std::vector<SpellID> getUsedSpells(BattleSide side) const override;
  123. int3 getLocation() const override;
  124. BattleLayout getLayout() const override;
  125. int64_t getTreeVersion() const;
  126. void makeWait(const battle::Unit * activeStack);
  127. void resetActiveUnit()
  128. {
  129. activeUnitId = -1;
  130. }
  131. #if SCRIPTING_ENABLED
  132. scripting::Pool * getContextPool() const override;
  133. #endif
  134. ServerCallback * getServerCallback();
  135. private:
  136. class HypotheticServerCallback : public ServerCallback
  137. {
  138. public:
  139. HypotheticServerCallback(HypotheticBattle * owner_);
  140. void complain(const std::string & problem) override;
  141. bool describeChanges() const override;
  142. vstd::RNG * getRNG() override;
  143. void apply(CPackForClient * pack) override;
  144. void apply(BattleLogMessage * pack) override;
  145. void apply(BattleStackMoved * pack) override;
  146. void apply(BattleUnitsChanged * pack) override;
  147. void apply(SetStackEffect * pack) override;
  148. void apply(StacksInjured * pack) override;
  149. void apply(BattleObstaclesChanged * pack) override;
  150. void apply(CatapultAttack * pack) override;
  151. private:
  152. HypotheticBattle * owner;
  153. RNGStub rngStub;
  154. };
  155. class HypotheticEnvironment : public Environment
  156. {
  157. public:
  158. HypotheticEnvironment(HypotheticBattle * owner_, const Environment * upperEnvironment);
  159. const Services * services() const override;
  160. const BattleCb * battle(const BattleID & battleID) const override;
  161. const GameCb * game() const override;
  162. vstd::CLoggerBase * logger() const override;
  163. events::EventBus * eventBus() const override;
  164. private:
  165. HypotheticBattle * owner;
  166. const Environment * env;
  167. };
  168. int32_t bonusTreeVersion;
  169. int32_t activeUnitId;
  170. mutable uint32_t nextId;
  171. std::unique_ptr<HypotheticServerCallback> serverCallback;
  172. std::unique_ptr<HypotheticEnvironment> localEnvironment;
  173. #if SCRIPTING_ENABLED
  174. mutable std::shared_ptr<scripting::Pool> pool;
  175. #endif
  176. mutable std::shared_ptr<events::EventBus> eventBus;
  177. };