AObjectTypeHandler.cpp 5.9 KB

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