MapIdentifiersH3M.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "../VCMI_Lib.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 (*VLC->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 (*VLC->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 (*VLC->identifiers()->getIdentifier(entryBuilding.second.getModScope(), "building." + VLC->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 = VLC->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 = VLC->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. }
  86. void MapIdentifiersH3M::remapTemplate(ObjectTemplate & objectTemplate)
  87. {
  88. auto name = objectTemplate.animationFile;
  89. if (mappingObjectTemplate.count(name))
  90. objectTemplate.animationFile = mappingObjectTemplate.at(name);
  91. ObjectTypeIdentifier objectType{ objectTemplate.id, objectTemplate.subid};
  92. if (mappingObjectIndex.count(objectType))
  93. {
  94. auto mappedType = mappingObjectIndex.at(objectType);
  95. objectTemplate.id = mappedType.ID;
  96. objectTemplate.subid = mappedType.subID;
  97. }
  98. if (VLC->objtypeh->knownObjects().count(objectTemplate.id) == 0)
  99. {
  100. logGlobal->warn("Unknown object found: %d | %d", objectTemplate.id, objectTemplate.subid);
  101. objectTemplate.id = Obj::NOTHING;
  102. objectTemplate.subid = {};
  103. }
  104. else
  105. {
  106. if (VLC->objtypeh->knownSubObjects(objectTemplate.id).count(objectTemplate.subid) == 0)
  107. {
  108. logGlobal->warn("Unknown subobject found: %d | %d", objectTemplate.id, objectTemplate.subid);
  109. objectTemplate.subid = {};
  110. }
  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. }
  119. BuildingID MapIdentifiersH3M::remapBuilding(std::optional<FactionID> owner, BuildingID input) const
  120. {
  121. if (owner.has_value() && mappingFactionBuilding.count(*owner))
  122. {
  123. auto submap = mappingFactionBuilding.at(*owner);
  124. if (submap.count(input))
  125. return submap.at(input);
  126. }
  127. if (mappingBuilding.count(input))
  128. return mappingBuilding.at(input);
  129. return BuildingID::NONE;
  130. }
  131. FactionID MapIdentifiersH3M::remap(FactionID input) const
  132. {
  133. if (mappingFaction.count(input))
  134. return mappingFaction.at(input);
  135. return input;
  136. }
  137. CreatureID MapIdentifiersH3M::remap(CreatureID input) const
  138. {
  139. if (mappingCreature.count(input))
  140. return mappingCreature.at(input);
  141. return input;
  142. }
  143. HeroTypeID MapIdentifiersH3M::remap(HeroTypeID input) const
  144. {
  145. if (mappingHeroType.count(input))
  146. return mappingHeroType.at(input);
  147. return input;
  148. }
  149. HeroTypeID MapIdentifiersH3M::remapPortrait(HeroTypeID input) const
  150. {
  151. if (mappingHeroPortrait.count(input))
  152. return mappingHeroPortrait.at(input);
  153. return input;
  154. }
  155. HeroClassID MapIdentifiersH3M::remap(HeroClassID input) const
  156. {
  157. if (mappingHeroClass.count(input))
  158. return mappingHeroClass.at(input);
  159. return input;
  160. }
  161. TerrainId MapIdentifiersH3M::remap(TerrainId input) const
  162. {
  163. if (mappingTerrain.count(input))
  164. return mappingTerrain.at(input);
  165. return input;
  166. }
  167. ArtifactID MapIdentifiersH3M::remap(ArtifactID input) const
  168. {
  169. if (mappingArtifact.count(input))
  170. return mappingArtifact.at(input);
  171. return input;
  172. }
  173. SecondarySkill MapIdentifiersH3M::remap(SecondarySkill input) const
  174. {
  175. if (mappingSecondarySkill.count(input))
  176. return mappingSecondarySkill.at(input);
  177. return input;
  178. }
  179. VCMI_LIB_NAMESPACE_END