MapIdentifiersH3M.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * MapIdentifiersH3M.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 "MapIdentifiersH3M.h"
  12. #include "../GameLibrary.h"
  13. #include "../entities/faction/CFaction.h"
  14. #include "../entities/faction/CTownHandler.h"
  15. #include "../filesystem/Filesystem.h"
  16. #include "../json/JsonUtils.h"
  17. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  18. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  19. #include "../mapObjects/ObjectTemplate.h"
  20. #include "../modding/IdentifierStorage.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. template<typename IdentifierID>
  23. void MapIdentifiersH3M::loadMapping(std::map<IdentifierID, IdentifierID> & result, const JsonNode & mapping, const std::string & identifierName)
  24. {
  25. for (auto entry : mapping.Struct())
  26. {
  27. IdentifierID sourceID (entry.second.Integer());
  28. IdentifierID targetID (*LIBRARY->identifiers()->getIdentifier(entry.second.getModScope(), identifierName, entry.first));
  29. result[sourceID] = targetID;
  30. }
  31. }
  32. void MapIdentifiersH3M::loadMapping(const JsonNode & mapping)
  33. {
  34. if (!mapping["supported"].Bool())
  35. throw std::runtime_error("Unsupported map format!");
  36. formatSettings.Struct(); // change type
  37. if (!mapping["settings"].isNull())
  38. JsonUtils::inherit(formatSettings, mapping["settings"]);
  39. for (auto entryFaction : mapping["buildings"].Struct())
  40. {
  41. FactionID factionID (*LIBRARY->identifiers()->getIdentifier(entryFaction.second.getModScope(), "faction", entryFaction.first));
  42. auto buildingMap = entryFaction.second;
  43. for (auto entryBuilding : buildingMap.Struct())
  44. {
  45. BuildingID sourceID (entryBuilding.second.Integer());
  46. BuildingID targetID (*LIBRARY->identifiers()->getIdentifier(entryBuilding.second.getModScope(), "building." + LIBRARY->factions()->getById(factionID)->getJsonKey(), entryBuilding.first));
  47. mappingFactionBuilding[factionID][sourceID] = targetID;
  48. }
  49. }
  50. for (auto entryTemplate : mapping["templates"].Struct())
  51. {
  52. AnimationPath h3mName = AnimationPath::builtinTODO(entryTemplate.second.String());
  53. AnimationPath vcmiName = AnimationPath::builtinTODO(entryTemplate.first);
  54. if (!CResourceHandler::get()->existsResource(vcmiName.addPrefix("SPRITES/")))
  55. logMod->warn("Template animation file %s was not found!", vcmiName.getOriginalName());
  56. mappingObjectTemplate[h3mName] = vcmiName;
  57. }
  58. for (auto entryOuter : mapping["objects"].Struct())
  59. {
  60. if (entryOuter.second.isStruct())
  61. {
  62. for (auto entryInner : entryOuter.second.Struct())
  63. {
  64. auto handler = LIBRARY->objtypeh->getHandlerFor( entryInner.second.getModScope(), entryOuter.first, entryInner.first);
  65. auto entryValues = entryInner.second.Vector();
  66. ObjectTypeIdentifier h3mID{Obj(entryValues[0].Integer()), int32_t(entryValues[1].Integer())};
  67. ObjectTypeIdentifier vcmiID{Obj(handler->getIndex()), handler->getSubIndex()};
  68. mappingObjectIndex[h3mID] = vcmiID;
  69. }
  70. }
  71. else
  72. {
  73. auto handler = LIBRARY->objtypeh->getHandlerFor( entryOuter.second.getModScope(), entryOuter.first, entryOuter.first);
  74. auto entryValues = entryOuter.second.Vector();
  75. ObjectTypeIdentifier h3mID{Obj(entryValues[0].Integer()), int32_t(entryValues[1].Integer())};
  76. ObjectTypeIdentifier vcmiID{Obj(handler->getIndex()), handler->getSubIndex()};
  77. mappingObjectIndex[h3mID] = vcmiID;
  78. }
  79. }
  80. for (auto entry : mapping["campaignVideo"].Struct())
  81. mappingCampaignVideo[entry.second.Integer()] = VideoPath::builtinTODO(entry.first);
  82. for (auto entry : mapping["campaignMusic"].Struct())
  83. mappingCampaignMusic[entry.second.Integer()] = AudioPath::builtinTODO(entry.first);
  84. loadMapping(mappingHeroPortrait, mapping["portraits"], "hero");
  85. loadMapping(mappingBuilding, mapping["buildingsCommon"], "building.core:random");
  86. loadMapping(mappingFaction, mapping["factions"], "faction");
  87. loadMapping(mappingCreature, mapping["creatures"], "creature");
  88. loadMapping(mappingHeroType, mapping["heroes"], "hero");
  89. loadMapping(mappingHeroClass, mapping["heroClasses"], "heroClass");
  90. loadMapping(mappingTerrain, mapping["terrains"], "terrain");
  91. loadMapping(mappingArtifact, mapping["artifacts"], "artifact");
  92. loadMapping(mappingSecondarySkill, mapping["skills"], "skill");
  93. loadMapping(mappingCampaignRegions, mapping["campaignRegions"], "campaignRegion");
  94. }
  95. void MapIdentifiersH3M::remapTemplate(ObjectTemplate & objectTemplate)
  96. {
  97. auto name = objectTemplate.animationFile;
  98. if (mappingObjectTemplate.count(name))
  99. objectTemplate.animationFile = mappingObjectTemplate.at(name);
  100. ObjectTypeIdentifier objectType{ objectTemplate.id, objectTemplate.subid};
  101. if (mappingObjectIndex.count(objectType))
  102. {
  103. auto mappedType = mappingObjectIndex.at(objectType);
  104. objectTemplate.id = mappedType.ID;
  105. objectTemplate.subid = mappedType.subID;
  106. }
  107. if (objectTemplate.id == Obj::TOWN || objectTemplate.id == Obj::RANDOM_DWELLING_FACTION)
  108. objectTemplate.subid = remap(FactionID(objectTemplate.subid));
  109. if (objectTemplate.id == Obj::MONSTER)
  110. objectTemplate.subid = remap(CreatureID(objectTemplate.subid));
  111. if (objectTemplate.id == Obj::ARTIFACT)
  112. objectTemplate.subid = remap(ArtifactID(objectTemplate.subid));
  113. if (LIBRARY->objtypeh->knownObjects().count(objectTemplate.id) == 0)
  114. {
  115. logGlobal->warn("Unknown object found: %d | %d", objectTemplate.id, objectTemplate.subid);
  116. objectTemplate.id = Obj::NOTHING;
  117. objectTemplate.subid = {};
  118. }
  119. else
  120. {
  121. if (LIBRARY->objtypeh->knownSubObjects(objectTemplate.id).count(objectTemplate.subid) == 0)
  122. {
  123. logGlobal->warn("Unknown subobject found: %d | %d", objectTemplate.id, objectTemplate.subid);
  124. objectTemplate.subid = {};
  125. }
  126. }
  127. }
  128. BuildingID MapIdentifiersH3M::remapBuilding(std::optional<FactionID> owner, BuildingID input) const
  129. {
  130. if (owner.has_value() && mappingFactionBuilding.count(*owner))
  131. {
  132. auto submap = mappingFactionBuilding.at(*owner);
  133. if (submap.count(input))
  134. return submap.at(input);
  135. }
  136. if (mappingBuilding.count(input))
  137. return mappingBuilding.at(input);
  138. return BuildingID::NONE;
  139. }
  140. FactionID MapIdentifiersH3M::remap(FactionID input) const
  141. {
  142. if (mappingFaction.count(input))
  143. return mappingFaction.at(input);
  144. return input;
  145. }
  146. CreatureID MapIdentifiersH3M::remap(CreatureID input) const
  147. {
  148. if (mappingCreature.count(input))
  149. return mappingCreature.at(input);
  150. return input;
  151. }
  152. HeroTypeID MapIdentifiersH3M::remap(HeroTypeID input) const
  153. {
  154. if (mappingHeroType.count(input))
  155. return mappingHeroType.at(input);
  156. return input;
  157. }
  158. HeroTypeID MapIdentifiersH3M::remapPortrait(HeroTypeID input) const
  159. {
  160. if (mappingHeroPortrait.count(input))
  161. return mappingHeroPortrait.at(input);
  162. return input;
  163. }
  164. HeroClassID MapIdentifiersH3M::remap(HeroClassID input) const
  165. {
  166. if (mappingHeroClass.count(input))
  167. return mappingHeroClass.at(input);
  168. return input;
  169. }
  170. TerrainId MapIdentifiersH3M::remap(TerrainId input) const
  171. {
  172. if (mappingTerrain.count(input))
  173. return mappingTerrain.at(input);
  174. return input;
  175. }
  176. ArtifactID MapIdentifiersH3M::remap(ArtifactID input) const
  177. {
  178. if (mappingArtifact.count(input))
  179. return mappingArtifact.at(input);
  180. return input;
  181. }
  182. SecondarySkill MapIdentifiersH3M::remap(SecondarySkill input) const
  183. {
  184. if (mappingSecondarySkill.count(input))
  185. return mappingSecondarySkill.at(input);
  186. return input;
  187. }
  188. CampaignRegionID MapIdentifiersH3M::remap(CampaignRegionID input) const
  189. {
  190. if (!mappingCampaignRegions.count(input))
  191. throw std::out_of_range("Campaign region with ID " + std::to_string(input.getNum()) + " is not defined");
  192. return mappingCampaignRegions.at(input);
  193. }
  194. VideoPath MapIdentifiersH3M::remapCampaignVideo(int input) const
  195. {
  196. if (!mappingCampaignVideo.count(input))
  197. throw std::out_of_range("Campaign video with ID " + std::to_string(input) + " is not defined");
  198. return mappingCampaignVideo.at(input);
  199. }
  200. AudioPath MapIdentifiersH3M::remapCampaignMusic(int input) const
  201. {
  202. if (!mappingCampaignMusic.count(input))
  203. throw std::out_of_range("Campaign music with ID " + std::to_string(input) + " is not defined");
  204. return mappingCampaignMusic.at(input);
  205. }
  206. VCMI_LIB_NAMESPACE_END