MapIdentifiersH3M.cpp 5.3 KB

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