MapIdentifiersH3M.cpp 5.5 KB

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