CObjectClassesHandler.cpp 21 KB

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