StackWithBonuses.h 5.4 KB

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