2013-08-17 15:46:48 +03:00
|
|
|
/*
|
|
|
|
* CRmgTemplate.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
2018-03-09 20:11:20 +02:00
|
|
|
#include <vstd/ContainerUtils.h>
|
2022-06-05 08:02:58 +02:00
|
|
|
#include <boost/bimap.hpp>
|
2013-08-17 15:46:48 +03:00
|
|
|
#include "CRmgTemplate.h"
|
|
|
|
|
|
|
|
#include "../mapping/CMap.h"
|
2018-03-05 16:05:17 +02:00
|
|
|
#include "../VCMI_Lib.h"
|
|
|
|
#include "../CTownHandler.h"
|
2022-12-20 18:35:40 +02:00
|
|
|
#include "../CModHandler.h"
|
2023-01-09 01:17:37 +02:00
|
|
|
#include "../TerrainHandler.h"
|
2018-03-09 20:11:20 +02:00
|
|
|
#include "../serializer/JsonSerializeFormat.h"
|
|
|
|
#include "../StringConstants.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
si32 decodeZoneId(const std::string & json)
|
|
|
|
{
|
|
|
|
return boost::lexical_cast<si32>(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string encodeZoneId(si32 id)
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
return std::to_string(id);
|
2018-03-09 20:11:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CTreasureInfo::CTreasureInfo()
|
|
|
|
: min(0),
|
|
|
|
max(0),
|
|
|
|
density(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-31 11:25:39 +02:00
|
|
|
CTreasureInfo::CTreasureInfo(ui32 imin, ui32 imax, ui16 idensity)
|
|
|
|
: min(imin), max(imax), density(idensity)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
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);
|
|
|
|
}
|
2013-08-17 15:46:48 +03:00
|
|
|
|
2018-03-05 16:05:17 +02:00
|
|
|
namespace rmg
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
class TerrainEncoder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static si32 decode(const std::string & identifier)
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
return *VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), "terrain", identifier);
|
2018-03-09 20:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string encode(const si32 index)
|
|
|
|
{
|
2023-01-01 22:42:28 +02:00
|
|
|
return VLC->terrainTypeHandler->getByIndex(index)->getJsonKey();
|
2018-03-09 20:11:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ZoneEncoder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static si32 decode(const std::string & json)
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
return std::stoi(json);
|
2018-03-09 20:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string encode(si32 id)
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
return std::to_string(id);
|
2018-03-09 20:11:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const TRmgTemplateZoneId ZoneOptions::NO_ZONE = -1;
|
|
|
|
|
2018-03-05 16:05:17 +02:00
|
|
|
ZoneOptions::CTownInfo::CTownInfo()
|
|
|
|
: townCount(0),
|
|
|
|
castleCount(0),
|
|
|
|
townDensity(0),
|
|
|
|
castleDensity(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int ZoneOptions::CTownInfo::getTownCount() const
|
|
|
|
{
|
|
|
|
return townCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ZoneOptions::CTownInfo::getCastleCount() const
|
|
|
|
{
|
|
|
|
return castleCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ZoneOptions::CTownInfo::getTownDensity() const
|
|
|
|
{
|
|
|
|
return townDensity;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ZoneOptions::CTownInfo::getCastleDensity() const
|
|
|
|
{
|
|
|
|
return castleDensity;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
void ZoneOptions::CTownInfo::serializeJson(JsonSerializeFormat & handler)
|
2018-03-05 16:05:17 +02:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
handler.serializeInt("towns", townCount, 0);
|
|
|
|
handler.serializeInt("castles", castleCount, 0);
|
|
|
|
handler.serializeInt("townDensity", townDensity, 0);
|
|
|
|
handler.serializeInt("castleDensity", castleDensity, 0);
|
2018-03-05 16:05:17 +02:00
|
|
|
}
|
|
|
|
|
2023-02-11 18:05:02 +02:00
|
|
|
ZoneOptions::ZoneOptions():
|
|
|
|
id(0),
|
2018-03-05 16:05:17 +02:00
|
|
|
type(ETemplateZoneType::PLAYER_START),
|
|
|
|
size(1),
|
|
|
|
owner(boost::none),
|
|
|
|
matchTerrainToTown(true),
|
|
|
|
townsAreSameType(false),
|
|
|
|
zoneMonsterStrength(EMonsterStrength::ZONE_NORMAL),
|
2018-03-09 20:11:20 +02:00
|
|
|
minesLikeZone(NO_ZONE),
|
|
|
|
terrainTypeLikeZone(NO_ZONE),
|
|
|
|
treasureLikeZone(NO_ZONE)
|
2018-03-05 16:05:17 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
for(const auto & terr : VLC->terrainTypeHandler->objects)
|
|
|
|
if(terr->isLand() && terr->isPassable())
|
2023-01-01 17:10:47 +02:00
|
|
|
terrainTypes.insert(terr->getId());
|
2018-03-05 16:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TRmgTemplateZoneId ZoneOptions::getId() const
|
|
|
|
{
|
|
|
|
return id;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-05 16:05:17 +02:00
|
|
|
void ZoneOptions::setId(TRmgTemplateZoneId value)
|
|
|
|
{
|
|
|
|
if(value <= 0)
|
|
|
|
throw std::runtime_error(boost::to_string(boost::format("Zone %d id should be greater than 0.") % id));
|
|
|
|
id = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
ETemplateZoneType::ETemplateZoneType ZoneOptions::getType() const
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
2022-05-31 11:25:39 +02:00
|
|
|
|
|
|
|
void ZoneOptions::setType(ETemplateZoneType::ETemplateZoneType value)
|
|
|
|
{
|
|
|
|
type = value;
|
|
|
|
}
|
2018-03-05 16:05:17 +02:00
|
|
|
|
|
|
|
int ZoneOptions::getSize() const
|
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoneOptions::setSize(int value)
|
|
|
|
{
|
|
|
|
size = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<int> ZoneOptions::getOwner() const
|
|
|
|
{
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:44:46 +02:00
|
|
|
const std::set<TerrainId> & ZoneOptions::getTerrainTypes() const
|
2018-03-05 16:05:17 +02:00
|
|
|
{
|
|
|
|
return terrainTypes;
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:44:46 +02:00
|
|
|
void ZoneOptions::setTerrainTypes(const std::set<TerrainId> & value)
|
2018-03-05 16:05:17 +02:00
|
|
|
{
|
2023-01-10 20:09:09 +02:00
|
|
|
//assert(value.find(ETerrainType::NONE) == value.end() &&
|
2022-06-20 16:39:50 +02:00
|
|
|
// value.find(ETerrainType::WATER) == value.end() && value.find(ETerrainType::ROCK) == value.end());
|
2018-03-05 16:05:17 +02:00
|
|
|
terrainTypes = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<TFaction> ZoneOptions::getDefaultTownTypes() const
|
|
|
|
{
|
|
|
|
std::set<TFaction> defaultTowns;
|
|
|
|
auto towns = VLC->townh->getDefaultAllowed();
|
|
|
|
for(int i = 0; i < towns.size(); ++i)
|
|
|
|
{
|
|
|
|
if(towns[i]) defaultTowns.insert(i);
|
|
|
|
}
|
|
|
|
return defaultTowns;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::set<TFaction> & ZoneOptions::getTownTypes() const
|
|
|
|
{
|
|
|
|
return townTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoneOptions::setTownTypes(const std::set<TFaction> & value)
|
|
|
|
{
|
|
|
|
townTypes = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoneOptions::setMonsterTypes(const std::set<TFaction> & value)
|
|
|
|
{
|
|
|
|
monsterTypes = value;
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
const std::set<TFaction> & ZoneOptions::getMonsterTypes() const
|
|
|
|
{
|
|
|
|
return monsterTypes;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
void ZoneOptions::setMinesInfo(const std::map<TResource, ui16> & value)
|
2018-03-05 16:05:17 +02:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
mines = value;
|
2018-03-05 16:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::map<TResource, ui16> ZoneOptions::getMinesInfo() const
|
|
|
|
{
|
|
|
|
return mines;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
void ZoneOptions::setTreasureInfo(const std::vector<CTreasureInfo> & value)
|
2018-03-05 16:05:17 +02:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
treasureInfo = value;
|
2018-03-05 16:05:17 +02:00
|
|
|
}
|
2022-05-31 11:25:39 +02:00
|
|
|
|
|
|
|
void ZoneOptions::addTreasureInfo(const CTreasureInfo & value)
|
|
|
|
{
|
|
|
|
treasureInfo.push_back(value);
|
|
|
|
}
|
2018-03-05 16:05:17 +02:00
|
|
|
|
|
|
|
const std::vector<CTreasureInfo> & ZoneOptions::getTreasureInfo() const
|
|
|
|
{
|
|
|
|
return treasureInfo;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
TRmgTemplateZoneId ZoneOptions::getMinesLikeZone() const
|
2018-03-05 16:05:17 +02:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
return minesLikeZone;
|
2018-03-05 16:05:17 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
TRmgTemplateZoneId ZoneOptions::getTerrainTypeLikeZone() const
|
2018-03-05 16:05:17 +02:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
return terrainTypeLikeZone;
|
2018-03-05 16:05:17 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
TRmgTemplateZoneId ZoneOptions::getTreasureLikeZone() const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
return treasureLikeZone;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
void ZoneOptions::addConnection(TRmgTemplateZoneId otherZone)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
connections.push_back (otherZone);
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
std::vector<TRmgTemplateZoneId> ZoneOptions::getConnections() const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
return connections;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
bool ZoneOptions::areTownsSameType() const
|
|
|
|
{
|
|
|
|
return townsAreSameType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ZoneOptions::isMatchTerrainToTown() const
|
|
|
|
{
|
|
|
|
return matchTerrainToTown;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ZoneOptions::CTownInfo & ZoneOptions::getPlayerTowns() const
|
|
|
|
{
|
|
|
|
return playerTowns;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ZoneOptions::CTownInfo & ZoneOptions::getNeutralTowns() const
|
|
|
|
{
|
|
|
|
return neutralTowns;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
void ZoneOptions::serializeJson(JsonSerializeFormat & handler)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
static const std::vector<std::string> zoneTypes =
|
|
|
|
{
|
|
|
|
"playerStart",
|
|
|
|
"cpuStart",
|
|
|
|
"treasure",
|
2022-05-31 11:25:39 +02:00
|
|
|
"junction",
|
|
|
|
"water"
|
2018-03-09 20:11:20 +02:00
|
|
|
};
|
2013-08-17 15:46:48 +03:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
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);
|
2013-08-17 15:46:48 +03:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
#define SERIALIZE_ZONE_LINK(fieldName) handler.serializeInt(#fieldName, fieldName, NO_ZONE);
|
2013-08-17 15:46:48 +03:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
SERIALIZE_ZONE_LINK(minesLikeZone);
|
|
|
|
SERIALIZE_ZONE_LINK(terrainTypeLikeZone);
|
|
|
|
SERIALIZE_ZONE_LINK(treasureLikeZone);
|
2018-03-05 16:05:17 +02:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
#undef SERIALIZE_ZONE_LINK
|
2018-03-05 16:05:17 +02:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
if(terrainTypeLikeZone == NO_ZONE)
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
|
|
|
JsonNode node;
|
|
|
|
if(handler.saving)
|
|
|
|
{
|
|
|
|
node.setType(JsonNode::JsonType::DATA_VECTOR);
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & ttype : terrainTypes)
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
|
|
|
JsonNode n;
|
2023-01-01 22:42:28 +02:00
|
|
|
n.String() = VLC->terrainTypeHandler->getById(ttype)->getJsonKey();
|
2022-06-20 16:39:50 +02:00
|
|
|
node.Vector().push_back(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler.serializeRaw("terrainTypes", node, boost::none);
|
|
|
|
if(!handler.saving)
|
|
|
|
{
|
|
|
|
if(!node.Vector().empty())
|
|
|
|
{
|
|
|
|
terrainTypes.clear();
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & ttype : node.Vector())
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-12-20 20:26:54 +02:00
|
|
|
VLC->modh->identifiers.requestIdentifier("terrain", ttype, [this](int32_t identifier)
|
|
|
|
{
|
|
|
|
terrainTypes.emplace(identifier);
|
|
|
|
});
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 16:05:17 +02:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
handler.serializeBool("townsAreSameType", townsAreSameType, false);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
handler.serializeIdArray<TFaction, FactionID>("allowedMonsters", monsterTypes, VLC->townh->getAllowedFactions(false));
|
|
|
|
handler.serializeIdArray<TFaction, FactionID>("allowedTowns", townTypes, VLC->townh->getAllowedFactions(true));
|
2018-03-05 16:05:17 +02:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
{
|
|
|
|
//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++;
|
|
|
|
}
|
2022-06-05 08:02:58 +02:00
|
|
|
handler.serializeEnum("monsters", rawStrength, EMonsterStrength::ZONE_NORMAL + 1, STRENGTH);
|
2018-03-09 20:11:20 +02:00
|
|
|
if(!handler.saving)
|
|
|
|
{
|
|
|
|
rawStrength--;
|
|
|
|
zoneMonsterStrength = static_cast<decltype(zoneMonsterStrength)>(rawStrength);
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 15:46:48 +03:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
if(treasureLikeZone == NO_ZONE)
|
|
|
|
{
|
|
|
|
auto treasureData = handler.enterArray("treasure");
|
|
|
|
treasureData.serializeStruct(treasureInfo);
|
|
|
|
}
|
2013-08-17 15:46:48 +03:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
if((minesLikeZone == NO_ZONE) && (!handler.saving || !mines.empty()))
|
|
|
|
{
|
|
|
|
auto minesData = handler.enterStruct("mines");
|
2013-08-17 15:46:48 +03:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
for(TResource idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
|
|
|
|
{
|
|
|
|
handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], mines[idx], 0);
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
ZoneConnection::ZoneConnection()
|
|
|
|
: zoneA(-1),
|
|
|
|
zoneB(-1),
|
|
|
|
guardStrength(0)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
TRmgTemplateZoneId ZoneConnection::getZoneA() const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
return zoneA;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
TRmgTemplateZoneId ZoneConnection::getZoneB() const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
return zoneB;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
int ZoneConnection::getGuardStrength() const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
return guardStrength;
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const ZoneConnection & l, const ZoneConnection & r)
|
|
|
|
{
|
|
|
|
return l.zoneA == r.zoneA && l.zoneB == r.zoneB && l.guardStrength == r.guardStrength;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
void ZoneConnection::serializeJson(JsonSerializeFormat & handler)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
handler.serializeId<TRmgTemplateZoneId, TRmgTemplateZoneId, ZoneEncoder>("a", zoneA, -1);
|
|
|
|
handler.serializeId<TRmgTemplateZoneId, TRmgTemplateZoneId, ZoneEncoder>("b", zoneB, -1);
|
|
|
|
handler.serializeInt("guard", guardStrength, 0);
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
using namespace rmg;//todo: remove
|
|
|
|
|
2013-08-17 15:46:48 +03:00
|
|
|
CRmgTemplate::CRmgTemplate()
|
2018-03-09 20:11:20 +02:00
|
|
|
: minSize(72, 72, 2),
|
|
|
|
maxSize(72, 72, 2)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
bool CRmgTemplate::matchesSize(const int3 & value) const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
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;
|
2013-08-17 15:46:48 +03:00
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
return minSquare <= square && square <= maxSquare;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2022-05-31 11:25:39 +02:00
|
|
|
bool CRmgTemplate::isWaterContentAllowed(EWaterContent::EWaterContent waterContent) const
|
|
|
|
{
|
|
|
|
return waterContent == EWaterContent::EWaterContent::RANDOM || allowedWaterContent.count(waterContent);
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:02:58 +02:00
|
|
|
const std::set<EWaterContent::EWaterContent> & CRmgTemplate::getWaterContentAllowed() const
|
|
|
|
{
|
|
|
|
return allowedWaterContent;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
void CRmgTemplate::setId(const std::string & value)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
id = value;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
2022-12-14 02:37:11 +02:00
|
|
|
void CRmgTemplate::setName(const std::string & value)
|
|
|
|
{
|
|
|
|
name = value;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
const std::string & CRmgTemplate::getName() const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2022-12-14 02:37:11 +02:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & CRmgTemplate::getId() const
|
|
|
|
{
|
|
|
|
return id;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getPlayers() const
|
|
|
|
{
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getCpuPlayers() const
|
|
|
|
{
|
|
|
|
return cpuPlayers;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
const CRmgTemplate::Zones & CRmgTemplate::getZones() const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
return zones;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
const std::vector<ZoneConnection> & CRmgTemplate::getConnections() const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
return connections;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRmgTemplate::validate() const
|
|
|
|
{
|
|
|
|
//TODO add some validation checks, throw on failure
|
|
|
|
}
|
|
|
|
|
2022-12-14 02:37:11 +02:00
|
|
|
std::pair<int3, int3> CRmgTemplate::getMapSizes() const
|
|
|
|
{
|
|
|
|
return {minSize, maxSize};
|
|
|
|
}
|
|
|
|
|
2013-08-17 15:46:48 +03:00
|
|
|
void CRmgTemplate::CPlayerCountRange::addRange(int lower, int upper)
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
range.emplace_back(lower, upper);
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRmgTemplate::CPlayerCountRange::addNumber(int value)
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
range.emplace_back(value, value);
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CRmgTemplate::CPlayerCountRange::isInRange(int count) const
|
|
|
|
{
|
|
|
|
for(const auto & pair : range)
|
|
|
|
{
|
|
|
|
if(count >= pair.first && count <= pair.second) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<int> CRmgTemplate::CPlayerCountRange::getNumbers() const
|
|
|
|
{
|
|
|
|
std::set<int> numbers;
|
|
|
|
for(const auto & pair : range)
|
|
|
|
{
|
|
|
|
for(int i = pair.first; i <= pair.second; ++i) numbers.insert(i);
|
|
|
|
}
|
|
|
|
return numbers;
|
|
|
|
}
|
2018-03-09 20:11:20 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & p : range)
|
2018-03-09 20:11:20 +02:00
|
|
|
{
|
|
|
|
if(!first)
|
|
|
|
ret +=",";
|
|
|
|
else
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
if(p.first == p.second)
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
ret += std::to_string(p.first);
|
2018-03-09 20:11:20 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2022-06-05 08:02:58 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
boost::bimap<EWaterContent::EWaterContent, std::string> enc;
|
|
|
|
enc.insert({EWaterContent::NONE, "none"});
|
|
|
|
enc.insert({EWaterContent::NORMAL, "normal"});
|
|
|
|
enc.insert({EWaterContent::ISLANDS, "islands"});
|
|
|
|
JsonNode node;
|
|
|
|
if(handler.saving)
|
|
|
|
{
|
|
|
|
node.setType(JsonNode::JsonType::DATA_VECTOR);
|
|
|
|
for(auto wc : allowedWaterContent)
|
|
|
|
{
|
|
|
|
JsonNode n;
|
|
|
|
n.String() = enc.left.at(wc);
|
|
|
|
node.Vector().push_back(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler.serializeRaw("allowedWaterContent", node, boost::none);
|
|
|
|
if(!handler.saving)
|
|
|
|
{
|
|
|
|
for(auto wc : node.Vector())
|
|
|
|
{
|
|
|
|
allowedWaterContent.insert(enc.right.at(std::string(wc.String())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 20:11:20 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
auto zonesData = handler.enterStruct("zones");
|
2023-02-11 18:05:02 +02:00
|
|
|
if(handler.saving)
|
2018-03-09 20:11:20 +02:00
|
|
|
{
|
|
|
|
for(auto & idAndZone : zones)
|
|
|
|
{
|
|
|
|
auto guard = handler.enterStruct(encodeZoneId(idAndZone.first));
|
|
|
|
idAndZone.second->serializeJson(handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & idAndZone : zonesData->getCurrent().Struct())
|
2018-03-09 20:11:20 +02:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-03-19 10:27:05 +02:00
|
|
|
std::set<TerrainId> CRmgTemplate::inheritTerrainType(std::shared_ptr<ZoneOptions> zone, uint32_t iteration /* = 0 */)
|
|
|
|
{
|
|
|
|
if (iteration >= 50)
|
|
|
|
{
|
|
|
|
logGlobal->error("Infinite recursion for terrain types detected in template %s", name);
|
|
|
|
return std::set<TerrainId>();
|
|
|
|
}
|
|
|
|
if (zone->getTerrainTypeLikeZone() != ZoneOptions::NO_ZONE)
|
|
|
|
{
|
|
|
|
iteration++;
|
|
|
|
const auto otherZone = zones.at(zone->getTerrainTypeLikeZone());
|
|
|
|
zone->setTerrainTypes(inheritTerrainType(otherZone, iteration));
|
|
|
|
}
|
|
|
|
return zone->getTerrainTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<TResource, ui16> CRmgTemplate::inheritMineTypes(std::shared_ptr<ZoneOptions> zone, uint32_t iteration /* = 0 */)
|
|
|
|
{
|
|
|
|
if (iteration >= 50)
|
|
|
|
{
|
|
|
|
logGlobal->error("Infinite recursion for mine types detected in template %s", name);
|
|
|
|
return std::map<TResource, ui16>();
|
|
|
|
}
|
|
|
|
if (zone->getMinesLikeZone() != ZoneOptions::NO_ZONE)
|
|
|
|
{
|
|
|
|
iteration++;
|
|
|
|
const auto otherZone = zones.at(zone->getMinesLikeZone());
|
|
|
|
zone->setMinesInfo(inheritMineTypes(otherZone, iteration));
|
|
|
|
}
|
|
|
|
return zone->getMinesInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CTreasureInfo> CRmgTemplate::inheritTreasureInfo(std::shared_ptr<ZoneOptions> zone, uint32_t iteration /* = 0 */)
|
|
|
|
{
|
|
|
|
if (iteration >= 50)
|
|
|
|
{
|
|
|
|
logGlobal->error("Infinite recursion for treasures detected in template %s", name);
|
|
|
|
return std::vector<CTreasureInfo>();
|
|
|
|
}
|
|
|
|
if (zone->getTreasureLikeZone() != ZoneOptions::NO_ZONE)
|
|
|
|
{
|
|
|
|
iteration++;
|
|
|
|
const auto otherZone = zones.at(zone->getTreasureLikeZone());
|
|
|
|
zone->setTreasureInfo(inheritTreasureInfo(otherZone, iteration));
|
|
|
|
}
|
|
|
|
return zone->getTreasureInfo();
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:11:20 +02:00
|
|
|
void CRmgTemplate::afterLoad()
|
|
|
|
{
|
|
|
|
for(auto & idAndZone : zones)
|
|
|
|
{
|
|
|
|
auto zone = idAndZone.second;
|
|
|
|
|
2023-03-19 10:27:05 +02:00
|
|
|
//Inherit properties recursively.
|
|
|
|
inheritTerrainType(zone);
|
|
|
|
inheritMineTypes(zone);
|
|
|
|
inheritTreasureInfo(zone);
|
2018-03-09 20:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-05-31 11:25:39 +02:00
|
|
|
|
|
|
|
if(allowedWaterContent.empty() || allowedWaterContent.count(EWaterContent::EWaterContent::RANDOM))
|
|
|
|
{
|
|
|
|
allowedWaterContent.insert(EWaterContent::EWaterContent::NONE);
|
|
|
|
allowedWaterContent.insert(EWaterContent::EWaterContent::NORMAL);
|
|
|
|
allowedWaterContent.insert(EWaterContent::EWaterContent::ISLANDS);
|
|
|
|
}
|
|
|
|
allowedWaterContent.erase(EWaterContent::EWaterContent::RANDOM);
|
2018-03-09 20:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|