1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/lib/mapObjects/CObjectClassesHandler.h

334 lines
9.8 KiB
C
Raw Normal View History

/*
2014-06-05 19:57:43 +03:00
* CObjectClassesHandler.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 "ObjectTemplate.h"
#include "../GameConstants.h"
#include "../ConstTransitivePtr.h"
#include "../IHandlerBase.h"
#include "../JsonNode.h"
2014-06-05 19:57:43 +03:00
VCMI_LIB_NAMESPACE_BEGIN
2014-06-05 19:57:43 +03:00
class JsonNode;
class CRandomGenerator;
struct SObjectSounds
{
std::vector<std::string> ambient;
std::vector<std::string> visit;
std::vector<std::string> removal;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & ambient;
h & visit;
h & removal;
}
};
/// Structure that describes placement rules for this object in random map
struct DLL_LINKAGE RandomMapInfo
{
/// How valuable this object is, 1k = worthless, 10k = Utopia-level
ui32 value;
/// How many of such objects can be placed on map, 0 = object can not be placed by RMG
ui32 mapLimit;
/// How many of such objects can be placed in one zone, 0 = unplaceable
ui32 zoneLimit;
/// Rarity of object, 5 = extremely rare, 100 = common
ui32 rarity;
RandomMapInfo():
value(0),
mapLimit(0),
zoneLimit(0),
rarity(0)
{}
template <typename Handler> void serialize(Handler &h, const int version)
{
h & value;
h & mapLimit;
h & zoneLimit;
h & rarity;
}
};
2018-07-22 19:12:11 +02:00
struct DLL_LINKAGE CompoundMapObjectID
2018-07-22 18:12:29 +02:00
{
si32 primaryID;
si32 secondaryID;
2018-07-22 19:12:11 +02:00
CompoundMapObjectID(si32 primID, si32 secID) : primaryID(primID), secondaryID(secID) {};
2018-07-22 18:12:29 +02:00
2018-07-22 19:12:11 +02:00
bool operator<(const CompoundMapObjectID& other) const
2018-07-22 18:12:29 +02:00
{
2018-07-26 16:43:40 +02:00
if(this->primaryID != other.primaryID)
return this->primaryID < other.primaryID;
else
return this->secondaryID < other.secondaryID;
2018-07-22 18:12:29 +02:00
}
2018-07-22 19:12:11 +02:00
bool operator==(const CompoundMapObjectID& other) const
2018-07-22 18:12:29 +02:00
{
2018-07-26 16:43:40 +02:00
return (this->primaryID == other.primaryID) && (this->secondaryID == other.secondaryID);
2018-07-22 18:12:29 +02:00
}
};
class DLL_LINKAGE IObjectInfo
2014-06-05 19:57:43 +03:00
{
public:
struct CArmyStructure
{
ui32 totalStrength;
ui32 shootersStrength;
ui32 flyersStrength;
ui32 walkersStrength;
CArmyStructure() :
totalStrength(0),
shootersStrength(0),
flyersStrength(0),
walkersStrength(0)
{}
bool operator <(const CArmyStructure & other) const
{
return this->totalStrength < other.totalStrength;
}
};
/// Returns possible composition of guards. Actual guards would be
/// somewhere between these two values
virtual CArmyStructure minGuards() const { return CArmyStructure(); }
virtual CArmyStructure maxGuards() const { return CArmyStructure(); }
virtual bool givesResources() const { return false; }
virtual bool givesExperience() const { return false; }
virtual bool givesMana() const { return false; }
virtual bool givesMovement() const { return false; }
virtual bool givesPrimarySkills() const { return false; }
virtual bool givesSecondarySkills() const { return false; }
virtual bool givesArtifacts() const { return false; }
virtual bool givesCreatures() const { return false; }
virtual bool givesSpells() const { return false; }
virtual bool givesBonuses() const { return false; }
2016-11-25 12:36:54 +02:00
virtual ~IObjectInfo() = default;
2014-06-05 19:57:43 +03:00
};
class CGObjectInstance;
2022-12-31 15:01:19 +02:00
/// Class responsible for creation of objects of specific type & subtype
class DLL_LINKAGE AObjectTypeHandler : public boost::noncopyable
2014-06-05 19:57:43 +03:00
{
2023-01-10 16:24:42 +02:00
friend class CObjectClassesHandler;
2023-01-10 16:24:42 +02:00
RandomMapInfo rmgInfo;
2014-06-05 19:57:43 +03:00
JsonNode base; /// describes base template
std::vector<std::shared_ptr<const ObjectTemplate>> templates;
SObjectSounds sounds;
boost::optional<si32> aiValue;
boost::optional<std::string> battlefield;
std::string modScope;
std::string typeName;
std::string subTypeName;
si32 type;
si32 subtype;
2014-06-05 19:57:43 +03:00
protected:
void preInitObject(CGObjectInstance * obj) const;
2023-02-12 22:39:17 +02:00
virtual bool objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const;
/// initialization for classes that inherit this one
virtual void initTypeData(const JsonNode & input);
2022-12-31 15:01:19 +02:00
public:
2023-02-12 22:39:17 +02:00
virtual ~AObjectTypeHandler() = default;
2014-06-05 19:57:43 +03:00
si32 getIndex() const;
si32 getSubIndex() const;
2014-06-05 19:57:43 +03:00
std::string getTypeName() const;
std::string getSubTypeName() const;
2022-12-31 15:01:19 +02:00
/// loads generic data from Json structure and passes it towards type-specific constructors
2022-12-31 15:01:19 +02:00
void init(const JsonNode & input);
/// returns full form of identifier of this object in form of modName:objectName
std::string getJsonKey() const;
/// Returns object-specific name, if set
SObjectSounds getSounds() const;
2014-06-05 19:57:43 +03:00
2023-02-12 22:39:17 +02:00
void addTemplate(const std::shared_ptr<const ObjectTemplate> & templ);
2014-06-05 19:57:43 +03:00
void addTemplate(JsonNode config);
/// returns all templates matching parameters
std::vector<std::shared_ptr<const ObjectTemplate>> getTemplates() const;
2022-09-29 11:44:46 +02:00
std::vector<std::shared_ptr<const ObjectTemplate>> getTemplates(const TerrainId terrainType) const;
/// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
/// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
2022-09-29 11:44:46 +02:00
std::shared_ptr<const ObjectTemplate> getOverride(TerrainId terrainType, const CGObjectInstance * object) const;
BattleField getBattlefield() const;
2014-06-05 19:57:43 +03:00
const RandomMapInfo & getRMGInfo();
boost::optional<si32> getAiValue() const;
/// returns true if this class provides custom text ID's instead of generic per-object name
virtual bool hasNameTextID() const;
/// returns object's name in form of translatable text ID
virtual std::string getNameTextID() const;
/// returns object's name in form of human-readable text
std::string getNameTranslated() const;
virtual bool isStaticObject();
virtual void afterLoadFinalization();
2014-06-05 19:57:43 +03:00
/// Creates object and set up core properties (like ID/subID). Object is NOT initialized
/// to allow creating objects before game start (e.g. map loading)
virtual CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const = 0;
2014-06-05 19:57:43 +03:00
/// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
/// This should set remaining properties, including randomized or depending on map
2014-06-05 19:57:43 +03:00
virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
/// Returns object configuration, if available. Otherwise returns NULL
virtual std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const = 0;
2014-06-05 19:57:43 +03:00
template <typename Handler> void serialize(Handler &h, const int version)
{
h & type;
h & subtype;
h & templates;
h & rmgInfo;
h & modScope;
h & typeName;
h & subTypeName;
h & sounds;
h & aiValue;
h & battlefield;
2014-06-05 19:57:43 +03:00
}
};
typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
2022-12-31 15:01:19 +02:00
/// Class responsible for creation of adventure map objects of specific type
class DLL_LINKAGE ObjectClass
2014-06-05 19:57:43 +03:00
{
2022-12-31 15:01:19 +02:00
public:
std::string modScope;
std::string identifier;
2014-06-05 19:57:43 +03:00
2022-12-31 15:01:19 +02:00
si32 id;
std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map
2022-12-31 15:01:19 +02:00
JsonNode base;
std::vector<TObjectTypeHandler> objects;
ObjectClass() = default;
std::string getJsonKey() const;
std::string getNameTextID() const;
std::string getNameTranslated() const;
2022-12-31 15:01:19 +02:00
template <typename Handler> void serialize(Handler &h, const int version)
{
h & handlerName;
h & base;
h & objects;
h & identifier;
h & modScope;
2022-12-31 15:01:19 +02:00
}
};
2014-06-05 19:57:43 +03:00
2022-12-31 15:01:19 +02:00
/// Main class responsible for creation of all adventure map objects
class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
{
2014-06-05 19:57:43 +03:00
/// list of object handlers, each of them handles only one type
2022-12-31 15:01:19 +02:00
std::vector<ObjectClass * > objects;
2014-06-05 19:57:43 +03:00
/// map that is filled during contruction with all known handlers. Not serializeable due to usage of std::function
2014-06-05 19:57:43 +03:00
std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
/// container with H3 templates, used only during loading, no need to serialize it
typedef std::multimap<std::pair<si32, si32>, std::shared_ptr<const ObjectTemplate>> TTemplatesContainer;
2014-06-05 19:57:43 +03:00
TTemplatesContainer legacyTemplates;
2022-12-31 15:01:19 +02:00
TObjectTypeHandler loadSubObjectFromJson(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index);
void loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj);
void loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index);
2022-12-31 15:01:19 +02:00
ObjectClass * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index);
2014-06-05 19:57:43 +03:00
public:
CObjectClassesHandler();
~CObjectClassesHandler();
2014-06-05 19:57:43 +03:00
std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
void loadObject(std::string scope, std::string name, const JsonNode & data) override;
void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
2022-12-31 15:01:19 +02:00
void loadSubObject(const std::string & identifier, JsonNode config, si32 ID, si32 subID);
void removeSubObject(si32 ID, si32 subID);
2014-06-05 19:57:43 +03:00
void beforeValidate(JsonNode & object) override;
void afterLoadFinalization() override;
std::vector<bool> getDefaultAllowed() const override;
/// Queries to detect loaded objects
std::set<si32> knownObjects() const;
std::set<si32> knownSubObjects(si32 primaryID) const;
2014-06-05 19:57:43 +03:00
/// returns handler for specified object (ID-based). ObjectHandler keeps ownership
TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
2023-02-12 22:39:17 +02:00
TObjectTypeHandler getHandlerFor(const std::string & scope, const std::string & type, const std::string & subtype) const;
2018-07-22 19:12:11 +02:00
TObjectTypeHandler getHandlerFor(CompoundMapObjectID compoundIdentifier) const;
2014-06-05 19:57:43 +03:00
std::string getObjectName(si32 type, si32 subtype) const;
SObjectSounds getObjectSounds(si32 type, si32 subtype) const;
/// Returns handler string describing the handler (for use in client)
std::string getObjectHandlerName(si32 type) const;
2014-06-05 19:57:43 +03:00
template <typename Handler> void serialize(Handler &h, const int version)
{
h & objects;
}
};
VCMI_LIB_NAMESPACE_END