CObjectClassesHandler.cpp 21 KB

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