2017-03-17 17:48:44 +02:00
|
|
|
/*
|
|
|
|
* CStack.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CStack.h"
|
2017-07-04 13:24:46 +02:00
|
|
|
#include "CGeneralTextHandler.h"
|
2017-06-24 16:42:05 +02:00
|
|
|
#include "battle/BattleInfo.h"
|
2017-03-17 17:48:44 +02:00
|
|
|
#include "spells/CSpellHandler.h"
|
|
|
|
#include "CRandomGenerator.h"
|
|
|
|
#include "NetPacks.h"
|
|
|
|
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
///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
|
2017-07-09 18:49:52 +02:00
|
|
|
CHealth::CHealth(const IUnitHealthInfo * Owner):
|
2017-07-04 13:24:46 +02:00
|
|
|
owner(Owner)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
CHealth::CHealth(const CHealth & other):
|
|
|
|
owner(other.owner),
|
|
|
|
firstHPleft(other.firstHPleft),
|
|
|
|
fullUnits(other.fullUnits),
|
|
|
|
resurrected(other.resurrected)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-09 18:49:52 +02:00
|
|
|
void CHealth::init()
|
2017-07-04 13:24:46 +02:00
|
|
|
{
|
2017-07-09 14:49:37 +02:00
|
|
|
reset();
|
2017-07-09 18:49:52 +02:00
|
|
|
fullUnits = owner->unitBaseAmount() > 1 ? owner->unitBaseAmount() - 1 : 0;
|
|
|
|
firstHPleft = owner->unitBaseAmount() > 0 ? owner->unitMaxHealth() : 0;
|
2017-07-04 13:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CHealth::addResurrected(int32_t amount)
|
|
|
|
{
|
|
|
|
resurrected += amount;
|
|
|
|
vstd::amax(resurrected, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t CHealth::available() const
|
|
|
|
{
|
2017-07-09 18:49:52 +02:00
|
|
|
return static_cast<int64_t>(firstHPleft) + owner->unitMaxHealth() * fullUnits;
|
2017-07-04 13:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t CHealth::total() const
|
|
|
|
{
|
2017-07-09 18:49:52 +02:00
|
|
|
return static_cast<int64_t>(owner->unitMaxHealth()) * owner->unitBaseAmount();
|
2017-07-04 13:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-07-09 18:49:52 +02:00
|
|
|
const int32_t unitHealth = owner->unitMaxHealth();
|
2017-07-04 13:24:46 +02:00
|
|
|
const int32_t oldCount = getCount();
|
|
|
|
|
|
|
|
int32_t maxHeal = std::numeric_limits<int32_t>::max();
|
|
|
|
|
|
|
|
switch(level)
|
|
|
|
{
|
|
|
|
case EHealLevel::HEAL:
|
2017-07-09 14:49:37 +02:00
|
|
|
maxHeal = std::max(0, unitHealth - firstHPleft);
|
2017-07-04 13:24:46 +02:00
|
|
|
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 totalHealth = total();
|
|
|
|
|
|
|
|
totalHealth += amount;
|
|
|
|
setFromTotal(totalHealth);
|
|
|
|
|
|
|
|
if(power == EHealPower::ONE_BATTLE)
|
|
|
|
addResurrected(getCount() - oldCount);
|
|
|
|
else
|
|
|
|
assert(power == EHealPower::PERMANENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHealth::setFromTotal(const int64_t totalHealth)
|
|
|
|
{
|
2017-07-09 18:49:52 +02:00
|
|
|
const int32_t unitHealth = owner->unitMaxHealth();
|
2017-07-04 13:24:46 +02:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
int64_t totalHealth = total();
|
|
|
|
|
2017-07-09 18:49:52 +02:00
|
|
|
totalHealth -= resurrected * owner->unitMaxHealth();
|
2017-07-04 13:24:46 +02:00
|
|
|
vstd::amax(totalHealth, 0);
|
|
|
|
setFromTotal(totalHealth);
|
|
|
|
resurrected = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
///CStack
|
2017-07-09 14:49:37 +02:00
|
|
|
CStack::CStack(const CStackInstance * Base, PlayerColor O, int I, ui8 Side, SlotID S):
|
|
|
|
base(Base), ID(I), owner(O), slot(S), side(Side),
|
2017-07-04 13:24:46 +02:00
|
|
|
counterAttacks(this), shots(this), casts(this), health(this), cloneID(-1),
|
|
|
|
position()
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
assert(base);
|
|
|
|
type = base->type;
|
2017-07-04 13:24:46 +02:00
|
|
|
baseAmount = base->count;
|
2017-07-09 18:49:52 +02:00
|
|
|
health.init(); //???
|
2017-03-17 17:48:44 +02:00
|
|
|
setNodeType(STACK_BATTLE);
|
|
|
|
}
|
2017-07-04 13:24:46 +02:00
|
|
|
|
|
|
|
CStack::CStack():
|
|
|
|
counterAttacks(this), shots(this), casts(this), health(this)
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
setNodeType(STACK_BATTLE);
|
|
|
|
}
|
2017-07-04 13:24:46 +02:00
|
|
|
|
2017-07-09 14:49:37 +02:00
|
|
|
CStack::CStack(const CStackBasicDescriptor * stack, PlayerColor O, int I, ui8 Side, SlotID S):
|
|
|
|
base(nullptr), ID(I), owner(O), slot(S), side(Side),
|
2017-07-04 13:24:46 +02:00
|
|
|
counterAttacks(this), shots(this), casts(this), health(this), cloneID(-1),
|
|
|
|
position()
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
type = stack->type;
|
2017-07-04 13:24:46 +02:00
|
|
|
baseAmount = stack->count;
|
2017-07-09 18:49:52 +02:00
|
|
|
health.init(); //???
|
2017-03-17 17:48:44 +02:00
|
|
|
setNodeType(STACK_BATTLE);
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
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
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:48:44 +02:00
|
|
|
void CStack::init()
|
|
|
|
{
|
|
|
|
base = nullptr;
|
|
|
|
type = nullptr;
|
|
|
|
ID = -1;
|
2017-07-04 13:24:46 +02:00
|
|
|
baseAmount = -1;
|
2017-03-17 17:48:44 +02:00
|
|
|
owner = PlayerColor::NEUTRAL;
|
|
|
|
slot = SlotID(255);
|
2017-07-01 10:34:00 +02:00
|
|
|
side = 1;
|
2017-03-17 17:48:44 +02:00
|
|
|
position = BattleHex();
|
|
|
|
cloneID = -1;
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
void CStack::localInit(BattleInfo * battleInfo)
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
battle = battleInfo;
|
2017-07-09 18:49:52 +02:00
|
|
|
cloneID = -1;
|
2017-03-17 17:48:44 +02:00
|
|
|
assert(type);
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
exportBonuses();
|
|
|
|
if(base) //stack originating from "real" stack in garrison -> attach to it
|
|
|
|
{
|
|
|
|
attachTo(const_cast<CStackInstance *>(base));
|
|
|
|
}
|
|
|
|
else //attach directly to obj to which stack belongs and creature type
|
|
|
|
{
|
|
|
|
CArmedInstance * army = battle->battleGetArmyObject(side);
|
|
|
|
attachTo(army);
|
|
|
|
attachTo(const_cast<CCreature *>(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
shots.reset();
|
|
|
|
counterAttacks.reset();
|
|
|
|
casts.reset();
|
2017-07-09 18:49:52 +02:00
|
|
|
health.init();
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ui32 CStack::level() const
|
|
|
|
{
|
2017-07-09 14:49:37 +02:00
|
|
|
if(base)
|
2017-03-17 17:48:44 +02:00
|
|
|
return base->getLevel(); //creatture or commander
|
|
|
|
else
|
|
|
|
return std::max(1, (int)getCreature()->level); //war machine, clone etc
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 CStack::magicResistance() const
|
|
|
|
{
|
|
|
|
si32 magicResistance;
|
2017-07-09 14:49:37 +02:00
|
|
|
if(base) //TODO: make war machines receive aura of magic resistance
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
magicResistance = base->magicResistance();
|
|
|
|
int auraBonus = 0;
|
2017-07-09 14:49:37 +02:00
|
|
|
for(const CStack * stack : base->armyObj->battle-> batteAdjacentCreatures(this))
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
2017-07-09 14:49:37 +02:00
|
|
|
if(stack->owner == owner)
|
|
|
|
{
|
|
|
|
vstd::amax(auraBonus, stack->valOfBonuses(Bonus::SPELL_RESISTANCE_AURA)); //max value
|
|
|
|
}
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
magicResistance += auraBonus;
|
2017-07-09 14:49:37 +02:00
|
|
|
vstd::amin(magicResistance, 100);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
magicResistance = type->magicResistance();
|
|
|
|
return magicResistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::willMove(int turn /*= 0*/) const
|
|
|
|
{
|
2017-07-09 14:49:37 +02:00
|
|
|
return (turn ? true : !vstd::contains(state, EBattleStackState::DEFENDING))
|
|
|
|
&& !moved(turn)
|
|
|
|
&& canMove(turn);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:49:37 +02:00
|
|
|
bool CStack::canMove(int turn /*= 0*/) const
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
return alive()
|
2017-07-09 14:49:37 +02:00
|
|
|
&& !hasBonus(Selector::type(Bonus::NOT_ACTIVE).And(Selector::turns(turn))); //eg. Ammo Cart or blinded creature
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:49:37 +02:00
|
|
|
bool CStack::moved(int turn /*= 0*/) const
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
if(!turn)
|
|
|
|
return vstd::contains(state, EBattleStackState::MOVED);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::waited(int turn /*= 0*/) 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
|
|
|
|
{
|
2017-07-01 10:34:00 +02:00
|
|
|
if(doubleWide())
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
2017-07-01 10:34:00 +02:00
|
|
|
if(side == BattleSide::ATTACKER)
|
2017-03-17 17:48:44 +02:00
|
|
|
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
|
|
|
|
{
|
2017-07-01 10:34:00 +02:00
|
|
|
return getHexes(assumedPos, doubleWide(), side);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 10:34:00 +02:00
|
|
|
std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos, bool twoHex, ui8 side)
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
std::vector<BattleHex> hexes;
|
|
|
|
hexes.push_back(assumedPos);
|
|
|
|
|
2017-07-01 10:34:00 +02:00
|
|
|
if(twoHex)
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
2017-07-01 10:34:00 +02:00
|
|
|
if(side == BattleSide::ATTACKER)
|
2017-03-17 17:48:44 +02:00
|
|
|
hexes.push_back(assumedPos - 1);
|
|
|
|
else
|
|
|
|
hexes.push_back(assumedPos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-07-01 10:34:00 +02:00
|
|
|
if(doubleWide())
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
const int WN = GameConstants::BFIELD_WIDTH;
|
2017-07-01 10:34:00 +02:00
|
|
|
if(side == BattleSide::ATTACKER)
|
2017-07-09 14:49:37 +02:00
|
|
|
{
|
|
|
|
//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);
|
2017-03-17 17:48:44 +02:00
|
|
|
BattleHex::checkAndPush(hex - 2, hexes);
|
|
|
|
BattleHex::checkAndPush(hex + 1, hexes);
|
2017-07-09 14:49:37 +02:00
|
|
|
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);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-09 14:49:37 +02:00
|
|
|
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);
|
2017-03-17 17:48:44 +02:00
|
|
|
BattleHex::checkAndPush(hex + 2, hexes);
|
|
|
|
BattleHex::checkAndPush(hex - 1, hexes);
|
2017-07-09 14:49:37 +02:00
|
|
|
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);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
return hexes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return hex.neighbouringTiles();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 10:34:00 +02:00
|
|
|
BattleHex::EDir CStack::destShiftDir() const
|
|
|
|
{
|
|
|
|
if(doubleWide())
|
|
|
|
{
|
|
|
|
if(side == BattleSide::ATTACKER)
|
|
|
|
return BattleHex::EDir::RIGHT;
|
|
|
|
else
|
|
|
|
return BattleHex::EDir::LEFT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return BattleHex::EDir::NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:48:44 +02:00
|
|
|
std::vector<si32> CStack::activeSpells() const
|
|
|
|
{
|
|
|
|
std::vector<si32> ret;
|
|
|
|
|
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "!type_" << Bonus::NONE << "source_" << Bonus::SPELL_EFFECT;
|
|
|
|
CSelector selector = Selector::sourceType(Bonus::SPELL_EFFECT)
|
2017-07-09 14:49:37 +02:00
|
|
|
.And(CSelector([](const Bonus * b)->bool
|
|
|
|
{
|
|
|
|
return b->type != Bonus::NONE;
|
|
|
|
}));
|
2017-03-17 17:48:44 +02:00
|
|
|
|
|
|
|
TBonusListPtr spellEffects = getBonuses(selector, Selector::all, cachingStr.str());
|
|
|
|
for(const std::shared_ptr<Bonus> it : *spellEffects)
|
|
|
|
{
|
2017-07-09 14:49:37 +02:00
|
|
|
if(!vstd::contains(ret, it->sid)) //do not duplicate spells with multiple effects
|
2017-03-17 17:48:44 +02:00
|
|
|
ret.push_back(it->sid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CStack::~CStack()
|
|
|
|
{
|
|
|
|
detachFromAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * CStack::getMyHero() const
|
|
|
|
{
|
|
|
|
if(base)
|
|
|
|
return dynamic_cast<const CGHeroInstance *>(base->armyObj);
|
|
|
|
else //we are attached directly?
|
2017-07-09 14:49:37 +02:00
|
|
|
for(const CBonusSystemNode * n : getParentNodes())
|
2017-03-17 17:48:44 +02:00
|
|
|
if(n->getNodeType() == HERO)
|
|
|
|
return dynamic_cast<const CGHeroInstance *>(n);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CStack::nodeName() const
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
2017-07-04 13:24:46 +02:00
|
|
|
oss << "Battle stack [" << ID << "]: " << health.getCount() << " creatures of ";
|
2017-03-17 17:48:44 +02:00
|
|
|
if(type)
|
|
|
|
oss << type->namePl;
|
|
|
|
else
|
|
|
|
oss << "[UNDEFINED TYPE]";
|
|
|
|
|
|
|
|
oss << " from slot " << slot;
|
|
|
|
if(base && base->armyObj)
|
|
|
|
oss << " of armyobj=" << base->armyObj->id.getNum();
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
CHealth CStack::healthAfterAttacked(int32_t & damage) const
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
2017-07-09 18:49:52 +02:00
|
|
|
return healthAfterAttacked(damage, health);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHealth CStack::healthAfterAttacked(int32_t & damage, const CHealth & customHealth) const
|
|
|
|
{
|
|
|
|
CHealth res = customHealth;
|
|
|
|
|
|
|
|
if(isClone())
|
|
|
|
{
|
|
|
|
// block ability should not kill clone (0 damage)
|
|
|
|
if(damage > 0)
|
|
|
|
{
|
|
|
|
damage = 1;//??? what should be actual damage against clone?
|
|
|
|
res.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.damage(damage);
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
return res;
|
|
|
|
}
|
2017-03-17 17:48:44 +02:00
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
CHealth CStack::healthAfterHealed(int32_t & toHeal, EHealLevel level, EHealPower power) const
|
|
|
|
{
|
|
|
|
CHealth res = health;
|
2017-03-17 17:48:44 +02:00
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
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);
|
2017-03-17 17:48:44 +02:00
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
return res;
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
void CStack::prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand) const
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
prepareAttacked(bsa, rand, health);
|
|
|
|
}
|
2017-03-17 17:48:44 +02:00
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
void CStack::prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand, const CHealth & customHealth) const
|
|
|
|
{
|
2017-07-09 18:49:52 +02:00
|
|
|
CHealth afterAttack = healthAfterAttacked(bsa.damageAmount, customHealth);
|
2017-03-17 17:48:44 +02:00
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
bsa.killedAmount = customHealth.getCount() - afterAttack.getCount();
|
|
|
|
afterAttack.toInfo(bsa.newHealth);
|
2017-07-09 18:49:52 +02:00
|
|
|
bsa.newHealth.stackId = ID;
|
2017-07-04 13:24:46 +02:00
|
|
|
bsa.newHealth.delta = -bsa.damageAmount;
|
2017-03-17 17:48:44 +02:00
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
if(afterAttack.available() <= 0 && isClone())
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
bsa.flags |= BattleStackAttacked::CLONE_KILLED;
|
|
|
|
return; // no rebirth I believe
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
if(afterAttack.available() <= 0) //stack killed
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
bsa.flags |= BattleStackAttacked::KILLED;
|
|
|
|
|
|
|
|
int resurrectFactor = valOfBonuses(Bonus::REBIRTH);
|
2017-07-04 13:24:46 +02:00
|
|
|
if(resurrectFactor > 0 && canCast()) //there must be casts left
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
int resurrectedStackCount = baseAmount * resurrectFactor / 100;
|
2017-03-17 17:48:44 +02:00
|
|
|
|
|
|
|
// last stack has proportional chance to rebirth
|
2017-07-04 13:24:46 +02:00
|
|
|
//FIXME: diff is always 0
|
|
|
|
auto diff = baseAmount * resurrectFactor / 100.0 - resurrectedStackCount;
|
|
|
|
if(diff > rand.nextDouble(0, 0.99))
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
resurrectedStackCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hasBonusOfType(Bonus::REBIRTH, 1))
|
|
|
|
{
|
|
|
|
// resurrect at least one Sacred Phoenix
|
|
|
|
vstd::amax(resurrectedStackCount, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(resurrectedStackCount > 0)
|
|
|
|
{
|
|
|
|
bsa.flags |= BattleStackAttacked::REBIRTH;
|
2017-07-04 13:24:46 +02:00
|
|
|
//TODO: use StackHealedOrResurrected
|
|
|
|
bsa.newHealth.firstHPleft = MaxHealth();
|
|
|
|
bsa.newHealth.fullUnits = resurrectedStackCount - 1;
|
|
|
|
bsa.newHealth.resurrected = 0; //TODO: add one-battle rebirth?
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::isMeleeAttackPossible(const CStack * attacker, const CStack * defender, BattleHex attackerPos /*= BattleHex::INVALID*/, BattleHex defenderPos /*= BattleHex::INVALID*/)
|
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
if(!attackerPos.isValid())
|
2017-03-17 17:48:44 +02:00
|
|
|
attackerPos = attacker->position;
|
2017-07-04 13:24:46 +02:00
|
|
|
if(!defenderPos.isValid())
|
2017-03-17 17:48:44 +02:00
|
|
|
defenderPos = defender->position;
|
|
|
|
|
|
|
|
return
|
2017-07-09 14:49:37 +02:00
|
|
|
(BattleHex::mutualPosition(attackerPos, defenderPos) >= 0)//front <=> front
|
|
|
|
|| (attacker->doubleWide()//back <=> front
|
|
|
|
&& BattleHex::mutualPosition(attackerPos + (attacker->side == BattleSide::ATTACKER ? -1 : 1), defenderPos) >= 0)
|
|
|
|
|| (defender->doubleWide()//front <=> back
|
|
|
|
&& BattleHex::mutualPosition(attackerPos, defenderPos + (defender->side == BattleSide::ATTACKER ? -1 : 1)) >= 0)
|
2017-03-17 17:48:44 +02:00
|
|
|
|| (defender->doubleWide() && attacker->doubleWide()//back <=> back
|
2017-07-09 14:49:37 +02:00
|
|
|
&& BattleHex::mutualPosition(attackerPos + (attacker->side == BattleSide::ATTACKER ? -1 : 1), defenderPos + (defender->side == BattleSide::ATTACKER ? -1 : 1)) >= 0);
|
2017-03-17 17:48:44 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
bool CStack::ableToRetaliate() const
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
return alive()
|
2017-07-09 14:49:37 +02:00
|
|
|
&& (counterAttacks.canUse() || hasBonusOfType(Bonus::UNLIMITED_RETALIATIONS))
|
|
|
|
&& !hasBonusOfType(Bonus::SIEGE_WEAPON)
|
|
|
|
&& !hasBonusOfType(Bonus::HYPNOTIZED)
|
|
|
|
&& !hasBonusOfType(Bonus::NO_RETALIATION);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CStack::getName() const
|
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
return (health.getCount() == 1) ? type->nameSing : type->namePl; //War machines can't use base
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::isValidTarget(bool allowDead/* = false*/) const
|
|
|
|
{
|
|
|
|
return (alive() || (allowDead && isDead())) && position.isValid() && !isTurret();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::isDead() const
|
|
|
|
{
|
|
|
|
return !alive() && !isGhost();
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
bool CStack::isClone() const
|
|
|
|
{
|
|
|
|
return vstd::contains(state, EBattleStackState::CLONED);
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:48:44 +02:00
|
|
|
bool CStack::isGhost() const
|
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
return vstd::contains(state, EBattleStackState::GHOST);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::isTurret() const
|
|
|
|
{
|
|
|
|
return type->idNumber == CreatureID::ARROW_TOWERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::canBeHealed() const
|
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
return getFirstHPleft() < MaxHealth()
|
2017-07-09 14:49:37 +02:00
|
|
|
&& isValidTarget()
|
|
|
|
&& !hasBonusOfType(Bonus::SIEGE_WEAPON);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStack::makeGhost()
|
|
|
|
{
|
|
|
|
state.erase(EBattleStackState::ALIVE);
|
|
|
|
state.insert(EBattleStackState::GHOST_PENDING);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::alive() const //determines if stack is alive
|
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
return vstd::contains(state, EBattleStackState::ALIVE);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
//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
|
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
return valOfBonuses(Bonus::CREATURE_SPELL_POWER) * health.getCount() / 100;
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CStack::getEnchantPower(const CSpell * spell) const
|
|
|
|
{
|
|
|
|
int res = valOfBonuses(Bonus::CREATURE_ENCHANT_POWER);
|
2017-07-04 13:24:46 +02:00
|
|
|
if(res <= 0)
|
2017-03-17 17:48:44 +02:00
|
|
|
res = 3;//default for creatures
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CStack::getEffectValue(const CSpell * spell) const
|
|
|
|
{
|
2017-07-04 13:24:46 +02:00
|
|
|
return valOfBonuses(Bonus::SPECIFIC_SPELL_POWER, spell->id.toEnum()) * health.getCount();
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const PlayerColor CStack::getOwner() const
|
|
|
|
{
|
2017-07-09 15:55:10 +02:00
|
|
|
return battle->battleGetOwner(this);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStack::getCasterName(MetaString & text) const
|
|
|
|
{
|
|
|
|
//always plural name in case of spell cast.
|
2017-07-04 13:24:46 +02:00
|
|
|
addNameReplacement(text, true);
|
2017-03-17 17:48:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:49:37 +02:00
|
|
|
void CStack::getCastDescription(const CSpell * spell, const std::vector<const CStack *> & attacked, MetaString & text) const
|
2017-03-17 17:48:44 +02:00
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
2017-07-04 13:24:46 +02:00
|
|
|
|
2017-07-09 18:49:52 +02:00
|
|
|
int32_t CStack::unitMaxHealth() const
|
|
|
|
{
|
|
|
|
return MaxHealth();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t CStack::unitBaseAmount() const
|
|
|
|
{
|
|
|
|
return baseAmount;
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
void CStack::addText(MetaString & text, ui8 type, int32_t serial, const boost::logic::tribool & plural) 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStack::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CStack::formatGeneralMessage(const int32_t baseTextId) 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStack::setHealth(const CHealthInfo & value)
|
|
|
|
{
|
|
|
|
health.reset();
|
|
|
|
health.fromInfo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStack::setHealth(const CHealth & value)
|
|
|
|
{
|
|
|
|
health = value;
|
|
|
|
}
|