| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238 | 
							- #include "StdInc.h"
 
- #include "CBattleCallback.h"
 
- #include "CStack.h"
 
- #include "BattleInfo.h"
 
- #include "CGameState.h"
 
- #include "NetPacks.h"
 
- #include "spells/CSpellHandler.h"
 
- #include "VCMI_Lib.h"
 
- #include "CTownHandler.h"
 
- #include "mapObjects/CGTownInstance.h"
 
- /*
 
-  * CBattleCallback.cpp, part of VCMI engine
 
-  *
 
-  * Authors: listed in file AUTHORS in main folder
 
-  *
 
-  * License: GNU General Public License v2.0 or later
 
-  * Full text of license available in license.txt file, in main folder
 
-  *
 
-  */
 
- #define RETURN_IF_NOT_BATTLE(X) if(!duringBattle()) {logGlobal->errorStream() << __FUNCTION__ << " called when no battle!"; return X; }
 
- namespace SiegeStuffThatShouldBeMovedToHandlers //  <=== TODO
 
- {
 
- 	static void retreiveTurretDamageRange(const CGTownInstance * town, const CStack *turret, double &outMinDmg, double &outMaxDmg)
 
- 	{
 
- 		assert(turret->getCreature()->idNumber == CreatureID::ARROW_TOWERS);
 
- 		assert(town);
 
- 		assert(turret->position >= -4 && turret->position <= -2);
 
- 		float multiplier = (turret->position == -2) ? 1 : 0.5;
 
- 		int baseMin = 6;
 
- 		int baseMax = 10;
 
- 		outMinDmg = multiplier * (baseMin + town->getTownLevel() * 2);
 
- 		outMaxDmg = multiplier * (baseMax + town->getTownLevel() * 3);
 
- 	}
 
- 	static BattleHex lineToWallHex(int line) //returns hex with wall in given line (y coordinate)
 
- 	{
 
- 		static const BattleHex lineToHex[] = {12, 29, 45, 62, 78, 95, 112, 130, 147, 165, 182};
 
- 		return lineToHex[line];
 
- 	}
 
- 	static bool sameSideOfWall(BattleHex pos1, BattleHex pos2)
 
- 	{
 
- 		const int wallInStackLine = lineToWallHex(pos1.getY());
 
- 		const int wallInDestLine = lineToWallHex(pos2.getY());
 
- 		const bool stackLeft = pos1 < wallInStackLine;
 
- 		const bool destLeft = pos2 < wallInDestLine;
 
- 		return stackLeft == destLeft;
 
- 	}
 
- 	// parts of wall
 
- 	static const std::pair<int, EWallPart::EWallPart> wallParts[] =
 
- 	{
 
- 		std::make_pair(50,  EWallPart::KEEP),
 
- 		std::make_pair(183, EWallPart::BOTTOM_TOWER),
 
- 		std::make_pair(182, EWallPart::BOTTOM_WALL),
 
- 		std::make_pair(130, EWallPart::BELOW_GATE),
 
- 		std::make_pair(78,  EWallPart::OVER_GATE),
 
- 		std::make_pair(29,  EWallPart::UPPER_WALL),
 
- 		std::make_pair(12,  EWallPart::UPPER_TOWER),
 
- 		std::make_pair(95,  EWallPart::INDESTRUCTIBLE_PART_OF_GATE),
 
- 		std::make_pair(96,  EWallPart::GATE),
 
- 		std::make_pair(45,  EWallPart::INDESTRUCTIBLE_PART),
 
- 		std::make_pair(62,  EWallPart::INDESTRUCTIBLE_PART),
 
- 		std::make_pair(112, EWallPart::INDESTRUCTIBLE_PART),
 
- 		std::make_pair(147, EWallPart::INDESTRUCTIBLE_PART),
 
- 		std::make_pair(165, EWallPart::INDESTRUCTIBLE_PART)
 
- 	};
 
- 	static EWallPart::EWallPart hexToWallPart(BattleHex hex)
 
- 	{
 
- 		for(auto & elem : wallParts)
 
- 		{
 
- 			if(elem.first == hex)
 
- 				return elem.second;
 
- 		}
 
- 		return EWallPart::INVALID; //not found!
 
- 	}
 
- 	static BattleHex WallPartToHex(EWallPart::EWallPart part)
 
- 	{
 
- 		for(auto & elem : wallParts)
 
- 		{
 
- 			if(elem.second == part)
 
- 				return elem.first;
 
- 		}
 
- 		return BattleHex::INVALID; //not found!
 
- 	}
 
- }
 
- using namespace SiegeStuffThatShouldBeMovedToHandlers;
 
- boost::shared_mutex& CCallbackBase::getGsMutex()
 
- {
 
- 	return *gs->mx;
 
- }
 
- bool CCallbackBase::duringBattle() const
 
- {
 
- 	return getBattle() != nullptr;
 
- }
 
- void CCallbackBase::setBattle(const BattleInfo *B)
 
- {
 
- 	battle = B;
 
- }
 
- boost::optional<PlayerColor> CCallbackBase::getPlayerID() const
 
- {
 
- 	return player;
 
- }
 
- ETerrainType CBattleInfoEssentials::battleTerrainType() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(ETerrainType::WRONG);
 
- 	return getBattle()->terrainType;
 
- }
 
- BFieldType CBattleInfoEssentials::battleGetBattlefieldType() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(BFieldType::NONE);
 
- 	return getBattle()->battlefieldType;
 
- }
 
- std::vector<std::shared_ptr<const CObstacleInstance> > CBattleInfoEssentials::battleGetAllObstacles(boost::optional<BattlePerspective::BattlePerspective> perspective /*= boost::none*/) const
 
- {
 
- 	std::vector<std::shared_ptr<const CObstacleInstance> > ret;
 
- 	RETURN_IF_NOT_BATTLE(ret);
 
- 	if(!perspective)
 
- 	{
 
- 		//if no particular perspective request, use default one
 
- 		perspective = battleGetMySide();
 
- 	}
 
- 	else
 
- 	{
 
- 		if(!!player && *perspective != battleGetMySide())
 
- 		{
 
- 			logGlobal->errorStream() << "Unauthorized access attempt!";
 
- 			assert(0); //I want to notice if that happens
 
- 			//perspective = battleGetMySide();
 
- 		}
 
- 	}
 
- 	for(auto oi : getBattle()->obstacles)
 
- 	{
 
- 		if(getBattle()->battleIsObstacleVisibleForSide(*oi, *perspective))
 
- 			ret.push_back(oi);
 
- 	}
 
- 	return ret;
 
- }
 
- bool CBattleInfoEssentials::battleIsObstacleVisibleForSide(const CObstacleInstance & coi, BattlePerspective::BattlePerspective side) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	return side == BattlePerspective::ALL_KNOWING || coi.visibleForSide(side, battleHasNativeStack(side));
 
- }
 
- bool CBattleInfoEssentials::battleHasNativeStack(ui8 side) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	for(const CStack *s : battleGetAllStacks())
 
- 	{
 
- 		if(s->attackerOwned == !side  &&  s->getCreature()->isItNativeTerrain(getBattle()->terrainType))
 
- 			return true;
 
- 	}
 
- 	return false;
 
- }
 
- TStacks CBattleInfoEssentials::battleGetAllStacks(bool includeTurrets /*= false*/) const
 
- {
 
- 	return battleGetStacksIf([=](const CStack * s)
 
- 	{
 
- 		return !s->isGhost() && (includeTurrets || !s->isTurret());
 
- 	});
 
- }
 
- TStacks CBattleInfoEssentials::battleGetStacksIf(TStackFilter predicate) const
 
- {
 
- 	TStacks ret;
 
- 	RETURN_IF_NOT_BATTLE(ret);
 
- 	vstd::copy_if(getBattle()->stacks, std::back_inserter(ret), predicate);
 
- 	return ret;
 
- }
 
- TStacks CBattleInfoEssentials::battleAliveStacks() const
 
- {
 
- 	return battleGetStacksIf([](const CStack * s){
 
- 		return s->isValidTarget(false);
 
- 	});
 
- }
 
- TStacks CBattleInfoEssentials::battleAliveStacks(ui8 side) const
 
- {
 
- 	return battleGetStacksIf([=](const CStack * s){
 
- 		return s->isValidTarget(false) && s->attackerOwned == !side;
 
- 	});
 
- }
 
- int CBattleInfoEssentials::battleGetMoatDmg() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(0);
 
- 	auto town = getBattle()->town;
 
- 	if(!town)
 
- 		return 0;
 
- 	return town->town->moatDamage;
 
- }
 
- const CGTownInstance * CBattleInfoEssentials::battleGetDefendedTown() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(nullptr);
 
- 	if(!getBattle() || getBattle()->town == nullptr)
 
- 		return nullptr;
 
- 	return getBattle()->town;
 
- }
 
- BattlePerspective::BattlePerspective CBattleInfoEssentials::battleGetMySide() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(BattlePerspective::INVALID);
 
- 	if(!player)
 
- 		return BattlePerspective::ALL_KNOWING;
 
- 	if(*player == getBattle()->sides[0].color)
 
- 		return BattlePerspective::LEFT_SIDE;
 
- 	if(*player == getBattle()->sides[1].color)
 
- 		return BattlePerspective::RIGHT_SIDE;
 
- 	logGlobal->errorStream() << "Cannot find player " << *player << " in battle!";
 
- 	return BattlePerspective::INVALID;
 
- }
 
- const CStack * CBattleInfoEssentials::battleActiveStack() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(nullptr);
 
- 	return battleGetStackByID(getBattle()->activeStack);
 
- }
 
- const CStack* CBattleInfoEssentials::battleGetStackByID(int ID, bool onlyAlive) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(nullptr);
 
- 	auto stacks = battleGetStacksIf([=](const CStack * s)
 
- 	{
 
- 		return s->ID == ID && (!onlyAlive || s->alive());
 
- 	});
 
- 	if(stacks.empty())
 
- 		return nullptr;
 
- 	else
 
- 		return stacks[0];
 
- }
 
- bool CBattleInfoEssentials::battleDoWeKnowAbout(ui8 side) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	auto p = battleGetMySide();
 
- 	return p == BattlePerspective::ALL_KNOWING  ||  p == side;
 
- }
 
- si8 CBattleInfoEssentials::battleTacticDist() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(0);
 
- 	return getBattle()->tacticDistance;
 
- }
 
- si8 CBattleInfoEssentials::battleGetTacticsSide() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(-1);
 
- 	return getBattle()->tacticsSide;
 
- }
 
- const CGHeroInstance * CBattleInfoEssentials::battleGetFightingHero(ui8 side) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(nullptr);
 
- 	if(side > 1)
 
- 	{
 
- 		logGlobal->errorStream() << "FIXME: " <<  __FUNCTION__ << " wrong argument!";
 
- 		return nullptr;
 
- 	}
 
- 	if(!battleDoWeKnowAbout(side))
 
- 	{
 
- 		logGlobal->errorStream() << "FIXME: " <<  __FUNCTION__ << " access check ";
 
- 		return nullptr;
 
- 	}
 
- 	return getBattle()->sides[side].hero;
 
- }
 
- const CArmedInstance * CBattleInfoEssentials::battleGetArmyObject(ui8 side) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(nullptr);
 
- 	if(side > 1)
 
- 	{
 
- 		logGlobal->errorStream() << "FIXME: " <<  __FUNCTION__ << " wrong argument!";
 
- 		return nullptr;
 
- 	}
 
