2022-06-20 16:39:50 +02:00
|
|
|
/*
|
2017-07-13 10:26:03 +02:00
|
|
|
* CObjectClassesHandler.cpp, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
2014-06-05 19:57:43 +03:00
|
|
|
#include "CObjectClassesHandler.h"
|
|
|
|
|
2014-06-05 20:26:50 +03:00
|
|
|
#include "../filesystem/Filesystem.h"
|
|
|
|
#include "../filesystem/CBinaryReader.h"
|
2014-06-23 20:58:19 +03:00
|
|
|
#include "../VCMI_Lib.h"
|
2014-06-05 20:26:50 +03:00
|
|
|
#include "../GameConstants.h"
|
|
|
|
#include "../StringConstants.h"
|
|
|
|
#include "../CGeneralTextHandler.h"
|
|
|
|
#include "../CModHandler.h"
|
|
|
|
#include "../JsonNode.h"
|
2017-09-13 02:35:58 +02:00
|
|
|
#include "../CSoundBase.h"
|
2014-06-05 19:57:43 +03:00
|
|
|
|
|
|
|
#include "CRewardableConstructor.h"
|
2014-06-14 18:42:13 +03:00
|
|
|
#include "CommonConstructors.h"
|
2014-06-05 19:57:43 +03:00
|
|
|
#include "MapObjects.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2018-01-09 09:14:56 +02:00
|
|
|
// FIXME: move into inheritNode?
|
|
|
|
static void inheritNodeWithMeta(JsonNode & descendant, const JsonNode & base)
|
|
|
|
{
|
|
|
|
std::string oldMeta = descendant.meta;
|
|
|
|
JsonUtils::inherit(descendant, base);
|
|
|
|
descendant.setMeta(oldMeta);
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
CObjectClassesHandler::CObjectClassesHandler()
|
|
|
|
{
|
|
|
|
#define SET_HANDLER_CLASS(STRING, CLASSNAME) handlerConstructors[STRING] = std::make_shared<CLASSNAME>;
|
2017-07-16 11:58:05 +02:00
|
|
|
#define SET_HANDLER(STRING, TYPENAME) handlerConstructors[STRING] = std::make_shared<CDefaultObjectTypeHandler<TYPENAME>>
|
2014-06-05 19:57:43 +03:00
|
|
|
|
|
|
|
// list of all known handlers, hardcoded for now since the only way to add new objects is via C++ code
|
2014-06-17 14:57:47 +03:00
|
|
|
//Note: should be in sync with registerTypesMapObjectTypes function
|
2014-06-05 19:57:43 +03:00
|
|
|
SET_HANDLER_CLASS("configurable", CRewardableConstructor);
|
2014-06-14 18:42:13 +03:00
|
|
|
SET_HANDLER_CLASS("dwelling", CDwellingInstanceConstructor);
|
|
|
|
SET_HANDLER_CLASS("hero", CHeroInstanceConstructor);
|
|
|
|
SET_HANDLER_CLASS("town", CTownInstanceConstructor);
|
2014-06-22 13:39:40 +03:00
|
|
|
SET_HANDLER_CLASS("bank", CBankInstanceConstructor);
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
SET_HANDLER_CLASS("static", CObstacleConstructor);
|
|
|
|
SET_HANDLER_CLASS("", CObstacleConstructor);
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2014-06-27 19:08:36 +03:00
|
|
|
SET_HANDLER("randomArtifact", CGArtifact);
|
|
|
|
SET_HANDLER("randomHero", CGHeroInstance);
|
|
|
|
SET_HANDLER("randomResource", CGResource);
|
|
|
|
SET_HANDLER("randomTown", CGTownInstance);
|
|
|
|
SET_HANDLER("randomMonster", CGCreature);
|
|
|
|
SET_HANDLER("randomDwelling", CGDwelling);
|
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
SET_HANDLER("generic", CGObjectInstance);
|
2014-06-05 19:57:43 +03:00
|
|
|
SET_HANDLER("market", CGMarket);
|
|
|
|
SET_HANDLER("cartographer", CCartographer);
|
|
|
|
SET_HANDLER("artifact", CGArtifact);
|
|
|
|
SET_HANDLER("blackMarket", CGBlackMarket);
|
|
|
|
SET_HANDLER("boat", CGBoat);
|
|
|
|
SET_HANDLER("bonusingObject", CGBonusingObject);
|
|
|
|
SET_HANDLER("borderGate", CGBorderGate);
|
|
|
|
SET_HANDLER("borderGuard", CGBorderGuard);
|
|
|
|
SET_HANDLER("monster", CGCreature);
|
|
|
|
SET_HANDLER("denOfThieves", CGDenOfthieves);
|
|
|
|
SET_HANDLER("event", CGEvent);
|
|
|
|
SET_HANDLER("garrison", CGGarrison);
|
|
|
|
SET_HANDLER("heroPlaceholder", CGHeroPlaceholder);
|
|
|
|
SET_HANDLER("keymaster", CGKeymasterTent);
|
|
|
|
SET_HANDLER("lighthouse", CGLighthouse);
|
|
|
|
SET_HANDLER("magi", CGMagi);
|
|
|
|
SET_HANDLER("magicSpring", CGMagicSpring);
|
|
|
|
SET_HANDLER("magicWell", CGMagicWell);
|
|
|
|
SET_HANDLER("market", CGMarket);
|
|
|
|
SET_HANDLER("mine", CGMine);
|
|
|
|
SET_HANDLER("obelisk", CGObelisk);
|
|
|
|
SET_HANDLER("observatory", CGObservatory);
|
|
|
|
SET_HANDLER("onceVisitable", CGOnceVisitable);
|
|
|
|
SET_HANDLER("pandora", CGPandoraBox);
|
|
|
|
SET_HANDLER("pickable", CGPickable);
|
2014-06-17 14:57:47 +03:00
|
|
|
SET_HANDLER("prison", CGHeroInstance);
|
2014-06-05 19:57:43 +03:00
|
|
|
SET_HANDLER("questGuard", CGQuestGuard);
|
|
|
|
SET_HANDLER("resource", CGResource);
|
|
|
|
SET_HANDLER("scholar", CGScholar);
|
|
|
|
SET_HANDLER("seerHut", CGSeerHut);
|
|
|
|
SET_HANDLER("shipyard", CGShipyard);
|
|
|
|
SET_HANDLER("shrine", CGShrine);
|
|
|
|
SET_HANDLER("sign", CGSignBottle);
|
|
|
|
SET_HANDLER("siren", CGSirens);
|
2015-03-08 15:11:23 +02:00
|
|
|
SET_HANDLER("monolith", CGMonolith);
|
|
|
|
SET_HANDLER("subterraneanGate", CGSubterraneanGate);
|
|
|
|
SET_HANDLER("whirlpool", CGWhirlpool);
|
2014-06-05 19:57:43 +03:00
|
|
|
SET_HANDLER("university", CGUniversity);
|
|
|
|
SET_HANDLER("oncePerHero", CGVisitableOPH);
|
|
|
|
SET_HANDLER("oncePerWeek", CGVisitableOPW);
|
|
|
|
SET_HANDLER("witch", CGWitchHut);
|
2022-06-26 09:21:05 +02:00
|
|
|
SET_HANDLER("terrain", CGTerrainPatch);
|
2014-06-05 19:57:43 +03:00
|
|
|
|
|
|
|
#undef SET_HANDLER_CLASS
|
|
|
|
#undef SET_HANDLER
|
|
|
|
}
|
|
|
|
|
2016-08-17 08:24:01 +02:00
|
|
|
CObjectClassesHandler::~CObjectClassesHandler()
|
|
|
|
{
|
|
|
|
for(auto p : objects)
|
|
|
|
delete p.second;
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:57:47 +03:00
|
|
|
std::vector<JsonNode> CObjectClassesHandler::loadLegacyData(size_t dataSize)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2014-06-17 14:57:47 +03:00
|
|
|
CLegacyConfigParser parser("Data/Objects.txt");
|
2020-10-01 10:38:06 +02:00
|
|
|
size_t totalNumber = static_cast<size_t>(parser.readNumber()); // first line contains number of objects to read and nothing else
|
2014-06-05 19:57:43 +03:00
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
for (size_t i=0; i<totalNumber; i++)
|
|
|
|
{
|
2022-09-11 15:12:35 +02:00
|
|
|
auto templ = new ObjectTemplate;
|
|
|
|
templ->readTxt(parser);
|
2014-06-05 19:57:43 +03:00
|
|
|
parser.endLine();
|
2022-09-11 15:12:35 +02:00
|
|
|
std::pair<si32, si32> key(templ->id.num, templ->subid);
|
|
|
|
legacyTemplates.insert(std::make_pair(key, std::shared_ptr<const ObjectTemplate>(templ)));
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<JsonNode> ret(dataSize);// create storage for 256 objects
|
|
|
|
assert(dataSize == 256);
|
|
|
|
|
2014-06-17 14:57:47 +03:00
|
|
|
CLegacyConfigParser namesParser("Data/ObjNames.txt");
|
2014-06-05 19:57:43 +03:00
|
|
|
for (size_t i=0; i<256; i++)
|
|
|
|
{
|
2014-06-17 14:57:47 +03:00
|
|
|
ret[i]["name"].String() = namesParser.readString();
|
|
|
|
namesParser.endLine();
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
2014-06-30 00:16:45 +03:00
|
|
|
|
|
|
|
CLegacyConfigParser cregen1Parser("data/crgen1");
|
|
|
|
do
|
|
|
|
customNames[Obj::CREATURE_GENERATOR1].push_back(cregen1Parser.readString());
|
|
|
|
while(cregen1Parser.endLine());
|
|
|
|
|
|
|
|
CLegacyConfigParser cregen4Parser("data/crgen4");
|
|
|
|
do
|
|
|
|
customNames[Obj::CREATURE_GENERATOR4].push_back(cregen4Parser.readString());
|
|
|
|
while(cregen4Parser.endLine());
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// selects preferred ID (or subID) for new object
|
|
|
|
template<typename Map>
|
2021-04-25 14:07:06 +02:00
|
|
|
si32 selectNextID(const JsonNode & fixedID, const Map & map, si32 fixedObjectsBound)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2021-04-25 14:07:06 +02:00
|
|
|
assert(fixedObjectsBound > 0);
|
|
|
|
if(fixedID.isNull())
|
|
|
|
{
|
|
|
|
auto lastID = map.empty() ? 0 : map.rbegin()->first;
|
|
|
|
return lastID < fixedObjectsBound ? fixedObjectsBound : lastID + 1;
|
|
|
|
}
|
|
|
|
auto id = static_cast<si32>(fixedID.Float());
|
|
|
|
if(id >= fixedObjectsBound)
|
|
|
|
logGlobal->error("Getting next ID overflowed: %d >= %d", id, fixedObjectsBound);
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2021-04-25 14:07:06 +02:00
|
|
|
return id;
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 14:07:06 +02:00
|
|
|
void CObjectClassesHandler::loadObjectEntry(const std::string & identifier, const JsonNode & entry, ObjectContainter * obj, bool isSubobject)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2021-04-25 14:07:06 +02:00
|
|
|
static const si32 fixedObjectsBound = 1000; // legacy value for backward compatibilitty
|
|
|
|
static const si32 fixedSubobjectsBound = 10000000; // large enough arbitrary value to avoid ID-collisions
|
|
|
|
si32 usedBound = fixedObjectsBound;
|
|
|
|
|
|
|
|
if(!handlerConstructors.count(obj->handlerName))
|
2014-06-22 13:39:40 +03:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Handler with name %s was not found!", obj->handlerName);
|
2014-06-22 13:39:40 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-04-25 14:07:06 +02:00
|
|
|
const auto convertedId = VLC->modh->normalizeIdentifier(entry.meta, "core", identifier);
|
|
|
|
const auto & entryIndex = entry["index"];
|
|
|
|
bool useSelectNextID = !isSubobject || entryIndex.isNull();
|
2015-11-16 16:38:42 +02:00
|
|
|
|
2021-04-25 14:07:06 +02:00
|
|
|
if(useSelectNextID && isSubobject)
|
|
|
|
{
|
|
|
|
usedBound = fixedSubobjectsBound;
|
|
|
|
logGlobal->error("Subobject index is Null. convertedId = '%s' obj->id = %d", convertedId, obj->id);
|
|
|
|
}
|
|
|
|
si32 id = useSelectNextID ? selectNextID(entryIndex, obj->subObjects, usedBound)
|
|
|
|
: (si32)entryIndex.Float();
|
2014-06-30 00:16:45 +03:00
|
|
|
|
|
|
|
auto handler = handlerConstructors.at(obj->handlerName)();
|
2014-06-05 19:57:43 +03:00
|
|
|
handler->setType(obj->id, id);
|
2015-11-16 16:38:42 +02:00
|
|
|
handler->setTypeName(obj->identifier, convertedId);
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2014-06-30 00:16:45 +03:00
|
|
|
if (customNames.count(obj->id) && customNames.at(obj->id).size() > id)
|
|
|
|
handler->init(entry, customNames.at(obj->id).at(id));
|
|
|
|
else
|
|
|
|
handler->init(entry);
|
|
|
|
|
2022-06-20 16:39:50 +02:00
|
|
|
//if (handler->getTemplates().empty())
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
|
|
|
auto range = legacyTemplates.equal_range(std::make_pair(obj->id, id));
|
|
|
|
for (auto & templ : boost::make_iterator_range(range.first, range.second))
|
|
|
|
{
|
|
|
|
handler->addTemplate(templ.second);
|
|
|
|
}
|
|
|
|
legacyTemplates.erase(range.first, range.second);
|
|
|
|
}
|
2014-06-30 17:11:25 +03:00
|
|
|
|
2018-01-09 09:14:56 +02:00
|
|
|
logGlobal->debug("Loaded object %s(%d)::%s(%d)", obj->identifier, obj->id, convertedId, id);
|
2020-10-03 23:06:25 +02:00
|
|
|
|
|
|
|
//some mods redefine content handlers in the decoration.json in such way:
|
|
|
|
//"core:sign" : { "types" : { "forgeSign" : { ...
|
2021-04-25 14:07:06 +02:00
|
|
|
static const std::vector<std::string> breakersRMG
|
2020-10-25 00:03:32 +02:00
|
|
|
{
|
|
|
|
"hota.hota decorations:hotaPandoraBox"
|
|
|
|
, "hota.hota decorations:hotaSubterreanGate"
|
|
|
|
};
|
2021-04-25 14:07:06 +02:00
|
|
|
const bool isExistingKey = obj->subObjects.count(id) > 0;
|
|
|
|
const bool isBreaker = std::any_of(breakersRMG.begin(), breakersRMG.end(),
|
|
|
|
[&handler](const std::string & str)
|
|
|
|
{
|
|
|
|
return str.compare(handler->subTypeName) == 0;
|
|
|
|
});
|
|
|
|
const bool passedHandler = !isExistingKey && !isBreaker;
|
2020-10-25 00:03:32 +02:00
|
|
|
|
2021-04-25 14:07:06 +02:00
|
|
|
if(passedHandler)
|
2020-10-03 23:06:25 +02:00
|
|
|
{
|
2021-02-20 03:57:50 +02:00
|
|
|
obj->subObjects[id] = handler;
|
|
|
|
obj->subIds[convertedId] = id;
|
|
|
|
}
|
2021-04-25 14:07:06 +02:00
|
|
|
else if(isExistingKey) //It's supposed that fan mods handlers are not overridden by default handlers
|
|
|
|
{
|
|
|
|
logGlobal->trace("Handler '%s' has not been overridden with handler '%s' in object %s(%d)::%s(%d)",
|
|
|
|
obj->subObjects[id]->subTypeName, obj->handlerName, obj->identifier, obj->id, convertedId, id);
|
|
|
|
}
|
2020-10-25 00:03:32 +02:00
|
|
|
else
|
|
|
|
{
|
2021-04-25 14:07:06 +02:00
|
|
|
logGlobal->warn("Handler '%s' for object %s(%d)::%s(%d) has not been activated as RMG breaker",
|
|
|
|
obj->handlerName, obj->identifier, obj->id, convertedId, id);
|
2020-10-25 00:03:32 +02:00
|
|
|
}
|
2014-06-05 19:57:43 +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
|
|
|
CObjectClassesHandler::ObjectContainter * CObjectClassesHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
|
|
|
auto obj = new ObjectContainter();
|
2021-04-25 14:07:06 +02:00
|
|
|
static const si32 fixedObjectsBound = 256; //Legacy value for backward compatibility
|
|
|
|
|
2015-11-14 15:50:29 +02:00
|
|
|
obj->identifier = name;
|
2014-06-05 19:57:43 +03:00
|
|
|
obj->name = json["name"].String();
|
|
|
|
obj->handlerName = json["handler"].String();
|
2014-06-16 19:27:26 +03:00
|
|
|
obj->base = json["base"];
|
2021-04-25 14:07:06 +02:00
|
|
|
obj->id = selectNextID(json["index"], objects, fixedObjectsBound);
|
|
|
|
|
2018-07-28 00:43:56 +02:00
|
|
|
if(json["defaultAiValue"].isNull())
|
|
|
|
obj->groupDefaultAiValue = boost::none;
|
|
|
|
else
|
2020-10-07 14:12:32 +02:00
|
|
|
obj->groupDefaultAiValue = static_cast<boost::optional<si32>>(json["defaultAiValue"].Integer());
|
2018-07-28 00:43:56 +02:00
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
for (auto entry : json["types"].Struct())
|
2015-11-14 15:50:29 +02:00
|
|
|
loadObjectEntry(entry.first, entry.second, obj);
|
2017-09-13 02:35:58 +02:00
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
|
|
|
|
{
|
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
|
|
|
auto object = loadFromJson(scope, data, normalizeIdentifier(scope, "core", name));
|
2014-06-05 19:57:43 +03:00
|
|
|
objects[object->id] = object;
|
|
|
|
VLC->modh->identifiers.registerObject(scope, "object", name, object->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
|
|
|
|
{
|
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
|
|
|
auto object = loadFromJson(scope, data, normalizeIdentifier(scope, "core", name));
|
2020-10-01 10:38:06 +02:00
|
|
|
assert(objects[(si32)index] == nullptr); // ensure that this id was not loaded before
|
|
|
|
objects[(si32)index] = object;
|
2014-06-05 19:57:43 +03:00
|
|
|
VLC->modh->identifiers.registerObject(scope, "object", name, object->id);
|
|
|
|
}
|
|
|
|
|
2015-11-14 15:50:29 +02:00
|
|
|
void CObjectClassesHandler::loadSubObject(const std::string & identifier, JsonNode config, si32 ID, boost::optional<si32> subID)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2021-04-25 14:07:06 +02:00
|
|
|
static const bool isSubObject = true;
|
|
|
|
|
2017-11-26 23:18:18 +02:00
|
|
|
config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not NULL
|
2014-06-05 19:57:43 +03:00
|
|
|
assert(objects.count(ID));
|
|
|
|
if (subID)
|
|
|
|
{
|
2015-11-14 15:50:29 +02:00
|
|
|
assert(objects.at(ID)->subObjects.count(subID.get()) == 0);
|
2014-06-05 19:57:43 +03:00
|
|
|
assert(config["index"].isNull());
|
|
|
|
config["index"].Float() = subID.get();
|
|
|
|
}
|
2018-01-09 09:14:56 +02:00
|
|
|
inheritNodeWithMeta(config, objects.at(ID)->base);
|
2021-04-25 14:07:06 +02:00
|
|
|
loadObjectEntry(identifier, config, objects[ID], isSubObject);
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 23:51:24 +03:00
|
|
|
void CObjectClassesHandler::removeSubObject(si32 ID, si32 subID)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
|
|
|
assert(objects.count(ID));
|
2015-11-14 15:50:29 +02:00
|
|
|
assert(objects.at(ID)->subObjects.count(subID));
|
|
|
|
objects.at(ID)->subObjects.erase(subID); //TODO: cleanup string id map
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<bool> CObjectClassesHandler::getDefaultAllowed() const
|
|
|
|
{
|
|
|
|
return std::vector<bool>(); //TODO?
|
|
|
|
}
|
|
|
|
|
|
|
|
TObjectTypeHandler CObjectClassesHandler::getHandlerFor(si32 type, si32 subtype) const
|
|
|
|
{
|
|
|
|
if (objects.count(type))
|
|
|
|
{
|
2015-11-14 15:50:29 +02:00
|
|
|
if (objects.at(type)->subObjects.count(subtype))
|
|
|
|
return objects.at(type)->subObjects.at(subtype);
|
|
|
|
}
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Failed to find object of type %d:%d", type, subtype);
|
2015-11-14 15:50:29 +02:00
|
|
|
throw std::runtime_error("Object type handler not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
TObjectTypeHandler CObjectClassesHandler::getHandlerFor(std::string type, std::string subtype) const
|
|
|
|
{
|
|
|
|
boost::optional<si32> id = VLC->modh->identifiers.getIdentifier("core", "object", type, false);
|
|
|
|
if(id)
|
|
|
|
{
|
2018-01-09 09:14:56 +02:00
|
|
|
auto object = objects.at(id.get());
|
|
|
|
if(object->subIds.count(subtype))
|
|
|
|
{
|
|
|
|
si32 subId = object->subIds.at(subtype);
|
|
|
|
|
|
|
|
return object->subObjects.at(subId);
|
|
|
|
}
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
2018-01-09 09:14:56 +02:00
|
|
|
logGlobal->error("Failed to find object of type %s::%s", type, subtype);
|
2015-11-14 15:50:29 +02:00
|
|
|
throw std::runtime_error("Object type handler not found");
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
2018-07-22 19:12:11 +02:00
|
|
|
TObjectTypeHandler CObjectClassesHandler::getHandlerFor(CompoundMapObjectID compoundIdentifier) const
|
2018-07-22 18:12:29 +02:00
|
|
|
{
|
|
|
|
return getHandlerFor(compoundIdentifier.primaryID, compoundIdentifier.secondaryID);
|
|
|
|
}
|
|
|
|
|
2014-06-05 23:51:24 +03:00
|
|
|
std::set<si32> CObjectClassesHandler::knownObjects() const
|
|
|
|
{
|
|
|
|
std::set<si32> ret;
|
|
|
|
|
|
|
|
for (auto entry : objects)
|
|
|
|
ret.insert(entry.first);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<si32> CObjectClassesHandler::knownSubObjects(si32 primaryID) const
|
|
|
|
{
|
|
|
|
std::set<si32> ret;
|
|
|
|
|
|
|
|
if (objects.count(primaryID))
|
|
|
|
{
|
2015-11-14 15:50:29 +02:00
|
|
|
for (auto entry : objects.at(primaryID)->subObjects)
|
2014-06-05 23:51:24 +03:00
|
|
|
ret.insert(entry.first);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
void CObjectClassesHandler::beforeValidate(JsonNode & object)
|
|
|
|
{
|
|
|
|
for (auto & entry : object["types"].Struct())
|
|
|
|
{
|
2018-01-09 09:14:56 +02:00
|
|
|
inheritNodeWithMeta(entry.second, object["base"]);
|
2014-06-05 19:57:43 +03:00
|
|
|
for (auto & templ : entry.second["templates"].Struct())
|
|
|
|
{
|
2018-01-09 09:14:56 +02:00
|
|
|
inheritNodeWithMeta(templ.second, entry.second["base"]);
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CObjectClassesHandler::afterLoadFinalization()
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
for(auto entry : objects)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
for(auto obj : entry.second->subObjects)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2014-06-14 18:42:13 +03:00
|
|
|
obj.second->afterLoadFinalization();
|
2017-08-11 19:03:05 +02:00
|
|
|
if(obj.second->getTemplates().empty())
|
|
|
|
logGlobal->warn("No templates found for %d:%d", entry.first, obj.first);
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-09 10:03:40 +02:00
|
|
|
|
|
|
|
//duplicate existing two-way portals to make reserve for RMG
|
2016-01-24 15:58:24 +02:00
|
|
|
auto& portalVec = objects[Obj::MONOLITH_TWO_WAY]->subObjects;
|
2016-01-09 10:03:40 +02:00
|
|
|
size_t portalCount = portalVec.size();
|
|
|
|
size_t currentIndex = portalCount;
|
2017-08-11 19:03:05 +02:00
|
|
|
while(portalVec.size() < 100)
|
2016-01-09 10:03:40 +02:00
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
portalVec[(si32)currentIndex] = portalVec[static_cast<si32>(currentIndex % portalCount)];
|
2016-01-09 10:03:40 +02:00
|
|
|
currentIndex++;
|
|
|
|
}
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CObjectClassesHandler::getObjectName(si32 type) const
|
|
|
|
{
|
2014-06-25 20:26:47 +03:00
|
|
|
if (objects.count(type))
|
|
|
|
return objects.at(type)->name;
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Access to non existing object of type %d", type);
|
2014-06-25 20:26:47 +03:00
|
|
|
return "";
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
2014-06-30 00:16:45 +03:00
|
|
|
std::string CObjectClassesHandler::getObjectName(si32 type, si32 subtype) const
|
|
|
|
{
|
|
|
|
if (knownSubObjects(type).count(subtype))
|
|
|
|
{
|
|
|
|
auto name = getHandlerFor(type, subtype)->getCustomName();
|
|
|
|
if (name)
|
|
|
|
return name.get();
|
|
|
|
}
|
|
|
|
return getObjectName(type);
|
|
|
|
}
|
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
SObjectSounds CObjectClassesHandler::getObjectSounds(si32 type) const
|
|
|
|
{
|
|
|
|
if(objects.count(type))
|
|
|
|
return objects.at(type)->sounds;
|
|
|
|
logGlobal->error("Access to non existing object of type %d", type);
|
|
|
|
return SObjectSounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
SObjectSounds CObjectClassesHandler::getObjectSounds(si32 type, si32 subtype) const
|
|
|
|
{
|
|
|
|
if(knownSubObjects(type).count(subtype))
|
|
|
|
return getHandlerFor(type, subtype)->getSounds();
|
|
|
|
else
|
|
|
|
return getObjectSounds(type);
|
|
|
|
}
|
|
|
|
|
2014-07-30 19:07:30 +03:00
|
|
|
std::string CObjectClassesHandler::getObjectHandlerName(si32 type) const
|
|
|
|
{
|
|
|
|
return objects.at(type)->handlerName;
|
|
|
|
}
|
|
|
|
|
2018-07-28 00:43:56 +02:00
|
|
|
boost::optional<si32> CObjectClassesHandler::getObjGroupAiValue(si32 primaryID) const
|
|
|
|
{
|
|
|
|
return objects.at(primaryID)->groupDefaultAiValue;
|
|
|
|
}
|
|
|
|
|
2016-11-27 21:07:01 +02:00
|
|
|
AObjectTypeHandler::AObjectTypeHandler():
|
|
|
|
type(-1), subtype(-1)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AObjectTypeHandler::~AObjectTypeHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
void AObjectTypeHandler::setType(si32 type, si32 subtype)
|
|
|
|
{
|
|
|
|
this->type = type;
|
|
|
|
this->subtype = subtype;
|
|
|
|
}
|
|
|
|
|
2015-11-14 15:50:29 +02:00
|
|
|
void AObjectTypeHandler::setTypeName(std::string type, std::string subtype)
|
|
|
|
{
|
|
|
|
this->typeName = type;
|
|
|
|
this->subTypeName = subtype;
|
|
|
|
}
|
|
|
|
|
2014-06-05 23:51:24 +03:00
|
|
|
static ui32 loadJsonOrMax(const JsonNode & input)
|
|
|
|
{
|
|
|
|
if (input.isNull())
|
|
|
|
return std::numeric_limits<ui32>::max();
|
|
|
|
else
|
2020-10-01 10:38:06 +02:00
|
|
|
return static_cast<ui32>(input.Float());
|
2014-06-05 23:51:24 +03:00
|
|
|
}
|
|
|
|
|
2014-06-30 00:16:45 +03:00
|
|
|
void AObjectTypeHandler::init(const JsonNode & input, boost::optional<std::string> name)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
|
|
|
base = input["base"];
|
2014-06-05 23:51:24 +03:00
|
|
|
|
|
|
|
if (!input["rmg"].isNull())
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
rmgInfo.value = static_cast<ui32>(input["rmg"]["value"].Float());
|
2014-06-05 23:51:24 +03:00
|
|
|
rmgInfo.mapLimit = loadJsonOrMax(input["rmg"]["mapLimit"]);
|
|
|
|
rmgInfo.zoneLimit = loadJsonOrMax(input["rmg"]["zoneLimit"]);
|
2020-10-01 10:38:06 +02:00
|
|
|
rmgInfo.rarity = static_cast<ui32>(input["rmg"]["rarity"].Float());
|
2014-06-05 23:51:24 +03:00
|
|
|
} // else block is not needed - set in constructor
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
for (auto entry : input["templates"].Struct())
|
|
|
|
{
|
2022-09-21 13:43:57 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
entry.second.setType(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
JsonUtils::inherit(entry.second, base);
|
|
|
|
|
|
|
|
auto tmpl = new ObjectTemplate;
|
|
|
|
tmpl->id = Obj(type);
|
|
|
|
tmpl->subid = subtype;
|
|
|
|
tmpl->stringID = entry.first; // FIXME: create "fullID" - type.object.template?
|
|
|
|
tmpl->readJson(entry.second);
|
|
|
|
templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
logGlobal->warn("Failed to load terrains for object %s: %s", entry.first, e.what());
|
|
|
|
}
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
2014-06-30 00:16:45 +03:00
|
|
|
|
|
|
|
if (input["name"].isNull())
|
|
|
|
objectName = name;
|
|
|
|
else
|
|
|
|
objectName.reset(input["name"].String());
|
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
for(const JsonNode & node : input["sounds"]["ambient"].Vector())
|
|
|
|
sounds.ambient.push_back(node.String());
|
|
|
|
|
|
|
|
for(const JsonNode & node : input["sounds"]["visit"].Vector())
|
|
|
|
sounds.visit.push_back(node.String());
|
|
|
|
|
|
|
|
for(const JsonNode & node : input["sounds"]["removal"].Vector())
|
|
|
|
sounds.removal.push_back(node.String());
|
|
|
|
|
2018-07-28 00:43:56 +02:00
|
|
|
if(input["aiValue"].isNull())
|
|
|
|
aiValue = boost::none;
|
|
|
|
else
|
2020-10-07 14:12:32 +02:00
|
|
|
aiValue = static_cast<boost::optional<si32>>(input["aiValue"].Integer());
|
2018-07-28 00:43:56 +02:00
|
|
|
|
2022-06-28 10:05:30 +02:00
|
|
|
if(input["battleground"].getType() == JsonNode::JsonType::DATA_STRING)
|
|
|
|
battlefield = input["battleground"].String();
|
2022-06-26 09:21:05 +02:00
|
|
|
else
|
2022-06-28 10:05:30 +02:00
|
|
|
battlefield = boost::none;
|
2022-06-26 09:21:05 +02:00
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
initTypeData(input);
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
bool AObjectTypeHandler::objectFilter(const CGObjectInstance *, std::shared_ptr<const ObjectTemplate>) const
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2014-06-05 23:51:24 +03:00
|
|
|
return false; // by default there are no overrides
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
2015-11-14 15:50:29 +02:00
|
|
|
void AObjectTypeHandler::preInitObject(CGObjectInstance * obj) const
|
|
|
|
{
|
|
|
|
obj->ID = Obj(type);
|
|
|
|
obj->subID = subtype;
|
|
|
|
obj->typeName = typeName;
|
|
|
|
obj->subTypeName = subTypeName;
|
|
|
|
}
|
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
void AObjectTypeHandler::initTypeData(const JsonNode & input)
|
|
|
|
{
|
2014-06-30 00:16:45 +03:00
|
|
|
// empty implementation for overrides
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<std::string> AObjectTypeHandler::getCustomName() const
|
|
|
|
{
|
|
|
|
return objectName;
|
2014-06-14 18:42:13 +03:00
|
|
|
}
|
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
SObjectSounds AObjectTypeHandler::getSounds() const
|
|
|
|
{
|
|
|
|
return sounds;
|
|
|
|
}
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
void AObjectTypeHandler::addTemplate(std::shared_ptr<const ObjectTemplate> templ)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2022-09-11 15:12:35 +02:00
|
|
|
//Otherwise the template remains constant
|
|
|
|
auto ptr = const_cast<ObjectTemplate*>(templ.get());
|
|
|
|
ptr->id = Obj(type);
|
|
|
|
ptr->subid = subtype;
|
2014-06-05 19:57:43 +03:00
|
|
|
templates.push_back(templ);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AObjectTypeHandler::addTemplate(JsonNode config)
|
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null
|
2014-06-05 19:57:43 +03:00
|
|
|
JsonUtils::inherit(config, base);
|
2022-09-11 15:12:35 +02:00
|
|
|
auto tmpl = new ObjectTemplate;
|
|
|
|
tmpl->id = Obj(type);
|
|
|
|
tmpl->subid = subtype;
|
|
|
|
tmpl->stringID = ""; // TODO?
|
|
|
|
tmpl->readJson(config);
|
|
|
|
templates.emplace_back(tmpl);
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTemplates() const
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
|
|
|
return templates;
|
|
|
|
}
|
|
|
|
|
2022-06-26 09:21:05 +02:00
|
|
|
BattleField AObjectTypeHandler::getBattlefield() const
|
|
|
|
{
|
2022-06-28 10:05:30 +02:00
|
|
|
return battlefield ? BattleField::fromString(battlefield.get()) : BattleField::NONE;
|
2022-06-26 09:21:05 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 09:33:56 +02:00
|
|
|
std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TTerrainId terrainType) const
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2022-09-11 15:12:35 +02:00
|
|
|
std::vector<std::shared_ptr<const ObjectTemplate>> templates = getTemplates();
|
|
|
|
std::vector<std::shared_ptr<const ObjectTemplate>> filtered;
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
std::copy_if(templates.begin(), templates.end(), std::back_inserter(filtered), [&](std::shared_ptr<const ObjectTemplate> obj)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2022-09-11 15:12:35 +02:00
|
|
|
return obj->canBePlacedAt(terrainType);
|
2014-06-05 19:57:43 +03:00
|
|
|
});
|
2014-06-17 14:57:47 +03:00
|
|
|
// H3 defines allowed terrains in a weird way - artifacts, monsters and resources have faulty masks here
|
|
|
|
// Perhaps we should re-define faulty templates and remove this workaround (already done for resources)
|
|
|
|
if (type == Obj::ARTIFACT || type == Obj::MONSTER)
|
|
|
|
return templates;
|
|
|
|
else
|
|
|
|
return filtered;
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
|
2022-09-25 09:33:56 +02:00
|
|
|
std::shared_ptr<const ObjectTemplate> AObjectTypeHandler::getOverride(TTerrainId terrainType, const CGObjectInstance * object) const
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
2022-09-11 15:12:35 +02:00
|
|
|
std::vector<std::shared_ptr<const ObjectTemplate>> ret = getTemplates(terrainType);
|
|
|
|
for (const auto & tmpl: ret)
|
2014-06-05 19:57:43 +03:00
|
|
|
{
|
|
|
|
if (objectFilter(object, tmpl))
|
|
|
|
return tmpl;
|
|
|
|
}
|
2022-09-11 15:12:35 +02:00
|
|
|
return std::shared_ptr<const ObjectTemplate>(); //empty
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
2014-06-05 23:51:24 +03:00
|
|
|
|
|
|
|
const RandomMapInfo & AObjectTypeHandler::getRMGInfo()
|
|
|
|
{
|
|
|
|
return rmgInfo;
|
|
|
|
}
|
2014-06-14 18:42:13 +03:00
|
|
|
|
2018-07-28 00:43:56 +02:00
|
|
|
boost::optional<si32> AObjectTypeHandler::getAiValue() const
|
|
|
|
{
|
|
|
|
return aiValue;
|
|
|
|
}
|
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
bool AObjectTypeHandler::isStaticObject()
|
|
|
|
{
|
|
|
|
return false; // most of classes are not static
|
|
|
|
}
|
|
|
|
|
|
|
|
void AObjectTypeHandler::afterLoadFinalization()
|
|
|
|
{
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|