CampaignRegions.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * CampaignRegions.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 "CampaignRegions.h"
  12. #include "../json/JsonNode.h"
  13. CampaignRegions::RegionDescription CampaignRegions::RegionDescription::fromJson(const JsonNode & node)
  14. {
  15. CampaignRegions::RegionDescription rd;
  16. rd.infix = node["infix"].String();
  17. rd.pos = Point(static_cast<int>(node["x"].Float()), static_cast<int>(node["y"].Float()));
  18. if(!node["labelPos"].isNull())
  19. rd.labelPos = Point(static_cast<int>(node["labelPos"]["x"].Float()), static_cast<int>(node["labelPos"]["y"].Float()));
  20. else
  21. rd.labelPos = std::nullopt;
  22. return rd;
  23. }
  24. JsonNode CampaignRegions::RegionDescription::toJson(CampaignRegions::RegionDescription & rd)
  25. {
  26. JsonNode node;
  27. node["infix"].String() = rd.infix;
  28. node["x"].Float() = rd.pos.x;
  29. node["y"].Float() = rd.pos.y;
  30. if(rd.labelPos != std::nullopt)
  31. {
  32. node["labelPos"]["x"].Float() = (*rd.labelPos).x;
  33. node["labelPos"]["y"].Float() = (*rd.labelPos).y;
  34. }
  35. else
  36. node["labelPos"].clear();
  37. return node;
  38. }
  39. CampaignRegions::CampaignRegions(const JsonNode & node)
  40. {
  41. campPrefix = node["prefix"].String();
  42. colorSuffixLength = static_cast<int>(node["colorSuffixLength"].Float());
  43. campSuffix = node["suffix"].isNull() ? std::vector<std::string>() : std::vector<std::string>{node["suffix"].Vector()[0].String(), node["suffix"].Vector()[1].String(), node["suffix"].Vector()[2].String()};
  44. campBackground = node["background"].isNull() ? "" : node["background"].String();
  45. for(const JsonNode & desc : node["desc"].Vector())
  46. regions.push_back(CampaignRegions::RegionDescription::fromJson(desc));
  47. }
  48. JsonNode CampaignRegions::toJson(CampaignRegions cr)
  49. {
  50. JsonNode node;
  51. node["prefix"].String() = cr.campPrefix;
  52. node["colorSuffixLength"].Float() = cr.colorSuffixLength;
  53. if(cr.campSuffix.empty())
  54. node["suffix"].clear();
  55. else
  56. node["suffix"].Vector() = JsonVector{ JsonNode(cr.campSuffix[0]), JsonNode(cr.campSuffix[1]), JsonNode(cr.campSuffix[2]) };
  57. if(cr.campBackground.empty())
  58. node["background"].clear();
  59. else
  60. node["background"].String() = cr.campBackground;
  61. node["desc"].Vector() = JsonVector();
  62. for(auto & region : cr.regions)
  63. node["desc"].Vector().push_back(CampaignRegions::RegionDescription::toJson(region));
  64. return node;
  65. }
  66. CampaignRegions CampaignRegions::getLegacy(int campId)
  67. {
  68. static std::vector<CampaignRegions> campDescriptions;
  69. if(campDescriptions.empty()) //read once
  70. {
  71. const JsonNode config(JsonPath::builtin("config/campaign_regions.json"));
  72. for(const JsonNode & campaign : config["campaign_regions"].Vector())
  73. campDescriptions.push_back(CampaignRegions(campaign));
  74. }
  75. return campDescriptions.at(campId);
  76. }
  77. ImagePath CampaignRegions::getBackgroundName() const
  78. {
  79. if(campBackground.empty())
  80. return ImagePath::builtin(campPrefix + "_BG.BMP");
  81. else
  82. return ImagePath::builtin(campBackground);
  83. }
  84. Point CampaignRegions::getPosition(CampaignScenarioID which) const
  85. {
  86. const auto & region = regions[which.getNum()];
  87. return region.pos;
  88. }
  89. std::optional<Point> CampaignRegions::getLabelPosition(CampaignScenarioID which) const
  90. {
  91. const auto & region = regions[which.getNum()];
  92. return region.labelPos;
  93. }
  94. ImagePath CampaignRegions::getNameFor(CampaignScenarioID which, int colorIndex, const std::string & type) const
  95. {
  96. const auto & region = regions[which.getNum()];
  97. static const std::array<std::array<std::string, 8>, 3> colors = {{
  98. { "", "", "", "", "", "", "", "" },
  99. { "R", "B", "N", "G", "O", "V", "T", "P" },
  100. { "Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi" }
  101. }};
  102. std::string color = colors[colorSuffixLength][colorIndex];
  103. return ImagePath::builtin(campPrefix + region.infix + "_" + type + color + ".BMP");
  104. }
  105. ImagePath CampaignRegions::getAvailableName(CampaignScenarioID which, int color) const
  106. {
  107. if(campSuffix.empty())
  108. return getNameFor(which, color, "En");
  109. else
  110. return getNameFor(which, color, campSuffix[0]);
  111. }
  112. ImagePath CampaignRegions::getSelectedName(CampaignScenarioID which, int color) const
  113. {
  114. if(campSuffix.empty())
  115. return getNameFor(which, color, "Se");
  116. else
  117. return getNameFor(which, color, campSuffix[1]);
  118. }
  119. ImagePath CampaignRegions::getConqueredName(CampaignScenarioID which, int color) const
  120. {
  121. if(campSuffix.empty())
  122. return getNameFor(which, color, "Co");
  123. else
  124. return getNameFor(which, color, campSuffix[2]);
  125. }
  126. int CampaignRegions::regionsCount() const
  127. {
  128. return regions.size();
  129. }