1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/lib/CCreatureSet.h

282 lines
11 KiB
C
Raw Normal View History

2012-12-19 17:54:10 +03:00
/*
* CCreatureSet.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 "HeroBonus.h"
#include "GameConstants.h"
#include "CArtHandler.h"
2022-09-26 02:50:08 +02:00
#include "CCreatureHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
2016-01-15 19:24:17 +02:00
class JsonNode;
2010-04-02 05:23:22 +03:00
class CCreature;
class CGHeroInstance;
class CArmedInstance;
2012-01-30 19:07:52 +03:00
class CCreatureArtifactSet;
class JsonSerializeFormat;
2010-04-02 05:23:22 +03:00
class DLL_LINKAGE CStackBasicDescriptor
{
public:
2023-03-16 20:48:37 +02:00
const CCreature *type = nullptr;
TQuantity count = -1; //exact quantity or quantity ID from CCreature::getQuantityID when getting info about enemy army
CStackBasicDescriptor();
2023-03-13 23:26:44 +02:00
CStackBasicDescriptor(const CreatureID & id, TQuantity Count);
CStackBasicDescriptor(const CCreature *c, TQuantity Count);
2016-10-29 17:00:12 +02:00
virtual ~CStackBasicDescriptor() = default;
const Creature * getType() const;
TQuantity getCount() const;
virtual void setType(const CCreature * c);
template <typename Handler> void serialize(Handler &h, const int version)
{
2022-09-26 02:50:08 +02:00
if(h.saving)
{
CreatureID idNumber = type ? type->idNumber : CreatureID(CreatureID::NONE);
h & idNumber;
}
else
{
CreatureID idNumber;
h & idNumber;
if(idNumber != CreatureID::NONE)
setType(VLC->creh->objects[idNumber]);
else
type = nullptr;
}
h & count;
}
2016-01-15 19:24:17 +02:00
void serializeJson(JsonSerializeFormat & handler);
};
class DLL_LINKAGE CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor, public CArtifactSet
2010-04-02 05:23:22 +03:00
{
protected:
const CArmedInstance *_armyObj; //stack must be part of some army, army must be part of some object
2010-04-02 05:23:22 +03:00
public:
// hlp variable used during loading map, when object (hero or town) have creatures that must have same alignment.
// idRand < 0 -> normal, non-random creature
// idRand / 2 -> level
// idRand % 2 -> upgrade number
int idRand;
const CArmedInstance * const & armyObj; //stack must be part of some army, army must be part of some object
TExpType experience;//commander needs same amount of exp as hero
2010-04-02 05:23:22 +03:00
template <typename Handler> void serialize(Handler &h, const int version)
{
h & static_cast<CBonusSystemNode&>(*this);
h & static_cast<CStackBasicDescriptor&>(*this);
h & static_cast<CArtifactSet&>(*this);
h & _armyObj;
h & experience;
BONUS_TREE_DESERIALIZATION_FIX
2010-04-02 05:23:22 +03:00
}
void serializeJson(JsonSerializeFormat & handler);
2016-01-15 19:24:17 +02:00
//overrides CBonusSystemNode
std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const override; // how would bonus description look for this particular type of node
2023-03-13 23:26:44 +02:00
std::string bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const; //file name of graphics from StackSkills , in future possibly others
virtual ui64 getPower() const;
CCreature::CreatureQuantityId getQuantityID() const;
std::string getQuantityTXT(bool capitalized = true) const;
virtual int getExpRank() const;
virtual int getLevel() const; //different for regular stack and commander
si32 magicResistance() const override;
CreatureID getCreatureID() const; //-1 if not available
std::string getName() const; //plural or singular
virtual void init();
2010-04-02 05:23:22 +03:00
CStackInstance();
2023-03-13 23:26:44 +02:00
CStackInstance(const CreatureID & id, TQuantity count, bool isHypothetic = false);
CStackInstance(const CCreature *cre, TQuantity count, bool isHypothetic = false);
2023-03-13 23:26:44 +02:00
virtual ~CStackInstance() = default;
2023-03-13 23:26:44 +02:00
void setType(const CreatureID & creID);
void setType(const CCreature * c) override;
void setArmyObj(const CArmedInstance *ArmyObj);
virtual void giveStackExp(TExpType exp);
bool valid(bool allowUnrandomized) const;
void putArtifact(ArtifactPosition pos, CArtifactInstance * art) override;//from CArtifactSet
ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
virtual std::string nodeName() const override; //from CBonusSystemnode
void deserializationFix();
PlayerColor getOwner() const override;
2010-04-02 05:23:22 +03:00
};
class DLL_LINKAGE CCommanderInstance : public CStackInstance
{
public:
//TODO: what if Commander is not a part of creature set?
//commander class is determined by its base creature
2016-10-02 16:57:26 +02:00
ui8 alive; //maybe change to bool when breaking save compatibility?
ui8 level; //required only to count callbacks
std::string name; // each Commander has different name
std::vector <ui8> secondarySkills; //ID -> level
std::set <ui8> specialSKills;
//std::vector <CArtifactInstance *> arts;
void init() override;
CCommanderInstance();
2023-03-13 23:26:44 +02:00
CCommanderInstance(const CreatureID & id);
void setAlive (bool alive);
void giveStackExp (TExpType exp) override;
void levelUp ();
bool gainsLevel() const; //true if commander has lower level than should upon his experience
ui64 getPower() const override {return 0;};
int getExpRank() const override;
2016-01-15 19:24:17 +02:00
int getLevel() const override;
ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
template <typename Handler> void serialize(Handler &h, const int version)
{
h & static_cast<CStackInstance&>(*this);
h & alive;
h & level;
h & name;
h & secondarySkills;
h & specialSKills;
}
};
2013-02-16 17:03:47 +03:00
typedef std::map<SlotID, CStackInstance*> TSlots;
2018-03-11 17:56:33 +02:00
typedef std::map<SlotID, std::pair<CreatureID, TQuantity>> TSimpleSlots;
2010-04-02 05:23:22 +03:00
typedef std::pair<const CCreature*, SlotID> TPairCreatureSlot;
typedef std::map<const CCreature*, SlotID> TMapCreatureSlot;
struct DLL_LINKAGE CreatureSlotComparer
{
bool operator()(const TPairCreatureSlot & lhs, const TPairCreatureSlot & rhs);
};
typedef std::priority_queue<
TPairCreatureSlot,
std::vector<TPairCreatureSlot>,
CreatureSlotComparer
> TCreatureQueue;
class IArmyDescriptor
{
public:
virtual void clear() = 0;
2013-02-16 17:03:47 +03:00
virtual bool setCreature(SlotID slot, CreatureID cre, TQuantity count) = 0;
};
//simplified version of CCreatureSet
class DLL_LINKAGE CSimpleArmy : public IArmyDescriptor
{
public:
TSimpleSlots army;
void clear() override;
bool setCreature(SlotID slot, CreatureID cre, TQuantity count) override;
operator bool() const;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & army;
}
};
class DLL_LINKAGE CCreatureSet : public IArmyDescriptor //seven combined creatures
2010-04-02 05:23:22 +03:00
{
2023-03-13 23:26:44 +02:00
CCreatureSet(const CCreatureSet &) = delete;
CCreatureSet &operator=(const CCreatureSet&);
2010-04-02 05:23:22 +03:00
public:
2011-01-21 04:36:30 +02:00
TSlots stacks; //slots[slot_id]->> pair(creature_id,creature_quantity)
2023-04-01 22:59:31 +02:00
ui8 formation = 0; //0 - wide, 1 - tight
2010-04-02 05:23:22 +03:00
2023-03-13 23:26:44 +02:00
CCreatureSet() = default; //Should be here to avoid compile errors
virtual ~CCreatureSet();
virtual void armyChanged();
2023-03-13 23:26:44 +02:00
const CStackInstance & operator[](const SlotID & slot) const;
2010-04-02 05:23:22 +03:00
2011-01-21 04:36:30 +02:00
const TSlots &Slots() const {return stacks;}
2010-04-02 05:23:22 +03:00
2023-03-13 23:26:44 +02:00
void addToSlot(const SlotID & slot, const CreatureID & cre, TQuantity count, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
void addToSlot(const SlotID & slot, CStackInstance * stack, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
void clear() override;
void setFormation(bool tight);
CArmedInstance *castToArmyObj();
//basic operations
2023-03-13 23:26:44 +02:00
void putStack(const SlotID & slot, CStackInstance * stack); //adds new stack to the army, slot must be empty
void setStackCount(const SlotID & slot, TQuantity count); //stack must exist!
CStackInstance * detachStack(const SlotID & slot); //removes stack from army but doesn't destroy it (so it can be moved somewhere else or safely deleted)
void setStackType(const SlotID & slot, const CreatureID & type);
void giveStackExp(TExpType exp);
2023-03-13 23:26:44 +02:00
void setStackExp(const SlotID & slot, TExpType exp);
//derivative
2023-03-13 23:26:44 +02:00
void eraseStack(const SlotID & slot); //slot must be occupied
void joinStack(const SlotID & slot, CStackInstance * stack); //adds new stack to the existing stack of the same type
void changeStackCount(const SlotID & slot, TQuantity toAdd); //stack must exist!
bool setCreature (SlotID slot, CreatureID type, TQuantity quantity) override; //replaces creature in stack; slots 0 to 6, if quantity=0 erases stack
void setToArmy(CSimpleArmy &src); //erases all our army and moves stacks from src to us; src MUST NOT be an armed object! WARNING: use it wisely. Or better do not use at all.
2023-03-13 23:26:44 +02:00
const CStackInstance & getStack(const SlotID & slot) const; //stack must exist
const CStackInstance * getStackPtr(const SlotID & slot) const; //if stack doesn't exist, returns nullptr
const CCreature * getCreature(const SlotID & slot) const; //workaround of map issue;
int getStackCount(const SlotID & slot) const;
TExpType getStackExperience(const SlotID & slot) const;
2013-02-16 17:03:47 +03:00
SlotID findStack(const CStackInstance *stack) const; //-1 if none
2023-03-13 23:26:44 +02:00
SlotID getSlotFor(const CreatureID & creature, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
2013-02-16 17:03:47 +03:00
SlotID getSlotFor(const CCreature *c, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
2023-03-13 23:26:44 +02:00
bool hasCreatureSlots(const CCreature * c, const SlotID & exclude) const;
std::vector<SlotID> getCreatureSlots(const CCreature * c, const SlotID & exclude, TQuantity ignoreAmount = -1) const;
bool isCreatureBalanced(const CCreature* c, TQuantity ignoreAmount = 1) const; // Check if the creature is evenly distributed across slots
SlotID getFreeSlot(ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns first free slot
std::vector<SlotID> getFreeSlots(ui32 slotsAmount = GameConstants::ARMY_SIZE) const;
std::queue<SlotID> getFreeSlotsQueue(ui32 slotsAmount = GameConstants::ARMY_SIZE) const;
TMapCreatureSlot getCreatureMap() const;
2023-03-13 23:26:44 +02:00
TCreatureQueue getCreatureQueue(const SlotID & exclude) const;
2023-03-13 23:26:44 +02:00
bool mergableStacks(std::pair<SlotID, SlotID> & out, const SlotID & preferable = SlotID()) const; //looks for two same stacks, returns slot positions;
2010-04-02 05:23:22 +03:00
bool validTypes(bool allowUnrandomized = false) const; //checks if all types of creatures are set properly
2023-03-13 23:26:44 +02:00
bool slotEmpty(const SlotID & slot) const;
int stacksCount() const;
virtual bool needsLastStack() const; //true if last stack cannot be taken
ui64 getArmyStrength() const; //sum of AI values of creatures
2023-03-13 23:26:44 +02:00
ui64 getPower(const SlotID & slot) const; //value of specific stack
std::string getRoughAmount(const SlotID & slot, int mode = 0) const; //rough size of specific stack
std::string getArmyDescription() const;
2023-03-13 23:26:44 +02:00
bool hasStackAtSlot(const SlotID & slot) const;
bool contains(const CStackInstance *stack) const;
bool canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks = true) const;
2010-04-02 05:23:22 +03:00
template <typename Handler> void serialize(Handler &h, const int version)
{
h & stacks;
h & formation;
2010-04-02 05:23:22 +03:00
}
2016-01-15 19:24:17 +02:00
void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName, const boost::optional<int> fixedSize = boost::none);
2016-01-15 19:24:17 +02:00
2010-04-02 05:23:22 +03:00
operator bool() const
{
2013-11-03 15:51:25 +03:00
return !stacks.empty();
2010-04-02 05:23:22 +03:00
}
void sweep();
};
VCMI_LIB_NAMESPACE_END