StackWithBonuses.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * StackWithBonuses.cpp, 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. #include "StdInc.h"
  11. #include "StackWithBonuses.h"
  12. #include "../../lib/NetPacksBase.h"
  13. #include "../../lib/CStack.h"
  14. void actualizeEffect(TBonusListPtr target, const Bonus & ef)
  15. {
  16. for(auto bonus : *target) //TODO: optimize
  17. {
  18. if(bonus->source == Bonus::SPELL_EFFECT && bonus->type == ef.type && bonus->subtype == ef.subtype)
  19. {
  20. bonus->turnsRemain = std::max(bonus->turnsRemain, ef.turnsRemain);
  21. }
  22. }
  23. }
  24. StackWithBonuses::StackWithBonuses(const HypotheticBattle * Owner, const CStack * Stack)
  25. : battle::CUnitState(),
  26. origBearer(Stack),
  27. owner(Owner),
  28. type(Stack->unitType()),
  29. baseAmount(Stack->unitBaseAmount()),
  30. id(Stack->unitId()),
  31. side(Stack->unitSide()),
  32. player(Stack->unitOwner()),
  33. slot(Stack->unitSlot())
  34. {
  35. localInit(Owner);
  36. battle::CUnitState::operator=(*Stack);
  37. }
  38. StackWithBonuses::StackWithBonuses(const HypotheticBattle * Owner, const battle::UnitInfo & info)
  39. : battle::CUnitState(),
  40. origBearer(nullptr),
  41. owner(Owner),
  42. baseAmount(info.count),
  43. id(info.id),
  44. side(info.side),
  45. slot(SlotID::SUMMONED_SLOT_PLACEHOLDER)
  46. {
  47. type = info.type.toCreature();
  48. origBearer = type;
  49. player = Owner->getSidePlayer(side);
  50. localInit(Owner);
  51. position = info.position;
  52. summoned = info.summoned;
  53. }
  54. StackWithBonuses::~StackWithBonuses() = default;
  55. StackWithBonuses & StackWithBonuses::operator=(const battle::CUnitState & other)
  56. {
  57. battle::CUnitState::operator=(other);
  58. return *this;
  59. }
  60. const CCreature * StackWithBonuses::unitType() const
  61. {
  62. return type;
  63. }
  64. int32_t StackWithBonuses::unitBaseAmount() const
  65. {
  66. return baseAmount;
  67. }
  68. uint32_t StackWithBonuses::unitId() const
  69. {
  70. return id;
  71. }
  72. ui8 StackWithBonuses::unitSide() const
  73. {
  74. return side;
  75. }
  76. PlayerColor StackWithBonuses::unitOwner() const
  77. {
  78. return player;
  79. }
  80. SlotID StackWithBonuses::unitSlot() const
  81. {
  82. return slot;
  83. }
  84. const TBonusListPtr StackWithBonuses::getAllBonuses(const CSelector & selector, const CSelector & limit,
  85. const CBonusSystemNode * root, const std::string & cachingStr) const
  86. {
  87. TBonusListPtr ret = std::make_shared<BonusList>();
  88. const TBonusListPtr originalList = origBearer->getAllBonuses(selector, limit, root, cachingStr);
  89. vstd::copy_if(*originalList, std::back_inserter(*ret), [this](const std::shared_ptr<Bonus> & b)
  90. {
  91. return !vstd::contains(bonusesToRemove, b);
  92. });
  93. for(const Bonus & bonus : bonusesToUpdate)
  94. {
  95. if(selector(&bonus) && (!limit || !limit(&bonus)))
  96. {
  97. if(ret->getFirst(Selector::source(Bonus::SPELL_EFFECT, bonus.sid).And(Selector::typeSubtype(bonus.type, bonus.subtype))))
  98. {
  99. actualizeEffect(ret, bonus);
  100. }
  101. else
  102. {
  103. auto b = std::make_shared<Bonus>(bonus);
  104. ret->push_back(b);
  105. }
  106. }
  107. }
  108. for(auto & bonus : bonusesToAdd)
  109. {
  110. auto b = std::make_shared<Bonus>(bonus);
  111. if(selector(b.get()) && (!limit || !limit(b.get())))
  112. ret->push_back(b);
  113. }
  114. //TODO limiters?
  115. return ret;
  116. }
  117. int64_t StackWithBonuses::getTreeVersion() const
  118. {
  119. return owner->getTreeVersion();
  120. }
  121. void StackWithBonuses::addUnitBonus(const std::vector<Bonus> & bonus)
  122. {
  123. vstd::concatenate(bonusesToAdd, bonus);
  124. }
  125. void StackWithBonuses::updateUnitBonus(const std::vector<Bonus> & bonus)
  126. {
  127. //TODO: optimize, actualize to last value
  128. vstd::concatenate(bonusesToUpdate, bonus);
  129. }
  130. void StackWithBonuses::removeUnitBonus(const std::vector<Bonus> & bonus)
  131. {
  132. for(auto & one : bonus)
  133. {
  134. CSelector selector([&one](const Bonus * b) -> bool
  135. {
  136. //compare everything but turnsRemain, limiter and propagator
  137. return one.duration == b->duration
  138. && one.type == b->type
  139. && one.subtype == b->subtype
  140. && one.source == b->source
  141. && one.val == b->val
  142. && one.sid == b->sid
  143. && one.valType == b->valType
  144. && one.additionalInfo == b->additionalInfo
  145. && one.effectRange == b->effectRange
  146. && one.description == b->description;
  147. });
  148. removeUnitBonus(selector);
  149. }
  150. }
  151. void StackWithBonuses::removeUnitBonus(const CSelector & selector)
  152. {
  153. TBonusListPtr toRemove = origBearer->getBonuses(selector);
  154. for(auto b : *toRemove)
  155. bonusesToRemove.insert(b);
  156. vstd::erase_if(bonusesToAdd, [&](const Bonus & b){return selector(&b);});
  157. vstd::erase_if(bonusesToUpdate, [&](const Bonus & b){return selector(&b);});
  158. }
  159. void StackWithBonuses::spendMana(const spells::PacketSender * server, const int spellCost) const
  160. {
  161. //TODO: evaluate cast use
  162. }
  163. HypotheticBattle::HypotheticBattle(Subject realBattle)
  164. : BattleProxy(realBattle),
  165. bonusTreeVersion(1)
  166. {
  167. auto activeUnit = realBattle->battleActiveUnit();
  168. activeUnitId = activeUnit ? activeUnit->unitId() : -1;
  169. nextId = 0xF0000000;
  170. }
  171. bool HypotheticBattle::unitHasAmmoCart(const battle::Unit * unit) const
  172. {
  173. //FIXME: check ammocart alive state here
  174. return false;
  175. }
  176. PlayerColor HypotheticBattle::unitEffectiveOwner(const battle::Unit * unit) const
  177. {
  178. return battleGetOwner(unit);
  179. }
  180. std::shared_ptr<StackWithBonuses> HypotheticBattle::getForUpdate(uint32_t id)
  181. {
  182. auto iter = stackStates.find(id);
  183. if(iter == stackStates.end())
  184. {
  185. const CStack * s = subject->battleGetStackByID(id, false);
  186. auto ret = std::make_shared<StackWithBonuses>(this, s);
  187. stackStates[id] = ret;
  188. return ret;
  189. }
  190. else
  191. {
  192. return iter->second;
  193. }
  194. }
  195. battle::Units HypotheticBattle::getUnitsIf(battle::UnitFilter predicate) const
  196. {
  197. battle::Units proxyed = BattleProxy::getUnitsIf(predicate);
  198. battle::Units ret;
  199. ret.reserve(proxyed.size());
  200. for(auto unit : proxyed)
  201. {
  202. //unit was not changed, trust proxyed data
  203. if(stackStates.find(unit->unitId()) == stackStates.end())
  204. ret.push_back(unit);
  205. }
  206. for(auto id_unit : stackStates)
  207. {
  208. if(predicate(id_unit.second.get()))
  209. ret.push_back(id_unit.second.get());
  210. }
  211. return ret;
  212. }
  213. int32_t HypotheticBattle::getActiveStackID() const
  214. {
  215. return activeUnitId;
  216. }
  217. void HypotheticBattle::nextRound(int32_t roundNr)
  218. {
  219. //TODO:HypotheticBattle::nextRound
  220. for(auto unit : battleAliveUnits())
  221. {
  222. auto forUpdate = getForUpdate(unit->unitId());
  223. //TODO: update Bonus::NTurns effects
  224. forUpdate->afterNewRound();
  225. }
  226. }
  227. void HypotheticBattle::nextTurn(uint32_t unitId)
  228. {
  229. activeUnitId = unitId;
  230. auto unit = getForUpdate(unitId);
  231. unit->removeUnitBonus(Bonus::UntilGetsTurn);
  232. unit->afterGetsTurn();
  233. }
  234. void HypotheticBattle::addUnit(uint32_t id, const JsonNode & data)
  235. {
  236. battle::UnitInfo info;
  237. info.load(id, data);
  238. std::shared_ptr<StackWithBonuses> newUnit = std::make_shared<StackWithBonuses>(this, info);
  239. stackStates[newUnit->unitId()] = newUnit;
  240. }
  241. void HypotheticBattle::moveUnit(uint32_t id, BattleHex destination)
  242. {
  243. std::shared_ptr<StackWithBonuses> changed = getForUpdate(id);
  244. changed->position = destination;
  245. }
  246. void HypotheticBattle::setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta)
  247. {
  248. std::shared_ptr<StackWithBonuses> changed = getForUpdate(id);
  249. changed->load(data);
  250. if(healthDelta < 0)
  251. {
  252. changed->removeUnitBonus(Bonus::UntilBeingAttacked);
  253. }
  254. }
  255. void HypotheticBattle::removeUnit(uint32_t id)
  256. {
  257. std::set<uint32_t> ids;
  258. ids.insert(id);
  259. while(!ids.empty())
  260. {
  261. auto toRemoveId = *ids.begin();
  262. auto toRemove = getForUpdate(toRemoveId);
  263. if(!toRemove->ghost)
  264. {
  265. toRemove->onRemoved();
  266. //TODO: emulate detachFromAll() somehow
  267. //stack may be removed instantly (not being killed first)
  268. //handle clone remove also here
  269. if(toRemove->cloneID >= 0)
  270. {
  271. ids.insert(toRemove->cloneID);
  272. toRemove->cloneID = -1;
  273. }
  274. //TODO: cleanup remaining clone links if any
  275. // for(auto s : stacks)
  276. // {
  277. // if(s->cloneID == toRemoveId)
  278. // s->cloneID = -1;
  279. // }
  280. }
  281. ids.erase(toRemoveId);
  282. }
  283. }
  284. void HypotheticBattle::addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
  285. {
  286. getForUpdate(id)->addUnitBonus(bonus);
  287. bonusTreeVersion++;
  288. }
  289. void HypotheticBattle::updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
  290. {
  291. getForUpdate(id)->updateUnitBonus(bonus);
  292. bonusTreeVersion++;
  293. }
  294. void HypotheticBattle::removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
  295. {
  296. getForUpdate(id)->removeUnitBonus(bonus);
  297. bonusTreeVersion++;
  298. }
  299. void HypotheticBattle::setWallState(int partOfWall, si8 state)
  300. {
  301. //TODO:HypotheticBattle::setWallState
  302. }
  303. void HypotheticBattle::addObstacle(const ObstacleChanges & changes)
  304. {
  305. //TODO:HypotheticBattle::addObstacle
  306. }
  307. void HypotheticBattle::removeObstacle(uint32_t id)
  308. {
  309. //TODO:HypotheticBattle::removeObstacle
  310. }
  311. uint32_t HypotheticBattle::nextUnitId() const
  312. {
  313. return nextId++;
  314. }
  315. int64_t HypotheticBattle::getActualDamage(const TDmgRange & damage, int32_t attackerCount, vstd::RNG & rng) const
  316. {
  317. return (damage.first + damage.second) / 2;
  318. }
  319. int64_t HypotheticBattle::getTreeVersion() const
  320. {
  321. return getBattleNode()->getTreeVersion() + bonusTreeVersion;
  322. }