CObjectClassesHandler.cpp 20 KB

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