AObjectTypeHandler.cpp 7.0 KB

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