2
0

AObjectTypeHandler.cpp 7.1 KB

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