2023-10-19 16:19:09 +02:00
|
|
|
/*
|
|
|
|
* CStack.h, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include "bonuses/Bonus.h"
|
|
|
|
#include "bonuses/CBonusSystemNode.h"
|
|
|
|
#include "CCreatureHandler.h" //todo: remove
|
|
|
|
#include "battle/BattleHex.h"
|
|
|
|
#include "mapObjects/CGHeroInstance.h" // for commander serialization
|
|
|
|
|
|
|
|
#include "battle/CUnitState.h"
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
struct BattleStackAttacked;
|
|
|
|
class BattleInfo;
|
|
|
|
|
|
|
|
//Represents STACK_BATTLE nodes
|
|
|
|
class DLL_LINKAGE CStack : public CBonusSystemNode, public battle::CUnitState, public battle::IUnitEnvironment
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
ui32 ID = -1; //unique ID of stack
|
2024-10-12 18:52:58 +02:00
|
|
|
CreatureID typeID;
|
2023-10-19 16:19:09 +02:00
|
|
|
TerrainId nativeTerrain; //tmp variable to save native terrain value on battle init
|
|
|
|
ui32 baseAmount = -1;
|
|
|
|
|
|
|
|
PlayerColor owner; //owner - player color (255 for neutrals)
|
2024-08-11 22:22:35 +02:00
|
|
|
BattleSide side = BattleSide::NONE;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
SlotID slot; //slot - position in garrison (may be 255 for neutrals/called creatures)
|
|
|
|
|
|
|
|
public:
|
|
|
|
const CStackInstance * base = nullptr; //garrison slot from which stack originates (nullptr for war machines, summoned cres, etc)
|
|
|
|
|
|
|
|
BattleHex initialPosition; //position on battlefield; -2 - keep, -3 - lower tower, -4 - upper tower
|
|
|
|
|
2024-08-11 22:22:35 +02:00
|
|
|
CStack(const CStackInstance * base, const PlayerColor & O, int I, BattleSide Side, const SlotID & S);
|
|
|
|
CStack(const CStackBasicDescriptor * stack, const PlayerColor & O, int I, BattleSide Side, const SlotID & S = SlotID(255));
|
2023-10-19 16:19:09 +02:00
|
|
|
CStack();
|
|
|
|
~CStack();
|
|
|
|
|
|
|
|
std::string nodeName() const override;
|
|
|
|
|
|
|
|
void localInit(BattleInfo * battleInfo);
|
|
|
|
std::string getName() const; //plural or singular
|
|
|
|
|
|
|
|
bool canBeHealed() const; //for first aid tent - only harmed stacks that are not war machines
|
|
|
|
bool isOnNativeTerrain() const;
|
|
|
|
bool isOnTerrain(TerrainId terrain) const;
|
|
|
|
|
|
|
|
ui32 level() const;
|
|
|
|
si32 magicResistance() const override; //include aura of resistance
|
2023-10-22 17:36:41 +02:00
|
|
|
std::vector<SpellID> activeSpells() const; //returns vector of active spell IDs sorted by time of cast
|
2023-10-19 16:19:09 +02:00
|
|
|
const CGHeroInstance * getMyHero() const; //if stack belongs to hero (directly or was by him summoned) returns hero, nullptr otherwise
|
|
|
|
|
|
|
|
static std::vector<BattleHex> meleeAttackHexes(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
|
|
|
|
static bool isMeleeAttackPossible(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
|
|
|
|
|
|
|
|
BattleHex::EDir destShiftDir() const;
|
|
|
|
|
2024-06-24 03:23:26 +02:00
|
|
|
void prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand) const; //requires bsa.damageAmount filled
|
2023-10-19 16:19:09 +02:00
|
|
|
static void prepareAttacked(BattleStackAttacked & bsa,
|
|
|
|
vstd::RNG & rand,
|
2024-06-24 03:23:26 +02:00
|
|
|
const std::shared_ptr<battle::CUnitState> & customState); //requires bsa.damageAmount filled
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
const CCreature * unitType() const override;
|
|
|
|
int32_t unitBaseAmount() const override;
|
|
|
|
|
|
|
|
uint32_t unitId() const override;
|
2024-08-11 22:22:35 +02:00
|
|
|
BattleSide unitSide() const override;
|
2023-10-19 16:19:09 +02:00
|
|
|
PlayerColor unitOwner() const override;
|
|
|
|
SlotID unitSlot() const override;
|
|
|
|
|
|
|
|
std::string getDescription() const override;
|
|
|
|
|
|
|
|
bool unitHasAmmoCart(const battle::Unit * unit) const override;
|
|
|
|
PlayerColor unitEffectiveOwner(const battle::Unit * unit) const override;
|
|
|
|
|
|
|
|
void spendMana(ServerCallback * server, const int spellCost) const override;
|
|
|
|
|
|
|
|
const IBonusBearer* getBonusBearer() const override;
|
|
|
|
|
|
|
|
PlayerColor getOwner() const override
|
|
|
|
{
|
|
|
|
return this->owner;
|
|
|
|
}
|
|
|
|
|
2024-01-20 20:34:51 +02:00
|
|
|
template <typename Handler> void serialize(Handler & h)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
//this assumes that stack objects is newly created
|
|
|
|
//stackState is not serialized here
|
|
|
|
assert(isIndependentNode());
|
|
|
|
h & static_cast<CBonusSystemNode&>(*this);
|
2024-10-12 18:52:58 +02:00
|
|
|
h & typeID;
|
2023-10-19 16:19:09 +02:00
|
|
|
h & ID;
|
|
|
|
h & baseAmount;
|
|
|
|
h & owner;
|
|
|
|
h & slot;
|
|
|
|
h & side;
|
|
|
|
h & initialPosition;
|
|
|
|
|
|
|
|
const CArmedInstance * army = (base ? base->armyObj : nullptr);
|
|
|
|
SlotID extSlot = (base ? base->armyObj->findStack(base) : SlotID());
|
|
|
|
|
|
|
|
if(h.saving)
|
|
|
|
{
|
|
|
|
h & army;
|
|
|
|
h & extSlot;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
h & army;
|
|
|
|
h & extSlot;
|
|
|
|
|
|
|
|
if(extSlot == SlotID::COMMANDER_SLOT_PLACEHOLDER)
|
|
|
|
{
|
|
|
|
const auto * hero = dynamic_cast<const CGHeroInstance *>(army);
|
|
|
|
assert(hero);
|
|
|
|
base = hero->commander;
|
|
|
|
}
|
|
|
|
else if(slot == SlotID::SUMMONED_SLOT_PLACEHOLDER || slot == SlotID::ARROW_TOWERS_SLOT || slot == SlotID::WAR_MACHINES_SLOT)
|
|
|
|
{
|
|
|
|
//no external slot possible, so no base stack
|
|
|
|
base = nullptr;
|
|
|
|
}
|
|
|
|
else if(!army || extSlot == SlotID() || !army->hasStackAtSlot(extSlot))
|
|
|
|
{
|
|
|
|
base = nullptr;
|
2024-10-12 18:52:58 +02:00
|
|
|
logGlobal->warn("%s doesn't have a base stack!", typeID.toEntity(VLC)->getNameSingularTranslated());
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
base = &army->getStack(extSlot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const BattleInfo * battle; //do not serialize
|
|
|
|
};
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|