CUnitState.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /*
  2. * CUnitState.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 "CUnitState.h"
  12. #include <vcmi/spells/Spell.h>
  13. #include "../NetPacks.h"
  14. #include "../CCreatureHandler.h"
  15. #include "../serializer/JsonDeserializer.h"
  16. #include "../serializer/JsonSerializer.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. namespace battle
  19. {
  20. ///CAmmo
  21. CAmmo::CAmmo(const battle::Unit * Owner, CSelector totalSelector)
  22. : used(0),
  23. owner(Owner),
  24. totalProxy(Owner, totalSelector)
  25. {
  26. reset();
  27. }
  28. CAmmo::CAmmo(const CAmmo & other)
  29. : used(other.used),
  30. owner(other.owner),
  31. totalProxy(other.totalProxy)
  32. {
  33. }
  34. CAmmo & CAmmo::operator= (const CAmmo & other)
  35. {
  36. used = other.used;
  37. totalProxy = other.totalProxy;
  38. return *this;
  39. }
  40. int32_t CAmmo::available() const
  41. {
  42. return total() - used;
  43. }
  44. bool CAmmo::canUse(int32_t amount) const
  45. {
  46. return !isLimited() || (available() - amount >= 0);
  47. }
  48. bool CAmmo::isLimited() const
  49. {
  50. return true;
  51. }
  52. void CAmmo::reset()
  53. {
  54. used = 0;
  55. }
  56. int32_t CAmmo::total() const
  57. {
  58. return totalProxy->totalValue();
  59. }
  60. void CAmmo::use(int32_t amount)
  61. {
  62. if(!isLimited())
  63. return;
  64. if(available() - amount < 0)
  65. {
  66. logGlobal->error("Stack ammo overuse. total: %d, used: %d, requested: %d", total(), used, amount);
  67. used += available();
  68. }
  69. else
  70. used += amount;
  71. }
  72. void CAmmo::serializeJson(JsonSerializeFormat & handler)
  73. {
  74. handler.serializeInt("used", used, 0);
  75. }
  76. ///CShots
  77. CShots::CShots(const battle::Unit * Owner)
  78. : CAmmo(Owner, Selector::type()(Bonus::SHOTS)),
  79. shooter(Owner, Selector::type()(Bonus::SHOOTER))
  80. {
  81. }
  82. CShots::CShots(const CShots & other)
  83. : CAmmo(other),
  84. env(other.env),
  85. shooter(other.shooter)
  86. {
  87. }
  88. CShots & CShots::operator=(const CShots & other)
  89. {
  90. CAmmo::operator=(other);
  91. shooter = other.shooter;
  92. return *this;
  93. }
  94. bool CShots::isLimited() const
  95. {
  96. return !env->unitHasAmmoCart(owner) || !shooter.getHasBonus();
  97. }
  98. void CShots::setEnv(const IUnitEnvironment * env_)
  99. {
  100. env = env_;
  101. }
  102. int32_t CShots::total() const
  103. {
  104. if(shooter.getHasBonus())
  105. return CAmmo::total();
  106. else
  107. return 0;
  108. }
  109. ///CCasts
  110. CCasts::CCasts(const battle::Unit * Owner):
  111. CAmmo(Owner, Selector::type()(Bonus::CASTS))
  112. {
  113. }
  114. CCasts::CCasts(const CCasts & other)
  115. : CAmmo(other)
  116. {
  117. }
  118. CCasts & CCasts::operator=(const CCasts & other)
  119. {
  120. CAmmo::operator=(other);
  121. return *this;
  122. }
  123. ///CRetaliations
  124. CRetaliations::CRetaliations(const battle::Unit * Owner)
  125. : CAmmo(Owner, Selector::type()(Bonus::ADDITIONAL_RETALIATION)),
  126. totalCache(0),
  127. noRetaliation(Owner, Selector::type()(Bonus::SIEGE_WEAPON).Or(Selector::type()(Bonus::HYPNOTIZED)).Or(Selector::type()(Bonus::NO_RETALIATION))),
  128. unlimited(Owner, Selector::type()(Bonus::UNLIMITED_RETALIATIONS))
  129. {
  130. }
  131. CRetaliations::CRetaliations(const CRetaliations & other)
  132. : CAmmo(other),
  133. totalCache(other.totalCache),
  134. noRetaliation(other.noRetaliation),
  135. unlimited(other.unlimited)
  136. {
  137. }
  138. CRetaliations & CRetaliations::operator=(const CRetaliations & other)
  139. {
  140. CAmmo::operator=(other);
  141. totalCache = other.totalCache;
  142. noRetaliation = other.noRetaliation;
  143. unlimited = other.unlimited;
  144. return *this;
  145. }
  146. bool CRetaliations::isLimited() const
  147. {
  148. return !unlimited.getHasBonus() || noRetaliation.getHasBonus();
  149. }
  150. int32_t CRetaliations::total() const
  151. {
  152. if(noRetaliation.getHasBonus())
  153. return 0;
  154. //after dispell bonus should remain during current round
  155. int32_t val = 1 + totalProxy->totalValue();
  156. vstd::amax(totalCache, val);
  157. return totalCache;
  158. }
  159. void CRetaliations::reset()
  160. {
  161. CAmmo::reset();
  162. totalCache = 0;
  163. }
  164. void CRetaliations::serializeJson(JsonSerializeFormat & handler)
  165. {
  166. CAmmo::serializeJson(handler);
  167. //we may be serialized in the middle of turn
  168. handler.serializeInt("totalCache", totalCache, 0);
  169. }
  170. ///CHealth
  171. CHealth::CHealth(const battle::Unit * Owner):
  172. owner(Owner)
  173. {
  174. reset();
  175. }
  176. CHealth::CHealth(const CHealth & other):
  177. owner(other.owner),
  178. firstHPleft(other.firstHPleft),
  179. fullUnits(other.fullUnits),
  180. resurrected(other.resurrected)
  181. {
  182. }
  183. CHealth & CHealth::operator=(const CHealth & other)
  184. {
  185. //do not change owner
  186. firstHPleft = other.firstHPleft;
  187. fullUnits = other.fullUnits;
  188. resurrected = other.resurrected;
  189. return *this;
  190. }
  191. void CHealth::init()
  192. {
  193. reset();
  194. fullUnits = owner->unitBaseAmount() > 1 ? owner->unitBaseAmount() - 1 : 0;
  195. firstHPleft = owner->unitBaseAmount() > 0 ? owner->MaxHealth() : 0;
  196. }
  197. void CHealth::addResurrected(int32_t amount)
  198. {
  199. resurrected += amount;
  200. vstd::amax(resurrected, 0);
  201. }
  202. int64_t CHealth::available() const
  203. {
  204. return static_cast<int64_t>(firstHPleft) + owner->MaxHealth() * fullUnits;
  205. }
  206. int64_t CHealth::total() const
  207. {
  208. return static_cast<int64_t>(owner->MaxHealth()) * owner->unitBaseAmount();
  209. }
  210. void CHealth::damage(int64_t & amount)
  211. {
  212. const int32_t oldCount = getCount();
  213. const bool withKills = amount >= firstHPleft;
  214. if(withKills)
  215. {
  216. int64_t totalHealth = available();
  217. if(amount > totalHealth)
  218. amount = totalHealth;
  219. totalHealth -= amount;
  220. if(totalHealth <= 0)
  221. {
  222. fullUnits = 0;
  223. firstHPleft = 0;
  224. }
  225. else
  226. {
  227. setFromTotal(totalHealth);
  228. }
  229. }
  230. else
  231. {
  232. firstHPleft -= static_cast<int32_t>(amount);
  233. }
  234. addResurrected(getCount() - oldCount);
  235. }
  236. void CHealth::heal(int64_t & amount, EHealLevel level, EHealPower power)
  237. {
  238. const int32_t unitHealth = owner->MaxHealth();
  239. const int32_t oldCount = getCount();
  240. int64_t maxHeal = std::numeric_limits<int64_t>::max();
  241. switch(level)
  242. {
  243. case EHealLevel::HEAL:
  244. maxHeal = std::max(0, unitHealth - firstHPleft);
  245. break;
  246. case EHealLevel::RESURRECT:
  247. maxHeal = total() - available();
  248. break;
  249. default:
  250. assert(level == EHealLevel::OVERHEAL);
  251. break;
  252. }
  253. vstd::amax(maxHeal, 0);
  254. vstd::abetween(amount, 0, maxHeal);
  255. if(amount == 0)
  256. return;
  257. int64_t availableHealth = available();
  258. availableHealth += amount;
  259. setFromTotal(availableHealth);
  260. if(power == EHealPower::ONE_BATTLE)
  261. addResurrected(getCount() - oldCount);
  262. else
  263. assert(power == EHealPower::PERMANENT);
  264. }
  265. void CHealth::setFromTotal(const int64_t totalHealth)
  266. {
  267. const int32_t unitHealth = owner->MaxHealth();
  268. firstHPleft = totalHealth % unitHealth;
  269. fullUnits = static_cast<int32_t>(totalHealth / unitHealth);
  270. if(firstHPleft == 0 && fullUnits >= 1)
  271. {
  272. firstHPleft = unitHealth;
  273. fullUnits -= 1;
  274. }
  275. }
  276. void CHealth::reset()
  277. {
  278. fullUnits = 0;
  279. firstHPleft = 0;
  280. resurrected = 0;
  281. }
  282. int32_t CHealth::getCount() const
  283. {
  284. return fullUnits + (firstHPleft > 0 ? 1 : 0);
  285. }
  286. int32_t CHealth::getFirstHPleft() const
  287. {
  288. return firstHPleft;
  289. }
  290. int32_t CHealth::getResurrected() const
  291. {
  292. return resurrected;
  293. }
  294. void CHealth::takeResurrected()
  295. {
  296. if(resurrected != 0)
  297. {
  298. int64_t totalHealth = available();
  299. totalHealth -= resurrected * owner->MaxHealth();
  300. vstd::amax(totalHealth, 0);
  301. setFromTotal(totalHealth);
  302. resurrected = 0;
  303. }
  304. }
  305. void CHealth::serializeJson(JsonSerializeFormat & handler)
  306. {
  307. handler.serializeInt("firstHPleft", firstHPleft, 0);
  308. handler.serializeInt("fullUnits", fullUnits, 0);
  309. handler.serializeInt("resurrected", resurrected, 0);
  310. }
  311. ///CUnitState
  312. CUnitState::CUnitState()
  313. : env(nullptr),
  314. cloned(false),
  315. defending(false),
  316. defendingAnim(false),
  317. drainedMana(false),
  318. fear(false),
  319. hadMorale(false),
  320. ghost(false),
  321. ghostPending(false),
  322. movedThisRound(false),
  323. summoned(false),
  324. waiting(false),
  325. waitedThisTurn(false),
  326. casts(this),
  327. counterAttacks(this),
  328. health(this),
  329. shots(this),
  330. totalAttacks(this, Selector::type()(Bonus::ADDITIONAL_ATTACK), 1),
  331. minDamage(this, Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 1)), 0),
  332. maxDamage(this, Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 2)), 0),
  333. attack(this, Selector::typeSubtype(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), 0),
  334. defence(this, Selector::typeSubtype(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), 0),
  335. inFrenzy(this, Selector::type()(Bonus::IN_FRENZY)),
  336. cloneLifetimeMarker(this, Selector::type()(Bonus::NONE).And(Selector::source(Bonus::SPELL_EFFECT, SpellID::CLONE))),
  337. cloneID(-1),
  338. position()
  339. {
  340. }
  341. CUnitState & CUnitState::operator=(const CUnitState & other)
  342. {
  343. //do not change unit and bonus info
  344. cloned = other.cloned;
  345. defending = other.defending;
  346. defendingAnim = other.defendingAnim;
  347. drainedMana = other.drainedMana;
  348. fear = other.fear;
  349. hadMorale = other.hadMorale;
  350. ghost = other.ghost;
  351. ghostPending = other.ghostPending;
  352. movedThisRound = other.movedThisRound;
  353. summoned = other.summoned;
  354. waiting = other.waiting;
  355. waitedThisTurn = other.waitedThisTurn;
  356. casts = other.casts;
  357. counterAttacks = other.counterAttacks;
  358. health = other.health;
  359. shots = other.shots;
  360. totalAttacks = other.totalAttacks;
  361. minDamage = other.minDamage;
  362. maxDamage = other.maxDamage;
  363. attack = other.attack;
  364. defence = other.defence;
  365. inFrenzy = other.inFrenzy;
  366. cloneLifetimeMarker = other.cloneLifetimeMarker;
  367. cloneID = other.cloneID;
  368. position = other.position;
  369. return *this;
  370. }
  371. int32_t CUnitState::creatureIndex() const
  372. {
  373. return static_cast<int32_t>(creatureId().toEnum());
  374. }
  375. CreatureID CUnitState::creatureId() const
  376. {
  377. return unitType()->idNumber;
  378. }
  379. int32_t CUnitState::creatureLevel() const
  380. {
  381. return static_cast<int32_t>(unitType()->level);
  382. }
  383. bool CUnitState::doubleWide() const
  384. {
  385. return unitType()->doubleWide;
  386. }
  387. int32_t CUnitState::creatureCost() const
  388. {
  389. return unitType()->cost[Res::GOLD];
  390. }
  391. int32_t CUnitState::creatureIconIndex() const
  392. {
  393. return unitType()->iconIndex;
  394. }
  395. int32_t CUnitState::getCasterUnitId() const
  396. {
  397. return static_cast<int32_t>(unitId());
  398. }
  399. int32_t CUnitState::getSpellSchoolLevel(const spells::Spell * spell, int32_t * outSelectedSchool) const
  400. {
  401. int32_t skill = valOfBonuses(Selector::typeSubtype(Bonus::SPELLCASTER, spell->getIndex()));
  402. vstd::abetween(skill, 0, 3);
  403. return skill;
  404. }
  405. int64_t CUnitState::getSpellBonus(const spells::Spell * spell, int64_t base, const Unit * affectedStack) const
  406. {
  407. //does not have sorcery-like bonuses (yet?)
  408. return base;
  409. }
  410. int64_t CUnitState::getSpecificSpellBonus(const spells::Spell * spell, int64_t base) const
  411. {
  412. return base;
  413. }
  414. int32_t CUnitState::getEffectLevel(const spells::Spell * spell) const
  415. {
  416. return getSpellSchoolLevel(spell);
  417. }
  418. int32_t CUnitState::getEffectPower(const spells::Spell * spell) const
  419. {
  420. return valOfBonuses(Bonus::CREATURE_SPELL_POWER) * getCount() / 100;
  421. }
  422. int32_t CUnitState::getEnchantPower(const spells::Spell * spell) const
  423. {
  424. int32_t res = valOfBonuses(Bonus::CREATURE_ENCHANT_POWER);
  425. if(res <= 0)
  426. res = 3;//default for creatures
  427. return res;
  428. }
  429. int64_t CUnitState::getEffectValue(const spells::Spell * spell) const
  430. {
  431. return static_cast<int64_t>(getCount()) * valOfBonuses(Bonus::SPECIFIC_SPELL_POWER, spell->getIndex());
  432. }
  433. PlayerColor CUnitState::getCasterOwner() const
  434. {
  435. return env->unitEffectiveOwner(this);
  436. }
  437. void CUnitState::getCasterName(MetaString & text) const
  438. {
  439. //always plural name in case of spell cast.
  440. addNameReplacement(text, true);
  441. }
  442. void CUnitState::getCastDescription(const spells::Spell * spell, const std::vector<const Unit *> & attacked, MetaString & text) const
  443. {
  444. text.addTxt(MetaString::GENERAL_TXT, 565);//The %s casts %s
  445. //todo: use text 566 for single creature
  446. getCasterName(text);
  447. text.addReplacement(MetaString::SPELL_NAME, spell->getIndex());
  448. }
  449. bool CUnitState::ableToRetaliate() const
  450. {
  451. return alive()
  452. && counterAttacks.canUse();
  453. }
  454. bool CUnitState::alive() const
  455. {
  456. return health.getCount() > 0;
  457. }
  458. bool CUnitState::isGhost() const
  459. {
  460. return ghost;
  461. }
  462. bool CUnitState::isFrozen() const
  463. {
  464. return hasBonus(Selector::source(Bonus::SPELL_EFFECT, SpellID::STONE_GAZE), Selector::all);
  465. }
  466. bool CUnitState::isValidTarget(bool allowDead) const
  467. {
  468. return (alive() || (allowDead && isDead())) && getPosition().isValid() && !isTurret();
  469. }
  470. bool CUnitState::isClone() const
  471. {
  472. return cloned;
  473. }
  474. bool CUnitState::hasClone() const
  475. {
  476. return cloneID > 0;
  477. }
  478. bool CUnitState::canCast() const
  479. {
  480. return casts.canUse(1);//do not check specific cast abilities here
  481. }
  482. bool CUnitState::isCaster() const
  483. {
  484. return casts.total() > 0;//do not check specific cast abilities here
  485. }
  486. bool CUnitState::canShoot() const
  487. {
  488. return shots.canUse(1);
  489. }
  490. bool CUnitState::isShooter() const
  491. {
  492. return shots.total() > 0;
  493. }
  494. int32_t CUnitState::getKilled() const
  495. {
  496. int32_t res = unitBaseAmount() - health.getCount() + health.getResurrected();
  497. vstd::amax(res, 0);
  498. return res;
  499. }
  500. int32_t CUnitState::getCount() const
  501. {
  502. return health.getCount();
  503. }
  504. int32_t CUnitState::getFirstHPleft() const
  505. {
  506. return health.getFirstHPleft();
  507. }
  508. int64_t CUnitState::getAvailableHealth() const
  509. {
  510. return health.available();
  511. }
  512. int64_t CUnitState::getTotalHealth() const
  513. {
  514. return health.total();
  515. }
  516. BattleHex CUnitState::getPosition() const
  517. {
  518. return position;
  519. }
  520. void CUnitState::setPosition(BattleHex hex)
  521. {
  522. position = hex;
  523. }
  524. int32_t CUnitState::getInitiative(int turn) const
  525. {
  526. return valOfBonuses(Selector::type()(Bonus::STACKS_SPEED).And(Selector::turns(turn)));
  527. }
  528. bool CUnitState::canMove(int turn) const
  529. {
  530. return alive() && !hasBonus(Selector::type()(Bonus::NOT_ACTIVE).And(Selector::turns(turn))); //eg. Ammo Cart or blinded creature
  531. }
  532. bool CUnitState::defended(int turn) const
  533. {
  534. return !turn && defending;
  535. }
  536. bool CUnitState::moved(int turn) const
  537. {
  538. if(!turn && !waiting)
  539. return movedThisRound;
  540. else
  541. return false;
  542. }
  543. bool CUnitState::willMove(int turn) const
  544. {
  545. return (turn ? true : !defending)
  546. && !moved(turn)
  547. && canMove(turn);
  548. }
  549. bool CUnitState::waited(int turn) const
  550. {
  551. if(!turn)
  552. return waiting;
  553. else
  554. return false;
  555. }
  556. int CUnitState::battleQueuePhase(int turn) const
  557. {
  558. if(turn <= 0 && waited()) //consider waiting state only for ongoing round
  559. {
  560. if(hadMorale)
  561. return 2;
  562. else
  563. return 3;
  564. }
  565. else if(creatureIndex() == CreatureID::CATAPULT || isTurret()) //catapult and turrets are first
  566. {
  567. return 0;
  568. }
  569. else
  570. {
  571. return 1;
  572. }
  573. }
  574. int CUnitState::getTotalAttacks(bool ranged) const
  575. {
  576. return ranged ? totalAttacks.getRangedValue() : totalAttacks.getMeleeValue();
  577. }
  578. int CUnitState::getMinDamage(bool ranged) const
  579. {
  580. return ranged ? minDamage.getRangedValue() : minDamage.getMeleeValue();
  581. }
  582. int CUnitState::getMaxDamage(bool ranged) const
  583. {
  584. return ranged ? maxDamage.getRangedValue() : maxDamage.getMeleeValue();
  585. }
  586. int CUnitState::getAttack(bool ranged) const
  587. {
  588. int ret = ranged ? attack.getRangedValue() : attack.getMeleeValue();
  589. if(!inFrenzy->empty())
  590. {
  591. double frenzyPower = (double)inFrenzy->totalValue() / 100;
  592. frenzyPower *= (double) (ranged ? defence.getRangedValue() : defence.getMeleeValue());
  593. ret += static_cast<int>(frenzyPower);
  594. }
  595. vstd::amax(ret, 0);
  596. return ret;
  597. }
  598. int CUnitState::getDefense(bool ranged) const
  599. {
  600. if(!inFrenzy->empty())
  601. {
  602. return 0;
  603. }
  604. else
  605. {
  606. int ret = ranged ? defence.getRangedValue() : defence.getMeleeValue();
  607. vstd::amax(ret, 0);
  608. return ret;
  609. }
  610. }
  611. std::shared_ptr<Unit> CUnitState::acquire() const
  612. {
  613. auto ret = std::make_shared<CUnitStateDetached>(this, this);
  614. ret->localInit(env);
  615. *ret = *this;
  616. return ret;
  617. }
  618. std::shared_ptr<CUnitState> CUnitState::acquireState() const
  619. {
  620. auto ret = std::make_shared<CUnitStateDetached>(this, this);
  621. ret->localInit(env);
  622. *ret = *this;
  623. return ret;
  624. }
  625. void CUnitState::serializeJson(JsonSerializeFormat & handler)
  626. {
  627. handler.serializeBool("cloned", cloned);
  628. handler.serializeBool("defending", defending);
  629. handler.serializeBool("defendingAnim", defendingAnim);
  630. handler.serializeBool("drainedMana", drainedMana);
  631. handler.serializeBool("fear", fear);
  632. handler.serializeBool("hadMorale", hadMorale);
  633. handler.serializeBool("ghost", ghost);
  634. handler.serializeBool("ghostPending", ghostPending);
  635. handler.serializeBool("moved", movedThisRound);
  636. handler.serializeBool("summoned", summoned);
  637. handler.serializeBool("waiting", waiting);
  638. handler.serializeBool("waitedThisTurn", waitedThisTurn);
  639. handler.serializeStruct("casts", casts);
  640. handler.serializeStruct("counterAttacks", counterAttacks);
  641. handler.serializeStruct("health", health);
  642. handler.serializeStruct("shots", shots);
  643. handler.serializeInt("cloneID", cloneID);
  644. handler.serializeInt("position", position);
  645. }
  646. void CUnitState::localInit(const IUnitEnvironment * env_)
  647. {
  648. env = env_;
  649. shots.setEnv(env);
  650. reset();
  651. health.init();
  652. }
  653. void CUnitState::reset()
  654. {
  655. cloned = false;
  656. defending = false;
  657. defendingAnim = false;
  658. drainedMana = false;
  659. fear = false;
  660. hadMorale = false;
  661. ghost = false;
  662. ghostPending = false;
  663. movedThisRound = false;
  664. summoned = false;
  665. waiting = false;
  666. waitedThisTurn = false;
  667. casts.reset();
  668. counterAttacks.reset();
  669. health.reset();
  670. shots.reset();
  671. cloneID = -1;
  672. position = BattleHex::INVALID;
  673. }
  674. void CUnitState::save(JsonNode & data)
  675. {
  676. //TODO: use instance resolver
  677. data.clear();
  678. JsonSerializer ser(nullptr, data);
  679. ser.serializeStruct("state", *this);
  680. }
  681. void CUnitState::load(const JsonNode & data)
  682. {
  683. //TODO: use instance resolver
  684. reset();
  685. JsonDeserializer deser(nullptr, data);
  686. deser.serializeStruct("state", *this);
  687. }
  688. void CUnitState::damage(int64_t & amount)
  689. {
  690. if(cloned)
  691. {
  692. // block ability should not kill clone (0 damage)
  693. if(amount > 0)
  694. {
  695. amount = 0;
  696. health.reset();
  697. }
  698. }
  699. else
  700. {
  701. health.damage(amount);
  702. }
  703. if(health.available() <= 0 && (cloned || summoned))
  704. ghostPending = true;
  705. }
  706. void CUnitState::heal(int64_t & amount, EHealLevel level, EHealPower power)
  707. {
  708. if(level == EHealLevel::HEAL && power == EHealPower::ONE_BATTLE)
  709. logGlobal->error("Heal for one battle does not make sense");
  710. else if(cloned)
  711. logGlobal->error("Attempt to heal clone");
  712. else
  713. health.heal(amount, level, power);
  714. }
  715. void CUnitState::afterAttack(bool ranged, bool counter)
  716. {
  717. if(counter)
  718. counterAttacks.use();
  719. if(ranged)
  720. shots.use();
  721. }
  722. void CUnitState::afterNewRound()
  723. {
  724. defending = false;
  725. waiting = false;
  726. waitedThisTurn = false;
  727. movedThisRound = false;
  728. hadMorale = false;
  729. fear = false;
  730. drainedMana = false;
  731. counterAttacks.reset();
  732. if(alive() && isClone())
  733. {
  734. if(!cloneLifetimeMarker.getHasBonus())
  735. makeGhost();
  736. }
  737. }
  738. void CUnitState::afterGetsTurn()
  739. {
  740. //if moving second time this round it must be high morale bonus
  741. if(movedThisRound)
  742. hadMorale = true;
  743. }
  744. void CUnitState::makeGhost()
  745. {
  746. health.reset();
  747. ghostPending = true;
  748. }
  749. void CUnitState::onRemoved()
  750. {
  751. health.reset();
  752. ghostPending = false;
  753. ghost = true;
  754. }
  755. CUnitStateDetached::CUnitStateDetached(const IUnitInfo * unit_, const IBonusBearer * bonus_)
  756. : CUnitState(),
  757. unit(unit_),
  758. bonus(bonus_)
  759. {
  760. }
  761. TConstBonusListPtr CUnitStateDetached::getAllBonuses(const CSelector & selector, const CSelector & limit, const CBonusSystemNode * root, const std::string & cachingStr) const
  762. {
  763. return bonus->getAllBonuses(selector, limit, root, cachingStr);
  764. }
  765. int64_t CUnitStateDetached::getTreeVersion() const
  766. {
  767. return bonus->getTreeVersion();
  768. }
  769. CUnitStateDetached & CUnitStateDetached::operator=(const CUnitState & other)
  770. {
  771. CUnitState::operator=(other);
  772. return *this;
  773. }
  774. uint32_t CUnitStateDetached::unitId() const
  775. {
  776. return unit->unitId();
  777. }
  778. ui8 CUnitStateDetached::unitSide() const
  779. {
  780. return unit->unitSide();
  781. }
  782. const CCreature * CUnitStateDetached::unitType() const
  783. {
  784. return unit->unitType();
  785. }
  786. PlayerColor CUnitStateDetached::unitOwner() const
  787. {
  788. return unit->unitOwner();
  789. }
  790. SlotID CUnitStateDetached::unitSlot() const
  791. {
  792. return unit->unitSlot();
  793. }
  794. int32_t CUnitStateDetached::unitBaseAmount() const
  795. {
  796. return unit->unitBaseAmount();
  797. }
  798. void CUnitStateDetached::spendMana(ServerCallback * server, const int spellCost) const
  799. {
  800. if(spellCost != 1)
  801. logGlobal->warn("Unexpected spell cost %d for creature", spellCost);
  802. //this is evil, but
  803. //use of netpacks in detached state is an error
  804. //non const API is more evil for hero
  805. const_cast<CUnitStateDetached *>(this)->casts.use(spellCost);
  806. }
  807. }
  808. VCMI_LIB_NAMESPACE_END