- 	if(!battleDoWeKnowAbout(side))
 
- 	{
 
- 		logGlobal->errorStream() << "FIXME: " <<  __FUNCTION__ << " access check ";
 
- 		return nullptr;
 
- 	}
 
- 	return getBattle()->sides[side].armyObject;
 
- }
 
- InfoAboutHero CBattleInfoEssentials::battleGetHeroInfo( ui8 side ) const
 
- {
 
- 	auto hero = getBattle()->sides[side].hero;
 
- 	if(!hero)
 
- 	{
 
- 		logGlobal->warnStream() << __FUNCTION__ << ": side " << (int)side << " does not have hero!";
 
- 		return InfoAboutHero();
 
- 	}
 
- 	InfoAboutHero::EInfoLevel infoLevel = battleDoWeKnowAbout(side) ? InfoAboutHero::EInfoLevel::DETAILED : InfoAboutHero::EInfoLevel::BASIC;
 
- 	return InfoAboutHero(hero, infoLevel);
 
- }
 
- int CBattleInfoEssentials::battleCastSpells(ui8 side) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(-1);
 
- 	return getBattle()->sides[side].castSpellsCount;
 
- }
 
- const IBonusBearer * CBattleInfoEssentials::getBattleNode() const
 
- {
 
- 	return getBattle();
 
- }
 
- ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastSpell(const ISpellCaster * caster, ECastingMode::ECastingMode mode) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(ESpellCastProblem::INVALID);
 
- 	if(caster == nullptr)
 
- 	{
 
- 		logGlobal->errorStream() << "CBattleInfoCallback::battleCanCastSpell: no spellcaster.";
 
- 		return ESpellCastProblem::INVALID;
 
- 	}
 
- 	const PlayerColor player = caster->getOwner();
 
- 	const si8 side = playerToSide(player);
 
- 	if(side < 0)
 
- 		return ESpellCastProblem::INVALID;
 
- 	if(!battleDoWeKnowAbout(side))
 
- 	{
 
- 		logGlobal->warnStream() << "You can't check if enemy can cast given spell!";
 
- 		return ESpellCastProblem::INVALID;
 
- 	}
 
- 	if(battleTacticDist())
 
- 		return ESpellCastProblem::ONGOING_TACTIC_PHASE;
 
- 	switch (mode)
 
- 	{
 
- 	case ECastingMode::HERO_CASTING:
 
- 		{
 
- 			if(battleCastSpells(side) > 0)
 
- 				return ESpellCastProblem::ALREADY_CASTED_THIS_TURN;
 
- 			auto hero = dynamic_cast<const CGHeroInstance *>(caster);
 
- 			if(!hero)
 
- 				return ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
 
- 			if(hero->hasBonusOfType(Bonus::BLOCK_ALL_MAGIC))
 
- 				return ESpellCastProblem::MAGIC_IS_BLOCKED;
 
- 		}
 
- 		break;
 
- 	default:
 
- 		break;
 
- 	}
 
- 	return ESpellCastProblem::OK;
 
- }
 
- bool CBattleInfoEssentials::battleCanFlee(PlayerColor player) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	const si8 mySide = playerToSide(player);
 
- 	const CGHeroInstance *myHero = battleGetFightingHero(mySide);
 
- 	//current player have no hero
 
- 	if(!myHero)
 
- 		return false;
 
- 	//eg. one of heroes is wearing shakles of war
 
- 	if(myHero->hasBonusOfType(Bonus::BATTLE_NO_FLEEING))
 
- 		return false;
 
- 	//we are besieged defender
 
- 	if(mySide == BattleSide::DEFENDER  &&  battleGetSiegeLevel())
 
- 	{
 
- 		auto town = battleGetDefendedTown();
 
- 		if(!town->hasBuilt(BuildingID::ESCAPE_TUNNEL, ETownType::STRONGHOLD))
 
- 			return false;
 
- 	}
 
- 	return true;
 
- }
 
- si8 CBattleInfoEssentials::playerToSide(PlayerColor player) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(-1);
 
- 	int ret = vstd::find_pos_if(getBattle()->sides, [=](const SideInBattle &side){ return side.color == player; });
 
- 	if(ret < 0)
 
- 		logGlobal->warnStream() << "Cannot find side for player " << player;
 
- 	return ret;
 
- }
 
- bool CBattleInfoEssentials::playerHasAccessToHeroInfo(PlayerColor player, const CGHeroInstance * h) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	const si8 playerSide = playerToSide(player);
 
- 	if (playerSide >= 0)
 
- 	{
 
- 		if (getBattle()->sides[!playerSide].hero == h)
 
- 			return true;
 
- 	}
 
- 	return false;
 
- }
 
- ui8 CBattleInfoEssentials::battleGetSiegeLevel() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(0);
 
- 	return getBattle()->town ? getBattle()->town->fortLevel() : CGTownInstance::NONE;
 
- }
 
- bool CBattleInfoEssentials::battleCanSurrender(PlayerColor player) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	ui8 mySide = playerToSide(player);
 
- 	bool iAmSiegeDefender = ( mySide == BattleSide::DEFENDER  &&  battleGetSiegeLevel() );
 
- 	//conditions like for fleeing (except escape tunnel presence) + enemy must have a hero
 
- 	return battleCanFlee(player) && !iAmSiegeDefender && battleHasHero(!mySide);
 
- }
 
- bool CBattleInfoEssentials::battleHasHero(ui8 side) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	assert(side < 2);
 
- 	return getBattle()->sides[side].hero;
 
- }
 
- si8 CBattleInfoEssentials::battleGetWallState(int partOfWall) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(0);
 
- 	if(getBattle()->town == nullptr || getBattle()->town->fortLevel() == CGTownInstance::NONE)
 
- 		return EWallState::NONE;
 
- 	assert(partOfWall >= 0 && partOfWall < EWallPart::PARTS_COUNT);
 
- 	return getBattle()->si.wallState[partOfWall];
 
- }
 
- EGateState CBattleInfoEssentials::battleGetGateState() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(EGateState::NONE);
 
- 	if(getBattle()->town == nullptr || getBattle()->town->fortLevel() == CGTownInstance::NONE)
 
- 		return EGateState::NONE;
 
- 	return getBattle()->si.gateState;
 
- }
 
- PlayerColor CBattleInfoEssentials::battleGetOwner(const CStack * stack) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
 
- 	if(stack->hasBonusOfType(Bonus::HYPNOTIZED))
 
- 		return getBattle()->theOtherPlayer(stack->owner);
 
- 	else
 
- 		return stack->owner;
 
- }
 
- const CGHeroInstance * CBattleInfoEssentials::battleGetOwnerHero(const CStack * stack) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(nullptr);
 
- 	return getBattle()->sides.at(playerToSide(battleGetOwner(stack))).hero;
 
- }
 
- bool CBattleInfoEssentials::battleMatchOwner(const CStack * attacker, const CStack * defender, const boost::logic::tribool positivness /* = false*/) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	if(boost::logic::indeterminate(positivness))
 
- 		return true;
 
- 	else if(defender->owner != battleGetOwner(defender))
 
- 		return true;//mind controlled unit is attackable for both sides
 
- 	else
 
- 		return (battleGetOwner(attacker) == battleGetOwner(defender)) == positivness;
 
- }
 
- si8 CBattleInfoCallback::battleHasWallPenalty( const CStack * stack, BattleHex destHex ) const
 
- {
 
- 	return battleHasWallPenalty(stack, stack->position, destHex);
 
- }
 
- si8 CBattleInfoCallback::battleHasWallPenalty(const IBonusBearer *bonusBearer, BattleHex shooterPosition, BattleHex destHex) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	if (!battleGetSiegeLevel() || bonusBearer->hasBonusOfType(Bonus::NO_WALL_PENALTY))
 
- 		return false;
 
- 	const int wallInStackLine = lineToWallHex(shooterPosition.getY());
 
- 	const int wallInDestLine = lineToWallHex(destHex.getY());
 
- 	const bool stackLeft = shooterPosition < wallInStackLine;
 
- 	const bool destRight = destHex > wallInDestLine;
 
- 	if (stackLeft && destRight) //shooting from outside to inside
 
- 	{
 
- 		int row = (shooterPosition + destHex) / (2 * GameConstants::BFIELD_WIDTH);
 
- 		if (shooterPosition > destHex && ((destHex % GameConstants::BFIELD_WIDTH - shooterPosition % GameConstants::BFIELD_WIDTH) < 2)) //shooting up high
 
- 			row -= 2;
 
- 		const int wallPos = lineToWallHex(row);
 
- 		if (!isWallPartPotentiallyAttackable(battleHexToWallPart(wallPos))) return true;
 
- 	}
 
- 	return false;
 
- }
 
- si8 CBattleInfoCallback::battleCanTeleportTo(const CStack * stack, BattleHex destHex, int telportLevel) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	if (!getAccesibility(stack).accessible(destHex, stack))
 
- 		return false;
 
- 	const ui8 siegeLevel = battleGetSiegeLevel();
 
- 	//check for wall
 
- 	//advanced teleport can pass wall of fort|citadel, expert - of castle
 
- 	if ((siegeLevel > CGTownInstance::NONE && telportLevel < 2) || (siegeLevel >= CGTownInstance::CASTLE && telportLevel < 3))
 
- 		return sameSideOfWall(stack->position, destHex);
 
- 	return true;
 
- }
 
- std::set<BattleHex> CBattleInfoCallback::battleGetAttackedHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos  /*= BattleHex::INVALID*/) const
 
- {
 
- 	std::set<BattleHex> attackedHexes;
 
- 	RETURN_IF_NOT_BATTLE(attackedHexes);
 
- 	AttackableTiles at = getPotentiallyAttackableHexes(attacker, destinationTile, attackerPos);
 
- 	for (BattleHex tile : at.hostileCreaturePositions)
 
- 	{
 
- 		const CStack * st = battleGetStackByPos(tile, true);
 
- 		if(st && st->owner != attacker->owner) //only hostile stacks - does it work well with Berserk?
 
- 		{
 
- 			attackedHexes.insert(tile);
 
- 		}
 
- 	}
 
- 	for (BattleHex tile : at.friendlyCreaturePositions)
 
- 	{
 
- 		if(battleGetStackByPos(tile, true)) //friendly stacks can also be damaged by Dragon Breath
 
- 		{
 
- 			attackedHexes.insert(tile);
 
- 		}
 
- 	}
 
- 	return attackedHexes;
 
- }
 
- SpellID CBattleInfoCallback::battleGetRandomStackSpell(CRandomGenerator & rand, const CStack * stack, ERandomSpell mode) const
 
- {
 
- 	switch (mode)
 
- 	{
 
- 	case RANDOM_GENIE:
 
- 		return getRandomBeneficialSpell(rand, stack); //target
 
- 		break;
 
- 	case RANDOM_AIMED:
 
- 		return getRandomCastedSpell(rand, stack); //caster
 
- 		break;
 
- 	default:
 
- 		logGlobal->errorStream() << "Incorrect mode of battleGetRandomSpell (" << mode <<")";
 
- 		return SpellID::NONE;
 
- 	}
 
- }
 
- const CStack* CBattleInfoCallback::battleGetStackByPos(BattleHex pos, bool onlyAlive) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(nullptr);
 
- 	for(auto s : battleGetAllStacks(true))
 
- 		if(vstd::contains(s->getHexes(), pos) && (!onlyAlive || s->alive()))
 
- 				return s;
 
- 	return nullptr;
 
- }
 
