AObjectTypeHandler.cpp 6.3 KB

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