MapIdentifiersH3M.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "../CHeroHandler.h"
  14. #include "../entities/faction/CFaction.h"
  15. #include "../entities/faction/CTownHandler.h"
  16. #include "../filesystem/Filesystem.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 (*VLC->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. for (auto entryFaction : mapping["buildings"].Struct())
  37. {
  38. FactionID factionID (*VLC->identifiers()->getIdentifier(entryFaction.second.getModScope(), "faction", entryFaction.first));
  39. auto buildingMap = entryFaction.second;
  40. for (auto entryBuilding : buildingMap.Struct())
  41. {
  42. BuildingID sourceID (entryBuilding.second.Integer());
  43. BuildingID targetID (*VLC->identifiers()->getIdentifier(entryBuilding.second.getModScope(), "building." + VLC->factions()->getById(factionID)->getJsonKey(), entryBuilding.first));
  44. mappingFactionBuilding[factionID][sourceID] = targetID;
  45. }
  46. }
  47. for (auto entryTemplate : mapping["templates"].Struct())
  48. {
  49. AnimationPath h3mName = AnimationPath::builtinTODO(entryTemplate.second.String());
  50. AnimationPath vcmiName = AnimationPath::builtinTODO(entryTemplate.first);
  51. if (!CResourceHandler::get()->existsResource(vcmiName.addPrefix("SPRITES/")))
  52. logMod->warn("Template animation file %s was not found!", vcmiName.getOriginalName());
  53. mappingObjectTemplate[h3mName] = vcmiName;
  54. }
  55. for (auto entryOuter : mapping["objects"].Struct())
  56. {
  57. if (entryOuter.second.isStruct())
  58. {
  59. for (auto entryInner : entryOuter.second.Struct())
  60. {
  61. auto handler = VLC->objtypeh->getHandlerFor( entryInner.second.getModScope(), entryOuter.first, entryInner.first);
  62. auto entryValues = entryInner.second.Vector();
  63. ObjectTypeIdentifier h3mID{Obj(entryValues[0].Integer()), int32_t(entryValues[1].Integer())};
  64. ObjectTypeIdentifier vcmiID{Obj(handler->getIndex()), handler->getSubIndex()};
  65. mappingObjectIndex[h3mID] = vcmiID;
  66. }
  67. }
  68. else
  69. {
  70. auto handler = VLC->objtypeh->getHandlerFor( entryOuter.second.getModScope(), entryOuter.first, entryOuter.first);
  71. auto entryValues = entryOuter.second.Vector();
  72. ObjectTypeIdentifier h3mID{Obj(entryValues[0].Integer()), int32_t(entryValues[1].Integer())};
  73. ObjectTypeIdentifier vcmiID{Obj(handler->getIndex()), handler->getSubIndex()};
  74. mappingObjectIndex[h3mID] = vcmiID;
  75. }
  76. }
  77. loadMapping(mappingHeroPortrait, mapping["portraits"], "hero");
  78. loadMapping(mappingBuilding, mapping["buildingsCommon"], "building.core:random");
  79. loadMapping(mappingFaction, mapping["factions"], "faction");
  80. loadMapping(mappingCreature, mapping["creatures"], "creature");
  81. loadMapping(mappingHeroType, mapping["heroes"], "hero");
  82. loadMapping(mappingHeroClass, mapping["heroClasses"], "heroClass");
  83. loadMapping(mappingTerrain, mapping["terrains"], "terrain");
  84. loadMapping(mappingArtifact, mapping["artifacts"], "artifact");
  85. loadMapping(mappingSecondarySkill, mapping["skills"], "skill");
  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. }
  106. BuildingID MapIdentifiersH3M::remapBuilding(std::optional<FactionID> owner, BuildingID input) const
  107. {
  108. if (owner.has_value() && mappingFactionBuilding.count(*owner))
  109. {
  110. auto submap = mappingFactionBuilding.at(*owner);
  111. if (submap.count(input))
  112. return submap.at(input);
  113. }
  114. if (mappingBuilding.count(input))
  115. return mappingBuilding.at(input);
  116. return BuildingID::NONE;
  117. }
  118. FactionID MapIdentifiersH3M::remap(FactionID input) const
  119. {
  120. if (mappingFaction.count(input))
  121. return mappingFaction.at(input);
  122. return input;
  123. }
  124. CreatureID MapIdentifiersH3M::remap(CreatureID input) const
  125. {
  126. if (mappingCreature.count(input))
  127. return mappingCreature.at(input);
  128. return input;
  129. }
  130. HeroTypeID MapIdentifiersH3M::remap(HeroTypeID input) const
  131. {
  132. if (mappingHeroType.count(input))
  133. return mappingHeroType.at(input);
  134. return input;
  135. }
  136. HeroTypeID MapIdentifiersH3M::remapPortrait(HeroTypeID input) const
  137. {
  138. if (mappingHeroPortrait.count(input))
  139. return mappingHeroPortrait.at(input);
  140. return input;
  141. }
  142. HeroClassID MapIdentifiersH3M::remap(HeroClassID input) const
  143. {
  144. if (mappingHeroClass.count(input))
  145. return mappingHeroClass.at(input);
  146. return input;
  147. }
  148. TerrainId MapIdentifiersH3M::remap(TerrainId input) const
  149. {
  150. if (mappingTerrain.count(input))
  151. return mappingTerrain.at(input);
  152. return input;
  153. }
  154. ArtifactID MapIdentifiersH3M::remap(ArtifactID input) const
  155. {
  156. if (mappingArtifact.count(input))
  157. return mappingArtifact.at(input);
  158. return input;
  159. }
  160. SecondarySkill MapIdentifiersH3M::remap(SecondarySkill input) const
  161. {
  162. if (mappingSecondarySkill.count(input))
  163. return mappingSecondarySkill.at(input);
  164. return input;
  165. }
  166. VCMI_LIB_NAMESPACE_END