MapIdentifiersH3M.cpp 7.8 KB

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