- void CBattleInfoCallback::battleGetStackQueue(std::vector<const CStack *> &out, const int howMany, const int turn /*= 0*/, int lastMoved /*= -1*/) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE();
 
- 	//let's define a huge lambda
 
- 	auto takeStack = [&](std::vector<const CStack *> &st) -> const CStack*
 
- 	{
 
- 		const CStack *ret = nullptr;
 
- 		unsigned i, //fastest stack
 
- 			j=0; //fastest stack of the other side
 
- 		for(i = 0; i < st.size(); i++)
 
- 			if(st[i])
 
- 				break;
 
- 		//no stacks left
 
- 		if(i == st.size())
 
- 			return nullptr;
 
- 		const CStack *fastest = st[i], *other = nullptr;
 
- 		int bestSpeed = fastest->Speed(turn);
 
- 		//FIXME: comparison between bool and integer. Logic does not makes sense either
 
- 		if(fastest->attackerOwned != lastMoved)
 
- 		{
 
- 			ret = fastest;
 
- 		}
 
- 		else
 
- 		{
 
- 			for(j = i + 1; j < st.size(); j++)
 
- 			{
 
- 				if(!st[j]) continue;
 
- 				if(st[j]->attackerOwned != lastMoved || st[j]->Speed(turn) != bestSpeed)
 
- 					break;
 
- 			}
 
- 			if(j >= st.size())
 
- 			{
 
- 				ret = fastest;
 
- 			}
 
- 			else
 
- 			{
 
- 				other = st[j];
 
- 				if(other->Speed(turn) != bestSpeed)
 
- 					ret = fastest;
 
- 				else
 
- 					ret = other;
 
- 			}
 
- 		}
 
- 		assert(ret);
 
- 		if(ret == fastest)
 
- 			st[i] = nullptr;
 
- 		else
 
- 			st[j] = nullptr;
 
- 		lastMoved = ret->attackerOwned;
 
- 		return ret;
 
- 	};
 
- 	//We'll split creatures with remaining movement to 4 buckets
 
- 	// [0] - turrets/catapult,
 
- 	// [1] - normal (unmoved) creatures, other war machines,
 
- 	// [2] - waited cres that had morale,
 
- 	// [3] - rest of waited cres
 
- 	std::vector<const CStack *> phase[4];
 
- 	int toMove = 0; //how many stacks still has move
 
- 	const CStack *active = battleActiveStack();
 
- 	//active stack hasn't taken any action yet - must be placed at the beginning of queue, no matter what
 
- 	if(!turn && active && active->willMove() && !active->waited())
 
- 	{
 
- 		out.push_back(active);
 
- 		if(out.size() == howMany)
 
- 			return;
 
- 	}
 
- 	auto allStacks = battleGetAllStacks(true);
 
- 	if(!vstd::contains_if(allStacks, [](const CStack *stack) { return stack->willMove(100000); })) //little evil, but 100000 should be enough for all effects to disappear
 
- 	{
 
- 		//No stack will be able to move, battle is over.
 
- 		out.clear();
 
- 		return;
 
- 	}
 
- 	for(auto s : battleGetAllStacks(true))
 
- 	{
 
- 		if((turn <= 0 && !s->willMove()) //we are considering current round and stack won't move
 
- 			|| (turn > 0 && !s->canMove(turn)) //stack won't be able to move in later rounds
 
- 			|| (turn <= 0 && s == active && out.size() && s == out.front())) //it's active stack already added at the beginning of queue
 
- 		{
 
- 			continue;
 
- 		}
 
- 		int p = -1; //in which phase this tack will move?
 
- 		if(turn <= 0 && s->waited()) //consider waiting state only for ongoing round
 
- 		{
 
- 			if(vstd::contains(s->state, EBattleStackState::HAD_MORALE))
 
- 				p = 2;
 
- 			else
 
- 				p = 3;
 
- 		}
 
- 		else if(s->getCreature()->idNumber == CreatureID::CATAPULT  ||  s->getCreature()->idNumber == CreatureID::ARROW_TOWERS) //catapult and turrets are first
 
- 		{
 
- 			p = 0;
 
- 		}
 
- 		else
 
- 		{
 
- 			p = 1;
 
- 		}
 
- 		phase[p].push_back(s);
 
- 		toMove++;
 
- 	}
 
- 	for(int i = 0; i < 4; i++)
 
- 		boost::sort(phase[i], CMP_stack(i, turn > 0 ? turn : 0));
 
- 	for(size_t i = 0; i < phase[0].size() && i < howMany; i++)
 
- 		out.push_back(phase[0][i]);
 
- 	if(out.size() == howMany)
 
- 		return;
 
- 	if(lastMoved == -1)
 
- 	{
 
- 		if(active)
 
- 		{
 
- 			//FIXME: both branches contain same code!!!
 
- 			if(out.size() && out.front() == active)
 
- 				lastMoved = active->attackerOwned;
 
- 			else
 
- 				lastMoved = active->attackerOwned;
 
- 		}
 
- 		else
 
- 		{
 
- 			lastMoved = 0;
 
- 		}
 
- 	}
 
- 	int pi = 1;
 
- 	while(out.size() < howMany)
 
- 	{
 
- 		const CStack *hlp = takeStack(phase[pi]);
 
- 		if(!hlp)
 
- 		{
 
- 			pi++;
 
- 			if(pi > 3)
 
- 			{
 
- 				//if(turn != 2)
 
- 				battleGetStackQueue(out, howMany, turn + 1, lastMoved);
 
- 				return;
 
- 			}
 
- 		}
 
- 		else
 
- 		{
 
- 			out.push_back(hlp);
 
- 		}
 
- 	}
 
- }
 
- void CBattleInfoCallback::battleGetStackCountOutsideHexes(bool *ac) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE();
 
- 	auto accessibility = getAccesibility();
 
- 	for(int i = 0; i < accessibility.size(); i++)
 
- 		ac[i] = (accessibility[i] == EAccessibility::ACCESSIBLE);
 
- }
 
- std::vector<BattleHex> CBattleInfoCallback::battleGetAvailableHexes(const CStack * stack, bool addOccupiable, std::vector<BattleHex> * attackable) const
 
- {
 
- 	std::vector<BattleHex> ret;
 
- 	RETURN_IF_NOT_BATTLE(ret);
 
- 	if(!stack->position.isValid()) //turrets
 
- 		return ret;
 
- 	auto reachability = getReachability(stack);
 
- 	for (int i = 0; i < GameConstants::BFIELD_SIZE; ++i)
 
- 	{
 
- 		// If obstacles or other stacks makes movement impossible, it can't be helped.
 
- 		if(!reachability.isReachable(i))
 
- 			continue;
 
- 		if(battleTacticDist() && battleGetTacticsSide() == !stack->attackerOwned)
 
- 		{
 
- 			//Stack has to perform tactic-phase movement -> can enter any reachable tile within given range
 
- 			if(!isInTacticRange(i))
 
- 				continue;
 
- 		}
 
- 		else
 
- 		{
 
- 			//Not tactics phase -> destination must be reachable and within stack range.
 
- 			if(reachability.distances[i] > stack->Speed(0, true))
 
- 				continue;
 
- 		}
 
- 		ret.push_back(i);
 
- 		if(addOccupiable && stack->doubleWide())
 
- 		{
 
- 			//If two-hex stack can stand on hex i then obviously it can occupy its second hex from that position
 
- 			ret.push_back(stack->occupiedHex(i));
 
- 		}
 
- 	}
 
- 	if(attackable)
 
- 	{
 
- 		auto meleeAttackable = [&](BattleHex hex) -> bool
 
- 		{
 
- 			// Return true if given hex has at least one available neighbour.
 
- 			// Available hexes are already present in ret vector.
 
- 			auto availableNeighbor = boost::find_if(ret, [=] (BattleHex availableHex)
 
- 				{  return BattleHex::mutualPosition(hex, availableHex) >= 0;  });
 
- 			return availableNeighbor != ret.end();
 
- 		};
 
- 		for(const CStack * otherSt : battleAliveStacks(stack->attackerOwned))
 
- 		{
 
- 			if(!otherSt->isValidTarget(false))
 
- 				continue;
 
- 			std::vector<BattleHex> occupied = otherSt->getHexes();
 
- 			if(battleCanShoot(stack, otherSt->position))
 
- 			{
 
- 				attackable->insert(attackable->end(), occupied.begin(), occupied.end());
 
- 				continue;
 
- 			}
 
- 			for(BattleHex he : occupied)
 
- 			{
 
- 				if(meleeAttackable(he))
 
- 					attackable->push_back(he);
 
- 			}
 
- 		}
 
- 	}
 
- 	//adding occupiable likely adds duplicates to ret -> clean it up
 
- 	boost::sort(ret);
 
- 	ret.erase(boost::unique(ret).end(), ret.end());
 
- 	return ret;
 
- }
 
- bool CBattleInfoCallback::battleCanAttack(const CStack * stack, const CStack * target, BattleHex dest) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	if(battleTacticDist())
 
- 		return false;
 
- 	if (!stack || !target)
 
- 		return false;
 
- 	if(!battleMatchOwner(stack, target))
 
- 		return false;
 
- 	auto &id = stack->getCreature()->idNumber;
 
- 	if (id == CreatureID::FIRST_AID_TENT || id == CreatureID::CATAPULT)
 
- 		return false;
 
- 	if (!target->alive())
 
- 		return false;
 
- 	return true;
 
- }
 
- bool CBattleInfoCallback::battleCanShoot(const CStack * stack, BattleHex dest) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	if(battleTacticDist()) //no shooting during tactics
 
- 		return false;
 
- 	const CStack *dst = battleGetStackByPos(dest);
 
- 	if(!stack || !dst)
 
- 		return false;
 
- 	//forgetfulness
 
- 	TBonusListPtr forgetfulList = stack->getBonuses(Selector::type(Bonus::FORGETFULL),"");
 
- 	if(!forgetfulList->empty())
 
- 	{
 
- 		int forgetful = forgetfulList->valOfBonuses(Selector::type(Bonus::FORGETFULL));
 
- 		//advanced+ level
 
- 		if(forgetful > 1)
 
- 			return false;
 
- 	}
 
- 	if(stack->getCreature()->idNumber == CreatureID::CATAPULT && dst) //catapult cannot attack creatures
 
- 		return false;
 
- 	if(stack->hasBonusOfType(Bonus::SHOOTER)//it's shooter
 
- 		&& battleMatchOwner(stack, dst)
 
- 		&& dst->alive()
 
- 		&& (!battleIsStackBlocked(stack)  ||  stack->hasBonusOfType(Bonus::FREE_SHOOTING))
 
- 		&& stack->shots
 
- 		)
 
- 		return true;
 
- 	return false;
 
- }
 
- TDmgRange CBattleInfoCallback::calculateDmgRange(const CStack* attacker, const CStack* defender, bool shooting,
 
- 												 ui8 charge, bool lucky, bool unlucky, bool deathBlow, bool ballistaDoubleDmg) const
 
- {
 
- 	return calculateDmgRange(attacker, defender, attacker->count, shooting, charge, lucky, unlucky, deathBlow, ballistaDoubleDmg);
 
- }
 
- TDmgRange CBattleInfoCallback::calculateDmgRange(const BattleAttackInfo &info) const
 
