StackWithBonuses.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 std::string & cachingStr = "") const override;
  76. int32_t getTreeVersion() const override;
  77. void addUnitBonus(const std::vector<Bonus> & bonus);
  78. void updateUnitBonus(const std::vector<Bonus> & bonus);
  79. void removeUnitBonus(const std::vector<Bonus> & bonus);
  80. void removeUnitBonus(const CSelector & selector);
  81. void spendMana(ServerCallback * server, const int spellCost) const override;
  82. std::string getDescription() const override;
  83. private:
  84. const IBonusBearer * origBearer;
  85. const HypotheticBattle * owner;
  86. const CCreature * type;
  87. ui32 baseAmount;
  88. uint32_t id;
  89. BattleSide side;
  90. PlayerColor player;
  91. SlotID slot;
  92. };
  93. class HypotheticBattle : public BattleProxy, public battle::IUnitEnvironment
  94. {
  95. public:
  96. std::map<uint32_t, std::shared_ptr<StackWithBonuses>> stackStates;
  97. const Environment * env;
  98. HypotheticBattle(const Environment * ENV, Subject realBattle);
  99. bool unitHasAmmoCart(const battle::Unit * unit) const override;
  100. PlayerColor unitEffectiveOwner(const battle::Unit * unit) const override;
  101. std::shared_ptr<StackWithBonuses> getForUpdate(uint32_t id);
  102. BattleID getBattleID() const override;
  103. int32_t getActiveStackID() const override;
  104. battle::Units getUnitsIf(const battle::UnitFilter & predicate) const override;
  105. void nextRound() override;
  106. void nextTurn(uint32_t unitId, BattleUnitTurnReason reason) override;
  107. void addUnit(uint32_t id, const JsonNode & data) override;
  108. void setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta) override;
  109. void moveUnit(uint32_t id, const BattleHex & destination) override;
  110. void removeUnit(uint32_t id) override;
  111. void updateUnit(uint32_t id, const JsonNode & data) override;
  112. void addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  113. void updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  114. void removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  115. void setWallState(EWallPart partOfWall, EWallState state) override;
  116. void addObstacle(const ObstacleChanges & changes) override;
  117. void updateObstacle(const ObstacleChanges& changes) override;
  118. void removeObstacle(uint32_t id) override;
  119. uint32_t nextUnitId() const override;
  120. int64_t getActualDamage(const DamageRange & damage, int32_t attackerCount, vstd::RNG & rng) const override;
  121. std::vector<SpellID> getUsedSpells(BattleSide side) const override;
  122. int3 getLocation() const override;
  123. BattleLayout getLayout() const override;
  124. int32_t getTreeVersion() const;
  125. void makeWait(const battle::Unit * activeStack);
  126. void resetActiveUnit()
  127. {
  128. activeUnitId = -1;
  129. }
  130. #if SCRIPTING_ENABLED
  131. scripting::Pool * getContextPool() const override;
  132. #endif
  133. ServerCallback * getServerCallback();
  134. private:
  135. class HypotheticServerCallback : public ServerCallback
  136. {
  137. public:
  138. HypotheticServerCallback(HypotheticBattle * owner_);
  139. void complain(const std::string & problem) override;
  140. bool describeChanges() const override;
  141. vstd::RNG * getRNG() override;
  142. void apply(CPackForClient & pack) override;
  143. void apply(BattleLogMessage & pack) override;
  144. void apply(BattleStackMoved & pack) override;
  145. void apply(BattleUnitsChanged & pack) override;
  146. void apply(SetStackEffect & pack) override;
  147. void apply(StacksInjured & pack) override;
  148. void apply(BattleObstaclesChanged & pack) override;
  149. void apply(CatapultAttack & pack) override;
  150. private:
  151. HypotheticBattle * owner;
  152. RNGStub rngStub;
  153. };
  154. class HypotheticEnvironment : public Environment
  155. {
  156. public:
  157. HypotheticEnvironment(HypotheticBattle * owner_, const Environment * upperEnvironment);
  158. const Services * services() const override;
  159. const BattleCb * battle(const BattleID & battleID) const override;
  160. const GameCb * game() const override;
  161. vstd::CLoggerBase * logger() const override;
  162. events::EventBus * eventBus() const override;
  163. private:
  164. HypotheticBattle * owner;
  165. const Environment * env;
  166. };
  167. int32_t bonusTreeVersion;
  168. int32_t activeUnitId;
  169. mutable uint32_t nextId;
  170. std::unique_ptr<HypotheticServerCallback> serverCallback;
  171. std::unique_ptr<HypotheticEnvironment> localEnvironment;
  172. #if SCRIPTING_ENABLED
  173. mutable std::shared_ptr<scripting::Pool> pool;
  174. #endif
  175. mutable std::shared_ptr<events::EventBus> eventBus;
  176. };