1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00
vcmi/lib/IHandlerBase.h

106 lines
3.4 KiB
C
Raw Normal View History

/*
* 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
*
*/
#pragma once
#include "../lib/ConstTransitivePtr.h"
#include "VCMI_Lib.h"
class JsonNode;
/// base class for all handlers that can be accessed from mod system
class DLL_LINKAGE IHandlerBase
{
// there also should be private member with such signature:
// Object * loadFromJson(const JsonNode & json);
// where Object is type of data loaded by handler
// primary used in loadObject methods
protected:
/// 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);
2016-02-21 19:58:09 +02:00
std::string normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier) const;
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;
/// allows handlers to alter object configuration before validation and actual load
virtual void beforeValidate(JsonNode & object){};
/// allows handler to load some custom internal data before identifier finalization
virtual void loadCustom(){};
/// allows handler to do post-loading step for validation or integration of loaded data
virtual void afterLoadFinalization(){};
/**
* 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(){}
};
template <class _ObjectID, class _Object> class CHandlerBase: public IHandlerBase
{
public:
2014-03-08 22:02:47 +03:00
virtual ~CHandlerBase()
{
for(auto & o : objects)
{
o.dellNull();
}
}
void loadObject(std::string scope, std::string name, const JsonNode & data) override
{
2014-03-08 22:02:47 +03:00
auto type_name = getTypeName();
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name), objects.size());
2014-03-08 22:02:47 +03:00
objects.push_back(object);
registerObject(scope, type_name, name, object->id);
}
void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override
{
2014-03-08 22:02:47 +03:00
auto type_name = getTypeName();
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name), index);
2014-03-08 22:02:47 +03:00
assert(objects[index] == nullptr); // ensure that this id was not loaded before
objects[index] = object;
registerObject(scope,type_name, name, object->id);
}
ConstTransitivePtr<_Object> operator[] (const _ObjectID id) const
2014-03-08 22:02:47 +03:00
{
const auto raw_id = id.toEnum();
if (raw_id < 0 || raw_id >= objects.size())
{
2017-08-30 23:23:19 +02:00
logMod->error("%s id %d is invalid", getTypeName(), static_cast<si64>(raw_id));
throw std::runtime_error("internal error");
2014-03-08 22:02:47 +03:00
}
return objects[raw_id];
}
protected:
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
virtual _Object * loadFromJson(const JsonNode & json, const std::string & identifier, size_t index) = 0;
2014-03-08 22:02:47 +03:00
virtual const std::string getTypeName() const = 0;
public: //todo: make private
2014-03-08 22:02:47 +03:00
std::vector<ConstTransitivePtr<_Object>> objects;
};