MapIdentifiersH3M.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. loadMapping(mappingHeroPortrait, mapping["portraits"], "hero");
  77. loadMapping(mappingBuilding, mapping["buildingsCommon"], "building.core:random");
  78. loadMapping(mappingFaction, mapping["factions"], "faction");
  79. loadMapping(mappingCreature, mapping["creatures"], "creature");
  80. loadMapping(mappingHeroType, mapping["heroes"], "hero");
  81. loadMapping(mappingHeroClass, mapping["heroClasses"], "heroClass");
  82. loadMapping(mappingTerrain, mapping["terrains"], "terrain");
  83. loadMapping(mappingArtifact, mapping["artifacts"], "artifact");
  84. loadMapping(mappingSecondarySkill, mapping["skills"], "skill");
  85. loadMapping(mappingCampaignRegions, mapping["campaignRegions"], "campaignRegion");
  86. }
  87. void MapIdentifiersH3M::remapTemplate(ObjectTemplate & objectTemplate)
  88. {
  89. auto name = objectTemplate.animationFile;
  90. if (mappingObjectTemplate.count(name))
  91. objectTemplate.animationFile = mappingObjectTemplate.at(name);
  92. ObjectTypeIdentifier objectType{ objectTemplate.id, objectTemplate.subid};
  93. if (mappingObjectIndex.count(objectType))
  94. {
  95. auto mappedType = mappingObjectIndex.at(objectType);
  96. objectTemplate.id = mappedType.ID;
  97. objectTemplate.subid = mappedType.subID;
  98. }
  99. if (objectTemplate.id == Obj::TOWN || objectTemplate.id == Obj::RANDOM_DWELLING_FACTION)
  100. objectTemplate.subid = remap(FactionID(objectTemplate.subid));
  101. if (objectTemplate.id == Obj::MONSTER)
  102. objectTemplate.subid = remap(CreatureID(objectTemplate.subid));
  103. if (objectTemplate.id == Obj::ARTIFACT)
  104. objectTemplate.subid = remap(ArtifactID(objectTemplate.subid));
  105. if (LIBRARY->objtypeh->knownObjects().count(objectTemplate.id) == 0)
  106. {
  107. logGlobal->warn("Unknown object found: %d | %d", objectTemplate.id, objectTemplate.subid);
  108. objectTemplate.id = Obj::NOTHING;
  109. objectTemplate.subid = {};
  110. }
  111. else
  112. {
  113. if (LIBRARY->objtypeh->knownSubObjects(objectTemplate.id).count(objectTemplate.subid) == 0)
  114. {
  115. logGlobal->warn("Unknown subobject found: %d | %d", objectTemplate.id, objectTemplate.subid);
  116. objectTemplate.subid = {};
  117. }
  118. }
  119. }
  120. BuildingID MapIdentifiersH3M::remapBuilding(std::optional<FactionID> owner, BuildingID input) const
  121. {
  122. if (owner.has_value() && mappingFactionBuilding.count(*owner))
  123. {
  124. auto submap = mappingFactionBuilding.at(*owner);
  125. if (submap.count(input))
  126. return submap.at(input);
  127. }
  128. if (mappingBuilding.count(input))
  129. return mappingBuilding.at(input);
  130. return BuildingID::NONE;
  131. }
  132. FactionID MapIdentifiersH3M::remap(FactionID input) const
  133. {
  134. if (mappingFaction.count(input))
  135. return mappingFaction.at(input);
  136. return input;
  137. }
  138. CreatureID MapIdentifiersH3M::remap(CreatureID input) const
  139. {
  140. if (mappingCreature.count(input))
  141. return mappingCreature.at(input);
  142. return input;
  143. }
  144. HeroTypeID MapIdentifiersH3M::remap(HeroTypeID input) const
  145. {
  146. if (mappingHeroType.count(input))
  147. return mappingHeroType.at(input);
  148. return input;
  149. }
  150. HeroTypeID MapIdentifiersH3M::remapPortrait(HeroTypeID input) const
  151. {
  152. if (mappingHeroPortrait.count(input))
  153. return mappingHeroPortrait.at(input);
  154. return input;
  155. }
  156. HeroClassID MapIdentifiersH3M::remap(HeroClassID input) const
  157. {
  158. if (mappingHeroClass.count(input))
  159. return mappingHeroClass.at(input);
  160. return input;
  161. }
  162. TerrainId MapIdentifiersH3M::remap(TerrainId input) const
  163. {
  164. if (mappingTerrain.count(input))
  165. return mappingTerrain.at(input);
  166. return input;
  167. }
  168. ArtifactID MapIdentifiersH3M::remap(ArtifactID input) const
  169. {
  170. if (mappingArtifact.count(input))
  171. return mappingArtifact.at(input);
  172. return input;
  173. }
  174. SecondarySkill MapIdentifiersH3M::remap(SecondarySkill input) const
  175. {
  176. if (mappingSecondarySkill.count(input))
  177. return mappingSecondarySkill.at(input);
  178. return input;
  179. }
  180. CampaignRegionID MapIdentifiersH3M::remap(CampaignRegionID input) const
  181. {
  182. if (mappingCampaignRegions.count(input))
  183. return mappingCampaignRegions.at(input);
  184. return input;
  185. }
  186. VCMI_LIB_NAMESPACE_END