|
|
@@ -9,15 +9,93 @@
|
|
|
*/
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
+#include <vstd/ContainerUtils.h>
|
|
|
#include "CRmgTemplate.h"
|
|
|
|
|
|
#include "../mapping/CMap.h"
|
|
|
#include "../VCMI_Lib.h"
|
|
|
#include "../CTownHandler.h"
|
|
|
+#include "../serializer/JsonSerializeFormat.h"
|
|
|
+#include "../StringConstants.h"
|
|
|
+
|
|
|
+namespace
|
|
|
+{
|
|
|
+ si32 decodeZoneId(const std::string & json)
|
|
|
+ {
|
|
|
+ return boost::lexical_cast<si32>(json);
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string encodeZoneId(si32 id)
|
|
|
+ {
|
|
|
+ return boost::lexical_cast<std::string>(id);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+CTreasureInfo::CTreasureInfo()
|
|
|
+ : min(0),
|
|
|
+ max(0),
|
|
|
+ density(0)
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+bool CTreasureInfo::operator==(const CTreasureInfo & other) const
|
|
|
+{
|
|
|
+ return (min == other.min) && (max == other.max) && (density == other.density);
|
|
|
+}
|
|
|
+
|
|
|
+void CTreasureInfo::serializeJson(JsonSerializeFormat & handler)
|
|
|
+{
|
|
|
+ handler.serializeInt("min", min, 0);
|
|
|
+ handler.serializeInt("max", max, 0);
|
|
|
+ handler.serializeInt("density", density, 0);
|
|
|
+}
|
|
|
|
|
|
namespace rmg
|
|
|
{
|
|
|
|
|
|
+class TerrainEncoder
|
|
|
+{
|
|
|
+public:
|
|
|
+ static si32 decode(const std::string & identifier)
|
|
|
+ {
|
|
|
+ return vstd::find_pos(GameConstants::TERRAIN_NAMES, identifier);
|
|
|
+ }
|
|
|
+
|
|
|
+ static std::string encode(const si32 index)
|
|
|
+ {
|
|
|
+ return (index >=0 && index < GameConstants::TERRAIN_TYPES) ? GameConstants::TERRAIN_NAMES[index] : "<INVALID TERRAIN>";
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+class ZoneEncoder
|
|
|
+{
|
|
|
+public:
|
|
|
+ static si32 decode(const std::string & json)
|
|
|
+ {
|
|
|
+ return boost::lexical_cast<si32>(json);
|
|
|
+ }
|
|
|
+
|
|
|
+ static std::string encode(si32 id)
|
|
|
+ {
|
|
|
+ return boost::lexical_cast<std::string>(id);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+class FactionEncoder
|
|
|
+{
|
|
|
+public:
|
|
|
+ static si32 decode(const std::string & json)
|
|
|
+ {
|
|
|
+ return VLC->townh->decodeFaction(json);
|
|
|
+ }
|
|
|
+
|
|
|
+ static std::string encode(si32 id)
|
|
|
+ {
|
|
|
+ return VLC->townh->encodeFaction(id);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
const std::set<ETerrainType> ZoneOptions::DEFAULT_TERRAIN_TYPES =
|
|
|
{
|
|
|
ETerrainType::DIRT,
|
|
|
@@ -30,6 +108,8 @@ const std::set<ETerrainType> ZoneOptions::DEFAULT_TERRAIN_TYPES =
|
|
|
ETerrainType::LAVA
|
|
|
};
|
|
|
|
|
|
+const TRmgTemplateZoneId ZoneOptions::NO_ZONE = -1;
|
|
|
+
|
|
|
ZoneOptions::CTownInfo::CTownInfo()
|
|
|
: townCount(0),
|
|
|
castleCount(0),
|
|
|
@@ -44,49 +124,30 @@ int ZoneOptions::CTownInfo::getTownCount() const
|
|
|
return townCount;
|
|
|
}
|
|
|
|
|
|
-void ZoneOptions::CTownInfo::setTownCount(int value)
|
|
|
-{
|
|
|
- if(value < 0)
|
|
|
- throw std::runtime_error("Negative value for town count not allowed.");
|
|
|
- townCount = value;
|
|
|
-}
|
|
|
-
|
|
|
int ZoneOptions::CTownInfo::getCastleCount() const
|
|
|
{
|
|
|
return castleCount;
|
|
|
}
|
|
|
|
|
|
-void ZoneOptions::CTownInfo::setCastleCount(int value)
|
|
|
-{
|
|
|
- if(value < 0)
|
|
|
- throw std::runtime_error("Negative value for castle count not allowed.");
|
|
|
- castleCount = value;
|
|
|
-}
|
|
|
-
|
|
|
int ZoneOptions::CTownInfo::getTownDensity() const
|
|
|
{
|
|
|
return townDensity;
|
|
|
}
|
|
|
|
|
|
-void ZoneOptions::CTownInfo::setTownDensity(int value)
|
|
|
-{
|
|
|
- if(value < 0)
|
|
|
- throw std::runtime_error("Negative value for town density not allowed.");
|
|
|
- townDensity = value;
|
|
|
-}
|
|
|
-
|
|
|
int ZoneOptions::CTownInfo::getCastleDensity() const
|
|
|
{
|
|
|
return castleDensity;
|
|
|
}
|
|
|
|
|
|
-void ZoneOptions::CTownInfo::setCastleDensity(int value)
|
|
|
+void ZoneOptions::CTownInfo::serializeJson(JsonSerializeFormat & handler)
|
|
|
{
|
|
|
- if(value < 0)
|
|
|
- throw std::runtime_error("Negative value for castle density not allowed.");
|
|
|
- castleDensity = value;
|
|
|
+ handler.serializeInt("towns", townCount, 0);
|
|
|
+ handler.serializeInt("castles", castleCount, 0);
|
|
|
+ handler.serializeInt("townDensity", townDensity, 0);
|
|
|
+ handler.serializeInt("castleDensity", castleDensity, 0);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
ZoneOptions::ZoneOptions()
|
|
|
: id(0),
|
|
|
type(ETemplateZoneType::PLAYER_START),
|
|
|
@@ -102,7 +163,10 @@ ZoneOptions::ZoneOptions()
|
|
|
zoneMonsterStrength(EMonsterStrength::ZONE_NORMAL),
|
|
|
mines(),
|
|
|
treasureInfo(),
|
|
|
- connections()
|
|
|
+ connections(),
|
|
|
+ minesLikeZone(NO_ZONE),
|
|
|
+ terrainTypeLikeZone(NO_ZONE),
|
|
|
+ treasureLikeZone(NO_ZONE)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
@@ -124,6 +188,9 @@ ZoneOptions & ZoneOptions::operator=(const ZoneOptions & other)
|
|
|
mines = other.mines;
|
|
|
treasureInfo = other.treasureInfo;
|
|
|
connections = other.connections;
|
|
|
+ minesLikeZone = other.minesLikeZone;
|
|
|
+ terrainTypeLikeZone = other.terrainTypeLikeZone;
|
|
|
+ treasureLikeZone = other.treasureLikeZone;
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
@@ -143,10 +210,6 @@ ETemplateZoneType::ETemplateZoneType ZoneOptions::getType() const
|
|
|
{
|
|
|
return type;
|
|
|
}
|
|
|
-void ZoneOptions::setType(ETemplateZoneType::ETemplateZoneType value)
|
|
|
-{
|
|
|
- type = value;
|
|
|
-}
|
|
|
|
|
|
int ZoneOptions::getSize() const
|
|
|
{
|
|
|
@@ -155,8 +218,6 @@ int ZoneOptions::getSize() const
|
|
|
|
|
|
void ZoneOptions::setSize(int value)
|
|
|
{
|
|
|
- if(value <= 0)
|
|
|
- throw std::runtime_error(boost::to_string(boost::format("Zone %d size needs to be greater than 0.") % id));
|
|
|
size = value;
|
|
|
}
|
|
|
|
|
|
@@ -165,43 +226,6 @@ boost::optional<int> ZoneOptions::getOwner() const
|
|
|
return owner;
|
|
|
}
|
|
|
|
|
|
-void ZoneOptions::setOwner(boost::optional<int> value)
|
|
|
-{
|
|
|
- if(value && !(*value >= 0 && *value <= PlayerColor::PLAYER_LIMIT_I))
|
|
|
- throw std::runtime_error(boost::to_string(boost::format ("Owner of zone %d has to be in range 0 to max player count.") % id));
|
|
|
- owner = value;
|
|
|
-}
|
|
|
-
|
|
|
-const ZoneOptions::CTownInfo & ZoneOptions::getPlayerTowns() const
|
|
|
-{
|
|
|
- return playerTowns;
|
|
|
-}
|
|
|
-
|
|
|
-void ZoneOptions::setPlayerTowns(const CTownInfo & value)
|
|
|
-{
|
|
|
- playerTowns = value;
|
|
|
-}
|
|
|
-
|
|
|
-const ZoneOptions::CTownInfo & ZoneOptions::getNeutralTowns() const
|
|
|
-{
|
|
|
- return neutralTowns;
|
|
|
-}
|
|
|
-
|
|
|
-void ZoneOptions::setNeutralTowns(const CTownInfo & value)
|
|
|
-{
|
|
|
- neutralTowns = value;
|
|
|
-}
|
|
|
-
|
|
|
-bool ZoneOptions::getMatchTerrainToTown() const
|
|
|
-{
|
|
|
- return matchTerrainToTown;
|
|
|
-}
|
|
|
-
|
|
|
-void ZoneOptions::setMatchTerrainToTown(bool value)
|
|
|
-{
|
|
|
- matchTerrainToTown = value;
|
|
|
-}
|
|
|
-
|
|
|
const std::set<ETerrainType> & ZoneOptions::getTerrainTypes() const
|
|
|
{
|
|
|
return terrainTypes;
|
|
|
@@ -214,16 +238,6 @@ void ZoneOptions::setTerrainTypes(const std::set<ETerrainType> & value)
|
|
|
terrainTypes = value;
|
|
|
}
|
|
|
|
|
|
-bool ZoneOptions::getTownsAreSameType() const
|
|
|
-{
|
|
|
- return townsAreSameType;
|
|
|
-}
|
|
|
-
|
|
|
-void ZoneOptions::setTownsAreSameType(bool value)
|
|
|
-{
|
|
|
- townsAreSameType = value;
|
|
|
-}
|
|
|
-
|
|
|
std::set<TFaction> ZoneOptions::getDefaultTownTypes() const
|
|
|
{
|
|
|
std::set<TFaction> defaultTowns;
|
|
|
@@ -250,15 +264,9 @@ void ZoneOptions::setMonsterTypes(const std::set<TFaction> & value)
|
|
|
monsterTypes = value;
|
|
|
}
|
|
|
|
|
|
-void ZoneOptions::setMonsterStrength(EMonsterStrength::EMonsterStrength val)
|
|
|
+void ZoneOptions::setMinesInfo(const std::map<TResource, ui16> & value)
|
|
|
{
|
|
|
- assert (vstd::iswithin(val, EMonsterStrength::ZONE_WEAK, EMonsterStrength::ZONE_STRONG));
|
|
|
- zoneMonsterStrength = val;
|
|
|
-}
|
|
|
-
|
|
|
-void ZoneOptions::setMinesAmount(TResource res, ui16 amount)
|
|
|
-{
|
|
|
- mines[res] = amount;
|
|
|
+ mines = value;
|
|
|
}
|
|
|
|
|
|
std::map<TResource, ui16> ZoneOptions::getMinesInfo() const
|
|
|
@@ -266,9 +274,9 @@ std::map<TResource, ui16> ZoneOptions::getMinesInfo() const
|
|
|
return mines;
|
|
|
}
|
|
|
|
|
|
-void ZoneOptions::addTreasureInfo(const CTreasureInfo & info)
|
|
|
+void ZoneOptions::setTreasureInfo(const std::vector<CTreasureInfo> & value)
|
|
|
{
|
|
|
- treasureInfo.push_back(info);
|
|
|
+ treasureInfo = value;
|
|
|
}
|
|
|
|
|
|
const std::vector<CTreasureInfo> & ZoneOptions::getTreasureInfo() const
|
|
|
@@ -276,171 +284,165 @@ const std::vector<CTreasureInfo> & ZoneOptions::getTreasureInfo() const
|
|
|
return treasureInfo;
|
|
|
}
|
|
|
|
|
|
-void ZoneOptions::addConnection(TRmgTemplateZoneId otherZone)
|
|
|
+TRmgTemplateZoneId ZoneOptions::getMinesLikeZone() const
|
|
|
{
|
|
|
- connections.push_back (otherZone);
|
|
|
+ return minesLikeZone;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-ZoneConnection::ZoneConnection()
|
|
|
- : zoneA(-1),
|
|
|
- zoneB(-1),
|
|
|
- guardStrength(0)
|
|
|
+TRmgTemplateZoneId ZoneOptions::getTerrainTypeLikeZone() const
|
|
|
{
|
|
|
-
|
|
|
+ return terrainTypeLikeZone;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-TRmgTemplateZoneId ZoneConnection::getZoneA() const
|
|
|
+TRmgTemplateZoneId ZoneOptions::getTreasureLikeZone() const
|
|
|
{
|
|
|
- return zoneA;
|
|
|
+ return treasureLikeZone;
|
|
|
}
|
|
|
|
|
|
-void ZoneConnection::setZoneA(TRmgTemplateZoneId value)
|
|
|
+void ZoneOptions::addConnection(TRmgTemplateZoneId otherZone)
|
|
|
{
|
|
|
- zoneA = value;
|
|
|
+ connections.push_back (otherZone);
|
|
|
}
|
|
|
|
|
|
-TRmgTemplateZoneId ZoneConnection::getZoneB() const
|
|
|
+std::vector<TRmgTemplateZoneId> ZoneOptions::getConnections() const
|
|
|
{
|
|
|
- return zoneB;
|
|
|
+ return connections;
|
|
|
}
|
|
|
|
|
|
-void ZoneConnection::setZoneB(TRmgTemplateZoneId value)
|
|
|
+void ZoneOptions::serializeJson(JsonSerializeFormat & handler)
|
|
|
{
|
|
|
- zoneB = value;
|
|
|
-}
|
|
|
+ static const std::vector<std::string> zoneTypes =
|
|
|
+ {
|
|
|
+ "playerStart",
|
|
|
+ "cpuStart",
|
|
|
+ "treasure",
|
|
|
+ "junction"
|
|
|
+ };
|
|
|
|
|
|
-int ZoneConnection::getGuardStrength() const
|
|
|
-{
|
|
|
- return guardStrength;
|
|
|
-}
|
|
|
+ handler.serializeEnum("type", type, zoneTypes);
|
|
|
+ handler.serializeInt("size", size, 1);
|
|
|
+ handler.serializeInt("owner", owner);
|
|
|
+ handler.serializeStruct("playerTowns", playerTowns);
|
|
|
+ handler.serializeStruct("neutralTowns", neutralTowns);
|
|
|
+ handler.serializeBool("matchTerrainToTown", matchTerrainToTown, true);
|
|
|
|
|
|
-void ZoneConnection::setGuardStrength(int value)
|
|
|
-{
|
|
|
- if(value < 0) throw std::runtime_error("Negative value for guard strength not allowed.");
|
|
|
- guardStrength = value;
|
|
|
-}
|
|
|
+ #define SERIALIZE_ZONE_LINK(fieldName) handler.serializeInt(#fieldName, fieldName, NO_ZONE);
|
|
|
|
|
|
-}
|
|
|
+ SERIALIZE_ZONE_LINK(minesLikeZone);
|
|
|
+ SERIALIZE_ZONE_LINK(terrainTypeLikeZone);
|
|
|
+ SERIALIZE_ZONE_LINK(treasureLikeZone);
|
|
|
|
|
|
+ #undef SERIALIZE_ZONE_LINK
|
|
|
|
|
|
-using namespace rmg;//todo: remove
|
|
|
+ if(terrainTypeLikeZone == NO_ZONE)
|
|
|
+ handler.serializeIdArray<ETerrainType, TerrainEncoder>("terrainTypes", terrainTypes, DEFAULT_TERRAIN_TYPES);
|
|
|
|
|
|
+ handler.serializeBool("townsAreSameType", townsAreSameType, false);
|
|
|
+ handler.serializeIdArray<TFaction, FactionEncoder>("allowedMonsters", monsterTypes, VLC->townh->getAllowedFactions(false));
|
|
|
+ handler.serializeIdArray<TFaction, FactionEncoder>("allowedTowns", townTypes, VLC->townh->getAllowedFactions(true));
|
|
|
|
|
|
-CRmgTemplate::CSize::CSize() : width(CMapHeader::MAP_SIZE_MIDDLE), height(CMapHeader::MAP_SIZE_MIDDLE), under(true)
|
|
|
-{
|
|
|
+ {
|
|
|
+ //TODO: add support for std::map to serializeEnum
|
|
|
+ static const std::vector<std::string> STRENGTH =
|
|
|
+ {
|
|
|
+ "weak",
|
|
|
+ "normal",
|
|
|
+ "strong"
|
|
|
+ };
|
|
|
+
|
|
|
+ si32 rawStrength = 0;
|
|
|
+ if(handler.saving)
|
|
|
+ {
|
|
|
+ rawStrength = static_cast<decltype(rawStrength)>(zoneMonsterStrength);
|
|
|
+ rawStrength++;
|
|
|
+ }
|
|
|
+ handler.serializeEnum("monsters", rawStrength, STRENGTH);
|
|
|
+ if(!handler.saving)
|
|
|
+ {
|
|
|
+ rawStrength--;
|
|
|
+ zoneMonsterStrength = static_cast<decltype(zoneMonsterStrength)>(rawStrength);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
-}
|
|
|
+ if(treasureLikeZone == NO_ZONE)
|
|
|
+ {
|
|
|
+ auto treasureData = handler.enterArray("treasure");
|
|
|
+ treasureData.serializeStruct(treasureInfo);
|
|
|
+ }
|
|
|
|
|
|
-CRmgTemplate::CSize::CSize(int width, int height, bool under) : under(under)
|
|
|
-{
|
|
|
- setWidth(width);
|
|
|
- setHeight(height);
|
|
|
-}
|
|
|
+ if((minesLikeZone == NO_ZONE) && (!handler.saving || !mines.empty()))
|
|
|
+ {
|
|
|
+ auto minesData = handler.enterStruct("mines");
|
|
|
|
|
|
-int CRmgTemplate::CSize::getWidth() const
|
|
|
-{
|
|
|
- return width;
|
|
|
+ for(TResource idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
|
|
|
+ {
|
|
|
+ handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], mines[idx], 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-void CRmgTemplate::CSize::setWidth(int value)
|
|
|
+ZoneConnection::ZoneConnection()
|
|
|
+ : zoneA(-1),
|
|
|
+ zoneB(-1),
|
|
|
+ guardStrength(0)
|
|
|
{
|
|
|
- if(value <= 0) throw std::runtime_error("Width > 0 failed.");
|
|
|
- width = value;
|
|
|
-}
|
|
|
|
|
|
-int CRmgTemplate::CSize::getHeight() const
|
|
|
-{
|
|
|
- return height;
|
|
|
}
|
|
|
|
|
|
-void CRmgTemplate::CSize::setHeight(int value)
|
|
|
+TRmgTemplateZoneId ZoneConnection::getZoneA() const
|
|
|
{
|
|
|
- if(value <= 0) throw std::runtime_error("Height > 0 failed.");
|
|
|
- height = value;
|
|
|
+ return zoneA;
|
|
|
}
|
|
|
|
|
|
-bool CRmgTemplate::CSize::getUnder() const
|
|
|
+TRmgTemplateZoneId ZoneConnection::getZoneB() const
|
|
|
{
|
|
|
- return under;
|
|
|
+ return zoneB;
|
|
|
}
|
|
|
|
|
|
-void CRmgTemplate::CSize::setUnder(bool value)
|
|
|
+int ZoneConnection::getGuardStrength() const
|
|
|
{
|
|
|
- under = value;
|
|
|
+ return guardStrength;
|
|
|
}
|
|
|
|
|
|
-bool CRmgTemplate::CSize::operator<=(const CSize & value) const
|
|
|
+void ZoneConnection::serializeJson(JsonSerializeFormat & handler)
|
|
|
{
|
|
|
- if(width < value.width && height < value.height)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- else if(width == value.width && height == value.height)
|
|
|
- {
|
|
|
- return under ? value.under : true;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ handler.serializeId<TRmgTemplateZoneId, TRmgTemplateZoneId, ZoneEncoder>("a", zoneA, -1);
|
|
|
+ handler.serializeId<TRmgTemplateZoneId, TRmgTemplateZoneId, ZoneEncoder>("b", zoneB, -1);
|
|
|
+ handler.serializeInt("guard", guardStrength, 0);
|
|
|
}
|
|
|
|
|
|
-bool CRmgTemplate::CSize::operator>=(const CSize & value) const
|
|
|
-{
|
|
|
- if(width > value.width && height > value.height)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- else if(width == value.width && height == value.height)
|
|
|
- {
|
|
|
- return under ? true : !value.under;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
+using namespace rmg;//todo: remove
|
|
|
+
|
|
|
CRmgTemplate::CRmgTemplate()
|
|
|
+ : minSize(72, 72, 2),
|
|
|
+ maxSize(72, 72, 2)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
CRmgTemplate::~CRmgTemplate()
|
|
|
{
|
|
|
- for (auto & pair : zones) delete pair.second;
|
|
|
}
|
|
|
|
|
|
-const std::string & CRmgTemplate::getName() const
|
|
|
+bool CRmgTemplate::matchesSize(const int3 & value) const
|
|
|
{
|
|
|
- return name;
|
|
|
-}
|
|
|
+ const int64_t square = value.x * value.y * value.z;
|
|
|
+ const int64_t minSquare = minSize.x * minSize.y * minSize.z;
|
|
|
+ const int64_t maxSquare = maxSize.x * maxSize.y * maxSize.z;
|
|
|
|
|
|
-void CRmgTemplate::setName(const std::string & value)
|
|
|
-{
|
|
|
- name = value;
|
|
|
+ return minSquare <= square && square <= maxSquare;
|
|
|
}
|
|
|
|
|
|
-const CRmgTemplate::CSize & CRmgTemplate::getMinSize() const
|
|
|
+void CRmgTemplate::setId(const std::string & value)
|
|
|
{
|
|
|
- return minSize;
|
|
|
-}
|
|
|
-
|
|
|
-void CRmgTemplate::setMinSize(const CSize & value)
|
|
|
-{
|
|
|
- minSize = value;
|
|
|
-}
|
|
|
-
|
|
|
-const CRmgTemplate::CSize & CRmgTemplate::getMaxSize() const
|
|
|
-{
|
|
|
- return maxSize;
|
|
|
+ id = value;
|
|
|
}
|
|
|
|
|
|
-void CRmgTemplate::setMaxSize(const CSize & value)
|
|
|
+const std::string & CRmgTemplate::getName() const
|
|
|
{
|
|
|
- maxSize = value;
|
|
|
+ return name.empty() ? id : name;
|
|
|
}
|
|
|
|
|
|
const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getPlayers() const
|
|
|
@@ -448,41 +450,21 @@ const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getPlayers() const
|
|
|
return players;
|
|
|
}
|
|
|
|
|
|
-void CRmgTemplate::setPlayers(const CPlayerCountRange & value)
|
|
|
-{
|
|
|
- players = value;
|
|
|
-}
|
|
|
-
|
|
|
const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getCpuPlayers() const
|
|
|
{
|
|
|
return cpuPlayers;
|
|
|
}
|
|
|
|
|
|
-void CRmgTemplate::setCpuPlayers(const CPlayerCountRange & value)
|
|
|
-{
|
|
|
- cpuPlayers = value;
|
|
|
-}
|
|
|
-
|
|
|
-const std::map<TRmgTemplateZoneId, ZoneOptions *> & CRmgTemplate::getZones() const
|
|
|
+const CRmgTemplate::Zones & CRmgTemplate::getZones() const
|
|
|
{
|
|
|
return zones;
|
|
|
}
|
|
|
|
|
|
-void CRmgTemplate::setZones(const std::map<TRmgTemplateZoneId, ZoneOptions *> & value)
|
|
|
-{
|
|
|
- zones = value;
|
|
|
-}
|
|
|
-
|
|
|
-const std::list<ZoneConnection> & CRmgTemplate::getConnections() const
|
|
|
+const std::vector<ZoneConnection> & CRmgTemplate::getConnections() const
|
|
|
{
|
|
|
return connections;
|
|
|
}
|
|
|
|
|
|
-void CRmgTemplate::setConnections(const std::list<ZoneConnection> & value)
|
|
|
-{
|
|
|
- connections = value;
|
|
|
-}
|
|
|
-
|
|
|
void CRmgTemplate::validate() const
|
|
|
{
|
|
|
//TODO add some validation checks, throw on failure
|
|
|
@@ -516,3 +498,212 @@ std::set<int> CRmgTemplate::CPlayerCountRange::getNumbers() const
|
|
|
}
|
|
|
return numbers;
|
|
|
}
|
|
|
+
|
|
|
+std::string CRmgTemplate::CPlayerCountRange::toString() const
|
|
|
+{
|
|
|
+ if(range.size() == 1)
|
|
|
+ {
|
|
|
+ const auto & p = range.front();
|
|
|
+ if((p.first == p.second) && (p.first == 0))
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string ret;
|
|
|
+
|
|
|
+ bool first = true;
|
|
|
+
|
|
|
+ for(auto & p : range)
|
|
|
+ {
|
|
|
+ if(!first)
|
|
|
+ ret +=",";
|
|
|
+ else
|
|
|
+ first = false;
|
|
|
+
|
|
|
+ if(p.first == p.second)
|
|
|
+ {
|
|
|
+ ret += boost::lexical_cast<std::string>(p.first);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ret += boost::to_string(boost::format("%d-%d") % p.first % p.second);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+void CRmgTemplate::CPlayerCountRange::fromString(const std::string & value)
|
|
|
+{
|
|
|
+ range.clear();
|
|
|
+
|
|
|
+ if(value.empty())
|
|
|
+ {
|
|
|
+ addNumber(0);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ std::vector<std::string> commaParts;
|
|
|
+ boost::split(commaParts, value, boost::is_any_of(","));
|
|
|
+ for(const auto & commaPart : commaParts)
|
|
|
+ {
|
|
|
+ std::vector<std::string> rangeParts;
|
|
|
+ boost::split(rangeParts, commaPart, boost::is_any_of("-"));
|
|
|
+ if(rangeParts.size() == 2)
|
|
|
+ {
|
|
|
+ auto lower = boost::lexical_cast<int>(rangeParts[0]);
|
|
|
+ auto upper = boost::lexical_cast<int>(rangeParts[1]);
|
|
|
+ addRange(lower, upper);
|
|
|
+ }
|
|
|
+ else if(rangeParts.size() == 1)
|
|
|
+ {
|
|
|
+ auto val = boost::lexical_cast<int>(rangeParts.front());
|
|
|
+ addNumber(val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void CRmgTemplate::serializeJson(JsonSerializeFormat & handler)
|
|
|
+{
|
|
|
+ handler.serializeString("name", name);
|
|
|
+ serializeSize(handler, minSize, "minSize");
|
|
|
+ serializeSize(handler, maxSize, "maxSize");
|
|
|
+ serializePlayers(handler, players, "players");
|
|
|
+ serializePlayers(handler, cpuPlayers, "cpu");
|
|
|
+
|
|
|
+ {
|
|
|
+ auto connectionsData = handler.enterArray("connections");
|
|
|
+ connectionsData.serializeStruct(connections);
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ auto zonesData = handler.enterStruct("zones");
|
|
|
+ if(handler.saving)
|
|
|
+ {
|
|
|
+ for(auto & idAndZone : zones)
|
|
|
+ {
|
|
|
+ auto guard = handler.enterStruct(encodeZoneId(idAndZone.first));
|
|
|
+ idAndZone.second->serializeJson(handler);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ for(auto & idAndZone : zonesData->getCurrent().Struct())
|
|
|
+ {
|
|
|
+ auto guard = handler.enterStruct(idAndZone.first);
|
|
|
+ auto zone = std::make_shared<ZoneOptions>();
|
|
|
+ zone->setId(decodeZoneId(idAndZone.first));
|
|
|
+ zone->serializeJson(handler);
|
|
|
+ zones[zone->getId()] = zone;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!handler.saving)
|
|
|
+ afterLoad();
|
|
|
+}
|
|
|
+
|
|
|
+void CRmgTemplate::afterLoad()
|
|
|
+{
|
|
|
+ for(auto & idAndZone : zones)
|
|
|
+ {
|
|
|
+ auto zone = idAndZone.second;
|
|
|
+ if(zone->getMinesLikeZone() != ZoneOptions::NO_ZONE)
|
|
|
+ {
|
|
|
+ const auto otherZone = zones.at(zone->getMinesLikeZone());
|
|
|
+ zone->setMinesInfo(otherZone->getMinesInfo());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(zone->getTerrainTypeLikeZone() != ZoneOptions::NO_ZONE)
|
|
|
+ {
|
|
|
+ const auto otherZone = zones.at(zone->getTerrainTypeLikeZone());
|
|
|
+ zone->setTerrainTypes(otherZone->getTerrainTypes());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(zone->getTreasureLikeZone() != ZoneOptions::NO_ZONE)
|
|
|
+ {
|
|
|
+ const auto otherZone = zones.at(zone->getTreasureLikeZone());
|
|
|
+ zone->setTreasureInfo(otherZone->getTreasureInfo());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for(const auto & connection : connections)
|
|
|
+ {
|
|
|
+ auto id1 = connection.getZoneA();
|
|
|
+ auto id2 = connection.getZoneB();
|
|
|
+
|
|
|
+ auto zone1 = zones.at(id1);
|
|
|
+ auto zone2 = zones.at(id2);
|
|
|
+
|
|
|
+ zone1->addConnection(id2);
|
|
|
+ zone2->addConnection(id1);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void CRmgTemplate::serializeSize(JsonSerializeFormat & handler, int3 & value, const std::string & fieldName)
|
|
|
+{
|
|
|
+ static const std::map<std::string, int3> sizeMapping =
|
|
|
+ {
|
|
|
+ {"s", { 36, 36, 1}},
|
|
|
+ {"s+u", { 36, 36, 2}},
|
|
|
+ {"m", { 72, 72, 1}},
|
|
|
+ {"m+u", { 72, 72, 2}},
|
|
|
+ {"l", {108, 108, 1}},
|
|
|
+ {"l+u", {108, 108, 2}},
|
|
|
+ {"xl", {144, 144, 1}},
|
|
|
+ {"xl+u", {144, 144, 2}},
|
|
|
+ {"h", {180, 180, 1}},
|
|
|
+ {"h+u", {180, 180, 2}},
|
|
|
+ {"xh", {216, 216, 1}},
|
|
|
+ {"xh+u", {216, 216, 2}},
|
|
|
+ {"g", {252, 252, 1}},
|
|
|
+ {"g+u", {252, 252, 2}}
|
|
|
+ };
|
|
|
+
|
|
|
+ static const std::map<int3, std::string> sizeReverseMapping = vstd::invertMap(sizeMapping);
|
|
|
+
|
|
|
+ std::string encodedValue;
|
|
|
+
|
|
|
+ if(handler.saving)
|
|
|
+ {
|
|
|
+ auto iter = sizeReverseMapping.find(value);
|
|
|
+ if(iter == sizeReverseMapping.end())
|
|
|
+ encodedValue = boost::str(boost::format("%dx%dx%d") % value.x % value.y % value.z);
|
|
|
+ else
|
|
|
+ encodedValue = iter->second;
|
|
|
+ }
|
|
|
+
|
|
|
+ handler.serializeString(fieldName, encodedValue);
|
|
|
+
|
|
|
+ if(!handler.saving)
|
|
|
+ {
|
|
|
+ auto iter = sizeMapping.find(encodedValue);
|
|
|
+
|
|
|
+ if(iter == sizeMapping.end())
|
|
|
+ {
|
|
|
+ std::vector<std::string> parts;
|
|
|
+ boost::split(parts, encodedValue, boost::is_any_of("x"));
|
|
|
+
|
|
|
+ value.x = (boost::lexical_cast<int>(parts.at(0)));
|
|
|
+ value.y = (boost::lexical_cast<int>(parts.at(1)));
|
|
|
+ value.z = (boost::lexical_cast<int>(parts.at(2)));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ value = iter->second;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void CRmgTemplate::serializePlayers(JsonSerializeFormat & handler, CPlayerCountRange & value, const std::string & fieldName)
|
|
|
+{
|
|
|
+ std::string encodedValue;
|
|
|
+
|
|
|
+ if(handler.saving)
|
|
|
+ encodedValue = value.toString();
|
|
|
+
|
|
|
+ handler.serializeString(fieldName, encodedValue);
|
|
|
+
|
|
|
+ if(!handler.saving)
|
|
|
+ value.fromString(encodedValue);
|
|
|
+}
|
|
|
+
|