2013-04-21 15:49:26 +03:00
|
|
|
/*
|
|
|
|
* IHandlerBase.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
|
|
|
|
|
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
|
|
|
#include "../lib/ConstTransitivePtr.h"
|
|
|
|
#include "VCMI_Lib.h"
|
2013-04-21 15:49:26 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
class JsonNode;
|
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
|
|
|
class Entity;
|
2013-04-21 15:49:26 +03:00
|
|
|
|
|
|
|
/// base class for all handlers that can be accessed from mod system
|
|
|
|
class DLL_LINKAGE IHandlerBase
|
|
|
|
{
|
2014-06-25 17:11:07 +03:00
|
|
|
protected:
|
2022-12-07 15:18:19 +02:00
|
|
|
std::string getScopeBuiltin() const;
|
|
|
|
|
2014-06-25 17:11:07 +03:00
|
|
|
/// Calls modhandler. Mostly needed to avoid large number of includes in headers
|
|
|
|
void registerObject(std::string scope, std::string type_name, std::string name, si32 index);
|
2013-04-21 15:49:26 +03:00
|
|
|
public:
|
|
|
|
/// loads all original game data in vector of json nodes
|
|
|
|
/// dataSize - is number of items that must be loaded (normally - constant from GameConstants)
|
|
|
|
virtual std::vector<JsonNode> loadLegacyData(size_t dataSize) = 0;
|
|
|
|
|
|
|
|
/// loads single object into game. Scope is namespace of this object, same as name of source mod
|
|
|
|
virtual void loadObject(std::string scope, std::string name, const JsonNode & data) = 0;
|
|
|
|
virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) = 0;
|
|
|
|
|
2014-05-18 17:47:18 +03:00
|
|
|
/// allows handlers to alter object configuration before validation and actual load
|
|
|
|
virtual void beforeValidate(JsonNode & object){};
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
/// allows handler to load some custom internal data before identifier finalization
|
|
|
|
virtual void loadCustom(){};
|
|
|
|
|
2013-07-21 17:19:29 +03:00
|
|
|
/// allows handler to do post-loading step for validation or integration of loaded data
|
|
|
|
virtual void afterLoadFinalization(){};
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
/**
|
|
|
|
* Gets a list of objects that are allowed by default on maps
|
|
|
|
*
|
|
|
|
* @return a list of allowed objects, the index is the object id
|
|
|
|
*/
|
|
|
|
virtual std::vector<bool> getDefaultAllowed() const = 0;
|
|
|
|
|
|
|
|
virtual ~IHandlerBase(){}
|
2014-03-07 16:21:09 +03:00
|
|
|
};
|
|
|
|
|
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
|
|
|
template <class _ObjectID, class _ObjectBase, class _Object, class _ServiceBase> class CHandlerBase : public _ServiceBase, public IHandlerBase
|
2014-03-07 16:21:09 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-03-08 22:02:47 +03:00
|
|
|
virtual ~CHandlerBase()
|
|
|
|
{
|
|
|
|
for(auto & o : objects)
|
|
|
|
{
|
|
|
|
o.dellNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
const Entity * getBaseByIndex(const int32_t index) const override
|
|
|
|
{
|
|
|
|
return getByIndex(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
const _ObjectBase * getById(const _ObjectID & id) const override
|
|
|
|
{
|
|
|
|
return (*this)[id].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
const _ObjectBase * getByIndex(const int32_t index) const override
|
|
|
|
{
|
|
|
|
return (*this)[_ObjectID(index)].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void forEachBase(const std::function<void(const Entity * entity, bool & stop)> & cb) const override
|
|
|
|
{
|
|
|
|
forEachT(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void forEach(const std::function<void(const _ObjectBase * entity, bool & stop)> & cb) const override
|
|
|
|
{
|
|
|
|
forEachT(cb);
|
|
|
|
}
|
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
void loadObject(std::string scope, std::string name, const JsonNode & data) override
|
|
|
|
{
|
2023-01-18 23:56:01 +02:00
|
|
|
auto object = loadFromJson(scope, data, name, objects.size());
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-08 22:02:47 +03:00
|
|
|
objects.push_back(object);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2018-03-31 07:56:40 +02:00
|
|
|
for(auto type_name : getTypeNames())
|
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
|
|
|
registerObject(scope, type_name, name, object->getIndex());
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
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
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override
|
|
|
|
{
|
2023-01-18 23:56:01 +02:00
|
|
|
auto object = loadFromJson(scope, data, name, index);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-08 22:02:47 +03:00
|
|
|
assert(objects[index] == nullptr); // ensure that this id was not loaded before
|
|
|
|
objects[index] = object;
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2018-03-31 07:56:40 +02:00
|
|
|
for(auto type_name : getTypeNames())
|
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
|
|
|
registerObject(scope, type_name, name, object->getIndex());
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstTransitivePtr<_Object> operator[] (const _ObjectID id) const
|
2014-03-08 22:02:47 +03:00
|
|
|
{
|
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
|
|
|
const int32_t raw_id = id.getNum();
|
|
|
|
return operator[](raw_id);
|
|
|
|
}
|
2014-03-08 22:02:47 +03:00
|
|
|
|
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
|
|
|
ConstTransitivePtr<_Object> operator[] (int32_t index) const
|
|
|
|
{
|
|
|
|
if(index < 0 || index >= objects.size())
|
2014-03-08 22:02:47 +03:00
|
|
|
{
|
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
|
|
|
logMod->error("%s id %d is invalid", getTypeNames()[0], index);
|
2017-08-11 19:03:05 +02:00
|
|
|
throw std::runtime_error("internal error");
|
2014-03-08 22:02:47 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
return objects[index];
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
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
|
|
|
|
|
|
|
void updateEntity(int32_t index, const JsonNode & data)
|
|
|
|
{
|
|
|
|
if(index < 0 || index >= objects.size())
|
|
|
|
{
|
|
|
|
logMod->error("%s id %d is invalid", getTypeNames()[0], index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
objects.at(index)->updateFrom(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 07:56:40 +02:00
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return objects.size();
|
|
|
|
}
|
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
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
protected:
|
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
|
|
|
virtual _Object * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) = 0;
|
2018-03-31 07:56:40 +02:00
|
|
|
virtual const std::vector<std::string> & getTypeNames() const = 0;
|
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
|
|
|
|
|
|
|
template<typename ItemType>
|
|
|
|
void forEachT(const std::function<void(const ItemType *, bool &)> & cb) const
|
|
|
|
{
|
|
|
|
bool stop = false;
|
|
|
|
|
|
|
|
for(auto & object : objects)
|
|
|
|
{
|
|
|
|
cb(object.get(), stop);
|
|
|
|
if(stop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
public: //todo: make private
|
2014-03-08 22:02:47 +03:00
|
|
|
std::vector<ConstTransitivePtr<_Object>> objects;
|
2014-03-07 16:21:09 +03:00
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|