|  | @@ -9,318 +9,58 @@
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  #include "StdInc.h"
 | 
	
		
			
				|  |  |  #include "CStack.h"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +#include <vstd/RNG.h>
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  #include "CGeneralTextHandler.h"
 | 
	
		
			
				|  |  |  #include "battle/BattleInfo.h"
 | 
	
		
			
				|  |  |  #include "spells/CSpellHandler.h"
 | 
	
		
			
				|  |  | -#include "CRandomGenerator.h"
 | 
	
		
			
				|  |  |  #include "NetPacks.h"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -///CAmmo
 | 
	
		
			
				|  |  | -CAmmo::CAmmo(const CStack * Owner, CSelector totalSelector):
 | 
	
		
			
				|  |  | -	CStackResource(Owner), totalProxy(Owner, totalSelector)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int32_t CAmmo::available() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return total() - used;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CAmmo::canUse(int32_t amount) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return available() - amount >= 0;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CAmmo::reset()
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	used = 0;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int32_t CAmmo::total() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return totalProxy->totalValue();
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CAmmo::use(int32_t amount)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	if(available() - amount < 0)
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		logGlobal->error("Stack ammo overuse");
 | 
	
		
			
				|  |  | -		used += available();
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -		used += amount;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -///CShots
 | 
	
		
			
				|  |  | -CShots::CShots(const CStack * Owner):
 | 
	
		
			
				|  |  | -	CAmmo(Owner, Selector::type(Bonus::SHOTS))
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CShots::use(int32_t amount)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	//don't remove ammo if we control a working ammo cart
 | 
	
		
			
				|  |  | -	bool hasAmmoCart = false;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	for(const CStack * st : owner->battle->stacks)
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		if(owner->battle->battleMatchOwner(st, owner, true) && st->getCreature()->idNumber == CreatureID::AMMO_CART && st->alive())
 | 
	
		
			
				|  |  | -		{
 | 
	
		
			
				|  |  | -			hasAmmoCart = true;
 | 
	
		
			
				|  |  | -			break;
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	if(!hasAmmoCart)
 | 
	
		
			
				|  |  | -		CAmmo::use(amount);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -///CCasts
 | 
	
		
			
				|  |  | -CCasts::CCasts(const CStack * Owner):
 | 
	
		
			
				|  |  | -	CAmmo(Owner, Selector::type(Bonus::CASTS))
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -///CRetaliations
 | 
	
		
			
				|  |  | -CRetaliations::CRetaliations(const CStack * Owner):
 | 
	
		
			
				|  |  | -	CAmmo(Owner, Selector::type(Bonus::ADDITIONAL_RETALIATION)), totalCache(0)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int32_t CRetaliations::total() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	//after dispell bonus should remain during current round
 | 
	
		
			
				|  |  | -	int32_t val = 1 + totalProxy->totalValue();
 | 
	
		
			
				|  |  | -	vstd::amax(totalCache, val);
 | 
	
		
			
				|  |  | -	return totalCache;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CRetaliations::reset()
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	CAmmo::reset();
 | 
	
		
			
				|  |  | -	totalCache = 0;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -///CHealth
 | 
	
		
			
				|  |  | -CHealth::CHealth(const IUnitHealthInfo * Owner):
 | 
	
		
			
				|  |  | -	owner(Owner)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	reset();
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -CHealth::CHealth(const CHealth & other):
 | 
	
		
			
				|  |  | -	owner(other.owner),
 | 
	
		
			
				|  |  | -	firstHPleft(other.firstHPleft),
 | 
	
		
			
				|  |  | -	fullUnits(other.fullUnits),
 | 
	
		
			
				|  |  | -	resurrected(other.resurrected)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CHealth::init()
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	reset();
 | 
	
		
			
				|  |  | -	fullUnits = owner->unitBaseAmount() > 1 ? owner->unitBaseAmount() - 1 : 0;
 | 
	
		
			
				|  |  | -	firstHPleft = owner->unitBaseAmount() > 0 ? owner->unitMaxHealth() : 0;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CHealth::addResurrected(int32_t amount)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	resurrected += amount;
 | 
	
		
			
				|  |  | -	vstd::amax(resurrected, 0);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int64_t CHealth::available() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return static_cast<int64_t>(firstHPleft) + owner->unitMaxHealth() * fullUnits;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int64_t CHealth::total() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return static_cast<int64_t>(owner->unitMaxHealth()) * owner->unitBaseAmount();
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CHealth::damage(int32_t & amount)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	const int32_t oldCount = getCount();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	const bool withKills = amount >= firstHPleft;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	if(withKills)
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		int64_t totalHealth = available();
 | 
	
		
			
				|  |  | -		if(amount > totalHealth)
 | 
	
		
			
				|  |  | -			amount = totalHealth;
 | 
	
		
			
				|  |  | -		totalHealth -= amount;
 | 
	
		
			
				|  |  | -		if(totalHealth <= 0)
 | 
	
		
			
				|  |  | -		{
 | 
	
		
			
				|  |  | -			fullUnits = 0;
 | 
	
		
			
				|  |  | -			firstHPleft = 0;
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -		else
 | 
	
		
			
				|  |  | -		{
 | 
	
		
			
				|  |  | -			setFromTotal(totalHealth);
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		firstHPleft -= amount;
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	addResurrected(getCount() - oldCount);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CHealth::heal(int32_t & amount, EHealLevel level, EHealPower power)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	const int32_t unitHealth = owner->unitMaxHealth();
 | 
	
		
			
				|  |  | -	const int32_t oldCount = getCount();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	int32_t maxHeal = std::numeric_limits<int32_t>::max();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	switch(level)
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -	case EHealLevel::HEAL:
 | 
	
		
			
				|  |  | -		maxHeal = std::max(0, unitHealth - firstHPleft);
 | 
	
		
			
				|  |  | -		break;
 | 
	
		
			
				|  |  | -	case EHealLevel::RESURRECT:
 | 
	
		
			
				|  |  | -		maxHeal = total() - available();
 | 
	
		
			
				|  |  | -		break;
 | 
	
		
			
				|  |  | -	default:
 | 
	
		
			
				|  |  | -		assert(level == EHealLevel::OVERHEAL);
 | 
	
		
			
				|  |  | -		break;
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	vstd::amax(maxHeal, 0);
 | 
	
		
			
				|  |  | -	vstd::abetween(amount, 0, maxHeal);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	if(amount == 0)
 | 
	
		
			
				|  |  | -		return;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	int64_t availableHealth = available();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	availableHealth	+= amount;
 | 
	
		
			
				|  |  | -	setFromTotal(availableHealth);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	if(power == EHealPower::ONE_BATTLE)
 | 
	
		
			
				|  |  | -		addResurrected(getCount() - oldCount);
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -		assert(power == EHealPower::PERMANENT);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CHealth::setFromTotal(const int64_t totalHealth)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	const int32_t unitHealth = owner->unitMaxHealth();
 | 
	
		
			
				|  |  | -	firstHPleft = totalHealth % unitHealth;
 | 
	
		
			
				|  |  | -	fullUnits = totalHealth / unitHealth;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	if(firstHPleft == 0 && fullUnits >= 1)
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		firstHPleft = unitHealth;
 | 
	
		
			
				|  |  | -		fullUnits -= 1;
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CHealth::reset()
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	fullUnits = 0;
 | 
	
		
			
				|  |  | -	firstHPleft = 0;
 | 
	
		
			
				|  |  | -	resurrected = 0;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int32_t CHealth::getCount() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return fullUnits + (firstHPleft > 0 ? 1 : 0);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int32_t CHealth::getFirstHPleft() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return firstHPleft;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int32_t CHealth::getResurrected() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return resurrected;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CHealth::fromInfo(const CHealthInfo & info)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	firstHPleft = info.firstHPleft;
 | 
	
		
			
				|  |  | -	fullUnits = info.fullUnits;
 | 
	
		
			
				|  |  | -	resurrected = info.resurrected;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CHealth::toInfo(CHealthInfo & info) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	info.firstHPleft = firstHPleft;
 | 
	
		
			
				|  |  | -	info.fullUnits = fullUnits;
 | 
	
		
			
				|  |  | -	info.resurrected = resurrected;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -void CHealth::takeResurrected()
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	if(resurrected != 0)
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		int64_t totalHealth = available();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		totalHealth -= resurrected * owner->unitMaxHealth();
 | 
	
		
			
				|  |  | -		vstd::amax(totalHealth, 0);
 | 
	
		
			
				|  |  | -		setFromTotal(totalHealth);
 | 
	
		
			
				|  |  | -		resurrected = 0;
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  ///CStack
 | 
	
		
			
				|  |  | -CStack::CStack(const CStackInstance * Base, PlayerColor O, int I, ui8 Side, SlotID S):
 | 
	
		
			
				|  |  | -	base(Base), ID(I), owner(O), slot(S), side(Side),
 | 
	
		
			
				|  |  | -	counterAttacks(this), shots(this), casts(this), health(this), cloneID(-1),
 | 
	
		
			
				|  |  | -	position()
 | 
	
		
			
				|  |  | +CStack::CStack(const CStackInstance * Base, PlayerColor O, int I, ui8 Side, SlotID S)
 | 
	
		
			
				|  |  | +	: CBonusSystemNode(STACK_BATTLE),
 | 
	
		
			
				|  |  | +	CUnitState(),
 | 
	
		
			
				|  |  | +	base(Base),
 | 
	
		
			
				|  |  | +	ID(I),
 | 
	
		
			
				|  |  | +	type(Base->type),
 | 
	
		
			
				|  |  | +	baseAmount(base->count),
 | 
	
		
			
				|  |  | +	owner(O),
 | 
	
		
			
				|  |  | +	slot(S),
 | 
	
		
			
				|  |  | +	side(Side),
 | 
	
		
			
				|  |  | +	initialPosition()
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	assert(base);
 | 
	
		
			
				|  |  | -	type = base->type;
 | 
	
		
			
				|  |  | -	baseAmount = base->count;
 | 
	
		
			
				|  |  |  	health.init(); //???
 | 
	
		
			
				|  |  | -	setNodeType(STACK_BATTLE);
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -CStack::CStack():
 | 
	
		
			
				|  |  | -	counterAttacks(this), shots(this), casts(this), health(this)
 | 
	
		
			
				|  |  | +CStack::CStack()
 | 
	
		
			
				|  |  | +	: CBonusSystemNode(STACK_BATTLE),
 | 
	
		
			
				|  |  | +	CUnitState()
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	init();
 | 
	
		
			
				|  |  | -	setNodeType(STACK_BATTLE);
 | 
	
		
			
				|  |  | +	base = nullptr;
 | 
	
		
			
				|  |  | +	type = nullptr;
 | 
	
		
			
				|  |  | +	ID = -1;
 | 
	
		
			
				|  |  | +	baseAmount = -1;
 | 
	
		
			
				|  |  | +	owner = PlayerColor::NEUTRAL;
 | 
	
		
			
				|  |  | +	slot = SlotID(255);
 | 
	
		
			
				|  |  | +	side = 1;
 | 
	
		
			
				|  |  | +	initialPosition = BattleHex();
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -CStack::CStack(const CStackBasicDescriptor * stack, PlayerColor O, int I, ui8 Side, SlotID S):
 | 
	
		
			
				|  |  | -	base(nullptr), ID(I), owner(O), slot(S), side(Side),
 | 
	
		
			
				|  |  | -	counterAttacks(this), shots(this), casts(this), health(this), cloneID(-1),
 | 
	
		
			
				|  |  | -	position()
 | 
	
		
			
				|  |  | +CStack::CStack(const CStackBasicDescriptor * stack, PlayerColor O, int I, ui8 Side, SlotID S)
 | 
	
		
			
				|  |  | +	: CBonusSystemNode(STACK_BATTLE),
 | 
	
		
			
				|  |  | +	CUnitState(),
 | 
	
		
			
				|  |  | +	base(nullptr),
 | 
	
		
			
				|  |  | +	ID(I),
 | 
	
		
			
				|  |  | +	type(stack->type),
 | 
	
		
			
				|  |  | +	baseAmount(stack->count),
 | 
	
		
			
				|  |  | +	owner(O),
 | 
	
		
			
				|  |  | +	slot(S),
 | 
	
		
			
				|  |  | +	side(Side),
 | 
	
		
			
				|  |  | +	initialPosition()
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	type = stack->type;
 | 
	
		
			
				|  |  | -	baseAmount = stack->count;
 | 
	
		
			
				|  |  |  	health.init(); //???
 | 
	
		
			
				|  |  | -	setNodeType(STACK_BATTLE);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int32_t CStack::getKilled() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	int32_t res = baseAmount - health.getCount() + health.getResurrected();
 | 
	
		
			
				|  |  | -	vstd::amax(res, 0);
 | 
	
		
			
				|  |  | -	return res;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int32_t CStack::getCount() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return health.getCount();
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int32_t CStack::getFirstHPleft() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return health.getFirstHPleft();
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  const CCreature * CStack::getCreature() const
 | 
	
	
		
			
				|  | @@ -328,23 +68,9 @@ const CCreature * CStack::getCreature() const
 | 
	
		
			
				|  |  |  	return type;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::init()
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	base = nullptr;
 | 
	
		
			
				|  |  | -	type = nullptr;
 | 
	
		
			
				|  |  | -	ID = -1;
 | 
	
		
			
				|  |  | -	baseAmount = -1;
 | 
	
		
			
				|  |  | -	owner = PlayerColor::NEUTRAL;
 | 
	
		
			
				|  |  | -	slot = SlotID(255);
 | 
	
		
			
				|  |  | -	side = 1;
 | 
	
		
			
				|  |  | -	position = BattleHex();
 | 
	
		
			
				|  |  | -	cloneID = -1;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  void CStack::localInit(BattleInfo * battleInfo)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |  	battle = battleInfo;
 | 
	
		
			
				|  |  | -	cloneID = -1;
 | 
	
		
			
				|  |  |  	assert(type);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	exportBonuses();
 | 
	
	
		
			
				|  | @@ -359,183 +85,33 @@ void CStack::localInit(BattleInfo * battleInfo)
 | 
	
		
			
				|  |  |  		attachTo(const_cast<CCreature *>(type));
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	shots.reset();
 | 
	
		
			
				|  |  | -	counterAttacks.reset();
 | 
	
		
			
				|  |  | -	casts.reset();
 | 
	
		
			
				|  |  | -	health.init();
 | 
	
		
			
				|  |  | +	CUnitState::localInit(this);
 | 
	
		
			
				|  |  | +	position = initialPosition;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  ui32 CStack::level() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |  	if(base)
 | 
	
		
			
				|  |  | -		return base->getLevel(); //creatture or commander
 | 
	
		
			
				|  |  | +		return base->getLevel(); //creature or commander
 | 
	
		
			
				|  |  |  	else
 | 
	
		
			
				|  |  |  		return std::max(1, (int)getCreature()->level); //war machine, clone etc
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  si32 CStack::magicResistance() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	si32 magicResistance;
 | 
	
		
			
				|  |  | -	if(base)  //TODO: make war machines receive aura of magic resistance
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		magicResistance = base->magicResistance();
 | 
	
		
			
				|  |  | -		int auraBonus = 0;
 | 
	
		
			
				|  |  | -		for(const CStack * stack : base->armyObj->battle-> batteAdjacentCreatures(this))
 | 
	
		
			
				|  |  | -		{
 | 
	
		
			
				|  |  | -			if(stack->owner == owner)
 | 
	
		
			
				|  |  | -			{
 | 
	
		
			
				|  |  | -				vstd::amax(auraBonus, stack->valOfBonuses(Bonus::SPELL_RESISTANCE_AURA)); //max value
 | 
	
		
			
				|  |  | -			}
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -		magicResistance += auraBonus;
 | 
	
		
			
				|  |  | -		vstd::amin(magicResistance, 100);
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -		magicResistance = type->magicResistance();
 | 
	
		
			
				|  |  | -	return magicResistance;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::willMove(int turn) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return (turn ? true : !vstd::contains(state, EBattleStackState::DEFENDING))
 | 
	
		
			
				|  |  | -		   && !moved(turn)
 | 
	
		
			
				|  |  | -		   && canMove(turn);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::canMove(int turn) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return alive()
 | 
	
		
			
				|  |  | -		   && !hasBonus(Selector::type(Bonus::NOT_ACTIVE).And(Selector::turns(turn))); //eg. Ammo Cart or blinded creature
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::canCast() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return casts.canUse(1);//do not check specific cast abilities here
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::isCaster() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return casts.total() > 0;//do not check specific cast abilities here
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::canShoot() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return shots.canUse(1) && hasBonusOfType(Bonus::SHOOTER);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::isShooter() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return shots.total() > 0 && hasBonusOfType(Bonus::SHOOTER);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::moved(int turn) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	if(!turn)
 | 
	
		
			
				|  |  | -		return vstd::contains(state, EBattleStackState::MOVED);
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -		return false;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::waited(int turn) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	if(!turn)
 | 
	
		
			
				|  |  | -		return vstd::contains(state, EBattleStackState::WAITING);
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -		return false;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::doubleWide() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return getCreature()->doubleWide;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -BattleHex CStack::occupiedHex() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return occupiedHex(position);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -BattleHex CStack::occupiedHex(BattleHex assumedPos) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	if(doubleWide())
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		if(side == BattleSide::ATTACKER)
 | 
	
		
			
				|  |  | -			return assumedPos - 1;
 | 
	
		
			
				|  |  | -		else
 | 
	
		
			
				|  |  | -			return assumedPos + 1;
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		return BattleHex::INVALID;
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -std::vector<BattleHex> CStack::getHexes() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return getHexes(position);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return getHexes(assumedPos, doubleWide(), side);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +	si32 magicResistance = IBonusBearer::magicResistance();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos, bool twoHex, ui8 side)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	std::vector<BattleHex> hexes;
 | 
	
		
			
				|  |  | -	hexes.push_back(assumedPos);
 | 
	
		
			
				|  |  | +	si32 auraBonus = 0;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	if(twoHex)
 | 
	
		
			
				|  |  | +	for(auto one : battle->battleAdjacentUnits(this))
 | 
	
		
			
				|  |  |  	{
 | 
	
		
			
				|  |  | -		if(side == BattleSide::ATTACKER)
 | 
	
		
			
				|  |  | -			hexes.push_back(assumedPos - 1);
 | 
	
		
			
				|  |  | -		else
 | 
	
		
			
				|  |  | -			hexes.push_back(assumedPos + 1);
 | 
	
		
			
				|  |  | +		if(one->unitOwner() == owner)
 | 
	
		
			
				|  |  | +			vstd::amax(auraBonus, one->valOfBonuses(Bonus::SPELL_RESISTANCE_AURA)); //max value
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | +	magicResistance += auraBonus;
 | 
	
		
			
				|  |  | +	vstd::amin(magicResistance, 100);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	return hexes;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::coversPos(BattleHex pos) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return vstd::contains(getHexes(), pos);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -std::vector<BattleHex> CStack::getSurroundingHexes(BattleHex attackerPos) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	BattleHex hex = (attackerPos != BattleHex::INVALID) ? attackerPos : position; //use hypothetical position
 | 
	
		
			
				|  |  | -	std::vector<BattleHex> hexes;
 | 
	
		
			
				|  |  | -	if(doubleWide())
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		const int WN = GameConstants::BFIELD_WIDTH;
 | 
	
		
			
				|  |  | -		if(side == BattleSide::ATTACKER)
 | 
	
		
			
				|  |  | -		{
 | 
	
		
			
				|  |  | -			//position is equal to front hex
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN + 2 : WN + 1), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN + 1 : WN), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN : WN - 1), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex - 2, hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex + 1, hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN - 2 : WN - 1), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN - 1 : WN), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN : WN + 1), hexes);
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -		else
 | 
	
		
			
				|  |  | -		{
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN + 1 : WN), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN : WN - 1), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex - ((hex / WN) % 2 ? WN - 1 : WN - 2), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex + 2, hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex - 1, hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN - 1 : WN), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN : WN + 1), hexes);
 | 
	
		
			
				|  |  | -			BattleHex::checkAndPush(hex + ((hex / WN) % 2 ? WN + 1 : WN + 2), hexes);
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -		return hexes;
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		return hex.neighbouringTiles();
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | +	return magicResistance;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  BattleHex::EDir CStack::destShiftDir() const
 | 
	
	
		
			
				|  | @@ -595,7 +171,8 @@ const CGHeroInstance * CStack::getMyHero() const
 | 
	
		
			
				|  |  |  std::string CStack::nodeName() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |  	std::ostringstream oss;
 | 
	
		
			
				|  |  | -	oss << "Battle stack [" << ID << "]: " << health.getCount() << " creatures of ";
 | 
	
		
			
				|  |  | +	oss << owner.getStr();
 | 
	
		
			
				|  |  | +	oss << " battle stack [" << ID << "]: " << getCount() << " of ";
 | 
	
		
			
				|  |  |  	if(type)
 | 
	
		
			
				|  |  |  		oss << type->namePl;
 | 
	
		
			
				|  |  |  	else
 | 
	
	
		
			
				|  | @@ -607,156 +184,95 @@ std::string CStack::nodeName() const
 | 
	
		
			
				|  |  |  	return oss.str();
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -CHealth CStack::healthAfterAttacked(int32_t & damage) const
 | 
	
		
			
				|  |  | +void CStack::prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand) const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	return healthAfterAttacked(damage, health);
 | 
	
		
			
				|  |  | +	auto newState = acquireState();
 | 
	
		
			
				|  |  | +	prepareAttacked(bsa, rand, newState);
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -CHealth CStack::healthAfterAttacked(int32_t & damage, const CHealth & customHealth) const
 | 
	
		
			
				|  |  | +void CStack::prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand, std::shared_ptr<battle::CUnitState> customState)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	CHealth res = customHealth;
 | 
	
		
			
				|  |  | +	auto initialCount = customState->getCount();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	customState->damage(bsa.damageAmount);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	if(isClone())
 | 
	
		
			
				|  |  | +	bsa.killedAmount = initialCount - customState->getCount();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if(!customState->alive() && customState->isClone())
 | 
	
		
			
				|  |  |  	{
 | 
	
		
			
				|  |  | -		// block ability should not kill clone (0 damage)
 | 
	
		
			
				|  |  | -		if(damage > 0)
 | 
	
		
			
				|  |  | -		{
 | 
	
		
			
				|  |  | -			damage = 1;//??? what should be actual damage against clone?
 | 
	
		
			
				|  |  | -			res.reset();
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | +		bsa.flags |= BattleStackAttacked::CLONE_KILLED;
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | +	else if(!customState->alive()) //stack killed
 | 
	
		
			
				|  |  |  	{
 | 
	
		
			
				|  |  | -		res.damage(damage);
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	return res;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -CHealth CStack::healthAfterHealed(int32_t & toHeal, EHealLevel level, EHealPower power) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	CHealth res = health;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	if(level == EHealLevel::HEAL && power == EHealPower::ONE_BATTLE)
 | 
	
		
			
				|  |  | -		logGlobal->error("Heal for one battle does not make sense", nodeName(), toHeal);
 | 
	
		
			
				|  |  | -	else if(isClone())
 | 
	
		
			
				|  |  | -		logGlobal->error("Attempt to heal clone: %s for %d HP", nodeName(), toHeal);
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -		res.heal(toHeal, level, power);
 | 
	
		
			
				|  |  | +		bsa.flags |= BattleStackAttacked::KILLED;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	return res;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +		auto resurrectValue = customState->valOfBonuses(Bonus::REBIRTH);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	prepareAttacked(bsa, rand, health);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +		if(resurrectValue > 0 && customState->canCast()) //there must be casts left
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			double resurrectFactor = resurrectValue / 100;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand, const CHealth & customHealth) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	CHealth afterAttack = healthAfterAttacked(bsa.damageAmount, customHealth);
 | 
	
		
			
				|  |  | +			auto baseAmount = customState->unitBaseAmount();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	bsa.killedAmount = customHealth.getCount() - afterAttack.getCount();
 | 
	
		
			
				|  |  | -	afterAttack.toInfo(bsa.newHealth);
 | 
	
		
			
				|  |  | -	bsa.newHealth.stackId = ID;
 | 
	
		
			
				|  |  | -	bsa.newHealth.delta = -bsa.damageAmount;
 | 
	
		
			
				|  |  | +			double resurrectedRaw = baseAmount * resurrectFactor;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	if(afterAttack.available() <= 0 && isClone())
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		bsa.flags |= BattleStackAttacked::CLONE_KILLED;
 | 
	
		
			
				|  |  | -		return; // no rebirth I believe
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | +			int32_t resurrectedCount = static_cast<int32_t>(floor(resurrectedRaw));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	if(afterAttack.available() <= 0) //stack killed
 | 
	
		
			
				|  |  | -	{
 | 
	
		
			
				|  |  | -		bsa.flags |= BattleStackAttacked::KILLED;
 | 
	
		
			
				|  |  | +			int32_t resurrectedAdd = static_cast<int32_t>(baseAmount - (resurrectedCount/resurrectFactor));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -		int resurrectFactor = valOfBonuses(Bonus::REBIRTH);
 | 
	
		
			
				|  |  | -		if(resurrectFactor > 0 && canCast()) //there must be casts left
 | 
	
		
			
				|  |  | -		{
 | 
	
		
			
				|  |  | -			int resurrectedStackCount = baseAmount * resurrectFactor / 100;
 | 
	
		
			
				|  |  | +			auto rangeGen = rand.getInt64Range(0, 99);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -			// last stack has proportional chance to rebirth
 | 
	
		
			
				|  |  | -			//FIXME: diff is always 0
 | 
	
		
			
				|  |  | -			auto diff = baseAmount * resurrectFactor / 100.0 - resurrectedStackCount;
 | 
	
		
			
				|  |  | -			if(diff > rand.nextDouble(0, 0.99))
 | 
	
		
			
				|  |  | +			for(int32_t i = 0; i < resurrectedAdd; i++)
 | 
	
		
			
				|  |  |  			{
 | 
	
		
			
				|  |  | -				resurrectedStackCount += 1;
 | 
	
		
			
				|  |  | +				if(resurrectValue > rangeGen())
 | 
	
		
			
				|  |  | +					resurrectedCount += 1;
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -			if(hasBonusOfType(Bonus::REBIRTH, 1))
 | 
	
		
			
				|  |  | +			if(customState->hasBonusOfType(Bonus::REBIRTH, 1))
 | 
	
		
			
				|  |  |  			{
 | 
	
		
			
				|  |  |  				// resurrect at least one Sacred Phoenix
 | 
	
		
			
				|  |  | -				vstd::amax(resurrectedStackCount, 1);
 | 
	
		
			
				|  |  | +				vstd::amax(resurrectedCount, 1);
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -			if(resurrectedStackCount > 0)
 | 
	
		
			
				|  |  | +			if(resurrectedCount > 0)
 | 
	
		
			
				|  |  |  			{
 | 
	
		
			
				|  |  | +				customState->casts.use();
 | 
	
		
			
				|  |  |  				bsa.flags |= BattleStackAttacked::REBIRTH;
 | 
	
		
			
				|  |  | -				//TODO: use StackHealedOrResurrected
 | 
	
		
			
				|  |  | -				bsa.newHealth.firstHPleft = MaxHealth();
 | 
	
		
			
				|  |  | -				bsa.newHealth.fullUnits = resurrectedStackCount - 1;
 | 
	
		
			
				|  |  | -				bsa.newHealth.resurrected = 0; //TODO: add one-battle rebirth?
 | 
	
		
			
				|  |  | +				int64_t toHeal = customState->MaxHealth() * resurrectedCount;
 | 
	
		
			
				|  |  | +				//TODO: add one-battle rebirth?
 | 
	
		
			
				|  |  | +				customState->heal(toHeal, EHealLevel::RESURRECT, EHealPower::PERMANENT);
 | 
	
		
			
				|  |  | +				customState->counterAttacks.use(customState->counterAttacks.available());
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	customState->save(bsa.newState.data);
 | 
	
		
			
				|  |  | +	bsa.newState.healthDelta = -bsa.damageAmount;
 | 
	
		
			
				|  |  | +	bsa.newState.id = customState->unitId();
 | 
	
		
			
				|  |  | +	bsa.newState.operation = UnitChanges::EOperation::RESET_STATE;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -bool CStack::isMeleeAttackPossible(const CStack * attacker, const CStack * defender, BattleHex attackerPos, BattleHex defenderPos)
 | 
	
		
			
				|  |  | +bool CStack::isMeleeAttackPossible(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos, BattleHex defenderPos)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |  	if(!attackerPos.isValid())
 | 
	
		
			
				|  |  | -		attackerPos = attacker->position;
 | 
	
		
			
				|  |  | +		attackerPos = attacker->getPosition();
 | 
	
		
			
				|  |  |  	if(!defenderPos.isValid())
 | 
	
		
			
				|  |  | -		defenderPos = defender->position;
 | 
	
		
			
				|  |  | +		defenderPos = defender->getPosition();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	return
 | 
	
		
			
				|  |  |  		(BattleHex::mutualPosition(attackerPos, defenderPos) >= 0)//front <=> front
 | 
	
		
			
				|  |  |  		|| (attacker->doubleWide()//back <=> front
 | 
	
		
			
				|  |  | -			&& BattleHex::mutualPosition(attackerPos + (attacker->side == BattleSide::ATTACKER ? -1 : 1), defenderPos) >= 0)
 | 
	
		
			
				|  |  | +			&& BattleHex::mutualPosition(attackerPos + (attacker->unitSide() == BattleSide::ATTACKER ? -1 : 1), defenderPos) >= 0)
 | 
	
		
			
				|  |  |  		|| (defender->doubleWide()//front <=> back
 | 
	
		
			
				|  |  | -			&& BattleHex::mutualPosition(attackerPos, defenderPos + (defender->side == BattleSide::ATTACKER ? -1 : 1)) >= 0)
 | 
	
		
			
				|  |  | +			&& BattleHex::mutualPosition(attackerPos, defenderPos + (defender->unitSide() == BattleSide::ATTACKER ? -1 : 1)) >= 0)
 | 
	
		
			
				|  |  |  		|| (defender->doubleWide() && attacker->doubleWide()//back <=> back
 | 
	
		
			
				|  |  | -			&& BattleHex::mutualPosition(attackerPos + (attacker->side == BattleSide::ATTACKER ? -1 : 1), defenderPos + (defender->side == BattleSide::ATTACKER ? -1 : 1)) >= 0);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +			&& BattleHex::mutualPosition(attackerPos + (attacker->unitSide() == BattleSide::ATTACKER ? -1 : 1), defenderPos + (defender->unitSide() == BattleSide::ATTACKER ? -1 : 1)) >= 0);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -bool CStack::ableToRetaliate() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return alive()
 | 
	
		
			
				|  |  | -		   && (counterAttacks.canUse() || hasBonusOfType(Bonus::UNLIMITED_RETALIATIONS))
 | 
	
		
			
				|  |  | -		   && !hasBonusOfType(Bonus::SIEGE_WEAPON)
 | 
	
		
			
				|  |  | -		   && !hasBonusOfType(Bonus::HYPNOTIZED)
 | 
	
		
			
				|  |  | -		   && !hasBonusOfType(Bonus::NO_RETALIATION);
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  std::string CStack::getName() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	return (health.getCount() == 1) ? type->nameSing : type->namePl; //War machines can't use base
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::isValidTarget(bool allowDead) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return (alive() || (allowDead && isDead())) && position.isValid() && !isTurret();
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::isDead() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return !alive() && !isGhost();
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::isClone() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return vstd::contains(state, EBattleStackState::CLONED);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::isGhost() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return vstd::contains(state, EBattleStackState::GHOST);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::isTurret() const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return type->idNumber == CreatureID::ARROW_TOWERS;
 | 
	
		
			
				|  |  | +	return (getCount() == 1) ? type->nameSing : type->namePl; //War machines can't use base
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  bool CStack::canBeHealed() const
 | 
	
	
		
			
				|  | @@ -766,122 +282,70 @@ bool CStack::canBeHealed() const
 | 
	
		
			
				|  |  |  		   && !hasBonusOfType(Bonus::SIEGE_WEAPON);
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::makeGhost()
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	state.erase(EBattleStackState::ALIVE);
 | 
	
		
			
				|  |  | -	state.insert(EBattleStackState::GHOST_PENDING);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -bool CStack::alive() const //determines if stack is alive
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return vstd::contains(state, EBattleStackState::ALIVE);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -ui8 CStack::getSpellSchoolLevel(const CSpell * spell, int * outSelectedSchool) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	int skill = valOfBonuses(Selector::typeSubtype(Bonus::SPELLCASTER, spell->id));
 | 
	
		
			
				|  |  | -	vstd::abetween(skill, 0, 3);
 | 
	
		
			
				|  |  | -	return skill;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -ui32 CStack::getSpellBonus(const CSpell * spell, ui32 base, const CStack * affectedStack) const
 | 
	
		
			
				|  |  | +const CCreature * CStack::unitType() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	//stacks does not have sorcery-like bonuses (yet?)
 | 
	
		
			
				|  |  | -	return base;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int CStack::getEffectLevel(const CSpell * spell) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return getSpellSchoolLevel(spell);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int CStack::getEffectPower(const CSpell * spell) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	return valOfBonuses(Bonus::CREATURE_SPELL_POWER) * health.getCount() / 100;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int CStack::getEnchantPower(const CSpell * spell) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	int res = valOfBonuses(Bonus::CREATURE_ENCHANT_POWER);
 | 
	
		
			
				|  |  | -	if(res <= 0)
 | 
	
		
			
				|  |  | -		res = 3;//default for creatures
 | 
	
		
			
				|  |  | -	return res;
 | 
	
		
			
				|  |  | +	return type;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -int CStack::getEffectValue(const CSpell * spell) const
 | 
	
		
			
				|  |  | +int32_t CStack::unitBaseAmount() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	return valOfBonuses(Bonus::SPECIFIC_SPELL_POWER, spell->id.toEnum()) * health.getCount();
 | 
	
		
			
				|  |  | +	return baseAmount;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -const PlayerColor CStack::getOwner() const
 | 
	
		
			
				|  |  | +bool CStack::unitHasAmmoCart(const battle::Unit * unit) const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	return battle->battleGetOwner(this);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +	bool hasAmmoCart = false;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::getCasterName(MetaString & text) const
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	//always plural name in case of spell cast.
 | 
	
		
			
				|  |  | -	addNameReplacement(text, true);
 | 
	
		
			
				|  |  | +	for(const CStack * st : battle->stacks)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		if(battle->battleMatchOwner(st, unit, true) && st->getCreature()->idNumber == CreatureID::AMMO_CART && st->alive())
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			hasAmmoCart = true;
 | 
	
		
			
				|  |  | +			break;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return hasAmmoCart;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::getCastDescription(const CSpell * spell, const std::vector<const CStack *> & attacked, MetaString & text) const
 | 
	
		
			
				|  |  | +PlayerColor CStack::unitEffectiveOwner(const battle::Unit * unit) const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	text.addTxt(MetaString::GENERAL_TXT, 565);//The %s casts %s
 | 
	
		
			
				|  |  | -	//todo: use text 566 for single creature
 | 
	
		
			
				|  |  | -	getCasterName(text);
 | 
	
		
			
				|  |  | -	text.addReplacement(MetaString::SPELL_NAME, spell->id.toEnum());
 | 
	
		
			
				|  |  | +	return battle->battleGetOwner(unit);
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -int32_t CStack::unitMaxHealth() const
 | 
	
		
			
				|  |  | +uint32_t CStack::unitId() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	return MaxHealth();
 | 
	
		
			
				|  |  | +	return ID;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -int32_t CStack::unitBaseAmount() const
 | 
	
		
			
				|  |  | +ui8 CStack::unitSide() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	return baseAmount;
 | 
	
		
			
				|  |  | +	return side;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::addText(MetaString & text, ui8 type, int32_t serial, const boost::logic::tribool & plural) const
 | 
	
		
			
				|  |  | +PlayerColor CStack::unitOwner() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	if(boost::logic::indeterminate(plural))
 | 
	
		
			
				|  |  | -		serial = VLC->generaltexth->pluralText(serial, health.getCount());
 | 
	
		
			
				|  |  | -	else if(plural)
 | 
	
		
			
				|  |  | -		serial = VLC->generaltexth->pluralText(serial, 2);
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -		serial = VLC->generaltexth->pluralText(serial, 1);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	text.addTxt(type, serial);
 | 
	
		
			
				|  |  | +	return owner;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) const
 | 
	
		
			
				|  |  | +SlotID CStack::unitSlot() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	if(boost::logic::indeterminate(plural))
 | 
	
		
			
				|  |  | -		text.addCreReplacement(type->idNumber, health.getCount());
 | 
	
		
			
				|  |  | -	else if(plural)
 | 
	
		
			
				|  |  | -		text.addReplacement(MetaString::CRE_PL_NAMES, type->idNumber.num);
 | 
	
		
			
				|  |  | -	else
 | 
	
		
			
				|  |  | -		text.addReplacement(MetaString::CRE_SING_NAMES, type->idNumber.num);
 | 
	
		
			
				|  |  | +	return slot;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -std::string CStack::formatGeneralMessage(const int32_t baseTextId) const
 | 
	
		
			
				|  |  | +std::string CStack::getDescription() const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	const int32_t textId = VLC->generaltexth->pluralText(baseTextId, health.getCount());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	MetaString text;
 | 
	
		
			
				|  |  | -	text.addTxt(MetaString::GENERAL_TXT, textId);
 | 
	
		
			
				|  |  | -	text.addCreReplacement(type->idNumber, health.getCount());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	return text.toString();
 | 
	
		
			
				|  |  | +	return nodeName();
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::setHealth(const CHealthInfo & value)
 | 
	
		
			
				|  |  | +void CStack::spendMana(const spells::PacketSender * server, const int spellCost) const
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	health.reset();
 | 
	
		
			
				|  |  | -	health.fromInfo(value);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +	if(spellCost != 1)
 | 
	
		
			
				|  |  | +		logGlobal->warn("Unexpected spell cost %d for creature", spellCost);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void CStack::setHealth(const CHealth & value)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -	health = value;
 | 
	
		
			
				|  |  | +	BattleSetStackProperty ssp;
 | 
	
		
			
				|  |  | +	ssp.stackID = unitId();
 | 
	
		
			
				|  |  | +	ssp.which = BattleSetStackProperty::CASTS;
 | 
	
		
			
				|  |  | +	ssp.val = -spellCost;
 | 
	
		
			
				|  |  | +	ssp.absolute = false;
 | 
	
		
			
				|  |  | +	server->sendAndApply(&ssp);
 | 
	
		
			
				|  |  |  }
 |