AObjectTypeHandler.cpp 6.1 KB

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