StackWithBonuses.cpp 8.8 KB

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