AObjectTypeHandler.cpp 5.9 KB

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