2
0

AObjectTypeHandler.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * AObjectTypeHandler.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 "AObjectTypeHandler.h"
  12. #include "IObjectInfo.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../modding/IdentifierStorage.h"
  15. #include "../VCMI_Lib.h"
  16. #include "../mapObjects/CGObjectInstance.h"
  17. #include "../mapObjects/ObjectTemplate.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. AObjectTypeHandler::AObjectTypeHandler() = default;
  20. AObjectTypeHandler::~AObjectTypeHandler()
  21. {
  22. // FIXME: currently on Android there is a weird crash in destructor of 'base' member
  23. // this code attempts to localize and fix this crash
  24. if (base)
  25. {
  26. base->clear();
  27. base.reset();
  28. }
  29. }
  30. std::string AObjectTypeHandler::getJsonKey() const
  31. {
  32. return modScope + ':' + subTypeName;
  33. }
  34. si32 AObjectTypeHandler::getIndex() const
  35. {
  36. return type;
  37. }
  38. si32 AObjectTypeHandler::getSubIndex() const
  39. {
  40. return subtype;
  41. }
  42. std::string AObjectTypeHandler::getTypeName() const
  43. {
  44. return typeName;
  45. }
  46. std::string AObjectTypeHandler::getSubTypeName() const
  47. {
  48. return subTypeName;
  49. }
  50. static ui32 loadJsonOrMax(const JsonNode & input)
  51. {
  52. if (input.isNull())
  53. return std::numeric_limits<ui32>::max();
  54. else
  55. return static_cast<ui32>(input.Float());
  56. }
  57. void AObjectTypeHandler::init(const JsonNode & input)
  58. {
  59. if (!input["base"].isNull())
  60. base = std::make_unique<JsonNode>(input["base"]);
  61. if (!input["rmg"].isNull())
  62. {
  63. rmgInfo.value = static_cast<ui32>(input["rmg"]["value"].Float());
  64. const JsonNode & mapLimit = input["rmg"]["mapLimit"];
  65. if (!mapLimit.isNull())
  66. rmgInfo.mapLimit = static_cast<ui32>(mapLimit.Float());
  67. rmgInfo.zoneLimit = loadJsonOrMax(input["rmg"]["zoneLimit"]);
  68. rmgInfo.rarity = static_cast<ui32>(input["rmg"]["rarity"].Float());
  69. } // else block is not needed - set in constructor
  70. for (auto entry : input["templates"].Struct())
  71. {
  72. entry.second.setType(JsonNode::JsonType::DATA_STRUCT);
  73. if (base)
  74. JsonUtils::inherit(entry.second, *base);
  75. auto * tmpl = new ObjectTemplate;
  76. tmpl->id = Obj(type);
  77. tmpl->subid = subtype;
  78. tmpl->stringID = entry.first; // FIXME: create "fullID" - type.object.template?
  79. tmpl->readJson(entry.second);
  80. templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
  81. }
  82. for(const JsonNode & node : input["sounds"]["ambient"].Vector())
  83. sounds.ambient.push_back(AudioPath::fromJson(node));
  84. for(const JsonNode & node : input["sounds"]["visit"].Vector())
  85. sounds.visit.push_back(AudioPath::fromJson(node));
  86. for(const JsonNode & node : input["sounds"]["removal"].Vector())
  87. sounds.removal.push_back(AudioPath::fromJson(node));
  88. if(input["aiValue"].isNull())
  89. aiValue = std::nullopt;
  90. else
  91. aiValue = static_cast<std::optional<si32>>(input["aiValue"].Integer());
  92. // TODO: Define properties, move them to actual object instance
  93. blockVisit = input["blockVisit"].Bool();
  94. removable = input["removable"].Bool();
  95. battlefield = BattleField::NONE;
  96. if(!input["battleground"].isNull())
  97. {
  98. VLC->identifiers()->requestIdentifier("battlefield", input["battleground"], [this](int32_t identifier)
  99. {
  100. battlefield = BattleField(identifier);
  101. });
  102. }
  103. initTypeData(input);
  104. }
  105. bool AObjectTypeHandler::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  106. {
  107. return false; // by default there are no overrides
  108. }
  109. void AObjectTypeHandler::preInitObject(CGObjectInstance * obj) const
  110. {
  111. obj->ID = Obj(type);
  112. obj->subID = subtype;
  113. obj->typeName = typeName;
  114. obj->subTypeName = subTypeName;
  115. obj->blockVisit = blockVisit;
  116. obj->removable = removable;
  117. }
  118. void AObjectTypeHandler::initTypeData(const JsonNode & input)
  119. {
  120. // empty implementation for overrides
  121. }
  122. bool AObjectTypeHandler::hasNameTextID() const
  123. {
  124. return false;
  125. }
  126. std::string AObjectTypeHandler::getBaseTextID() const
  127. {
  128. return TextIdentifier("mapObject", modScope, typeName, subTypeName).get();
  129. }
  130. std::string AObjectTypeHandler::getNameTextID() const
  131. {
  132. return TextIdentifier(getBaseTextID(), "name").get();
  133. }
  134. std::string AObjectTypeHandler::getNameTranslated() const
  135. {
  136. return VLC->generaltexth->translate(getNameTextID());
  137. }
  138. SObjectSounds AObjectTypeHandler::getSounds() const
  139. {
  140. return sounds;
  141. }
  142. void AObjectTypeHandler::clearTemplates()
  143. {
  144. templates.clear();
  145. }
  146. void AObjectTypeHandler::addTemplate(const std::shared_ptr<const ObjectTemplate> & templ)
  147. {
  148. templates.push_back(templ);
  149. }
  150. void AObjectTypeHandler::addTemplate(JsonNode config)
  151. {
  152. config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null
  153. if (base)
  154. JsonUtils::inherit(config, *base);
  155. auto * tmpl = new ObjectTemplate;
  156. tmpl->id = Obj(type);
  157. tmpl->subid = subtype;
  158. tmpl->stringID.clear(); // TODO?
  159. tmpl->readJson(config);
  160. templates.emplace_back(tmpl);
  161. }
  162. std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTemplates() const
  163. {
  164. return templates;
  165. }
  166. BattleField AObjectTypeHandler::getBattlefield() const
  167. {
  168. return battlefield;
  169. }
  170. std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TerrainId terrainType) const
  171. {
  172. std::vector<std::shared_ptr<const ObjectTemplate>> templates = getTemplates();
  173. std::vector<std::shared_ptr<const ObjectTemplate>> filtered;
  174. const auto cfun = [&](const std::shared_ptr<const ObjectTemplate> & obj)
  175. {
  176. return obj->canBePlacedAt(terrainType);
  177. };
  178. std::copy_if(templates.begin(), templates.end(), std::back_inserter(filtered), cfun);
  179. // H3 defines allowed terrains in a weird way - artifacts, monsters and resources have faulty masks here
  180. // Perhaps we should re-define faulty templates and remove this workaround (already done for resources)
  181. if (type == Obj::ARTIFACT || type == Obj::MONSTER)
  182. return templates;
  183. else
  184. return filtered;
  185. }
  186. std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getMostSpecificTemplates(TerrainId terrainType) const
  187. {
  188. auto templates = getTemplates(terrainType);
  189. if (!templates.empty())
  190. {
  191. //Get terrain-specific template if possible
  192. int leastTerrains = (*boost::min_element(templates, [](const std::shared_ptr<const ObjectTemplate> & tmp1, const std::shared_ptr<const ObjectTemplate> & tmp2)
  193. {
  194. return tmp1->getAllowedTerrains().size() < tmp2->getAllowedTerrains().size();
  195. }))->getAllowedTerrains().size();
  196. vstd::erase_if(templates, [leastTerrains](const std::shared_ptr<const ObjectTemplate> & tmp)
  197. {
  198. return tmp->getAllowedTerrains().size() > leastTerrains;
  199. });
  200. }
  201. return templates;
  202. }
  203. std::shared_ptr<const ObjectTemplate> AObjectTypeHandler::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
  204. {
  205. std::vector<std::shared_ptr<const ObjectTemplate>> ret = getTemplates(terrainType);
  206. for (const auto & tmpl: ret)
  207. {
  208. if (objectFilter(object, tmpl))
  209. return tmpl;
  210. }
  211. return std::shared_ptr<const ObjectTemplate>(); //empty
  212. }
  213. const RandomMapInfo & AObjectTypeHandler::getRMGInfo()
  214. {
  215. return rmgInfo;
  216. }
  217. std::optional<si32> AObjectTypeHandler::getAiValue() const
  218. {
  219. return aiValue;
  220. }
  221. bool AObjectTypeHandler::isStaticObject()
  222. {
  223. return false; // most of classes are not static
  224. }
  225. void AObjectTypeHandler::afterLoadFinalization()
  226. {
  227. }
  228. std::unique_ptr<IObjectInfo> AObjectTypeHandler::getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const
  229. {
  230. return nullptr;
  231. }
  232. VCMI_LIB_NAMESPACE_END