StackWithBonuses.cpp 12 KB

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