CStack.cpp 20 KB

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