- {
 
- 	auto battleBonusValue = [&](const IBonusBearer * bearer, CSelector selector) -> int
 
- 	{
 
- 		auto noLimit = Selector::effectRange(Bonus::NO_LIMIT);
 
- 		auto limitMatches = info.shooting
 
- 				? Selector::effectRange(Bonus::ONLY_DISTANCE_FIGHT)
 
- 				: Selector::effectRange(Bonus::ONLY_MELEE_FIGHT);
 
- 		//any regular bonuses or just ones for melee/ranged
 
- 		return bearer->getBonuses(selector, noLimit.Or(limitMatches))->totalValue();
 
- 	};
 
- 	double additiveBonus = 1.0, multBonus = 1.0,
 
- 		minDmg = info.attackerBonuses->getMinDamage() * info.attackerCount,//TODO: ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT
 
- 		maxDmg = info.attackerBonuses->getMaxDamage() * info.attackerCount;
 
- 	const CCreature *attackerType = info.attacker->getCreature(),
 
- 		*defenderType = info.defender->getCreature();
 
- 	if(attackerType->idNumber == CreatureID::ARROW_TOWERS)
 
- 	{
 
- 		SiegeStuffThatShouldBeMovedToHandlers::retreiveTurretDamageRange(battleGetDefendedTown(), info.attacker, minDmg, maxDmg);
 
- 	}
 
- 	if(info.attackerBonuses->hasBonusOfType(Bonus::SIEGE_WEAPON) && attackerType->idNumber != CreatureID::ARROW_TOWERS) //any siege weapon, but only ballista can attack (second condition - not arrow turret)
 
- 	{ //minDmg and maxDmg are multiplied by hero attack + 1
 
- 		auto retreiveHeroPrimSkill = [&](int skill) -> int
 
- 		{
 
- 			const std::shared_ptr<Bonus> b = info.attackerBonuses->getBonus(Selector::sourceTypeSel(Bonus::HERO_BASE_SKILL).And(Selector::typeSubtype(Bonus::PRIMARY_SKILL, skill)));
 
- 			return b ? b->val : 0; //if there is no hero or no info on his primary skill, return 0
 
- 		};
 
- 		minDmg *= retreiveHeroPrimSkill(PrimarySkill::ATTACK) + 1;
 
- 		maxDmg *= retreiveHeroPrimSkill(PrimarySkill::ATTACK) + 1;
 
- 	}
 
- 	int attackDefenceDifference = 0;
 
- 	double multAttackReduction = (100 - battleBonusValue (info.attackerBonuses, Selector::type(Bonus::GENERAL_ATTACK_REDUCTION))) / 100.0;
 
- 	attackDefenceDifference += battleBonusValue (info.attackerBonuses, Selector::typeSubtype(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK)) * multAttackReduction;
 
- 	double multDefenceReduction = (100 - battleBonusValue (info.attackerBonuses, Selector::type(Bonus::ENEMY_DEFENCE_REDUCTION))) / 100.0;
 
- 	attackDefenceDifference -= info.defenderBonuses->Defense() * multDefenceReduction;
 
- 	if(const std::shared_ptr<Bonus> slayerEffect = info.attackerBonuses->getBonus(Selector::type(Bonus::SLAYER))) //slayer handling //TODO: apply only ONLY_MELEE_FIGHT / DISTANCE_FIGHT?
 
- 	{
 
- 		std::vector<int> affectedIds;
 
- 		int spLevel = slayerEffect->val;
 
- 		//FIXME: do not check all creatures
 
- 		for(int g = 0; g < VLC->creh->creatures.size(); ++g)
 
- 		{
 
- 			for(const std::shared_ptr<Bonus> b : VLC->creh->creatures[g]->getBonusList())
 
- 			{
 
- 				if ( (b->type == Bonus::KING3 && spLevel >= 3) || //expert
 
- 					(b->type == Bonus::KING2 && spLevel >= 2) || //adv +
 
- 					(b->type == Bonus::KING1 && spLevel >= 0) ) //none or basic +
 
- 				{
 
- 					affectedIds.push_back(g);
 
- 					break;
 
- 				}
 
- 			}
 
- 		}
 
- 		for(auto & affectedId : affectedIds)
 
- 		{
 
- 			if(defenderType->idNumber == affectedId)
 
- 			{
 
- 				attackDefenceDifference += SpellID(SpellID::SLAYER).toSpell()->getPower(spLevel);
 
- 				break;
 
- 			}
 
- 		}
 
- 	}
 
- 	//bonus from attack/defense skills
 
- 	if(attackDefenceDifference < 0) //decreasing dmg
 
- 	{
 
- 		const double dec = std::min(0.025 * (-attackDefenceDifference), 0.7);
 
- 		multBonus *= 1.0 - dec;
 
- 	}
 
- 	else //increasing dmg
 
- 	{
 
- 		const double inc = std::min(0.05 * attackDefenceDifference, 4.0);
 
- 		additiveBonus += inc;
 
- 	}
 
- 	//applying jousting bonus
 
- 	if( info.attackerBonuses->hasBonusOfType(Bonus::JOUSTING) && !info.defenderBonuses->hasBonusOfType(Bonus::CHARGE_IMMUNITY) )
 
- 		additiveBonus += info.chargedFields * 0.05;
 
- 	//handling secondary abilities and artifacts giving premies to them
 
- 	if(info.shooting)
 
- 		additiveBonus += info.attackerBonuses->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::ARCHERY) / 100.0;
 
- 	else
 
- 		additiveBonus += info.attackerBonuses->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::OFFENCE) / 100.0;
 
- 	if(info.defenderBonuses)
 
- 		multBonus *= (std::max(0, 100 - info.defenderBonuses->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::ARMORER))) / 100.0;
 
- 	//handling hate effect
 
- 	additiveBonus += info.attackerBonuses->valOfBonuses(Bonus::HATE, defenderType->idNumber.toEnum()) / 100.;
 
- 	//luck bonus
 
- 	if (info.luckyHit)
 
- 	{
 
- 		additiveBonus += 1.0;
 
- 	}
 
- 	//unlucky hit, used only if negative luck is enabled
 
- 	if (info.unluckyHit)
 
- 	{
 
- 		additiveBonus -= 0.5; // FIXME: how bad (and luck in general) should work with following bonuses?
 
- 	}
 
- 	//ballista double dmg
 
- 	if(info.ballistaDoubleDamage)
 
- 	{
 
- 		additiveBonus += 1.0;
 
- 	}
 
- 	if (info.deathBlow) //Dread Knight and many WoGified creatures
 
- 	{
 
- 		additiveBonus += 1.0;
 
- 	}
 
- 	//handling spell effects
 
- 	if(!info.shooting) //eg. shield
 
- 	{
 
- 		multBonus *= (100 - info.defenderBonuses->valOfBonuses(Bonus::GENERAL_DAMAGE_REDUCTION, 0)) / 100.0;
 
- 	}
 
- 	else if(info.shooting) //eg. air shield
 
- 	{
 
- 		multBonus *= (100 - info.defenderBonuses->valOfBonuses(Bonus::GENERAL_DAMAGE_REDUCTION, 1)) / 100.0;
 
- 	}
 
- 	if(info.shooting)
 
- 	{
 
- 		//todo: set actual percentage in spell bonus configuration instead of just level; requires non trivial backward compatibility handling
 
- 		//get list first, total value of 0 also counts
 
- 		TBonusListPtr forgetfulList = info.attackerBonuses->getBonuses(Selector::type(Bonus::FORGETFULL),"");
 
- 		if(!forgetfulList->empty())
 
- 		{
 
- 			int forgetful = forgetfulList->valOfBonuses(Selector::type(Bonus::FORGETFULL));
 
- 			//none of basic level
 
- 			if(forgetful == 0 || forgetful == 1)
 
- 				multBonus *= 0.5;
 
- 			else
 
- 				logGlobal->warn("Attempt to calculate shooting damage with adv+ FORGETFULL effect");
 
- 		}
 
- 	}
 
- 	TBonusListPtr curseEffects = info.attackerBonuses->getBonuses(Selector::type(Bonus::ALWAYS_MINIMUM_DAMAGE));
 
- 	TBonusListPtr blessEffects = info.attackerBonuses->getBonuses(Selector::type(Bonus::ALWAYS_MAXIMUM_DAMAGE));
 
- 	int curseBlessAdditiveModifier = blessEffects->totalValue() - curseEffects->totalValue();
 
- 	double curseMultiplicativePenalty = curseEffects->size() ? (*std::max_element(curseEffects->begin(), curseEffects->end(), &Bonus::compareByAdditionalInfo<std::shared_ptr<Bonus>>))->additionalInfo : 0;
 
- 	if(curseMultiplicativePenalty) //curse handling (partial, the rest is below)
 
- 	{
 
- 		multBonus *= 1.0 - curseMultiplicativePenalty/100;
 
- 	}
 
- 	auto isAdvancedAirShield = [](const Bonus* bonus)
 
- 	{
 
- 		return bonus->source == Bonus::SPELL_EFFECT
 
- 			&& bonus->sid == SpellID::AIR_SHIELD
 
- 			&& bonus->val >= SecSkillLevel::ADVANCED;
 
- 	};
 
- 	//wall / distance penalty + advanced air shield
 
- 	const bool distPenalty = !info.attackerBonuses->hasBonusOfType(Bonus::NO_DISTANCE_PENALTY) && battleHasDistancePenalty(info.attackerBonuses, info.attackerPosition, info.defenderPosition);
 
- 	const bool obstaclePenalty = battleHasWallPenalty(info.attackerBonuses, info.attackerPosition, info.defenderPosition);
 
- 	if(info.shooting)
 
- 	{
 
- 		if (distPenalty || info.defenderBonuses->hasBonus(isAdvancedAirShield))
 
- 		{
 
- 			multBonus *= 0.5;
 
- 		}
 
- 		if (obstaclePenalty)
 
- 		{
 
- 			multBonus *= 0.5; //cumulative
 
- 		}
 
- 	}
 
- 	if(!info.shooting && info.attackerBonuses->hasBonusOfType(Bonus::SHOOTER) && !info.attackerBonuses->hasBonusOfType(Bonus::NO_MELEE_PENALTY))
 
- 	{
 
- 		multBonus *= 0.5;
 
- 	}
 
- 	// psychic elementals versus mind immune units 50%
 
- 	if(attackerType->idNumber == CreatureID::PSYCHIC_ELEMENTAL
 
- 	   && info.defenderBonuses->hasBonusOfType(Bonus::MIND_IMMUNITY))
 
- 	{
 
- 		multBonus *= 0.5;
 
- 	}
 
- 	// TODO attack on petrified unit 50%
 
- 	// blinded unit retaliates
 
- 	minDmg *= additiveBonus * multBonus;
 
- 	maxDmg *= additiveBonus * multBonus;
 
- 	TDmgRange returnedVal;
 
- 	if(curseEffects->size()) //curse handling (rest)
 
- 	{
 
- 		minDmg += curseBlessAdditiveModifier;
 
- 		returnedVal = std::make_pair(int(minDmg), int(minDmg));
 
- 	}
 
- 	else if(blessEffects->size()) //bless handling
 
- 	{
 
- 		maxDmg += curseBlessAdditiveModifier;
 
- 		returnedVal =  std::make_pair(int(maxDmg), int(maxDmg));
 
- 	}
 
- 	else
 
- 	{
 
- 		returnedVal =  std::make_pair(int(minDmg), int(maxDmg));
 
- 	}
 
- 	//damage cannot be less than 1
 
- 	vstd::amax(returnedVal.first, 1);
 
- 	vstd::amax(returnedVal.second, 1);
 
- 	return returnedVal;
 
- }
 
