AObjectTypeHandler.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. battlefield = BattleField::NONE;
  92. if(!input["battleground"].isNull())
  93. {
  94. LIBRARY->identifiers()->requestIdentifier("battlefield", input["battleground"], [this](int32_t identifier)
  95. {
  96. battlefield = BattleField(identifier);
  97. });
  98. }
  99. initTypeData(input);
  100. }
  101. bool AObjectTypeHandler::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  102. {
  103. return false; // by default there are no overrides
  104. }
  105. void AObjectTypeHandler::preInitObject(CGObjectInstance * obj) const
  106. {
  107. obj->ID = Obj(type);
  108. obj->subID = subtype;
  109. obj->blockVisit = blockVisit;
  110. obj->removable = removable;
  111. }
  112. void AObjectTypeHandler::initTypeData(const JsonNode & input)
  113. {
  114. // empty implementation for overrides
  115. }
  116. bool AObjectTypeHandler::hasNameTextID() const
  117. {
  118. return false;
  119. }
  120. std::string AObjectTypeHandler::getBaseTextID() const
  121. {
  122. return TextIdentifier("mapObject", modScope, typeName, subTypeName).get();
  123. }
  124. std::string AObjectTypeHandler::getNameTextID() const
  125. {
  126. return TextIdentifier(getBaseTextID(), "name").get();
  127. }
  128. std::string AObjectTypeHandler::getNameTranslated() const
  129. {
  130. return LIBRARY->generaltexth->translate(getNameTextID());
  131. }
  132. SObjectSounds AObjectTypeHandler::getSounds() const
  133. {
  134. return sounds;
  135. }
  136. void AObjectTypeHandler::clearTemplates()
  137. {
  138. templates.clear();
  139. }
  140. void AObjectTypeHandler::addTemplate(const std::shared_ptr<const ObjectTemplate> & templ)
  141. {
  142. templates.push_back(templ);
  143. }
  144. void AObjectTypeHandler::addTemplate(JsonNode config)
  145. {
  146. config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null
  147. if (base)
  148. JsonUtils::inherit(config, *base);
  149. auto tmpl = std::make_shared<ObjectTemplate>();
  150. tmpl->id = Obj(type);
  151. tmpl->subid = subtype;
  152. tmpl->stringID.clear(); // TODO?
  153. tmpl->readJson(config);
  154. templates.push_back(tmpl);
  155. }
  156. std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTemplates() const
  157. {
  158. return templates;
  159. }
  160. BattleField AObjectTypeHandler::getBattlefield() const
  161. {
  162. return battlefield;
  163. }
  164. std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TerrainId terrainType) const
  165. {
  166. std::vector<std::shared_ptr<const ObjectTemplate>> templates = getTemplates();
  167. std::vector<std::shared_ptr<const ObjectTemplate>> filtered;
  168. const auto cfun = [&](const std::shared_ptr<const ObjectTemplate> & obj)
  169. {
  170. return obj->canBePlacedAt(terrainType);
  171. };
  172. std::copy_if(templates.begin(), templates.end(), std::back_inserter(filtered), cfun);
  173. // H3 defines allowed terrains in a weird way - artifacts, monsters and resources have faulty masks here
  174. // Perhaps we should re-define faulty templates and remove this workaround (already done for resources)
  175. if (type == Obj::ARTIFACT || type == Obj::MONSTER)
  176. return templates;
  177. else
  178. return filtered;
  179. }
  180. std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getMostSpecificTemplates(TerrainId terrainType) const
  181. {
  182. auto templates = getTemplates(terrainType);
  183. if (!templates.empty())
  184. {
  185. //Get terrain-specific template if possible
  186. int leastTerrains = (*std::min_element(templates.begin(), templates.end(), [](const std::shared_ptr<const ObjectTemplate> & tmp1, const std::shared_ptr<const ObjectTemplate> & tmp2)
  187. {
  188. return tmp1->getAllowedTerrains().size() < tmp2->getAllowedTerrains().size();
  189. }))->getAllowedTerrains().size();
  190. vstd::erase_if(templates, [leastTerrains](const std::shared_ptr<const ObjectTemplate> & tmp)
  191. {
  192. return tmp->getAllowedTerrains().size() > leastTerrains;
  193. });
  194. }
  195. return templates;
  196. }
  197. std::shared_ptr<const ObjectTemplate> AObjectTypeHandler::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
  198. {
  199. std::vector<std::shared_ptr<const ObjectTemplate>> ret = getTemplates(terrainType);
  200. for (const auto & tmpl: ret)
  201. {
  202. if (objectFilter(object, tmpl))
  203. return tmpl;
  204. }
  205. return std::shared_ptr<const ObjectTemplate>(); //empty
  206. }
  207. const RandomMapInfo & AObjectTypeHandler::getRMGInfo()
  208. {
  209. return rmgInfo;
  210. }
  211. std::optional<si32> AObjectTypeHandler::getAiValue() const
  212. {
  213. return aiValue;
  214. }
  215. bool AObjectTypeHandler::isStaticObject()
  216. {
  217. return false; // most of classes are not static
  218. }
  219. void AObjectTypeHandler::afterLoadFinalization()
  220. {
  221. }
  222. std::unique_ptr<IObjectInfo> AObjectTypeHandler::getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const
  223. {
  224. return nullptr;
  225. }
  226. VCMI_LIB_NAMESPACE_END