AObjectTypeHandler.cpp 7.5 KB

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