2015-12-02 21:05:10 +02:00
|
|
|
/*
|
2017-03-17 17:48:44 +02:00
|
|
|
* BattleInfo.cpp, part of VCMI engine
|
2014-03-17 22:51:07 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-01 20:05:10 +02:00
|
|
|
#include "StdInc.h"
|
2017-03-17 17:48:44 +02:00
|
|
|
#include "BattleInfo.h"
|
2017-06-29 01:02:05 +02:00
|
|
|
#include "../CStack.h"
|
|
|
|
#include "../CHeroHandler.h"
|
|
|
|
#include "../NetPacks.h"
|
|
|
|
#include "../filesystem/Filesystem.h"
|
|
|
|
#include "../mapObjects/CGTownInstance.h"
|
2017-10-05 14:53:39 +02:00
|
|
|
#include "../CGeneralTextHandler.h"
|
2022-06-20 16:39:50 +02:00
|
|
|
#include "../Terrain.h"
|
2022-06-28 10:05:30 +02:00
|
|
|
#include "../BattleFieldHandler.h"
|
2022-09-15 10:06:54 +02:00
|
|
|
#include "../ObstacleHandler.h"
|
2010-12-25 21:23:30 +02:00
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
//TODO: remove
|
|
|
|
#include "../IGameCallback.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
///BattleInfo
|
2017-06-26 18:50:35 +02: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
|
|
|
}
|
|
|
|
|
2017-06-26 18:50:35 +02:00
|
|
|
void BattleInfo::calculateCasualties(std::map<ui32,si32> * casualties) const
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
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;
|
2017-07-04 13:24:46 +02:00
|
|
|
si32 killed = st->getKilled();
|
|
|
|
if(killed > 0)
|
2017-07-01 10:34:00 +02:00
|
|
|
casualties[st->side][st->getCreature()->idNumber] += killed;
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
CStack * BattleInfo::generateNewStack(uint32_t id, const CStackInstance & base, ui8 side, SlotID slot, BattleHex position)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2017-07-01 10:34:00 +02:00
|
|
|
PlayerColor owner = sides[side].color;
|
2017-06-26 18:50:35 +02:00
|
|
|
assert((owner >= PlayerColor::PLAYER_LIMIT) ||
|
|
|
|
(base.armyObj && base.armyObj->tempOwner == owner));
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
auto ret = new CStack(&base, owner, id, side, slot);
|
|
|
|
ret->initialPosition = getAvaliableHex(base.getCreatureID(), side, position); //TODO: what if no free tile on battlefield was found?
|
|
|
|
stacks.push_back(ret);
|
2010-12-25 21:23:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2012-08-28 15:28:13 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
CStack * BattleInfo::generateNewStack(uint32_t id, const CStackBasicDescriptor & base, ui8 side, SlotID slot, BattleHex position)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2017-07-01 10:34:00 +02:00
|
|
|
PlayerColor owner = sides[side].color;
|
2017-07-20 06:08:49 +02:00
|
|
|
auto ret = new CStack(&base, owner, id, side, slot);
|
|
|
|
ret->initialPosition = position;
|
|
|
|
stacks.push_back(ret);
|
2010-12-25 21:23:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-11-06 01:26:13 +02:00
|
|
|
armyObj->attachTo(*this);
|
2013-07-22 01:01:29 +03:00
|
|
|
}
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
for(CStack * s : stacks)
|
|
|
|
s->localInit(this);
|
2011-03-01 12:19:05 +02:00
|
|
|
|
|
|
|
exportBonuses();
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace CGH
|
|
|
|
{
|
2015-12-29 01:14:08 +02:00
|
|
|
static void readBattlePositions(const JsonNode &node, std::vector< std::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
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
pom.push_back((int)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
|
|
|
|
{
|
2018-04-28 19:51:30 +02:00
|
|
|
ui32 seed;
|
2012-04-23 22:56:37 +03:00
|
|
|
|
2018-04-28 19:51:30 +02:00
|
|
|
void srand(ui32 s)
|
2012-04-23 22:56:37 +03:00
|
|
|
{
|
|
|
|
seed = s;
|
|
|
|
}
|
|
|
|
void srand(int3 pos)
|
|
|
|
{
|
2018-04-28 19:51:30 +02:00
|
|
|
srand(110291 * ui32(pos.x) + 167801 * ui32(pos.y) + 81569);
|
2012-04-23 22:56:37 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2022-09-29 11:44:46 +02:00
|
|
|
BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const BattleField & 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;
|
2016-08-18 23:13:12 +02: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;
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
curB->terrainType = (*VLC->townh)[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())
|
|
|
|
{
|
2016-02-13 16:40:31 +02:00
|
|
|
curB->si.gateState = EGateState::CLOSED;
|
2016-01-29 21:35:11 +02:00
|
|
|
|
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
|
|
|
{
|
|
|
|
RandGen r;
|
2017-07-19 00:39:38 +02:00
|
|
|
auto ourRand = [&](){ return r.rand(); };
|
2012-06-16 17:25:39 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
std::vector<BattleHex> blockedTiles;
|
|
|
|
|
|
|
|
auto appropriateAbsoluteObstacle = [&](int id)
|
|
|
|
{
|
2022-09-15 10:06:54 +02:00
|
|
|
auto * info = Obstacle(id).getInfo();
|
|
|
|
return info && info->isAbsoluteObstacle && info->isAppropriate(curB->terrainType, battlefieldType);
|
2012-06-16 17:25:39 +03:00
|
|
|
};
|
2022-09-15 10:06:54 +02:00
|
|
|
auto appropriateUsualObstacle = [&](int id)
|
2012-06-16 17:25:39 +03:00
|
|
|
{
|
2022-09-15 10:06:54 +02:00
|
|
|
auto * info = Obstacle(id).getInfo();
|
|
|
|
return info && !info->isAbsoluteObstacle && info->isAppropriate(curB->terrainType, battlefieldType);
|
2012-06-16 17:25:39 +03:00
|
|
|
};
|
2022-09-15 10:06:54 +02:00
|
|
|
|
2012-06-16 17:25:39 +03:00
|
|
|
if(r.rand(1,100) <= 40) //put cliff-like obstacle
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-11-26 22:04:14 +02:00
|
|
|
RangeGenerator obidgen(0, VLC->obstacleHandler->objects.size() - 1, ourRand);
|
2015-12-29 04:43:33 +02:00
|
|
|
auto obstPtr = std::make_shared<CObstacleInstance>();
|
2012-06-16 17:25:39 +03:00
|
|
|
obstPtr->obstacleType = CObstacleInstance::ABSOLUTE_OBSTACLE;
|
|
|
|
obstPtr->ID = obidgen.getSuchNumber(appropriateAbsoluteObstacle);
|
2020-10-01 10:38:06 +02:00
|
|
|
obstPtr->uniqueID = static_cast<si32>(curB->obstacles.size());
|
2012-06-16 17:25:39 +03:00
|
|
|
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);
|
2022-09-15 10:06:54 +02:00
|
|
|
tilesToBlock -= Obstacle(obstPtr->ID).getInfo()->blockedTiles.size() / 2;
|
2012-06-16 17:25:39 +03:00
|
|
|
}
|
|
|
|
catch(RangeGenerator::ExhaustedPossibilities &)
|
|
|
|
{
|
|
|
|
//silently ignore, if we can't place absolute obstacle, we'll go with the usual ones
|
2017-07-13 17:55:49 +02:00
|
|
|
logGlobal->debug("RangeGenerator::ExhaustedPossibilities exception occured - cannot place absolute obstacle");
|
2012-06-16 17:25:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while(tilesToBlock > 0)
|
2017-08-11 19:03:05 +02:00
|
|
|
{
|
2022-11-26 22:04:14 +02:00
|
|
|
RangeGenerator obidgen(0, VLC->obstacleHandler->objects.size() - 1, ourRand);
|
2017-07-13 17:55:49 +02:00
|
|
|
auto tileAccessibility = curB->getAccesibility();
|
2012-06-16 17:25:39 +03:00
|
|
|
const int obid = obidgen.getSuchNumber(appropriateUsualObstacle);
|
2022-09-15 10:06:54 +02:00
|
|
|
const ObstacleInfo &obi = *Obstacle(obid).getInfo();
|
2012-06-16 17:25:39 +03:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-07-13 17:55:49 +02:00
|
|
|
if(tileAccessibility[blocked] == EAccessibility::UNAVAILABLE) //for ship-to-ship battlefield - exclude hardcoded unavailable tiles
|
|
|
|
return false;
|
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);
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
auto obstPtr = std::make_shared<CObstacleInstance>();
|
2012-06-16 17:25:39 +03:00
|
|
|
obstPtr->ID = obid;
|
|
|
|
obstPtr->pos = posgenerator.getSuchNumber(validPosition);
|
2020-10-01 10:38:06 +02:00
|
|
|
obstPtr->uniqueID = static_cast<si32>(curB->obstacles.size());
|
2012-06-16 17:25:39 +03:00
|
|
|
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);
|
2020-10-01 10:38:06 +02:00
|
|
|
tilesToBlock -= static_cast<int>(obi.blockedTiles.size());
|
2012-06-16 17:25:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(RangeGenerator::ExhaustedPossibilities &)
|
|
|
|
{
|
2017-07-13 17:55:49 +02:00
|
|
|
logGlobal->debug("RangeGenerator::ExhaustedPossibilities exception occured - cannot place usual obstacle");
|
2012-06-16 17:25:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
commanderField.push_back ((int)position.Float());
|
2012-04-28 14:41:20 +03:00
|
|
|
}
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto position : config["commanderPositions"]["creBank"].Vector())
|
2012-04-28 14:41:20 +03:00
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
commanderBank.push_back ((int)position.Float());
|
2012-04-28 14:41:20 +03:00
|
|
|
}
|
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
|
2017-05-26 18:51:45 +02:00
|
|
|
auto handleWarMachine= [&](int side, ArtifactPosition artslot, BattleHex hex)
|
2012-08-28 15:28:13 +03:00
|
|
|
{
|
2017-05-26 18:51:45 +02:00
|
|
|
const CArtifactInstance * warMachineArt = heroes[side]->getArt(artslot);
|
|
|
|
|
|
|
|
if(nullptr != warMachineArt)
|
|
|
|
{
|
|
|
|
CreatureID cre = warMachineArt->artType->warMachine;
|
|
|
|
|
|
|
|
if(cre != CreatureID::NONE)
|
2017-07-20 06:08:49 +02:00
|
|
|
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(cre, 1), side, SlotID::WAR_MACHINES_SLOT, hex);
|
2017-05-26 18:51:45 +02:00
|
|
|
}
|
2012-08-28 15:28:13 +03:00
|
|
|
};
|
|
|
|
|
2017-05-26 18:51:45 +02:00
|
|
|
if(heroes[0])
|
|
|
|
{
|
2012-08-28 15:28:13 +03:00
|
|
|
|
2017-05-26 18:51:45 +02:00
|
|
|
handleWarMachine(0, ArtifactPosition::MACH1, 52);
|
|
|
|
handleWarMachine(0, ArtifactPosition::MACH2, 18);
|
|
|
|
handleWarMachine(0, ArtifactPosition::MACH3, 154);
|
|
|
|
if(town && town->hasFort())
|
|
|
|
handleWarMachine(0, ArtifactPosition::MACH4, 120);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(heroes[1])
|
|
|
|
{
|
|
|
|
if(!town) //defending hero shouldn't receive ballista (bug #551)
|
|
|
|
handleWarMachine(1, ArtifactPosition::MACH1, 66);
|
|
|
|
handleWarMachine(1, ArtifactPosition::MACH2, 32);
|
|
|
|
handleWarMachine(1, ArtifactPosition::MACH3, 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
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
curB->generateNewStack(curB->nextUnitId(), *i->second, side, i->first, pos);
|
2012-08-30 19:01:19 +03:00
|
|
|
}
|
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)
|
|
|
|
{
|
2016-10-02 16:25:20 +02:00
|
|
|
if (heroes[i] && heroes[i]->commander && heroes[i]->commander->alive)
|
2012-04-28 14:41:20 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
curB->generateNewStack(curB->nextUnitId(), *heroes[i]->commander, i, SlotID::COMMANDER_SLOT_PLACEHOLDER, creatureBank ? commanderBank[i] : commanderField[i]);
|
2012-04-28 14:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
2022-11-17 18:50:12 +02:00
|
|
|
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), 1, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_CENTRAL_TOWER);
|
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
|
2022-11-17 18:50:12 +02:00
|
|
|
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), 1, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_UPPER_TOWER);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2022-11-17 18:50:12 +02:00
|
|
|
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), 1, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_BOTTOM_TOWER);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
2012-05-18 23:50:16 +03:00
|
|
|
|
|
|
|
//moat
|
2015-12-29 04:43:33 +02:00
|
|
|
auto moat = std::make_shared<MoatObstacle>();
|
2012-05-18 23:50:16 +03:00
|
|
|
moat->ID = curB->town->subID;
|
|
|
|
moat->obstacleType = CObstacleInstance::MOAT;
|
2020-10-01 10:38:06 +02:00
|
|
|
moat->uniqueID = static_cast<si32>(curB->obstacles.size());
|
2012-05-18 23:50:16 +03:00
|
|
|
curB->obstacles.push_back(moat);
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::stable_sort(stacks.begin(),stacks.end(),cmpst);
|
|
|
|
|
2016-10-01 13:10:09 +02:00
|
|
|
auto neutral = std::make_shared<CreatureAlignmentLimiter>(EAlignment::NEUTRAL);
|
|
|
|
auto good = std::make_shared<CreatureAlignmentLimiter>(EAlignment::GOOD);
|
|
|
|
auto evil = std::make_shared<CreatureAlignmentLimiter>(EAlignment::EVIL);
|
|
|
|
|
2022-06-28 10:05:30 +02:00
|
|
|
auto bgInfo = VLC->battlefields()->getById(battlefieldType);
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2022-06-28 10:05:30 +02:00
|
|
|
for(const std::shared_ptr<Bonus> & bonus : bgInfo->bonuses)
|
2022-06-22 10:41:02 +02:00
|
|
|
{
|
2022-06-28 10:05:30 +02:00
|
|
|
curB->addNewBonus(bonus);
|
2022-06-22 10:41:02 +02:00
|
|
|
}
|
2010-12-25 21:23:30 +02:00
|
|
|
|
|
|
|
//native terrain bonuses
|
2021-02-03 19:19:56 +02:00
|
|
|
static auto nativeTerrain = std::make_shared<CreatureTerrainLimiter>();
|
|
|
|
|
2016-10-01 13:10:09 +02:00
|
|
|
curB->addNewBonus(std::make_shared<Bonus>(Bonus::ONE_BATTLE, Bonus::STACKS_SPEED, Bonus::TERRAIN_NATIVE, 1, 0, 0)->addLimiter(nativeTerrain));
|
|
|
|
curB->addNewBonus(std::make_shared<Bonus>(Bonus::ONE_BATTLE, Bonus::PRIMARY_SKILL, Bonus::TERRAIN_NATIVE, 1, 0, PrimarySkill::ATTACK)->addLimiter(nativeTerrain));
|
|
|
|
curB->addNewBonus(std::make_shared<Bonus>(Bonus::ONE_BATTLE, Bonus::PRIMARY_SKILL, Bonus::TERRAIN_NATIVE, 1, 0, PrimarySkill::DEFENSE)->addLimiter(nativeTerrain));
|
2010-12-25 21:23:30 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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])
|
2017-08-25 15:43:20 +02:00
|
|
|
tacticLvls[i] += heroes[i]->valOfBonuses(Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, 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;
|
2017-09-09 23:26:56 +02:00
|
|
|
//bonus specifies distance you can move beyond base row; this allows 100% compatibility with HMM3 mechanics
|
|
|
|
curB->tacticDistance = 1 + std::abs(tacticsSkillDiff);
|
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-12 18:12:48 +02:00
|
|
|
return curB;
|
|
|
|
}
|
2010-12-25 21:23:30 +02:00
|
|
|
|
2021-02-03 19:19:56 +02:00
|
|
|
|
2017-06-26 18:50:35 +02: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
|
|
|
|
2017-08-12 13:36:04 +02:00
|
|
|
logGlobal->error("Player %s is not in battle!", player.getStr());
|
2013-07-22 01:01:29 +03:00
|
|
|
return nullptr;
|
2011-02-21 18:53:23 +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;
|
|
|
|
|
2017-08-12 13:36:04 +02:00
|
|
|
logGlobal->warn("BattleInfo::whatSide: Player %s is not in battle!", player.getStr());
|
2011-03-05 18:38:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
CStack * BattleInfo::getStack(int stackID, bool onlyAlive)
|
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
|
|
|
}
|
|
|
|
|
2022-09-18 16:39:10 +02:00
|
|
|
BattleInfo::BattleInfo():
|
|
|
|
round(-1),
|
|
|
|
activeStack(-1),
|
|
|
|
town(nullptr),
|
|
|
|
tile(-1,-1,-1),
|
|
|
|
battlefieldType(BattleField::NONE),
|
|
|
|
terrainType(),
|
|
|
|
tacticsSide(0),
|
|
|
|
tacticDistance(0)
|
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
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
BattleInfo::~BattleInfo() = default;
|
|
|
|
|
|
|
|
int32_t BattleInfo::getActiveStackID() const
|
|
|
|
{
|
|
|
|
return activeStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
TStacks BattleInfo::getStacksIf(TStackFilter predicate) const
|
|
|
|
{
|
|
|
|
TStacks ret;
|
|
|
|
vstd::copy_if(stacks, std::back_inserter(ret), predicate);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
battle::Units BattleInfo::getUnitsIf(battle::UnitFilter predicate) const
|
|
|
|
{
|
|
|
|
battle::Units ret;
|
|
|
|
vstd::copy_if(stacks, std::back_inserter(ret), predicate);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-22 10:41:02 +02:00
|
|
|
BattleField BattleInfo::getBattlefieldType() const
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
return battlefieldType;
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:44:46 +02:00
|
|
|
TerrainId BattleInfo::getTerrainType() const
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
return terrainType;
|
|
|
|
}
|
|
|
|
|
|
|
|
IBattleInfo::ObstacleCList BattleInfo::getAllObstacles() const
|
|
|
|
{
|
|
|
|
ObstacleCList ret;
|
|
|
|
|
|
|
|
for(auto iter = obstacles.cbegin(); iter != obstacles.cend(); iter++)
|
|
|
|
ret.push_back(*iter);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor BattleInfo::getSidePlayer(ui8 side) const
|
|
|
|
{
|
|
|
|
return sides.at(side).color;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CArmedInstance * BattleInfo::getSideArmy(ui8 side) const
|
|
|
|
{
|
|
|
|
return sides.at(side).armyObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * BattleInfo::getSideHero(ui8 side) const
|
|
|
|
{
|
|
|
|
return sides.at(side).hero;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui8 BattleInfo::getTacticDist() const
|
|
|
|
{
|
|
|
|
return tacticDistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui8 BattleInfo::getTacticsSide() const
|
|
|
|
{
|
|
|
|
return tacticsSide;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGTownInstance * BattleInfo::getDefendedTown() const
|
|
|
|
{
|
|
|
|
return town;
|
|
|
|
}
|
|
|
|
|
|
|
|
si8 BattleInfo::getWallState(int partOfWall) const
|
|
|
|
{
|
|
|
|
return si.wallState.at(partOfWall);
|
|
|
|
}
|
|
|
|
|
|
|
|
EGateState BattleInfo::getGateState() const
|
|
|
|
{
|
|
|
|
return si.gateState;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t BattleInfo::getCastSpells(ui8 side) const
|
|
|
|
{
|
|
|
|
return sides.at(side).castSpellsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t BattleInfo::getEnchanterCounter(ui8 side) const
|
|
|
|
{
|
|
|
|
return sides.at(side).enchanterCounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IBonusBearer * BattleInfo::asBearer() const
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t BattleInfo::getActualDamage(const TDmgRange & damage, int32_t attackerCount, vstd::RNG & rng) const
|
|
|
|
{
|
|
|
|
if(damage.first != damage.second)
|
|
|
|
{
|
|
|
|
int64_t sum = 0;
|
|
|
|
|
|
|
|
auto howManyToAv = std::min<int32_t>(10, attackerCount);
|
|
|
|
auto rangeGen = rng.getInt64Range(damage.first, damage.second);
|
|
|
|
|
|
|
|
for(int32_t g = 0; g < howManyToAv; ++g)
|
|
|
|
sum += rangeGen();
|
|
|
|
|
|
|
|
return sum / howManyToAv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return damage.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::nextRound(int32_t roundNr)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
sides.at(i).castSpellsCount = 0;
|
|
|
|
vstd::amax(--sides.at(i).enchanterCounter, 0);
|
|
|
|
}
|
|
|
|
round = roundNr;
|
|
|
|
|
|
|
|
for(CStack * s : stacks)
|
|
|
|
{
|
|
|
|
// new turn effects
|
2017-09-09 14:25:55 +02:00
|
|
|
s->reduceBonusDurations(Bonus::NTurns);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
s->afterNewRound();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto & obst : obstacles)
|
|
|
|
obst->battleTurnPassed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::nextTurn(uint32_t unitId)
|
|
|
|
{
|
|
|
|
activeStack = unitId;
|
|
|
|
|
|
|
|
CStack * st = getStack(activeStack);
|
|
|
|
|
|
|
|
//remove bonuses that last until when stack gets new turn
|
2018-03-05 08:51:35 +02:00
|
|
|
st->removeBonusesRecursive(Bonus::UntilGetsTurn);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
st->afterGetsTurn();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::addUnit(uint32_t id, const JsonNode & data)
|
|
|
|
{
|
|
|
|
battle::UnitInfo info;
|
|
|
|
info.load(id, data);
|
|
|
|
CStackBasicDescriptor base(info.type, info.count);
|
|
|
|
|
|
|
|
PlayerColor owner = getSidePlayer(info.side);
|
|
|
|
|
|
|
|
auto ret = new CStack(&base, owner, info.id, info.side, SlotID::SUMMONED_SLOT_PLACEHOLDER);
|
|
|
|
ret->initialPosition = info.position;
|
|
|
|
stacks.push_back(ret);
|
|
|
|
ret->localInit(this);
|
2018-02-23 20:58:05 +02:00
|
|
|
ret->summoned = info.summoned;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::moveUnit(uint32_t id, BattleHex destination)
|
|
|
|
{
|
|
|
|
auto sta = getStack(id);
|
|
|
|
if(!sta)
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot find stack %d", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sta->position = destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta)
|
|
|
|
{
|
|
|
|
CStack * changedStack = getStack(id, false);
|
|
|
|
if(!changedStack)
|
|
|
|
throw std::runtime_error("Invalid unit id in BattleInfo update");
|
|
|
|
|
|
|
|
if(!changedStack->alive() && healthDelta > 0)
|
|
|
|
{
|
|
|
|
//checking if we resurrect a stack that is under a living stack
|
|
|
|
auto accessibility = getAccesibility();
|
|
|
|
|
|
|
|
if(!accessibility.accessible(changedStack->getPosition(), changedStack))
|
|
|
|
{
|
|
|
|
logNetwork->error("Cannot resurrect %s because hex %d is occupied!", changedStack->nodeName(), changedStack->getPosition().hex);
|
|
|
|
return; //position is already occupied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool killed = (-healthDelta) >= changedStack->getAvailableHealth();//todo: check using alive state once rebirth will be handled separately
|
|
|
|
|
|
|
|
bool resurrected = !changedStack->alive() && healthDelta > 0;
|
|
|
|
|
|
|
|
//applying changes
|
|
|
|
changedStack->load(data);
|
|
|
|
|
|
|
|
|
|
|
|
if(healthDelta < 0)
|
|
|
|
{
|
2018-03-05 08:51:35 +02:00
|
|
|
changedStack->removeBonusesRecursive(Bonus::UntilBeingAttacked);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resurrected = resurrected || (killed && changedStack->alive());
|
|
|
|
|
|
|
|
if(killed)
|
|
|
|
{
|
|
|
|
if(changedStack->cloneID >= 0)
|
|
|
|
{
|
|
|
|
//remove clone as well
|
|
|
|
CStack * clone = getStack(changedStack->cloneID);
|
|
|
|
if(clone)
|
|
|
|
clone->makeGhost();
|
|
|
|
|
|
|
|
changedStack->cloneID = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(resurrected || killed)
|
|
|
|
{
|
|
|
|
//removing all spells effects
|
|
|
|
auto selector = [](const Bonus * b)
|
|
|
|
{
|
|
|
|
//Special case: DISRUPTING_RAY is absolutely permanent
|
2019-03-20 21:58:15 +02:00
|
|
|
return b->source == Bonus::SPELL_EFFECT && b->sid != SpellID::DISRUPTING_RAY;
|
2017-07-20 06:08:49 +02:00
|
|
|
};
|
2018-03-05 08:51:35 +02:00
|
|
|
changedStack->removeBonusesRecursive(selector);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!changedStack->alive() && changedStack->isClone())
|
|
|
|
{
|
|
|
|
for(CStack * s : stacks)
|
|
|
|
{
|
|
|
|
if(s->cloneID == changedStack->unitId())
|
|
|
|
s->cloneID = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::removeUnit(uint32_t id)
|
|
|
|
{
|
|
|
|
std::set<uint32_t> ids;
|
|
|
|
ids.insert(id);
|
|
|
|
|
|
|
|
while(!ids.empty())
|
|
|
|
{
|
|
|
|
auto toRemoveId = *ids.begin();
|
|
|
|
auto toRemove = getStack(toRemoveId, false);
|
|
|
|
|
|
|
|
if(!toRemove)
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot find stack %d", toRemoveId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!toRemove->ghost)
|
|
|
|
{
|
|
|
|
toRemove->onRemoved();
|
|
|
|
toRemove->detachFromAll();
|
|
|
|
|
|
|
|
//stack may be removed instantly (not being killed first)
|
|
|
|
//handle clone remove also here
|
|
|
|
if(toRemove->cloneID >= 0)
|
|
|
|
{
|
|
|
|
ids.insert(toRemove->cloneID);
|
|
|
|
toRemove->cloneID = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//cleanup remaining clone links if any
|
|
|
|
for(auto s : stacks)
|
|
|
|
{
|
|
|
|
if(s->cloneID == toRemoveId)
|
|
|
|
s->cloneID = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ids.erase(toRemoveId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
void BattleInfo::updateUnit(uint32_t id, const JsonNode & data)
|
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void BattleInfo::addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
|
|
|
|
{
|
|
|
|
CStack * sta = getStack(id, false);
|
|
|
|
|
|
|
|
if(!sta)
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot find stack %d", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const Bonus & b : bonus)
|
|
|
|
addOrUpdateUnitBonus(sta, b, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
|
|
|
|
{
|
|
|
|
CStack * sta = getStack(id, false);
|
|
|
|
|
|
|
|
if(!sta)
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot find stack %d", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const Bonus & b : bonus)
|
|
|
|
addOrUpdateUnitBonus(sta, b, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
|
|
|
|
{
|
|
|
|
CStack * sta = getStack(id, false);
|
|
|
|
|
|
|
|
if(!sta)
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot find stack %d", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const Bonus & one : bonus)
|
|
|
|
{
|
|
|
|
auto selector = [one](const Bonus * b)
|
|
|
|
{
|
|
|
|
//compare everything but turnsRemain, limiter and propagator
|
|
|
|
return one.duration == b->duration
|
|
|
|
&& one.type == b->type
|
|
|
|
&& one.subtype == b->subtype
|
|
|
|
&& one.source == b->source
|
|
|
|
&& one.val == b->val
|
|
|
|
&& one.sid == b->sid
|
|
|
|
&& one.valType == b->valType
|
|
|
|
&& one.additionalInfo == b->additionalInfo
|
|
|
|
&& one.effectRange == b->effectRange
|
|
|
|
&& one.description == b->description;
|
|
|
|
};
|
2018-03-05 08:51:35 +02:00
|
|
|
sta->removeBonusesRecursive(selector);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t BattleInfo::nextUnitId() const
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
return static_cast<uint32_t>(stacks.size());
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::addOrUpdateUnitBonus(CStack * sta, const Bonus & value, bool forceAdd)
|
|
|
|
{
|
|
|
|
if(forceAdd || !sta->hasBonus(Selector::source(Bonus::SPELL_EFFECT, value.sid).And(Selector::typeSubtype(value.type, value.subtype))))
|
|
|
|
{
|
|
|
|
//no such effect or cumulative - add new
|
|
|
|
logBonus->trace("%s receives a new bonus: %s", sta->nodeName(), value.Description());
|
|
|
|
sta->addNewBonus(std::make_shared<Bonus>(value));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logBonus->trace("%s updated bonus: %s", sta->nodeName(), value.Description());
|
|
|
|
|
|
|
|
for(auto stackBonus : sta->getExportedBonusList()) //TODO: optimize
|
|
|
|
{
|
|
|
|
if(stackBonus->source == value.source && stackBonus->sid == value.sid && stackBonus->type == value.type && stackBonus->subtype == value.subtype)
|
|
|
|
{
|
|
|
|
stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, value.turnsRemain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CBonusSystemNode::treeHasChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::setWallState(int partOfWall, si8 state)
|
|
|
|
{
|
|
|
|
si.wallState.at(partOfWall) = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::addObstacle(const ObstacleChanges & changes)
|
|
|
|
{
|
|
|
|
std::shared_ptr<SpellCreatedObstacle> obstacle = std::make_shared<SpellCreatedObstacle>();
|
|
|
|
obstacle->fromInfo(changes);
|
|
|
|
obstacles.push_back(obstacle);
|
|
|
|
}
|
|
|
|
|
2020-02-12 19:12:12 +02:00
|
|
|
void BattleInfo::updateObstacle(const ObstacleChanges& changes)
|
|
|
|
{
|
|
|
|
std::shared_ptr<SpellCreatedObstacle> changedObstacle = std::make_shared<SpellCreatedObstacle>();
|
|
|
|
changedObstacle->fromInfo(changes);
|
|
|
|
|
|
|
|
for(int i = 0; i < obstacles.size(); ++i)
|
|
|
|
{
|
|
|
|
if(obstacles[i]->uniqueID == changes.id) // update this obstacle
|
|
|
|
{
|
|
|
|
SpellCreatedObstacle * spellObstacle = dynamic_cast<SpellCreatedObstacle *>(obstacles[i].get());
|
|
|
|
assert(spellObstacle);
|
|
|
|
|
|
|
|
// Currently we only support to update the "revealed" property
|
|
|
|
spellObstacle->revealed = changedObstacle->revealed;
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
|
2020-02-12 19:12:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void BattleInfo::removeObstacle(uint32_t id)
|
|
|
|
{
|
|
|
|
for(int i=0; i < obstacles.size(); ++i)
|
|
|
|
{
|
|
|
|
if(obstacles[i]->uniqueID == id) //remove this obstacle
|
|
|
|
{
|
|
|
|
obstacles.erase(obstacles.begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2022-09-21 18:31:14 +02:00
|
|
|
#if SCRIPTING_ENABLED
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
scripting::Pool * BattleInfo::getContextPool() const
|
|
|
|
{
|
|
|
|
//this is real battle, use global scripting context pool
|
|
|
|
//TODO: make this line not ugly
|
|
|
|
return IObjectInterface::cb->getGlobalContextPool();
|
|
|
|
}
|
2022-09-21 18:31:14 +02:00
|
|
|
#endif
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
bool CMP_stack::operator()(const battle::Unit * a, const battle::Unit * b)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
switch(phase)
|
|
|
|
{
|
|
|
|
case 0: //catapult moves after turrets
|
2017-07-20 06:08:49 +02:00
|
|
|
return a->creatureIndex() > b->creatureIndex(); //catapult is 145 and turrets are 149
|
2021-01-30 14:13:45 +02:00
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
int as = a->getInitiative(turn), bs = b->getInitiative(turn);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
|
2010-12-25 21:23:30 +02:00
|
|
|
if(as != bs)
|
2019-05-16 15:41:02 +02:00
|
|
|
return as > bs;
|
2020-05-11 17:03:37 +02:00
|
|
|
|
|
|
|
if(a->unitSide() == b->unitSide())
|
|
|
|
return a->unitSlot() < b->unitSlot();
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
|
2021-01-30 14:13:45 +02:00
|
|
|
return (a->unitSide() == side || b->unitSide() == side)
|
|
|
|
? a->unitSide() != side
|
|
|
|
: a->unitSide() < b->unitSide();
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
}
|
2010-12-25 21:23:30 +02:00
|
|
|
default:
|
2020-05-11 17:03:37 +02:00
|
|
|
assert(false);
|
2010-12-25 21:23:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-11 17:03:37 +02:00
|
|
|
|
|
|
|
assert(false);
|
|
|
|
return false;
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
2019-05-17 10:34:59 +02:00
|
|
|
CMP_stack::CMP_stack(int Phase, int Turn, uint8_t Side)
|
2010-12-25 21:23:30 +02:00
|
|
|
{
|
|
|
|
phase = Phase;
|
|
|
|
turn = Turn;
|
2019-05-16 15:41:02 +02:00
|
|
|
side = Side;
|
2010-12-25 21:23:30 +02:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|