AObjectTypeHandler.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "../CGeneralTextHandler.h"
  13. #include "../VCMI_Lib.h"
  14. #include "../mapObjects/CGObjectInstance.h"
  15. #include "../mapObjects/ObjectTemplate.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. std::string AObjectTypeHandler::getJsonKey() const
  18. {
  19. return modScope + ':' + subTypeName;
  20. }
  21. si32 AObjectTypeHandler::getIndex() const
  22. {
  23. return type;
  24. }
  25. si32 AObjectTypeHandler::getSubIndex() const
  26. {
  27. return subtype;
  28. }
  29. std::string AObjectTypeHandler::getTypeName() const
  30. {
  31. return typeName;
  32. }
  33. std::string AObjectTypeHandler::getSubTypeName() const
  34. {
  35. return subTypeName;
  36. }
  37. static ui32 loadJsonOrMax(const JsonNode & input)
  38. {
  39. if (input.isNull())
  40. return std::numeric_limits<ui32>::max();
  41. else
  42. return static_cast<ui32>(input.Float());
  43. }
  44. void AObjectTypeHandler::init(const JsonNode & input)
  45. {
  46. base = input["base"];
  47. if (!input["rmg"].isNull())
  48. {
  49. rmgInfo.value = static_cast<ui32>(input["rmg"]["value"].Float());
  50. const JsonNode & mapLimit = input["rmg"]["mapLimit"];
  51. if (!mapLimit.isNull())
  52. rmgInfo.mapLimit = static_cast<ui32>(mapLimit.Float());
  53. rmgInfo.zoneLimit = loadJsonOrMax(input["rmg"]["zoneLimit"]);
  54. rmgInfo.rarity = static_cast<ui32>(input["rmg"]["rarity"].Float());
  55. } // else block is not needed - set in constructor
  56. for (auto entry : input["templates"].Struct())
  57. {
  58. entry.second.setType(JsonNode::JsonType::DATA_STRUCT);
  59. JsonUtils::inherit(entry.second, base);
  60. auto * tmpl = new ObjectTemplate;
  61. tmpl->id = Obj(type);
  62. tmpl->subid = subtype;
  63. tmpl->stringID = entry.first; // FIXME: create "fullID" - type.object.template?
  64. try
  65. {
  66. tmpl->readJson(entry.second);
  67. templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
  68. }
  69. catch (const std::exception & e)
  70. {
  71. logGlobal->warn("Failed to load terrains for object %s: %s", entry.first, e.what());
  72. }
  73. }
  74. for(const JsonNode & node : input["sounds"]["ambient"].Vector())
  75. sounds.ambient.push_back(node.String());
  76. for(const JsonNode & node : input["sounds"]["visit"].Vector())
  77. sounds.visit.push_back(node.String());
  78. for(const JsonNode & node : input["sounds"]["removal"].Vector())
  79. sounds.removal.push_back(node.String());
  80. if(input["aiValue"].isNull())
  81. aiValue = std::nullopt;
  82. else
  83. aiValue = static_cast<std::optional<si32>>(input["aiValue"].Integer());
  84. if(input["battleground"].getType() == JsonNode::JsonType::DATA_STRING)
  85. battlefield = input["battleground"].String();
  86. else
  87. battlefield = std::nullopt;
  88. initTypeData(input);
  89. }
  90. bool AObjectTypeHandler::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  91. {
  92. return false; // by default there are no overrides
  93. }
  94. void AObjectTypeHandler::preInitObject(CGObjectInstance * obj) const
  95. {
  96. obj->ID = Obj(type);
  97. obj->subID = subtype;
  98. obj->typeName = typeName;
  99. obj->subTypeName = subTypeName;
  100. }
  101. void AObjectTypeHandler::initTypeData(const JsonNode & input)
  102. {
  103. // empty implementation for overrides
  104. }
  105. bool AObjectTypeHandler::hasNameTextID() const
  106. {
  107. return false;
  108. }
  109. std::string AObjectTypeHandler::getNameTextID() const
  110. {
  111. return TextIdentifier("mapObject", modScope, typeName, subTypeName, "name").get();
  112. }
  113. std::string AObjectTypeHandler::getNameTranslated() const
  114. {
  115. return VLC->generaltexth->translate(getNameTextID());
  116. }
  117. SObjectSounds AObjectTypeHandler::getSounds() const
  118. {
  119. return sounds;
  120. }
  121. void AObjectTypeHandler::addTemplate(const std::shared_ptr<const ObjectTemplate> & templ)
  122. {
  123. templates.push_back(templ);
  124. }
  125. void AObjectTypeHandler::addTemplate(JsonNode config)
  126. {
  127. config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null
  128. JsonUtils::inherit(config, base);
  129. auto * tmpl = new ObjectTemplate;
  130. tmpl->id = Obj(type);
  131. tmpl->subid = subtype;
  132. tmpl->stringID.clear(); // TODO?
  133. tmpl->readJson(config);
  134. templates.emplace_back(tmpl);
  135. }
  136. std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTemplates() const
  137. {
  138. return templates;
  139. }
  140. BattleField AObjectTypeHandler::getBattlefield() const
  141. {
  142. return battlefield ? BattleField::fromString(battlefield.value()) : BattleField::NONE;
  143. }
  144. std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TerrainId terrainType) const
  145. {
  146. std::vector<std::shared_ptr<const ObjectTemplate>> templates = getTemplates();
  147. std::vector<std::shared_ptr<const ObjectTemplate>> filtered;
  148. const auto cfun = [&](const std::shared_ptr<const ObjectTemplate> & obj)
  149. {
  150. return obj->canBePlacedAt(terrainType);
  151. };
  152. std::copy_if(templates.begin(), templates.end(), std::back_inserter(filtered), cfun);
  153. // H3 defines allowed terrains in a weird way - artifacts, monsters and resources have faulty masks here
  154. // Perhaps we should re-define faulty templates and remove this workaround (already done for resources)
  155. if (type == Obj::ARTIFACT || type == Obj::MONSTER)
  156. return templates;
  157. else
  158. return filtered;
  159. }
  160. std::shared_ptr<const ObjectTemplate> AObjectTypeHandler::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
  161. {
  162. std::vector<std::shared_ptr<const ObjectTemplate>> ret = getTemplates(terrainType);
  163. for (const auto & tmpl: ret)
  164. {
  165. if (objectFilter(object, tmpl))
  166. return tmpl;
  167. }
  168. return std::shared_ptr<const ObjectTemplate>(); //empty
  169. }
  170. const RandomMapInfo & AObjectTypeHandler::getRMGInfo()
  171. {
  172. return rmgInfo;
  173. }
  174. std::optional<si32> AObjectTypeHandler::getAiValue() const
  175. {
  176. return aiValue;
  177. }
  178. bool AObjectTypeHandler::isStaticObject()
  179. {
  180. return false; // most of classes are not static
  181. }
  182. void AObjectTypeHandler::afterLoadFinalization()
  183. {
  184. }
  185. VCMI_LIB_NAMESPACE_END