StackWithBonuses.cpp 11 KB

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