StackWithBonuses.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 <vcmi/events/EventBus.h>
  13. #include "../../lib/NetPacks.h"
  14. #include "../../lib/CStack.h"
  15. #include "../../lib/ScriptHandler.h"
  16. #if SCRIPTING_ENABLED
  17. using scripting::Pool;
  18. #endif
  19. void actualizeEffect(TBonusListPtr target, const Bonus & ef)
  20. {
  21. for(auto & bonus : *target) //TODO: optimize
  22. {
  23. if(bonus->source == Bonus::SPELL_EFFECT && bonus->type == ef.type && bonus->subtype == ef.subtype)
  24. {
  25. if(bonus->turnsRemain < ef.turnsRemain)
  26. {
  27. bonus.reset(new Bonus(*bonus));
  28. bonus->turnsRemain = ef.turnsRemain;
  29. }
  30. }
  31. }
  32. }
  33. StackWithBonuses::StackWithBonuses(const HypotheticBattle * Owner, const CStack * Stack)
  34. : battle::CUnitState(),
  35. origBearer(Stack),
  36. owner(Owner),
  37. type(Stack->unitType()),
  38. baseAmount(Stack->unitBaseAmount()),
  39. id(Stack->unitId()),
  40. side(Stack->unitSide()),
  41. player(Stack->unitOwner()),
  42. slot(Stack->unitSlot())
  43. {
  44. localInit(Owner);
  45. battle::CUnitState::operator=(*Stack);
  46. }
  47. StackWithBonuses::StackWithBonuses(const HypotheticBattle * Owner, const battle::UnitInfo & info)
  48. : battle::CUnitState(),
  49. origBearer(nullptr),
  50. owner(Owner),
  51. baseAmount(info.count),
  52. id(info.id),
  53. side(info.side),
  54. slot(SlotID::SUMMONED_SLOT_PLACEHOLDER)
  55. {
  56. type = info.type.toCreature();
  57. origBearer = type;
  58. player = Owner->getSidePlayer(side);
  59. localInit(Owner);
  60. position = info.position;
  61. summoned = info.summoned;
  62. }
  63. StackWithBonuses::~StackWithBonuses() = default;
  64. StackWithBonuses & StackWithBonuses::operator=(const battle::CUnitState & other)
  65. {
  66. battle::CUnitState::operator=(other);
  67. return *this;
  68. }
  69. const CCreature * StackWithBonuses::unitType() const
  70. {
  71. return type;
  72. }
  73. int32_t StackWithBonuses::unitBaseAmount() const
  74. {
  75. return baseAmount;
  76. }
  77. uint32_t StackWithBonuses::unitId() const
  78. {
  79. return id;
  80. }
  81. ui8 StackWithBonuses::unitSide() const
  82. {
  83. return side;
  84. }
  85. PlayerColor StackWithBonuses::unitOwner() const
  86. {
  87. return player;
  88. }
  89. SlotID StackWithBonuses::unitSlot() const
  90. {
  91. return slot;
  92. }
  93. TConstBonusListPtr StackWithBonuses::getAllBonuses(const CSelector & selector, const CSelector & limit,
  94. const CBonusSystemNode * root, const std::string & cachingStr) const
  95. {
  96. TBonusListPtr ret = std::make_shared<BonusList>();
  97. TConstBonusListPtr originalList = origBearer->getAllBonuses(selector, limit, root, cachingStr);
  98. vstd::copy_if(*originalList, std::back_inserter(*ret), [this](const std::shared_ptr<Bonus> & b)
  99. {
  100. return !vstd::contains(bonusesToRemove, b);
  101. });
  102. for(const Bonus & bonus : bonusesToUpdate)
  103. {
  104. if(selector(&bonus) && (!limit || !limit(&bonus)))
  105. {
  106. if(ret->getFirst(Selector::source(Bonus::SPELL_EFFECT, bonus.sid).And(Selector::typeSubtype(bonus.type, bonus.subtype))))
  107. {
  108. actualizeEffect(ret, bonus);
  109. }
  110. else
  111. {
  112. auto b = std::make_shared<Bonus>(bonus);
  113. ret->push_back(b);
  114. }
  115. }
  116. }
  117. for(auto & bonus : bonusesToAdd)
  118. {
  119. auto b = std::make_shared<Bonus>(bonus);
  120. if(selector(b.get()) && (!limit || !limit(b.get())))
  121. ret->push_back(b);
  122. }
  123. //TODO limiters?
  124. return ret;
  125. }
  126. int64_t StackWithBonuses::getTreeVersion() const
  127. {
  128. return owner->getTreeVersion();
  129. }
  130. void StackWithBonuses::addUnitBonus(const std::vector<Bonus> & bonus)
  131. {
  132. vstd::concatenate(bonusesToAdd, bonus);
  133. }
  134. void StackWithBonuses::updateUnitBonus(const std::vector<Bonus> & bonus)
  135. {
  136. //TODO: optimize, actualize to last value
  137. vstd::concatenate(bonusesToUpdate, bonus);
  138. }
  139. void StackWithBonuses::removeUnitBonus(const std::vector<Bonus> & bonus)
  140. {
  141. for(auto & one : bonus)
  142. {
  143. CSelector selector([&one](const Bonus * b) -> bool
  144. {
  145. //compare everything but turnsRemain, limiter and propagator
  146. return one.duration == b->duration
  147. && one.type == b->type
  148. && one.subtype == b->subtype
  149. && one.source == b->source
  150. && one.val == b->val
  151. && one.sid == b->sid
  152. && one.valType == b->valType
  153. && one.additionalInfo == b->additionalInfo
  154. && one.effectRange == b->effectRange
  155. && one.description == b->description;
  156. });
  157. removeUnitBonus(selector);
  158. }
  159. }
  160. void StackWithBonuses::removeUnitBonus(const CSelector & selector)
  161. {
  162. TConstBonusListPtr toRemove = origBearer->getBonuses(selector);
  163. for(auto b : *toRemove)
  164. bonusesToRemove.insert(b);
  165. vstd::erase_if(bonusesToAdd, [&](const Bonus & b){return selector(&b);});
  166. vstd::erase_if(bonusesToUpdate, [&](const Bonus & b){return selector(&b);});
  167. }
  168. void StackWithBonuses::spendMana(ServerCallback * server, const int spellCost) const
  169. {
  170. //TODO: evaluate cast use
  171. }
  172. HypotheticBattle::HypotheticBattle(const Environment * ENV, Subject realBattle)
  173. : BattleProxy(realBattle),
  174. env(ENV),
  175. bonusTreeVersion(1)
  176. {
  177. auto activeUnit = realBattle->battleActiveUnit();
  178. activeUnitId = activeUnit ? activeUnit->unitId() : -1;
  179. nextId = 0x00F00000;
  180. eventBus.reset(new events::EventBus());
  181. localEnvironment.reset(new HypotheticEnvironment(this, env));
  182. serverCallback.reset(new HypotheticServerCallback(this));
  183. #if SCRIPTING_ENABLED
  184. pool.reset(new scripting::PoolImpl(localEnvironment.get(), serverCallback.get()));
  185. #endif
  186. }
  187. bool HypotheticBattle::unitHasAmmoCart(const battle::Unit * unit) const
  188. {
  189. //FIXME: check ammocart alive state here
  190. return false;
  191. }
  192. PlayerColor HypotheticBattle::unitEffectiveOwner(const battle::Unit * unit) const
  193. {
  194. return battleGetOwner(unit);
  195. }
  196. std::shared_ptr<StackWithBonuses> HypotheticBattle::getForUpdate(uint32_t id)
  197. {
  198. auto iter = stackStates.find(id);
  199. if(iter == stackStates.end())
  200. {
  201. const CStack * s = subject->battleGetStackByID(id, false);
  202. auto ret = std::make_shared<StackWithBonuses>(this, s);
  203. stackStates[id] = ret;
  204. return ret;
  205. }
  206. else
  207. {
  208. return iter->second;
  209. }
  210. }
  211. battle::Units HypotheticBattle::getUnitsIf(battle::UnitFilter predicate) const
  212. {
  213. battle::Units proxyed = BattleProxy::getUnitsIf(predicate);
  214. battle::Units ret;
  215. ret.reserve(proxyed.size());
  216. for(auto unit : proxyed)
  217. {
  218. //unit was not changed, trust proxyed data
  219. if(stackStates.find(unit->unitId()) == stackStates.end())
  220. ret.push_back(unit);
  221. }
  222. for(auto id_unit : stackStates)
  223. {
  224. if(predicate(id_unit.second.get()))
  225. ret.push_back(id_unit.second.get());
  226. }
  227. return ret;
  228. }
  229. int32_t HypotheticBattle::getActiveStackID() const
  230. {
  231. return activeUnitId;
  232. }
  233. void HypotheticBattle::nextRound(int32_t roundNr)
  234. {
  235. //TODO:HypotheticBattle::nextRound
  236. for(auto unit : battleAliveUnits())
  237. {
  238. auto forUpdate = getForUpdate(unit->unitId());
  239. //TODO: update Bonus::NTurns effects
  240. forUpdate->afterNewRound();
  241. }
  242. }
  243. void HypotheticBattle::nextTurn(uint32_t unitId)
  244. {
  245. activeUnitId = unitId;
  246. auto unit = getForUpdate(unitId);
  247. unit->removeUnitBonus(Bonus::UntilGetsTurn);
  248. unit->afterGetsTurn();
  249. }
  250. void HypotheticBattle::addUnit(uint32_t id, const JsonNode & data)
  251. {
  252. battle::UnitInfo info;
  253. info.load(id, data);
  254. std::shared_ptr<StackWithBonuses> newUnit = std::make_shared<StackWithBonuses>(this, info);
  255. stackStates[newUnit->unitId()] = newUnit;
  256. }
  257. void HypotheticBattle::moveUnit(uint32_t id, BattleHex destination)
  258. {
  259. std::shared_ptr<StackWithBonuses> changed = getForUpdate(id);
  260. changed->position = destination;
  261. }
  262. void HypotheticBattle::setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta)
  263. {
  264. std::shared_ptr<StackWithBonuses> changed = getForUpdate(id);
  265. changed->load(data);
  266. if(healthDelta < 0)
  267. {
  268. changed->removeUnitBonus(Bonus::UntilBeingAttacked);
  269. }
  270. }
  271. void HypotheticBattle::removeUnit(uint32_t id)
  272. {
  273. std::set<uint32_t> ids;
  274. ids.insert(id);
  275. while(!ids.empty())
  276. {
  277. auto toRemoveId = *ids.begin();
  278. auto toRemove = getForUpdate(toRemoveId);
  279. if(!toRemove->ghost)
  280. {
  281. toRemove->onRemoved();
  282. //TODO: emulate detachFromAll() somehow
  283. //stack may be removed instantly (not being killed first)
  284. //handle clone remove also here
  285. if(toRemove->cloneID >= 0)
  286. {
  287. ids.insert(toRemove->cloneID);
  288. toRemove->cloneID = -1;
  289. }
  290. //TODO: cleanup remaining clone links if any
  291. // for(auto s : stacks)
  292. // {
  293. // if(s->cloneID == toRemoveId)
  294. // s->cloneID = -1;
  295. // }
  296. }
  297. ids.erase(toRemoveId);
  298. }
  299. }
  300. void HypotheticBattle::updateUnit(uint32_t id, const JsonNode & data)
  301. {
  302. //TODO:
  303. }
  304. void HypotheticBattle::addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
  305. {
  306. getForUpdate(id)->addUnitBonus(bonus);
  307. bonusTreeVersion++;
  308. }
  309. void HypotheticBattle::updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
  310. {
  311. getForUpdate(id)->updateUnitBonus(bonus);
  312. bonusTreeVersion++;
  313. }
  314. void HypotheticBattle::removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
  315. {
  316. getForUpdate(id)->removeUnitBonus(bonus);
  317. bonusTreeVersion++;
  318. }
  319. void HypotheticBattle::setWallState(int partOfWall, si8 state)
  320. {
  321. //TODO:HypotheticBattle::setWallState
  322. }
  323. void HypotheticBattle::addObstacle(const ObstacleChanges & changes)
  324. {
  325. //TODO:HypotheticBattle::addObstacle
  326. }
  327. void HypotheticBattle::updateObstacle(const ObstacleChanges& changes)
  328. {
  329. //TODO:HypotheticBattle::updateObstacle
  330. }
  331. void HypotheticBattle::removeObstacle(uint32_t id)
  332. {
  333. //TODO:HypotheticBattle::removeObstacle
  334. }
  335. uint32_t HypotheticBattle::nextUnitId() const
  336. {
  337. return nextId++;
  338. }
  339. int64_t HypotheticBattle::getActualDamage(const TDmgRange & damage, int32_t attackerCount, vstd::RNG & rng) const
  340. {
  341. return (damage.first + damage.second) / 2;
  342. }
  343. int64_t HypotheticBattle::getTreeVersion() const
  344. {
  345. return getBattleNode()->getTreeVersion() + bonusTreeVersion;
  346. }
  347. #if SCRIPTING_ENABLED
  348. Pool * HypotheticBattle::getContextPool() const
  349. {
  350. return pool.get();
  351. }
  352. #endif
  353. ServerCallback * HypotheticBattle::getServerCallback()
  354. {
  355. return serverCallback.get();
  356. }
  357. HypotheticBattle::HypotheticServerCallback::HypotheticServerCallback(HypotheticBattle * owner_)
  358. :owner(owner_)
  359. {
  360. }
  361. void HypotheticBattle::HypotheticServerCallback::complain(const std::string & problem)
  362. {
  363. logAi->error(problem);
  364. }
  365. bool HypotheticBattle::HypotheticServerCallback::describeChanges() const
  366. {
  367. return false;
  368. }
  369. vstd::RNG * HypotheticBattle::HypotheticServerCallback::getRNG()
  370. {
  371. return &rngStub;
  372. }
  373. void HypotheticBattle::HypotheticServerCallback::apply(CPackForClient * pack)
  374. {
  375. logAi->error("Package of type %s is not allowed in battle evaluation", typeid(pack).name());
  376. }
  377. void HypotheticBattle::HypotheticServerCallback::apply(BattleLogMessage * pack)
  378. {
  379. pack->applyBattle(owner);
  380. }
  381. void HypotheticBattle::HypotheticServerCallback::apply(BattleStackMoved * pack)
  382. {
  383. pack->applyBattle(owner);
  384. }
  385. void HypotheticBattle::HypotheticServerCallback::apply(BattleUnitsChanged * pack)
  386. {
  387. pack->applyBattle(owner);
  388. }
  389. void HypotheticBattle::HypotheticServerCallback::apply(SetStackEffect * pack)
  390. {
  391. pack->applyBattle(owner);
  392. }
  393. void HypotheticBattle::HypotheticServerCallback::apply(StacksInjured * pack)
  394. {
  395. pack->applyBattle(owner);
  396. }
  397. void HypotheticBattle::HypotheticServerCallback::apply(BattleObstaclesChanged * pack)
  398. {
  399. pack->applyBattle(owner);
  400. }
  401. void HypotheticBattle::HypotheticServerCallback::apply(CatapultAttack * pack)
  402. {
  403. pack->applyBattle(owner);
  404. }
  405. HypotheticBattle::HypotheticEnvironment::HypotheticEnvironment(HypotheticBattle * owner_, const Environment * upperEnvironment)
  406. : owner(owner_),
  407. env(upperEnvironment)
  408. {
  409. }
  410. const Services * HypotheticBattle::HypotheticEnvironment::services() const
  411. {
  412. return env->services();
  413. }
  414. const Environment::BattleCb * HypotheticBattle::HypotheticEnvironment::battle() const
  415. {
  416. return owner;
  417. }
  418. const Environment::GameCb * HypotheticBattle::HypotheticEnvironment::game() const
  419. {
  420. return env->game();
  421. }
  422. vstd::CLoggerBase * HypotheticBattle::HypotheticEnvironment::logger() const
  423. {
  424. return env->logger();
  425. }
  426. events::EventBus * HypotheticBattle::HypotheticEnvironment::eventBus() const
  427. {
  428. return owner->eventBus.get();
  429. }