CObjectClassesHandler.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 "../JsonNode.h"
  20. #include "../CSoundBase.h"
  21. #include "CRewardableConstructor.h"
  22. #include "CommonConstructors.h"
  23. #include "MapObjects.h"
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. // FIXME: move into inheritNode?
  26. static void inheritNodeWithMeta(JsonNode & descendant, const JsonNode & base)
  27. {
  28. std::string oldMeta = descendant.meta;
  29. JsonUtils::inherit(descendant, base);
  30. descendant.setMeta(oldMeta);
  31. }
  32. CObjectClassesHandler::CObjectClassesHandler()
  33. {
  34. #define SET_HANDLER_CLASS(STRING, CLASSNAME) handlerConstructors[STRING] = std::make_shared<CLASSNAME>;
  35. #define SET_HANDLER(STRING, TYPENAME) handlerConstructors[STRING] = std::make_shared<CDefaultObjectTypeHandler<TYPENAME>>
  36. // list of all known handlers, hardcoded for now since the only way to add new objects is via C++ code
  37. //Note: should be in sync with registerTypesMapObjectTypes function
  38. SET_HANDLER_CLASS("configurable", CRewardableConstructor);
  39. SET_HANDLER_CLASS("dwelling", CDwellingInstanceConstructor);
  40. SET_HANDLER_CLASS("hero", CHeroInstanceConstructor);
  41. SET_HANDLER_CLASS("town", CTownInstanceConstructor);
  42. SET_HANDLER_CLASS("bank", CBankInstanceConstructor);
  43. SET_HANDLER_CLASS("static", CObstacleConstructor);
  44. SET_HANDLER_CLASS("", CObstacleConstructor);
  45. SET_HANDLER("randomArtifact", CGArtifact);
  46. SET_HANDLER("randomHero", CGHeroInstance);
  47. SET_HANDLER("randomResource", CGResource);
  48. SET_HANDLER("randomTown", CGTownInstance);
  49. SET_HANDLER("randomMonster", CGCreature);
  50. SET_HANDLER("randomDwelling", CGDwelling);
  51. SET_HANDLER("generic", CGObjectInstance);
  52. SET_HANDLER("market", CGMarket);
  53. SET_HANDLER("cartographer", CCartographer);
  54. SET_HANDLER("artifact", CGArtifact);
  55. SET_HANDLER("blackMarket", CGBlackMarket);
  56. SET_HANDLER("boat", CGBoat);
  57. SET_HANDLER("bonusingObject", CGBonusingObject);
  58. SET_HANDLER("borderGate", CGBorderGate);
  59. SET_HANDLER("borderGuard", CGBorderGuard);
  60. SET_HANDLER("monster", CGCreature);
  61. SET_HANDLER("denOfThieves", CGDenOfthieves);
  62. SET_HANDLER("event", CGEvent);
  63. SET_HANDLER("garrison", CGGarrison);
  64. SET_HANDLER("heroPlaceholder", CGHeroPlaceholder);
  65. SET_HANDLER("keymaster", CGKeymasterTent);
  66. SET_HANDLER("lighthouse", CGLighthouse);
  67. SET_HANDLER("magi", CGMagi);
  68. SET_HANDLER("magicSpring", CGMagicSpring);
  69. SET_HANDLER("magicWell", CGMagicWell);
  70. SET_HANDLER("market", CGMarket);
  71. SET_HANDLER("mine", CGMine);
  72. SET_HANDLER("obelisk", CGObelisk);
  73. SET_HANDLER("observatory", CGObservatory);
  74. SET_HANDLER("onceVisitable", CGOnceVisitable);
  75. SET_HANDLER("pandora", CGPandoraBox);
  76. SET_HANDLER("pickable", CGPickable);
  77. SET_HANDLER("prison", CGHeroInstance);
  78. SET_HANDLER("questGuard", CGQuestGuard);
  79. SET_HANDLER("resource", CGResource);
  80. SET_HANDLER("scholar", CGScholar);
  81. SET_HANDLER("seerHut", CGSeerHut);
  82. SET_HANDLER("shipyard", CGShipyard);
  83. SET_HANDLER("shrine", CGShrine);
  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("university", CGUniversity);
  90. SET_HANDLER("oncePerHero", CGVisitableOPH);
  91. SET_HANDLER("oncePerWeek", CGVisitableOPW);
  92. SET_HANDLER("witch", CGWitchHut);
  93. SET_HANDLER("terrain", CGTerrainPatch);
  94. #undef SET_HANDLER_CLASS
  95. #undef SET_HANDLER
  96. }
  97. CObjectClassesHandler::~CObjectClassesHandler()
  98. {
  99. for(auto p : objects)
  100. delete p.second;
  101. }
  102. std::vector<JsonNode> CObjectClassesHandler::loadLegacyData(size_t dataSize)
  103. {
  104. CLegacyConfigParser parser("Data/Objects.txt");
  105. size_t totalNumber = static_cast<size_t>(parser.readNumber()); // first line contains number of objects to read and nothing else
  106. parser.endLine();
  107. for (size_t i=0; i<totalNumber; i++)
  108. {
  109. auto templ = new ObjectTemplate;
  110. templ->readTxt(parser);
  111. parser.endLine();
  112. std::pair<si32, si32> key(templ->id.num, templ->subid);
  113. legacyTemplates.insert(std::make_pair(key, std::shared_ptr<const ObjectTemplate>(templ)));
  114. }
  115. std::vector<JsonNode> ret(dataSize);// create storage for 256 objects
  116. assert(dataSize == 256);
  117. CLegacyConfigParser namesParser("Data/ObjNames.txt");
  118. for (size_t i=0; i<256; i++)
  119. {
  120. ret[i]["name"].String() = namesParser.readString();
  121. namesParser.endLine();
  122. }
  123. CLegacyConfigParser cregen1Parser("data/crgen1");
  124. do
  125. customNames[Obj::CREATURE_GENERATOR1].push_back(cregen1Parser.readString());
  126. while(cregen1Parser.endLine());
  127. CLegacyConfigParser cregen4Parser("data/crgen4");
  128. do
  129. customNames[Obj::CREATURE_GENERATOR4].push_back(cregen4Parser.readString());
  130. while(cregen4Parser.endLine());
  131. return ret;
  132. }
  133. /// selects preferred ID (or subID) for new object
  134. template<typename Map>
  135. si32 selectNextID(const JsonNode & fixedID, const Map & map, si32 fixedObjectsBound)
  136. {
  137. assert(fixedObjectsBound > 0);
  138. if(fixedID.isNull())
  139. {
  140. auto lastID = map.empty() ? 0 : map.rbegin()->first;
  141. return lastID < fixedObjectsBound ? fixedObjectsBound : lastID + 1;
  142. }
  143. auto id = static_cast<si32>(fixedID.Float());
  144. if(id >= fixedObjectsBound)
  145. logGlobal->error("Getting next ID overflowed: %d >= %d", id, fixedObjectsBound);
  146. return id;
  147. }
  148. void CObjectClassesHandler::loadObjectEntry(const std::string & identifier, const JsonNode & entry, ObjectContainter * obj, bool isSubobject)
  149. {
  150. static const si32 fixedObjectsBound = 1000; // legacy value for backward compatibilitty
  151. static const si32 fixedSubobjectsBound = 10000000; // large enough arbitrary value to avoid ID-collisions
  152. si32 usedBound = fixedObjectsBound;
  153. if(!handlerConstructors.count(obj->handlerName))
  154. {
  155. logGlobal->error("Handler with name %s was not found!", obj->handlerName);
  156. return;
  157. }
  158. const auto convertedId = VLC->modh->normalizeIdentifier(entry.meta, "core", identifier);
  159. const auto & entryIndex = entry["index"];
  160. bool useSelectNextID = !isSubobject || entryIndex.isNull();
  161. if(useSelectNextID && isSubobject)
  162. {
  163. usedBound = fixedSubobjectsBound;
  164. logGlobal->error("Subobject index is Null. convertedId = '%s' obj->id = %d", convertedId, obj->id);
  165. }
  166. si32 id = useSelectNextID ? selectNextID(entryIndex, obj->subObjects, usedBound)
  167. : (si32)entryIndex.Float();
  168. auto handler = handlerConstructors.at(obj->handlerName)();
  169. handler->setType(obj->id, id);
  170. handler->setTypeName(obj->identifier, convertedId);
  171. if (customNames.count(obj->id) && customNames.at(obj->id).size() > id)
  172. handler->init(entry, customNames.at(obj->id).at(id));
  173. else
  174. handler->init(entry);
  175. //if (handler->getTemplates().empty())
  176. {
  177. auto range = legacyTemplates.equal_range(std::make_pair(obj->id, id));
  178. for (auto & templ : boost::make_iterator_range(range.first, range.second))
  179. {
  180. handler->addTemplate(templ.second);
  181. }
  182. legacyTemplates.erase(range.first, range.second);
  183. }
  184. logGlobal->debug("Loaded object %s(%d)::%s(%d)", obj->identifier, obj->id, convertedId, id);
  185. //some mods redefine content handlers in the decoration.json in such way:
  186. //"core:sign" : { "types" : { "forgeSign" : { ...
  187. static const std::vector<std::string> breakersRMG
  188. {
  189. "hota.hota decorations:hotaPandoraBox"
  190. , "hota.hota decorations:hotaSubterreanGate"
  191. };
  192. const bool isExistingKey = obj->subObjects.count(id) > 0;
  193. const bool isBreaker = std::any_of(breakersRMG.begin(), breakersRMG.end(),
  194. [&handler](const std::string & str)
  195. {
  196. return str.compare(handler->subTypeName) == 0;
  197. });
  198. const bool passedHandler = !isExistingKey && !isBreaker;
  199. if(passedHandler)
  200. {
  201. obj->subObjects[id] = handler;
  202. obj->subIds[convertedId] = id;
  203. }
  204. else if(isExistingKey) //It's supposed that fan mods handlers are not overridden by default handlers
  205. {
  206. logGlobal->trace("Handler '%s' has not been overridden with handler '%s' in object %s(%d)::%s(%d)",
  207. obj->subObjects[id]->subTypeName, obj->handlerName, obj->identifier, obj->id, convertedId, id);
  208. }
  209. else
  210. {
  211. logGlobal->warn("Handler '%s' for object %s(%d)::%s(%d) has not been activated as RMG breaker",
  212. obj->handlerName, obj->identifier, obj->id, convertedId, id);
  213. }
  214. }
  215. CObjectClassesHandler::ObjectContainter * CObjectClassesHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name)
  216. {
  217. auto obj = new ObjectContainter();
  218. static const si32 fixedObjectsBound = 256; //Legacy value for backward compatibility
  219. obj->identifier = name;
  220. obj->name = json["name"].String();
  221. obj->handlerName = json["handler"].String();
  222. obj->base = json["base"];
  223. obj->id = selectNextID(json["index"], objects, fixedObjectsBound);
  224. if(json["defaultAiValue"].isNull())
  225. obj->groupDefaultAiValue = boost::none;
  226. else
  227. obj->groupDefaultAiValue = static_cast<boost::optional<si32>>(json["defaultAiValue"].Integer());
  228. for (auto entry : json["types"].Struct())
  229. loadObjectEntry(entry.first, entry.second, obj);
  230. return obj;
  231. }
  232. void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  233. {
  234. auto object = loadFromJson(scope, data, normalizeIdentifier(scope, "core", name));
  235. objects[object->id] = object;
  236. VLC->modh->identifiers.registerObject(scope, "object", name, object->id);
  237. }
  238. void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  239. {
  240. auto object = loadFromJson(scope, data, normalizeIdentifier(scope, "core", name));
  241. assert(objects[(si32)index] == nullptr); // ensure that this id was not loaded before
  242. objects[(si32)index] = object;
  243. VLC->modh->identifiers.registerObject(scope, "object", name, object->id);
  244. }
  245. void CObjectClassesHandler::loadSubObject(const std::string & identifier, JsonNode config, si32 ID, boost::optional<si32> subID)
  246. {
  247. static const bool isSubObject = true;
  248. config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not NULL
  249. assert(objects.count(ID));
  250. if (subID)
  251. {
  252. assert(objects.at(ID)->subObjects.count(subID.get()) == 0);
  253. assert(config["index"].isNull());
  254. config["index"].Float() = subID.get();
  255. }
  256. inheritNodeWithMeta(config, objects.at(ID)->base);
  257. loadObjectEntry(identifier, config, objects[ID], isSubObject);
  258. }
  259. void CObjectClassesHandler::removeSubObject(si32 ID, si32 subID)
  260. {
  261. assert(objects.count(ID));
  262. assert(objects.at(ID)->subObjects.count(subID));
  263. objects.at(ID)->subObjects.erase(subID); //TODO: cleanup string id map
  264. }
  265. std::vector<bool> CObjectClassesHandler::getDefaultAllowed() const
  266. {
  267. return std::vector<bool>(); //TODO?
  268. }
  269. TObjectTypeHandler CObjectClassesHandler::getHandlerFor(si32 type, si32 subtype) const
  270. {
  271. if (objects.count(type))
  272. {
  273. if (objects.at(type)->subObjects.count(subtype))
  274. return objects.at(type)->subObjects.at(subtype);
  275. }
  276. logGlobal->error("Failed to find object of type %d:%d", type, subtype);
  277. throw std::runtime_error("Object type handler not found");
  278. }
  279. TObjectTypeHandler CObjectClassesHandler::getHandlerFor(std::string type, std::string subtype) const
  280. {
  281. boost::optional<si32> id = VLC->modh->identifiers.getIdentifier("core", "object", type, false);
  282. if(id)
  283. {
  284. auto object = objects.at(id.get());
  285. if(object->subIds.count(subtype))
  286. {
  287. si32 subId = object->subIds.at(subtype);
  288. return object->subObjects.at(subId);
  289. }
  290. }
  291. logGlobal->error("Failed to find object of type %s::%s", type, subtype);
  292. throw std::runtime_error("Object type handler not found");
  293. }
  294. TObjectTypeHandler CObjectClassesHandler::getHandlerFor(CompoundMapObjectID compoundIdentifier) const
  295. {
  296. return getHandlerFor(compoundIdentifier.primaryID, compoundIdentifier.secondaryID);
  297. }
  298. std::set<si32> CObjectClassesHandler::knownObjects() const
  299. {
  300. std::set<si32> ret;
  301. for (auto entry : objects)
  302. ret.insert(entry.first);
  303. return ret;
  304. }
  305. std::set<si32> CObjectClassesHandler::knownSubObjects(si32 primaryID) const
  306. {
  307. std::set<si32> ret;
  308. if (objects.count(primaryID))
  309. {
  310. for (auto entry : objects.at(primaryID)->subObjects)
  311. ret.insert(entry.first);
  312. }
  313. return ret;
  314. }
  315. void CObjectClassesHandler::beforeValidate(JsonNode & object)
  316. {
  317. for (auto & entry : object["types"].Struct())
  318. {
  319. inheritNodeWithMeta(entry.second, object["base"]);
  320. for (auto & templ : entry.second["templates"].Struct())
  321. {
  322. inheritNodeWithMeta(templ.second, entry.second["base"]);
  323. }
  324. }
  325. }
  326. void CObjectClassesHandler::afterLoadFinalization()
  327. {
  328. for(auto entry : objects)
  329. {
  330. for(auto obj : entry.second->subObjects)
  331. {
  332. obj.second->afterLoadFinalization();
  333. if(obj.second->getTemplates().empty())
  334. logGlobal->warn("No templates found for %d:%d", entry.first, obj.first);
  335. }
  336. }
  337. //duplicate existing two-way portals to make reserve for RMG
  338. auto& portalVec = objects[Obj::MONOLITH_TWO_WAY]->subObjects;
  339. size_t portalCount = portalVec.size();
  340. size_t currentIndex = portalCount;
  341. while(portalVec.size() < 100)
  342. {
  343. portalVec[(si32)currentIndex] = portalVec[static_cast<si32>(currentIndex % portalCount)];
  344. currentIndex++;
  345. }
  346. }
  347. std::string CObjectClassesHandler::getObjectName(si32 type) const
  348. {
  349. if (objects.count(type))
  350. return objects.at(type)->name;
  351. logGlobal->error("Access to non existing object of type %d", type);
  352. return "";
  353. }
  354. std::string CObjectClassesHandler::getObjectName(si32 type, si32 subtype) const
  355. {
  356. if (knownSubObjects(type).count(subtype))
  357. {
  358. auto name = getHandlerFor(type, subtype)->getCustomName();
  359. if (name)
  360. return name.get();
  361. }
  362. return getObjectName(type);
  363. }
  364. SObjectSounds CObjectClassesHandler::getObjectSounds(si32 type) const
  365. {
  366. if(objects.count(type))
  367. return objects.at(type)->sounds;
  368. logGlobal->error("Access to non existing object of type %d", type);
  369. return SObjectSounds();
  370. }
  371. SObjectSounds CObjectClassesHandler::getObjectSounds(si32 type, si32 subtype) const
  372. {
  373. if(knownSubObjects(type).count(subtype))
  374. return getHandlerFor(type, subtype)->getSounds();
  375. else
  376. return getObjectSounds(type);
  377. }
  378. std::string CObjectClassesHandler::getObjectHandlerName(si32 type) const
  379. {
  380. return objects.at(type)->handlerName;
  381. }
  382. boost::optional<si32> CObjectClassesHandler::getObjGroupAiValue(si32 primaryID) const
  383. {
  384. return objects.at(primaryID)->groupDefaultAiValue;
  385. }
  386. AObjectTypeHandler::AObjectTypeHandler():
  387. type(-1), subtype(-1)
  388. {
  389. }
  390. AObjectTypeHandler::~AObjectTypeHandler()
  391. {
  392. }
  393. void AObjectTypeHandler::setType(si32 type, si32 subtype)
  394. {
  395. this->type = type;
  396. this->subtype = subtype;
  397. }
  398. void AObjectTypeHandler::setTypeName(std::string type, std::string subtype)
  399. {
  400. this->typeName = type;
  401. this->subTypeName = subtype;
  402. }
  403. static ui32 loadJsonOrMax(const JsonNode & input)
  404. {
  405. if (input.isNull())
  406. return std::numeric_limits<ui32>::max();
  407. else
  408. return static_cast<ui32>(input.Float());
  409. }
  410. void AObjectTypeHandler::init(const JsonNode & input, boost::optional<std::string> name)
  411. {
  412. base = input["base"];
  413. if (!input["rmg"].isNull())
  414. {
  415. rmgInfo.value = static_cast<ui32>(input["rmg"]["value"].Float());
  416. rmgInfo.mapLimit = loadJsonOrMax(input["rmg"]["mapLimit"]);
  417. rmgInfo.zoneLimit = loadJsonOrMax(input["rmg"]["zoneLimit"]);
  418. rmgInfo.rarity = static_cast<ui32>(input["rmg"]["rarity"].Float());
  419. } // else block is not needed - set in constructor
  420. for (auto entry : input["templates"].Struct())
  421. {
  422. entry.second.setType(JsonNode::JsonType::DATA_STRUCT);
  423. JsonUtils::inherit(entry.second, base);
  424. auto tmpl = new ObjectTemplate;
  425. tmpl->id = Obj(type);
  426. tmpl->subid = subtype;
  427. tmpl->stringID = entry.first; // FIXME: create "fullID" - type.object.template?
  428. try
  429. {
  430. tmpl->readJson(entry.second);
  431. templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
  432. }
  433. catch (const std::exception & e)
  434. {
  435. logGlobal->warn("Failed to load terrains for object %s: %s", entry.first, e.what());
  436. }
  437. }
  438. if (input["name"].isNull())
  439. objectName = name;
  440. else
  441. objectName.reset(input["name"].String());
  442. for(const JsonNode & node : input["sounds"]["ambient"].Vector())
  443. sounds.ambient.push_back(node.String());
  444. for(const JsonNode & node : input["sounds"]["visit"].Vector())
  445. sounds.visit.push_back(node.String());
  446. for(const JsonNode & node : input["sounds"]["removal"].Vector())
  447. sounds.removal.push_back(node.String());
  448. if(input["aiValue"].isNull())
  449. aiValue = boost::none;
  450. else
  451. aiValue = static_cast<boost::optional<si32>>(input["aiValue"].Integer());
  452. if(input["battleground"].getType() == JsonNode::JsonType::DATA_STRING)
  453. battlefield = input["battleground"].String();
  454. else
  455. battlefield = boost::none;
  456. initTypeData(input);
  457. }
  458. bool AObjectTypeHandler::objectFilter(const CGObjectInstance *, std::shared_ptr<const ObjectTemplate>) const
  459. {
  460. return false; // by default there are no overrides
  461. }
  462. void AObjectTypeHandler::preInitObject(CGObjectInstance * obj) const
  463. {
  464. obj->ID = Obj(type);
  465. obj->subID = subtype;
  466. obj->typeName = typeName;
  467. obj->subTypeName = subTypeName;
  468. }
  469. void AObjectTypeHandler::initTypeData(const JsonNode & input)
  470. {
  471. // empty implementation for overrides
  472. }
  473. boost::optional<std::string> AObjectTypeHandler::getCustomName() const
  474. {
  475. return objectName;
  476. }
  477. SObjectSounds AObjectTypeHandler::getSounds() const
  478. {
  479. return sounds;
  480. }
  481. void AObjectTypeHandler::addTemplate(std::shared_ptr<const ObjectTemplate> templ)
  482. {
  483. //Otherwise the template remains constant
  484. auto ptr = const_cast<ObjectTemplate*>(templ.get());
  485. ptr->id = Obj(type);
  486. ptr->subid = subtype;
  487. templates.push_back(templ);
  488. }
  489. void AObjectTypeHandler::addTemplate(JsonNode config)
  490. {
  491. config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null
  492. JsonUtils::inherit(config, base);
  493. auto tmpl = new ObjectTemplate;
  494. tmpl->id = Obj(type);
  495. tmpl->subid = subtype;
  496. tmpl->stringID = ""; // TODO?
  497. tmpl->readJson(config);
  498. templates.emplace_back(tmpl);
  499. }
  500. std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTemplates() const
  501. {
  502. return templates;
  503. }
  504. BattleField AObjectTypeHandler::getBattlefield() const
  505. {
  506. return battlefield ? BattleField::fromString(battlefield.get()) : BattleField::NONE;
  507. }
  508. std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TerrainId terrainType) const
  509. {
  510. std::vector<std::shared_ptr<const ObjectTemplate>> templates = getTemplates();
  511. std::vector<std::shared_ptr<const ObjectTemplate>> filtered;
  512. std::copy_if(templates.begin(), templates.end(), std::back_inserter(filtered), [&](std::shared_ptr<const ObjectTemplate> obj)
  513. {
  514. return obj->canBePlacedAt(terrainType);
  515. });
  516. // H3 defines allowed terrains in a weird way - artifacts, monsters and resources have faulty masks here
  517. // Perhaps we should re-define faulty templates and remove this workaround (already done for resources)
  518. if (type == Obj::ARTIFACT || type == Obj::MONSTER)
  519. return templates;
  520. else
  521. return filtered;
  522. }
  523. std::shared_ptr<const ObjectTemplate> AObjectTypeHandler::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
  524. {
  525. std::vector<std::shared_ptr<const ObjectTemplate>> ret = getTemplates(terrainType);
  526. for (const auto & tmpl: ret)
  527. {
  528. if (objectFilter(object, tmpl))
  529. return tmpl;
  530. }
  531. return std::shared_ptr<const ObjectTemplate>(); //empty
  532. }
  533. const RandomMapInfo & AObjectTypeHandler::getRMGInfo()
  534. {
  535. return rmgInfo;
  536. }
  537. boost::optional<si32> AObjectTypeHandler::getAiValue() const
  538. {
  539. return aiValue;
  540. }
  541. bool AObjectTypeHandler::isStaticObject()
  542. {
  543. return false; // most of classes are not static
  544. }
  545. void AObjectTypeHandler::afterLoadFinalization()
  546. {
  547. }
  548. VCMI_LIB_NAMESPACE_END