CObjectClassesHandler.cpp 19 KB

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