2
0

CStack.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. if(resurrected != 0)
  221. {
  222. int64_t totalHealth = available();
  223. totalHealth -= resurrected * owner->unitMaxHealth();
  224. vstd::amax(totalHealth, 0);
  225. setFromTotal(totalHealth);
  226. resurrected = 0;
  227. }
  228. }
  229. ///CStack
  230. CStack::CStack(const CStackInstance * Base, PlayerColor O, int I, ui8 Side, SlotID S):
  231. base(Base), ID(I), owner(O), slot(S), side(Side),
  232. counterAttacks(this), shots(this), casts(this), health(this), cloneID(-1),
  233. position()
  234. {
  235. assert(base);
  236. type = base->type;
  237. baseAmount = base->count;
  238. health.init(); //???
  239. setNodeType(STACK_BATTLE);
  240. }
  241. CStack::CStack():
  242. counterAttacks(this), shots(this), casts(this), health(this)
  243. {
  244. init();
  245. setNodeType(STACK_BATTLE);
  246. }
  247. CStack::CStack(const CStackBasicDescriptor * stack, PlayerColor O, int I, ui8 Side, SlotID S):
  248. base(nullptr), ID(I), owner(O), slot(S), side(Side),
  249. counterAttacks(this), shots(this), casts(this), health(this), cloneID(-1),
  250. position()
  251. {
  252. type = stack->type;
  253. baseAmount = stack->count;
  254. health.init(); //???
  255. setNodeType(STACK_BATTLE);
  256. }
  257. int32_t CStack::getKilled() const
  258. {
  259. int32_t res = baseAmount - health.getCount() + health.getResurrected();
  260. vstd::amax(res, 0);
  261. return res;
  262. }
  263. int32_t CStack::getCount() const
  264. {
  265. return health.getCount();
  266. }
  267. int32_t CStack::getFirstHPleft() const
  268. {
  269. return health.getFirstHPleft();
  270. }
  271. const CCreature * CStack::getCreature() const
  272. {
  273. return type;
  274. }
  275. void CStack::init()
  276. {
  277. base = nullptr;
  278. type = nullptr;
  279. ID = -1;
  280. baseAmount = -1;
  281. owner = PlayerColor::NEUTRAL;
  282. slot = SlotID(255);
  283. side = 1;
  284. position = BattleHex();
  285. cloneID = -1;
  286. }
  287. void CStack::localInit(BattleInfo * battleInfo)
  288. {
  289. battle = battleInfo;
  290. cloneID = -1;
  291. assert(type);
  292. exportBonuses();
  293. if(base) //stack originating from "real" stack in garrison -> attach to it
  294. {
  295. attachTo(const_cast<CStackInstance *>(base));
  296. }
  297. else //attach directly to obj to which stack belongs and creature type
  298. {
  299. CArmedInstance * army = battle->battleGetArmyObject(side);
  300. attachTo(army);
  301. attachTo(const_cast<CCreature *>(type));
  302. }
  303. shots.reset();
  304. counterAttacks.reset();
  305. casts.reset();
  306. health.init();
  307. }
  308. ui32 CStack::level() const
  309. {
  310. if(base)
  311. return base->getLevel(); //creatture or commander
  312. else
  313. return std::max(1, (int)getCreature()->level); //war machine, clone etc
  314. }
  315. si32 CStack::magicResistance() const
  316. {
  317. si32 magicResistance;
  318. if(base) //TODO: make war machines receive aura of magic resistance
  319. {
  320. magicResistance = base->magicResistance();
  321. int auraBonus = 0;
  322. for(const CStack * stack : base->armyObj->battle-> batteAdjacentCreatures(this))
  323. {
  324. if(stack->owner == owner)
  325. {
  326. vstd::amax(auraBonus, stack->valOfBonuses(Bonus::SPELL_RESISTANCE_AURA)); //max value
  327. }
  328. }
  329. magicResistance += auraBonus;
  330. vstd::amin(magicResistance, 100);
  331. }
  332. else
  333. magicResistance = type->magicResistance();
  334. return magicResistance;
  335. }
  336. bool CStack::willMove(int turn) const
  337. {
  338. return (turn ? true : !vstd::contains(state, EBattleStackState::DEFENDING))
  339. && !moved(turn)
  340. && canMove(turn);
  341. }
  342. bool CStack::canMove(int turn) const
  343. {
  344. return alive()
  345. && !hasBonus(Selector::type(Bonus::NOT_ACTIVE).And(Selector::turns(turn))); //eg. Ammo Cart or blinded creature
  346. }
  347. bool CStack::canCast() const
  348. {
  349. return casts.canUse(1);//do not check specific cast abilities here
  350. }
  351. bool CStack::isCaster() const
  352. {
  353. return casts.total() > 0;//do not check specific cast abilities here
  354. }
  355. bool CStack::canShoot() const
  356. {
  357. return shots.canUse(1) && hasBonusOfType(Bonus::SHOOTER);
  358. }
  359. bool CStack::isShooter() const
  360. {
  361. return shots.total() > 0 && hasBonusOfType(Bonus::SHOOTER);
  362. }
  363. bool CStack::moved(int turn) const
  364. {
  365. if(!turn)
  366. return vstd::contains(state, EBattleStackState::MOVED);
  367. else
  368. return false;
  369. }
  370. bool CStack::waited(int turn) const
  371. {
  372. if(!turn)
  373. return vstd::contains(state, EBattleStackState::WAITING);
  374. else
  375. return false;
  376. }
  377. bool CStack::doubleWide() const
  378. {
  379. return getCreature()->doubleWide;
  380. }
  381. BattleHex CStack::occupiedHex() const
  382. {
  383. return occupiedHex(position);
  384. }
  385. BattleHex CStack::occupiedHex(BattleHex assumedPos) const
  386. {
  387. if(doubleWide())
  388. {
  389. if(side == BattleSide::ATTACKER)
  390. return assumedPos - 1;
  391. else
  392. return assumedPos + 1;
  393. }
  394. else
  395. {
  396. return BattleHex::INVALID;
  397. }
  398. }
  399. std::vector<BattleHex> CStack::getHexes() const
  400. {
  401. return getHexes(position);
  402. }
  403. std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos) const
  404. {
  405. return getHexes(assumedPos, doubleWide(), side);
  406. }
  407. std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos, bool twoHex, ui8 side)
  408. {
  409. std::vector<BattleHex> hexes;
  410. hexes.push_back(assumedPos);
  411. if(twoHex)
  412. {
  413. if(side == BattleSide::ATTACKER)
  414. hexes.push_back(assumedPos - 1);
  415. else
  416. hexes.push_back(assumedPos + 1);
  417. }
  418. return hexes;
  419. }
  420. bool CStack::coversPos(BattleHex pos) const
  421. {
  422. return vstd::contains(getHexes(), pos);
  423. }
  424. std::vector<BattleHex> CStack::getSurroundingHexes(BattleHex attackerPos) const
  425. {
  426. BattleHex hex = (attackerPos != BattleHex::INVALID) ? attackerPos : position; //use hypothetical position
  427. std::vector<BattleHex> hexes;
  428. if(doubleWide())
  429. {
  430. const int WN = GameConstants::BFIELD_WIDTH;
  431. if(side == BattleSide::ATTACKER)
  432. {
  433. //position is equal to front hex
  434. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN + 2 : WN + 1), hexes);
  435. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN + 1 : WN), hexes);
  436. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN : WN - 1), hexes);
  437. BattleHex::checkAndPush(hex - 2, hexes);
  438. BattleHex::checkAndPush(hex + 1, hexes);
  439. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN - 2 : WN - 1), hexes);
  440. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN - 1 : WN), hexes);
  441. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN : WN + 1), hexes);
  442. }
  443. else
  444. {
  445. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN + 1 : WN), hexes);
  446. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN : WN - 1), hexes);
  447. BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN - 1 : WN - 2), hexes);
  448. BattleHex::checkAndPush(hex + 2, hexes);
  449. BattleHex::checkAndPush(hex - 1, hexes);
  450. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN - 1 : WN), hexes);
  451. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN : WN + 1), hexes);
  452. BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN + 1 : WN + 2), hexes);
  453. }
  454. return hexes;
  455. }
  456. else
  457. {
  458. return hex.neighbouringTiles();
  459. }
  460. }
  461. BattleHex::EDir CStack::destShiftDir() const
  462. {
  463. if(doubleWide())
  464. {
  465. if(side == BattleSide::ATTACKER)
  466. return BattleHex::EDir::RIGHT;
  467. else
  468. return BattleHex::EDir::LEFT;
  469. }
  470. else
  471. {
  472. return BattleHex::EDir::NONE;
  473. }
  474. }
  475. std::vector<si32> CStack::activeSpells() const
  476. {
  477. std::vector<si32> ret;
  478. std::stringstream cachingStr;
  479. cachingStr << "!type_" << Bonus::NONE << "source_" << Bonus::SPELL_EFFECT;
  480. CSelector selector = Selector::sourceType(Bonus::SPELL_EFFECT)
  481. .And(CSelector([](const Bonus * b)->bool
  482. {
  483. return b->type != Bonus::NONE;
  484. }));
  485. TBonusListPtr spellEffects = getBonuses(selector, Selector::all, cachingStr.str());
  486. for(const std::shared_ptr<Bonus> it : *spellEffects)
  487. {
  488. if(!vstd::contains(ret, it->sid)) //do not duplicate spells with multiple effects
  489. ret.push_back(it->sid);
  490. }
  491. return ret;
  492. }
  493. CStack::~CStack()
  494. {
  495. detachFromAll();
  496. }
  497. const CGHeroInstance * CStack::getMyHero() const
  498. {
  499. if(base)
  500. return dynamic_cast<const CGHeroInstance *>(base->armyObj);
  501. else //we are attached directly?
  502. for(const CBonusSystemNode * n : getParentNodes())
  503. if(n->getNodeType() == HERO)
  504. return dynamic_cast<const CGHeroInstance *>(n);
  505. return nullptr;
  506. }
  507. std::string CStack::nodeName() const
  508. {
  509. std::ostringstream oss;
  510. oss << "Battle stack [" << ID << "]: " << health.getCount() << " creatures of ";
  511. if(type)
  512. oss << type->namePl;
  513. else
  514. oss << "[UNDEFINED TYPE]";
  515. oss << " from slot " << slot;
  516. if(base && base->armyObj)
  517. oss << " of armyobj=" << base->armyObj->id.getNum();
  518. return oss.str();
  519. }
  520. CHealth CStack::healthAfterAttacked(int32_t & damage) const
  521. {
  522. return healthAfterAttacked(damage, health);
  523. }
  524. CHealth CStack::healthAfterAttacked(int32_t & damage, const CHealth & customHealth) const
  525. {
  526. CHealth res = customHealth;
  527. if(isClone())
  528. {
  529. // block ability should not kill clone (0 damage)
  530. if(damage > 0)
  531. {
  532. damage = 1;//??? what should be actual damage against clone?
  533. res.reset();
  534. }
  535. }
  536. else
  537. {
  538. res.damage(damage);
  539. }
  540. return res;
  541. }
  542. CHealth CStack::healthAfterHealed(int32_t & toHeal, EHealLevel level, EHealPower power) const
  543. {
  544. CHealth res = health;
  545. if(level == EHealLevel::HEAL && power == EHealPower::ONE_BATTLE)
  546. logGlobal->error("Heal for one battle does not make sense", nodeName(), toHeal);
  547. else if(isClone())
  548. logGlobal->error("Attempt to heal clone: %s for %d HP", nodeName(), toHeal);
  549. else
  550. res.heal(toHeal, level, power);
  551. return res;
  552. }
  553. void CStack::prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand) const
  554. {
  555. prepareAttacked(bsa, rand, health);
  556. }
  557. void CStack::prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand, const CHealth & customHealth) const
  558. {
  559. CHealth afterAttack = healthAfterAttacked(bsa.damageAmount, customHealth);
  560. bsa.killedAmount = customHealth.getCount() - afterAttack.getCount();
  561. afterAttack.toInfo(bsa.newHealth);
  562. bsa.newHealth.stackId = ID;
  563. bsa.newHealth.delta = -bsa.damageAmount;
  564. if(afterAttack.available() <= 0 && isClone())
  565. {
  566. bsa.flags |= BattleStackAttacked::CLONE_KILLED;
  567. return; // no rebirth I believe
  568. }
  569. if(afterAttack.available() <= 0) //stack killed
  570. {
  571. bsa.flags |= BattleStackAttacked::KILLED;
  572. int resurrectFactor = valOfBonuses(Bonus::REBIRTH);
  573. if(resurrectFactor > 0 && canCast()) //there must be casts left
  574. {
  575. int resurrectedStackCount = baseAmount * resurrectFactor / 100;
  576. // last stack has proportional chance to rebirth
  577. //FIXME: diff is always 0
  578. auto diff = baseAmount * resurrectFactor / 100.0 - resurrectedStackCount;
  579. if(diff > rand.nextDouble(0, 0.99))
  580. {
  581. resurrectedStackCount += 1;
  582. }
  583. if(hasBonusOfType(Bonus::REBIRTH, 1))
  584. {
  585. // resurrect at least one Sacred Phoenix
  586. vstd::amax(resurrectedStackCount, 1);
  587. }
  588. if(resurrectedStackCount > 0)
  589. {
  590. bsa.flags |= BattleStackAttacked::REBIRTH;
  591. //TODO: use StackHealedOrResurrected
  592. bsa.newHealth.firstHPleft = MaxHealth();
  593. bsa.newHealth.fullUnits = resurrectedStackCount - 1;
  594. bsa.newHealth.resurrected = 0; //TODO: add one-battle rebirth?
  595. }
  596. }
  597. }
  598. }
  599. bool CStack::isMeleeAttackPossible(const CStack * attacker, const CStack * defender, BattleHex attackerPos, BattleHex defenderPos)
  600. {
  601. if(!attackerPos.isValid())
  602. attackerPos = attacker->position;
  603. if(!defenderPos.isValid())
  604. defenderPos = defender->position;
  605. return
  606. (BattleHex::mutualPosition(attackerPos, defenderPos) >= 0)//front <=> front
  607. || (attacker->doubleWide()//back <=> front
  608. && BattleHex::mutualPosition(attackerPos + (attacker->side == BattleSide::ATTACKER ? -1 : 1), defenderPos) >= 0)
  609. || (defender->doubleWide()//front <=> back
  610. && BattleHex::mutualPosition(attackerPos, defenderPos + (defender->side == BattleSide::ATTACKER ? -1 : 1)) >= 0)
  611. || (defender->doubleWide() && attacker->doubleWide()//back <=> back
  612. && BattleHex::mutualPosition(attackerPos + (attacker->side == BattleSide::ATTACKER ? -1 : 1), defenderPos + (defender->side == BattleSide::ATTACKER ? -1 : 1)) >= 0);
  613. }
  614. bool CStack::ableToRetaliate() const
  615. {
  616. return alive()
  617. && (counterAttacks.canUse() || hasBonusOfType(Bonus::UNLIMITED_RETALIATIONS))
  618. && !hasBonusOfType(Bonus::SIEGE_WEAPON)
  619. && !hasBonusOfType(Bonus::HYPNOTIZED)
  620. && !hasBonusOfType(Bonus::NO_RETALIATION);
  621. }
  622. std::string CStack::getName() const
  623. {
  624. return (health.getCount() == 1) ? type->nameSing : type->namePl; //War machines can't use base
  625. }
  626. bool CStack::isValidTarget(bool allowDead) const
  627. {
  628. return (alive() || (allowDead && isDead())) && position.isValid() && !isTurret();
  629. }
  630. bool CStack::isDead() const
  631. {
  632. return !alive() && !isGhost();
  633. }
  634. bool CStack::isClone() const
  635. {
  636. return vstd::contains(state, EBattleStackState::CLONED);
  637. }
  638. bool CStack::isGhost() const
  639. {
  640. return vstd::contains(state, EBattleStackState::GHOST);
  641. }
  642. bool CStack::isTurret() const
  643. {
  644. return type->idNumber == CreatureID::ARROW_TOWERS;
  645. }
  646. bool CStack::canBeHealed() const
  647. {
  648. return getFirstHPleft() < MaxHealth()
  649. && isValidTarget()
  650. && !hasBonusOfType(Bonus::SIEGE_WEAPON);
  651. }
  652. void CStack::makeGhost()
  653. {
  654. state.erase(EBattleStackState::ALIVE);
  655. state.insert(EBattleStackState::GHOST_PENDING);
  656. }
  657. bool CStack::alive() const //determines if stack is alive
  658. {
  659. return vstd::contains(state, EBattleStackState::ALIVE);
  660. }
  661. ui8 CStack::getSpellSchoolLevel(const CSpell * spell, int * outSelectedSchool) const
  662. {
  663. int skill = valOfBonuses(Selector::typeSubtype(Bonus::SPELLCASTER, spell->id));
  664. vstd::abetween(skill, 0, 3);
  665. return skill;
  666. }
  667. ui32 CStack::getSpellBonus(const CSpell * spell, ui32 base, const CStack * affectedStack) const
  668. {
  669. //stacks does not have sorcery-like bonuses (yet?)
  670. return base;
  671. }
  672. int CStack::getEffectLevel(const CSpell * spell) const
  673. {
  674. return getSpellSchoolLevel(spell);
  675. }
  676. int CStack::getEffectPower(const CSpell * spell) const
  677. {
  678. return valOfBonuses(Bonus::CREATURE_SPELL_POWER) * health.getCount() / 100;
  679. }
  680. int CStack::getEnchantPower(const CSpell * spell) const
  681. {
  682. int res = valOfBonuses(Bonus::CREATURE_ENCHANT_POWER);
  683. if(res <= 0)
  684. res = 3;//default for creatures
  685. return res;
  686. }
  687. int CStack::getEffectValue(const CSpell * spell) const
  688. {
  689. return valOfBonuses(Bonus::SPECIFIC_SPELL_POWER, spell->id.toEnum()) * health.getCount();
  690. }
  691. const PlayerColor CStack::getOwner() const
  692. {
  693. return battle->battleGetOwner(this);
  694. }
  695. void CStack::getCasterName(MetaString & text) const
  696. {
  697. //always plural name in case of spell cast.
  698. addNameReplacement(text, true);
  699. }
  700. void CStack::getCastDescription(const CSpell * spell, const std::vector<const CStack *> & attacked, MetaString & text) const
  701. {
  702. text.addTxt(MetaString::GENERAL_TXT, 565);//The %s casts %s
  703. //todo: use text 566 for single creature
  704. getCasterName(text);
  705. text.addReplacement(MetaString::SPELL_NAME, spell->id.toEnum());
  706. }
  707. int32_t CStack::unitMaxHealth() const
  708. {
  709. return MaxHealth();
  710. }
  711. int32_t CStack::unitBaseAmount() const
  712. {
  713. return baseAmount;
  714. }
  715. void CStack::addText(MetaString & text, ui8 type, int32_t serial, const boost::logic::tribool & plural) const
  716. {
  717. if(boost::logic::indeterminate(plural))
  718. serial = VLC->generaltexth->pluralText(serial, health.getCount());
  719. else if(plural)
  720. serial = VLC->generaltexth->pluralText(serial, 2);
  721. else
  722. serial = VLC->generaltexth->pluralText(serial, 1);
  723. text.addTxt(type, serial);
  724. }
  725. void CStack::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) const
  726. {
  727. if(boost::logic::indeterminate(plural))
  728. text.addCreReplacement(type->idNumber, health.getCount());
  729. else if(plural)
  730. text.addReplacement(MetaString::CRE_PL_NAMES, type->idNumber.num);
  731. else
  732. text.addReplacement(MetaString::CRE_SING_NAMES, type->idNumber.num);
  733. }
  734. std::string CStack::formatGeneralMessage(const int32_t baseTextId) const
  735. {
  736. const int32_t textId = VLC->generaltexth->pluralText(baseTextId, health.getCount());
  737. MetaString text;
  738. text.addTxt(MetaString::GENERAL_TXT, textId);
  739. text.addCreReplacement(type->idNumber, health.getCount());
  740. return text.toString();
  741. }
  742. void CStack::setHealth(const CHealthInfo & value)
  743. {
  744. health.reset();
  745. health.fromInfo(value);
  746. }
  747. void CStack::setHealth(const CHealth & value)
  748. {
  749. health = value;
  750. }