MapIdentifiersH3M.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. {
  82. if(mappingCampaignVideo[entry.second.Integer()].first.empty())
  83. mappingCampaignVideo[entry.second.Integer()].first = VideoPath::builtinTODO(entry.first);
  84. else
  85. mappingCampaignVideo[entry.second.Integer()].second = VideoPath::builtinTODO(entry.first);
  86. }
  87. for (auto entry : mapping["campaignMusic"].Struct())
  88. mappingCampaignMusic[entry.second.Integer()] = AudioPath::builtinTODO(entry.first);
  89. loadMapping(mappingHeroPortrait, mapping["portraits"], "hero");
  90. loadMapping(mappingBuilding, mapping["buildingsCommon"], "building.core:random");
  91. loadMapping(mappingFaction, mapping["factions"], "faction");
  92. loadMapping(mappingCreature, mapping["creatures"], "creature");
  93. loadMapping(mappingHeroType, mapping["heroes"], "hero");
  94. loadMapping(mappingHeroClass, mapping["heroClasses"], "heroClass");
  95. loadMapping(mappingTerrain, mapping["terrains"], "terrain");
  96. loadMapping(mappingArtifact, mapping["artifacts"], "artifact");
  97. loadMapping(mappingSecondarySkill, mapping["skills"], "skill");
  98. loadMapping(mappingCampaignRegions, mapping["campaignRegions"], "campaignRegion");
  99. }
  100. void MapIdentifiersH3M::remapTemplate(ObjectTemplate & objectTemplate)
  101. {
  102. auto name = objectTemplate.animationFile;
  103. if (mappingObjectTemplate.count(name))
  104. objectTemplate.animationFile = mappingObjectTemplate.at(name);
  105. ObjectTypeIdentifier objectType{ objectTemplate.id, objectTemplate.subid};
  106. if (mappingObjectIndex.count(objectType))
  107. {
  108. auto mappedType = mappingObjectIndex.at(objectType);
  109. objectTemplate.id = mappedType.ID;
  110. objectTemplate.subid = mappedType.subID;
  111. }
  112. if (objectTemplate.id == Obj::TOWN || objectTemplate.id == Obj::RANDOM_DWELLING_FACTION)
  113. objectTemplate.subid = remap(FactionID(objectTemplate.subid));
  114. if (objectTemplate.id == Obj::MONSTER)
  115. objectTemplate.subid = remap(CreatureID(objectTemplate.subid));
  116. if (objectTemplate.id == Obj::ARTIFACT)
  117. objectTemplate.subid = remap(ArtifactID(objectTemplate.subid));
  118. if (LIBRARY->objtypeh->knownObjects().count(objectTemplate.id) == 0)
  119. {
  120. logGlobal->warn("Unknown object found: %d | %d", objectTemplate.id, objectTemplate.subid);
  121. objectTemplate.id = Obj::NOTHING;
  122. objectTemplate.subid = {};
  123. }
  124. else
  125. {
  126. if (LIBRARY->objtypeh->knownSubObjects(objectTemplate.id).count(objectTemplate.subid) == 0)
  127. {
  128. logGlobal->warn("Unknown subobject found: %d | %d", objectTemplate.id, objectTemplate.subid);
  129. objectTemplate.subid = {};
  130. }
  131. }
  132. }
  133. BuildingID MapIdentifiersH3M::remapBuilding(std::optional<FactionID> owner, BuildingID input) const
  134. {
  135. if (owner.has_value() && mappingFactionBuilding.count(*owner))
  136. {
  137. auto submap = mappingFactionBuilding.at(*owner);
  138. if (submap.count(input))
  139. return submap.at(input);
  140. }
  141. if (mappingBuilding.count(input))
  142. return mappingBuilding.at(input);
  143. return BuildingID::NONE;
  144. }
  145. FactionID MapIdentifiersH3M::remap(FactionID input) const
  146. {
  147. if (mappingFaction.count(input))
  148. return mappingFaction.at(input);
  149. return input;
  150. }
  151. CreatureID MapIdentifiersH3M::remap(CreatureID input) const
  152. {
  153. if (mappingCreature.count(input))
  154. return mappingCreature.at(input);
  155. return input;
  156. }
  157. HeroTypeID MapIdentifiersH3M::remap(HeroTypeID input) const
  158. {
  159. if (mappingHeroType.count(input))
  160. return mappingHeroType.at(input);
  161. return input;
  162. }
  163. HeroTypeID MapIdentifiersH3M::remapPortrait(HeroTypeID input) const
  164. {
  165. if (mappingHeroPortrait.count(input))
  166. return mappingHeroPortrait.at(input);
  167. return input;
  168. }
  169. HeroClassID MapIdentifiersH3M::remap(HeroClassID input) const
  170. {
  171. if (mappingHeroClass.count(input))
  172. return mappingHeroClass.at(input);
  173. return input;
  174. }
  175. TerrainId MapIdentifiersH3M::remap(TerrainId input) const
  176. {
  177. if (mappingTerrain.count(input))
  178. return mappingTerrain.at(input);
  179. return input;
  180. }
  181. ArtifactID MapIdentifiersH3M::remap(ArtifactID input) const
  182. {
  183. if (mappingArtifact.count(input))
  184. return mappingArtifact.at(input);
  185. return input;
  186. }
  187. SecondarySkill MapIdentifiersH3M::remap(SecondarySkill input) const
  188. {
  189. if (mappingSecondarySkill.count(input))
  190. return mappingSecondarySkill.at(input);
  191. return input;
  192. }
  193. CampaignRegionID MapIdentifiersH3M::remap(CampaignRegionID input) const
  194. {
  195. if (!mappingCampaignRegions.count(input))
  196. throw std::out_of_range("Campaign region with ID " + std::to_string(input.getNum()) + " is not defined");
  197. return mappingCampaignRegions.at(input);
  198. }
  199. std::pair<VideoPath, VideoPath> MapIdentifiersH3M::remapCampaignVideo(int input) const
  200. {
  201. if (!mappingCampaignVideo.count(input))
  202. throw std::out_of_range("Campaign video with ID " + std::to_string(input) + " is not defined");
  203. return mappingCampaignVideo.at(input);
  204. }
  205. AudioPath MapIdentifiersH3M::remapCampaignMusic(int input) const
  206. {
  207. if (!mappingCampaignMusic.count(input))
  208. throw std::out_of_range("Campaign music with ID " + std::to_string(input) + " is not defined");
  209. return mappingCampaignMusic.at(input);
  210. }
  211. VCMI_LIB_NAMESPACE_END