2014-03-17 22:51:07 +03:00
|
|
|
/*
|
|
|
|
* BattleState.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"
|
2010-12-25 21:23:30 +02:00
|
|
|
#include "BattleState.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
#include <numeric>
|
|
|
|
#include "VCMI_Lib.h"
|
2014-06-05 14:19:47 +03:00
|
|
|
#include "mapObjects/CObjectHandler.h"
|
2010-12-25 21:23:30 +02:00
|
|
|
#include "CHeroHandler.h"
|
|
|
|
#include "CCreatureHandler.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "spells/CSpellHandler.h"
|
2010-12-25 21:23:30 +02:00
|
|
|
#include "CTownHandler.h"
|
2011-01-08 20:33:40 +02:00
|
|
|
#include "NetPacks.h"
|
2012-08-02 14:03:26 +03:00
|
|
|
#include "JsonNode.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "filesystem/Filesystem.h"
|
2014-03-17 22:51:07 +03:00
|
|
|
#include "CRandomGenerator.h"
|
2010-12-25 21:23:30 +02:00
|
|
|
|
|
|
|
const CStack * BattleInfo::getNextStack() const
|
|
|
|
{
|
|
|
|
std::vector<const CStack *> hlp;
|
2012-08-26 12:07:48 +03:00
|
|
|
battleGetStackQueue(hlp, 1, -1);
|
2010-12-25 21:23:30 +02:00
|
|
|
|
|
|
|
if(hlp.size())
|
|
|
|
return hlp[0];
|
|
|
|
else
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
2013-02-11 02:24:57 +03:00
|
|
|
int BattleInfo::getAvaliableHex(CreatureID creID, bool attackerOwned, int initialPos) const
|
2011-10-01 22:56:54 +03:00
|
|
|
{
|
|
|
|
bool twoHex = VLC->creh->creatures[creID]->isDoubleWide();
|
2012-08-26 12:59:07 +03:00
|
|
|
//bool flying = VLC->creh->creatures[creID]->isFlying();
|
2012-06-09 22:24:04 +03:00
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
int pos;
|
2012-06-16 17:25:39 +03:00
|
|
|
if (initialPos > -1)
|
|
|
|
pos = initialPos;
|
|
|
|
else //summon elementals depending on player side
|
2012-08-26 12:59:07 +03:00
|
|
|
{
|
|
|
|
if (attackerOwned)
|
|
|
|
pos = 0; //top left
|
|
|
|
else
|
|
|
|
pos = GameConstants::BFIELD_WIDTH - 1; //top right
|
|
|
|
}
|
2012-06-16 17:25:39 +03:00
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
auto accessibility = getAccesibility();
|
|
|
|
|
2012-06-09 22:24:04 +03:00
|
|
|
std::set<BattleHex> occupyable;
|
2012-08-26 12:07:48 +03:00
|
|
|
for(int i = 0; i < accessibility.size(); i++)
|
|
|
|
if(accessibility.accessible(i, twoHex, attackerOwned))
|
|
|
|
occupyable.insert(i);
|
2012-04-22 20:38:36 +03:00
|
|
|
|
2013-11-03 15:51:25 +03:00
|
|
|
if (occupyable.empty())
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
return BattleHex::INVALID; //all tiles are covered
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
2012-08-26 12:07:48 +03:00
|
|
|
|
2012-09-24 02:10:56 +03:00
|
|
|
return BattleHex::getClosestTile(attackerOwned, pos, occupyable);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
std::pair< std::vector<BattleHex>, int > BattleInfo::getPath(BattleHex start, BattleHex dest, const CStack *stack)
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
auto reachability = getReachability(stack);
|
2012-08-26 12:59:07 +03:00
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
if(reachability.predecessors[dest] == -1) //cannot reach destination
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2011-12-22 16:05:19 +03:00
|
|
|
return std::make_pair(std::vector<BattleHex>(), 0);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//making the Path
|
2011-12-22 16:05:19 +03:00
|
|
|
std::vector<BattleHex> path;
|
|
|
|
BattleHex curElem = dest;
|
2010-12-25 21:23:30 +02:00
|
|
|
while(curElem != start)
|
|
|
|
{
|
|
|
|
path.push_back(curElem);
|
2012-08-26 12:07:48 +03:00
|
|
|
curElem = reachability.predecessors[curElem];
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
return std::make_pair(path, reachability.distances[dest]);
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:00:45 +03:00
|
|
|
ui32 BattleInfo::calculateDmg( const CStack* attacker, const CStack* defender, const CGHeroInstance * attackerHero, const CGHeroInstance * defendingHero,
|
2014-04-10 20:11:09 +03:00
|
|
|
bool shooting, ui8 charge, bool lucky, bool unlucky, bool deathBlow, bool ballistaDoubleDmg, CRandomGenerator & rand )
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2013-05-04 16:14:23 +03:00
|
|
|
TDmgRange range = calculateDmgRange(attacker, defender, shooting, charge, lucky, unlucky, deathBlow, ballistaDoubleDmg);
|
2010-12-25 21:23:30 +02:00
|
|
|
|
|
|
|
if(range.first != range.second)
|
|
|
|
{
|
|
|
|
int valuesToAverage[10];
|
|
|
|
int howManyToAv = std::min<ui32>(10, attacker->count);
|
|
|
|
for (int g=0; g<howManyToAv; ++g)
|
|
|
|
{
|
2014-04-10 20:11:09 +03:00
|
|
|
valuesToAverage[g] = rand.nextInt(range.first, range.second);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::accumulate(valuesToAverage, valuesToAverage + howManyToAv, 0) / howManyToAv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return range.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::calculateCasualties( std::map<ui32,si32> *casualties ) const
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & elem : stacks)//setting casualties
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
const CStack * const st = elem;
|
2013-07-27 22:01:31 +03:00
|
|
|
si32 killed = (st->alive() ? (st->baseAmount - st->count + st->resurrected) : st->baseAmount);
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amax(killed, 0);
|
2010-12-25 21:23:30 +02:00
|
|
|
if(killed)
|
|
|
|
casualties[!st->attackerOwned][st->getCreature()->idNumber] += killed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
CStack * BattleInfo::generateNewStack(const CStackInstance &base, bool attackerOwned, SlotID slot, BattleHex position) const
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2012-02-29 04:31:48 +03:00
|
|
|
int stackID = getIdForNewStack();
|
2013-07-22 01:01:29 +03:00
|
|
|
PlayerColor owner = sides[attackerOwned ? 0 : 1].color;
|
2013-03-03 20:06:03 +03:00
|
|
|
assert((owner >= PlayerColor::PLAYER_LIMIT) ||
|
2011-05-30 22:20:14 +03:00
|
|
|
(base.armyObj && base.armyObj->tempOwner == owner));
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
auto ret = new CStack(&base, owner, stackID, attackerOwned, slot);
|
2012-06-16 17:25:39 +03:00
|
|
|
ret->position = getAvaliableHex (base.getCreatureID(), attackerOwned, position); //TODO: what if no free tile on battlefield was found?
|
2012-08-28 15:28:13 +03:00
|
|
|
ret->state.insert(EBattleStackState::ALIVE); //alive state indication
|
2010-12-25 21:23:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2012-08-28 15:28:13 +03:00
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
CStack * BattleInfo::generateNewStack(const CStackBasicDescriptor &base, bool attackerOwned, SlotID slot, BattleHex position) const
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2012-02-29 04:31:48 +03:00
|
|
|
int stackID = getIdForNewStack();
|
2013-07-22 01:01:29 +03:00
|
|
|
PlayerColor owner = sides[attackerOwned ? 0 : 1].color;
|
2013-06-29 16:05:48 +03:00
|
|
|
auto ret = new CStack(&base, owner, stackID, attackerOwned, slot);
|
2010-12-25 21:23:30 +02:00
|
|
|
ret->position = position;
|
2012-08-28 15:28:13 +03:00
|
|
|
ret->state.insert(EBattleStackState::ALIVE); //alive state indication
|
2010-12-25 21:23:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
const CStack * BattleInfo::battleGetStack(BattleHex pos, bool onlyAlive)
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
CStack * stack = nullptr;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & elem : stacks)
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
if(elem->position == pos
|
|
|
|
|| (elem->doubleWide()
|
|
|
|
&&( (elem->attackerOwned && elem->position-1 == pos)
|
|
|
|
|| (!elem->attackerOwned && elem->position+1 == pos) )
|
2011-11-06 20:39:49 +03:00
|
|
|
) )
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
if (elem->alive())
|
2013-07-22 01:01:29 +03:00
|
|
|
return elem; //we prefer living stacks - there can be only one stack on the tile, so return it immediately
|
2011-11-06 20:39:49 +03:00
|
|
|
else if (!onlyAlive)
|
2013-06-29 16:05:48 +03:00
|
|
|
stack = elem; //dead stacks are only accessible when there's no alive stack on this tile
|
2011-11-06 20:39:49 +03:00
|
|
|
}
|
2011-01-07 12:48:31 +02:00
|
|
|
}
|
2011-11-06 20:39:49 +03:00
|
|
|
return stack;
|
2011-01-07 12:48:31 +02:00
|
|
|
}
|
|
|
|
|
2011-01-08 20:33:40 +02:00
|
|
|
const CGHeroInstance * BattleInfo::battleGetOwner(const CStack * stack) const
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2013-07-22 01:01:29 +03:00
|
|
|
return sides[!stack->attackerOwned].hero;
|
2011-01-07 12:48:31 +02:00
|
|
|
}
|
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
void BattleInfo::localInit()
|
|
|
|
{
|
2013-07-22 01:01:29 +03:00
|
|
|
for(int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
auto armyObj = battleGetArmyObject(i);
|
|
|
|
armyObj->battle = this;
|
|
|
|
armyObj->attachTo(this);
|
|
|
|
}
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(CStack *s : stacks)
|
2012-02-29 04:31:48 +03:00
|
|
|
localInitStack(s);
|
2011-03-01 12:19:05 +02:00
|
|
|
|
|
|
|
exportBonuses();
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
2012-02-29 04:31:48 +03:00
|
|
|
void BattleInfo::localInitStack(CStack * s)
|
|
|
|
{
|
|
|
|
s->exportBonuses();
|
|
|
|
if(s->base) //stack originating from "real" stack in garrison -> attach to it
|
|
|
|
{
|
|
|
|
s->attachTo(const_cast<CStackInstance*>(s->base));
|
|
|
|
}
|
|
|
|
else //attach directly to obj to which stack belongs and creature type
|
|
|
|
{
|
2013-07-22 01:01:29 +03:00
|
|
|
CArmedInstance *army = battleGetArmyObject(!s->attackerOwned);
|
2012-02-29 04:31:48 +03:00
|
|
|
s->attachTo(army);
|
|
|
|
assert(s->type);
|
|
|
|
s->attachTo(const_cast<CCreature*>(s->type));
|
|
|
|
}
|
|
|
|
s->postInit();
|
|
|
|
}
|
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
namespace CGH
|
|
|
|
{
|
|
|
|
using namespace std;
|
2011-09-03 22:25:37 +03:00
|
|
|
|
|
|
|
static void readBattlePositions(const JsonNode &node, vector< vector<int> > & dest)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const JsonNode &level : node.Vector())
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
std::vector<int> pom;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const JsonNode &value : level.Vector())
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2011-09-03 22:25:37 +03:00
|
|
|
pom.push_back(value.Float());
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
2011-09-03 22:25:37 +03:00
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
dest.push_back(pom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-23 22:56:37 +03:00
|
|
|
//RNG that works like H3 one
|
|
|
|
struct RandGen
|
|
|
|
{
|
|
|
|
int seed;
|
|
|
|
|
|
|
|
void srand(int s)
|
|
|
|
{
|
|
|
|
seed = s;
|
|
|
|
}
|
|
|
|
void srand(int3 pos)
|
|
|
|
{
|
|
|
|
srand(110291 * pos.x + 167801 * pos.y + 81569);
|
|
|
|
}
|
|
|
|
int rand()
|
|
|
|
{
|
|
|
|
seed = 214013 * seed + 2531011;
|
|
|
|
return (seed >> 16) & 0x7FFF;
|
|
|
|
}
|
|
|
|
int rand(int min, int max)
|
|
|
|
{
|
|
|
|
if(min == max)
|
|
|
|
return min;
|
|
|
|
if(min > max)
|
|
|
|
return min;
|
|
|
|
return min + rand() % (max - min + 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RangeGenerator
|
|
|
|
{
|
|
|
|
class ExhaustedPossibilities : public std::exception
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2013-11-06 16:42:58 +03:00
|
|
|
RangeGenerator(int _min, int _max, std::function<int()> _myRand):
|
|
|
|
min(_min),
|
|
|
|
remainingCount(_max - _min + 1),
|
|
|
|
remaining(remainingCount, true),
|
|
|
|
myRand(_myRand)
|
2012-04-23 22:56:37 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-24 08:16:04 +03:00
|
|
|
int generateNumber()
|
|
|
|
{
|
|
|
|
if(!remainingCount)
|
|
|
|
throw ExhaustedPossibilities();
|
|
|
|
if(remainingCount == 1)
|
|
|
|
return 0;
|
|
|
|
return myRand() % remainingCount;
|
|
|
|
}
|
|
|
|
|
2012-04-23 22:56:37 +03:00
|
|
|
//get number fulfilling predicate. Never gives the same number twice.
|
2013-06-29 16:05:48 +03:00
|
|
|
int getSuchNumber(std::function<bool(int)> goodNumberPred = nullptr)
|
2012-04-23 22:56:37 +03:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
do
|
|
|
|
{
|
2012-04-24 08:16:04 +03:00
|
|
|
int n = generateNumber();
|
2012-04-23 22:56:37 +03:00
|
|
|
int i = 0;
|
|
|
|
for(;;i++)
|
|
|
|
{
|
|
|
|
assert(i < (int)remaining.size());
|
|
|
|
if(!remaining[i])
|
|
|
|
continue;
|
|
|
|
if(!n)
|
|
|
|
break;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
remainingCount--;
|
|
|
|
remaining[i] = false;
|
|
|
|
ret = i + min;
|
|
|
|
} while(goodNumberPred && !goodNumberPred(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int min, remainingCount;
|
|
|
|
std::vector<bool> remaining;
|
2013-06-26 14:18:27 +03:00
|
|
|
std::function<int()> myRand;
|
2012-04-23 22:56:37 +03:00
|
|
|
};
|
|
|
|
|
2013-02-13 01:24:48 +03:00
|
|
|
BattleInfo * BattleInfo::setupBattle( int3 tile, ETerrainType terrain, BFieldType battlefieldType, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town )
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
CMP_stack cmpst;
|
2013-06-29 16:05:48 +03:00
|
|
|
auto curB = new BattleInfo;
|
2013-07-22 01:01:29 +03:00
|
|
|
|
|
|
|
for(auto i = 0u; i < curB->sides.size(); i++)
|
|
|
|
curB->sides[i].init(heroes[i], armies[i]);
|
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
|
|
|
|
std::vector<CStack*> & stacks = (curB->stacks);
|
|
|
|
|
|
|
|
curB->tile = tile;
|
2012-05-18 23:50:16 +03:00
|
|
|
curB->battlefieldType = battlefieldType;
|
2010-12-25 21:23:30 +02:00
|
|
|
curB->round = -2;
|
|
|
|
curB->activeStack = -1;
|
|
|
|
|
|
|
|
if(town)
|
|
|
|
{
|
2011-01-07 12:48:31 +02:00
|
|
|
curB->town = town;
|
2013-04-21 15:49:26 +03:00
|
|
|
curB->terrainType = VLC->townh->factions[town->subID]->nativeTerrain;
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
curB->town = nullptr;
|
2012-05-18 23:50:16 +03:00
|
|
|
curB->terrainType = terrain;
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
2012-06-16 17:25:39 +03:00
|
|
|
//setting up siege obstacles
|
|
|
|
if (town && town->hasFort())
|
|
|
|
{
|
2013-08-06 14:20:28 +03:00
|
|
|
for (int b = 0; b < curB->si.wallState.size(); ++b)
|
2012-06-16 17:25:39 +03:00
|
|
|
{
|
2013-08-06 14:20:28 +03:00
|
|
|
curB->si.wallState[b] = EWallState::INTACT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!town->hasBuilt(BuildingID::CITADEL))
|
|
|
|
{
|
2013-12-08 20:54:13 +03:00
|
|
|
curB->si.wallState[EWallPart::KEEP] = EWallState::NONE;
|
2013-08-06 14:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!town->hasBuilt(BuildingID::CASTLE))
|
|
|
|
{
|
2013-12-08 20:54:13 +03:00
|
|
|
curB->si.wallState[EWallPart::UPPER_TOWER] = EWallState::NONE;
|
|
|
|
curB->si.wallState[EWallPart::BOTTOM_TOWER] = EWallState::NONE;
|
2012-06-16 17:25:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//randomize obstacles
|
2013-06-26 14:18:27 +03:00
|
|
|
if (town == nullptr && !creatureBank) //do it only when it's not siege and not creature bank
|
2012-06-16 17:25:39 +03:00
|
|
|
{
|
|
|
|
const int ABSOLUTE_OBSTACLES_COUNT = 34, USUAL_OBSTACLES_COUNT = 91; //shouldn't be changes if we want H3-like obstacle placement
|
|
|
|
|
|
|
|
RandGen r;
|
|
|
|
auto ourRand = [&]{ return r.rand(); };
|
|
|
|
r.srand(tile);
|
|
|
|
r.rand(1,8); //battle sound ID to play... can't do anything with it here
|
|
|
|
int tilesToBlock = r.rand(5,12);
|
|
|
|
const int specialBattlefield = battlefieldTypeToBI(battlefieldType);
|
|
|
|
|
|
|
|
std::vector<BattleHex> blockedTiles;
|
|
|
|
|
|
|
|
auto appropriateAbsoluteObstacle = [&](int id)
|
|
|
|
{
|
|
|
|
return VLC->heroh->absoluteObstacles[id].isAppropriate(curB->terrainType, specialBattlefield);
|
|
|
|
};
|
|
|
|
auto appropriateUsualObstacle = [&](int id) -> bool
|
|
|
|
{
|
|
|
|
return VLC->heroh->obstacles[id].isAppropriate(curB->terrainType, specialBattlefield);
|
|
|
|
};
|
|
|
|
|
|
|
|
if(r.rand(1,100) <= 40) //put cliff-like obstacle
|
|
|
|
{
|
|
|
|
RangeGenerator obidgen(0, ABSOLUTE_OBSTACLES_COUNT-1, ourRand);
|
2012-08-26 12:59:07 +03:00
|
|
|
|
2012-06-16 17:25:39 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto obstPtr = make_shared<CObstacleInstance>();
|
|
|
|
obstPtr->obstacleType = CObstacleInstance::ABSOLUTE_OBSTACLE;
|
|
|
|
obstPtr->ID = obidgen.getSuchNumber(appropriateAbsoluteObstacle);
|
|
|
|
obstPtr->uniqueID = curB->obstacles.size();
|
|
|
|
curB->obstacles.push_back(obstPtr);
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(BattleHex blocked : obstPtr->getBlockedTiles())
|
2012-06-16 17:25:39 +03:00
|
|
|
blockedTiles.push_back(blocked);
|
|
|
|
tilesToBlock -= VLC->heroh->absoluteObstacles[obstPtr->ID].blockedTiles.size() / 2;
|
|
|
|
}
|
|
|
|
catch(RangeGenerator::ExhaustedPossibilities &)
|
|
|
|
{
|
|
|
|
//silently ignore, if we can't place absolute obstacle, we'll go with the usual ones
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RangeGenerator obidgen(0, USUAL_OBSTACLES_COUNT-1, ourRand);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while(tilesToBlock > 0)
|
|
|
|
{
|
|
|
|
const int obid = obidgen.getSuchNumber(appropriateUsualObstacle);
|
|
|
|
const CObstacleInfo &obi = VLC->heroh->obstacles[obid];
|
|
|
|
|
|
|
|
auto validPosition = [&](BattleHex pos) -> bool
|
|
|
|
{
|
|
|
|
if(obi.height >= pos.getY())
|
|
|
|
return false;
|
|
|
|
if(pos.getX() == 0)
|
|
|
|
return false;
|
|
|
|
if(pos.getX() + obi.width > 15)
|
|
|
|
return false;
|
|
|
|
if(vstd::contains(blockedTiles, pos))
|
|
|
|
return false;
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(BattleHex blocked : obi.getBlocked(pos))
|
2012-06-16 17:25:39 +03:00
|
|
|
{
|
|
|
|
if(vstd::contains(blockedTiles, blocked))
|
|
|
|
return false;
|
|
|
|
int x = blocked.getX();
|
|
|
|
if(x <= 2 || x >= 14)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
RangeGenerator posgenerator(18, 168, ourRand);
|
|
|
|
|
|
|
|
auto obstPtr = make_shared<CObstacleInstance>();
|
|
|
|
obstPtr->ID = obid;
|
|
|
|
obstPtr->pos = posgenerator.getSuchNumber(validPosition);
|
|
|
|
obstPtr->uniqueID = curB->obstacles.size();
|
|
|
|
curB->obstacles.push_back(obstPtr);
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(BattleHex blocked : obstPtr->getBlockedTiles())
|
2012-06-16 17:25:39 +03:00
|
|
|
blockedTiles.push_back(blocked);
|
|
|
|
tilesToBlock -= obi.blockedTiles.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(RangeGenerator::ExhaustedPossibilities &)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//reading battleStartpos - add creatures AFTER random obstacles are generated
|
2012-04-28 14:41:20 +03:00
|
|
|
//TODO: parse once to some structure
|
2012-08-30 19:01:19 +03:00
|
|
|
std::vector< std::vector<int> > looseFormations[2], tightFormations[2], creBankFormations[2];
|
2012-04-28 14:41:20 +03:00
|
|
|
std::vector <int> commanderField, commanderBank;
|
2012-08-02 14:03:26 +03:00
|
|
|
const JsonNode config(ResourceID("config/battleStartpos.json"));
|
2011-09-03 22:25:37 +03:00
|
|
|
const JsonVector &positions = config["battle_positions"].Vector();
|
|
|
|
|
2012-08-30 19:01:19 +03:00
|
|
|
CGH::readBattlePositions(positions[0]["levels"], looseFormations[0]);
|
|
|
|
CGH::readBattlePositions(positions[1]["levels"], looseFormations[1]);
|
|
|
|
CGH::readBattlePositions(positions[2]["levels"], tightFormations[0]);
|
|
|
|
CGH::readBattlePositions(positions[3]["levels"], tightFormations[1]);
|
|
|
|
CGH::readBattlePositions(positions[4]["levels"], creBankFormations[0]);
|
|
|
|
CGH::readBattlePositions(positions[5]["levels"], creBankFormations[1]);
|
2012-08-26 12:59:07 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto position : config["commanderPositions"]["field"].Vector())
|
2012-04-28 14:41:20 +03:00
|
|
|
{
|
|
|
|
commanderField.push_back (position.Float());
|
|
|
|
}
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto position : config["commanderPositions"]["creBank"].Vector())
|
2012-04-28 14:41:20 +03:00
|
|
|
{
|
|
|
|
commanderBank.push_back (position.Float());
|
|
|
|
}
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2012-08-28 15:28:13 +03:00
|
|
|
|
|
|
|
//adding war machines
|
|
|
|
if(!creatureBank)
|
|
|
|
{
|
|
|
|
//Checks if hero has artifact and create appropriate stack
|
2013-02-12 22:49:40 +03:00
|
|
|
auto handleWarMachine= [&](int side, ArtifactPosition artslot, CreatureID cretype, BattleHex hex)
|
2012-08-28 15:28:13 +03:00
|
|
|
{
|
2012-08-29 16:01:54 +03:00
|
|
|
if(heroes[side] && heroes[side]->getArt(artslot))
|
2013-02-16 17:03:47 +03:00
|
|
|
stacks.push_back(curB->generateNewStack(CStackBasicDescriptor(cretype, 1), !side, SlotID(255), hex));
|
2012-08-28 15:28:13 +03:00
|
|
|
};
|
|
|
|
|
2013-02-07 20:34:50 +03:00
|
|
|
handleWarMachine(0, ArtifactPosition::MACH1, CreatureID::BALLISTA, 52);
|
|
|
|
handleWarMachine(0, ArtifactPosition::MACH2, CreatureID::AMMO_CART, 18);
|
|
|
|
handleWarMachine(0, ArtifactPosition::MACH3, CreatureID::FIRST_AID_TENT, 154);
|
2012-08-28 15:28:13 +03:00
|
|
|
if(town && town->hasFort())
|
2013-02-07 20:34:50 +03:00
|
|
|
handleWarMachine(0, ArtifactPosition::MACH4, CreatureID::CATAPULT, 120);
|
2012-08-28 15:28:13 +03:00
|
|
|
|
|
|
|
if(!town) //defending hero shouldn't receive ballista (bug #551)
|
2013-02-07 20:34:50 +03:00
|
|
|
handleWarMachine(1, ArtifactPosition::MACH1, CreatureID::BALLISTA, 66);
|
|
|
|
handleWarMachine(1, ArtifactPosition::MACH2, CreatureID::AMMO_CART, 32);
|
|
|
|
handleWarMachine(1, ArtifactPosition::MACH3, CreatureID::FIRST_AID_TENT, 168);
|
2012-08-28 15:28:13 +03:00
|
|
|
}
|
|
|
|
//war machines added
|
|
|
|
|
2011-09-03 22:25:37 +03:00
|
|
|
//battleStartpos read
|
2012-08-30 19:01:19 +03:00
|
|
|
for(int side = 0; side < 2; side++)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2012-08-30 19:01:19 +03:00
|
|
|
int formationNo = armies[side]->stacksCount() - 1;
|
|
|
|
vstd::abetween(formationNo, 0, GameConstants::ARMY_SIZE - 1);
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2012-08-30 19:01:19 +03:00
|
|
|
int k = 0; //stack serial
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto i = armies[side]->Slots().begin(); i != armies[side]->Slots().end(); i++, k++)
|
2012-08-30 19:01:19 +03:00
|
|
|
{
|
|
|
|
std::vector<int> *formationVector = nullptr;
|
|
|
|
if(creatureBank)
|
|
|
|
formationVector = &creBankFormations[side][formationNo];
|
|
|
|
else if(armies[side]->formation)
|
|
|
|
formationVector = &tightFormations[side][formationNo];
|
|
|
|
else
|
|
|
|
formationVector = &looseFormations[side][formationNo];
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2012-08-30 19:01:19 +03:00
|
|
|
BattleHex pos = (k < formationVector->size() ? formationVector->at(k) : 0);
|
|
|
|
if(creatureBank && i->second->type->isDoubleWide())
|
|
|
|
pos += side ? BattleHex::LEFT : BattleHex::RIGHT;
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2012-08-30 19:01:19 +03:00
|
|
|
CStack * stack = curB->generateNewStack(*i->second, !side, i->first, pos);
|
|
|
|
stacks.push_back(stack);
|
|
|
|
}
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
2012-04-28 14:41:20 +03:00
|
|
|
//adding commanders
|
|
|
|
for (int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
if (heroes[i] && heroes[i]->commander)
|
|
|
|
{
|
2013-02-16 17:03:47 +03:00
|
|
|
CStack * stack = curB->generateNewStack (*heroes[i]->commander, !i, SlotID::COMMANDER_SLOT_PLACEHOLDER,
|
2012-04-28 14:41:20 +03:00
|
|
|
creatureBank ? commanderBank[i] : commanderField[i]);
|
|
|
|
stacks.push_back(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-25 17:11:07 +03:00
|
|
|
if (curB->town && curB->town->fortLevel() >= CGTownInstance::CITADEL)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2011-05-18 20:51:10 +03:00
|
|
|
// keep tower
|
2013-02-16 17:03:47 +03:00
|
|
|
CStack * stack = curB->generateNewStack(CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), false, SlotID(255), -2);
|
2011-05-18 20:51:10 +03:00
|
|
|
stacks.push_back(stack);
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2014-06-25 17:11:07 +03:00
|
|
|
if (curB->town->fortLevel() >= CGTownInstance::CASTLE)
|
2011-05-18 20:51:10 +03:00
|
|
|
{
|
|
|
|
// lower tower + upper tower
|
2013-02-16 17:03:47 +03:00
|
|
|
CStack * stack = curB->generateNewStack(CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), false, SlotID(255), -4);
|
2010-12-25 21:23:30 +02:00
|
|
|
stacks.push_back(stack);
|
2013-02-16 17:03:47 +03:00
|
|
|
stack = curB->generateNewStack(CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), false, SlotID(255), -3);
|
2010-12-25 21:23:30 +02:00
|
|
|
stacks.push_back(stack);
|
|
|
|
}
|
2012-05-18 23:50:16 +03:00
|
|
|
|
|
|
|
//moat
|
|
|
|
auto moat = make_shared<MoatObstacle>();
|
|
|
|
moat->ID = curB->town->subID;
|
|
|
|
moat->obstacleType = CObstacleInstance::MOAT;
|
|
|
|
moat->uniqueID = curB->obstacles.size();
|
|
|
|
curB->obstacles.push_back(moat);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::stable_sort(stacks.begin(),stacks.end(),cmpst);
|
|
|
|
|
2011-02-21 15:27:31 +02:00
|
|
|
//spell level limiting bonus
|
2011-02-21 18:53:23 +02:00
|
|
|
curB->addNewBonus(new Bonus(Bonus::ONE_BATTLE, Bonus::LEVEL_SPELL_IMMUNITY, Bonus::OTHER,
|
|
|
|
0, -1, -1, Bonus::INDEPENDENT_MAX));
|
2011-02-21 15:27:31 +02:00
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
//giving terrain overalay premies
|
|
|
|
int bonusSubtype = -1;
|
2012-05-18 23:50:16 +03:00
|
|
|
switch(battlefieldType)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2013-02-04 00:05:44 +03:00
|
|
|
case BFieldType::MAGIC_PLAINS:
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
bonusSubtype = 0;
|
|
|
|
}
|
2013-02-04 00:05:44 +03:00
|
|
|
case BFieldType::FIERY_FIELDS:
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
if(bonusSubtype == -1) bonusSubtype = 1;
|
|
|
|
}
|
2013-02-04 00:05:44 +03:00
|
|
|
case BFieldType::ROCKLANDS:
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
if(bonusSubtype == -1) bonusSubtype = 8;
|
|
|
|
}
|
2013-02-04 00:05:44 +03:00
|
|
|
case BFieldType::MAGIC_CLOUDS:
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
if(bonusSubtype == -1) bonusSubtype = 2;
|
|
|
|
}
|
2013-02-04 00:05:44 +03:00
|
|
|
case BFieldType::LUCID_POOLS:
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
if(bonusSubtype == -1) bonusSubtype = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
{ //common part for cases 9, 14, 15, 16, 17
|
|
|
|
curB->addNewBonus(new Bonus(Bonus::ONE_BATTLE, Bonus::MAGIC_SCHOOL_SKILL, Bonus::TERRAIN_OVERLAY, 3, -1, "", bonusSubtype));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-02-04 00:05:44 +03:00
|
|
|
case BFieldType::HOLY_GROUND:
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
curB->addNewBonus(makeFeature(Bonus::MORALE, Bonus::ONE_BATTLE, 0, +1, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureAlignmentLimiter>(EAlignment::GOOD)));
|
|
|
|
curB->addNewBonus(makeFeature(Bonus::MORALE, Bonus::ONE_BATTLE, 0, -1, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureAlignmentLimiter>(EAlignment::EVIL)));
|
2010-12-25 21:23:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-02-04 00:05:44 +03:00
|
|
|
case BFieldType::CLOVER_FIELD:
|
2010-12-25 21:23:30 +02:00
|
|
|
{ //+2 luck bonus for neutral creatures
|
2013-05-04 16:14:23 +03:00
|
|
|
curB->addNewBonus(makeFeature(Bonus::LUCK, Bonus::ONE_BATTLE, 0, +2, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureAlignmentLimiter>(EAlignment::NEUTRAL)));
|
2010-12-25 21:23:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-02-04 00:05:44 +03:00
|
|
|
case BFieldType::EVIL_FOG:
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
curB->addNewBonus(makeFeature(Bonus::MORALE, Bonus::ONE_BATTLE, 0, -1, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureAlignmentLimiter>(EAlignment::GOOD)));
|
|
|
|
curB->addNewBonus(makeFeature(Bonus::MORALE, Bonus::ONE_BATTLE, 0, +1, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureAlignmentLimiter>(EAlignment::EVIL)));
|
2010-12-25 21:23:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-02-04 00:05:44 +03:00
|
|
|
case BFieldType::CURSED_GROUND:
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
curB->addNewBonus(makeFeature(Bonus::NO_MORALE, Bonus::ONE_BATTLE, 0, 0, Bonus::TERRAIN_OVERLAY));
|
|
|
|
curB->addNewBonus(makeFeature(Bonus::NO_LUCK, Bonus::ONE_BATTLE, 0, 0, Bonus::TERRAIN_OVERLAY));
|
2011-12-14 00:23:17 +03:00
|
|
|
Bonus * b = makeFeature(Bonus::LEVEL_SPELL_IMMUNITY, Bonus::ONE_BATTLE, GameConstants::SPELL_LEVELS, 1, Bonus::TERRAIN_OVERLAY);
|
2011-02-21 18:53:23 +02:00
|
|
|
b->valType = Bonus::INDEPENDENT_MAX;
|
2011-02-21 15:27:31 +02:00
|
|
|
curB->addNewBonus(b);
|
2010-12-25 21:23:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//overlay premies given
|
|
|
|
|
|
|
|
//native terrain bonuses
|
2012-05-18 23:50:16 +03:00
|
|
|
auto nativeTerrain = make_shared<CreatureNativeTerrainLimiter>(curB->terrainType);
|
2010-12-25 21:23:30 +02:00
|
|
|
curB->addNewBonus(makeFeature(Bonus::STACKS_SPEED, Bonus::ONE_BATTLE, 0, 1, Bonus::TERRAIN_NATIVE)->addLimiter(nativeTerrain));
|
|
|
|
curB->addNewBonus(makeFeature(Bonus::PRIMARY_SKILL, Bonus::ONE_BATTLE, PrimarySkill::ATTACK, 1, Bonus::TERRAIN_NATIVE)->addLimiter(nativeTerrain));
|
|
|
|
curB->addNewBonus(makeFeature(Bonus::PRIMARY_SKILL, Bonus::ONE_BATTLE, PrimarySkill::DEFENSE, 1, Bonus::TERRAIN_NATIVE)->addLimiter(nativeTerrain));
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
//tactics
|
2013-07-22 01:01:29 +03:00
|
|
|
bool isTacticsAllowed = !creatureBank; //no tactics in creature banks
|
2012-03-29 00:54:43 +03:00
|
|
|
|
2011-02-12 18:12:48 +02:00
|
|
|
int tacticLvls[2] = {0};
|
|
|
|
for(int i = 0; i < ARRAY_COUNT(tacticLvls); i++)
|
|
|
|
{
|
|
|
|
if(heroes[i])
|
2013-02-04 22:43:16 +03:00
|
|
|
tacticLvls[i] += heroes[i]->getSecSkillLevel(SecondarySkill::TACTICS);
|
2011-02-12 18:12:48 +02:00
|
|
|
}
|
2012-03-29 00:54:43 +03:00
|
|
|
int tacticsSkillDiff = tacticLvls[0] - tacticLvls[1];
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2012-03-29 00:54:43 +03:00
|
|
|
if(tacticsSkillDiff && isTacticsAllowed)
|
2011-02-12 18:12:48 +02:00
|
|
|
{
|
2012-03-29 00:54:43 +03:00
|
|
|
curB->tacticsSide = tacticsSkillDiff < 0;
|
|
|
|
curB->tacticDistance = std::abs(tacticsSkillDiff)*2 + 1;
|
2011-02-12 18:12:48 +02:00
|
|
|
}
|
2011-02-14 00:46:04 +02:00
|
|
|
else
|
|
|
|
curB->tacticDistance = 0;
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
|
2013-04-27 09:39:59 +03:00
|
|
|
// workaround bonuses affecting only enemy - DOES NOT WORK
|
2011-02-21 06:13:00 +02:00
|
|
|
for(int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
TNodes nodes;
|
2013-07-22 01:01:29 +03:00
|
|
|
curB->battleGetArmyObject(i)->getRedAncestors(nodes);
|
2013-06-29 16:05:48 +03:00
|
|
|
for(CBonusSystemNode *n : nodes)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus *b : n->getExportedBonusList())
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
if(b->effectRange == Bonus::ONLY_ENEMY_ARMY/* && b->propagator && b->propagator->shouldBeAttached(curB)*/)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
auto bCopy = new Bonus(*b);
|
2011-02-21 06:13:00 +02:00
|
|
|
bCopy->effectRange = Bonus::NO_LIMIT;
|
|
|
|
bCopy->propagator.reset();
|
2013-07-22 01:01:29 +03:00
|
|
|
bCopy->limiter.reset(new StackOwnerLimiter(curB->sides[!i].color));
|
2011-02-21 06:13:00 +02:00
|
|
|
curB->addNewBonus(bCopy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-12 18:12:48 +02:00
|
|
|
return curB;
|
|
|
|
}
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
const CGHeroInstance * BattleInfo::getHero( PlayerColor player ) const
|
2011-02-21 18:53:23 +02:00
|
|
|
{
|
2013-07-22 01:01:29 +03:00
|
|
|
for(int i = 0; i < sides.size(); i++)
|
|
|
|
if(sides[i].color == player)
|
|
|
|
return sides[i].hero;
|
2011-02-21 18:53:23 +02:00
|
|
|
|
2013-07-22 01:01:29 +03:00
|
|
|
logGlobal->errorStream() << "Player " << player << " is not in battle!";
|
|
|
|
return nullptr;
|
2011-02-21 18:53:23 +02:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerColor BattleInfo::theOtherPlayer(PlayerColor player) const
|
2011-03-05 18:38:22 +02:00
|
|
|
{
|
2013-07-22 01:01:29 +03:00
|
|
|
return sides[!whatSide(player)].color;
|
2011-03-05 18:38:22 +02:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
ui8 BattleInfo::whatSide(PlayerColor player) const
|
2011-03-05 18:38:22 +02:00
|
|
|
{
|
2013-07-22 01:01:29 +03:00
|
|
|
for(int i = 0; i < sides.size(); i++)
|
|
|
|
if(sides[i].color == player)
|
2011-03-05 18:38:22 +02:00
|
|
|
return i;
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->warnStream() << "BattleInfo::whatSide: Player " << player << " is not in battle!";
|
2011-03-05 18:38:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-29 04:31:48 +03:00
|
|
|
int BattleInfo::getIdForNewStack() const
|
|
|
|
{
|
|
|
|
if(stacks.size())
|
|
|
|
{
|
|
|
|
//stacks vector may be sorted not by ID and they may be not contiguous -> find stack with max ID
|
2012-08-26 12:59:07 +03:00
|
|
|
auto highestIDStack = *std::max_element(stacks.begin(), stacks.end(),
|
2012-02-29 04:31:48 +03:00
|
|
|
[](const CStack *a, const CStack *b) { return a->ID < b->ID; });
|
|
|
|
|
|
|
|
return highestIDStack->ID + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-18 23:50:16 +03:00
|
|
|
shared_ptr<CObstacleInstance> BattleInfo::getObstacleOnTile(BattleHex tile) const
|
2012-03-31 00:36:07 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto &obs : obstacles)
|
2012-05-18 23:50:16 +03:00
|
|
|
if(vstd::contains(obs->getAffectedTiles(), tile))
|
|
|
|
return obs;
|
|
|
|
|
2012-06-23 20:19:50 +03:00
|
|
|
return shared_ptr<CObstacleInstance>();
|
2012-03-31 00:36:07 +03:00
|
|
|
}
|
|
|
|
|
2013-02-13 01:24:48 +03:00
|
|
|
BattlefieldBI::BattlefieldBI BattleInfo::battlefieldTypeToBI(BFieldType bfieldType)
|
2012-04-23 22:56:37 +03:00
|
|
|
{
|
2014-10-02 18:43:46 +03:00
|
|
|
static const std::map<BFieldType, BattlefieldBI::BattlefieldBI> theMap =
|
|
|
|
{
|
|
|
|
{BFieldType::CLOVER_FIELD, BattlefieldBI::CLOVER_FIELD},
|
|
|
|
{BFieldType::CURSED_GROUND, BattlefieldBI::CURSED_GROUND},
|
|
|
|
{BFieldType::EVIL_FOG, BattlefieldBI::EVIL_FOG},
|
|
|
|
{BFieldType::FAVOURABLE_WINDS, BattlefieldBI::NONE},
|
|
|
|
{BFieldType::FIERY_FIELDS, BattlefieldBI::FIERY_FIELDS},
|
|
|
|
{BFieldType::HOLY_GROUND, BattlefieldBI::HOLY_GROUND},
|
|
|
|
{BFieldType::LUCID_POOLS, BattlefieldBI::LUCID_POOLS},
|
|
|
|
{BFieldType::MAGIC_CLOUDS, BattlefieldBI::MAGIC_CLOUDS},
|
|
|
|
{BFieldType::MAGIC_PLAINS, BattlefieldBI::MAGIC_PLAINS},
|
|
|
|
{BFieldType::ROCKLANDS, BattlefieldBI::ROCKLANDS},
|
|
|
|
{BFieldType::SAND_SHORE, BattlefieldBI::COASTAL}
|
|
|
|
};
|
2012-04-23 22:56:37 +03:00
|
|
|
|
|
|
|
auto itr = theMap.find(bfieldType);
|
|
|
|
if(itr != theMap.end())
|
|
|
|
return itr->second;
|
|
|
|
|
|
|
|
return BattlefieldBI::NONE;
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
CStack * BattleInfo::getStack(int stackID, bool onlyAlive /*= true*/)
|
2012-05-05 00:16:39 +03:00
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
return const_cast<CStack *>(battleGetStackByID(stackID, onlyAlive));
|
2012-05-05 00:16:39 +03:00
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
CStack * BattleInfo::getStackT(BattleHex tileID, bool onlyAlive /*= true*/)
|
2012-05-18 23:50:16 +03:00
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
return const_cast<CStack *>(battleGetStackByPos(tileID, onlyAlive));
|
2012-05-18 23:50:16 +03:00
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
BattleInfo::BattleInfo()
|
2012-05-18 23:50:16 +03:00
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
setBattle(this);
|
|
|
|
setNodeType(BATTLE);
|
2012-05-18 23:50:16 +03:00
|
|
|
}
|
|
|
|
|
2013-07-22 01:01:29 +03:00
|
|
|
CArmedInstance * BattleInfo::battleGetArmyObject(ui8 side) const
|
|
|
|
{
|
|
|
|
return const_cast<CArmedInstance*>(CBattleInfoEssentials::battleGetArmyObject(side));
|
|
|
|
}
|
|
|
|
|
|
|
|
CGHeroInstance * BattleInfo::battleGetFightingHero(ui8 side) const
|
|
|
|
{
|
|
|
|
return const_cast<CGHeroInstance*>(CBattleInfoEssentials::battleGetFightingHero(side));
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
CStack::CStack(const CStackInstance *Base, PlayerColor O, int I, bool AO, SlotID S)
|
2012-08-26 12:59:07 +03:00
|
|
|
: base(Base), ID(I), owner(O), slot(S), attackerOwned(AO),
|
2010-12-25 21:23:30 +02:00
|
|
|
counterAttacks(1)
|
|
|
|
{
|
|
|
|
assert(base);
|
|
|
|
type = base->type;
|
|
|
|
count = baseAmount = base->count;
|
2011-07-13 21:39:02 +03:00
|
|
|
setNodeType(STACK_BATTLE);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
CStack::CStack()
|
|
|
|
{
|
|
|
|
init();
|
2011-07-13 21:39:02 +03:00
|
|
|
setNodeType(STACK_BATTLE);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
2013-03-03 20:06:03 +03:00
|
|
|
CStack::CStack(const CStackBasicDescriptor *stack, PlayerColor O, int I, bool AO, SlotID S)
|
2013-06-26 14:18:27 +03:00
|
|
|
: base(nullptr), ID(I), owner(O), slot(S), attackerOwned(AO), counterAttacks(1)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
type = stack->type;
|
|
|
|
count = baseAmount = stack->count;
|
2011-07-13 21:39:02 +03:00
|
|
|
setNodeType(STACK_BATTLE);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStack::init()
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
base = nullptr;
|
|
|
|
type = nullptr;
|
2010-12-25 21:23:30 +02:00
|
|
|
ID = -1;
|
|
|
|
count = baseAmount = -1;
|
|
|
|
firstHPleft = -1;
|
2013-03-03 20:06:03 +03:00
|
|
|
owner = PlayerColor::NEUTRAL;
|
2013-02-16 17:03:47 +03:00
|
|
|
slot = SlotID(255);
|
2010-12-25 21:23:30 +02:00
|
|
|
attackerOwned = false;
|
2011-12-22 16:05:19 +03:00
|
|
|
position = BattleHex();
|
2010-12-25 21:23:30 +02:00
|
|
|
counterAttacks = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStack::postInit()
|
|
|
|
{
|
|
|
|
assert(type);
|
2011-07-13 21:39:02 +03:00
|
|
|
assert(getParentNodes().size());
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2011-10-02 10:16:34 +03:00
|
|
|
firstHPleft = MaxHealth();
|
2011-04-12 11:59:18 +03:00
|
|
|
shots = getCreature()->valOfBonuses(Bonus::SHOTS);
|
2010-12-25 21:23:30 +02:00
|
|
|
counterAttacks = 1 + valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
|
2012-05-07 15:54:22 +03:00
|
|
|
casts = valOfBonuses(Bonus::CASTS);
|
2013-07-27 22:01:31 +03:00
|
|
|
resurrected = 0;
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
2013-03-30 23:09:50 +03:00
|
|
|
ui32 CStack::level() const
|
|
|
|
{
|
|
|
|
if (base)
|
|
|
|
return base->getLevel(); //creatture or commander
|
|
|
|
else
|
|
|
|
return std::max(1, (int)getCreature()->level); //war machine, clone etc
|
|
|
|
}
|
|
|
|
|
2011-10-20 14:03:04 +03:00
|
|
|
si32 CStack::magicResistance() const
|
|
|
|
{
|
2011-10-22 10:05:57 +03:00
|
|
|
si32 magicResistance;
|
|
|
|
if (base) //TODO: make war machines receive aura of magic resistance
|
|
|
|
{
|
|
|
|
magicResistance = base->magicResistance();
|
|
|
|
int auraBonus = 0;
|
2013-06-29 16:05:48 +03:00
|
|
|
for (const CStack * stack : base->armyObj->battle-> batteAdjacentCreatures(this))
|
2011-10-20 14:03:04 +03:00
|
|
|
{
|
|
|
|
if (stack->owner == owner)
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amax(auraBonus, stack->valOfBonuses(Bonus::SPELL_RESISTANCE_AURA)); //max value
|
2011-10-20 14:03:04 +03:00
|
|
|
}
|
|
|
|
}
|
2011-10-22 10:05:57 +03:00
|
|
|
magicResistance += auraBonus;
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amin (magicResistance, 100);
|
2011-10-22 10:05:57 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
magicResistance = type->magicResistance();
|
2011-10-20 14:03:04 +03:00
|
|
|
return magicResistance;
|
|
|
|
}
|
|
|
|
|
2011-01-20 21:57:12 +02:00
|
|
|
void CStack::stackEffectToFeature(std::vector<Bonus> & sf, const Bonus & sse)
|
2012-08-26 12:59:07 +03:00
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
const CSpell * sp = SpellID(sse.sid).toSpell();
|
2013-01-15 17:20:48 +03:00
|
|
|
|
2013-01-16 14:19:04 +03:00
|
|
|
std::vector<Bonus> tmp;
|
|
|
|
sp->getEffects(tmp, sse.val);
|
2013-01-15 17:20:48 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus& b : tmp)
|
2013-01-15 17:20:48 +03:00
|
|
|
{
|
2013-01-16 14:19:04 +03:00
|
|
|
b.turnsRemain = sse.turnsRemain;
|
|
|
|
sf.push_back(b);
|
2011-01-20 21:57:12 +02:00
|
|
|
}
|
2013-01-16 14:19:04 +03:00
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::willMove(int turn /*= 0*/) const
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
return ( turn ? true : !vstd::contains(state, EBattleStackState::DEFENDING) )
|
2010-12-25 21:23:30 +02:00
|
|
|
&& !moved(turn)
|
|
|
|
&& canMove(turn);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::canMove( int turn /*= 0*/ ) const
|
|
|
|
{
|
|
|
|
return alive()
|
2013-07-02 15:08:30 +03:00
|
|
|
&& !hasBonus(Selector::type(Bonus::NOT_ACTIVE).And(Selector::turns(turn))); //eg. Ammo Cart or blinded creature
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CStack::moved( int turn /*= 0*/ ) const
|
|
|
|
{
|
|
|
|
if(!turn)
|
2011-12-14 00:23:17 +03:00
|
|
|
return vstd::contains(state, EBattleStackState::MOVED);
|
2010-12-25 21:23:30 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
bool CStack::waited(int turn /*= 0*/) const
|
|
|
|
{
|
|
|
|
if(!turn)
|
|
|
|
return vstd::contains(state, EBattleStackState::WAITING);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
bool CStack::doubleWide() const
|
|
|
|
{
|
|
|
|
return getCreature()->doubleWide;
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
BattleHex CStack::occupiedHex() const
|
2012-08-26 12:07:48 +03:00
|
|
|
{
|
|
|
|
return occupiedHex(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleHex CStack::occupiedHex(BattleHex assumedPos) const
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
if (doubleWide())
|
|
|
|
{
|
|
|
|
if (attackerOwned)
|
2012-08-26 12:07:48 +03:00
|
|
|
return assumedPos - 1;
|
2010-12-25 21:23:30 +02:00
|
|
|
else
|
2012-08-26 12:07:48 +03:00
|
|
|
return assumedPos + 1;
|
2012-08-26 12:59:07 +03:00
|
|
|
}
|
2010-12-25 21:23:30 +02:00
|
|
|
else
|
|
|
|
{
|
2011-12-22 16:05:19 +03:00
|
|
|
return BattleHex::INVALID;
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
std::vector<BattleHex> CStack::getHexes() const
|
2012-08-26 12:07:48 +03:00
|
|
|
{
|
|
|
|
return getHexes(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos) const
|
|
|
|
{
|
|
|
|
return getHexes(assumedPos, doubleWide(), attackerOwned);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos, bool twoHex, bool AttackerOwned)
|
2011-07-03 08:55:57 +03:00
|
|
|
{
|
2011-12-22 16:05:19 +03:00
|
|
|
std::vector<BattleHex> hexes;
|
2012-08-26 12:07:48 +03:00
|
|
|
hexes.push_back(assumedPos);
|
|
|
|
|
|
|
|
if (twoHex)
|
|
|
|
{
|
|
|
|
if (AttackerOwned)
|
|
|
|
hexes.push_back(assumedPos - 1);
|
|
|
|
else
|
|
|
|
hexes.push_back(assumedPos + 1);
|
|
|
|
}
|
2011-07-03 22:10:36 +03:00
|
|
|
|
2011-07-03 08:55:57 +03:00
|
|
|
return hexes;
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
bool CStack::coversPos(BattleHex pos) const
|
2011-07-03 08:55:57 +03:00
|
|
|
{
|
2011-07-03 22:10:36 +03:00
|
|
|
return vstd::contains(getHexes(), pos);
|
2011-07-03 08:55:57 +03:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
std::vector<BattleHex> CStack::getSurroundingHexes(BattleHex attackerPos) const
|
2011-07-04 22:34:49 +03:00
|
|
|
{
|
2011-12-22 16:05:19 +03:00
|
|
|
BattleHex hex = (attackerPos != BattleHex::INVALID) ? attackerPos : position; //use hypothetical position
|
|
|
|
std::vector<BattleHex> hexes;
|
2011-07-04 22:34:49 +03:00
|
|
|
if (doubleWide())
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
const int WN = GameConstants::BFIELD_WIDTH;
|
2011-07-04 22:34:49 +03:00
|
|
|
if(attackerOwned)
|
|
|
|
{ //position is equal to front hex
|
2011-12-22 16:05:19 +03: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);
|
|
|
|
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);
|
2011-07-04 22:34:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-22 16:05:19 +03: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);
|
|
|
|
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);
|
2011-07-04 22:34:49 +03:00
|
|
|
}
|
|
|
|
return hexes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-10 16:04:20 +03:00
|
|
|
return hex.neighbouringTiles();
|
2011-07-04 22:34:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
std::vector<si32> CStack::activeSpells() const
|
|
|
|
{
|
|
|
|
std::vector<si32> ret;
|
|
|
|
|
2011-09-06 16:59:26 +03:00
|
|
|
TBonusListPtr spellEffects = getSpellBonuses();
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const Bonus *it : *spellEffects)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2011-02-20 20:32:39 +02:00
|
|
|
if (!vstd::contains(ret, it->sid)) //do not duplicate spells with multiple effects
|
|
|
|
ret.push_back(it->sid);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CStack::~CStack()
|
|
|
|
{
|
|
|
|
detachFromAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * CStack::getMyHero() const
|
|
|
|
{
|
|
|
|
if(base)
|
|
|
|
return dynamic_cast<const CGHeroInstance *>(base->armyObj);
|
|
|
|
else //we are attached directly?
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CBonusSystemNode *n : getParentNodes())
|
2012-05-30 02:22:28 +03:00
|
|
|
if(n->getNodeType() == HERO)
|
|
|
|
return dynamic_cast<const CGHeroInstance *>(n);
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CStack::nodeName() const
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "Battle stack [" << ID << "]: " << count << " creatures of ";
|
|
|
|
if(type)
|
|
|
|
oss << type->namePl;
|
|
|
|
else
|
|
|
|
oss << "[UNDEFINED TYPE]";
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
oss << " from slot " << slot;
|
2010-12-25 21:23:30 +02:00
|
|
|
if(base && base->armyObj)
|
2013-02-14 02:55:42 +03:00
|
|
|
oss << " of armyobj=" << base->armyObj->id.getNum();
|
2010-12-25 21:23:30 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
std::pair<int,int> CStack::countKilledByAttack(int damageReceived) const
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
int killedCount = 0;
|
|
|
|
int newRemainingHP = 0;
|
2011-01-08 20:33:40 +02:00
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
killedCount = damageReceived / MaxHealth();
|
|
|
|
unsigned damageFirst = damageReceived % MaxHealth();
|
2012-02-10 16:13:24 +03:00
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
if (damageReceived && vstd::contains(state, EBattleStackState::CLONED)) // block ability should not kill clone (0 damage)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
killedCount = count;
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
if( firstHPleft <= damageFirst )
|
|
|
|
{
|
|
|
|
killedCount++;
|
|
|
|
newRemainingHP = firstHPleft + MaxHealth() - damageFirst;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newRemainingHP = firstHPleft - damageFirst;
|
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
return std::make_pair(killedCount, newRemainingHP);
|
|
|
|
}
|
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
void CStack::prepareAttacked(BattleStackAttacked &bsa, CRandomGenerator & rand, boost::optional<int> customCount /*= boost::none*/) const
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
|
|
|
auto afterAttack = countKilledByAttack(bsa.damageAmount);
|
|
|
|
|
|
|
|
bsa.killedAmount = afterAttack.first;
|
|
|
|
bsa.newHP = afterAttack.second;
|
|
|
|
|
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
if(bsa.damageAmount && vstd::contains(state, EBattleStackState::CLONED)) // block ability should not kill clone (0 damage)
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
|
|
|
bsa.flags |= BattleStackAttacked::CLONE_KILLED;
|
|
|
|
return; // no rebirth I believe
|
|
|
|
}
|
|
|
|
|
|
|
|
const int countToUse = customCount ? *customCount : count;
|
|
|
|
|
|
|
|
if(countToUse <= bsa.killedAmount) //stack killed
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
bsa.newAmount = 0;
|
2011-02-24 17:33:03 +02:00
|
|
|
bsa.flags |= BattleStackAttacked::KILLED;
|
2013-06-21 23:59:32 +03:00
|
|
|
bsa.killedAmount = countToUse; //we cannot kill more creatures than we have
|
2011-07-08 17:54:20 +03:00
|
|
|
|
|
|
|
int resurrectFactor = valOfBonuses(Bonus::REBIRTH);
|
2014-03-17 22:51:07 +03:00
|
|
|
if(resurrectFactor > 0 && casts) //there must be casts left
|
2011-07-08 17:54:20 +03:00
|
|
|
{
|
2014-03-17 22:51:07 +03:00
|
|
|
int resurrectedStackCount = base->count * resurrectFactor / 100;
|
|
|
|
|
|
|
|
// last stack has proportional chance to rebirth
|
|
|
|
auto diff = base->count * resurrectFactor / 100.0 - resurrectedStackCount;
|
|
|
|
if (diff > rand.nextDouble(0, 0.99))
|
|
|
|
{
|
|
|
|
resurrectedStackCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hasBonusOfType(Bonus::REBIRTH, 1))
|
|
|
|
{
|
|
|
|
// resurrect at least one Sacred Phoenix
|
|
|
|
vstd::amax(resurrectedStackCount, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(resurrectedStackCount > 0)
|
2011-07-08 17:54:20 +03:00
|
|
|
{
|
|
|
|
bsa.flags |= BattleStackAttacked::REBIRTH;
|
2014-03-17 22:51:07 +03:00
|
|
|
bsa.newAmount = resurrectedStackCount; //risky?
|
2011-07-10 11:22:20 +03:00
|
|
|
bsa.newHP = MaxHealth(); //resore full health
|
2011-07-08 17:54:20 +03:00
|
|
|
}
|
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
bsa.newAmount = countToUse - bsa.killedAmount;
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
bool CStack::isMeleeAttackPossible(const CStack * attacker, const CStack * defender, BattleHex attackerPos /*= BattleHex::INVALID*/, BattleHex defenderPos /*= BattleHex::INVALID*/)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
if (!attackerPos.isValid())
|
|
|
|
{
|
|
|
|
attackerPos = attacker->position;
|
|
|
|
}
|
|
|
|
if (!defenderPos.isValid())
|
|
|
|
{
|
|
|
|
defenderPos = defender->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2011-12-22 16:05:19 +03:00
|
|
|
(BattleHex::mutualPosition(attackerPos, defenderPos) >= 0) //front <=> front
|
2011-01-08 20:33:40 +02:00
|
|
|
|| (attacker->doubleWide() //back <=> front
|
2011-12-22 16:05:19 +03:00
|
|
|
&& BattleHex::mutualPosition(attackerPos + (attacker->attackerOwned ? -1 : 1), defenderPos) >= 0)
|
2011-01-08 20:33:40 +02:00
|
|
|
|| (defender->doubleWide() //front <=> back
|
2011-12-22 16:05:19 +03:00
|
|
|
&& BattleHex::mutualPosition(attackerPos, defenderPos + (defender->attackerOwned ? -1 : 1)) >= 0)
|
2011-01-08 20:33:40 +02:00
|
|
|
|| (defender->doubleWide() && attacker->doubleWide()//back <=> back
|
2011-12-22 16:05:19 +03:00
|
|
|
&& BattleHex::mutualPosition(attackerPos + (attacker->attackerOwned ? -1 : 1), defenderPos + (defender->attackerOwned ? -1 : 1)) >= 0);
|
2012-08-26 12:59:07 +03:00
|
|
|
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 20:43:10 +03:00
|
|
|
bool CStack::ableToRetaliate() const //FIXME: crash after clone is killed
|
2011-09-01 04:40:46 +03:00
|
|
|
{
|
2012-08-26 12:59:07 +03:00
|
|
|
return alive()
|
2011-09-01 04:40:46 +03:00
|
|
|
&& (counterAttacks > 0 || hasBonusOfType(Bonus::UNLIMITED_RETALIATIONS))
|
|
|
|
&& !hasBonusOfType(Bonus::SIEGE_WEAPON)
|
2012-02-20 14:02:19 +03:00
|
|
|
&& !hasBonusOfType(Bonus::HYPNOTIZED)
|
|
|
|
&& !hasBonusOfType(Bonus::NO_RETALIATION);
|
2011-09-01 04:40:46 +03:00
|
|
|
}
|
|
|
|
|
2011-10-22 10:05:57 +03:00
|
|
|
std::string CStack::getName() const
|
|
|
|
{
|
|
|
|
return (count > 1) ? type->namePl : type->nameSing; //War machines can't use base
|
|
|
|
}
|
|
|
|
|
2012-02-17 00:19:07 +03:00
|
|
|
bool CStack::isValidTarget(bool allowDead/* = false*/) const /*alive non-turret stacks (can be attacked or be object of magic effect) */
|
|
|
|
{
|
|
|
|
return (alive() || allowDead) && position.isValid();
|
|
|
|
}
|
|
|
|
|
2012-03-31 00:36:07 +03:00
|
|
|
bool CStack::canBeHealed() const
|
|
|
|
{
|
2012-06-10 16:07:08 +03:00
|
|
|
return firstHPleft < MaxHealth()
|
|
|
|
&& isValidTarget()
|
2012-03-31 00:36:07 +03:00
|
|
|
&& !hasBonusOfType(Bonus::SIEGE_WEAPON);
|
|
|
|
}
|
|
|
|
|
2015-03-18 21:22:52 +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 spellpower etc. (yet?)
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
bool CMP_stack::operator()( const CStack* a, const CStack* b )
|
|
|
|
{
|
|
|
|
switch(phase)
|
|
|
|
{
|
|
|
|
case 0: //catapult moves after turrets
|
2011-05-18 20:51:10 +03:00
|
|
|
return a->getCreature()->idNumber > b->getCreature()->idNumber; //catapult is 145 and turrets are 149
|
2010-12-25 21:23:30 +02:00
|
|
|
case 1: //fastest first, upper slot first
|
|
|
|
{
|
|
|
|
int as = a->Speed(turn), bs = b->Speed(turn);
|
|
|
|
if(as != bs)
|
|
|
|
return as > bs;
|
|
|
|
else
|
|
|
|
return a->slot < b->slot;
|
|
|
|
}
|
|
|
|
case 2: //fastest last, upper slot first
|
|
|
|
//TODO: should be replaced with order of receiving morale!
|
|
|
|
case 3: //fastest last, upper slot first
|
|
|
|
{
|
|
|
|
int as = a->Speed(turn), bs = b->Speed(turn);
|
|
|
|
if(as != bs)
|
|
|
|
return as < bs;
|
|
|
|
else
|
|
|
|
return a->slot < b->slot;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CMP_stack::CMP_stack( int Phase /*= 1*/, int Turn )
|
|
|
|
{
|
|
|
|
phase = Phase;
|
|
|
|
turn = Turn;
|
|
|
|
}
|
|
|
|
|
2013-07-22 01:01:29 +03:00
|
|
|
|
|
|
|
SideInBattle::SideInBattle()
|
|
|
|
{
|
|
|
|
hero = nullptr;
|
|
|
|
armyObject = nullptr;
|
|
|
|
castSpellsCount = 0;
|
|
|
|
enchanterCounter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SideInBattle::init(const CGHeroInstance *Hero, const CArmedInstance *Army)
|
|
|
|
{
|
|
|
|
hero = Hero;
|
|
|
|
armyObject = Army;
|
|
|
|
color = armyObject->getOwner();
|
|
|
|
|
|
|
|
if(color == PlayerColor::UNFLAGGABLE)
|
|
|
|
color = PlayerColor::NEUTRAL;
|
|
|
|
}
|