- TDmgRange CBattleInfoCallback::calculateDmgRange( const CStack* attacker, const CStack* defender, TQuantity attackerCount,
 
- 	bool shooting, ui8 charge, bool lucky, bool unlucky, bool deathBlow, bool ballistaDoubleDmg ) const
 
- {
 
- 	BattleAttackInfo bai(attacker, defender, shooting);
 
- 	bai.attackerCount = attackerCount;
 
- 	bai.chargedFields = charge;
 
- 	bai.luckyHit = lucky;
 
- 	bai.unluckyHit = unlucky;
 
- 	bai.deathBlow = deathBlow;
 
- 	bai.ballistaDoubleDamage = ballistaDoubleDmg;
 
- 	return calculateDmgRange(bai);
 
- }
 
- TDmgRange CBattleInfoCallback::battleEstimateDamage(CRandomGenerator & rand, const CStack * attacker, const CStack * defender, TDmgRange * retaliationDmg) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(std::make_pair(0, 0));
 
- 	const bool shooting = battleCanShoot(attacker, defender->position);
 
- 	const BattleAttackInfo bai(attacker, defender, shooting);
 
- 	return battleEstimateDamage(rand, bai, retaliationDmg);
 
- }
 
- std::pair<ui32, ui32> CBattleInfoCallback::battleEstimateDamage(CRandomGenerator & rand, const BattleAttackInfo &bai, std::pair<ui32, ui32> * retaliationDmg /*= nullptr*/) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(std::make_pair(0, 0));
 
- 	//const bool shooting = battleCanShoot(bai.attacker, bai.defenderPosition); //TODO handle bonus bearer
 
- 	//const ui8 mySide = !attacker->attackerOwned;
 
- 	TDmgRange ret = calculateDmgRange(bai);
 
- 	if(retaliationDmg)
 
- 	{
 
- 		if(bai.shooting)
 
- 		{
 
- 			retaliationDmg->first = retaliationDmg->second = 0;
 
- 		}
 
- 		else
 
- 		{
 
- 			ui32 TDmgRange::* pairElems[] = {&TDmgRange::first, &TDmgRange::second};
 
- 			for (int i=0; i<2; ++i)
 
- 			{
 
- 				BattleStackAttacked bsa;
 
- 				bsa.damageAmount = ret.*pairElems[i];
 
- 				bai.defender->prepareAttacked(bsa, rand, bai.defenderCount);
 
- 				auto retaliationAttack = bai.reverse();
 
- 				retaliationAttack.attackerCount = bsa.newAmount;
 
- 				retaliationDmg->*pairElems[!i] = calculateDmgRange(retaliationAttack).*pairElems[!i];
 
- 			}
 
- 		}
 
- 	}
 
- 	return ret;
 
- }
 
- std::shared_ptr<const CObstacleInstance> CBattleInfoCallback::battleGetObstacleOnPos(BattleHex tile, bool onlyBlocking /*= true*/) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(std::shared_ptr<const CObstacleInstance>());
 
- 	for(auto &obs : battleGetAllObstacles())
 
- 	{
 
- 		if(vstd::contains(obs->getBlockedTiles(), tile)
 
- 			|| (!onlyBlocking  &&  vstd::contains(obs->getAffectedTiles(), tile)))
 
- 		{
 
- 			return obs;
 
- 		}
 
- 	}
 
- 	return std::shared_ptr<const CObstacleInstance>();
 
- }
 
- AccessibilityInfo CBattleInfoCallback::getAccesibility() const
 
- {
 
- 	AccessibilityInfo ret;
 
- 	ret.fill(EAccessibility::ACCESSIBLE);
 
- 	//removing accessibility for side columns of hexes
 
- 	for(int y = 0; y < GameConstants::BFIELD_HEIGHT; y++)
 
- 	{
 
- 		ret[BattleHex(GameConstants::BFIELD_WIDTH - 1, y)] = EAccessibility::SIDE_COLUMN;
 
- 		ret[BattleHex(0, y)] = EAccessibility::SIDE_COLUMN;
 
- 	}
 
- 	//gate -> should be before stacks
 
- 	if(battleGetSiegeLevel() > 0)
 
- 	{
 
- 		EAccessibility::EAccessibility accessability = EAccessibility::ACCESSIBLE;
 
- 		switch(battleGetGateState())
 
- 		{
 
- 		case EGateState::CLOSED:
 
- 			accessability = EAccessibility::GATE;
 
- 			break;
 
- 		case EGateState::BLOCKED:
 
- 			accessability = EAccessibility::UNAVAILABLE;
 
- 			break;
 
- 		}
 
- 		ret[ESiegeHex::GATE_OUTER] = ret[ESiegeHex::GATE_INNER] = accessability;
 
- 	}
 
- 	//tiles occupied by standing stacks
 
- 	for(auto stack : battleAliveStacks())
 
- 	{
 
- 		for(auto hex : stack->getHexes())
 
- 			if(hex.isAvailable()) //towers can have <0 pos; we don't also want to overwrite side columns
 
- 				ret[hex] = EAccessibility::ALIVE_STACK;
 
- 	}
 
- 	//obstacles
 
- 	for(const auto &obst : battleGetAllObstacles())
 
- 	{
 
- 		for(auto hex : obst->getBlockedTiles())
 
- 			ret[hex] = EAccessibility::OBSTACLE;
 
- 	}
 
- 	//walls
 
- 	if(battleGetSiegeLevel() > 0)
 
- 	{
 
- 		static const int permanentlyLocked[] = {12, 45, 62, 112, 147, 165};
 
- 		for(auto hex : permanentlyLocked)
 
- 			ret[hex] = EAccessibility::UNAVAILABLE;
 
- 		//TODO likely duplicated logic
 
- 		static const std::pair<int, BattleHex> lockedIfNotDestroyed[] =
 
- 		{
 
- 			//which part of wall, which hex is blocked if this part of wall is not destroyed
 
- 			std::make_pair(2, BattleHex(ESiegeHex::DESTRUCTIBLE_WALL_4)),
 
- 			std::make_pair(3, BattleHex(ESiegeHex::DESTRUCTIBLE_WALL_3)),
 
- 			std::make_pair(4, BattleHex(ESiegeHex::DESTRUCTIBLE_WALL_2)),
 
- 			std::make_pair(5, BattleHex(ESiegeHex::DESTRUCTIBLE_WALL_1))
 
- 		};
 
- 		for(auto & elem : lockedIfNotDestroyed)
 
- 		{
 
- 			if(battleGetWallState(elem.first) != EWallState::DESTROYED)
 
- 				ret[elem.second] = EAccessibility::DESTRUCTIBLE_WALL;
 
- 		}
 
- 	}
 
- 	return ret;
 
- }
 
- AccessibilityInfo CBattleInfoCallback::getAccesibility(const CStack *stack) const
 
- {
 
- 	return getAccesibility(stack->getHexes());
 
- }
 
- AccessibilityInfo CBattleInfoCallback::getAccesibility(const std::vector<BattleHex> &accessibleHexes) const
 
- {
 
- 	auto ret = getAccesibility();
 
- 	for(auto hex : accessibleHexes)
 
- 		if(hex.isValid())
 
- 			ret[hex] = EAccessibility::ACCESSIBLE;
 
- 	return ret;
 
- }
 
- ReachabilityInfo CBattleInfoCallback::makeBFS(const AccessibilityInfo &accessibility, const ReachabilityInfo::Parameters ¶ms) const
 
- {
 
- 	ReachabilityInfo ret;
 
- 	ret.accessibility = accessibility;
 
- 	ret.params = params;
 
- 	ret.predecessors.fill(BattleHex::INVALID);
 
- 	ret.distances.fill(ReachabilityInfo::INFINITE_DIST);
 
- 	if(!params.startPosition.isValid()) //if got call for arrow turrets
 
- 		return ret;
 
- 	const std::set<BattleHex> quicksands = getStoppers(params.perspective);
 
- 	//const bool twoHexCreature = params.doubleWide;
 
- 	std::queue<BattleHex> hexq; //bfs queue
 
- 	//first element
 
- 	hexq.push(params.startPosition);
 
- 	ret.distances[params.startPosition] = 0;
 
- 	while(!hexq.empty()) //bfs loop
 
- 	{
 
- 		const BattleHex curHex = hexq.front();
 
- 		hexq.pop();
 
- 		//walking stack can't step past the quicksands
 
- 		//TODO what if second hex of two-hex creature enters quicksand
 
- 		if(curHex != params.startPosition && vstd::contains(quicksands, curHex))
 
- 			continue;
 
- 		const int costToNeighbour = ret.distances[curHex] + 1;
 
- 		for(BattleHex neighbour : curHex.neighbouringTiles())
 
- 		{
 
- 			const bool accessible = accessibility.accessible(neighbour, params.doubleWide, params.attackerOwned);
 
- 			const int costFoundSoFar = ret.distances[neighbour];
 
- 			if(accessible  &&  costToNeighbour < costFoundSoFar)
 
- 			{
 
- 				hexq.push(neighbour);
 
- 				ret.distances[neighbour] = costToNeighbour;
 
- 				ret.predecessors[neighbour] = curHex;
 
- 			}
 
- 		}
 
- 	}
 
- 	return ret;
 
- }
 
- ReachabilityInfo CBattleInfoCallback::makeBFS(const CStack *stack) const
 
- {
 
- 	return makeBFS(getAccesibility(stack), ReachabilityInfo::Parameters(stack));
 
- }
 
- std::set<BattleHex> CBattleInfoCallback::getStoppers(BattlePerspective::BattlePerspective whichSidePerspective) const
 
- {
 
- 	std::set<BattleHex> ret;
 
- 	RETURN_IF_NOT_BATTLE(ret);
 
- 	for(auto &oi : battleGetAllObstacles(whichSidePerspective))
 
- 	{
 
- 		if(battleIsObstacleVisibleForSide(*oi, whichSidePerspective))
 
- 		{
 
- 			range::copy(oi->getStoppingTile(), vstd::set_inserter(ret));
 
- 		}
 
- 	}
 
- 	return ret;
 
- }
 
- std::pair<const CStack *, BattleHex> CBattleInfoCallback::getNearestStack(const CStack * closest, boost::logic::tribool attackerOwned) const
 
- {
 
- 	auto reachability = getReachability(closest);
 
- 	auto avHexes = battleGetAvailableHexes(closest, false);
 
- 	// I hate std::pairs with their undescriptive member names first / second
 
- 	struct DistStack
 
- 	{
 
- 		int distanceToPred;
 
- 		BattleHex destination;
 
- 		const CStack *stack;
 
- 	};
 
- 	std::vector<DistStack> stackPairs;
 
- 	std::vector<const CStack *> possibleStacks = battleGetStacksIf([=](const CStack * s)
 
- 	{
 
- 		return s->isValidTarget(false) && s != closest && (boost::logic::indeterminate(attackerOwned) || s->attackerOwned == attackerOwned);
 
- 	});
 
- 	for(const CStack * st : possibleStacks)
 
- 		for(BattleHex hex : avHexes)
 
- 			if(CStack::isMeleeAttackPossible(closest, st, hex))
 
- 			{
 
- 				DistStack hlp = {reachability.distances[hex], hex, st};
 
- 				stackPairs.push_back(hlp);
 
- 			}
 
- 	if (stackPairs.size())
 
- 	{
 
- 		auto comparator = [](DistStack lhs, DistStack rhs) { return lhs.distanceToPred < rhs.distanceToPred; };
 
- 		auto minimal = boost::min_element(stackPairs, comparator);
 
- 		return std::make_pair(minimal->stack, minimal->destination);
 
- 	}
 
- 	else
 
- 		return std::make_pair<const CStack * , BattleHex>(nullptr, BattleHex::INVALID);
 
- }
 
