AObjectTypeHandler.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. tmpl->readJson(entry.second);
  66. templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
  67. }
  68. for(const JsonNode & node : input["sounds"]["ambient"].Vector())
  69. sounds.ambient.push_back(node.String());
  70. for(const JsonNode & node : input["sounds"]["visit"].Vector())
  71. sounds.visit.push_back(node.String());
  72. for(const JsonNode & node : input["sounds"]["removal"].Vector())
  73. sounds.removal.push_back(node.String());
  74. if(input["aiValue"].isNull())
  75. aiValue = std::nullopt;
  76. else
  77. aiValue = static_cast<std::optional<si32>>(input["aiValue"].Integer());
  78. if(input["battleground"].getType() == JsonNode::JsonType::DATA_STRING)
  79. battlefield = input["battleground"].String();
  80. else
  81. battlefield = std::nullopt;
  82. initTypeData(input);
  83. }
  84. bool AObjectTypeHandler::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  85. {
  86. return false; // by default there are no overrides
  87. }
  88. void AObjectTypeHandler::preInitObject(CGObjectInstance * obj) const
  89. {
  90. obj->ID = Obj(type);
  91. obj->subID = subtype;
  92. obj->typeName = typeName;
  93. obj->subTypeName = subTypeName;
  94. }
  95. void AObjectTypeHandler::initTypeData(const JsonNode & input)
  96. {
  97. // empty implementation for overrides
  98. }
  99. bool AObjectTypeHandler::hasNameTextID() const
  100. {
  101. return false;
  102. }
  103. std::string AObjectTypeHandler::getBaseTextID() const
  104. {
  105. return TextIdentifier("mapObject", modScope, typeName, subTypeName).get();
  106. }
  107. std::string AObjectTypeHandler::getNameTextID() const
  108. {
  109. return TextIdentifier(getBaseTextID(), "name").get();
  110. }
  111. std::string AObjectTypeHandler::getNameTranslated() const
  112. {
  113. return VLC->generaltexth->translate(getNameTextID());
  114. }
  115. SObjectSounds AObjectTypeHandler::getSounds() const
  116. {
  117. return sounds;
  118. }
  119. void AObjectTypeHandler::clearTemplates()
  120. {
  121. templates.clear();
  122. }
  123. void AObjectTypeHandler::addTemplate(const std::shared_ptr<const ObjectTemplate> & templ)
  124. {
  125. templates.push_back(templ);
  126. }
  127. void AObjectTypeHandler::addTemplate(JsonNode config)
  128. {
  129. config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null
  130. JsonUtils::inherit(config, base);
  131. auto * tmpl = new ObjectTemplate;
  132. tmpl->id = Obj(type);
  133. tmpl->subid = subtype;
  134. tmpl->stringID.clear(); // TODO?
  135. tmpl->readJson(config);
  136. templates.emplace_back(tmpl);
  137. }
  138. std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTemplates() const
  139. {
  140. return templates;
  141. }
  142. BattleField AObjectTypeHandler::getBattlefield() const
  143. {
  144. return battlefield ? BattleField::fromString(battlefield.value()) : BattleField::NONE;
  145. }
  146. std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TerrainId terrainType) const
  147. {
  148. std::vector<std::shared_ptr<const ObjectTemplate>> templates = getTemplates();
  149. std::vector<std::shared_ptr<const ObjectTemplate>> filtered;
  150. const auto cfun = [&](const std::shared_ptr<const ObjectTemplate> & obj)
  151. {
  152. return obj->canBePlacedAt(terrainType);
  153. };
  154. std::copy_if(templates.begin(), templates.end(), std::back_inserter(filtered), cfun);
  155. // H3 defines allowed terrains in a weird way - artifacts, monsters and resources have faulty masks here
  156. // Perhaps we should re-define faulty templates and remove this workaround (already done for resources)
  157. if (type == Obj::ARTIFACT || type == Obj::MONSTER)
  158. return templates;
  159. else
  160. return filtered;
  161. }
  162. std::shared_ptr<const ObjectTemplate> AObjectTypeHandler::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
  163. {
  164. std::vector<std::shared_ptr<const ObjectTemplate>> ret = getTemplates(terrainType);
  165. for (const auto & tmpl: ret)
  166. {
  167. if (objectFilter(object, tmpl))
  168. return tmpl;
  169. }
  170. return std::shared_ptr<const ObjectTemplate>(); //empty
  171. }
  172. const RandomMapInfo & AObjectTypeHandler::getRMGInfo()
  173. {
  174. return rmgInfo;
  175. }
  176. std::optional<si32> AObjectTypeHandler::getAiValue() const
  177. {
  178. return aiValue;
  179. }
  180. bool AObjectTypeHandler::isStaticObject()
  181. {
  182. return false; // most of classes are not static
  183. }
  184. void AObjectTypeHandler::afterLoadFinalization()
  185. {
  186. }
  187. std::unique_ptr<IObjectInfo> AObjectTypeHandler::getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const
  188. {
  189. return nullptr;
  190. }
  191. VCMI_LIB_NAMESPACE_END