StackWithBonuses.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/HeroBonus.h"
  15. #include "../../lib/battle/BattleProxy.h"
  16. #include "../../lib/battle/CUnitState.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class CStack;
  19. VCMI_LIB_NAMESPACE_END
  20. class HypotheticBattle;
  21. ///Fake random generator, used by AI to evaluate random server behavior
  22. class RNGStub : public vstd::RNG
  23. {
  24. public:
  25. vstd::TRandI64 getInt64Range(int64_t lower, int64_t upper) override
  26. {
  27. return [=]()->int64_t
  28. {
  29. return (lower + upper)/2;
  30. };
  31. }
  32. vstd::TRand getDoubleRange(double lower, double upper) override
  33. {
  34. return [=]()->double
  35. {
  36. return (lower + upper)/2;
  37. };
  38. }
  39. };
  40. class StackWithBonuses : public battle::CUnitState, public virtual IBonusBearer
  41. {
  42. public:
  43. std::vector<Bonus> bonusesToAdd;
  44. std::vector<Bonus> bonusesToUpdate;
  45. std::set<std::shared_ptr<Bonus>> bonusesToRemove;
  46. StackWithBonuses(const HypotheticBattle * Owner, const CStack * Stack);
  47. StackWithBonuses(const HypotheticBattle * Owner, const battle::UnitInfo & info);
  48. virtual ~StackWithBonuses();
  49. StackWithBonuses & operator= (const battle::CUnitState & other);
  50. ///IUnitInfo
  51. const CCreature * unitType() const override;
  52. int32_t unitBaseAmount() const override;
  53. uint32_t unitId() const override;
  54. ui8 unitSide() const override;
  55. PlayerColor unitOwner() const override;
  56. SlotID unitSlot() const override;
  57. ///IBonusBearer
  58. TConstBonusListPtr getAllBonuses(const CSelector & selector, const CSelector & limit,
  59. const CBonusSystemNode * root = nullptr, const std::string & cachingStr = "") const override;
  60. int64_t getTreeVersion() const override;
  61. void addUnitBonus(const std::vector<Bonus> & bonus);
  62. void updateUnitBonus(const std::vector<Bonus> & bonus);
  63. void removeUnitBonus(const std::vector<Bonus> & bonus);
  64. void removeUnitBonus(const CSelector & selector);
  65. void spendMana(ServerCallback * server, const int spellCost) const override;
  66. std::string getDescription() const override;
  67. private:
  68. const IBonusBearer * origBearer;
  69. const HypotheticBattle * owner;
  70. const CCreature * type;
  71. ui32 baseAmount;
  72. uint32_t id;
  73. ui8 side;
  74. PlayerColor player;
  75. SlotID slot;
  76. };
  77. class HypotheticBattle : public BattleProxy, public battle::IUnitEnvironment
  78. {
  79. public:
  80. std::map<uint32_t, std::shared_ptr<StackWithBonuses>> stackStates;
  81. const Environment * env;
  82. HypotheticBattle(const Environment * ENV, Subject realBattle);
  83. bool unitHasAmmoCart(const battle::Unit * unit) const override;
  84. PlayerColor unitEffectiveOwner(const battle::Unit * unit) const override;
  85. std::shared_ptr<StackWithBonuses> getForUpdate(uint32_t id);
  86. int32_t getActiveStackID() const override;
  87. battle::Units getUnitsIf(battle::UnitFilter predicate) const override;
  88. void nextRound(int32_t roundNr) 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 TDmgRange & damage, int32_t attackerCount, vstd::RNG & rng) const override;
  104. int64_t getTreeVersion() const;
  105. #if SCRIPTING_ENABLED
  106. scripting::Pool * getContextPool() const override;
  107. #endif
  108. ServerCallback * getServerCallback();
  109. private:
  110. class HypotheticServerCallback : public ServerCallback
  111. {
  112. public:
  113. HypotheticServerCallback(HypotheticBattle * owner_);
  114. void complain(const std::string & problem) override;
  115. bool describeChanges() const override;
  116. vstd::RNG * getRNG() override;
  117. void apply(CPackForClient * pack) override;
  118. void apply(BattleLogMessage * pack) override;
  119. void apply(BattleStackMoved * pack) override;
  120. void apply(BattleUnitsChanged * pack) override;
  121. void apply(SetStackEffect * pack) override;
  122. void apply(StacksInjured * pack) override;
  123. void apply(BattleObstaclesChanged * pack) override;
  124. void apply(CatapultAttack * pack) override;
  125. private:
  126. HypotheticBattle * owner;
  127. RNGStub rngStub;
  128. };
  129. class HypotheticEnvironment : public Environment
  130. {
  131. public:
  132. HypotheticEnvironment(HypotheticBattle * owner_, const Environment * upperEnvironment);
  133. const Services * services() const override;
  134. const BattleCb * battle() const override;
  135. const GameCb * game() const override;
  136. vstd::CLoggerBase * logger() const override;
  137. events::EventBus * eventBus() const override;
  138. private:
  139. HypotheticBattle * owner;
  140. const Environment * env;
  141. };
  142. int32_t bonusTreeVersion;
  143. int32_t activeUnitId;
  144. mutable uint32_t nextId;
  145. std::unique_ptr<HypotheticServerCallback> serverCallback;
  146. std::unique_ptr<HypotheticEnvironment> localEnvironment;
  147. #if SCRIPTING_ENABLED
  148. mutable std::shared_ptr<scripting::Pool> pool;
  149. #endif
  150. mutable std::shared_ptr<events::EventBus> eventBus;
  151. };