CObjectClassesHandler.cpp 19 KB

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