MapIdentifiersH3M.cpp 9.0 KB

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