CBattleInfoCallback.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. /*
  2. * CBattleInfoCallback.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 "CBattleInfoCallback.h"
  12. #include <vcmi/scripting/Service.h>
  13. #include "../CStack.h"
  14. #include "BattleInfo.h"
  15. #include "../NetPacks.h"
  16. #include "../spells/CSpellHandler.h"
  17. #include "../mapObjects/CGTownInstance.h"
  18. #include "../BattleFieldHandler.h"
  19. #include "../CModHandler.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. namespace SiegeStuffThatShouldBeMovedToHandlers // <=== TODO
  22. {
  23. /*
  24. *Here are 2 explanations how below algorithm should work in H3, looks like they are not 100% accurate as it results in one damage number, not min/max range:
  25. *
  26. *1. http://heroes.thelazy.net/wiki/Arrow_tower
  27. *
  28. *2. All towns' turrets do the same damage. If Fort, Citadel or Castle is built damage of the Middle turret is 15, and 7,5 for others.
  29. *Buildings increase turrets' damage, but only those buildings that are new in town view, not upgrades to the existing. So, every building save:
  30. *- dwellings' upgrades
  31. *- Mage Guild upgrades
  32. *- Horde buildings
  33. *- income upgrades
  34. *- some special ones
  35. *increases middle Turret damage by 3, and 1,5 for the other two.
  36. *Damage is almost always the maximum one (right click on the Turret), sometimes +1/2 points, and it does not depend on the target. Nothing can influence it, except the mentioned above (but it will be roughly double if the defender has Armorer or Air Shield).
  37. *Maximum damage for Castle, Conflux is 120, Necropolis, Inferno, Fortress 125, Stronghold, Turret, and Dungeon 130 (for all three Turrets).
  38. *Artillery allows the player to control the Turrets.
  39. */
  40. static void retrieveTurretDamageRange(const CGTownInstance * town, const battle::Unit * turret, double & outMinDmg, double & outMaxDmg)//does not match OH3 yet, but damage is somewhat close
  41. {
  42. assert(turret->creatureIndex() == CreatureID::ARROW_TOWERS);
  43. assert(town);
  44. assert(turret->getPosition() >= -4 && turret->getPosition() <= -2);
  45. const float multiplier = (turret->getPosition() == -2) ? 1.0f : 0.5f;
  46. //Revised - Where do below values come from?
  47. /*int baseMin = 6;
  48. int baseMax = 10;*/
  49. const int baseDamage = 15;
  50. outMinDmg = multiplier * (baseDamage + town->getTownLevel() * 3);
  51. outMaxDmg = outMinDmg;
  52. }
  53. static BattleHex lineToWallHex(int line) //returns hex with wall in given line (y coordinate)
  54. {
  55. static const BattleHex lineToHex[] = {12, 29, 45, 62, 78, 95, 112, 130, 147, 165, 182};
  56. return lineToHex[line];
  57. }
  58. static bool sameSideOfWall(BattleHex pos1, BattleHex pos2)
  59. {
  60. const int wallInStackLine = lineToWallHex(pos1.getY());
  61. const int wallInDestLine = lineToWallHex(pos2.getY());
  62. const bool stackLeft = pos1 < wallInStackLine;
  63. const bool destLeft = pos2 < wallInDestLine;
  64. return stackLeft == destLeft;
  65. }
  66. // parts of wall
  67. static const std::pair<int, EWallPart::EWallPart> wallParts[] =
  68. {
  69. std::make_pair(50, EWallPart::KEEP),
  70. std::make_pair(183, EWallPart::BOTTOM_TOWER),
  71. std::make_pair(182, EWallPart::BOTTOM_WALL),
  72. std::make_pair(130, EWallPart::BELOW_GATE),
  73. std::make_pair(78, EWallPart::OVER_GATE),
  74. std::make_pair(29, EWallPart::UPPER_WALL),
  75. std::make_pair(12, EWallPart::UPPER_TOWER),
  76. std::make_pair(95, EWallPart::INDESTRUCTIBLE_PART_OF_GATE),
  77. std::make_pair(96, EWallPart::GATE),
  78. std::make_pair(45, EWallPart::INDESTRUCTIBLE_PART),
  79. std::make_pair(62, EWallPart::INDESTRUCTIBLE_PART),
  80. std::make_pair(112, EWallPart::INDESTRUCTIBLE_PART),
  81. std::make_pair(147, EWallPart::INDESTRUCTIBLE_PART),
  82. std::make_pair(165, EWallPart::INDESTRUCTIBLE_PART)
  83. };
  84. static EWallPart::EWallPart hexToWallPart(BattleHex hex)
  85. {
  86. for(auto & elem : wallParts)
  87. {
  88. if(elem.first == hex)
  89. return elem.second;
  90. }
  91. return EWallPart::INVALID; //not found!
  92. }
  93. static BattleHex WallPartToHex(EWallPart::EWallPart part)
  94. {
  95. for(auto & elem : wallParts)
  96. {
  97. if(elem.second == part)
  98. return elem.first;
  99. }
  100. return BattleHex::INVALID; //not found!
  101. }
  102. }
  103. using namespace SiegeStuffThatShouldBeMovedToHandlers;
  104. ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastSpell(const spells::Caster * caster, spells::Mode mode) const
  105. {
  106. RETURN_IF_NOT_BATTLE(ESpellCastProblem::INVALID);
  107. if(caster == nullptr)
  108. {
  109. logGlobal->error("CBattleInfoCallback::battleCanCastSpell: no spellcaster.");
  110. return ESpellCastProblem::INVALID;
  111. }
  112. const PlayerColor player = caster->getCasterOwner();
  113. const auto side = playerToSide(player);
  114. if(!side)
  115. return ESpellCastProblem::INVALID;
  116. if(!battleDoWeKnowAbout(side.get()))
  117. {
  118. logGlobal->warn("You can't check if enemy can cast given spell!");
  119. return ESpellCastProblem::INVALID;
  120. }
  121. if(battleTacticDist())
  122. return ESpellCastProblem::ONGOING_TACTIC_PHASE;
  123. switch(mode)
  124. {
  125. case spells::Mode::HERO:
  126. {
  127. if(battleCastSpells(side.get()) > 0)
  128. return ESpellCastProblem::CASTS_PER_TURN_LIMIT;
  129. auto hero = dynamic_cast<const CGHeroInstance *>(caster);
  130. if(!hero)
  131. return ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
  132. if(hero->hasBonusOfType(Bonus::BLOCK_ALL_MAGIC))
  133. return ESpellCastProblem::MAGIC_IS_BLOCKED;
  134. }
  135. break;
  136. default:
  137. break;
  138. }
  139. return ESpellCastProblem::OK;
  140. }
  141. bool CBattleInfoCallback::battleHasWallPenalty(const IBonusBearer * shooter, BattleHex shooterPosition, BattleHex destHex) const
  142. {
  143. RETURN_IF_NOT_BATTLE(false);
  144. if(!battleGetSiegeLevel())
  145. return false;
  146. const std::string cachingStrNoWallPenalty = "type_NO_WALL_PENALTY";
  147. static const auto selectorNoWallPenalty = Selector::type()(Bonus::NO_WALL_PENALTY);
  148. if(shooter->hasBonus(selectorNoWallPenalty, cachingStrNoWallPenalty))
  149. return false;
  150. const int wallInStackLine = lineToWallHex(shooterPosition.getY());
  151. const int wallInDestLine = lineToWallHex(destHex.getY());
  152. const bool stackLeft = shooterPosition < wallInStackLine;
  153. const bool destRight = destHex > wallInDestLine;
  154. if (stackLeft && destRight) //shooting from outside to inside
  155. {
  156. int row = (shooterPosition + destHex) / (2 * GameConstants::BFIELD_WIDTH);
  157. if (shooterPosition > destHex && ((destHex % GameConstants::BFIELD_WIDTH - shooterPosition % GameConstants::BFIELD_WIDTH) < 2)) //shooting up high
  158. row -= 2;
  159. const int wallPos = lineToWallHex(row);
  160. if (!isWallPartPotentiallyAttackable(battleHexToWallPart(wallPos))) return true;
  161. }
  162. return false;
  163. }
  164. si8 CBattleInfoCallback::battleCanTeleportTo(const battle::Unit * stack, BattleHex destHex, int telportLevel) const
  165. {
  166. RETURN_IF_NOT_BATTLE(false);
  167. if (!getAccesibility(stack).accessible(destHex, stack))
  168. return false;
  169. const ui8 siegeLevel = battleGetSiegeLevel();
  170. //check for wall
  171. //advanced teleport can pass wall of fort|citadel, expert - of castle
  172. if ((siegeLevel > CGTownInstance::NONE && telportLevel < 2) || (siegeLevel >= CGTownInstance::CASTLE && telportLevel < 3))
  173. return sameSideOfWall(stack->getPosition(), destHex);
  174. return true;
  175. }
  176. std::vector<PossiblePlayerBattleAction> CBattleInfoCallback::getClientActionsForStack(const CStack * stack, const BattleClientInterfaceData & data)
  177. {
  178. RETURN_IF_NOT_BATTLE(std::vector<PossiblePlayerBattleAction>());
  179. std::vector<PossiblePlayerBattleAction> allowedActionList;
  180. if(data.tacticsMode) //would "if(battleGetTacticDist() > 0)" work?
  181. {
  182. allowedActionList.push_back(PossiblePlayerBattleAction::MOVE_TACTICS);
  183. allowedActionList.push_back(PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK);
  184. }
  185. else
  186. {
  187. if(stack->canCast()) //TODO: check for battlefield effects that prevent casting?
  188. {
  189. if(stack->hasBonusOfType(Bonus::SPELLCASTER) && data.creatureSpellToCast != -1)
  190. {
  191. const CSpell *spell = SpellID(data.creatureSpellToCast).toSpell();
  192. PossiblePlayerBattleAction act = getCasterAction(spell, stack, spells::Mode::CREATURE_ACTIVE);
  193. allowedActionList.push_back(act);
  194. }
  195. if(stack->hasBonusOfType(Bonus::RANDOM_SPELLCASTER))
  196. allowedActionList.push_back(PossiblePlayerBattleAction::RANDOM_GENIE_SPELL);
  197. if(stack->hasBonusOfType(Bonus::DAEMON_SUMMONING))
  198. allowedActionList.push_back(PossiblePlayerBattleAction::RISE_DEMONS);
  199. }
  200. if(stack->canShoot())
  201. allowedActionList.push_back(PossiblePlayerBattleAction::SHOOT);
  202. if(stack->hasBonusOfType(Bonus::RETURN_AFTER_STRIKE))
  203. allowedActionList.push_back(PossiblePlayerBattleAction::ATTACK_AND_RETURN);
  204. allowedActionList.push_back(PossiblePlayerBattleAction::ATTACK); //all active stacks can attack
  205. allowedActionList.push_back(PossiblePlayerBattleAction::WALK_AND_ATTACK); //not all stacks can always walk, but we will check this elsewhere
  206. if(stack->canMove() && stack->Speed(0, true)) //probably no reason to try move war machines or bound stacks
  207. allowedActionList.push_back(PossiblePlayerBattleAction::MOVE_STACK);
  208. auto siegedTown = battleGetDefendedTown();
  209. if(siegedTown && siegedTown->hasFort() && stack->hasBonusOfType(Bonus::CATAPULT)) //TODO: check shots
  210. allowedActionList.push_back(PossiblePlayerBattleAction::CATAPULT);
  211. if(stack->hasBonusOfType(Bonus::HEALER))
  212. allowedActionList.push_back(PossiblePlayerBattleAction::HEAL);
  213. }
  214. return allowedActionList;
  215. }
  216. PossiblePlayerBattleAction CBattleInfoCallback::getCasterAction(const CSpell * spell, const spells::Caster * caster, spells::Mode mode) const
  217. {
  218. RETURN_IF_NOT_BATTLE(PossiblePlayerBattleAction::INVALID);
  219. PossiblePlayerBattleAction spellSelMode = PossiblePlayerBattleAction::ANY_LOCATION;
  220. const CSpell::TargetInfo ti(spell, caster->getSpellSchoolLevel(spell), mode);
  221. if(ti.massive || ti.type == spells::AimType::NO_TARGET)
  222. spellSelMode = PossiblePlayerBattleAction::NO_LOCATION;
  223. else if(ti.type == spells::AimType::LOCATION && ti.clearAffected)
  224. spellSelMode = PossiblePlayerBattleAction::FREE_LOCATION;
  225. else if(ti.type == spells::AimType::CREATURE)
  226. spellSelMode = PossiblePlayerBattleAction::AIMED_SPELL_CREATURE;
  227. else if(ti.type == spells::AimType::OBSTACLE)
  228. spellSelMode = PossiblePlayerBattleAction::OBSTACLE;
  229. return spellSelMode;
  230. }
  231. std::set<BattleHex> CBattleInfoCallback::battleGetAttackedHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos) const
  232. {
  233. std::set<BattleHex> attackedHexes;
  234. RETURN_IF_NOT_BATTLE(attackedHexes);
  235. AttackableTiles at = getPotentiallyAttackableHexes(attacker, destinationTile, attackerPos);
  236. for (BattleHex tile : at.hostileCreaturePositions)
  237. {
  238. const CStack * st = battleGetStackByPos(tile, true);
  239. if(st && st->owner != attacker->owner) //only hostile stacks - does it work well with Berserk?
  240. {
  241. attackedHexes.insert(tile);
  242. }
  243. }
  244. for (BattleHex tile : at.friendlyCreaturePositions)
  245. {
  246. if(battleGetStackByPos(tile, true)) //friendly stacks can also be damaged by Dragon Breath
  247. {
  248. attackedHexes.insert(tile);
  249. }
  250. }
  251. return attackedHexes;
  252. }
  253. SpellID CBattleInfoCallback::battleGetRandomStackSpell(CRandomGenerator & rand, const CStack * stack, ERandomSpell mode) const
  254. {
  255. switch (mode)
  256. {
  257. case RANDOM_GENIE:
  258. return getRandomBeneficialSpell(rand, stack); //target
  259. break;
  260. case RANDOM_AIMED:
  261. return getRandomCastedSpell(rand, stack); //caster
  262. break;
  263. default:
  264. logGlobal->error("Incorrect mode of battleGetRandomSpell (%d)", static_cast<int>(mode));
  265. return SpellID::NONE;
  266. }
  267. }
  268. const CStack* CBattleInfoCallback::battleGetStackByPos(BattleHex pos, bool onlyAlive) const
  269. {
  270. RETURN_IF_NOT_BATTLE(nullptr);
  271. for(auto s : battleGetAllStacks(true))
  272. if(vstd::contains(s->getHexes(), pos) && (!onlyAlive || s->alive()))
  273. return s;
  274. return nullptr;
  275. }
  276. const battle::Unit * CBattleInfoCallback::battleGetUnitByPos(BattleHex pos, bool onlyAlive) const
  277. {
  278. RETURN_IF_NOT_BATTLE(nullptr);
  279. auto ret = battleGetUnitsIf([=](const battle::Unit * unit)
  280. {
  281. return !unit->isGhost()
  282. && vstd::contains(battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()), pos)
  283. && (!onlyAlive || unit->alive());
  284. });
  285. if(!ret.empty())
  286. return ret.front();
  287. else
  288. return nullptr;
  289. }
  290. battle::Units CBattleInfoCallback::battleAliveUnits() const
  291. {
  292. return battleGetUnitsIf([](const battle::Unit * unit)
  293. {
  294. return unit->isValidTarget(false);
  295. });
  296. }
  297. battle::Units CBattleInfoCallback::battleAliveUnits(ui8 side) const
  298. {
  299. return battleGetUnitsIf([=](const battle::Unit * unit)
  300. {
  301. return unit->isValidTarget(false) && unit->unitSide() == side;
  302. });
  303. }
  304. //T is battle::Unit descendant
  305. template <typename T>
  306. const T * takeOneUnit(std::vector<const T*> & all, const int turn, int8_t & lastMoved, int phase)
  307. {
  308. const T * returnedUnit = nullptr;
  309. size_t currentUnitIndex = 0;
  310. for(size_t i = 0; i < all.size(); i++)
  311. {
  312. int32_t currentUnitSpeed = -1;
  313. int32_t returnedUnitSpeed = -1;
  314. if(returnedUnit)
  315. returnedUnitSpeed = returnedUnit->getInitiative(turn);
  316. if(all[i])
  317. {
  318. currentUnitSpeed = all[i]->getInitiative(turn);
  319. switch(phase)
  320. {
  321. case 1: // Faster first, attacker priority, higher slot first
  322. if(returnedUnit == nullptr || currentUnitSpeed > returnedUnitSpeed)
  323. {
  324. returnedUnit = all[i];
  325. currentUnitIndex = i;
  326. }
  327. else if(currentUnitSpeed == returnedUnitSpeed)
  328. {
  329. if(lastMoved == -1 && turn <= 0 && all[i]->unitSide() == BattleSide::ATTACKER
  330. && !(returnedUnit->unitSide() == all[i]->unitSide() && returnedUnit->unitSlot() < all[i]->unitSlot())) // Turn 0 attacker priority
  331. {
  332. returnedUnit = all[i];
  333. currentUnitIndex = i;
  334. }
  335. else if(lastMoved != -1 && all[i]->unitSide() != lastMoved
  336. && !(returnedUnit->unitSide() == all[i]->unitSide() && returnedUnit->unitSlot() < all[i]->unitSlot())) // Alternate equal speeds units
  337. {
  338. returnedUnit = all[i];
  339. currentUnitIndex = i;
  340. }
  341. }
  342. break;
  343. case 2: // Slower first, higher slot first
  344. case 3:
  345. if(returnedUnit == nullptr || currentUnitSpeed < returnedUnitSpeed)
  346. {
  347. returnedUnit = all[i];
  348. currentUnitIndex = i;
  349. }
  350. else if(currentUnitSpeed == returnedUnitSpeed && lastMoved != -1 && all[i]->unitSide() != lastMoved
  351. && !(returnedUnit->unitSide() == all[i]->unitSide() && returnedUnit->unitSlot() < all[i]->unitSlot())) // Alternate equal speeds units
  352. {
  353. returnedUnit = all[i];
  354. currentUnitIndex = i;
  355. }
  356. break;
  357. default:
  358. break;
  359. }
  360. }
  361. }
  362. if(!returnedUnit)
  363. return nullptr;
  364. all[currentUnitIndex] = nullptr;
  365. return returnedUnit;
  366. }
  367. void CBattleInfoCallback::battleGetTurnOrder(std::vector<battle::Units> & out, const size_t maxUnits, const int maxTurns, const int turn, int8_t lastMoved) const
  368. {
  369. RETURN_IF_NOT_BATTLE();
  370. if(maxUnits == 0 && maxTurns == 0)
  371. {
  372. logGlobal->error("Attempt to get infinite battle queue");
  373. return;
  374. }
  375. auto actualTurn = turn > 0 ? turn : 0;
  376. auto outputFull = [&]() -> bool
  377. {
  378. if(maxUnits == 0)
  379. return false;//no limit
  380. size_t outSize = 0;
  381. for(const auto & oneTurn : out)
  382. outSize += oneTurn.size();
  383. return outSize >= maxUnits;
  384. };
  385. out.emplace_back();
  386. //We'll split creatures with remaining movement to 4 buckets
  387. // [0] - turrets/catapult,
  388. // [1] - normal (unmoved) creatures, other war machines,
  389. // [2] - waited cres that had morale,
  390. // [3] - rest of waited cres
  391. std::array<battle::Units, 4> phase;
  392. const battle::Unit * active = battleActiveUnit();
  393. if(active)
  394. {
  395. //its first turn and active unit hasn't taken any action yet - must be placed at the beginning of queue, no matter what
  396. if(turn == 0 && active->willMove() && !active->waited())
  397. {
  398. out.back().push_back(active);
  399. if(outputFull())
  400. return;
  401. }
  402. //its first or current turn, turn priority for active stack side
  403. //TODO: what if active stack mind-controlled?
  404. if(turn <= 0 && lastMoved < 0)
  405. lastMoved = active->unitSide();
  406. }
  407. auto all = battleGetUnitsIf([](const battle::Unit * unit)
  408. {
  409. return !unit->isGhost();
  410. });
  411. if(!vstd::contains_if(all, [](const battle::Unit * unit) { return unit->willMove(100000); })) //little evil, but 100000 should be enough for all effects to disappear
  412. {
  413. //No unit will be able to move, battle is over.
  414. out.clear();
  415. return;
  416. }
  417. for(auto one : all)
  418. {
  419. if((actualTurn == 0 && !one->willMove()) //we are considering current round and unit won't move
  420. || (actualTurn > 0 && !one->canMove(turn)) //unit won't be able to move in later rounds
  421. || (actualTurn == 0 && one == active && !out.at(0).empty() && one == out.front().front())) //it's active unit already added at the beginning of queue
  422. {
  423. continue;
  424. }
  425. int p = one->battleQueuePhase(turn);
  426. phase[p].push_back(one);
  427. }
  428. boost::sort(phase[0], CMP_stack(0, actualTurn, lastMoved));
  429. std::copy(phase[0].begin(), phase[0].end(), std::back_inserter(out.back()));
  430. if(outputFull())
  431. return;
  432. for(int i = 1; i < 4; i++)
  433. boost::sort(phase[i], CMP_stack(i, actualTurn, lastMoved));
  434. int pi = 1;
  435. while(!outputFull() && pi < 4)
  436. {
  437. const battle::Unit * current = nullptr;
  438. if(phase[pi].empty())
  439. pi++;
  440. else
  441. {
  442. current = takeOneUnit(phase[pi], actualTurn, lastMoved, pi);
  443. if(!current)
  444. {
  445. pi++;
  446. }
  447. else
  448. {
  449. out.back().push_back(current);
  450. lastMoved = current->unitSide();
  451. }
  452. }
  453. }
  454. if(lastMoved < 0)
  455. lastMoved = BattleSide::ATTACKER;
  456. if(!outputFull() && (maxTurns == 0 || out.size() < maxTurns))
  457. battleGetTurnOrder(out, maxUnits, maxTurns, actualTurn + 1, lastMoved);
  458. }
  459. std::vector<BattleHex> CBattleInfoCallback::battleGetAvailableHexes(const battle::Unit * unit) const
  460. {
  461. RETURN_IF_NOT_BATTLE(std::vector<BattleHex>());
  462. if(!unit->getPosition().isValid()) //turrets
  463. return std::vector<BattleHex>();
  464. auto reachability = getReachability(unit);
  465. return battleGetAvailableHexes(reachability, unit);
  466. }
  467. std::vector<BattleHex> CBattleInfoCallback::battleGetAvailableHexes(const ReachabilityInfo & cache, const battle::Unit * unit) const
  468. {
  469. std::vector<BattleHex> ret;
  470. RETURN_IF_NOT_BATTLE(ret);
  471. if(!unit->getPosition().isValid()) //turrets
  472. return ret;
  473. auto unitSpeed = unit->Speed(0, true);
  474. const bool tacticPhase = battleTacticDist() && battleGetTacticsSide() == unit->unitSide();
  475. for(int i = 0; i < GameConstants::BFIELD_SIZE; ++i)
  476. {
  477. // If obstacles or other stacks makes movement impossible, it can't be helped.
  478. if(!cache.isReachable(i))
  479. continue;
  480. if(tacticPhase)
  481. {
  482. //Stack has to perform tactic-phase movement -> can enter any reachable tile within given range
  483. if(!isInTacticRange(i))
  484. continue;
  485. }
  486. else
  487. {
  488. //Not tactics phase -> destination must be reachable and within unit range.
  489. if(cache.distances[i] > (int)unitSpeed)
  490. continue;
  491. }
  492. ret.push_back(i);
  493. }
  494. return ret;
  495. }
  496. std::vector<BattleHex> CBattleInfoCallback::battleGetAvailableHexes(const battle::Unit * unit, bool addOccupiable, std::vector<BattleHex> * attackable) const
  497. {
  498. std::vector<BattleHex> ret = battleGetAvailableHexes(unit);
  499. if(ret.empty())
  500. return ret;
  501. if(addOccupiable && unit->doubleWide())
  502. {
  503. std::vector<BattleHex> occupiable;
  504. for(auto hex : ret)
  505. occupiable.push_back(unit->occupiedHex(hex));
  506. vstd::concatenate(ret, occupiable);
  507. }
  508. if(attackable)
  509. {
  510. auto meleeAttackable = [&](BattleHex hex) -> bool
  511. {
  512. // Return true if given hex has at least one available neighbour.
  513. // Available hexes are already present in ret vector.
  514. auto availableNeighbor = boost::find_if(ret, [=] (BattleHex availableHex)
  515. {
  516. return BattleHex::mutualPosition(hex, availableHex) >= 0;
  517. });
  518. return availableNeighbor != ret.end();
  519. };
  520. for(auto otherSt : battleAliveUnits(otherSide(unit->unitSide())))
  521. {
  522. if(!otherSt->isValidTarget(false))
  523. continue;
  524. std::vector<BattleHex> occupied = otherSt->getHexes();
  525. if(battleCanShoot(unit, otherSt->getPosition()))
  526. {
  527. attackable->insert(attackable->end(), occupied.begin(), occupied.end());
  528. continue;
  529. }
  530. for(BattleHex he : occupied)
  531. {
  532. if(meleeAttackable(he))
  533. attackable->push_back(he);
  534. }
  535. }
  536. }
  537. //adding occupiable likely adds duplicates to ret -> clean it up
  538. boost::sort(ret);
  539. ret.erase(boost::unique(ret).end(), ret.end());
  540. return ret;
  541. }
  542. bool CBattleInfoCallback::battleCanAttack(const CStack * stack, const CStack * target, BattleHex dest) const
  543. {
  544. RETURN_IF_NOT_BATTLE(false);
  545. if(battleTacticDist())
  546. return false;
  547. if (!stack || !target)
  548. return false;
  549. if(!battleMatchOwner(stack, target))
  550. return false;
  551. auto &id = stack->getCreature()->idNumber;
  552. if (id == CreatureID::FIRST_AID_TENT || id == CreatureID::CATAPULT)
  553. return false;
  554. return target->alive();
  555. }
  556. bool CBattleInfoCallback::battleCanShoot(const battle::Unit * attacker) const
  557. {
  558. RETURN_IF_NOT_BATTLE(false);
  559. if(battleTacticDist()) //no shooting during tactics
  560. return false;
  561. if (!attacker)
  562. return false;
  563. if (attacker->creatureIndex() == CreatureID::CATAPULT) //catapult cannot attack creatures
  564. return false;
  565. //forgetfulness
  566. TConstBonusListPtr forgetfulList = attacker->getBonuses(Selector::type()(Bonus::FORGETFULL));
  567. if(!forgetfulList->empty())
  568. {
  569. int forgetful = forgetfulList->valOfBonuses(Selector::type()(Bonus::FORGETFULL));
  570. //advanced+ level
  571. if(forgetful > 1)
  572. return false;
  573. }
  574. return attacker->canShoot() && (!battleIsUnitBlocked(attacker)
  575. || attacker->hasBonusOfType(Bonus::FREE_SHOOTING));
  576. }
  577. bool CBattleInfoCallback::battleCanShoot(const battle::Unit * attacker, BattleHex dest) const
  578. {
  579. RETURN_IF_NOT_BATTLE(false);
  580. const battle::Unit * defender = battleGetUnitByPos(dest);
  581. if(!attacker || !defender)
  582. return false;
  583. if(battleMatchOwner(attacker, defender) && defender->alive())
  584. return battleCanShoot(attacker);
  585. return false;
  586. }
  587. TDmgRange CBattleInfoCallback::calculateDmgRange(const BattleAttackInfo & info) const
  588. {
  589. auto battleBonusValue = [&](const IBonusBearer * bearer, CSelector selector) -> int
  590. {
  591. auto noLimit = Selector::effectRange()(Bonus::NO_LIMIT);
  592. auto limitMatches = info.shooting
  593. ? Selector::effectRange()(Bonus::ONLY_DISTANCE_FIGHT)
  594. : Selector::effectRange()(Bonus::ONLY_MELEE_FIGHT);
  595. //any regular bonuses or just ones for melee/ranged
  596. return bearer->getBonuses(selector, noLimit.Or(limitMatches))->totalValue();
  597. };
  598. const IBonusBearer * attackerBonuses = info.attacker;
  599. const IBonusBearer * defenderBonuses = info.defender;
  600. double additiveBonus = 1.0 + info.additiveBonus;
  601. double multBonus = 1.0 * info.multBonus;
  602. double minDmg = 0.0;
  603. double maxDmg = 0.0;
  604. minDmg = info.attacker->getMinDamage(info.shooting);
  605. maxDmg = info.attacker->getMaxDamage(info.shooting);
  606. minDmg *= info.attacker->getCount(),
  607. maxDmg *= info.attacker->getCount();
  608. if(info.attacker->creatureIndex() == CreatureID::ARROW_TOWERS)
  609. {
  610. SiegeStuffThatShouldBeMovedToHandlers::retrieveTurretDamageRange(battleGetDefendedTown(), info.attacker, minDmg, maxDmg);
  611. TDmgRange unmodifiableTowerDamage = std::make_pair(int64_t(minDmg), int64_t(maxDmg));
  612. return unmodifiableTowerDamage;
  613. }
  614. const std::string cachingStrSiedgeWeapon = "type_SIEGE_WEAPON";
  615. static const auto selectorSiedgeWeapon = Selector::type()(Bonus::SIEGE_WEAPON);
  616. if(attackerBonuses->hasBonus(selectorSiedgeWeapon, cachingStrSiedgeWeapon) && info.attacker->creatureIndex() != CreatureID::ARROW_TOWERS) //any siege weapon, but only ballista can attack (second condition - not arrow turret)
  617. { //minDmg and maxDmg are multiplied by hero attack + 1
  618. auto retrieveHeroPrimSkill = [&](int skill) -> int
  619. {
  620. std::shared_ptr<const Bonus> b = attackerBonuses->getBonus(Selector::sourceTypeSel(Bonus::HERO_BASE_SKILL).And(Selector::typeSubtype(Bonus::PRIMARY_SKILL, skill)));
  621. return b ? b->val : 0; //if there is no hero or no info on his primary skill, return 0
  622. };
  623. minDmg *= retrieveHeroPrimSkill(PrimarySkill::ATTACK) + 1;
  624. maxDmg *= retrieveHeroPrimSkill(PrimarySkill::ATTACK) + 1;
  625. }
  626. double attackDefenceDifference = 0.0;
  627. double multAttackReduction = 1.0 - battleBonusValue(attackerBonuses, Selector::type()(Bonus::GENERAL_ATTACK_REDUCTION)) / 100.0;
  628. attackDefenceDifference += info.attacker->getAttack(info.shooting) * multAttackReduction;
  629. double multDefenceReduction = 1.0 - battleBonusValue(attackerBonuses, Selector::type()(Bonus::ENEMY_DEFENCE_REDUCTION)) / 100.0;
  630. attackDefenceDifference -= info.defender->getDefense(info.shooting) * multDefenceReduction;
  631. const std::string cachingStrSlayer = "type_SLAYER";
  632. static const auto selectorSlayer = Selector::type()(Bonus::SLAYER);
  633. //slayer handling //TODO: apply only ONLY_MELEE_FIGHT / DISTANCE_FIGHT?
  634. auto slayerEffects = attackerBonuses->getBonuses(selectorSlayer, cachingStrSlayer);
  635. if(std::shared_ptr<const Bonus> slayerEffect = slayerEffects->getFirst(Selector::all))
  636. {
  637. std::vector<int32_t> affectedIds;
  638. const auto spLevel = slayerEffect->val;
  639. const CCreature * defenderType = info.defender->unitType();
  640. bool isAffected = false;
  641. for(const auto & b : defenderType->getBonusList())
  642. {
  643. if((b->type == Bonus::KING3 && spLevel >= 3) || //expert
  644. (b->type == Bonus::KING2 && spLevel >= 2) || //adv +
  645. (b->type == Bonus::KING1 && spLevel >= 0)) //none or basic +
  646. {
  647. isAffected = true;
  648. break;
  649. }
  650. }
  651. if(isAffected)
  652. {
  653. attackDefenceDifference += SpellID(SpellID::SLAYER).toSpell()->getLevelPower(spLevel);
  654. if(info.attacker->hasBonusOfType(Bonus::SPECIAL_PECULIAR_ENCHANT, SpellID::SLAYER))
  655. {
  656. ui8 attackerTier = info.attacker->unitType()->level;
  657. ui8 specialtyBonus = std::max(5 - attackerTier, 0);
  658. attackDefenceDifference += specialtyBonus;
  659. }
  660. }
  661. }
  662. //bonus from attack/defense skills
  663. if(attackDefenceDifference < 0) //decreasing dmg
  664. {
  665. double defenseMultiplier = VLC->modh->settings.DEFENSE_POINT_DMG_MULTIPLIER;
  666. double defenseMultiplierCap = VLC->modh->settings.DEFENSE_POINTS_DMG_MULTIPLIER_CAP;
  667. const double dec = std::min(defenseMultiplier * (-attackDefenceDifference), defenseMultiplierCap);
  668. multBonus *= 1.0 - dec;
  669. }
  670. else //increasing dmg
  671. {
  672. double attackMultiplier = VLC->modh->settings.ATTACK_POINT_DMG_MULTIPLIER;
  673. double attackMultiplierCap = VLC->modh->settings.ATTACK_POINTS_DMG_MULTIPLIER_CAP;
  674. const double inc = std::min(attackMultiplier * attackDefenceDifference, attackMultiplierCap);
  675. additiveBonus += inc;
  676. }
  677. const std::string cachingStrJousting = "type_JOUSTING";
  678. static const auto selectorJousting = Selector::type()(Bonus::JOUSTING);
  679. const std::string cachingStrChargeImmunity = "type_CHARGE_IMMUNITY";
  680. static const auto selectorChargeImmunity = Selector::type()(Bonus::CHARGE_IMMUNITY);
  681. //applying jousting bonus
  682. if(info.chargedFields > 0 && attackerBonuses->hasBonus(selectorJousting, cachingStrJousting) && !defenderBonuses->hasBonus(selectorChargeImmunity, cachingStrChargeImmunity))
  683. additiveBonus += info.chargedFields * 0.05;
  684. //handling secondary abilities and artifacts giving premies to them
  685. const std::string cachingStrArchery = "type_SECONDARY_SKILL_PREMYs_ARCHERY";
  686. static const auto selectorArchery = Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::ARCHERY);
  687. const std::string cachingStrOffence = "type_SECONDARY_SKILL_PREMYs_OFFENCE";
  688. static const auto selectorOffence = Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::OFFENCE);
  689. const std::string cachingStrArmorer = "type_SECONDARY_SKILL_PREMYs_ARMORER";
  690. static const auto selectorArmorer = Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::ARMORER);
  691. if(info.shooting)
  692. additiveBonus += attackerBonuses->valOfBonuses(selectorArchery, cachingStrArchery) / 100.0;
  693. else
  694. additiveBonus += attackerBonuses->valOfBonuses(selectorOffence, cachingStrOffence) / 100.0;
  695. multBonus *= (std::max(0, 100 - defenderBonuses->valOfBonuses(selectorArmorer, cachingStrArmorer))) / 100.0;
  696. //handling hate effect
  697. //assume that unit have only few HATE features and cache them all
  698. const std::string cachingStrHate = "type_HATE";
  699. static const auto selectorHate = Selector::type()(Bonus::HATE);
  700. auto allHateEffects = attackerBonuses->getBonuses(selectorHate, cachingStrHate);
  701. additiveBonus += allHateEffects->valOfBonuses(Selector::subtype()(info.defender->creatureIndex())) / 100.0;
  702. const std::string cachingStrMeleeReduction = "type_GENERAL_DAMAGE_REDUCTIONs_0";
  703. static const auto selectorMeleeReduction = Selector::typeSubtype(Bonus::GENERAL_DAMAGE_REDUCTION, 0);
  704. const std::string cachingStrRangedReduction = "type_GENERAL_DAMAGE_REDUCTIONs_1";
  705. static const auto selectorRangedReduction = Selector::typeSubtype(Bonus::GENERAL_DAMAGE_REDUCTION, 1);
  706. //handling spell effects
  707. if(!info.shooting) //eg. shield
  708. {
  709. multBonus *= (100 - defenderBonuses->valOfBonuses(selectorMeleeReduction, cachingStrMeleeReduction)) / 100.0;
  710. }
  711. else //eg. air shield
  712. {
  713. multBonus *= (100 - defenderBonuses->valOfBonuses(selectorRangedReduction, cachingStrRangedReduction)) / 100.0;
  714. }
  715. if(info.shooting)
  716. {
  717. //todo: set actual percentage in spell bonus configuration instead of just level; requires non trivial backward compatibility handling
  718. //get list first, total value of 0 also counts
  719. TConstBonusListPtr forgetfulList = attackerBonuses->getBonuses(Selector::type()(Bonus::FORGETFULL),"type_FORGETFULL");
  720. if(!forgetfulList->empty())
  721. {
  722. int forgetful = forgetfulList->valOfBonuses(Selector::all);
  723. //none of basic level
  724. if(forgetful == 0 || forgetful == 1)
  725. multBonus *= 0.5;
  726. else
  727. logGlobal->warn("Attempt to calculate shooting damage with adv+ FORGETFULL effect");
  728. }
  729. }
  730. const std::string cachingStrForcedMinDamage = "type_ALWAYS_MINIMUM_DAMAGE";
  731. static const auto selectorForcedMinDamage = Selector::type()(Bonus::ALWAYS_MINIMUM_DAMAGE);
  732. const std::string cachingStrForcedMaxDamage = "type_ALWAYS_MAXIMUM_DAMAGE";
  733. static const auto selectorForcedMaxDamage = Selector::type()(Bonus::ALWAYS_MAXIMUM_DAMAGE);
  734. TConstBonusListPtr curseEffects = attackerBonuses->getBonuses(selectorForcedMinDamage, cachingStrForcedMinDamage);
  735. TConstBonusListPtr blessEffects = attackerBonuses->getBonuses(selectorForcedMaxDamage, cachingStrForcedMaxDamage);
  736. int curseBlessAdditiveModifier = blessEffects->totalValue() - curseEffects->totalValue();
  737. double curseMultiplicativePenalty = curseEffects->size() ? (*std::max_element(curseEffects->begin(), curseEffects->end(), &Bonus::compareByAdditionalInfo<std::shared_ptr<Bonus>>))->additionalInfo[0] : 0;
  738. if(curseMultiplicativePenalty) //curse handling (partial, the rest is below)
  739. {
  740. multBonus *= 1.0 - curseMultiplicativePenalty/100;
  741. }
  742. const std::string cachingStrAdvAirShield = "isAdvancedAirShield";
  743. auto isAdvancedAirShield = [](const Bonus* bonus)
  744. {
  745. return bonus->source == Bonus::SPELL_EFFECT
  746. && bonus->sid == SpellID::AIR_SHIELD
  747. && bonus->val >= SecSkillLevel::ADVANCED;
  748. };
  749. if(info.shooting)
  750. {
  751. //wall / distance penalty + advanced air shield
  752. BattleHex attackerPos = info.attackerPos.isValid() ? info.attackerPos : info.attacker->getPosition();
  753. BattleHex defenderPos = info.defenderPos.isValid() ? info.defenderPos : info.defender->getPosition();
  754. const bool distPenalty = battleHasDistancePenalty(attackerBonuses, attackerPos, defenderPos);
  755. const bool obstaclePenalty = battleHasWallPenalty(attackerBonuses, attackerPos, defenderPos);
  756. if(distPenalty || defenderBonuses->hasBonus(isAdvancedAirShield, cachingStrAdvAirShield))
  757. multBonus *= 0.5;
  758. if(obstaclePenalty)
  759. multBonus *= 0.5; //cumulative
  760. }
  761. else
  762. {
  763. const std::string cachingStrNoMeleePenalty = "type_NO_MELEE_PENALTY";
  764. static const auto selectorNoMeleePenalty = Selector::type()(Bonus::NO_MELEE_PENALTY);
  765. if(info.attacker->isShooter() && !attackerBonuses->hasBonus(selectorNoMeleePenalty, cachingStrNoMeleePenalty))
  766. multBonus *= 0.5;
  767. }
  768. // psychic elementals versus mind immune units 50%
  769. if(info.attacker->creatureIndex() == CreatureID::PSYCHIC_ELEMENTAL)
  770. {
  771. const std::string cachingStrMindImmunity = "type_MIND_IMMUNITY";
  772. static const auto selectorMindImmunity = Selector::type()(Bonus::MIND_IMMUNITY);
  773. if(defenderBonuses->hasBonus(selectorMindImmunity, cachingStrMindImmunity))
  774. multBonus *= 0.5;
  775. }
  776. // TODO attack on petrified unit 50%
  777. // blinded unit retaliates
  778. minDmg *= additiveBonus * multBonus;
  779. maxDmg *= additiveBonus * multBonus;
  780. if(curseEffects->size()) //curse handling (rest)
  781. {
  782. minDmg += curseBlessAdditiveModifier;
  783. maxDmg = minDmg;
  784. }
  785. else if(blessEffects->size()) //bless handling
  786. {
  787. maxDmg += curseBlessAdditiveModifier;
  788. minDmg = maxDmg;
  789. }
  790. TDmgRange returnedVal = std::make_pair(int64_t(minDmg), int64_t(maxDmg));
  791. //damage cannot be less than 1
  792. vstd::amax(returnedVal.first, 1);
  793. vstd::amax(returnedVal.second, 1);
  794. return returnedVal;
  795. }
  796. TDmgRange CBattleInfoCallback::battleEstimateDamage(const CStack * attacker, const CStack * defender, TDmgRange * retaliationDmg) const
  797. {
  798. RETURN_IF_NOT_BATTLE(std::make_pair(0, 0));
  799. const bool shooting = battleCanShoot(attacker, defender->getPosition());
  800. const BattleAttackInfo bai(attacker, defender, shooting);
  801. return battleEstimateDamage(bai, retaliationDmg);
  802. }
  803. TDmgRange CBattleInfoCallback::battleEstimateDamage(const BattleAttackInfo & bai, TDmgRange * retaliationDmg) const
  804. {
  805. RETURN_IF_NOT_BATTLE(std::make_pair(0, 0));
  806. TDmgRange ret = calculateDmgRange(bai);
  807. if(retaliationDmg)
  808. {
  809. if(bai.shooting)
  810. {
  811. //FIXME: handle RANGED_RETALIATION
  812. retaliationDmg->first = retaliationDmg->second = 0;
  813. }
  814. else
  815. {
  816. //TODO: rewrite using boost::numeric::interval
  817. //TODO: rewire once more using interval-based fuzzy arithmetic
  818. int64_t TDmgRange::* pairElems[] = {&TDmgRange::first, &TDmgRange::second};
  819. for (int i=0; i<2; ++i)
  820. {
  821. auto retaliationAttack = bai.reverse();
  822. int64_t dmg = ret.*pairElems[i];
  823. auto state = retaliationAttack.attacker->acquireState();
  824. state->damage(dmg);
  825. retaliationAttack.attacker = state.get();
  826. retaliationDmg->*pairElems[!i] = calculateDmgRange(retaliationAttack).*pairElems[!i];
  827. }
  828. }
  829. }
  830. return ret;
  831. }
  832. std::vector<std::shared_ptr<const CObstacleInstance>> CBattleInfoCallback::battleGetAllObstaclesOnPos(BattleHex tile, bool onlyBlocking) const
  833. {
  834. std::vector<std::shared_ptr<const CObstacleInstance>> obstacles = std::vector<std::shared_ptr<const CObstacleInstance>>();
  835. RETURN_IF_NOT_BATTLE(obstacles);
  836. for(auto & obs : battleGetAllObstacles())
  837. {
  838. if(vstd::contains(obs->getBlockedTiles(), tile)
  839. || (!onlyBlocking && vstd::contains(obs->getAffectedTiles(), tile)))
  840. {
  841. obstacles.push_back(obs);
  842. }
  843. }
  844. return obstacles;
  845. }
  846. std::vector<std::shared_ptr<const CObstacleInstance>> CBattleInfoCallback::getAllAffectedObstaclesByStack(const battle::Unit * unit) const
  847. {
  848. std::vector<std::shared_ptr<const CObstacleInstance>> affectedObstacles = std::vector<std::shared_ptr<const CObstacleInstance>>();
  849. RETURN_IF_NOT_BATTLE(affectedObstacles);
  850. if(unit->alive())
  851. {
  852. affectedObstacles = battleGetAllObstaclesOnPos(unit->getPosition(), false);
  853. if(unit->doubleWide())
  854. {
  855. BattleHex otherHex = unit->occupiedHex(unit->getPosition());
  856. if(otherHex.isValid())
  857. for(auto & i : battleGetAllObstaclesOnPos(otherHex, false))
  858. affectedObstacles.push_back(i);
  859. }
  860. for(auto hex : unit->getHexes())
  861. if(hex == ESiegeHex::GATE_BRIDGE)
  862. if(battleGetGateState() == EGateState::OPENED || battleGetGateState() == EGateState::DESTROYED)
  863. for(int i=0; i<affectedObstacles.size(); i++)
  864. if(affectedObstacles.at(i)->obstacleType == CObstacleInstance::MOAT)
  865. affectedObstacles.erase(affectedObstacles.begin()+i);
  866. }
  867. return affectedObstacles;
  868. }
  869. AccessibilityInfo CBattleInfoCallback::getAccesibility() const
  870. {
  871. AccessibilityInfo ret;
  872. ret.fill(EAccessibility::ACCESSIBLE);
  873. //removing accessibility for side columns of hexes
  874. for(int y = 0; y < GameConstants::BFIELD_HEIGHT; y++)
  875. {
  876. ret[BattleHex(GameConstants::BFIELD_WIDTH - 1, y)] = EAccessibility::SIDE_COLUMN;
  877. ret[BattleHex(0, y)] = EAccessibility::SIDE_COLUMN;
  878. }
  879. //special battlefields with logically unavailable tiles
  880. auto bFieldType = battleGetBattlefieldType();
  881. if(bFieldType != BattleField::NONE)
  882. {
  883. std::vector<BattleHex> impassableHexes = bFieldType.getInfo()->impassableHexes;
  884. for(auto hex : impassableHexes)
  885. ret[hex] = EAccessibility::UNAVAILABLE;
  886. }
  887. //gate -> should be before stacks
  888. if(battleGetSiegeLevel() > 0)
  889. {
  890. EAccessibility accessability = EAccessibility::ACCESSIBLE;
  891. switch(battleGetGateState())
  892. {
  893. case EGateState::CLOSED:
  894. accessability = EAccessibility::GATE;
  895. break;
  896. case EGateState::BLOCKED:
  897. accessability = EAccessibility::UNAVAILABLE;
  898. break;
  899. }
  900. ret[ESiegeHex::GATE_OUTER] = ret[ESiegeHex::GATE_INNER] = accessability;
  901. }
  902. //tiles occupied by standing stacks
  903. for(auto unit : battleAliveUnits())
  904. {
  905. for(auto hex : unit->getHexes())
  906. if(hex.isAvailable()) //towers can have <0 pos; we don't also want to overwrite side columns
  907. ret[hex] = EAccessibility::ALIVE_STACK;
  908. }
  909. //obstacles
  910. for(const auto &obst : battleGetAllObstacles())
  911. {
  912. for(auto hex : obst->getBlockedTiles())
  913. ret[hex] = EAccessibility::OBSTACLE;
  914. }
  915. //walls
  916. if(battleGetSiegeLevel() > 0)
  917. {
  918. static const int permanentlyLocked[] = {12, 45, 62, 112, 147, 165};
  919. for(auto hex : permanentlyLocked)
  920. ret[hex] = EAccessibility::UNAVAILABLE;
  921. //TODO likely duplicated logic
  922. static const std::pair<int, BattleHex> lockedIfNotDestroyed[] =
  923. {
  924. //which part of wall, which hex is blocked if this part of wall is not destroyed
  925. std::make_pair(2, BattleHex(ESiegeHex::DESTRUCTIBLE_WALL_4)),
  926. std::make_pair(3, BattleHex(ESiegeHex::DESTRUCTIBLE_WALL_3)),
  927. std::make_pair(4, BattleHex(ESiegeHex::DESTRUCTIBLE_WALL_2)),
  928. std::make_pair(5, BattleHex(ESiegeHex::DESTRUCTIBLE_WALL_1))
  929. };
  930. for(auto & elem : lockedIfNotDestroyed)
  931. {
  932. if(battleGetWallState(elem.first) != EWallState::DESTROYED)
  933. ret[elem.second] = EAccessibility::DESTRUCTIBLE_WALL;
  934. }
  935. }
  936. return ret;
  937. }
  938. AccessibilityInfo CBattleInfoCallback::getAccesibility(const battle::Unit * stack) const
  939. {
  940. return getAccesibility(battle::Unit::getHexes(stack->getPosition(), stack->doubleWide(), stack->unitSide()));
  941. }
  942. AccessibilityInfo CBattleInfoCallback::getAccesibility(const std::vector<BattleHex> & accessibleHexes) const
  943. {
  944. auto ret = getAccesibility();
  945. for(auto hex : accessibleHexes)
  946. if(hex.isValid())
  947. ret[hex] = EAccessibility::ACCESSIBLE;
  948. return ret;
  949. }
  950. ReachabilityInfo CBattleInfoCallback::makeBFS(const AccessibilityInfo &accessibility, const ReachabilityInfo::Parameters & params) const
  951. {
  952. ReachabilityInfo ret;
  953. ret.accessibility = accessibility;
  954. ret.params = params;
  955. ret.predecessors.fill(BattleHex::INVALID);
  956. ret.distances.fill(ReachabilityInfo::INFINITE_DIST);
  957. if(!params.startPosition.isValid()) //if got call for arrow turrets
  958. return ret;
  959. const std::set<BattleHex> obstacles = getStoppers(params.perspective);
  960. std::queue<BattleHex> hexq; //bfs queue
  961. //first element
  962. hexq.push(params.startPosition);
  963. ret.distances[params.startPosition] = 0;
  964. std::array<bool, GameConstants::BFIELD_SIZE> accessibleCache;
  965. for(int hex = 0; hex < GameConstants::BFIELD_SIZE; hex++)
  966. accessibleCache[hex] = accessibility.accessible(hex, params.doubleWide, params.side);
  967. while(!hexq.empty()) //bfs loop
  968. {
  969. const BattleHex curHex = hexq.front();
  970. hexq.pop();
  971. //walking stack can't step past the obstacles
  972. if(curHex != params.startPosition && isInObstacle(curHex, obstacles, params))
  973. continue;
  974. const int costToNeighbour = ret.distances[curHex.hex] + 1;
  975. for(BattleHex neighbour : BattleHex::neighbouringTilesCache[curHex.hex])
  976. {
  977. if(neighbour.isValid())
  978. {
  979. const int costFoundSoFar = ret.distances[neighbour.hex];
  980. if(accessibleCache[neighbour.hex] && costToNeighbour < costFoundSoFar)
  981. {
  982. hexq.push(neighbour);
  983. ret.distances[neighbour.hex] = costToNeighbour;
  984. ret.predecessors[neighbour.hex] = curHex;
  985. }
  986. }
  987. }
  988. }
  989. return ret;
  990. }
  991. bool CBattleInfoCallback::isInObstacle(
  992. BattleHex hex,
  993. const std::set<BattleHex> & obstacles,
  994. const ReachabilityInfo::Parameters & params) const
  995. {
  996. auto occupiedHexes = battle::Unit::getHexes(hex, params.doubleWide, params.side);
  997. for(auto occupiedHex : occupiedHexes)
  998. {
  999. if(vstd::contains(obstacles, occupiedHex))
  1000. {
  1001. if(occupiedHex == ESiegeHex::GATE_BRIDGE)
  1002. {
  1003. if(battleGetGateState() != EGateState::DESTROYED && params.side == BattleSide::ATTACKER)
  1004. return true;
  1005. }
  1006. else
  1007. return true;
  1008. }
  1009. }
  1010. return false;
  1011. }
  1012. std::set<BattleHex> CBattleInfoCallback::getStoppers(BattlePerspective::BattlePerspective whichSidePerspective) const
  1013. {
  1014. std::set<BattleHex> ret;
  1015. RETURN_IF_NOT_BATTLE(ret);
  1016. for(auto &oi : battleGetAllObstacles(whichSidePerspective))
  1017. {
  1018. if(battleIsObstacleVisibleForSide(*oi, whichSidePerspective))
  1019. {
  1020. range::copy(oi->getStoppingTile(), vstd::set_inserter(ret));
  1021. }
  1022. }
  1023. return ret;
  1024. }
  1025. std::pair<const battle::Unit *, BattleHex> CBattleInfoCallback::getNearestStack(const battle::Unit * closest) const
  1026. {
  1027. auto reachability = getReachability(closest);
  1028. auto avHexes = battleGetAvailableHexes(reachability, closest);
  1029. // I hate std::pairs with their undescriptive member names first / second
  1030. struct DistStack
  1031. {
  1032. uint32_t distanceToPred;
  1033. BattleHex destination;
  1034. const battle::Unit * stack;
  1035. };
  1036. std::vector<DistStack> stackPairs;
  1037. std::vector<const battle::Unit *> possible = battleGetUnitsIf([=](const battle::Unit * unit)
  1038. {
  1039. return unit->isValidTarget(false) && unit != closest;
  1040. });
  1041. for(const battle::Unit * st : possible)
  1042. {
  1043. for(BattleHex hex : avHexes)
  1044. if(CStack::isMeleeAttackPossible(closest, st, hex))
  1045. {
  1046. DistStack hlp = {reachability.distances[hex], hex, st};
  1047. stackPairs.push_back(hlp);
  1048. }
  1049. }
  1050. if (stackPairs.size())
  1051. {
  1052. auto comparator = [](DistStack lhs, DistStack rhs) { return lhs.distanceToPred < rhs.distanceToPred; };
  1053. auto minimal = boost::min_element(stackPairs, comparator);
  1054. return std::make_pair(minimal->stack, minimal->destination);
  1055. }
  1056. else
  1057. return std::make_pair<const battle::Unit * , BattleHex>(nullptr, BattleHex::INVALID);
  1058. }
  1059. BattleHex CBattleInfoCallback::getAvaliableHex(CreatureID creID, ui8 side, int initialPos) const
  1060. {
  1061. bool twoHex = VLC->creh->objects[creID]->isDoubleWide();
  1062. int pos;
  1063. if (initialPos > -1)
  1064. pos = initialPos;
  1065. else //summon elementals depending on player side
  1066. {
  1067. if(side == BattleSide::ATTACKER)
  1068. pos = 0; //top left
  1069. else
  1070. pos = GameConstants::BFIELD_WIDTH - 1; //top right
  1071. }
  1072. auto accessibility = getAccesibility();
  1073. std::set<BattleHex> occupyable;
  1074. for(int i = 0; i < accessibility.size(); i++)
  1075. if(accessibility.accessible(i, twoHex, side))
  1076. occupyable.insert(i);
  1077. if(occupyable.empty())
  1078. {
  1079. return BattleHex::INVALID; //all tiles are covered
  1080. }
  1081. return BattleHex::getClosestTile(side, pos, occupyable);
  1082. }
  1083. si8 CBattleInfoCallback::battleGetTacticDist() const
  1084. {
  1085. RETURN_IF_NOT_BATTLE(0);
  1086. //TODO get rid of this method
  1087. if(battleDoWeKnowAbout(battleGetTacticsSide()))
  1088. return battleTacticDist();
  1089. return 0;
  1090. }
  1091. bool CBattleInfoCallback::isInTacticRange(BattleHex dest) const
  1092. {
  1093. RETURN_IF_NOT_BATTLE(false);
  1094. auto side = battleGetTacticsSide();
  1095. auto dist = battleGetTacticDist();
  1096. return ((!side && dest.getX() > 0 && dest.getX() <= dist)
  1097. || (side && dest.getX() < GameConstants::BFIELD_WIDTH - 1 && dest.getX() >= GameConstants::BFIELD_WIDTH - dist - 1));
  1098. }
  1099. ReachabilityInfo CBattleInfoCallback::getReachability(const battle::Unit * unit) const
  1100. {
  1101. ReachabilityInfo::Parameters params(unit, unit->getPosition());
  1102. if(!battleDoWeKnowAbout(unit->unitSide()))
  1103. {
  1104. //Stack is held by enemy, we can't use his perspective to check for reachability.
  1105. // Happens ie. when hovering enemy stack for its range. The arg could be set properly, but it's easier to fix it here.
  1106. params.perspective = battleGetMySide();
  1107. }
  1108. return getReachability(params);
  1109. }
  1110. ReachabilityInfo CBattleInfoCallback::getReachability(const ReachabilityInfo::Parameters &params) const
  1111. {
  1112. if(params.flying)
  1113. return getFlyingReachability(params);
  1114. else
  1115. return makeBFS(getAccesibility(params.knownAccessible), params);
  1116. }
  1117. ReachabilityInfo CBattleInfoCallback::getFlyingReachability(const ReachabilityInfo::Parameters &params) const
  1118. {
  1119. ReachabilityInfo ret;
  1120. ret.accessibility = getAccesibility(params.knownAccessible);
  1121. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  1122. {
  1123. if(ret.accessibility.accessible(i, params.doubleWide, params.side))
  1124. {
  1125. ret.predecessors[i] = params.startPosition;
  1126. ret.distances[i] = BattleHex::getDistance(params.startPosition, i);
  1127. }
  1128. }
  1129. return ret;
  1130. }
  1131. AttackableTiles CBattleInfoCallback::getPotentiallyAttackableHexes (const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const
  1132. {
  1133. //does not return hex attacked directly
  1134. //TODO: apply rotation to two-hex attackers
  1135. bool isAttacker = attacker->unitSide() == BattleSide::ATTACKER;
  1136. AttackableTiles at;
  1137. RETURN_IF_NOT_BATTLE(at);
  1138. const int WN = GameConstants::BFIELD_WIDTH;
  1139. BattleHex hex = (attackerPos != BattleHex::INVALID) ? attackerPos : attacker->getPosition(); //real or hypothetical (cursor) position
  1140. //FIXME: dragons or cerbers can rotate before attack, making their base hex different (#1124)
  1141. bool reverse = isToReverse(hex, destinationTile, isAttacker, attacker->doubleWide(), isAttacker);
  1142. if(reverse && attacker->doubleWide())
  1143. {
  1144. hex = attacker->occupiedHex(hex); //the other hex stack stands on
  1145. }
  1146. if(attacker->hasBonusOfType(Bonus::ATTACKS_ALL_ADJACENT))
  1147. {
  1148. boost::copy(attacker->getSurroundingHexes(attackerPos), vstd::set_inserter(at.hostileCreaturePositions));
  1149. }
  1150. if(attacker->hasBonusOfType(Bonus::THREE_HEADED_ATTACK))
  1151. {
  1152. std::vector<BattleHex> hexes = attacker->getSurroundingHexes(attackerPos);
  1153. for(BattleHex tile : hexes)
  1154. {
  1155. if((BattleHex::mutualPosition(tile, destinationTile) > -1 && BattleHex::mutualPosition(tile, hex) > -1)) //adjacent both to attacker's head and attacked tile
  1156. {
  1157. auto st = battleGetUnitByPos(tile, true);
  1158. if(st && battleMatchOwner(st, attacker)) //only hostile stacks - does it work well with Berserk?
  1159. at.hostileCreaturePositions.insert(tile);
  1160. }
  1161. }
  1162. }
  1163. if(attacker->hasBonusOfType(Bonus::WIDE_BREATH))
  1164. {
  1165. std::vector<BattleHex> hexes = destinationTile.neighbouringTiles();
  1166. for(int i = 0; i<hexes.size(); i++)
  1167. {
  1168. if(hexes.at(i) == hex)
  1169. {
  1170. hexes.erase(hexes.begin() + i);
  1171. i = 0;
  1172. }
  1173. }
  1174. for(BattleHex tile : hexes)
  1175. {
  1176. //friendly stacks can also be damaged by Dragon Breath
  1177. auto st = battleGetUnitByPos(tile, true);
  1178. if(st && st != attacker)
  1179. at.friendlyCreaturePositions.insert(tile);
  1180. }
  1181. }
  1182. else if(attacker->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH))
  1183. {
  1184. int pos = BattleHex::mutualPosition(destinationTile, hex);
  1185. if(pos > -1) //only adjacent hexes are subject of dragon breath calculation
  1186. {
  1187. std::vector<BattleHex> hexes; //only one, in fact
  1188. int pseudoVector = destinationTile.hex - hex;
  1189. switch(pseudoVector)
  1190. {
  1191. case 1:
  1192. case -1:
  1193. BattleHex::checkAndPush(destinationTile.hex + pseudoVector, hexes);
  1194. break;
  1195. case WN: //17 //left-down or right-down
  1196. case -WN: //-17 //left-up or right-up
  1197. case WN + 1: //18 //right-down
  1198. case -WN + 1: //-16 //right-up
  1199. BattleHex::checkAndPush(destinationTile.hex + pseudoVector + (((hex / WN) % 2) ? 1 : -1), hexes);
  1200. break;
  1201. case WN - 1: //16 //left-down
  1202. case -WN - 1: //-18 //left-up
  1203. BattleHex::checkAndPush(destinationTile.hex + pseudoVector + (((hex / WN) % 2) ? 1 : 0), hexes);
  1204. break;
  1205. }
  1206. for(BattleHex tile : hexes)
  1207. {
  1208. //friendly stacks can also be damaged by Dragon Breath
  1209. auto st = battleGetUnitByPos(tile, true);
  1210. if(st != nullptr)
  1211. at.friendlyCreaturePositions.insert(tile);
  1212. }
  1213. }
  1214. }
  1215. return at;
  1216. }
  1217. AttackableTiles CBattleInfoCallback::getPotentiallyShootableHexes(const battle::Unit * attacker, BattleHex destinationTile, BattleHex attackerPos) const
  1218. {
  1219. //does not return hex attacked directly
  1220. AttackableTiles at;
  1221. RETURN_IF_NOT_BATTLE(at);
  1222. if(attacker->hasBonusOfType(Bonus::SHOOTS_ALL_ADJACENT) && !vstd::contains(attackerPos.neighbouringTiles(), destinationTile))
  1223. {
  1224. std::vector<BattleHex> targetHexes = destinationTile.neighbouringTiles();
  1225. targetHexes.push_back(destinationTile);
  1226. boost::copy(targetHexes, vstd::set_inserter(at.hostileCreaturePositions));
  1227. }
  1228. return at;
  1229. }
  1230. std::vector<const battle::Unit*> CBattleInfoCallback::getAttackedBattleUnits(const battle::Unit* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos) const
  1231. {
  1232. std::vector<const battle::Unit*> units;
  1233. RETURN_IF_NOT_BATTLE(units);
  1234. AttackableTiles at;
  1235. if (rangedAttack)
  1236. at = getPotentiallyShootableHexes(attacker, destinationTile, attackerPos);
  1237. else
  1238. at = getPotentiallyAttackableHexes(attacker, destinationTile, attackerPos);
  1239. units = battleGetUnitsIf([=](const battle::Unit * unit)
  1240. {
  1241. if (unit->isGhost() || !unit->alive())
  1242. return false;
  1243. for (BattleHex hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  1244. {
  1245. if (vstd::contains(at.hostileCreaturePositions, hex))
  1246. return true;
  1247. if (vstd::contains(at.friendlyCreaturePositions, hex))
  1248. return true;
  1249. }
  1250. return false;
  1251. });
  1252. return units;
  1253. }
  1254. std::set<const CStack*> CBattleInfoCallback::getAttackedCreatures(const CStack* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos) const
  1255. {
  1256. std::set<const CStack*> attackedCres;
  1257. RETURN_IF_NOT_BATTLE(attackedCres);
  1258. AttackableTiles at;
  1259. if(rangedAttack)
  1260. at = getPotentiallyShootableHexes(attacker, destinationTile, attackerPos);
  1261. else
  1262. at = getPotentiallyAttackableHexes(attacker, destinationTile, attackerPos);
  1263. for (BattleHex tile : at.hostileCreaturePositions) //all around & three-headed attack
  1264. {
  1265. const CStack * st = battleGetStackByPos(tile, true);
  1266. if(st && st->owner != attacker->owner) //only hostile stacks - does it work well with Berserk?
  1267. {
  1268. attackedCres.insert(st);
  1269. }
  1270. }
  1271. for (BattleHex tile : at.friendlyCreaturePositions)
  1272. {
  1273. const CStack * st = battleGetStackByPos(tile, true);
  1274. if(st) //friendly stacks can also be damaged by Dragon Breath
  1275. {
  1276. attackedCres.insert(st);
  1277. }
  1278. }
  1279. return attackedCres;
  1280. }
  1281. //TODO: this should apply also to mechanics and cursor interface
  1282. bool CBattleInfoCallback::isToReverseHlp (BattleHex hexFrom, BattleHex hexTo, bool curDir) const
  1283. {
  1284. int fromX = hexFrom.getX();
  1285. int fromY = hexFrom.getY();
  1286. int toX = hexTo.getX();
  1287. int toY = hexTo.getY();
  1288. if (curDir) // attacker, facing right
  1289. {
  1290. if (fromX < toX)
  1291. return false;
  1292. if (fromX > toX)
  1293. return true;
  1294. if (fromY % 2 == 0 && toY % 2 == 1)
  1295. return true;
  1296. return false;
  1297. }
  1298. else // defender, facing left
  1299. {
  1300. if(fromX < toX)
  1301. return true;
  1302. if(fromX > toX)
  1303. return false;
  1304. if (fromY % 2 == 1 && toY % 2 == 0)
  1305. return true;
  1306. return false;
  1307. }
  1308. }
  1309. //TODO: this should apply also to mechanics and cursor interface
  1310. bool CBattleInfoCallback::isToReverse (BattleHex hexFrom, BattleHex hexTo, bool curDir, bool toDoubleWide, bool toDir) const
  1311. {
  1312. if (hexTo < 0 || hexFrom < 0) //turret
  1313. return false;
  1314. if (toDoubleWide)
  1315. {
  1316. if (isToReverseHlp (hexFrom, hexTo, curDir))
  1317. {
  1318. if (toDir)
  1319. return isToReverseHlp (hexFrom, hexTo-1, curDir);
  1320. else
  1321. return isToReverseHlp (hexFrom, hexTo+1, curDir);
  1322. }
  1323. return false;
  1324. }
  1325. else
  1326. {
  1327. return isToReverseHlp(hexFrom, hexTo, curDir);
  1328. }
  1329. }
  1330. ReachabilityInfo::TDistances CBattleInfoCallback::battleGetDistances(const battle::Unit * unit, BattleHex assumedPosition) const
  1331. {
  1332. ReachabilityInfo::TDistances ret;
  1333. ret.fill(-1);
  1334. RETURN_IF_NOT_BATTLE(ret);
  1335. auto reachability = getReachability(unit);
  1336. boost::copy(reachability.distances, ret.begin());
  1337. return ret;
  1338. }
  1339. bool CBattleInfoCallback::battleHasDistancePenalty(const IBonusBearer * shooter, BattleHex shooterPosition, BattleHex destHex) const
  1340. {
  1341. RETURN_IF_NOT_BATTLE(false);
  1342. const std::string cachingStrNoDistancePenalty = "type_NO_DISTANCE_PENALTY";
  1343. static const auto selectorNoDistancePenalty = Selector::type()(Bonus::NO_DISTANCE_PENALTY);
  1344. if(shooter->hasBonus(selectorNoDistancePenalty, cachingStrNoDistancePenalty))
  1345. return false;
  1346. if(auto target = battleGetUnitByPos(destHex, true))
  1347. {
  1348. //If any hex of target creature is within range, there is no penalty
  1349. for(auto hex : target->getHexes())
  1350. if(BattleHex::getDistance(shooterPosition, hex) <= GameConstants::BATTLE_PENALTY_DISTANCE)
  1351. return false;
  1352. //TODO what about two-hex shooters?
  1353. }
  1354. else
  1355. {
  1356. if(BattleHex::getDistance(shooterPosition, destHex) <= GameConstants::BATTLE_PENALTY_DISTANCE)
  1357. return false;
  1358. }
  1359. return true;
  1360. }
  1361. BattleHex CBattleInfoCallback::wallPartToBattleHex(EWallPart::EWallPart part) const
  1362. {
  1363. RETURN_IF_NOT_BATTLE(BattleHex::INVALID);
  1364. return WallPartToHex(part);
  1365. }
  1366. EWallPart::EWallPart CBattleInfoCallback::battleHexToWallPart(BattleHex hex) const
  1367. {
  1368. RETURN_IF_NOT_BATTLE(EWallPart::INVALID);
  1369. return hexToWallPart(hex);
  1370. }
  1371. bool CBattleInfoCallback::isWallPartPotentiallyAttackable(EWallPart::EWallPart wallPart) const
  1372. {
  1373. RETURN_IF_NOT_BATTLE(false);
  1374. return wallPart != EWallPart::INDESTRUCTIBLE_PART && wallPart != EWallPart::INDESTRUCTIBLE_PART_OF_GATE &&
  1375. wallPart != EWallPart::INVALID;
  1376. }
  1377. std::vector<BattleHex> CBattleInfoCallback::getAttackableBattleHexes() const
  1378. {
  1379. std::vector<BattleHex> attackableBattleHexes;
  1380. RETURN_IF_NOT_BATTLE(attackableBattleHexes);
  1381. for(auto & wallPartPair : wallParts)
  1382. {
  1383. if(isWallPartPotentiallyAttackable(wallPartPair.second))
  1384. {
  1385. auto wallState = static_cast<EWallState::EWallState>(battleGetWallState(static_cast<int>(wallPartPair.second)));
  1386. if(wallState == EWallState::INTACT || wallState == EWallState::DAMAGED)
  1387. {
  1388. attackableBattleHexes.push_back(BattleHex(wallPartPair.first));
  1389. }
  1390. }
  1391. }
  1392. return attackableBattleHexes;
  1393. }
  1394. int32_t CBattleInfoCallback::battleGetSpellCost(const spells::Spell * sp, const CGHeroInstance * caster) const
  1395. {
  1396. RETURN_IF_NOT_BATTLE(-1);
  1397. //TODO should be replaced using bonus system facilities (propagation onto battle node)
  1398. int32_t ret = caster->getSpellCost(sp);
  1399. //checking for friendly stacks reducing cost of the spell and
  1400. //enemy stacks increasing it
  1401. int32_t manaReduction = 0;
  1402. int32_t manaIncrease = 0;
  1403. for(auto unit : battleAliveUnits())
  1404. {
  1405. if(unit->unitOwner() == caster->tempOwner && unit->hasBonusOfType(Bonus::CHANGES_SPELL_COST_FOR_ALLY))
  1406. {
  1407. vstd::amax(manaReduction, unit->valOfBonuses(Bonus::CHANGES_SPELL_COST_FOR_ALLY));
  1408. }
  1409. if(unit->unitOwner() != caster->tempOwner && unit->hasBonusOfType(Bonus::CHANGES_SPELL_COST_FOR_ENEMY))
  1410. {
  1411. vstd::amax(manaIncrease, unit->valOfBonuses(Bonus::CHANGES_SPELL_COST_FOR_ENEMY));
  1412. }
  1413. }
  1414. return ret - manaReduction + manaIncrease;
  1415. }
  1416. bool CBattleInfoCallback::battleHasShootingPenalty(const battle::Unit * shooter, BattleHex destHex) const
  1417. {
  1418. return battleHasDistancePenalty(shooter, shooter->getPosition(), destHex) || battleHasWallPenalty(shooter, shooter->getPosition(), destHex);
  1419. }
  1420. bool CBattleInfoCallback::battleIsUnitBlocked(const battle::Unit * unit) const
  1421. {
  1422. RETURN_IF_NOT_BATTLE(false);
  1423. if(unit->hasBonusOfType(Bonus::SIEGE_WEAPON)) //siege weapons cannot be blocked
  1424. return false;
  1425. for(auto adjacent : battleAdjacentUnits(unit))
  1426. {
  1427. if(adjacent->unitOwner() != unit->unitOwner()) //blocked by enemy stack
  1428. return true;
  1429. }
  1430. return false;
  1431. }
  1432. std::set<const battle::Unit *> CBattleInfoCallback::battleAdjacentUnits(const battle::Unit * unit) const
  1433. {
  1434. std::set<const battle::Unit *> ret;
  1435. RETURN_IF_NOT_BATTLE(ret);
  1436. for(auto hex : unit->getSurroundingHexes())
  1437. {
  1438. if(auto neighbour = battleGetUnitByPos(hex, true))
  1439. ret.insert(neighbour);
  1440. }
  1441. return ret;
  1442. }
  1443. SpellID CBattleInfoCallback::getRandomBeneficialSpell(CRandomGenerator & rand, const CStack * subject) const
  1444. {
  1445. RETURN_IF_NOT_BATTLE(SpellID::NONE);
  1446. //This is complete list. No spells from mods.
  1447. //todo: this should be Spellbook of caster Stack
  1448. static const std::set<SpellID> allPossibleSpells =
  1449. {
  1450. SpellID::AIR_SHIELD,
  1451. SpellID::ANTI_MAGIC,
  1452. SpellID::BLESS,
  1453. SpellID::BLOODLUST,
  1454. SpellID::COUNTERSTRIKE,
  1455. SpellID::CURE,
  1456. SpellID::FIRE_SHIELD,
  1457. SpellID::FORTUNE,
  1458. SpellID::HASTE,
  1459. SpellID::MAGIC_MIRROR,
  1460. SpellID::MIRTH,
  1461. SpellID::PRAYER,
  1462. SpellID::PRECISION,
  1463. SpellID::PROTECTION_FROM_AIR,
  1464. SpellID::PROTECTION_FROM_EARTH,
  1465. SpellID::PROTECTION_FROM_FIRE,
  1466. SpellID::PROTECTION_FROM_WATER,
  1467. SpellID::SHIELD,
  1468. SpellID::SLAYER,
  1469. SpellID::STONE_SKIN
  1470. };
  1471. std::vector<SpellID> beneficialSpells;
  1472. auto getAliveEnemy = [=](const std::function<bool(const CStack *)> & pred) -> const CStack *
  1473. {
  1474. auto stacks = battleGetStacksIf([=](const CStack * stack)
  1475. {
  1476. return pred(stack) && stack->owner != subject->owner && stack->isValidTarget(false);
  1477. });
  1478. if(stacks.empty())
  1479. return nullptr;
  1480. else
  1481. return stacks.front();
  1482. };
  1483. for(const SpellID& spellID : allPossibleSpells)
  1484. {
  1485. std::stringstream cachingStr;
  1486. cachingStr << "source_" << Bonus::SPELL_EFFECT << "id_" << spellID.num;
  1487. if(subject->hasBonus(Selector::source(Bonus::SPELL_EFFECT, spellID), Selector::all, cachingStr.str())
  1488. //TODO: this ability has special limitations
  1489. || !(spellID.toSpell()->canBeCast(this, spells::Mode::CREATURE_ACTIVE, subject)))
  1490. continue;
  1491. switch (spellID)
  1492. {
  1493. case SpellID::SHIELD:
  1494. case SpellID::FIRE_SHIELD: // not if all enemy units are shooters
  1495. {
  1496. auto walker = getAliveEnemy([&](const CStack * stack) //look for enemy, non-shooting stack
  1497. {
  1498. return !stack->canShoot();
  1499. });
  1500. if (!walker)
  1501. continue;
  1502. }
  1503. break;
  1504. case SpellID::AIR_SHIELD: //only against active shooters
  1505. {
  1506. auto shooter = getAliveEnemy([&](const CStack * stack) //look for enemy, non-shooting stack
  1507. {
  1508. return stack->canShoot();
  1509. });
  1510. if (!shooter)
  1511. continue;
  1512. }
  1513. break;
  1514. case SpellID::ANTI_MAGIC:
  1515. case SpellID::MAGIC_MIRROR:
  1516. case SpellID::PROTECTION_FROM_AIR:
  1517. case SpellID::PROTECTION_FROM_EARTH:
  1518. case SpellID::PROTECTION_FROM_FIRE:
  1519. case SpellID::PROTECTION_FROM_WATER:
  1520. {
  1521. const ui8 enemySide = 1 - subject->side;
  1522. //todo: only if enemy has spellbook
  1523. if (!battleHasHero(enemySide)) //only if there is enemy hero
  1524. continue;
  1525. }
  1526. break;
  1527. case SpellID::CURE: //only damaged units
  1528. {
  1529. //do not cast on affected by debuffs
  1530. if(!subject->canBeHealed())
  1531. continue;
  1532. }
  1533. break;
  1534. case SpellID::BLOODLUST:
  1535. {
  1536. if(subject->canShoot()) //TODO: if can shoot - only if enemy units are adjacent
  1537. continue;
  1538. }
  1539. break;
  1540. case SpellID::PRECISION:
  1541. {
  1542. if(!subject->canShoot())
  1543. continue;
  1544. }
  1545. break;
  1546. case SpellID::SLAYER://only if monsters are present
  1547. {
  1548. auto kingMonster = getAliveEnemy([&](const CStack * stack) -> bool //look for enemy, non-shooting stack
  1549. {
  1550. const auto isKing = Selector::type()(Bonus::KING1)
  1551. .Or(Selector::type()(Bonus::KING2))
  1552. .Or(Selector::type()(Bonus::KING3));
  1553. return stack->hasBonus(isKing);
  1554. });
  1555. if (!kingMonster)
  1556. continue;
  1557. }
  1558. break;
  1559. }
  1560. beneficialSpells.push_back(spellID);
  1561. }
  1562. if(!beneficialSpells.empty())
  1563. {
  1564. return *RandomGeneratorUtil::nextItem(beneficialSpells, rand);
  1565. }
  1566. else
  1567. {
  1568. return SpellID::NONE;
  1569. }
  1570. }
  1571. SpellID CBattleInfoCallback::getRandomCastedSpell(CRandomGenerator & rand,const CStack * caster) const
  1572. {
  1573. RETURN_IF_NOT_BATTLE(SpellID::NONE);
  1574. TConstBonusListPtr bl = caster->getBonuses(Selector::type()(Bonus::SPELLCASTER));
  1575. if (!bl->size())
  1576. return SpellID::NONE;
  1577. int totalWeight = 0;
  1578. for(auto b : *bl)
  1579. {
  1580. totalWeight += std::max(b->additionalInfo[0], 1); //minimal chance to cast is 1
  1581. }
  1582. int randomPos = rand.nextInt(totalWeight - 1);
  1583. for(auto b : *bl)
  1584. {
  1585. randomPos -= std::max(b->additionalInfo[0], 1);
  1586. if(randomPos < 0)
  1587. {
  1588. return SpellID(b->subtype);
  1589. }
  1590. }
  1591. return SpellID::NONE;
  1592. }
  1593. int CBattleInfoCallback::battleGetSurrenderCost(PlayerColor Player) const
  1594. {
  1595. RETURN_IF_NOT_BATTLE(-3);
  1596. if(!battleCanSurrender(Player))
  1597. return -1;
  1598. const auto sideOpt = playerToSide(Player);
  1599. if(!sideOpt)
  1600. return -1;
  1601. const auto side = sideOpt.get();
  1602. int ret = 0;
  1603. double discount = 0;
  1604. for(auto unit : battleAliveUnits(side))
  1605. ret += unit->getRawSurrenderCost();
  1606. if(const CGHeroInstance * h = battleGetFightingHero(side))
  1607. discount += h->valOfBonuses(Bonus::SURRENDER_DISCOUNT);
  1608. ret = static_cast<int>(ret * (100.0 - discount) / 100.0);
  1609. vstd::amax(ret, 0); //no negative costs for >100% discounts (impossible in original H3 mechanics, but some day...)
  1610. return ret;
  1611. }
  1612. si8 CBattleInfoCallback::battleMinSpellLevel(ui8 side) const
  1613. {
  1614. const IBonusBearer * node = nullptr;
  1615. if(const CGHeroInstance * h = battleGetFightingHero(side))
  1616. node = h;
  1617. else
  1618. node = getBattleNode();
  1619. if(!node)
  1620. return 0;
  1621. auto b = node->getBonuses(Selector::type()(Bonus::BLOCK_MAGIC_BELOW));
  1622. if(b->size())
  1623. return b->totalValue();
  1624. return 0;
  1625. }
  1626. si8 CBattleInfoCallback::battleMaxSpellLevel(ui8 side) const
  1627. {
  1628. const IBonusBearer *node = nullptr;
  1629. if(const CGHeroInstance * h = battleGetFightingHero(side))
  1630. node = h;
  1631. else
  1632. node = getBattleNode();
  1633. if(!node)
  1634. return GameConstants::SPELL_LEVELS;
  1635. //We can't "just get value" - it'd be 0 if there are bonuses (and all would be blocked)
  1636. auto b = node->getBonuses(Selector::type()(Bonus::BLOCK_MAGIC_ABOVE));
  1637. if(b->size())
  1638. return b->totalValue();
  1639. return GameConstants::SPELL_LEVELS;
  1640. }
  1641. boost::optional<int> CBattleInfoCallback::battleIsFinished() const
  1642. {
  1643. auto units = battleGetUnitsIf([=](const battle::Unit * unit)
  1644. {
  1645. return unit->alive() && !unit->isTurret() && !unit->hasBonusOfType(Bonus::SIEGE_WEAPON);
  1646. });
  1647. std::array<bool, 2> hasUnit = {false, false}; //index is BattleSide
  1648. for(auto & unit : units)
  1649. {
  1650. //todo: move SIEGE_WEAPON check to Unit state
  1651. hasUnit.at(unit->unitSide()) = true;
  1652. if(hasUnit[0] && hasUnit[1])
  1653. return boost::none;
  1654. }
  1655. hasUnit = {false, false};
  1656. for(auto & unit : units)
  1657. {
  1658. if(!unit->isClone() && !unit->acquireState()->summoned && !dynamic_cast <const CCommanderInstance *>(unit))
  1659. {
  1660. hasUnit.at(unit->unitSide()) = true;
  1661. }
  1662. }
  1663. if(!hasUnit[0] && !hasUnit[1])
  1664. return 2;
  1665. if(!hasUnit[1])
  1666. return 0;
  1667. else
  1668. return 1;
  1669. }
  1670. VCMI_LIB_NAMESPACE_END