2023-10-19 16:19:09 +02:00
|
|
|
/*
|
|
|
|
* BattleInfo.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "BattleInfo.h"
|
2024-08-31 23:04:32 +02:00
|
|
|
|
|
|
|
#include "BattleLayout.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "CObstacleInstance.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "bonuses/Limiters.h"
|
|
|
|
#include "bonuses/Updaters.h"
|
|
|
|
#include "../CStack.h"
|
2024-08-28 21:33:56 +02:00
|
|
|
#include "../entities/building/TownFortifications.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "../filesystem/Filesystem.h"
|
|
|
|
#include "../mapObjects/CGTownInstance.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../texts/CGeneralTextHandler.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "../BattleFieldHandler.h"
|
|
|
|
#include "../ObstacleHandler.h"
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
#include <vstd/RNG.h>
|
2024-01-31 00:17:40 +02:00
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
//TODO: remove
|
|
|
|
#include "../IGameCallback.h"
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
const SideInBattle & BattleInfo::getSide(BattleSide side) const
|
|
|
|
{
|
|
|
|
return sides.at(side);
|
|
|
|
}
|
|
|
|
|
|
|
|
SideInBattle & BattleInfo::getSide(BattleSide side)
|
|
|
|
{
|
|
|
|
return sides.at(side);
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
///BattleInfo
|
2024-08-11 22:22:35 +02:00
|
|
|
CStack * BattleInfo::generateNewStack(uint32_t id, const CStackInstance & base, BattleSide side, const SlotID & slot, BattleHex position)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
PlayerColor owner = getSide(side).color;
|
2023-10-19 16:19:09 +02:00
|
|
|
assert(!owner.isValidPlayer() || (base.armyObj && base.armyObj->tempOwner == owner));
|
|
|
|
|
|
|
|
auto * ret = new CStack(&base, owner, id, side, slot);
|
2024-06-24 03:23:26 +02:00
|
|
|
ret->initialPosition = getAvailableHex(base.getCreatureID(), side, position); //TODO: what if no free tile on battlefield was found?
|
2023-10-19 16:19:09 +02:00
|
|
|
stacks.push_back(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
CStack * BattleInfo::generateNewStack(uint32_t id, const CStackBasicDescriptor & base, BattleSide side, const SlotID & slot, BattleHex position)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
PlayerColor owner = getSide(side).color;
|
2023-10-19 16:19:09 +02:00
|
|
|
auto * ret = new CStack(&base, owner, id, side, slot);
|
|
|
|
ret->initialPosition = position;
|
|
|
|
stacks.push_back(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::localInit()
|
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
for(BattleSide i : { BattleSide::ATTACKER, BattleSide::DEFENDER})
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
auto * armyObj = battleGetArmyObject(i);
|
|
|
|
armyObj->battle = this;
|
|
|
|
armyObj->attachTo(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(CStack * s : stacks)
|
|
|
|
s->localInit(this);
|
|
|
|
|
|
|
|
exportBonuses();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//RNG that works like H3 one
|
|
|
|
struct RandGen
|
|
|
|
{
|
|
|
|
ui32 seed;
|
|
|
|
|
|
|
|
void srand(ui32 s)
|
|
|
|
{
|
|
|
|
seed = s;
|
|
|
|
}
|
|
|
|
void srand(const int3 & pos)
|
|
|
|
{
|
|
|
|
srand(110291 * static_cast<ui32>(pos.x) + 167801 * static_cast<ui32>(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
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
RangeGenerator(int _min, int _max, std::function<int()> _myRand):
|
|
|
|
min(_min),
|
|
|
|
remainingCount(_max - _min + 1),
|
|
|
|
remaining(remainingCount, true),
|
|
|
|
myRand(std::move(_myRand))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int generateNumber() const
|
|
|
|
{
|
|
|
|
if(!remainingCount)
|
|
|
|
throw ExhaustedPossibilities();
|
|
|
|
if(remainingCount == 1)
|
|
|
|
return 0;
|
|
|
|
return myRand() % remainingCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
//get number fulfilling predicate. Never gives the same number twice.
|
|
|
|
int getSuchNumber(const std::function<bool(int)> & goodNumberPred = nullptr)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int n = generateNumber();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-10 00:38:54 +02:00
|
|
|
int min;
|
|
|
|
int remainingCount;
|
2023-10-19 16:19:09 +02:00
|
|
|
std::vector<bool> remaining;
|
|
|
|
std::function<int()> myRand;
|
|
|
|
};
|
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const BattleField & battlefieldType, BattleSideArray<const CArmedInstance *> armies, BattleSideArray<const CGHeroInstance *> heroes, const BattleLayout & layout, const CGTownInstance * town)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
CMP_stack cmpst;
|
2024-10-16 14:14:47 +02:00
|
|
|
auto * currentBattle = new BattleInfo(layout);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
for(auto i : { BattleSide::LEFT_SIDE, BattleSide::RIGHT_SIDE})
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->sides[i].init(heroes[i], armies[i]);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-10-16 14:14:47 +02:00
|
|
|
std::vector<CStack*> & stacks = (currentBattle->stacks);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->tile = tile;
|
|
|
|
currentBattle->terrainType = terrain;
|
|
|
|
currentBattle->battlefieldType = battlefieldType;
|
|
|
|
currentBattle->round = -2;
|
|
|
|
currentBattle->activeStack = -1;
|
|
|
|
currentBattle->replayAllowed = false;
|
|
|
|
currentBattle->town = town;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
//setting up siege obstacles
|
2024-08-28 21:33:56 +02:00
|
|
|
if (town && town->fortificationsLevel().wallsHealth != 0)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-28 21:33:56 +02:00
|
|
|
auto fortification = town->fortificationsLevel();
|
|
|
|
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->si.gateState = EGateState::CLOSED;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->si.wallState[EWallPart::GATE] = EWallState::INTACT;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
for(const auto wall : {EWallPart::BOTTOM_WALL, EWallPart::BELOW_GATE, EWallPart::OVER_GATE, EWallPart::UPPER_WALL})
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->si.wallState[wall] = static_cast<EWallState>(fortification.wallsHealth);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-08-28 21:33:56 +02:00
|
|
|
if (fortification.citadelHealth != 0)
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->si.wallState[EWallPart::KEEP] = static_cast<EWallState>(fortification.citadelHealth);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-08-28 21:33:56 +02:00
|
|
|
if (fortification.upperTowerHealth != 0)
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->si.wallState[EWallPart::UPPER_TOWER] = static_cast<EWallState>(fortification.upperTowerHealth);
|
2024-08-28 21:33:56 +02:00
|
|
|
|
|
|
|
if (fortification.lowerTowerHealth != 0)
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->si.wallState[EWallPart::BOTTOM_TOWER] = static_cast<EWallState>(fortification.lowerTowerHealth);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//randomize obstacles
|
2024-10-16 14:14:47 +02:00
|
|
|
if (layout.obstaclesAllowed && (!town || !town->hasFort()))
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
std::vector<BattleHex> blockedTiles;
|
|
|
|
|
|
|
|
auto appropriateAbsoluteObstacle = [&](int id)
|
|
|
|
{
|
|
|
|
const auto * info = Obstacle(id).getInfo();
|
2024-10-16 14:14:47 +02:00
|
|
|
return info && info->isAbsoluteObstacle && info->isAppropriate(currentBattle->terrainType, battlefieldType);
|
2023-10-19 16:19:09 +02:00
|
|
|
};
|
|
|
|
auto appropriateUsualObstacle = [&](int id)
|
|
|
|
{
|
|
|
|
const auto * info = Obstacle(id).getInfo();
|
2024-10-16 14:14:47 +02:00
|
|
|
return info && !info->isAbsoluteObstacle && info->isAppropriate(currentBattle->terrainType, battlefieldType);
|
2023-10-19 16:19:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(r.rand(1,100) <= 40) //put cliff-like obstacle
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-01-16 19:06:41 +02:00
|
|
|
RangeGenerator obidgen(0, VLC->obstacleHandler->size() - 1, ourRand);
|
2023-10-19 16:19:09 +02:00
|
|
|
auto obstPtr = std::make_shared<CObstacleInstance>();
|
|
|
|
obstPtr->obstacleType = CObstacleInstance::ABSOLUTE_OBSTACLE;
|
|
|
|
obstPtr->ID = obidgen.getSuchNumber(appropriateAbsoluteObstacle);
|
2024-10-16 14:14:47 +02:00
|
|
|
obstPtr->uniqueID = static_cast<si32>(currentBattle->obstacles.size());
|
|
|
|
currentBattle->obstacles.push_back(obstPtr);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
for(BattleHex blocked : obstPtr->getBlockedTiles())
|
|
|
|
blockedTiles.push_back(blocked);
|
|
|
|
tilesToBlock -= Obstacle(obstPtr->ID).getInfo()->blockedTiles.size() / 2;
|
|
|
|
}
|
|
|
|
catch(RangeGenerator::ExhaustedPossibilities &)
|
|
|
|
{
|
|
|
|
//silently ignore, if we can't place absolute obstacle, we'll go with the usual ones
|
|
|
|
logGlobal->debug("RangeGenerator::ExhaustedPossibilities exception occurred - cannot place absolute obstacle");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while(tilesToBlock > 0)
|
|
|
|
{
|
2024-01-16 19:06:41 +02:00
|
|
|
RangeGenerator obidgen(0, VLC->obstacleHandler->size() - 1, ourRand);
|
2024-10-16 14:14:47 +02:00
|
|
|
auto tileAccessibility = currentBattle->getAccessibility();
|
2023-10-19 16:19:09 +02:00
|
|
|
const int obid = obidgen.getSuchNumber(appropriateUsualObstacle);
|
|
|
|
const ObstacleInfo &obi = *Obstacle(obid).getInfo();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
for(BattleHex blocked : obi.getBlocked(pos))
|
|
|
|
{
|
|
|
|
if(tileAccessibility[blocked] == EAccessibility::UNAVAILABLE) //for ship-to-ship battlefield - exclude hardcoded unavailable tiles
|
|
|
|
return false;
|
|
|
|
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 = std::make_shared<CObstacleInstance>();
|
|
|
|
obstPtr->ID = obid;
|
|
|
|
obstPtr->pos = posgenerator.getSuchNumber(validPosition);
|
2024-10-16 14:14:47 +02:00
|
|
|
obstPtr->uniqueID = static_cast<si32>(currentBattle->obstacles.size());
|
|
|
|
currentBattle->obstacles.push_back(obstPtr);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
for(BattleHex blocked : obstPtr->getBlockedTiles())
|
|
|
|
blockedTiles.push_back(blocked);
|
|
|
|
tilesToBlock -= static_cast<int>(obi.blockedTiles.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(RangeGenerator::ExhaustedPossibilities &)
|
|
|
|
{
|
|
|
|
logGlobal->debug("RangeGenerator::ExhaustedPossibilities exception occurred - cannot place usual obstacle");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//adding war machines
|
2024-08-31 23:04:32 +02:00
|
|
|
//Checks if hero has artifact and create appropriate stack
|
|
|
|
auto handleWarMachine = [&](BattleSide side, const ArtifactPosition & artslot, BattleHex hex)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-31 23:04:32 +02:00
|
|
|
const CArtifactInstance * warMachineArt = heroes[side]->getArt(artslot);
|
|
|
|
|
|
|
|
if(nullptr != warMachineArt && hex.isValid())
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-10-12 10:41:59 +02:00
|
|
|
CreatureID cre = warMachineArt->getType()->getWarMachine();
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
if(cre != CreatureID::NONE)
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->generateNewStack(currentBattle->nextUnitId(), CStackBasicDescriptor(cre, 1), side, SlotID::WAR_MACHINES_SLOT, hex);
|
2024-08-31 23:04:32 +02:00
|
|
|
}
|
|
|
|
};
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
if(heroes[BattleSide::ATTACKER])
|
|
|
|
{
|
|
|
|
auto warMachineHexes = layout.warMachines.at(BattleSide::ATTACKER);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
handleWarMachine(BattleSide::ATTACKER, ArtifactPosition::MACH1, warMachineHexes.at(0));
|
|
|
|
handleWarMachine(BattleSide::ATTACKER, ArtifactPosition::MACH2, warMachineHexes.at(1));
|
|
|
|
handleWarMachine(BattleSide::ATTACKER, ArtifactPosition::MACH3, warMachineHexes.at(2));
|
|
|
|
if(town && town->fortificationsLevel().wallsHealth > 0)
|
|
|
|
handleWarMachine(BattleSide::ATTACKER, ArtifactPosition::MACH4, warMachineHexes.at(3));
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
if(heroes[BattleSide::DEFENDER])
|
|
|
|
{
|
|
|
|
auto warMachineHexes = layout.warMachines.at(BattleSide::DEFENDER);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
if(!town) //defending hero shouldn't receive ballista (bug #551)
|
|
|
|
handleWarMachine(BattleSide::DEFENDER, ArtifactPosition::MACH1, warMachineHexes.at(0));
|
|
|
|
handleWarMachine(BattleSide::DEFENDER, ArtifactPosition::MACH2, warMachineHexes.at(1));
|
|
|
|
handleWarMachine(BattleSide::DEFENDER, ArtifactPosition::MACH3, warMachineHexes.at(2));
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
//war machines added
|
|
|
|
|
|
|
|
//battleStartpos read
|
2024-08-11 22:22:35 +02:00
|
|
|
for(BattleSide side : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
int formationNo = armies[side]->stacksCount() - 1;
|
|
|
|
vstd::abetween(formationNo, 0, GameConstants::ARMY_SIZE - 1);
|
|
|
|
|
|
|
|
int k = 0; //stack serial
|
|
|
|
for(auto i = armies[side]->Slots().begin(); i != armies[side]->Slots().end(); i++, k++)
|
|
|
|
{
|
2024-08-31 23:04:32 +02:00
|
|
|
const BattleHex & pos = layout.units.at(side).at(k);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
if (pos.isValid())
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->generateNewStack(currentBattle->nextUnitId(), *i->second, side, i->first, pos);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//adding commanders
|
2024-08-11 22:22:35 +02:00
|
|
|
for(BattleSide i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
if (heroes[i] && heroes[i]->commander && heroes[i]->commander->alive)
|
|
|
|
{
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->generateNewStack(currentBattle->nextUnitId(), *heroes[i]->commander, i, SlotID::COMMANDER_SLOT_PLACEHOLDER, layout.commanders.at(i));
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 14:14:47 +02:00
|
|
|
if (currentBattle->town)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-10-16 14:14:47 +02:00
|
|
|
if (currentBattle->town->fortificationsLevel().citadelHealth != 0)
|
|
|
|
currentBattle->generateNewStack(currentBattle->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), BattleSide::DEFENDER, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_CENTRAL_TOWER);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-10-16 14:14:47 +02:00
|
|
|
if (currentBattle->town->fortificationsLevel().upperTowerHealth != 0)
|
|
|
|
currentBattle->generateNewStack(currentBattle->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), BattleSide::DEFENDER, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_UPPER_TOWER);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-10-16 14:14:47 +02:00
|
|
|
if (currentBattle->town->fortificationsLevel().lowerTowerHealth != 0)
|
|
|
|
currentBattle->generateNewStack(currentBattle->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), BattleSide::DEFENDER, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_BOTTOM_TOWER);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
//Moat generating is done on server
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stable_sort(stacks.begin(),stacks.end(),cmpst);
|
|
|
|
|
|
|
|
auto neutral = std::make_shared<CreatureAlignmentLimiter>(EAlignment::NEUTRAL);
|
|
|
|
auto good = std::make_shared<CreatureAlignmentLimiter>(EAlignment::GOOD);
|
|
|
|
auto evil = std::make_shared<CreatureAlignmentLimiter>(EAlignment::EVIL);
|
|
|
|
|
|
|
|
const auto * bgInfo = VLC->battlefields()->getById(battlefieldType);
|
|
|
|
|
|
|
|
for(const std::shared_ptr<Bonus> & bonus : bgInfo->bonuses)
|
|
|
|
{
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->addNewBonus(bonus);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//native terrain bonuses
|
2024-02-12 13:22:54 +02:00
|
|
|
auto nativeTerrain = std::make_shared<CreatureTerrainLimiter>();
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->addNewBonus(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::STACKS_SPEED, BonusSource::TERRAIN_NATIVE, 1, BonusSourceID())->addLimiter(nativeTerrain));
|
|
|
|
currentBattle->addNewBonus(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::PRIMARY_SKILL, BonusSource::TERRAIN_NATIVE, 1, BonusSourceID(), BonusSubtypeID(PrimarySkill::ATTACK))->addLimiter(nativeTerrain));
|
|
|
|
currentBattle->addNewBonus(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::PRIMARY_SKILL, BonusSource::TERRAIN_NATIVE, 1, BonusSourceID(), BonusSubtypeID(PrimarySkill::DEFENSE))->addLimiter(nativeTerrain));
|
2023-10-19 16:19:09 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//tactics
|
2024-08-11 22:22:35 +02:00
|
|
|
BattleSideArray<int> battleRepositionHex = {};
|
|
|
|
BattleSideArray<int> battleRepositionHexBlock = {};
|
|
|
|
for(auto i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
if(heroes[i])
|
|
|
|
{
|
|
|
|
battleRepositionHex[i] += heroes[i]->valOfBonuses(Selector::type()(BonusType::BEFORE_BATTLE_REPOSITION));
|
|
|
|
battleRepositionHexBlock[i] += heroes[i]->valOfBonuses(Selector::type()(BonusType::BEFORE_BATTLE_REPOSITION_BLOCK));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int tacticsSkillDiffAttacker = battleRepositionHex[BattleSide::ATTACKER] - battleRepositionHexBlock[BattleSide::DEFENDER];
|
|
|
|
int tacticsSkillDiffDefender = battleRepositionHex[BattleSide::DEFENDER] - battleRepositionHexBlock[BattleSide::ATTACKER];
|
|
|
|
|
|
|
|
/* for current tactics, we need to choose one side, so, we will choose side when first - second > 0, and ignore sides
|
|
|
|
when first - second <= 0. If there will be situations when both > 0, attacker will be chosen. Anyway, in OH3 this
|
|
|
|
will not happen because tactics block opposite tactics on same value.
|
|
|
|
TODO: For now, it is an error to use BEFORE_BATTLE_REPOSITION bonus without counterpart, but it can be changed if
|
|
|
|
double tactics will be implemented.
|
|
|
|
*/
|
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
if(layout.tacticsAllowed)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
if(tacticsSkillDiffAttacker > 0 && tacticsSkillDiffDefender > 0)
|
|
|
|
logGlobal->warn("Double tactics is not implemented, only attacker will have tactics!");
|
|
|
|
if(tacticsSkillDiffAttacker > 0)
|
|
|
|
{
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->tacticsSide = BattleSide::ATTACKER;
|
2023-10-19 16:19:09 +02:00
|
|
|
//bonus specifies distance you can move beyond base row; this allows 100% compatibility with HMM3 mechanics
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->tacticDistance = 1 + tacticsSkillDiffAttacker;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
else if(tacticsSkillDiffDefender > 0)
|
|
|
|
{
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->tacticsSide = BattleSide::DEFENDER;
|
2023-10-19 16:19:09 +02:00
|
|
|
//bonus specifies distance you can move beyond base row; this allows 100% compatibility with HMM3 mechanics
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->tacticDistance = 1 + tacticsSkillDiffDefender;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
else
|
2024-10-16 14:14:47 +02:00
|
|
|
currentBattle->tacticDistance = 0;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 14:14:47 +02:00
|
|
|
return currentBattle;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * BattleInfo::getHero(const PlayerColor & player) const
|
|
|
|
{
|
|
|
|
for(const auto & side : sides)
|
|
|
|
if(side.color == player)
|
|
|
|
return side.hero;
|
|
|
|
|
|
|
|
logGlobal->error("Player %s is not in battle!", player.toString());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
BattleSide BattleInfo::whatSide(const PlayerColor & player) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
for(auto i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
2023-10-19 16:19:09 +02:00
|
|
|
if(sides[i].color == player)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
logGlobal->warn("BattleInfo::whatSide: Player %s is not in battle!", player.toString());
|
2024-08-11 22:22:35 +02:00
|
|
|
return BattleSide::NONE;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CStack * BattleInfo::getStack(int stackID, bool onlyAlive)
|
|
|
|
{
|
|
|
|
return const_cast<CStack *>(battleGetStackByID(stackID, onlyAlive));
|
|
|
|
}
|
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
BattleInfo::BattleInfo(const BattleLayout & layout):
|
|
|
|
BattleInfo()
|
|
|
|
{
|
|
|
|
*this->layout = layout;
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
BattleInfo::BattleInfo():
|
2024-09-02 21:15:12 +02:00
|
|
|
layout(std::make_unique<BattleLayout>()),
|
2023-10-19 16:19:09 +02:00
|
|
|
round(-1),
|
|
|
|
activeStack(-1),
|
|
|
|
town(nullptr),
|
|
|
|
tile(-1,-1,-1),
|
|
|
|
battlefieldType(BattleField::NONE),
|
2024-08-11 22:22:35 +02:00
|
|
|
tacticsSide(BattleSide::NONE),
|
2023-10-19 16:19:09 +02:00
|
|
|
tacticDistance(0)
|
|
|
|
{
|
|
|
|
setNodeType(BATTLE);
|
|
|
|
}
|
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
BattleLayout BattleInfo::getLayout() const
|
|
|
|
{
|
|
|
|
return *layout;
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
BattleID BattleInfo::getBattleID() const
|
|
|
|
{
|
|
|
|
return battleID;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IBattleInfo * BattleInfo::getBattle() const
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<PlayerColor> BattleInfo::getPlayerID() const
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleInfo::~BattleInfo()
|
|
|
|
{
|
|
|
|
for (auto & elem : stacks)
|
|
|
|
delete elem;
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
for(auto i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
2023-10-19 16:19:09 +02:00
|
|
|
if(auto * _armyObj = battleGetArmyObject(i))
|
|
|
|
_armyObj->battle = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t BattleInfo::getActiveStackID() const
|
|
|
|
{
|
|
|
|
return activeStack;
|
|
|
|
}
|
|
|
|
|
2023-11-18 19:43:11 +02:00
|
|
|
TStacks BattleInfo::getStacksIf(const TStackFilter & predicate) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
TStacks ret;
|
|
|
|
vstd::copy_if(stacks, std::back_inserter(ret), predicate);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-11-18 19:43:11 +02:00
|
|
|
battle::Units BattleInfo::getUnitsIf(const battle::UnitFilter & predicate) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
battle::Units ret;
|
|
|
|
vstd::copy_if(stacks, std::back_inserter(ret), predicate);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BattleField BattleInfo::getBattlefieldType() const
|
|
|
|
{
|
|
|
|
return battlefieldType;
|
|
|
|
}
|
|
|
|
|
|
|
|
TerrainId BattleInfo::getTerrainType() const
|
|
|
|
{
|
|
|
|
return terrainType;
|
|
|
|
}
|
|
|
|
|
|
|
|
IBattleInfo::ObstacleCList BattleInfo::getAllObstacles() const
|
|
|
|
{
|
|
|
|
ObstacleCList ret;
|
|
|
|
|
|
|
|
for(const auto & obstacle : obstacles)
|
|
|
|
ret.push_back(obstacle);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
PlayerColor BattleInfo::getSidePlayer(BattleSide side) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
return getSide(side).color;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
const CArmedInstance * BattleInfo::getSideArmy(BattleSide side) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
return getSide(side).armyObject;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
const CGHeroInstance * BattleInfo::getSideHero(BattleSide side) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
return getSide(side).hero;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
uint8_t BattleInfo::getTacticDist() const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
return tacticDistance;
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
BattleSide BattleInfo::getTacticsSide() const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
return tacticsSide;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGTownInstance * BattleInfo::getDefendedTown() const
|
|
|
|
{
|
|
|
|
return town;
|
|
|
|
}
|
|
|
|
|
|
|
|
EWallState BattleInfo::getWallState(EWallPart partOfWall) const
|
|
|
|
{
|
|
|
|
return si.wallState.at(partOfWall);
|
|
|
|
}
|
|
|
|
|
|
|
|
EGateState BattleInfo::getGateState() const
|
|
|
|
{
|
|
|
|
return si.gateState;
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
uint32_t BattleInfo::getCastSpells(BattleSide side) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
return getSide(side).castSpellsCount;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
int32_t BattleInfo::getEnchanterCounter(BattleSide side) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
return getSide(side).enchanterCounter;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const IBonusBearer * BattleInfo::getBonusBearer() const
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t BattleInfo::getActualDamage(const DamageRange & damage, int32_t attackerCount, vstd::RNG & rng) const
|
|
|
|
{
|
|
|
|
if(damage.min != damage.max)
|
|
|
|
{
|
|
|
|
int64_t sum = 0;
|
|
|
|
|
|
|
|
auto howManyToAv = std::min<int32_t>(10, attackerCount);
|
|
|
|
|
|
|
|
for(int32_t g = 0; g < howManyToAv; ++g)
|
2024-06-01 15:36:38 +02:00
|
|
|
sum += rng.nextInt64(damage.min, damage.max);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
return sum / howManyToAv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return damage.min;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 BattleInfo::getLocation() const
|
|
|
|
{
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
std::vector<SpellID> BattleInfo::getUsedSpells(BattleSide side) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
return getSide(side).usedSpellsHistory;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::nextRound()
|
|
|
|
{
|
2024-08-11 22:22:35 +02:00
|
|
|
for(auto i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
sides.at(i).castSpellsCount = 0;
|
|
|
|
vstd::amax(--sides.at(i).enchanterCounter, 0);
|
|
|
|
}
|
|
|
|
round += 1;
|
|
|
|
|
|
|
|
for(CStack * s : stacks)
|
|
|
|
{
|
|
|
|
// new turn effects
|
|
|
|
s->reduceBonusDurations(Bonus::NTurns);
|
|
|
|
|
|
|
|
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
|
|
|
|
st->removeBonusesRecursive(Bonus::UntilGetsTurn);
|
|
|
|
|
|
|
|
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);
|
|
|
|
ret->summoned = info.summoned;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
//Bonuses can be limited by unit placement, so, change tree version
|
|
|
|
//to force updating a bonus. TODO: update version only when such bonuses are present
|
|
|
|
CBonusSystemNode::treeHasChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-06-24 03:23:26 +02:00
|
|
|
auto accessibility = getAccessibility();
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
changedStack->removeBonusesRecursive(Bonus::UntilBeingAttacked);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-10-22 17:36:41 +02:00
|
|
|
return b->source == BonusSource::SPELL_EFFECT && b->sid.as<SpellID>() != SpellID::DISRUPTING_RAY;
|
2023-10-19 16:19:09 +02:00
|
|
|
};
|
|
|
|
changedStack->removeBonusesRecursive(selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::updateUnit(uint32_t id, const JsonNode & data)
|
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-04-07 18:57:49 +02:00
|
|
|
&& one.effectRange == b->effectRange;
|
2023-10-19 16:19:09 +02:00
|
|
|
};
|
|
|
|
sta->removeBonusesRecursive(selector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t BattleInfo::nextUnitId() const
|
|
|
|
{
|
|
|
|
return static_cast<uint32_t>(stacks.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::addOrUpdateUnitBonus(CStack * sta, const Bonus & value, bool forceAdd)
|
|
|
|
{
|
2024-05-28 19:01:30 +02:00
|
|
|
if(forceAdd || !sta->hasBonus(Selector::source(BonusSource::SPELL_EFFECT, value.sid).And(Selector::typeSubtypeValueType(value.type, value.subtype, value.valType))))
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
//no such effect or cumulative - add new
|
2024-10-25 20:36:02 +02:00
|
|
|
logBonus->trace("%s receives a new bonus: %s", sta->nodeName(), value.Description(nullptr));
|
2023-10-19 16:19:09 +02:00
|
|
|
sta->addNewBonus(std::make_shared<Bonus>(value));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-10-25 20:36:02 +02:00
|
|
|
logBonus->trace("%s updated bonus: %s", sta->nodeName(), value.Description(nullptr));
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
for(const auto & stackBonus : sta->getExportedBonusList()) //TODO: optimize
|
|
|
|
{
|
2024-05-28 19:01:30 +02:00
|
|
|
if(stackBonus->source == value.source && stackBonus->sid == value.sid && stackBonus->type == value.type && stackBonus->subtype == value.subtype && stackBonus->valType == value.valType)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, value.turnsRemain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CBonusSystemNode::treeHasChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::setWallState(EWallPart partOfWall, EWallState state)
|
|
|
|
{
|
|
|
|
si.wallState[partOfWall] = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::addObstacle(const ObstacleChanges & changes)
|
|
|
|
{
|
2024-01-17 14:50:00 +02:00
|
|
|
auto obstacle = std::make_shared<SpellCreatedObstacle>();
|
2023-10-19 16:19:09 +02:00
|
|
|
obstacle->fromInfo(changes);
|
|
|
|
obstacles.push_back(obstacle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleInfo::updateObstacle(const ObstacleChanges& changes)
|
|
|
|
{
|
2024-01-17 14:50:00 +02:00
|
|
|
auto changedObstacle = std::make_shared<SpellCreatedObstacle>();
|
2023-10-19 16:19:09 +02:00
|
|
|
changedObstacle->fromInfo(changes);
|
|
|
|
|
|
|
|
for(auto & obstacle : obstacles)
|
|
|
|
{
|
|
|
|
if(obstacle->uniqueID == changes.id) // update this obstacle
|
|
|
|
{
|
|
|
|
auto * spellObstacle = dynamic_cast<SpellCreatedObstacle *>(obstacle.get());
|
|
|
|
assert(spellObstacle);
|
|
|
|
|
|
|
|
// Currently we only support to update the "revealed" property
|
|
|
|
spellObstacle->revealed = changedObstacle->revealed;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
CArmedInstance * BattleInfo::battleGetArmyObject(BattleSide side) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
return const_cast<CArmedInstance*>(CBattleInfoEssentials::battleGetArmyObject(side));
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
CGHeroInstance * BattleInfo::battleGetFightingHero(BattleSide side) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
return const_cast<CGHeroInstance*>(CBattleInfoEssentials::battleGetFightingHero(side));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SCRIPTING_ENABLED
|
|
|
|
scripting::Pool * BattleInfo::getContextPool() const
|
|
|
|
{
|
|
|
|
//this is real battle, use global scripting context pool
|
|
|
|
//TODO: make this line not ugly
|
2024-08-11 22:22:35 +02:00
|
|
|
return battleGetFightingHero(BattleSide::ATTACKER)->cb->getGlobalContextPool();
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool CMP_stack::operator()(const battle::Unit * a, const battle::Unit * b) const
|
|
|
|
{
|
|
|
|
switch(phase)
|
|
|
|
{
|
|
|
|
case 0: //catapult moves after turrets
|
|
|
|
return a->creatureIndex() > b->creatureIndex(); //catapult is 145 and turrets are 149
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
int as = a->getInitiative(turn);
|
|
|
|
int bs = b->getInitiative(turn);
|
|
|
|
|
|
|
|
if(as != bs)
|
|
|
|
return as > bs;
|
|
|
|
|
|
|
|
if(a->unitSide() == b->unitSide())
|
|
|
|
return a->unitSlot() < b->unitSlot();
|
|
|
|
|
|
|
|
return (a->unitSide() == side || b->unitSide() == side)
|
|
|
|
? a->unitSide() != side
|
|
|
|
: a->unitSide() < b->unitSide();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
CMP_stack::CMP_stack(int Phase, int Turn, BattleSide Side):
|
2023-10-19 16:19:09 +02:00
|
|
|
phase(Phase),
|
|
|
|
turn(Turn),
|
|
|
|
side(Side)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|