StackWithBonuses.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 == BonusSource::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 battle::CUnitState * Stack)
  34. : battle::CUnitState(),
  35. origBearer(Stack->getBonusBearer()),
  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(BonusSource::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. auto result = owner->getTreeVersion();
  129. if(bonusesToAdd.empty() && bonusesToUpdate.empty() && bonusesToRemove.empty())
  130. return result;
  131. else
  132. return result + treeVersionLocal;
  133. }
  134. void StackWithBonuses::addUnitBonus(const std::vector<Bonus> & bonus)
  135. {
  136. vstd::concatenate(bonusesToAdd, bonus);
  137. treeVersionLocal++;
  138. }
  139. void StackWithBonuses::updateUnitBonus(const std::vector<Bonus> & bonus)
  140. {
  141. //TODO: optimize, actualize to last value
  142. vstd::concatenate(bonusesToUpdate, bonus);
  143. treeVersionLocal++;
  144. }
  145. void StackWithBonuses::removeUnitBonus(const std::vector<Bonus> & bonus)
  146. {
  147. for(auto & one : bonus)
  148. {
  149. CSelector selector([&one](const Bonus * b) -> bool
  150. {
  151. //compare everything but turnsRemain, limiter and propagator
  152. return one.duration == b->duration
  153. && one.type == b->type
  154. && one.subtype == b->subtype
  155. && one.source == b->source
  156. && one.val == b->val
  157. && one.sid == b->sid
  158. && one.valType == b->valType
  159. && one.additionalInfo == b->additionalInfo
  160. && one.effectRange == b->effectRange
  161. && one.description == b->description;
  162. });
  163. removeUnitBonus(selector);
  164. }
  165. }
  166. void StackWithBonuses::removeUnitBonus(const CSelector & selector)
  167. {
  168. TConstBonusListPtr toRemove = origBearer->getBonuses(selector);
  169. for(auto b : *toRemove)
  170. bonusesToRemove.insert(b);
  171. vstd::erase_if(bonusesToAdd, [&](const Bonus & b){return selector(&b);});
  172. vstd::erase_if(bonusesToUpdate, [&](const Bonus & b){return selector(&b);});
  173. treeVersionLocal++;
  174. }
  175. std::string StackWithBonuses::getDescription() const
  176. {
  177. std::ostringstream oss;
  178. oss << unitOwner().getStr();
  179. oss << " battle stack [" << unitId() << "]: " << getCount() << " of ";
  180. if(type)
  181. oss << type->getJsonKey();
  182. else
  183. oss << "[UNDEFINED TYPE]";
  184. oss << " from slot " << slot;
  185. return oss.str();
  186. }
  187. void StackWithBonuses::spendMana(ServerCallback * server, const int spellCost) const
  188. {
  189. //TODO: evaluate cast use
  190. }
  191. HypotheticBattle::HypotheticBattle(const Environment * ENV, Subject realBattle)
  192. : BattleProxy(realBattle),
  193. env(ENV),
  194. bonusTreeVersion(1)
  195. {
  196. auto activeUnit = realBattle->battleActiveUnit();
  197. activeUnitId = activeUnit ? activeUnit->unitId() : -1;
  198. nextId = 0x00F00000;
  199. eventBus.reset(new events::EventBus());
  200. localEnvironment.reset(new HypotheticEnvironment(this, env));
  201. serverCallback.reset(new HypotheticServerCallback(this));
  202. #if SCRIPTING_ENABLED
  203. pool.reset(new scripting::PoolImpl(localEnvironment.get(), serverCallback.get()));
  204. #endif
  205. }
  206. bool HypotheticBattle::unitHasAmmoCart(const battle::Unit * unit) const
  207. {
  208. //FIXME: check ammocart alive state here
  209. return false;
  210. }
  211. PlayerColor HypotheticBattle::unitEffectiveOwner(const battle::Unit * unit) const
  212. {
  213. return battleGetOwner(unit);
  214. }
  215. std::shared_ptr<StackWithBonuses> HypotheticBattle::getForUpdate(uint32_t id)
  216. {
  217. auto iter = stackStates.find(id);
  218. if(iter == stackStates.end())
  219. {
  220. const CStack * s = subject->battleGetStackByID(id, false);
  221. auto ret = std::make_shared<StackWithBonuses>(this, s);
  222. stackStates[id] = ret;
  223. return ret;
  224. }
  225. else
  226. {
  227. return iter->second;
  228. }
  229. }
  230. battle::Units HypotheticBattle::getUnitsIf(battle::UnitFilter predicate) const
  231. {
  232. battle::Units proxyed = BattleProxy::getUnitsIf(predicate);
  233. battle::Units ret;
  234. ret.reserve(proxyed.size());
  235. for(auto unit : proxyed)
  236. {
  237. //unit was not changed, trust proxyed data
  238. if(stackStates.find(unit->unitId()) == stackStates.end())
  239. ret.push_back(unit);
  240. }
  241. for(auto id_unit : stackStates)
  242. {
  243. if(predicate(id_unit.second.get()))
  244. ret.push_back(id_unit.second.get());
  245. }
  246. return ret;
  247. }
  248. int32_t HypotheticBattle::getActiveStackID() const
  249. {
  250. return activeUnitId;
  251. }
  252. void HypotheticBattle::nextRound(int32_t roundNr)
  253. {
  254. //TODO:HypotheticBattle::nextRound
  255. for(auto unit : battleAliveUnits())
  256. {
  257. auto forUpdate = getForUpdate(unit->unitId());
  258. //TODO: update Bonus::NTurns effects
  259. forUpdate->afterNewRound();
  260. }
  261. }
  262. void HypotheticBattle::nextTurn(uint32_t unitId)
  263. {
  264. activeUnitId = unitId;
  265. auto unit = getForUpdate(unitId);
  266. unit->removeUnitBonus(Bonus::UntilGetsTurn);
  267. unit->afterGetsTurn();
  268. }
  269. void HypotheticBattle::addUnit(uint32_t id, const JsonNode & data)
  270. {
  271. battle::UnitInfo info;
  272. info.load(id, data);
  273. std::shared_ptr<StackWithBonuses> newUnit = std::make_shared<StackWithBonuses>(this, info);
  274. stackStates[newUnit->unitId()] = newUnit;
  275. }
  276. void HypotheticBattle::moveUnit(uint32_t id, BattleHex destination)
  277. {
  278. std::shared_ptr<StackWithBonuses> changed = getForUpdate(id);
  279. changed->position = destination;
  280. }
  281. void HypotheticBattle::setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta)
  282. {
  283. std::shared_ptr<StackWithBonuses> changed = getForUpdate(id);
  284. changed->load(data);
  285. if(healthDelta < 0)
  286. {
  287. changed->removeUnitBonus(Bonus::UntilBeingAttacked);
  288. }
  289. }
  290. void HypotheticBattle::removeUnit(uint32_t id)
  291. {
  292. std::set<uint32_t> ids;
  293. ids.insert(id);
  294. while(!ids.empty())
  295. {
  296. auto toRemoveId = *ids.begin();
  297. auto toRemove = getForUpdate(toRemoveId);
  298. if(!toRemove->ghost)
  299. {
  300. toRemove->onRemoved();
  301. //TODO: emulate detachFromAll() somehow
  302. //stack may be removed instantly (not being killed first)
  303. //handle clone remove also here
  304. if(toRemove->cloneID >= 0)
  305. {
  306. ids.insert(toRemove->cloneID);
  307. toRemove->cloneID = -1;
  308. }
  309. //TODO: cleanup remaining clone links if any
  310. // for(auto s : stacks)
  311. // {
  312. // if(s->cloneID == toRemoveId)
  313. // s->cloneID = -1;
  314. // }
  315. }
  316. ids.erase(toRemoveId);
  317. }
  318. }
  319. void HypotheticBattle::updateUnit(uint32_t id, const JsonNode & data)
  320. {
  321. //TODO:
  322. }
  323. void HypotheticBattle::addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
  324. {
  325. getForUpdate(id)->addUnitBonus(bonus);
  326. bonusTreeVersion++;
  327. }
  328. void HypotheticBattle::updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
  329. {
  330. getForUpdate(id)->updateUnitBonus(bonus);
  331. bonusTreeVersion++;
  332. }
  333. void HypotheticBattle::removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
  334. {
  335. getForUpdate(id)->removeUnitBonus(bonus);
  336. bonusTreeVersion++;
  337. }
  338. void HypotheticBattle::setWallState(EWallPart partOfWall, EWallState state)
  339. {
  340. //TODO:HypotheticBattle::setWallState
  341. }
  342. void HypotheticBattle::addObstacle(const ObstacleChanges & changes)
  343. {
  344. //TODO:HypotheticBattle::addObstacle
  345. }
  346. void HypotheticBattle::updateObstacle(const ObstacleChanges& changes)
  347. {
  348. //TODO:HypotheticBattle::updateObstacle
  349. }
  350. void HypotheticBattle::removeObstacle(uint32_t id)
  351. {
  352. //TODO:HypotheticBattle::removeObstacle
  353. }
  354. uint32_t HypotheticBattle::nextUnitId() const
  355. {
  356. return nextId++;
  357. }
  358. int64_t HypotheticBattle::getActualDamage(const DamageRange & damage, int32_t attackerCount, vstd::RNG & rng) const
  359. {
  360. return (damage.min + damage.max) / 2;
  361. }
  362. int64_t HypotheticBattle::getTreeVersion() const
  363. {
  364. return getBonusBearer()->getTreeVersion() + bonusTreeVersion;
  365. }
  366. #if SCRIPTING_ENABLED
  367. Pool * HypotheticBattle::getContextPool() const
  368. {
  369. return pool.get();
  370. }
  371. #endif
  372. ServerCallback * HypotheticBattle::getServerCallback()
  373. {
  374. return serverCallback.get();
  375. }
  376. HypotheticBattle::HypotheticServerCallback::HypotheticServerCallback(HypotheticBattle * owner_)
  377. :owner(owner_)
  378. {
  379. }
  380. void HypotheticBattle::HypotheticServerCallback::complain(const std::string & problem)
  381. {
  382. logAi->error(problem);
  383. }
  384. bool HypotheticBattle::HypotheticServerCallback::describeChanges() const
  385. {
  386. return false;
  387. }
  388. vstd::RNG * HypotheticBattle::HypotheticServerCallback::getRNG()
  389. {
  390. return &rngStub;
  391. }
  392. void HypotheticBattle::HypotheticServerCallback::apply(CPackForClient * pack)
  393. {
  394. logAi->error("Package of type %s is not allowed in battle evaluation", typeid(pack).name());
  395. }
  396. void HypotheticBattle::HypotheticServerCallback::apply(BattleLogMessage * pack)
  397. {
  398. pack->applyBattle(owner);
  399. }
  400. void HypotheticBattle::HypotheticServerCallback::apply(BattleStackMoved * pack)
  401. {
  402. pack->applyBattle(owner);
  403. }
  404. void HypotheticBattle::HypotheticServerCallback::apply(BattleUnitsChanged * pack)
  405. {
  406. pack->applyBattle(owner);
  407. }
  408. void HypotheticBattle::HypotheticServerCallback::apply(SetStackEffect * pack)
  409. {
  410. pack->applyBattle(owner);
  411. }
  412. void HypotheticBattle::HypotheticServerCallback::apply(StacksInjured * pack)
  413. {
  414. pack->applyBattle(owner);
  415. }
  416. void HypotheticBattle::HypotheticServerCallback::apply(BattleObstaclesChanged * pack)
  417. {
  418. pack->applyBattle(owner);
  419. }
  420. void HypotheticBattle::HypotheticServerCallback::apply(CatapultAttack * pack)
  421. {
  422. pack->applyBattle(owner);
  423. }
  424. HypotheticBattle::HypotheticEnvironment::HypotheticEnvironment(HypotheticBattle * owner_, const Environment * upperEnvironment)
  425. : owner(owner_),
  426. env(upperEnvironment)
  427. {
  428. }
  429. const Services * HypotheticBattle::HypotheticEnvironment::services() const
  430. {
  431. return env->services();
  432. }
  433. const Environment::BattleCb * HypotheticBattle::HypotheticEnvironment::battle() const
  434. {
  435. return owner;
  436. }
  437. const Environment::GameCb * HypotheticBattle::HypotheticEnvironment::game() const
  438. {
  439. return env->game();
  440. }
  441. vstd::CLoggerBase * HypotheticBattle::HypotheticEnvironment::logger() const
  442. {
  443. return env->logger();
  444. }
  445. events::EventBus * HypotheticBattle::HypotheticEnvironment::eventBus() const
  446. {
  447. return owner->eventBus.get();
  448. }