- si8 CBattleInfoCallback::battleGetTacticDist() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(0);
 
- 	//TODO get rid of this method
 
- 	if(battleDoWeKnowAbout(battleGetTacticsSide()))
 
- 		return battleTacticDist();
 
- 	return 0;
 
- }
 
- bool CBattleInfoCallback::isInTacticRange(BattleHex dest) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	auto side = battleGetTacticsSide();
 
- 	auto dist = battleGetTacticDist();
 
- 	return ((!side && dest.getX() > 0 && dest.getX() <= dist)
 
- 		|| (side && dest.getX() < GameConstants::BFIELD_WIDTH - 1 && dest.getX() >= GameConstants::BFIELD_WIDTH - dist - 1));
 
- }
 
- ReachabilityInfo CBattleInfoCallback::getReachability(const CStack *stack) const
 
- {
 
- 	ReachabilityInfo::Parameters params(stack);
 
- 	if(!battleDoWeKnowAbout(!stack->attackerOwned))
 
- 	{
 
- 		//Stack is held by enemy, we can't use his perspective to check for reachability.
 
- 		// Happens ie. when hovering enemy stack for its range. The arg could be set properly, but it's easier to fix it here.
 
- 		params.perspective = battleGetMySide();
 
- 	}
 
- 	return getReachability(params);
 
- }
 
- ReachabilityInfo CBattleInfoCallback::getReachability(const ReachabilityInfo::Parameters ¶ms) const
 
- {
 
- 	if(params.flying)
 
- 		return getFlyingReachability(params);
 
- 	else
 
- 		return makeBFS(getAccesibility(params.knownAccessible), params);
 
- }
 
- ReachabilityInfo CBattleInfoCallback::getFlyingReachability(const ReachabilityInfo::Parameters ¶ms) const
 
- {
 
- 	ReachabilityInfo ret;
 
- 	ret.accessibility = getAccesibility(params.knownAccessible);
 
- 	for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
 
- 	{
 
- 		if(ret.accessibility.accessible(i, params.doubleWide, params.attackerOwned))
 
- 		{
 
- 			ret.predecessors[i] = params.startPosition;
 
- 			ret.distances[i] = BattleHex::getDistance(params.startPosition, i);
 
- 		}
 
- 	}
 
- 	return ret;
 
- }
 
- AttackableTiles CBattleInfoCallback::getPotentiallyAttackableHexes (const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos) const
 
- {
 
- 	//does not return hex attacked directly
 
- 	//TODO: apply rotation to two-hex attackers
 
- 	bool isAttacker = attacker->attackerOwned;
 
- 	AttackableTiles at;
 
- 	RETURN_IF_NOT_BATTLE(at);
 
- 	const int WN = GameConstants::BFIELD_WIDTH;
 
- 	BattleHex hex = (attackerPos != BattleHex::INVALID) ? attackerPos.hex : attacker->position.hex; //real or hypothetical (cursor) position
 
- 	//FIXME: dragons or cerbers can rotate before attack, making their base hex different (#1124)
 
- 	bool reverse = isToReverse (hex, destinationTile, isAttacker, attacker->doubleWide(), isAttacker);
 
- 	if (reverse && attacker->doubleWide())
 
- 	{
 
- 		hex = attacker->occupiedHex(hex); //the other hex stack stands on
 
- 	}
 
- 	if (attacker->hasBonusOfType(Bonus::ATTACKS_ALL_ADJACENT))
 
- 	{
 
- 		boost::copy (attacker->getSurroundingHexes (attackerPos), vstd::set_inserter (at.hostileCreaturePositions));
 
- 	}
 
- 	if (attacker->hasBonusOfType(Bonus::THREE_HEADED_ATTACK))
 
- 	{
 
- 		std::vector<BattleHex> hexes = attacker->getSurroundingHexes(attackerPos);
 
- 		for (BattleHex tile : hexes)
 
- 		{
 
- 			if ((BattleHex::mutualPosition(tile, destinationTile) > -1 && BattleHex::mutualPosition (tile, hex) > -1)) //adjacent both to attacker's head and attacked tile
 
- 			{
 
- 				const CStack * st = battleGetStackByPos(tile, true);
 
- 				if(st && st->owner != attacker->owner) //only hostile stacks - does it work well with Berserk?
 
- 				{
 
- 					at.hostileCreaturePositions.insert(tile);
 
- 				}
 
- 			}
 
- 		}
 
- 	}
 
- 	if (attacker->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH) && BattleHex::mutualPosition (destinationTile.hex, hex) > -1) //only adjacent hexes are subject of dragon breath calculation
 
- 	{
 
- 		std::vector<BattleHex> hexes; //only one, in fact
 
- 		int pseudoVector = destinationTile.hex - hex;
 
- 		switch (pseudoVector)
 
- 		{
 
- 		case 1:
 
- 		case -1:
 
- 			BattleHex::checkAndPush (destinationTile.hex + pseudoVector, hexes);
 
- 			break;
 
- 		case WN: //17 //left-down or right-down
 
- 		case -WN: //-17 //left-up or right-up
 
- 		case WN + 1: //18 //right-down
 
- 		case -WN + 1: //-16 //right-up
 
- 			BattleHex::checkAndPush (destinationTile.hex + pseudoVector + (((hex/WN)%2) ? 1 : -1 ), hexes);
 
- 			break;
 
- 		case WN-1: //16 //left-down
 
- 		case -WN-1: //-18 //left-up
 
- 			BattleHex::checkAndPush (destinationTile.hex + pseudoVector + (((hex/WN)%2) ? 1 : 0), hexes);
 
- 			break;
 
- 		}
 
- 		for (BattleHex tile : hexes)
 
- 		{
 
- 			//friendly stacks can also be damaged by Dragon Breath
 
- 			if (battleGetStackByPos (tile, true))
 
- 				at.friendlyCreaturePositions.insert (tile);
 
- 		}
 
- 	}
 
- 	return at;
 
- }
 
- std::set<const CStack*> CBattleInfoCallback::getAttackedCreatures(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos /*= BattleHex::INVALID*/) const
 
- {
 
- 	std::set<const CStack*> attackedCres;
 
- 	RETURN_IF_NOT_BATTLE(attackedCres);
 
- 	AttackableTiles at = getPotentiallyAttackableHexes(attacker, destinationTile, attackerPos);
 
- 	for (BattleHex tile : at.hostileCreaturePositions) //all around & three-headed attack
 
- 	{
 
- 		const CStack * st = battleGetStackByPos(tile, true);
 
- 		if(st && st->owner != attacker->owner) //only hostile stacks - does it work well with Berserk?
 
- 		{
 
- 			attackedCres.insert(st);
 
- 		}
 
- 	}
 
- 	for (BattleHex tile : at.friendlyCreaturePositions)
 
- 	{
 
- 		const CStack * st = battleGetStackByPos(tile, true);
 
- 		if(st) //friendly stacks can also be damaged by Dragon Breath
 
- 		{
 
- 			attackedCres.insert(st);
 
- 		}
 
- 	}
 
- 	return attackedCres;
 
- }
 
- //TODO: this should apply also to mechanics and cursor interface
 
- bool CBattleInfoCallback::isToReverseHlp (BattleHex hexFrom, BattleHex hexTo, bool curDir) const
 
- {
 
- 	int fromX = hexFrom.getX();
 
- 	int fromY = hexFrom.getY();
 
- 	int toX = hexTo.getX();
 
- 	int toY = hexTo.getY();
 
- 	if (curDir) // attacker, facing right
 
- 	{
 
- 		if (fromX < toX)
 
- 			return false;
 
- 		if (fromX > toX)
 
- 			return true;
 
- 		if (fromY % 2 == 0 && toY % 2 == 1)
 
- 			return true;
 
- 		return false;
 
- 	}
 
- 	else // defender, facing left
 
- 	{
 
- 		if(fromX < toX)
 
- 			return true;
 
- 		if(fromX > toX)
 
- 			return false;
 
- 		if (fromY % 2 == 1 && toY % 2 == 0)
 
- 			return true;
 
- 		return false;
 
- 	}
 
- }
 
- //TODO: this should apply also to mechanics and cursor interface
 
- bool CBattleInfoCallback::isToReverse (BattleHex hexFrom, BattleHex hexTo, bool curDir, bool toDoubleWide, bool toDir) const
 
- {
 
- 	if (hexTo < 0  ||  hexFrom < 0) //turret
 
- 		return false;
 
- 	if (toDoubleWide)
 
- 	{
 
- 		if (isToReverseHlp (hexFrom, hexTo, curDir))
 
- 		{
 
- 			if (toDir)
 
- 				return isToReverseHlp (hexFrom, hexTo-1, curDir);
 
- 			else
 
- 				return isToReverseHlp (hexFrom, hexTo+1, curDir);
 
- 		}
 
- 		return false;
 
- 	}
 
- 	else
 
- 	{
 
- 		return isToReverseHlp(hexFrom, hexTo, curDir);
 
- 	}
 
- }
 
- ReachabilityInfo::TDistances CBattleInfoCallback::battleGetDistances(const CStack * stack, BattleHex hex /*= BattleHex::INVALID*/, BattleHex * predecessors /*= nullptr*/) const
 
- {
 
- 	ReachabilityInfo::TDistances ret;
 
- 	ret.fill(-1);
 
- 	RETURN_IF_NOT_BATTLE(ret);
 
- 	ReachabilityInfo::Parameters params(stack);
 
- 	params.perspective = battleGetMySide();
 
- 	params.startPosition = hex.isValid() ? hex : stack->position;
 
- 	auto reachability = getReachability(params);
 
- 	boost::copy(reachability.distances, ret.begin());
 
- 	if(predecessors)
 
- 		for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
 
- 			predecessors[i] = reachability.predecessors[i];
 
- 	return ret;
 
- }
 
- si8 CBattleInfoCallback::battleHasDistancePenalty(const CStack * stack, BattleHex destHex) const
 
- {
 
- 	return battleHasDistancePenalty(stack, stack->position, destHex);
 
- }
 
- si8 CBattleInfoCallback::battleHasDistancePenalty(const IBonusBearer *bonusBearer, BattleHex shooterPosition, BattleHex destHex) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	if(bonusBearer->hasBonusOfType(Bonus::NO_DISTANCE_PENALTY))
 
- 		return false;
 
- 	if(const CStack * dstStack = battleGetStackByPos(destHex, false))
 
- 	{
 
- 		//If any hex of target creature is within range, there is no penalty
 
- 		for(auto hex : dstStack->getHexes())
 
- 			if(BattleHex::getDistance(shooterPosition, hex) <= GameConstants::BATTLE_PENALTY_DISTANCE)
 
- 				return false;
 
- 		//TODO what about two-hex shooters?
 
- 	}
 
- 	else
 
- 	{
 
- 		if (BattleHex::getDistance(shooterPosition, destHex) <= GameConstants::BATTLE_PENALTY_DISTANCE)
 
- 			return false;
 
- 	}
 
- 	return true;
 
- }
 
- BattleHex CBattleInfoCallback::wallPartToBattleHex(EWallPart::EWallPart part) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(BattleHex::INVALID);
 
- 	return WallPartToHex(part);
 
- }
 
