MapIdentifiersH3M.cpp 6.1 KB

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