2017-09-13 02:35:58 +02:00
|
|
|
/*
|
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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "ObjectTemplate.h"
|
|
|
|
|
|
|
|
#include "../GameConstants.h"
|
|
|
|
#include "../ConstTransitivePtr.h"
|
|
|
|
#include "../IHandlerBase.h"
|
|
|
|
#include "../JsonNode.h"
|
2022-06-26 09:21:05 +02:00
|
|
|
#include "Terrain.h"
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
class JsonNode;
|
|
|
|
class CRandomGenerator;
|
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-05 23:51:24 +03:00
|
|
|
/// Structure that describes placement rules for this object in random map
|
2014-06-26 21:12:37 +03:00
|
|
|
struct DLL_LINKAGE RandomMapInfo
|
2014-06-05 23:51:24 +03:00
|
|
|
{
|
|
|
|
/// 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)
|
|
|
|
{
|
2017-07-31 15:35:42 +02:00
|
|
|
h & value;
|
|
|
|
h & mapLimit;
|
|
|
|
h & zoneLimit;
|
|
|
|
h & rarity;
|
2014-06-05 23:51:24 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-26 21:12:37 +03:00
|
|
|
class DLL_LINKAGE IObjectInfo
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-06-22 13:39:40 +03:00
|
|
|
struct CArmyStructure
|
|
|
|
{
|
|
|
|
ui32 totalStrength;
|
|
|
|
ui32 shootersStrength;
|
|
|
|
ui32 flyersStrength;
|
|
|
|
ui32 walkersStrength;
|
|
|
|
|
|
|
|
CArmyStructure() :
|
|
|
|
totalStrength(0),
|
|
|
|
shootersStrength(0),
|
|
|
|
flyersStrength(0),
|
|
|
|
walkersStrength(0)
|
|
|
|
{}
|
|
|
|
|
2014-06-26 01:23:12 +03:00
|
|
|
bool operator <(const CArmyStructure & other) const
|
2014-06-22 13:39:40 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2014-06-23 09:24:49 +03:00
|
|
|
class DLL_LINKAGE AObjectTypeHandler : public boost::noncopyable
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2014-06-05 23:51:24 +03:00
|
|
|
RandomMapInfo rmgInfo;
|
|
|
|
|
2014-06-30 00:16:45 +03:00
|
|
|
/// Human-readable name of this object, used for objects like banks and dwellings, if set
|
|
|
|
boost::optional<std::string> objectName;
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
JsonNode base; /// describes base template
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
std::vector<std::shared_ptr<const ObjectTemplate>> templates;
|
2017-09-13 02:35:58 +02:00
|
|
|
|
|
|
|
SObjectSounds sounds;
|
2018-07-28 00:43:56 +02:00
|
|
|
|
|
|
|
boost::optional<si32> aiValue;
|
2022-06-26 09:21:05 +02:00
|
|
|
|
2022-06-28 10:05:30 +02:00
|
|
|
boost::optional<std::string> battlefield;
|
2022-06-26 09:21:05 +02:00
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
protected:
|
2015-11-14 15:50:29 +02:00
|
|
|
void preInitObject(CGObjectInstance * obj) const;
|
2022-09-11 15:12:35 +02:00
|
|
|
virtual bool objectFilter(const CGObjectInstance *, std::shared_ptr<const ObjectTemplate>) const;
|
2014-06-14 18:42:13 +03:00
|
|
|
|
|
|
|
/// initialization for classes that inherit this one
|
|
|
|
virtual void initTypeData(const JsonNode & input);
|
2014-06-05 19:57:43 +03:00
|
|
|
public:
|
2016-02-26 01:03:14 +02:00
|
|
|
std::string typeName;
|
|
|
|
std::string subTypeName;
|
|
|
|
|
|
|
|
si32 type;
|
|
|
|
si32 subtype;
|
2016-11-27 21:07:01 +02:00
|
|
|
AObjectTypeHandler();
|
|
|
|
virtual ~AObjectTypeHandler();
|
2014-06-05 19:57:43 +03:00
|
|
|
|
|
|
|
void setType(si32 type, si32 subtype);
|
2015-11-14 15:50:29 +02:00
|
|
|
void setTypeName(std::string type, std::string subtype);
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
/// loads generic data from Json structure and passes it towards type-specific constructors
|
2014-06-30 00:16:45 +03:00
|
|
|
void init(const JsonNode & input, boost::optional<std::string> name = boost::optional<std::string>());
|
|
|
|
|
|
|
|
/// Returns object-specific name, if set
|
|
|
|
boost::optional<std::string> getCustomName() const;
|
2017-09-13 02:35:58 +02:00
|
|
|
SObjectSounds getSounds() const;
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
void addTemplate(std::shared_ptr<const ObjectTemplate> templ);
|
2014-06-05 19:57:43 +03:00
|
|
|
void addTemplate(JsonNode config);
|
|
|
|
|
2014-06-05 23:51:24 +03:00
|
|
|
/// returns all templates matching parameters
|
2022-09-11 15:12:35 +02:00
|
|
|
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;
|
2022-09-11 15:12:35 +02:00
|
|
|
|
|
|
|
/// 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;
|
2022-09-11 15:12:35 +02:00
|
|
|
|
2022-06-26 09:21:05 +02:00
|
|
|
BattleField getBattlefield() const;
|
2014-06-05 19:57:43 +03:00
|
|
|
|
|
|
|
/// 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)
|
|
|
|
|
2014-06-05 23:51:24 +03:00
|
|
|
const RandomMapInfo & getRMGInfo();
|
|
|
|
|
2018-07-28 00:43:56 +02:00
|
|
|
boost::optional<si32> getAiValue() const;
|
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
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)
|
2022-09-11 15:12:35 +02:00
|
|
|
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
|
2014-06-14 18:42:13 +03:00
|
|
|
/// 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;
|
|
|
|
|
2015-11-14 15:50:29 +02:00
|
|
|
/// Returns object configuration, if available. Otherwise returns NULL
|
2022-09-11 15:12:35 +02:00
|
|
|
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)
|
|
|
|
{
|
2017-07-31 15:35:42 +02:00
|
|
|
h & type;
|
|
|
|
h & subtype;
|
|
|
|
h & templates;
|
|
|
|
h & rmgInfo;
|
|
|
|
h & objectName;
|
2022-06-20 16:39:50 +02:00
|
|
|
h & typeName;
|
|
|
|
h & subTypeName;
|
|
|
|
h & sounds;
|
|
|
|
h & aiValue;
|
2022-06-28 10:05:30 +02:00
|
|
|
h & battlefield;
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
|
|
|
|
|
|
|
|
class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
|
|
|
|
{
|
|
|
|
/// Small internal structure that contains information on specific group of objects
|
|
|
|
/// (creating separate entity is overcomplicating at least at this point)
|
|
|
|
struct ObjectContainter
|
|
|
|
{
|
|
|
|
si32 id;
|
2015-11-14 15:50:29 +02:00
|
|
|
std::string identifier;
|
2014-06-05 19:57:43 +03:00
|
|
|
std::string name; // human-readable name
|
2015-11-14 15:50:29 +02:00
|
|
|
std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map
|
2014-06-05 19:57:43 +03:00
|
|
|
|
|
|
|
JsonNode base;
|
2015-11-14 15:50:29 +02:00
|
|
|
std::map<si32, TObjectTypeHandler> subObjects;
|
|
|
|
std::map<std::string, si32> subIds;//full id from core scope -> subtype
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
SObjectSounds sounds;
|
|
|
|
|
2018-07-28 00:43:56 +02:00
|
|
|
boost::optional<si32> groupDefaultAiValue;
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2017-07-31 15:35:42 +02:00
|
|
|
h & name;
|
|
|
|
h & handlerName;
|
|
|
|
h & base;
|
|
|
|
h & subObjects;
|
2022-06-20 16:39:50 +02:00
|
|
|
h & identifier;
|
|
|
|
h & subIds;
|
|
|
|
h & sounds;
|
|
|
|
h & groupDefaultAiValue;
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// list of object handlers, each of them handles only one type
|
|
|
|
std::map<si32, ObjectContainter * > objects;
|
|
|
|
|
2014-06-05 23:51:24 +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;
|
|
|
|
|
2014-06-05 23:51:24 +03:00
|
|
|
/// container with H3 templates, used only during loading, no need to serialize it
|
2022-09-11 15:12:35 +02:00
|
|
|
typedef std::multimap<std::pair<si32, si32>, std::shared_ptr<const ObjectTemplate>> TTemplatesContainer;
|
2014-06-05 19:57:43 +03:00
|
|
|
TTemplatesContainer legacyTemplates;
|
|
|
|
|
2014-06-30 00:16:45 +03:00
|
|
|
/// contains list of custom names for H3 objects (e.g. Dwellings), used to load H3 data
|
|
|
|
/// format: customNames[primaryID][secondaryID] -> name
|
|
|
|
std::map<si32, std::vector<std::string>> customNames;
|
|
|
|
|
2021-04-25 14:07:06 +02:00
|
|
|
void loadObjectEntry(const std::string & identifier, const JsonNode & entry, ObjectContainter * obj, bool isSubobject = false);
|
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
|
|
|
ObjectContainter * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name);
|
2021-04-25 14:07:06 +02:00
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
public:
|
|
|
|
CObjectClassesHandler();
|
2016-08-17 08:24:01 +02:00
|
|
|
~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;
|
|
|
|
|
2015-11-14 15:50:29 +02:00
|
|
|
void loadSubObject(const std::string & identifier, JsonNode config, si32 ID, boost::optional<si32> subID = boost::optional<si32>());
|
2014-06-05 23:51:24 +03:00
|
|
|
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;
|
|
|
|
|
2014-06-05 23:51:24 +03:00
|
|
|
/// 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;
|
2022-11-29 22:31:33 +02:00
|
|
|
TObjectTypeHandler getHandlerFor(std::string scope, std::string type, 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) const;
|
2014-06-30 00:16:45 +03:00
|
|
|
std::string getObjectName(si32 type, si32 subtype) const;
|
2015-11-14 15:50:29 +02:00
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
SObjectSounds getObjectSounds(si32 type) const;
|
|
|
|
SObjectSounds getObjectSounds(si32 type, si32 subtype) const;
|
|
|
|
|
2014-07-30 19:07:30 +03:00
|
|
|
/// Returns handler string describing the handler (for use in client)
|
|
|
|
std::string getObjectHandlerName(si32 type) const;
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2018-07-28 00:43:56 +02:00
|
|
|
boost::optional<si32> getObjGroupAiValue(si32 primaryID) const; //default AI value of objects belonging to particular primaryID
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & objects;
|
|
|
|
}
|
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|