- EWallPart::EWallPart CBattleInfoCallback::battleHexToWallPart(BattleHex hex) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(EWallPart::INVALID);
 
- 	return hexToWallPart(hex);
 
- }
 
- bool CBattleInfoCallback::isWallPartPotentiallyAttackable(EWallPart::EWallPart wallPart) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	return wallPart != EWallPart::INDESTRUCTIBLE_PART && wallPart != EWallPart::INDESTRUCTIBLE_PART_OF_GATE &&
 
- 			wallPart != EWallPart::INVALID;
 
- }
 
- std::vector<BattleHex> CBattleInfoCallback::getAttackableBattleHexes() const
 
- {
 
- 	std::vector<BattleHex> attackableBattleHexes;
 
- 	RETURN_IF_NOT_BATTLE(attackableBattleHexes);
 
- 	for(auto & wallPartPair : wallParts)
 
- 	{
 
- 		if(isWallPartPotentiallyAttackable(wallPartPair.second))
 
- 		{
 
- 			auto wallState = static_cast<EWallState::EWallState>(battleGetWallState(static_cast<int>(wallPartPair.second)));
 
- 			if(wallState == EWallState::INTACT || wallState == EWallState::DAMAGED)
 
- 			{
 
- 				attackableBattleHexes.push_back(BattleHex(wallPartPair.first));
 
- 			}
 
- 		}
 
- 	}
 
- 	return attackableBattleHexes;
 
- }
 
- ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastThisSpell(const ISpellCaster * caster, const CSpell * spell, ECastingMode::ECastingMode mode) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(ESpellCastProblem::INVALID);
 
- 	if(caster == nullptr)
 
- 	{
 
- 		logGlobal->errorStream() << "CBattleInfoCallback::battleCanCastThisSpell: no spellcaster.";
 
- 		return ESpellCastProblem::INVALID;
 
- 	}
 
- 	const PlayerColor player = caster->getOwner();
 
- 	const si8 side = playerToSide(player);
 
- 	if(side < 0)
 
- 		return ESpellCastProblem::INVALID;
 
- 	if(!battleDoWeKnowAbout(side))
 
- 		return ESpellCastProblem::INVALID;
 
- 	ESpellCastProblem::ESpellCastProblem genProblem = battleCanCastSpell(caster, mode);
 
- 	if(genProblem != ESpellCastProblem::OK)
 
- 		return genProblem;
 
- 	switch(mode)
 
- 	{
 
- 	case ECastingMode::HERO_CASTING:
 
- 		{
 
- 			const CGHeroInstance * castingHero = dynamic_cast<const CGHeroInstance *>(caster);//todo: unify hero|creature spell cost
 
- 			if(!castingHero)
 
- 			{
 
- 				logGlobal->error("battleCanCastThisSpell: invalid caster");
 
- 				return ESpellCastProblem::INVALID;
 
- 			}
 
- 			if(!castingHero->getArt(ArtifactPosition::SPELLBOOK))
 
- 				return ESpellCastProblem::NO_SPELLBOOK;
 
- 			if(!castingHero->canCastThisSpell(spell))
 
- 				return ESpellCastProblem::HERO_DOESNT_KNOW_SPELL;
 
- 			if(castingHero->mana < battleGetSpellCost(spell, castingHero)) //not enough mana
 
- 				return ESpellCastProblem::NOT_ENOUGH_MANA;
 
- 		}
 
- 		break;
 
- 	}
 
- 	if(!spell->combatSpell)
 
- 		return ESpellCastProblem::ADVMAP_SPELL_INSTEAD_OF_BATTLE_SPELL;
 
- 	//effect like Recanter's Cloak. Blocks also passive casting.
 
- 	//TODO: check creature abilities to block
 
- 	if(battleMaxSpellLevel(side) < spell->level)
 
- 		return ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED;
 
- 	return spell->canBeCast(this, mode, caster);
 
- }
 
- ui32 CBattleInfoCallback::battleGetSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(-1);
 
- 	//TODO should be replaced using bonus system facilities (propagation onto battle node)
 
- 	ui32 ret = caster->getSpellCost(sp);
 
- 	//checking for friendly stacks reducing cost of the spell and
 
- 	//enemy stacks increasing it
 
- 	si32 manaReduction = 0;
 
- 	si32 manaIncrease = 0;
 
- 	for(auto stack : battleAliveStacks())
 
- 	{
 
- 		if(stack->owner == caster->tempOwner  &&  stack->hasBonusOfType(Bonus::CHANGES_SPELL_COST_FOR_ALLY) )
 
- 		{
 
- 			vstd::amax(manaReduction, stack->valOfBonuses(Bonus::CHANGES_SPELL_COST_FOR_ALLY));
 
- 		}
 
- 		if( stack->owner != caster->tempOwner  &&  stack->hasBonusOfType(Bonus::CHANGES_SPELL_COST_FOR_ENEMY) )
 
- 		{
 
- 			vstd::amax(manaIncrease, stack->valOfBonuses(Bonus::CHANGES_SPELL_COST_FOR_ENEMY));
 
- 		}
 
- 	}
 
- 	return ret - manaReduction + manaIncrease;
 
- }
 
- ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastThisSpellHere(const ISpellCaster * caster, const CSpell * spell, ECastingMode::ECastingMode mode, BattleHex dest) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(ESpellCastProblem::INVALID);
 
- 	if(caster == nullptr)
 
- 	{
 
- 		logGlobal->errorStream() << "CBattleInfoCallback::battleCanCastThisSpellHere: no spellcaster.";
 
- 		return ESpellCastProblem::INVALID;
 
- 	}
 
- 	ESpellCastProblem::ESpellCastProblem problem = battleCanCastThisSpell(caster, spell, mode);
 
- 	if(problem != ESpellCastProblem::OK)
 
- 		return problem;
 
- 	return spell->canBeCastAt(this, caster, mode, dest);
 
- }
 
- const CStack * CBattleInfoCallback::getStackIf(std::function<bool(const CStack*)> pred) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(nullptr);
 
- 	auto stacks = battleGetAllStacks();
 
- 	auto stackItr = range::find_if(stacks, pred);
 
- 	return stackItr == stacks.end()
 
- 		? nullptr
 
- 		: *stackItr;
 
- }
 
- bool CBattleInfoCallback::battleIsStackBlocked(const CStack * stack) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	if(stack->hasBonusOfType(Bonus::SIEGE_WEAPON)) //siege weapons cannot be blocked
 
- 		return false;
 
- 	for(const CStack * s :  batteAdjacentCreatures(stack))
 
- 	{
 
- 		if (s->owner != stack->owner) //blocked by enemy stack
 
- 			return true;
 
- 	}
 
- 	return false;
 
- }
 
- std::set<const CStack*> CBattleInfoCallback:: batteAdjacentCreatures(const CStack * stack) const
 
- {
 
- 	std::set<const CStack*> stacks;
 
- 	RETURN_IF_NOT_BATTLE(stacks);
 
- 	for (BattleHex hex : stack->getSurroundingHexes())
 
- 		if(const CStack *neighbour = battleGetStackByPos(hex, true))
 
- 			stacks.insert(neighbour);
 
- 	return stacks;
 
- }
 
- SpellID CBattleInfoCallback::getRandomBeneficialSpell(CRandomGenerator & rand, const CStack * subject) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(SpellID::NONE);
 
- 	//This is complete list. No spells from mods.
 
- 	//todo: this should be Spellbook of caster Stack
 
- 	static const std::set<SpellID> allPossibleSpells =
 
- 	{
 
- 		SpellID::AIR_SHIELD,
 
- 		SpellID::ANTI_MAGIC,
 
- 		SpellID::BLESS,
 
- 		SpellID::BLOODLUST,
 
- 		SpellID::COUNTERSTRIKE,
 
- 		SpellID::CURE,
 
- 		SpellID::FIRE_SHIELD,
 
- 		SpellID::FORTUNE,
 
- 		SpellID::HASTE,
 
- 		SpellID::MAGIC_MIRROR,
 
- 		SpellID::MIRTH,
 
- 		SpellID::PRAYER,
 
- 		SpellID::PRECISION,
 
- 		SpellID::PROTECTION_FROM_AIR,
 
- 		SpellID::PROTECTION_FROM_EARTH,
 
- 		SpellID::PROTECTION_FROM_FIRE,
 
- 		SpellID::PROTECTION_FROM_WATER,
 
- 		SpellID::SHIELD,
 
- 		SpellID::SLAYER,
 
- 		SpellID::STONE_SKIN
 
- 	};
 
- 	std::vector<SpellID> beneficialSpells;
 
- 	auto getAliveEnemy = [=](const std::function<bool(const CStack * )> & pred)
 
- 	{
 
- 		return getStackIf([=](const CStack * stack)
 
- 		{
 
- 			return pred(stack) && stack->owner != subject->owner && stack->alive();
 
- 		});
 
- 	};
 
- 	for(const SpellID spellID : allPossibleSpells)
 
- 	{
 
- 		std::stringstream cachingStr;
 
- 		cachingStr << "source_" << Bonus::SPELL_EFFECT << "id_" << spellID.num;
 
- 		if(subject->hasBonus(Selector::source(Bonus::SPELL_EFFECT, spellID), Selector::all, cachingStr.str())
 
- 			//TODO: this ability has special limitations
 
- 			|| battleCanCastThisSpellHere(subject, spellID.toSpell(), ECastingMode::CREATURE_ACTIVE_CASTING, subject->position) != ESpellCastProblem::OK)
 
- 			continue;
 
- 		switch (spellID)
 
- 		{
 
- 		case SpellID::SHIELD:
 
- 		case SpellID::FIRE_SHIELD: // not if all enemy units are shooters
 
- 			{
 
- 				auto walker = getAliveEnemy([&](const CStack * stack) //look for enemy, non-shooting stack
 
- 				{
 
- 					return !stack->shots;
 
- 				});
 
- 				if (!walker)
 
- 					continue;
 
- 			}
 
- 			break;
 
- 		case SpellID::AIR_SHIELD: //only against active shooters
 
- 			{
 
- 				auto shooter = getAliveEnemy([&](const CStack * stack) //look for enemy, non-shooting stack
 
- 				{
 
- 					return stack->hasBonusOfType(Bonus::SHOOTER) && stack->shots;
 
- 				});
 
- 				if (!shooter)
 
- 					continue;
 
- 			}
 
- 			break;
 
- 		case SpellID::ANTI_MAGIC:
 
- 		case SpellID::MAGIC_MIRROR:
 
- 		case SpellID::PROTECTION_FROM_AIR:
 
- 		case SpellID::PROTECTION_FROM_EARTH:
 
- 		case SpellID::PROTECTION_FROM_FIRE:
 
- 		case SpellID::PROTECTION_FROM_WATER:
 
- 			{
 
- 				const ui8 enemySide = (ui8)subject->attackerOwned;
 
- 				//todo: only if enemy has spellbook
 
- 				if (!battleHasHero(enemySide)) //only if there is enemy hero
 
- 					continue;
 
- 			}
 
- 			break;
 
- 		case SpellID::CURE: //only damaged units
 
- 			{
 
- 				//do not cast on affected by debuffs
 
- 				if (subject->firstHPleft >= subject->MaxHealth())
 
- 					continue;
 
- 			}
 
- 			break;
 
- 		case SpellID::BLOODLUST:
 
- 			{
 
- 				if (subject->shots) //if can shoot - only if enemy uits are adjacent
 
- 					continue;
 
- 			}
 
- 			break;
 
- 		case SpellID::PRECISION:
 
- 			{
 
- 				if (!(subject->hasBonusOfType(Bonus::SHOOTER) && subject->shots))
 
- 					continue;
 
- 			}
 
- 			break;
 
- 		case SpellID::SLAYER://only if monsters are present
 
- 			{
 
- 				auto kingMonster = getAliveEnemy([&](const CStack *stack) -> bool //look for enemy, non-shooting stack
 
- 				{
 
- 					const auto isKing = Selector::type(Bonus::KING1)
 
- 						.Or(Selector::type(Bonus::KING2))
 
- 						.Or(Selector::type(Bonus::KING3));
 
- 					return stack->hasBonus(isKing);
 
- 				});
 
- 				if (!kingMonster)
 
- 					continue;
 
- 			}
 
- 			break;
 
- 		}
 
- 		beneficialSpells.push_back(spellID);
 
- 	}
 
