2
0

CObjectClassesHandler.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * CObjectClassesHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CConfigHandler.h"
  12. #include "CObjectClassesHandler.h"
  13. #include "../filesystem/Filesystem.h"
  14. #include "../filesystem/CBinaryReader.h"
  15. #include "../json/JsonUtils.h"
  16. #include "../GameLibrary.h"
  17. #include "../GameConstants.h"
  18. #include "../constants/StringConstants.h"
  19. #include "../IGameSettings.h"
  20. #include "../CSoundBase.h"
  21. #include "../mapObjectConstructors/CRewardableConstructor.h"
  22. #include "../mapObjectConstructors/CommonConstructors.h"
  23. #include "../mapObjectConstructors/DwellingInstanceConstructor.h"
  24. #include "../mapObjectConstructors/FlaggableInstanceConstructor.h"
  25. #include "../mapObjectConstructors/HillFortInstanceConstructor.h"
  26. #include "../mapObjectConstructors/MarketInstanceConstructor.h"
  27. #include "../mapObjectConstructors/ShipyardInstanceConstructor.h"
  28. #include "../mapObjects/CGCreature.h"
  29. #include "../mapObjects/CGHeroInstance.h"
  30. #include "../mapObjects/CGMarket.h"
  31. #include "../mapObjects/CGPandoraBox.h"
  32. #include "../mapObjects/CGTownInstance.h"
  33. #include "../mapObjects/CQuest.h"
  34. #include "../mapObjects/FlaggableMapObject.h"
  35. #include "../mapObjects/MiscObjects.h"
  36. #include "../mapObjects/ObjectTemplate.h"
  37. #include "../mapObjects/ObstacleSetHandler.h"
  38. #include "../modding/IdentifierStorage.h"
  39. #include "../modding/CModHandler.h"
  40. #include "../modding/ModScope.h"
  41. #include "../texts/CGeneralTextHandler.h"
  42. #include "../texts/CLegacyConfigParser.h"
  43. #include <vstd/StringUtils.h>
  44. VCMI_LIB_NAMESPACE_BEGIN
  45. CObjectClassesHandler::CObjectClassesHandler()
  46. {
  47. #define SET_HANDLER_CLASS(STRING, CLASSNAME) handlerConstructors[STRING] = std::make_shared<CLASSNAME>
  48. #define SET_HANDLER(STRING, TYPENAME) handlerConstructors[STRING] = std::make_shared<CDefaultObjectTypeHandler<TYPENAME>>
  49. // list of all known handlers, hardcoded for now since the only way to add new objects is via C++ code
  50. //Note: should be in sync with registerTypesMapObjectTypes function
  51. SET_HANDLER_CLASS("configurable", CRewardableConstructor);
  52. SET_HANDLER_CLASS("dwelling", DwellingInstanceConstructor);
  53. SET_HANDLER_CLASS("hero", CHeroInstanceConstructor);
  54. SET_HANDLER_CLASS("town", CTownInstanceConstructor);
  55. SET_HANDLER_CLASS("boat", BoatInstanceConstructor);
  56. SET_HANDLER_CLASS("flaggable", FlaggableInstanceConstructor);
  57. SET_HANDLER_CLASS("market", MarketInstanceConstructor);
  58. SET_HANDLER_CLASS("hillFort", HillFortInstanceConstructor);
  59. SET_HANDLER_CLASS("shipyard", ShipyardInstanceConstructor);
  60. SET_HANDLER_CLASS("monster", CreatureInstanceConstructor);
  61. SET_HANDLER_CLASS("resource", ResourceInstanceConstructor);
  62. SET_HANDLER_CLASS("mine", MineInstanceConstructor);
  63. SET_HANDLER_CLASS("static", CObstacleConstructor);
  64. SET_HANDLER_CLASS("", CObstacleConstructor);
  65. SET_HANDLER("randomArtifact", CGArtifact);
  66. SET_HANDLER("randomHero", CGHeroInstance);
  67. SET_HANDLER("randomResource", CGResource);
  68. SET_HANDLER("randomTown", CGTownInstance);
  69. SET_HANDLER("randomMonster", CGCreature);
  70. SET_HANDLER("randomDwelling", CGDwelling);
  71. SET_HANDLER("generic", CGObjectInstance);
  72. SET_HANDLER("artifact", CGArtifact);
  73. SET_HANDLER("borderGate", CGBorderGate);
  74. SET_HANDLER("borderGuard", CGBorderGuard);
  75. SET_HANDLER("denOfThieves", CGDenOfthieves);
  76. SET_HANDLER("event", CGEvent);
  77. SET_HANDLER("garrison", CGGarrison);
  78. SET_HANDLER("heroPlaceholder", CGHeroPlaceholder);
  79. SET_HANDLER("keymaster", CGKeymasterTent);
  80. SET_HANDLER("magi", CGMagi);
  81. SET_HANDLER("obelisk", CGObelisk);
  82. SET_HANDLER("pandora", CGPandoraBox);
  83. SET_HANDLER("prison", CGHeroInstance);
  84. SET_HANDLER("questGuard", CGQuestGuard);
  85. SET_HANDLER("seerHut", CGSeerHut);
  86. SET_HANDLER("sign", CGSignBottle);
  87. SET_HANDLER("siren", CGSirens);
  88. SET_HANDLER("monolith", CGMonolith);
  89. SET_HANDLER("subterraneanGate", CGSubterraneanGate);
  90. SET_HANDLER("whirlpool", CGWhirlpool);
  91. SET_HANDLER("terrain", CGTerrainPatch);
  92. #undef SET_HANDLER_CLASS
  93. #undef SET_HANDLER
  94. }
  95. CObjectClassesHandler::~CObjectClassesHandler() = default;
  96. std::vector<JsonNode> CObjectClassesHandler::loadLegacyData()
  97. {
  98. size_t dataSize = LIBRARY->engineSettings()->getInteger(EGameSettings::TEXTS_OBJECT);
  99. CLegacyConfigParser parser(TextPath::builtin("Data/Objects.txt"));
  100. auto totalNumber = static_cast<size_t>(parser.readNumber()); // first line contains number of objects to read and nothing else
  101. parser.endLine();
  102. for (size_t i = 0; i < totalNumber; i++)
  103. {
  104. auto tmpl = std::make_shared<ObjectTemplate>();
  105. tmpl->readTxt(parser);
  106. parser.endLine();
  107. std::pair key(tmpl->id, tmpl->subid);
  108. legacyTemplates.insert(std::make_pair(key, tmpl));
  109. }
  110. mapObjectTypes.resize(256);
  111. std::vector<JsonNode> ret(dataSize);// create storage for 256 objects
  112. assert(dataSize == 256);
  113. CLegacyConfigParser namesParser(TextPath::builtin("Data/ObjNames.txt"));
  114. for (size_t i=0; i<256; i++)
  115. {
  116. ret[i]["name"].String() = namesParser.readString();
  117. namesParser.endLine();
  118. }
  119. JsonNode cregen1;
  120. JsonNode cregen4;
  121. CLegacyConfigParser cregen1Parser(TextPath::builtin("data/crgen1"));
  122. do
  123. {
  124. JsonNode subObject;
  125. subObject["name"].String() = cregen1Parser.readString();
  126. cregen1.Vector().push_back(subObject);
  127. }
  128. while(cregen1Parser.endLine());
  129. CLegacyConfigParser cregen4Parser(TextPath::builtin("data/crgen4"));
  130. do
  131. {
  132. JsonNode subObject;
  133. subObject["name"].String() = cregen4Parser.readString();
  134. cregen4.Vector().push_back(subObject);
  135. }
  136. while(cregen4Parser.endLine());
  137. ret[Obj::CREATURE_GENERATOR1]["subObjects"] = cregen1;
  138. ret[Obj::CREATURE_GENERATOR4]["subObjects"] = cregen4;
  139. ret[Obj::REFUGEE_CAMP]["subObjects"].Vector().push_back(ret[Obj::REFUGEE_CAMP]);
  140. ret[Obj::WAR_MACHINE_FACTORY]["subObjects"].Vector().push_back(ret[Obj::WAR_MACHINE_FACTORY]);
  141. return ret;
  142. }
  143. void CObjectClassesHandler::loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * baseObject)
  144. {
  145. auto subObject = loadSubObjectFromJson(scope, identifier, entry, baseObject, baseObject->objectTypeHandlers.size());
  146. assert(subObject);
  147. baseObject->objectTypeHandlers.push_back(subObject);
  148. registerObject(scope, baseObject->getJsonKey(), subObject->getSubTypeName(), subObject->subtype);
  149. for(const auto & compatID : entry["compatibilityIdentifiers"].Vector())
  150. {
  151. if (identifier != compatID.String())
  152. registerObject(scope, baseObject->getJsonKey(), compatID.String(), subObject->subtype);
  153. else
  154. logMod->warn("Mod '%s' map object '%s': compatibility identifier has same name as object itself!", scope, identifier);
  155. }
  156. }
  157. void CObjectClassesHandler::loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * baseObject, size_t index)
  158. {
  159. auto subObject = loadSubObjectFromJson(scope, identifier, entry, baseObject, index);
  160. assert(subObject);
  161. if (baseObject->objectTypeHandlers.at(index) != nullptr)
  162. throw std::runtime_error("Attempt to load already loaded object:" + identifier);
  163. baseObject->objectTypeHandlers.at(index) = subObject;
  164. registerObject(scope, baseObject->getJsonKey(), subObject->getSubTypeName(), subObject->subtype);
  165. for(const auto & compatID : entry["compatibilityIdentifiers"].Vector())
  166. {
  167. if (identifier != compatID.String())
  168. registerObject(scope, baseObject->getJsonKey(), compatID.String(), subObject->subtype);
  169. else
  170. logMod->warn("Mod '%s' map object '%s': compatibility identifier has same name as object itself!");
  171. }
  172. }
  173. TObjectTypeHandler CObjectClassesHandler::loadSubObjectFromJson(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * baseObject, size_t index)
  174. {
  175. assert(!scope.empty());
  176. if (settings["mods"]["validation"].String() != "off")
  177. {
  178. size_t separator = identifier.find(':');
  179. if (separator != std::string::npos)
  180. {
  181. std::string modName = identifier.substr(0, separator);
  182. std::string objectName = identifier.substr(separator + 1);
  183. logMod->warn("Mod %s: Map object type with format '%s' will add new map object, not modify it! Please use '%s' form and add dependency on mod '%s' instead!", scope, identifier, modName, identifier );
  184. }
  185. }
  186. std::string handler = baseObject->handlerName;
  187. if(!handlerConstructors.count(handler))
  188. {
  189. logMod->error("Handler with name %s was not found!", handler);
  190. // workaround for potential crash - if handler does not exists, continue with generic handler that is used for objects without any custom logc
  191. handler = "generic";
  192. assert(handlerConstructors.count(handler) != 0);
  193. }
  194. auto createdObject = handlerConstructors.at(handler)();
  195. createdObject->modScope = scope;
  196. createdObject->typeName = baseObject->identifier;
  197. createdObject->subTypeName = identifier;
  198. createdObject->type = baseObject->id;
  199. createdObject->subtype = index;
  200. createdObject->init(entry);
  201. bool staticObject = createdObject->isStaticObject();
  202. if (staticObject)
  203. {
  204. for (auto & templ : createdObject->getTemplates())
  205. {
  206. // Register templates for new objects from mods
  207. LIBRARY->biomeHandler->addTemplate(scope, templ->stringID, templ);
  208. }
  209. }
  210. auto range = legacyTemplates.equal_range(std::make_pair(baseObject->id, index));
  211. for (auto & templ : boost::make_iterator_range(range.first, range.second))
  212. {
  213. if (staticObject)
  214. {
  215. // Register legacy templates as "core"
  216. // FIXME: Why does it clear stringID?
  217. LIBRARY->biomeHandler->addTemplate("core", templ.second->stringID, templ.second);
  218. }
  219. createdObject->addTemplate(templ.second);
  220. }
  221. legacyTemplates.erase(range.first, range.second);
  222. logGlobal->debug("Loaded object %s(%d)::%s(%d)", baseObject->getJsonKey(), baseObject->id, identifier, index);
  223. return createdObject;
  224. }
  225. ObjectClass::ObjectClass() = default;
  226. ObjectClass::~ObjectClass() = default;
  227. std::string ObjectClass::getJsonKey() const
  228. {
  229. return modScope + ':' + identifier;
  230. }
  231. std::string ObjectClass::getNameTextID() const
  232. {
  233. return TextIdentifier("object", modScope, identifier, "name").get();
  234. }
  235. std::string ObjectClass::getNameTranslated() const
  236. {
  237. return LIBRARY->generaltexth->translate(getNameTextID());
  238. }
  239. std::unique_ptr<ObjectClass> CObjectClassesHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index)
  240. {
  241. auto newObject = std::make_unique<ObjectClass>();
  242. newObject->modScope = scope;
  243. newObject->identifier = name;
  244. newObject->handlerName = json["handler"].String();
  245. newObject->base = json["base"];
  246. newObject->id = index;
  247. LIBRARY->generaltexth->registerString(scope, newObject->getNameTextID(), json["name"]);
  248. newObject->objectTypeHandlers.resize(json["lastReservedIndex"].Float() + 1);
  249. for (auto subData : json["types"].Struct())
  250. {
  251. if (!subData.second["index"].isNull())
  252. {
  253. const std::string & subMeta = subData.second["index"].getModScope();
  254. if ( subMeta == "core")
  255. {
  256. size_t subIndex = subData.second["index"].Integer();
  257. loadSubObject(subData.second.getModScope(), subData.first, subData.second, newObject.get(), subIndex);
  258. }
  259. else
  260. {
  261. logMod->error("Object %s:%s.%s - attempt to load object with preset index! This option is reserved for built-in mod", subMeta, name, subData.first );
  262. loadSubObject(subData.second.getModScope(), subData.first, subData.second, newObject.get());
  263. }
  264. }
  265. else
  266. loadSubObject(subData.second.getModScope(), subData.first, subData.second, newObject.get());
  267. }
  268. if (newObject->id == MapObjectID::MONOLITH_TWO_WAY)
  269. generateExtraMonolithsForRMG(newObject.get());
  270. return newObject;
  271. }
  272. void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  273. {
  274. mapObjectTypes.push_back(loadFromJson(scope, data, name, mapObjectTypes.size()));
  275. LIBRARY->identifiersHandler->registerObject(scope, "object", name, mapObjectTypes.back()->id);
  276. }
  277. void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  278. {
  279. assert(mapObjectTypes.at(index) == nullptr); // ensure that this id was not loaded before
  280. mapObjectTypes.at(index) = loadFromJson(scope, data, name, index);
  281. LIBRARY->identifiersHandler->registerObject(scope, "object", name, mapObjectTypes.at(index)->id);
  282. }
  283. void CObjectClassesHandler::loadSubObject(const std::string & identifier, JsonNode config, MapObjectID ID, MapObjectSubID subID)
  284. {
  285. config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not NULL
  286. assert(mapObjectTypes.at(ID.getNum()));
  287. if (subID.getNum() >= mapObjectTypes.at(ID.getNum())->objectTypeHandlers.size())
  288. {
  289. mapObjectTypes.at(ID.getNum())->objectTypeHandlers.resize(subID.getNum() + 1);
  290. }
  291. JsonUtils::inherit(config, mapObjectTypes.at(ID.getNum())->base);
  292. for (auto & templ : config["templates"].Struct())
  293. JsonUtils::inherit(templ.second, config["base"]);
  294. if (settings["mods"]["validation"].String() != "off")
  295. JsonUtils::validate(config, "vcmi:objectType", identifier);
  296. loadSubObject(config.getModScope(), identifier, config, mapObjectTypes.at(ID.getNum()).get(), subID.getNum());
  297. }
  298. void CObjectClassesHandler::removeSubObject(MapObjectID ID, MapObjectSubID subID)
  299. {
  300. assert(mapObjectTypes.at(ID.getNum()));
  301. mapObjectTypes.at(ID.getNum())->objectTypeHandlers.at(subID.getNum()) = nullptr;
  302. }
  303. TObjectTypeHandler CObjectClassesHandler::getHandlerFor(MapObjectID type, MapObjectSubID subtype) const
  304. {
  305. try
  306. {
  307. if (mapObjectTypes.at(type.getNum()) == nullptr)
  308. return mapObjectTypes.front()->objectTypeHandlers.front();
  309. auto subID = subtype.getNum();
  310. if (type == Obj::PRISON || type == Obj::HERO_PLACEHOLDER || type == Obj::SPELL_SCROLL)
  311. subID = 0;
  312. auto result = mapObjectTypes.at(type.getNum())->objectTypeHandlers.at(subID);
  313. if (result != nullptr)
  314. return result;
  315. }
  316. catch (std::out_of_range & e)
  317. {
  318. // Leave catch block silently and handle error in block outside of try ... catch - all valid values should use 'return' in try block
  319. }
  320. std::string errorString = "Failed to find object of type " + std::to_string(type.getNum()) + "::" + std::to_string(subtype.getNum());
  321. logGlobal->error(errorString);
  322. throw std::out_of_range(errorString);
  323. }
  324. TObjectTypeHandler CObjectClassesHandler::getHandlerFor(const std::string & scope, const std::string & type, const std::string & subtype) const
  325. {
  326. std::optional<si32> id = LIBRARY->identifiers()->getIdentifier(scope, "object", type);
  327. if(id)
  328. {
  329. const auto & object = mapObjectTypes.at(id.value());
  330. std::optional<si32> subID = LIBRARY->identifiers()->getIdentifier(scope, object->getJsonKey(), subtype);
  331. if (subID)
  332. return object->objectTypeHandlers.at(subID.value());
  333. }
  334. std::string errorString = "Failed to find object of type " + type + "::" + subtype;
  335. logGlobal->error(errorString);
  336. throw std::runtime_error(errorString);
  337. }
  338. TObjectTypeHandler CObjectClassesHandler::getHandlerFor(CompoundMapObjectID compoundIdentifier) const
  339. {
  340. return getHandlerFor(compoundIdentifier.primaryID, compoundIdentifier.secondaryID);
  341. }
  342. CompoundMapObjectID CObjectClassesHandler::getCompoundIdentifier(const std::string & scope, const std::string & type, const std::string & subtype) const
  343. {
  344. std::optional<si32> id;
  345. if (scope.empty())
  346. {
  347. id = LIBRARY->identifiers()->getIdentifier("object", type);
  348. }
  349. else
  350. {
  351. id = LIBRARY->identifiers()->getIdentifier(scope, "object", type);
  352. }
  353. if(id)
  354. {
  355. if (subtype.empty())
  356. return CompoundMapObjectID(id.value(), 0);
  357. const auto & object = mapObjectTypes.at(id.value());
  358. std::optional<si32> subID = LIBRARY->identifiers()->getIdentifier(scope, object->getJsonKey(), subtype);
  359. if (subID)
  360. return CompoundMapObjectID(id.value(), subID.value());
  361. }
  362. std::string errorString = "Failed to get id for object of type " + type + "." + subtype;
  363. logGlobal->error(errorString);
  364. throw std::runtime_error(errorString);
  365. }
  366. CompoundMapObjectID CObjectClassesHandler::getCompoundIdentifier(const std::string & objectName) const
  367. {
  368. std::string subtype = "object"; //Default for objects with no subIds
  369. std::string type;
  370. auto scopeAndFullName = vstd::splitStringToPair(objectName, ':');
  371. logGlobal->debug("scopeAndFullName: %s, %s", scopeAndFullName.first, scopeAndFullName.second);
  372. auto typeAndName = vstd::splitStringToPair(scopeAndFullName.second, '.');
  373. logGlobal->debug("typeAndName: %s, %s", typeAndName.first, typeAndName.second);
  374. auto nameAndSubtype = vstd::splitStringToPair(typeAndName.second, '.');
  375. logGlobal->debug("nameAndSubtype: %s, %s", nameAndSubtype.first, nameAndSubtype.second);
  376. if (!nameAndSubtype.first.empty())
  377. {
  378. type = nameAndSubtype.first;
  379. subtype = nameAndSubtype.second;
  380. }
  381. else
  382. {
  383. type = typeAndName.second;
  384. }
  385. return getCompoundIdentifier(boost::to_lower_copy(scopeAndFullName.first), type, subtype);
  386. }
  387. std::set<MapObjectID> CObjectClassesHandler::knownObjects() const
  388. {
  389. std::set<MapObjectID> ret;
  390. for(auto & entry : mapObjectTypes)
  391. if (entry)
  392. ret.insert(entry->id);
  393. return ret;
  394. }
  395. std::set<MapObjectSubID> CObjectClassesHandler::knownSubObjects(MapObjectID primaryID) const
  396. {
  397. std::set<MapObjectSubID> ret;
  398. if (!mapObjectTypes.at(primaryID.getNum()))
  399. {
  400. logGlobal->error("Failed to find object %d", primaryID);
  401. return ret;
  402. }
  403. for(const auto & entry : mapObjectTypes.at(primaryID.getNum())->objectTypeHandlers)
  404. if (entry)
  405. ret.insert(entry->subtype);
  406. return ret;
  407. }
  408. void CObjectClassesHandler::beforeValidate(JsonNode & object)
  409. {
  410. for (auto & entry : object["types"].Struct())
  411. {
  412. if (object.Struct().count("subObjects"))
  413. {
  414. const auto & vector = object["subObjects"].Vector();
  415. if (entry.second.Struct().count("index"))
  416. {
  417. size_t index = entry.second["index"].Integer();
  418. if (index < vector.size())
  419. JsonUtils::inherit(entry.second, vector[index]);
  420. }
  421. }
  422. JsonUtils::inherit(entry.second, object["base"]);
  423. for (auto & templ : entry.second["templates"].Struct())
  424. JsonUtils::inherit(templ.second, entry.second["base"]);
  425. }
  426. object.Struct().erase("subObjects");
  427. }
  428. void CObjectClassesHandler::afterLoadFinalization()
  429. {
  430. for(auto & entry : mapObjectTypes)
  431. {
  432. if (!entry)
  433. continue;
  434. for(const auto & obj : entry->objectTypeHandlers)
  435. {
  436. if (!obj)
  437. continue;
  438. obj->afterLoadFinalization();
  439. if(obj->getTemplates().empty())
  440. logMod->debug("No templates found for %s:%s", entry->getJsonKey(), obj->getJsonKey());
  441. }
  442. }
  443. for(auto & entry : objectIdHandlers)
  444. {
  445. // Call function for each object id
  446. entry.second(entry.first);
  447. }
  448. }
  449. void CObjectClassesHandler::resolveObjectCompoundId(const std::string & id, std::function<void(CompoundMapObjectID)> callback)
  450. {
  451. auto compoundId = getCompoundIdentifier(id);
  452. objectIdHandlers.push_back(std::make_pair(compoundId, callback));
  453. }
  454. void CObjectClassesHandler::generateExtraMonolithsForRMG(ObjectClass * container)
  455. {
  456. //duplicate existing two-way portals to make reserve for RMG
  457. auto& portalVec = container->objectTypeHandlers;
  458. //FIXME: Monoliths in this vector can be already not useful for every terrain
  459. const size_t portalCount = portalVec.size();
  460. //Invalid portals will be skipped and portalVec size stays unchanged
  461. for (size_t i = portalCount; portalVec.size() < 100; ++i)
  462. {
  463. auto index = static_cast<si32>(i % portalCount);
  464. auto portal = portalVec[index];
  465. auto templates = portal->getTemplates();
  466. if (templates.empty() || !templates[0]->canBePlacedAtAnyTerrain())
  467. {
  468. continue; //Do not clone HoTA water-only portals or any others we can't use
  469. }
  470. //deep copy of noncopyable object :?
  471. auto newPortal = std::make_shared<CDefaultObjectTypeHandler<CGMonolith>>();
  472. newPortal->rmgInfo = portal->getRMGInfo();
  473. newPortal->templates = portal->getTemplates();
  474. newPortal->sounds = portal->getSounds();
  475. newPortal->aiValue = portal->getAiValue();
  476. newPortal->battlefield = portal->battlefield; //getter is not initialized at this point
  477. newPortal->modScope = portal->modScope; //private
  478. newPortal->typeName = portal->getTypeName();
  479. newPortal->subTypeName = std::string("monolith") + std::to_string(portalVec.size());
  480. newPortal->type = portal->getIndex();
  481. // Inconsintent original indexing: monolith1 has index 0
  482. newPortal->subtype = portalVec.size() - 1; //indexes must be unique, they are returned as a set
  483. newPortal->blockVisit = portal->blockVisit;
  484. newPortal->removable = portal->removable;
  485. portalVec.push_back(newPortal);
  486. registerObject(newPortal->modScope, container->getJsonKey(), newPortal->subTypeName, newPortal->subtype);
  487. }
  488. }
  489. std::string CObjectClassesHandler::getObjectName(MapObjectID type, MapObjectSubID subtype) const
  490. {
  491. const auto handler = getHandlerFor(type, subtype);
  492. if (handler && handler->hasNameTextID())
  493. return handler->getNameTranslated();
  494. if (mapObjectTypes.at(type.getNum()))
  495. return mapObjectTypes.at(type.getNum())->getNameTranslated();
  496. return mapObjectTypes.front()->getNameTranslated();
  497. }
  498. SObjectSounds CObjectClassesHandler::getObjectSounds(MapObjectID type, MapObjectSubID subtype) const
  499. {
  500. // TODO: these objects may have subID's that does not have associated handler:
  501. // Prison: uses hero type as subID
  502. // Hero: uses hero type as subID, but registers hero classes as subtypes
  503. // Spell scroll: uses spell ID as subID
  504. if(type == Obj::PRISON || type == Obj::HERO || type == Obj::SPELL_SCROLL)
  505. subtype = 0;
  506. if(mapObjectTypes.at(type.getNum()))
  507. return getHandlerFor(type, subtype)->getSounds();
  508. else
  509. return mapObjectTypes.front()->objectTypeHandlers.front()->getSounds();
  510. }
  511. std::string CObjectClassesHandler::getObjectHandlerName(MapObjectID type) const
  512. {
  513. if (mapObjectTypes.at(type.getNum()))
  514. return mapObjectTypes.at(type.getNum())->handlerName;
  515. else
  516. return mapObjectTypes.front()->handlerName;
  517. }
  518. std::string CObjectClassesHandler::getJsonKey(MapObjectID type) const
  519. {
  520. if (mapObjectTypes.at(type.getNum()) != nullptr)
  521. return mapObjectTypes.at(type.getNum())->getJsonKey();
  522. logGlobal->warn("Unknown object of type %d!", type);
  523. return mapObjectTypes.front()->getJsonKey();
  524. }
  525. VCMI_LIB_NAMESPACE_END