StackWithBonuses.h 5.8 KB

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