CObjectClassesHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 "../VCMI_Lib.h"
  15. #include "../GameConstants.h"
  16. #include "../StringConstants.h"
  17. #include "../CGeneralTextHandler.h"
  18. #include "../CModHandler.h"
  19. #include "../GameSettings.h"
  20. #include "../JsonNode.h"
  21. #include "../CSoundBase.h"
  22. #include "CRewardableConstructor.h"
  23. #include "CommonConstructors.h"
  24. #include "MapObjects.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. CObjectClassesHandler::CObjectClassesHandler()
  27. {
  28. #define SET_HANDLER_CLASS(STRING, CLASSNAME) handlerConstructors[STRING] = std::make_shared<CLASSNAME>;
  29. #define SET_HANDLER(STRING, TYPENAME) handlerConstructors[STRING] = std::make_shared<CDefaultObjectTypeHandler<TYPENAME>>
  30. // list of all known handlers, hardcoded for now since the only way to add new objects is via C++ code
  31. //Note: should be in sync with registerTypesMapObjectTypes function
  32. SET_HANDLER_CLASS("configurable", CRewardableConstructor);
  33. SET_HANDLER_CLASS("dwelling", CDwellingInstanceConstructor);
  34. SET_HANDLER_CLASS("hero", CHeroInstanceConstructor);
  35. SET_HANDLER_CLASS("town", CTownInstanceConstructor);
  36. SET_HANDLER_CLASS("bank", CBankInstanceConstructor);
  37. SET_HANDLER_CLASS("static", CObstacleConstructor);
  38. SET_HANDLER_CLASS("", CObstacleConstructor);
  39. SET_HANDLER("randomArtifact", CGArtifact);
  40. SET_HANDLER("randomHero", CGHeroInstance);
  41. SET_HANDLER("randomResource", CGResource);
  42. SET_HANDLER("randomTown", CGTownInstance);
  43. SET_HANDLER("randomMonster", CGCreature);
  44. SET_HANDLER("randomDwelling", CGDwelling);
  45. SET_HANDLER("generic", CGObjectInstance);
  46. SET_HANDLER("cartographer", CCartographer);
  47. SET_HANDLER("artifact", CGArtifact);
  48. SET_HANDLER("blackMarket", CGBlackMarket);
  49. SET_HANDLER("boat", CGBoat);
  50. SET_HANDLER("borderGate", CGBorderGate);
  51. SET_HANDLER("borderGuard", CGBorderGuard);
  52. SET_HANDLER("monster", CGCreature);
  53. SET_HANDLER("denOfThieves", CGDenOfthieves);
  54. SET_HANDLER("event", CGEvent);
  55. SET_HANDLER("garrison", CGGarrison);
  56. SET_HANDLER("heroPlaceholder", CGHeroPlaceholder);
  57. SET_HANDLER("keymaster", CGKeymasterTent);
  58. SET_HANDLER("lighthouse", CGLighthouse);
  59. SET_HANDLER("magi", CGMagi);
  60. SET_HANDLER("market", CGMarket);
  61. SET_HANDLER("mine", CGMine);
  62. SET_HANDLER("obelisk", CGObelisk);
  63. SET_HANDLER("observatory", CGObservatory);
  64. SET_HANDLER("pandora", CGPandoraBox);
  65. SET_HANDLER("prison", CGHeroInstance);
  66. SET_HANDLER("questGuard", CGQuestGuard);
  67. SET_HANDLER("resource", CGResource);
  68. SET_HANDLER("scholar", CGScholar);
  69. SET_HANDLER("seerHut", CGSeerHut);
  70. SET_HANDLER("shipyard", CGShipyard);
  71. SET_HANDLER("shrine", CGShrine);
  72. SET_HANDLER("sign", CGSignBottle);
  73. SET_HANDLER("siren", CGSirens);
  74. SET_HANDLER("monolith", CGMonolith);
  75. SET_HANDLER("subterraneanGate", CGSubterraneanGate);
  76. SET_HANDLER("whirlpool", CGWhirlpool);
  77. SET_HANDLER("university", CGUniversity);
  78. SET_HANDLER("witch", CGWitchHut);
  79. SET_HANDLER("terrain", CGTerrainPatch);
  80. #undef SET_HANDLER_CLASS
  81. #undef SET_HANDLER
  82. }
  83. CObjectClassesHandler::~CObjectClassesHandler()
  84. {
  85. for(auto * p : objects)
  86. delete p;
  87. }
  88. std::vector<JsonNode> CObjectClassesHandler::loadLegacyData()
  89. {
  90. size_t dataSize = VLC->settings()->getInteger(EGameSettings::TEXTS_OBJECT);
  91. CLegacyConfigParser parser("Data/Objects.txt");
  92. auto totalNumber = static_cast<size_t>(parser.readNumber()); // first line contains number of objects to read and nothing else
  93. parser.endLine();
  94. for (size_t i = 0; i < totalNumber; i++)
  95. {
  96. auto * tmpl = new ObjectTemplate;
  97. tmpl->readTxt(parser);
  98. parser.endLine();
  99. std::pair<si32, si32> key(tmpl->id.num, tmpl->subid);
  100. legacyTemplates.insert(std::make_pair(key, std::shared_ptr<const ObjectTemplate>(tmpl)));
  101. }
  102. objects.resize(256);
  103. std::vector<JsonNode> ret(dataSize);// create storage for 256 objects
  104. assert(dataSize == 256);
  105. CLegacyConfigParser namesParser("Data/ObjNames.txt");
  106. for (size_t i=0; i<256; i++)
  107. {
  108. ret[i]["name"].String() = namesParser.readString();
  109. namesParser.endLine();
  110. }
  111. JsonNode cregen1;
  112. JsonNode cregen4;
  113. CLegacyConfigParser cregen1Parser("data/crgen1");
  114. do
  115. {
  116. JsonNode subObject;
  117. subObject["name"].String() = cregen1Parser.readString();
  118. cregen1.Vector().push_back(subObject);
  119. }
  120. while(cregen1Parser.endLine());
  121. CLegacyConfigParser cregen4Parser("data/crgen4");
  122. do
  123. {
  124. JsonNode subObject;
  125. subObject["name"].String() = cregen4Parser.readString();
  126. cregen4.Vector().push_back(subObject);
  127. }
  128. while(cregen4Parser.endLine());
  129. ret[Obj::CREATURE_GENERATOR1]["subObjects"] = cregen1;
  130. ret[Obj::CREATURE_GENERATOR4]["subObjects"] = cregen4;
  131. ret[Obj::REFUGEE_CAMP]["subObjects"].Vector().push_back(ret[Obj::REFUGEE_CAMP]);
  132. ret[Obj::WAR_MACHINE_FACTORY]["subObjects"].Vector().push_back(ret[Obj::WAR_MACHINE_FACTORY]);
  133. return ret;
  134. }
  135. void CObjectClassesHandler::loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj)
  136. {
  137. auto object = loadSubObjectFromJson(scope, identifier, entry, obj, obj->objects.size());
  138. assert(object);
  139. obj->objects.push_back(object);
  140. registerObject(scope, obj->getJsonKey(), object->getSubTypeName(), object->subtype);
  141. for(const auto & compatID : entry["compatibilityIdentifiers"].Vector())
  142. registerObject(scope, obj->getJsonKey(), compatID.String(), object->subtype);
  143. }
  144. void CObjectClassesHandler::loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index)
  145. {
  146. auto object = loadSubObjectFromJson(scope, identifier, entry, obj, index);
  147. assert(object);
  148. assert(obj->objects[index] == nullptr); // ensure that this id was not loaded before
  149. obj->objects[index] = object;
  150. registerObject(scope, obj->getJsonKey(), object->getSubTypeName(), object->subtype);
  151. for(const auto & compatID : entry["compatibilityIdentifiers"].Vector())
  152. registerObject(scope, obj->getJsonKey(), compatID.String(), object->subtype);
  153. }
  154. TObjectTypeHandler CObjectClassesHandler::loadSubObjectFromJson(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index)
  155. {
  156. assert(identifier.find(':') == std::string::npos);
  157. assert(!scope.empty());
  158. if(!handlerConstructors.count(obj->handlerName))
  159. {
  160. logGlobal->error("Handler with name %s was not found!", obj->handlerName);
  161. return nullptr;
  162. }
  163. auto createdObject = handlerConstructors.at(obj->handlerName)();
  164. createdObject->modScope = scope;
  165. createdObject->typeName = obj->identifier;;
  166. createdObject->subTypeName = identifier;
  167. createdObject->type = obj->id;
  168. createdObject->subtype = index;
  169. createdObject->init(entry);
  170. auto range = legacyTemplates.equal_range(std::make_pair(obj->id, index));
  171. for (auto & templ : boost::make_iterator_range(range.first, range.second))
  172. {
  173. createdObject->addTemplate(templ.second);
  174. }
  175. legacyTemplates.erase(range.first, range.second);
  176. logGlobal->debug("Loaded object %s(%d)::%s(%d)", obj->getJsonKey(), obj->id, identifier, index);
  177. return createdObject;
  178. }
  179. std::string ObjectClass::getJsonKey() const
  180. {
  181. return modScope + ':' + identifier;
  182. }
  183. std::string ObjectClass::getNameTextID() const
  184. {
  185. return TextIdentifier("object", modScope, identifier, "name").get();
  186. }
  187. std::string ObjectClass::getNameTranslated() const
  188. {
  189. return VLC->generaltexth->translate(getNameTextID());
  190. }
  191. ObjectClass * CObjectClassesHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index)
  192. {
  193. auto * obj = new ObjectClass();
  194. obj->modScope = scope;
  195. obj->identifier = name;
  196. obj->handlerName = json["handler"].String();
  197. obj->base = json["base"];
  198. obj->id = index;
  199. VLC->generaltexth->registerString(scope, obj->getNameTextID(), json["name"].String());
  200. obj->objects.resize(json["lastReservedIndex"].Float() + 1);
  201. for (auto subData : json["types"].Struct())
  202. {
  203. if (!subData.second["index"].isNull())
  204. {
  205. const std::string & subMeta = subData.second["index"].meta;
  206. if ( subMeta != "core")
  207. logMod->warn("Object %s:%s.%s - attempt to load object with preset index! This option is reserved for built-in mod", subMeta, name, subData.first );
  208. size_t subIndex = subData.second["index"].Integer();
  209. loadSubObject(subData.second.meta, subData.first, subData.second, obj, subIndex);
  210. }
  211. else
  212. loadSubObject(subData.second.meta, subData.first, subData.second, obj);
  213. }
  214. return obj;
  215. }
  216. void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  217. {
  218. auto * object = loadFromJson(scope, data, name, objects.size());
  219. objects.push_back(object);
  220. VLC->modh->identifiers.registerObject(scope, "object", name, object->id);
  221. }
  222. void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  223. {
  224. auto * object = loadFromJson(scope, data, name, index);
  225. assert(objects[(si32)index] == nullptr); // ensure that this id was not loaded before
  226. objects[static_cast<si32>(index)] = object;
  227. VLC->modh->identifiers.registerObject(scope, "object", name, object->id);
  228. }
  229. void CObjectClassesHandler::loadSubObject(const std::string & identifier, JsonNode config, si32 ID, si32 subID)
  230. {
  231. config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not NULL
  232. assert(ID < objects.size());
  233. assert(objects[ID]);
  234. if ( subID >= objects[ID]->objects.size())
  235. objects[ID]->objects.resize(subID+1);
  236. JsonUtils::inherit(config, objects.at(ID)->base);
  237. loadSubObject(config.meta, identifier, config, objects[ID], subID);
  238. }
  239. void CObjectClassesHandler::removeSubObject(si32 ID, si32 subID)
  240. {
  241. assert(ID < objects.size());
  242. assert(objects[ID]);
  243. assert(subID < objects[ID]->objects.size());
  244. objects[ID]->objects[subID] = nullptr;
  245. }
  246. std::vector<bool> CObjectClassesHandler::getDefaultAllowed() const
  247. {
  248. return std::vector<bool>(); //TODO?
  249. }
  250. TObjectTypeHandler CObjectClassesHandler::getHandlerFor(si32 type, si32 subtype) const
  251. {
  252. assert(type < objects.size());
  253. assert(objects[type]);
  254. assert(subtype < objects[type]->objects.size());
  255. return objects.at(type)->objects.at(subtype);
  256. }
  257. TObjectTypeHandler CObjectClassesHandler::getHandlerFor(const std::string & scope, const std::string & type, const std::string & subtype) const
  258. {
  259. std::optional<si32> id = VLC->modh->identifiers.getIdentifier(scope, "object", type);
  260. if(id)
  261. {
  262. auto * object = objects[id.value()];
  263. std::optional<si32> subID = VLC->modh->identifiers.getIdentifier(scope, object->getJsonKey(), subtype);
  264. if (subID)
  265. return object->objects[subID.value()];
  266. }
  267. std::string errorString = "Failed to find object of type " + type + "::" + subtype;
  268. logGlobal->error(errorString);
  269. throw std::runtime_error(errorString);
  270. }
  271. TObjectTypeHandler CObjectClassesHandler::getHandlerFor(CompoundMapObjectID compoundIdentifier) const
  272. {
  273. return getHandlerFor(compoundIdentifier.primaryID, compoundIdentifier.secondaryID);
  274. }
  275. std::set<si32> CObjectClassesHandler::knownObjects() const
  276. {
  277. std::set<si32> ret;
  278. for(auto * entry : objects)
  279. if (entry)
  280. ret.insert(entry->id);
  281. return ret;
  282. }
  283. std::set<si32> CObjectClassesHandler::knownSubObjects(si32 primaryID) const
  284. {
  285. std::set<si32> ret;
  286. if (!objects.at(primaryID))
  287. {
  288. logGlobal->error("Failed to find object %d", primaryID);
  289. return ret;
  290. }
  291. for(const auto & entry : objects.at(primaryID)->objects)
  292. if (entry)
  293. ret.insert(entry->subtype);
  294. return ret;
  295. }
  296. void CObjectClassesHandler::beforeValidate(JsonNode & object)
  297. {
  298. for (auto & entry : object["types"].Struct())
  299. {
  300. if (object.Struct().count("subObjects"))
  301. {
  302. const auto & vector = object["subObjects"].Vector();
  303. if (entry.second.Struct().count("index"))
  304. {
  305. size_t index = entry.second["index"].Integer();
  306. if (index < vector.size())
  307. JsonUtils::inherit(entry.second, vector[index]);
  308. }
  309. }
  310. JsonUtils::inherit(entry.second, object["base"]);
  311. for (auto & templ : entry.second["templates"].Struct())
  312. JsonUtils::inherit(templ.second, entry.second["base"]);
  313. }
  314. object.Struct().erase("subObjects");
  315. }
  316. void CObjectClassesHandler::afterLoadFinalization()
  317. {
  318. for(auto * entry : objects)
  319. {
  320. if (!entry)
  321. continue;
  322. for(const auto & obj : entry->objects)
  323. {
  324. if (!obj)
  325. continue;
  326. obj->afterLoadFinalization();
  327. if(obj->getTemplates().empty())
  328. logGlobal->warn("No templates found for %s:%s", entry->getJsonKey(), obj->getJsonKey());
  329. }
  330. }
  331. generateExtraMonolithsForRMG();
  332. }
  333. void CObjectClassesHandler::generateExtraMonolithsForRMG()
  334. {
  335. //duplicate existing two-way portals to make reserve for RMG
  336. auto& portalVec = objects[Obj::MONOLITH_TWO_WAY]->objects;
  337. //FIXME: Monoliths in this vector can be already not useful for every terrain
  338. const size_t portalCount = portalVec.size();
  339. //Invalid portals will be skipped and portalVec size stays unchanged
  340. for (size_t i = portalCount; portalVec.size() < 100; ++i)
  341. {
  342. auto index = static_cast<si32>(i % portalCount);
  343. auto portal = portalVec[index];
  344. auto templates = portal->getTemplates();
  345. if (templates.empty() || !templates[0]->canBePlacedAtAnyTerrain())
  346. {
  347. continue; //Do not clone HoTA water-only portals or any others we can't use
  348. }
  349. //deep copy of noncopyable object :?
  350. auto newPortal = std::make_shared<CDefaultObjectTypeHandler<CGMonolith>>();
  351. newPortal->rmgInfo = portal->getRMGInfo();
  352. newPortal->base = portal->base; //not needed?
  353. newPortal->templates = portal->getTemplates();
  354. newPortal->sounds = portal->getSounds();
  355. newPortal->aiValue = portal->getAiValue();
  356. newPortal->battlefield = portal->battlefield; //getter is not initialized at this point
  357. newPortal->modScope = portal->modScope; //private
  358. newPortal->typeName = portal->getTypeName();
  359. newPortal->subTypeName = std::string("monolith") + std::to_string(portalVec.size());
  360. newPortal->type = portal->getIndex();
  361. newPortal->subtype = portalVec.size(); //indexes must be unique, they are returned as a set
  362. portalVec.push_back(newPortal);
  363. }
  364. }
  365. std::string CObjectClassesHandler::getObjectName(si32 type, si32 subtype) const
  366. {
  367. const auto handler = getHandlerFor(type, subtype);
  368. if (handler && handler->hasNameTextID())
  369. return handler->getNameTranslated();
  370. else
  371. return objects[type]->getNameTranslated();
  372. }
  373. SObjectSounds CObjectClassesHandler::getObjectSounds(si32 type, si32 subtype) const
  374. {
  375. // TODO: these objects may have subID's that does not have associated handler:
  376. // Prison: uses hero type as subID
  377. // Hero: uses hero type as subID, but registers hero classes as subtypes
  378. // Spell scroll: uses spell ID as subID
  379. if(type == Obj::PRISON || type == Obj::HERO || type == Obj::SPELL_SCROLL)
  380. subtype = 0;
  381. assert(type < objects.size());
  382. assert(objects[type]);
  383. assert(subtype < objects[type]->objects.size());
  384. return getHandlerFor(type, subtype)->getSounds();
  385. }
  386. std::string CObjectClassesHandler::getObjectHandlerName(si32 type) const
  387. {
  388. return objects.at(type)->handlerName;
  389. }
  390. std::string AObjectTypeHandler::getJsonKey() const
  391. {
  392. return modScope + ':' + subTypeName;
  393. }
  394. si32 AObjectTypeHandler::getIndex() const
  395. {
  396. return type;
  397. }
  398. si32 AObjectTypeHandler::getSubIndex() const
  399. {
  400. return subtype;
  401. }
  402. std::string AObjectTypeHandler::getTypeName() const
  403. {
  404. return typeName;
  405. }
  406. std::string AObjectTypeHandler::getSubTypeName() const
  407. {
  408. return subTypeName;
  409. }
  410. static ui32 loadJsonOrMax(const JsonNode & input)
  411. {
  412. if (input.isNull())
  413. return std::numeric_limits<ui32>::max();
  414. else
  415. return static_cast<ui32>(input.Float());
  416. }
  417. void AObjectTypeHandler::init(const JsonNode & input)
  418. {
  419. base = input["base"];
  420. if (!input["rmg"].isNull())
  421. {
  422. rmgInfo.value = static_cast<ui32>(input["rmg"]["value"].Float());
  423. const JsonNode & mapLimit = input["rmg"]["mapLimit"];
  424. if (!mapLimit.isNull())
  425. rmgInfo.mapLimit = static_cast<ui32>(mapLimit.Float());
  426. rmgInfo.zoneLimit = loadJsonOrMax(input["rmg"]["zoneLimit"]);
  427. rmgInfo.rarity = static_cast<ui32>(input["rmg"]["rarity"].Float());
  428. } // else block is not needed - set in constructor
  429. for (auto entry : input["templates"].Struct())
  430. {
  431. entry.second.setType(JsonNode::JsonType::DATA_STRUCT);
  432. JsonUtils::inherit(entry.second, base);
  433. auto * tmpl = new ObjectTemplate;
  434. tmpl->id = Obj(type);
  435. tmpl->subid = subtype;
  436. tmpl->stringID = entry.first; // FIXME: create "fullID" - type.object.template?
  437. try
  438. {
  439. tmpl->readJson(entry.second);
  440. templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
  441. }
  442. catch (const std::exception & e)
  443. {
  444. logGlobal->warn("Failed to load terrains for object %s: %s", entry.first, e.what());
  445. }
  446. }
  447. for(const JsonNode & node : input["sounds"]["ambient"].Vector())
  448. sounds.ambient.push_back(node.String());
  449. for(const JsonNode & node : input["sounds"]["visit"].Vector())
  450. sounds.visit.push_back(node.String());
  451. for(const JsonNode & node : input["sounds"]["removal"].Vector())
  452. sounds.removal.push_back(node.String());
  453. if(input["aiValue"].isNull())
  454. aiValue = std::nullopt;
  455. else
  456. aiValue = static_cast<std::optional<si32>>(input["aiValue"].Integer());
  457. if(input["battleground"].getType() == JsonNode::JsonType::DATA_STRING)
  458. battlefield = input["battleground"].String();
  459. else
  460. battlefield = std::nullopt;
  461. initTypeData(input);
  462. }
  463. bool AObjectTypeHandler::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  464. {
  465. return false; // by default there are no overrides
  466. }
  467. void AObjectTypeHandler::preInitObject(CGObjectInstance * obj) const
  468. {
  469. obj->ID = Obj(type);
  470. obj->subID = subtype;
  471. obj->typeName = typeName;
  472. obj->subTypeName = subTypeName;
  473. }
  474. void AObjectTypeHandler::initTypeData(const JsonNode & input)
  475. {
  476. // empty implementation for overrides
  477. }
  478. bool AObjectTypeHandler::hasNameTextID() const
  479. {
  480. return false;
  481. }
  482. std::string AObjectTypeHandler::getNameTextID() const
  483. {
  484. return TextIdentifier("mapObject", modScope, typeName, subTypeName, "name").get();
  485. }
  486. std::string AObjectTypeHandler::getNameTranslated() const
  487. {
  488. return VLC->generaltexth->translate(getNameTextID());
  489. }
  490. SObjectSounds AObjectTypeHandler::getSounds() const
  491. {
  492. return sounds;
  493. }
  494. void AObjectTypeHandler::addTemplate(const std::shared_ptr<const ObjectTemplate> & templ)
  495. {
  496. templates.push_back(templ);
  497. }
  498. void AObjectTypeHandler::addTemplate(JsonNode config)
  499. {
  500. config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null
  501. JsonUtils::inherit(config, base);
  502. auto * tmpl = new ObjectTemplate;
  503. tmpl->id = Obj(type);
  504. tmpl->subid = subtype;
  505. tmpl->stringID.clear(); // TODO?
  506. tmpl->readJson(config);
  507. templates.emplace_back(tmpl);
  508. }
  509. std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTemplates() const
  510. {
  511. return templates;
  512. }
  513. BattleField AObjectTypeHandler::getBattlefield() const
  514. {
  515. return battlefield ? BattleField::fromString(battlefield.value()) : BattleField::NONE;
  516. }
  517. std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TerrainId terrainType) const
  518. {
  519. std::vector<std::shared_ptr<const ObjectTemplate>> templates = getTemplates();
  520. std::vector<std::shared_ptr<const ObjectTemplate>> filtered;
  521. const auto cfun = [&](const std::shared_ptr<const ObjectTemplate> & obj)
  522. {
  523. return obj->canBePlacedAt(terrainType);
  524. };
  525. std::copy_if(templates.begin(), templates.end(), std::back_inserter(filtered), cfun);
  526. // H3 defines allowed terrains in a weird way - artifacts, monsters and resources have faulty masks here
  527. // Perhaps we should re-define faulty templates and remove this workaround (already done for resources)
  528. if (type == Obj::ARTIFACT || type == Obj::MONSTER)
  529. return templates;
  530. else
  531. return filtered;
  532. }
  533. std::shared_ptr<const ObjectTemplate> AObjectTypeHandler::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
  534. {
  535. std::vector<std::shared_ptr<const ObjectTemplate>> ret = getTemplates(terrainType);
  536. for (const auto & tmpl: ret)
  537. {
  538. if (objectFilter(object, tmpl))
  539. return tmpl;
  540. }
  541. return std::shared_ptr<const ObjectTemplate>(); //empty
  542. }
  543. const RandomMapInfo & AObjectTypeHandler::getRMGInfo()
  544. {
  545. return rmgInfo;
  546. }
  547. std::optional<si32> AObjectTypeHandler::getAiValue() const
  548. {
  549. return aiValue;
  550. }
  551. bool AObjectTypeHandler::isStaticObject()
  552. {
  553. return false; // most of classes are not static
  554. }
  555. void AObjectTypeHandler::afterLoadFinalization()
  556. {
  557. }
  558. VCMI_LIB_NAMESPACE_END