CStack.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * CStack.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 "CStack.h"
  12. #include "CGeneralTextHandler.h"
  13. #include "battle/BattleInfo.h"
  14. #include "spells/CSpellHandler.h"
  15. #include "CRandomGenerator.h"
  16. #include "NetPacks.h"
  17. ///CAmmo
  18. CAmmo::CAmmo(const CStack * Owner, CSelector totalSelector):
  19. CStackResource(Owner), totalProxy(Owner, totalSelector)
  20. {
  21. }
  22. int32_t CAmmo::available() const
  23. {
  24. return total() - used;
  25. }
  26. bool CAmmo::canUse(int32_t amount) const
  27. {
  28. return available() - amount >= 0;
  29. }
  30. void CAmmo::reset()
  31. {
  32. used = 0;
  33. }
  34. int32_t CAmmo::total() const
  35. {
  36. return totalProxy->totalValue();
  37. }
  38. void CAmmo::use(int32_t amount)
  39. {
  40. if(available() - amount < 0)
  41. {
  42. logGlobal->error("Stack ammo overuse");
  43. used += available();
  44. }
  45. else
  46. used += amount;
  47. }
  48. ///CShots
  49. CShots::CShots(const CStack * Owner):
  50. CAmmo(Owner, Selector::type(Bonus::SHOTS))
  51. {
  52. }
  53. void CShots::use(int32_t amount)
  54. {
  55. //don't remove ammo if we control a working ammo cart
  56. bool hasAmmoCart = false;
  57. for(const CStack * st : owner->battle->stacks)
  58. {
  59. if(owner->battle->battleMatchOwner(st, owner, true) && st->getCreature()->idNumber == CreatureID::AMMO_CART && st->alive())
  60. {
  61. hasAmmoCart = true;
  62. break;
  63. }
  64. }
  65. if(!hasAmmoCart)
  66. CAmmo::use(amount);
  67. }
  68. ///CCasts
  69. CCasts::CCasts(const CStack * Owner):
  70. CAmmo(Owner, Selector::type(Bonus::CASTS))
  71. {
  72. }
  73. ///CRetaliations
  74. CRetaliations::CRetaliations(const CStack * Owner):
  75. CAmmo(Owner, Selector::type(Bonus::ADDITIONAL_RETALIATION)), totalCache(0)
  76. {
  77. }
  78. int32_t CRetaliations::total() const
  79. {
  80. //after dispell bonus should remain during current round
  81. int32_t val = 1 + totalProxy->totalValue();
  82. vstd::amax(totalCache, val);
  83. return totalCache;
  84. }
  85. void CRetaliations::reset()
  86. {
  87. CAmmo::reset();
  88. totalCache = 0;
  89. }
  90. ///CHealth
  91. CHealth::CHealth(const IUnitHealthInfo * Owner):
  92. owner(Owner)
  93. {
  94. reset();
  95. }
  96. CHealth::CHealth(const CHealth & other):
  97. owner(other.owner),
  98. firstHPleft(other.firstHPleft),
  99. fullUnits(other.fullUnits),
  100. resurrected(other.resurrected)
  101. {
  102. }
  103. void CHealth::init()
  104. {
  105. reset();
  106. fullUnits = owner->unitBaseAmount() > 1 ? owner->unitBaseAmount() - 1 : 0;
  107. firstHPleft = owner->unitBaseAmount() > 0 ? owner->unitMaxHealth() : 0;
  108. }
  109. void CHealth::addResurrected(int32_t amount)
  110. {
  111. resurrected += amount;
  112. vstd::amax(resurrected, 0);
  113. }
  114. int64_t CHealth::available() const
  115. {
  116. return static_cast<int64_t>(firstHPleft) + owner->unitMaxHealth() * fullUnits;
  117. }
  118. int64_t CHealth::total() const
  119. {
  120. return static_cast<int64_t>(owner->unitMaxHealth()) * owner->unitBaseAmount();
  121. }
  122. void CHealth::damage(int32_t & amount)
  123. {
  124. const int32_t oldCount = getCount();
  125. const bool withKills = amount >= firstHPleft;
  126. if(withKills)
  127. {
  128. int64_t totalHealth = available();
  129. if(amount > totalHealth)
  130. amount = totalHealth;
  131. totalHealth -= amount;
  132. if(totalHealth <= 0)
  133. {
  134. fullUnits = 0;
  135. firstHPleft = 0;
  136. }
  137. else
  138. {
  139. setFromTotal(totalHealth);
  140. }
  141. }
  142. else
  143. {
  144. firstHPleft -= amount;
  145. }
  146. addResurrected(getCount() - oldCount);
  147. }
  148. void CHealth::heal(int32_t & amount, EHealLevel level, EHealPower power)
  149. {
  150. const int32_t unitHealth = owner->unitMaxHealth();
  151. const int32_t oldCount = getCount();
  152. int32_t maxHeal = std::numeric_limits<int32_t>::max();
  153. switch(level)
  154. {
  155. case EHealLevel::HEAL:
  156. maxHeal = std::max(0, unitHealth - firstHPleft);
  157. break;
  158. case EHealLevel::RESURRECT:
  159. maxHeal = total() - available();
  160. break;
  161. default:
  162. assert(level == EHealLevel::OVERHEAL);
  163. break;
  164. }
  165. vstd::amax(maxHeal, 0);
  166. vstd::abetween(amount, 0, maxHeal);
  167. if(amount == 0)
  168. return;
  169. int64_t availableHealth = available();
  170. availableHealth += amount;
  171. setFromTotal(availableHealth);
  172. if(power == EHealPower::ONE_BATTLE)
  173. addResurrected(getCount() - oldCount);
  174. else
  175. assert(power == EHealPower::PERMANENT);
  176. }
  177. void CHealth::setFromTotal(const int64_t totalHealth)
  178. {
  179. const int32_t unitHealth = owner->unitMaxHealth();
  180. firstHPleft = totalHealth % unitHealth;
  181. fullUnits = totalHealth / unitHealth;
  182. if(firstHPleft == 0 && fullUnits > 1)
  183. {
  184. firstHPleft = unitHealth;
  185. fullUnits -= 1;
  186. }
  187. }
  188. void CHealth::reset()
  189. {
  190. fullUnits = 0;
  191. firstHPleft = 0;
  192. resurrected = 0;
  193. }
  194. int32_t CHealth::getCount() const
  195. {
  196. return fullUnits + (firstHPleft > 0 ? 1 : 0);
  197. }
  198. int32_t CHealth::getFirstHPleft() const
  199. {
  200. return firstHPleft;
  201. }
  202. int32_t CHealth::getResurrected() const
  203. {
  204. return resurrected;
  205. }
  206. void CHealth::fromInfo(const CHealthInfo & info)
  207. {
  208. firstHPleft = info.firstHPleft;
  209. fullUnits = info.fullUnits;
  210. resurrected = info.resurrected;
  211. }
  212. void CHealth::toInfo(CHealthInfo & info) const
  213. {
  214. info.firstHPleft = firstHPleft;
  215. info.fullUnits = fullUnits;
  216. info.resurrected = resurrected;
  217. }
  218. void CHealth::takeResurrected()
  219. {
  220. int64_t totalHealth = total();
  221. totalHealth -= resurrected * owner->unitMaxHealth();
  222. vstd::amax(totalHealth, 0);
  223. setFromTotal(totalHealth);
  224. resurrected = 0;
  225. }
  226. ///CStack
  227. CStack::CStack(const CStackInstance * Base, PlayerColor O, int I, ui8 Side, SlotID S):
  228. base(Base), ID(I), owner(O), slot(S), side(Side),
  229. counterAttacks(this), shots(this), casts(this), health(this), cloneID(-1),
  230. position()
  231. {
  232. assert(base);
  233. type = base->type;
  234. baseAmount = base->count;
  235. health.init(); //???
  236. setNodeType(STACK_BATTLE);
  237. }
  238. CStack::CStack():
  239. counterAttacks(this), shots(this), casts(this), health(this)
  240. {
  241. init();
  242. setNodeType(STACK_BATTLE);
  243. }
  244. CStack::CStack(const CStackBasicDescriptor * stack, PlayerColor O, int I, ui8 Side, SlotID S):
  245. base(nullptr), ID(I), owner(O), slot(S), side(Side),
  246. counterAttacks(this), shots(this), casts(this), health(this), cloneID(-1),
  247. position()
  248. {
  249. type = stack->type;
  250. baseAmount = stack->count;
  251. health.init(); //???
  252. setNodeType(STACK_BATTLE);
  253. }
  254. int32_t CStack::getKilled() const
  255. {
  256. int32_t res = baseAmount - health.getCount() + health.getResurrected();
  257. vstd::amax(res, 0);
  258. return res;
  259. }
  260. int32_t CStack::getCount() const
  261. {
  262. return health.getCount();
  263. }
  264. int32_t CStack::getFirstHPleft() const
  265. {
  266. return health.getFirstHPleft();
  267. }
  268. const CCreature * CStack::getCreature() const
  269. {
  270. return type;
  271. }
  272. void CStack::init()
  273. {
  274. base = nullptr;
  275. type = nullptr;
  276. ID = -1;
  277. baseAmount = -1;
  278. owner = PlayerColor::NEUTRAL;
  279. slot = SlotID(255);
  280. side = 1;
  281. position = BattleHex();
  282. cloneID = -1;
  283. }
  284. void CStack::localInit(BattleInfo * battleInfo)
  285. {
  286. battle = battleInfo;
  287. cloneID = -1;
  288. assert(type);
  289. exportBonuses();
  290. if(base) //stack originating from "real" stack in garrison -> attach to it
  291. {
  292. attachTo(const_cast<CStackInstance *>(base));
  293. }
  294. else //attach directly to obj to which stack belongs and creature type
  295. {
  296. CArmedInstance * army = battle->battleGetArmyObject(side);
  297. attachTo(army);
  298. attachTo(const_cast<CCreature *>(type));
  299. }
  300. shots.reset();
  301. counterAttacks.reset();
  302. casts.reset();
  303. health.init();
  304. }
  305. ui32 CStack::level() const
  306. {
  307. if(base)
  308. return base->getLevel(); //creatture or commander
  309. else
  310. return std::max(1, (int)getCreature()->level); //war machine, clone etc
  311. }
  312. si32 CStack::magicResistance() const
  313. {
  314. si32 magicResistance;
  315. if(base) //TODO: make war machines receive aura of magic resistance
  316. {
  317. magicResistance = base->magicResistance();
  318. int auraBonus = 0;
  319. for(const CStack * stack : base->armyObj->battle-> batteAdjacentCreatures(this))
  320. {
  321. if(stack->owner == owner)
  322. {
  323. vstd::amax(auraBonus, stack->valOfBonuses(Bonus::SPELL_RESISTANCE_AURA)); //max value
  324. }
  325. }
  326. magicResistance += auraBonus;
  327. vstd::amin(magicResistance, 100);
  328. }
  329. else
  330. magicResistance = type->magicResistance();
  331. return magicResistance;
  332. }
  333. bool CStack::willMove(int turn /*= 0*/) const
  334. {
  335. return (turn ? true : !vstd::contains(state, EBattleStackState::DEFENDING))
  336. && !moved(turn)
  337. && canMove(turn);
  338. }
  339. bool CStack::canMove(int turn /*= 0*/) const
  340. {
  341. return alive()
  342. && !hasBonus(Selector::type(Bonus::NOT_ACTIVE).And(Selector::turns(turn))); //eg. Ammo Cart or blinded creature
  343. }
  344. bool CStack::canCast() const
  345. {
  346. return casts.canUse(1);//do not check specific cast abilities here
  347. }
  348. bool CStack::isCaster() const
  349. {
  350. return casts.total() > 0;//do not check specific cast abilities here
  351. }
  352. bool CStack::canShoot() const
  353. {
  354. return shots.canUse(1) && hasBonusOfType(Bonus::SHOOTER);
  355. }
  356. bool CStack::isShooter() const
  357. {
  358. return shots.total() > 0 && hasBonusOfType(Bonus::SHOOTER);
  359. }
  360. bool CStack::moved(int turn /*= 0*/) const
  361. {
  362. if(!turn)
  363. return vstd::contains(state, EBattleStackState::MOVED);
  364. else
  365. return false;
  366. }
  367. bool CStack::waited(int turn /*= 0*/) const
  368. {
  369. if(!turn)
  370. return vstd::contains(state, EBattleStackState::WAITING);
  371. else
  372. return false;
  373. }
  374. bool CStack::doubleWide() const
  375. {
  376. return getCreature()->doubleWide;
  377. }
  378. BattleHex CStack::occupiedHex() const
  379. {
  380. return occupiedHex(position);
  381. }
  382. BattleHex CStack::occupiedHex(BattleHex assumedPos) const
  383. {
  384. if(doubleWide())
  385. {
  386. if(side == BattleSide::ATTACKER)
  387. return assumedPos - 1;
  388. else
  389. return assumedPos + 1;
  390. }
  391. else
  392. {
  393. return BattleHex::INVALID;
  394. }
  395. }
  396. std::vector<BattleHex> CStack::getHexes() const
  397. {
  398. return getHexes(position);
  399. }
  400. std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos) const
  401. {
  402. return getHexes(assumedPos, doubleWide(), side);
  403. }
  404. std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos, bool twoHex, ui8 side)
  405. {
  406. std::vector<BattleHex> hexes;
  407. hexes.push_back(assumedPos);
  408. if(twoHex)
  409. {
  410. if(side == BattleSide::ATTACKER)
  411. hexes.push_back(assumedPos - 1);
  412. else
  413. hexes.push_back(assumedPos + 1);
  414. }
  415. return hexes;
  416. }
  417. bool CStack::coversPos(BattleHex pos) const
  418. {
  419. return vstd::contains(getHexes(), pos);
  420. }
  421. std::vector<BattleHex> CStack::getSurroundingHexes(BattleHex attackerPos) const
  422. {
  423. BattleHex hex = (attackerPos != BattleHex::INVALID) ? attackerPos : position; //use hypothetical position
  424. std::vector<BattleHex> hexes;
  425. if(doubleWide())
  426. {
  427. const int WN = GameConstants::BFIELD_WIDTH;
  428. if(side == BattleSide::ATTACKER)
  429. {
  430. //position is equal to front hex
  431. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN + 2 : WN + 1), hexes);
  432. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN + 1 : WN), hexes);
  433. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN : WN - 1), hexes);
  434. BattleHex::checkAndPush(hex - 2, hexes);
  435. BattleHex::checkAndPush(hex + 1, hexes);
  436. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN - 2 : WN - 1), hexes);
  437. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN - 1 : WN), hexes);
  438. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN : WN + 1), hexes);
  439. }
  440. else
  441. {
  442. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN + 1 : WN), hexes);
  443. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN : WN - 1), hexes);
  444. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN - 1 : WN - 2), hexes);
  445. BattleHex::checkAndPush(hex + 2, hexes);
  446. BattleHex::checkAndPush(hex - 1, hexes);
  447. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN - 1 : WN), hexes);
  448. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN : WN + 1), hexes);
  449. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN + 1 : WN + 2), hexes);
  450. }
  451. return hexes;
  452. }
  453. else
  454. {
  455. return hex.neighbouringTiles();
  456. }
  457. }
  458. BattleHex::EDir CStack::destShiftDir() const
  459. {
  460. if(doubleWide())
  461. {
  462. if(side == BattleSide::ATTACKER)
  463. return BattleHex::EDir::RIGHT;
  464. else
  465. return BattleHex::EDir::LEFT;
  466. }
  467. else
  468. {
  469. return BattleHex::EDir::NONE;
  470. }
  471. }
  472. std::vector<si32> CStack::activeSpells() const
  473. {
  474. std::vector<si32> ret;
  475. std::stringstream cachingStr;
  476. cachingStr << "!type_" << Bonus::NONE << "source_" << Bonus::SPELL_EFFECT;
  477. CSelector selector = Selector::sourceType(Bonus::SPELL_EFFECT)
  478. .And(CSelector([](const Bonus * b)->bool
  479. {
  480. return b->type != Bonus::NONE;
  481. }));
  482. TBonusListPtr spellEffects = getBonuses(selector, Selector::all, cachingStr.str());
  483. for(const std::shared_ptr<Bonus> it : *spellEffects)
  484. {
  485. if(!vstd::contains(ret, it->sid)) //do not duplicate spells with multiple effects
  486. ret.push_back(it->sid);
  487. }
  488. return ret;
  489. }
  490. CStack::~CStack()
  491. {
  492. detachFromAll();
  493. }
  494. const CGHeroInstance * CStack::getMyHero() const
  495. {
  496. if(base)
  497. return dynamic_cast<const CGHeroInstance *>(base->armyObj);
  498. else //we are attached directly?
  499. for(const CBonusSystemNode * n : getParentNodes())
  500. if(n->getNodeType() == HERO)
  501. return dynamic_cast<const CGHeroInstance *>(n);
  502. return nullptr;
  503. }
  504. std::string CStack::nodeName() const
  505. {
  506. std::ostringstream oss;
  507. oss << "Battle stack [" << ID << "]: " << health.getCount() << " creatures of ";
  508. if(type)
  509. oss << type->namePl;
  510. else
  511. oss << "[UNDEFINED TYPE]";
  512. oss << " from slot " << slot;
  513. if(base && base->armyObj)
  514. oss << " of armyobj=" << base->armyObj->id.getNum();
  515. return oss.str();
  516. }
  517. CHealth CStack::healthAfterAttacked(int32_t & damage) const
  518. {
  519. return healthAfterAttacked(damage, health);
  520. }
  521. CHealth CStack::healthAfterAttacked(int32_t & damage, const CHealth & customHealth) const
  522. {
  523. CHealth res = customHealth;
  524. if(isClone())
  525. {
  526. // block ability should not kill clone (0 damage)
  527. if(damage > 0)
  528. {
  529. damage = 1;//??? what should be actual damage against clone?
  530. res.reset();
  531. }
  532. }
  533. else
  534. {
  535. res.damage(damage);
  536. }
  537. return res;
  538. }
  539. CHealth CStack::healthAfterHealed(int32_t & toHeal, EHealLevel level, EHealPower power) const
  540. {
  541. CHealth res = health;
  542. if(level == EHealLevel::HEAL && power == EHealPower::ONE_BATTLE)
  543. logGlobal->error("Heal for one battle does not make sense", nodeName(), toHeal);
  544. else if(isClone())
  545. logGlobal->error("Attempt to heal clone: %s for %d HP", nodeName(), toHeal);
  546. else
  547. res.heal(toHeal, level, power);
  548. return res;
  549. }
  550. void CStack::prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand) const
  551. {
  552. prepareAttacked(bsa, rand, health);
  553. }
  554. void CStack::prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand, const CHealth & customHealth) const
  555. {
  556. CHealth afterAttack = healthAfterAttacked(bsa.damageAmount, customHealth);
  557. bsa.killedAmount = customHealth.getCount() - afterAttack.getCount();
  558. afterAttack.toInfo(bsa.newHealth);
  559. bsa.newHealth.stackId = ID;
  560. bsa.newHealth.delta = -bsa.damageAmount;
  561. if(afterAttack.available() <= 0 && isClone())
  562. {
  563. bsa.flags |= BattleStackAttacked::CLONE_KILLED;
  564. return; // no rebirth I believe
  565. }
  566. if(afterAttack.available() <= 0) //stack killed
  567. {
  568. bsa.flags |= BattleStackAttacked::KILLED;
  569. int resurrectFactor = valOfBonuses(Bonus::REBIRTH);
  570. if(resurrectFactor > 0 && canCast()) //there must be casts left
  571. {
  572. int resurrectedStackCount = baseAmount * resurrectFactor / 100;
  573. // last stack has proportional chance to rebirth
  574. //FIXME: diff is always 0
  575. auto diff = baseAmount * resurrectFactor / 100.0 - resurrectedStackCount;
  576. if(diff > rand.nextDouble(0, 0.99))
  577. {
  578. resurrectedStackCount += 1;
  579. }
  580. if(hasBonusOfType(Bonus::REBIRTH, 1))
  581. {
  582. // resurrect at least one Sacred Phoenix
  583. vstd::amax(resurrectedStackCount, 1);
  584. }
  585. if(resurrectedStackCount > 0)
  586. {
  587. bsa.flags |= BattleStackAttacked::REBIRTH;
  588. //TODO: use StackHealedOrResurrected
  589. bsa.newHealth.firstHPleft = MaxHealth();
  590. bsa.newHealth.fullUnits = resurrectedStackCount - 1;
  591. bsa.newHealth.resurrected = 0; //TODO: add one-battle rebirth?
  592. }
  593. }
  594. }
  595. }
  596. bool CStack::isMeleeAttackPossible(const CStack * attacker, const CStack * defender, BattleHex attackerPos /*= BattleHex::INVALID*/, BattleHex defenderPos /*= BattleHex::INVALID*/)
  597. {
  598. if(!attackerPos.isValid())
  599. attackerPos = attacker->position;
  600. if(!defenderPos.isValid())
  601. defenderPos = defender->position;
  602. return
  603. (BattleHex::mutualPosition(attackerPos, defenderPos) >= 0)//front <=> front
  604. || (attacker->doubleWide()//back <=> front
  605. && BattleHex::mutualPosition(attackerPos + (attacker->side == BattleSide::ATTACKER ? -1 : 1), defenderPos) >= 0)
  606. || (defender->doubleWide()//front <=> back
  607. && BattleHex::mutualPosition(attackerPos, defenderPos + (defender->side == BattleSide::ATTACKER ? -1 : 1)) >= 0)
  608. || (defender->doubleWide() && attacker->doubleWide()//back <=> back
  609. && BattleHex::mutualPosition(attackerPos + (attacker->side == BattleSide::ATTACKER ? -1 : 1), defenderPos + (defender->side == BattleSide::ATTACKER ? -1 : 1)) >= 0);
  610. }
  611. bool CStack::ableToRetaliate() const
  612. {
  613. return alive()
  614. && (counterAttacks.canUse() || hasBonusOfType(Bonus::UNLIMITED_RETALIATIONS))
  615. && !hasBonusOfType(Bonus::SIEGE_WEAPON)
  616. && !hasBonusOfType(Bonus::HYPNOTIZED)
  617. && !hasBonusOfType(Bonus::NO_RETALIATION);
  618. }
  619. std::string CStack::getName() const
  620. {
  621. return (health.getCount() == 1) ? type->nameSing : type->namePl; //War machines can't use base
  622. }
  623. bool CStack::isValidTarget(bool allowDead/* = false*/) const
  624. {
  625. return (alive() || (allowDead && isDead())) && position.isValid() && !isTurret();
  626. }
  627. bool CStack::isDead() const
  628. {
  629. return !alive() && !isGhost();
  630. }
  631. bool CStack::isClone() const
  632. {
  633. return vstd::contains(state, EBattleStackState::CLONED);
  634. }
  635. bool CStack::isGhost() const
  636. {
  637. return vstd::contains(state, EBattleStackState::GHOST);
  638. }
  639. bool CStack::isTurret() const
  640. {
  641. return type->idNumber == CreatureID::ARROW_TOWERS;
  642. }
  643. bool CStack::canBeHealed() const
  644. {
  645. return getFirstHPleft() < MaxHealth()
  646. && isValidTarget()
  647. && !hasBonusOfType(Bonus::SIEGE_WEAPON);
  648. }
  649. void CStack::makeGhost()
  650. {
  651. state.erase(EBattleStackState::ALIVE);
  652. state.insert(EBattleStackState::GHOST_PENDING);
  653. }
  654. bool CStack::alive() const //determines if stack is alive
  655. {
  656. return vstd::contains(state, EBattleStackState::ALIVE);
  657. }
  658. ui8 CStack::getSpellSchoolLevel(const CSpell * spell, int * outSelectedSchool) const
  659. {
  660. int skill = valOfBonuses(Selector::typeSubtype(Bonus::SPELLCASTER, spell->id));
  661. vstd::abetween(skill, 0, 3);
  662. return skill;
  663. }
  664. ui32 CStack::getSpellBonus(const CSpell * spell, ui32 base, const CStack * affectedStack) const
  665. {
  666. //stacks does not have sorcery-like bonuses (yet?)
  667. return base;
  668. }
  669. int CStack::getEffectLevel(const CSpell * spell) const
  670. {
  671. return getSpellSchoolLevel(spell);
  672. }
  673. int CStack::getEffectPower(const CSpell * spell) const
  674. {
  675. return valOfBonuses(Bonus::CREATURE_SPELL_POWER) * health.getCount() / 100;
  676. }
  677. int CStack::getEnchantPower(const CSpell * spell) const
  678. {
  679. int res = valOfBonuses(Bonus::CREATURE_ENCHANT_POWER);
  680. if(res <= 0)
  681. res = 3;//default for creatures
  682. return res;
  683. }
  684. int CStack::getEffectValue(const CSpell * spell) const
  685. {
  686. return valOfBonuses(Bonus::SPECIFIC_SPELL_POWER, spell->id.toEnum()) * health.getCount();
  687. }
  688. const PlayerColor CStack::getOwner() const
  689. {
  690. return battle->battleGetOwner(this);
  691. }
  692. void CStack::getCasterName(MetaString & text) const
  693. {
  694. //always plural name in case of spell cast.
  695. addNameReplacement(text, true);
  696. }
  697. void CStack::getCastDescription(const CSpell * spell, const std::vector<const CStack *> & attacked, MetaString & text) const
  698. {
  699. text.addTxt(MetaString::GENERAL_TXT, 565);//The %s casts %s
  700. //todo: use text 566 for single creature
  701. getCasterName(text);
  702. text.addReplacement(MetaString::SPELL_NAME, spell->id.toEnum());
  703. }
  704. int32_t CStack::unitMaxHealth() const
  705. {
  706. return MaxHealth();
  707. }
  708. int32_t CStack::unitBaseAmount() const
  709. {
  710. return baseAmount;
  711. }
  712. void CStack::addText(MetaString & text, ui8 type, int32_t serial, const boost::logic::tribool & plural) const
  713. {
  714. if(boost::logic::indeterminate(plural))
  715. serial = VLC->generaltexth->pluralText(serial, health.getCount());
  716. else if(plural)
  717. serial = VLC->generaltexth->pluralText(serial, 2);
  718. else
  719. serial = VLC->generaltexth->pluralText(serial, 1);
  720. text.addTxt(type, serial);
  721. }
  722. void CStack::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) const
  723. {
  724. if(boost::logic::indeterminate(plural))
  725. text.addCreReplacement(type->idNumber, health.getCount());
  726. else if(plural)
  727. text.addReplacement(MetaString::CRE_PL_NAMES, type->idNumber.num);
  728. else
  729. text.addReplacement(MetaString::CRE_SING_NAMES, type->idNumber.num);
  730. }
  731. std::string CStack::formatGeneralMessage(const int32_t baseTextId) const
  732. {
  733. const int32_t textId = VLC->generaltexth->pluralText(baseTextId, health.getCount());
  734. MetaString text;
  735. text.addTxt(MetaString::GENERAL_TXT, textId);
  736. text.addCreReplacement(type->idNumber, health.getCount());
  737. return text.toString();
  738. }
  739. void CStack::setHealth(const CHealthInfo & value)
  740. {
  741. health.reset();
  742. health.fromInfo(value);
  743. }
  744. void CStack::setHealth(const CHealth & value)
  745. {
  746. health = value;
  747. }