AObjectTypeHandler.cpp 7.1 KB

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