MapIdentifiersH3M.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 (objectTemplate.id == Obj::TOWN || objectTemplate.id == Obj::RANDOM_DWELLING_FACTION)
  99. objectTemplate.subid = remap(FactionID(objectTemplate.subid));
  100. if (objectTemplate.id == Obj::MONSTER)
  101. objectTemplate.subid = remap(CreatureID(objectTemplate.subid));
  102. if (objectTemplate.id == Obj::ARTIFACT)
  103. objectTemplate.subid = remap(ArtifactID(objectTemplate.subid));
  104. }
  105. BuildingID MapIdentifiersH3M::remapBuilding(std::optional<FactionID> owner, BuildingID input) const
  106. {
  107. if (owner.has_value() && mappingFactionBuilding.count(*owner))
  108. {
  109. auto submap = mappingFactionBuilding.at(*owner);
  110. if (submap.count(input))
  111. return submap.at(input);
  112. }
  113. if (mappingBuilding.count(input))
  114. return mappingBuilding.at(input);
  115. return BuildingID::NONE;
  116. }
  117. FactionID MapIdentifiersH3M::remap(FactionID input) const
  118. {
  119. if (mappingFaction.count(input))
  120. return mappingFaction.at(input);
  121. return input;
  122. }
  123. CreatureID MapIdentifiersH3M::remap(CreatureID input) const
  124. {
  125. if (mappingCreature.count(input))
  126. return mappingCreature.at(input);
  127. return input;
  128. }
  129. HeroTypeID MapIdentifiersH3M::remap(HeroTypeID input) const
  130. {
  131. if (mappingHeroType.count(input))
  132. return mappingHeroType.at(input);
  133. return input;
  134. }
  135. HeroTypeID MapIdentifiersH3M::remapPortrait(HeroTypeID input) const
  136. {
  137. if (mappingHeroPortrait.count(input))
  138. return mappingHeroPortrait.at(input);
  139. return input;
  140. }
  141. HeroClassID MapIdentifiersH3M::remap(HeroClassID input) const
  142. {
  143. if (mappingHeroClass.count(input))
  144. return mappingHeroClass.at(input);
  145. return input;
  146. }
  147. TerrainId MapIdentifiersH3M::remap(TerrainId input) const
  148. {
  149. if (mappingTerrain.count(input))
  150. return mappingTerrain.at(input);
  151. return input;
  152. }
  153. ArtifactID MapIdentifiersH3M::remap(ArtifactID input) const
  154. {
  155. if (mappingArtifact.count(input))
  156. return mappingArtifact.at(input);
  157. return input;
  158. }
  159. SecondarySkill MapIdentifiersH3M::remap(SecondarySkill input) const
  160. {
  161. if (mappingSecondarySkill.count(input))
  162. return mappingSecondarySkill.at(input);
  163. return input;
  164. }
  165. VCMI_LIB_NAMESPACE_END