CUnitState.cpp 19 KB

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