- 	if(!beneficialSpells.empty())
 
- 	{
 
- 		return *RandomGeneratorUtil::nextItem(beneficialSpells, rand);
 
- 	}
 
- 	else
 
- 	{
 
- 		return SpellID::NONE;
 
- 	}
 
- }
 
- SpellID CBattleInfoCallback::getRandomCastedSpell(CRandomGenerator & rand,const CStack * caster) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(SpellID::NONE);
 
- 	TBonusListPtr bl = caster->getBonuses(Selector::type(Bonus::SPELLCASTER));
 
- 	if (!bl->size())
 
- 		return SpellID::NONE;
 
- 	int totalWeight = 0;
 
- 	for(auto b : *bl)
 
- 	{
 
- 		totalWeight += std::max(b->additionalInfo, 1); //minimal chance to cast is 1
 
- 	}
 
- 	int randomPos = rand.nextInt(totalWeight - 1);
 
- 	for(auto b : *bl)
 
- 	{
 
- 		randomPos -= std::max(b->additionalInfo, 1);
 
- 		if(randomPos < 0)
 
- 		{
 
- 			return SpellID(b->subtype);
 
- 		}
 
- 	}
 
- 	return SpellID::NONE;
 
- }
 
- int CBattleInfoCallback::battleGetSurrenderCost(PlayerColor Player) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(-3);
 
- 	if(!battleCanSurrender(Player))
 
- 		return -1;
 
- 	const si8 playerSide = playerToSide(Player);
 
- 	if(playerSide < 0)
 
- 		return -1;
 
- 	int ret = 0;
 
- 	double discount = 0;
 
- 	for(const CStack *s : battleAliveStacks(playerSide))
 
- 		if(s->base) //we pay for our stack that comes from our army slots - condition eliminates summoned cres and war machines
 
- 			ret += s->getCreature()->cost[Res::GOLD] * s->count;
 
- 	if(const CGHeroInstance *h = battleGetFightingHero(playerSide))
 
- 		discount += h->valOfBonuses(Bonus::SURRENDER_DISCOUNT);
 
- 	ret *= (100.0 - discount) / 100.0;
 
- 	vstd::amax(ret, 0); //no negative costs for >100% discounts (impossible in original H3 mechanics, but some day...)
 
- 	return ret;
 
- }
 
- si8 CBattleInfoCallback::battleMaxSpellLevel(ui8 side) const
 
- {
 
- 	const IBonusBearer *node = nullptr;
 
- 	if(const CGHeroInstance * h = battleGetFightingHero(side))
 
- 		node = h;
 
- 	else
 
- 		node = getBattleNode();
 
- 	if(!node)
 
- 		return GameConstants::SPELL_LEVELS;
 
- 	//We can't "just get value" - it'd be 0 if there are bonuses (and all would be blocked)
 
- 	auto b = node->getBonuses(Selector::type(Bonus::BLOCK_MAGIC_ABOVE));
 
- 	if(b->size())
 
- 		return b->totalValue();
 
- 	return GameConstants::SPELL_LEVELS;
 
- }
 
- boost::optional<int> CBattleInfoCallback::battleIsFinished() const
 
- {
 
- 	auto stacks = battleGetAllStacks();
 
- 	//checking winning condition
 
- 	bool hasStack[2]; //hasStack[0] - true if attacker has a living stack; defender similarly
 
- 	hasStack[0] = hasStack[1] = false;
 
- 	for(auto & stack : stacks)
 
- 	{
 
- 		if(stack->alive() && !stack->hasBonusOfType(Bonus::SIEGE_WEAPON))
 
- 		{
 
- 			hasStack[1-stack->attackerOwned] = true;
 
- 		}
 
- 	}
 
- 	if(!hasStack[0] && !hasStack[1])
 
- 		return 2;
 
- 	if(!hasStack[1])
 
- 		return 0;
 
- 	if(!hasStack[0])
 
- 		return 1;
 
- 	return boost::none;
 
- }
 
- bool AccessibilityInfo::accessible(BattleHex tile, const CStack *stack) const
 
- {
 
- 	return accessible(tile, stack->doubleWide(), stack->attackerOwned);
 
- }
 
- bool AccessibilityInfo::accessible(BattleHex tile, bool doubleWide, bool attackerOwned) const
 
- {
 
- 	// All hexes that stack would cover if standing on tile have to be accessible.
 
- 	for(auto hex : CStack::getHexes(tile, doubleWide, attackerOwned))
 
- 	{
 
- 		// If the hex is out of range then the tile isn't accessible
 
- 		if(!hex.isValid())
 
- 			return false;
 
- 		// If we're no defender which step on gate and the hex isn't accessible, then the tile
 
- 		// isn't accessible
 
- 		else if(at(hex) != EAccessibility::ACCESSIBLE &&
 
- 			!(at(hex) == EAccessibility::GATE && !attackerOwned))
 
- 		{
 
- 			return false;
 
- 		}
 
- 	}
 
- 	return true;
 
- }
 
- bool AccessibilityInfo::occupiable(const CStack *stack, BattleHex tile) const
 
- {
 
- 	//obviously, we can occupy tile by standing on it
 
- 	if(accessible(tile, stack))
 
- 		return true;
 
- 	if(stack->doubleWide())
 
- 	{
 
- 		//Check the tile next to -> if stack stands there, it'll also occupy considered hex
 
- 		const BattleHex anotherTile = tile + (stack->attackerOwned ? BattleHex::RIGHT : BattleHex::LEFT);
 
- 		if(accessible(anotherTile, stack))
 
- 			return true;
 
- 	}
 
- 	return false;
 
- }
 
- ReachabilityInfo::Parameters::Parameters()
 
- {
 
- 	stack = nullptr;
 
- 	perspective = BattlePerspective::ALL_KNOWING;
 
- 	attackerOwned = doubleWide = flying = false;
 
- }
 
- ReachabilityInfo::Parameters::Parameters(const CStack *Stack)
 
- {
 
- 	stack = Stack;
 
- 	perspective = (BattlePerspective::BattlePerspective)(!Stack->attackerOwned);
 
- 	startPosition = Stack->position;
 
- 	doubleWide = stack->doubleWide();
 
- 	attackerOwned = stack->attackerOwned;
 
- 	flying = stack->hasBonusOfType(Bonus::FLYING);
 
- 	knownAccessible = stack->getHexes();
 
- }
 
- ESpellCastProblem::ESpellCastProblem CPlayerBattleCallback::battleCanCastThisSpell(const CSpell * spell) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(ESpellCastProblem::INVALID);
 
- 	ASSERT_IF_CALLED_WITH_PLAYER
 
- 	const ISpellCaster * hero = battleGetMyHero();
 
- 	if(hero == nullptr)
 
- 		return ESpellCastProblem::INVALID;
 
- 	else
 
- 		return CBattleInfoCallback::battleCanCastThisSpell(hero, spell, ECastingMode::HERO_CASTING);
 
- }
 
- bool CPlayerBattleCallback::battleCanFlee() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	ASSERT_IF_CALLED_WITH_PLAYER
 
- 	return CBattleInfoEssentials::battleCanFlee(*player);
 
- }
 
- TStacks CPlayerBattleCallback::battleGetStacks(EStackOwnership whose /*= MINE_AND_ENEMY*/, bool onlyAlive /*= true*/) const
 
- {
 
- 	if(whose != MINE_AND_ENEMY)
 
- 	{
 
- 		ASSERT_IF_CALLED_WITH_PLAYER
 
- 	}
 
- 	return battleGetStacksIf([=](const CStack * s){
 
- 		const bool ownerMatches = (whose == MINE_AND_ENEMY)
 
- 			|| (whose == ONLY_MINE && s->owner == player)
 
- 			|| (whose == ONLY_ENEMY && s->owner != player);
 
- 		return ownerMatches && s->isValidTarget(!onlyAlive);
 
- 	});
 
- }
 
- int CPlayerBattleCallback::battleGetSurrenderCost() const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(-3)
 
- 	ASSERT_IF_CALLED_WITH_PLAYER
 
- 	return CBattleInfoCallback::battleGetSurrenderCost(*player);
 
- }
 
- bool CPlayerBattleCallback::battleCanCastSpell(ESpellCastProblem::ESpellCastProblem *outProblem /*= nullptr*/) const
 
- {
 
- 	RETURN_IF_NOT_BATTLE(false);
 
- 	ASSERT_IF_CALLED_WITH_PLAYER
 
- 	const CGHeroInstance * hero = battleGetMyHero();
 
- 	if(!hero)
 
- 	{
 
- 		if(outProblem)
 
- 			*outProblem = ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
 
- 		return false;
 
- 	}
 
- 	auto problem = CBattleInfoCallback::battleCanCastSpell(hero, ECastingMode::HERO_CASTING);
 
- 	if(outProblem)
 
- 		*outProblem = problem;
 
- 	return problem == ESpellCastProblem::OK;
 
- }
 
- const CGHeroInstance * CPlayerBattleCallback::battleGetMyHero() const
 
- {
 
- 	return CBattleInfoEssentials::battleGetFightingHero(battleGetMySide());
 
- }
 
- InfoAboutHero CPlayerBattleCallback::battleGetEnemyHero() const
 
- {
 
- 	return battleGetHeroInfo(!battleGetMySide());
 
- }
 
- BattleAttackInfo::BattleAttackInfo(const CStack *Attacker, const CStack *Defender, bool Shooting)
 
- {
 
- 	attacker = Attacker;
 
- 	defender = Defender;
 
- 	attackerBonuses = Attacker;
 
- 	defenderBonuses = Defender;
 
- 	attackerPosition = Attacker->position;
 
- 	defenderPosition = Defender->position;
 
- 	attackerCount = Attacker->count;
 
- 	defenderCount = Defender->count;
 
- 	shooting = Shooting;
 
- 	chargedFields = 0;
 
- 	luckyHit = false;
 
- 	unluckyHit = false;
 
- 	deathBlow = false;
 
- 	ballistaDoubleDamage = false;
 
- }
 
- BattleAttackInfo BattleAttackInfo::reverse() const
 
- {
 
- 	BattleAttackInfo ret = *this;
 
- 	std::swap(ret.attacker, ret.defender);
 
- 	std::swap(ret.attackerBonuses, ret.defenderBonuses);
 
- 	std::swap(ret.attackerPosition, ret.defenderPosition);
 
- 	std::swap(ret.attackerCount, ret.defenderCount);
 
- 	ret.shooting = false;
 
- 	ret.chargedFields = 0;
 
- 	ret.luckyHit = ret.ballistaDoubleDamage = ret.deathBlow = false;
 
- 	return ret;
 
- }
 
 
  |