2013-12-31 02:09:58 +03:00
|
|
|
/*
|
2017-07-13 10:26:03 +02:00
|
|
|
* MapFormatJson.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
|
|
|
|
*
|
|
|
|
*/
|
2013-12-31 02:09:58 +03:00
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "MapFormatJson.h"
|
|
|
|
|
2015-08-08 17:35:54 +02:00
|
|
|
#include "../filesystem/CInputStream.h"
|
|
|
|
#include "../filesystem/COutputStream.h"
|
2017-08-11 15:27:42 +02:00
|
|
|
#include "../JsonDetail.h"
|
2013-12-31 02:09:58 +03:00
|
|
|
#include "CMap.h"
|
|
|
|
#include "../CModHandler.h"
|
2015-12-05 11:04:00 +02:00
|
|
|
#include "../CHeroHandler.h"
|
|
|
|
#include "../CTownHandler.h"
|
2013-12-31 02:09:58 +03:00
|
|
|
#include "../VCMI_Lib.h"
|
2023-01-11 15:17:24 +02:00
|
|
|
#include "../RiverHandler.h"
|
|
|
|
#include "../RoadHandler.h"
|
2023-01-09 01:17:37 +02:00
|
|
|
#include "../TerrainHandler.h"
|
2015-11-13 16:47:47 +02:00
|
|
|
#include "../mapObjects/ObjectTemplate.h"
|
|
|
|
#include "../mapObjects/CObjectHandler.h"
|
|
|
|
#include "../mapObjects/CObjectClassesHandler.h"
|
2015-12-05 12:56:38 +02:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
|
|
|
#include "../mapObjects/CGTownInstance.h"
|
2016-02-21 19:58:09 +02:00
|
|
|
#include "../spells/CSpellHandler.h"
|
2018-03-31 07:56:40 +02:00
|
|
|
#include "../CSkillHandler.h"
|
2015-08-19 20:02:30 +02:00
|
|
|
#include "../StringConstants.h"
|
2016-02-13 09:47:40 +02:00
|
|
|
#include "../serializer/JsonDeserializer.h"
|
|
|
|
#include "../serializer/JsonSerializer.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
class MapObjectResolver: public IInstanceResolver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MapObjectResolver(const CMapFormatJson * owner_);
|
|
|
|
|
|
|
|
si32 decode (const std::string & identifier) const override;
|
|
|
|
std::string encode(si32 identifier) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const CMapFormatJson * owner;
|
|
|
|
};
|
|
|
|
|
|
|
|
MapObjectResolver::MapObjectResolver(const CMapFormatJson * owner_):
|
|
|
|
owner(owner_)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 MapObjectResolver::decode(const std::string & identifier) const
|
|
|
|
{
|
|
|
|
//always decode as ObjectInstanceID
|
|
|
|
|
|
|
|
auto it = owner->map->instanceNames.find(identifier);
|
|
|
|
|
|
|
|
if(it != owner->map->instanceNames.end())
|
|
|
|
{
|
|
|
|
return (*it).second->id.getNum();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Object not found: %s", identifier);
|
2016-11-13 12:38:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MapObjectResolver::encode(si32 identifier) const
|
|
|
|
{
|
|
|
|
ObjectInstanceID id;
|
|
|
|
|
|
|
|
//use h3m questIdentifiers if they are present
|
|
|
|
if(owner->map->questIdentifierToId.empty())
|
|
|
|
{
|
|
|
|
id = ObjectInstanceID(identifier);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
id = owner->map->questIdentifierToId[identifier];
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 oid = id.getNum();
|
|
|
|
if(oid < 0 || oid >= owner->map->objects.size())
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Cannot get object with id %d", oid);
|
2016-11-13 12:38:42 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return owner->map->objects[oid]->instanceName;
|
|
|
|
}
|
|
|
|
|
2016-02-13 09:47:40 +02:00
|
|
|
namespace HeaderDetail
|
|
|
|
{
|
2016-02-14 13:22:46 +02:00
|
|
|
static const std::vector<std::string> difficultyMap =
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
2016-02-14 13:22:46 +02:00
|
|
|
"EASY",
|
|
|
|
"NORMAL",
|
|
|
|
"HARD",
|
|
|
|
"EXPERT",
|
|
|
|
"IMPOSSIBLE"
|
2016-02-13 09:47:40 +02:00
|
|
|
};
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-11-15 23:03:27 +02:00
|
|
|
enum class ECanPlay
|
|
|
|
{
|
|
|
|
NONE = 0,
|
|
|
|
PLAYER_OR_AI = 1,
|
|
|
|
PLAYER_ONLY = 2,
|
|
|
|
AI_ONLY = 3
|
|
|
|
};
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
static const std::vector<std::string> canPlayMap =
|
|
|
|
{
|
2017-11-15 23:03:27 +02:00
|
|
|
"",
|
|
|
|
"PlayerOrAI",
|
|
|
|
"PlayerOnly",
|
|
|
|
"AIOnly"
|
2016-11-13 12:38:42 +02:00
|
|
|
};
|
2016-02-13 09:47:40 +02:00
|
|
|
}
|
2013-12-31 02:09:58 +03:00
|
|
|
|
2015-08-18 01:21:35 +02:00
|
|
|
namespace TriggeredEventsDetail
|
2015-08-07 21:33:44 +02:00
|
|
|
{
|
2016-02-22 22:43:57 +02:00
|
|
|
static const std::array<std::string, 15> conditionNames =
|
2015-08-07 21:33:44 +02:00
|
|
|
{
|
2015-08-18 01:21:35 +02:00
|
|
|
"haveArtifact", "haveCreatures", "haveResources", "haveBuilding",
|
|
|
|
"control", "destroy", "transport", "daysPassed",
|
2016-02-22 22:43:57 +02:00
|
|
|
"isHuman", "daysWithoutTown", "standardWin", "constValue",
|
|
|
|
|
|
|
|
"have_0", "haveBuilding_0", "destroy_0"
|
2015-08-18 01:21:35 +02:00
|
|
|
};
|
2015-08-07 21:33:44 +02:00
|
|
|
|
2015-08-18 01:21:35 +02:00
|
|
|
static const std::array<std::string, 2> typeNames = { "victory", "defeat" };
|
2015-08-07 21:33:44 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
static EMetaclass decodeMetaclass(const std::string & source)
|
|
|
|
{
|
2022-11-15 02:20:55 +02:00
|
|
|
if(source.empty())
|
2016-11-13 12:38:42 +02:00
|
|
|
return EMetaclass::INVALID;
|
|
|
|
auto rawId = vstd::find_pos(NMetaclass::names, source);
|
|
|
|
|
|
|
|
if(rawId >= 0)
|
2023-02-11 18:30:06 +02:00
|
|
|
return static_cast<EMetaclass>(rawId);
|
2016-11-13 12:38:42 +02:00
|
|
|
else
|
|
|
|
return EMetaclass::INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string encodeIdentifier(EMetaclass metaType, si32 type)
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
std::string metaclassName = NMetaclass::names[static_cast<int>(metaType)];
|
|
|
|
std::string identifier;
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
switch(metaType)
|
|
|
|
{
|
|
|
|
case EMetaclass::ARTIFACT:
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
identifier = ArtifactID::encode(type);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EMetaclass::CREATURE:
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
identifier = CreatureID::encode(type);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EMetaclass::OBJECT:
|
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
std::set<si32> subtypes = VLC->objtypeh->knownSubObjects(type);
|
|
|
|
if(!subtypes.empty())
|
|
|
|
{
|
|
|
|
si32 subtype = *subtypes.begin();
|
|
|
|
auto handler = VLC->objtypeh->getHandlerFor(type, subtype);
|
2022-12-31 15:01:19 +02:00
|
|
|
identifier = handler->getTypeName();
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EMetaclass::RESOURCE:
|
|
|
|
{
|
|
|
|
identifier = GameConstants::RESOURCE_NAMES[type];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
logGlobal->error("Unsupported metaclass %s for event condition", metaclassName);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
return CModHandler::makeFullIdentifier("", metaclassName, identifier);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 01:21:35 +02:00
|
|
|
static EventCondition JsonToCondition(const JsonNode & node)
|
|
|
|
{
|
|
|
|
EventCondition event;
|
2016-02-10 12:24:03 +02:00
|
|
|
|
|
|
|
const auto & conditionName = node.Vector()[0].String();
|
|
|
|
|
|
|
|
auto pos = vstd::find_pos(conditionNames, conditionName);
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
event.condition = static_cast<EventCondition::EWinLoseType>(pos);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2015-08-18 01:21:35 +02:00
|
|
|
if (node.Vector().size() > 1)
|
2015-08-07 21:33:44 +02:00
|
|
|
{
|
2015-08-18 01:21:35 +02:00
|
|
|
const JsonNode & data = node.Vector()[1];
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
switch (event.condition)
|
2016-02-10 12:24:03 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
case EventCondition::HAVE_0:
|
|
|
|
case EventCondition::DESTROY_0:
|
|
|
|
{
|
|
|
|
//todo: support subtypes
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
std::string fullIdentifier = data["type"].String();
|
|
|
|
std::string metaTypeName;
|
|
|
|
std::string scope;
|
|
|
|
std::string identifier;
|
2016-11-13 12:38:42 +02:00
|
|
|
CModHandler::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
|
2016-02-10 12:24:03 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
event.metaType = decodeMetaclass(metaTypeName);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2022-12-07 15:18:19 +02:00
|
|
|
auto type = VLC->modh->identifiers.getIdentifier(CModHandler::scopeBuiltin(), fullIdentifier, false);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
if(type)
|
2023-04-16 19:42:56 +02:00
|
|
|
event.objectType = type.value();
|
2016-11-13 12:38:42 +02:00
|
|
|
event.objectInstanceName = data["object"].String();
|
|
|
|
if(data["value"].isNumber())
|
2020-10-01 10:38:06 +02:00
|
|
|
event.value = static_cast<si32>(data["value"].Integer());
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventCondition::HAVE_BUILDING_0:
|
|
|
|
{
|
|
|
|
//todo: support of new condition format HAVE_BUILDING_0
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
//old format
|
2017-11-26 23:18:18 +02:00
|
|
|
if (data["type"].getType() == JsonNode::JsonType::DATA_STRING)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
auto identifier = VLC->modh->identifiers.getIdentifier(data["type"]);
|
|
|
|
if(identifier)
|
2023-04-16 19:42:56 +02:00
|
|
|
event.objectType = identifier.value();
|
2016-11-13 12:38:42 +02:00
|
|
|
else
|
|
|
|
throw std::runtime_error("Identifier resolution failed in event condition");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data["type"].isNumber())
|
2020-10-01 10:38:06 +02:00
|
|
|
event.objectType = static_cast<si32>(data["type"].Float());
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
if (!data["value"].isNull())
|
2020-10-01 10:38:06 +02:00
|
|
|
event.value = static_cast<si32>(data["value"].Float());
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-08-18 01:21:35 +02:00
|
|
|
|
|
|
|
if (!data["position"].isNull())
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
const auto & position = data["position"].Vector();
|
2020-10-01 10:38:06 +02:00
|
|
|
event.position.x = static_cast<si32>(position.at(0).Float());
|
|
|
|
event.position.y = static_cast<si32>(position.at(1).Float());
|
|
|
|
event.position.z = static_cast<si32>(position.at(2).Float());
|
2015-08-18 01:21:35 +02:00
|
|
|
}
|
2015-08-07 21:33:44 +02:00
|
|
|
}
|
2015-08-18 01:21:35 +02:00
|
|
|
return event;
|
2015-08-07 21:33:44 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
static JsonNode ConditionToJson(const EventCondition & event)
|
2015-08-15 17:09:39 +02:00
|
|
|
{
|
2015-08-18 01:21:35 +02:00
|
|
|
JsonNode json;
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonVector & asVector = json.Vector();
|
2015-08-18 01:21:35 +02:00
|
|
|
|
|
|
|
JsonNode condition;
|
|
|
|
condition.String() = conditionNames.at(event.condition);
|
|
|
|
asVector.push_back(condition);
|
|
|
|
|
|
|
|
JsonNode data;
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
switch (event.condition)
|
|
|
|
{
|
|
|
|
case EventCondition::HAVE_0:
|
|
|
|
case EventCondition::DESTROY_0:
|
|
|
|
{
|
|
|
|
//todo: support subtypes
|
|
|
|
|
|
|
|
if(event.metaType != EMetaclass::INVALID)
|
|
|
|
data["type"].String() = encodeIdentifier(event.metaType, event.objectType);
|
|
|
|
|
|
|
|
if(event.value > 0)
|
|
|
|
data["value"].Integer() = event.value;
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2022-11-15 02:20:55 +02:00
|
|
|
if(!event.objectInstanceName.empty())
|
2016-11-13 12:38:42 +02:00
|
|
|
data["object"].String() = event.objectInstanceName;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventCondition::HAVE_BUILDING_0:
|
|
|
|
{
|
|
|
|
//todo: support of new condition format HAVE_BUILDING_0
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
//old format
|
|
|
|
if(event.objectType != -1)
|
|
|
|
data["type"].Integer() = event.objectType;
|
2015-11-14 18:47:29 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
if(event.value != -1)
|
|
|
|
data["value"].Integer() = event.value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
if(event.position != int3(-1, -1, -1))
|
2015-08-18 01:21:35 +02:00
|
|
|
{
|
|
|
|
auto & position = data["position"].Vector();
|
|
|
|
position.resize(3);
|
|
|
|
position[0].Float() = event.position.x;
|
|
|
|
position[1].Float() = event.position.y;
|
|
|
|
position[2].Float() = event.position.z;
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:47:29 +02:00
|
|
|
if(!data.isNull())
|
|
|
|
asVector.push_back(data);
|
2015-08-14 04:22:24 +02:00
|
|
|
|
2019-01-19 12:52:02 +02:00
|
|
|
return json;
|
2015-08-18 01:21:35 +02:00
|
|
|
}
|
2015-08-18 00:56:16 +02:00
|
|
|
}//namespace TriggeredEventsDetail
|
|
|
|
|
2015-08-18 01:21:35 +02:00
|
|
|
namespace TerrainDetail
|
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
static const std::array<char, 4> flipCodes =
|
2015-08-18 00:56:16 +02:00
|
|
|
{
|
|
|
|
'_', '-', '|', '+'
|
2015-08-18 01:21:35 +02:00
|
|
|
};
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-07 21:33:44 +02:00
|
|
|
///CMapFormatJson
|
2015-08-12 02:31:06 +02:00
|
|
|
const int CMapFormatJson::VERSION_MAJOR = 1;
|
2023-04-12 00:59:55 +02:00
|
|
|
const int CMapFormatJson::VERSION_MINOR = 1;
|
2015-08-12 02:31:06 +02:00
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
const std::string CMapFormatJson::HEADER_FILE_NAME = "header.json";
|
2015-11-13 16:47:47 +02:00
|
|
|
const std::string CMapFormatJson::OBJECTS_FILE_NAME = "objects.json";
|
2015-08-11 20:20:13 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
CMapFormatJson::CMapFormatJson():
|
2017-05-28 12:04:27 +02:00
|
|
|
fileVersionMajor(0), fileVersionMinor(0),
|
2022-12-07 23:36:20 +02:00
|
|
|
mapObjectResolver(std::make_unique<MapObjectResolver>(this)),
|
2017-05-28 12:04:27 +02:00
|
|
|
map(nullptr), mapHeader(nullptr)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
TerrainType * CMapFormatJson::getTerrainByCode(const std::string & code)
|
2022-12-20 18:35:40 +02:00
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
for(const auto & object : VLC->terrainTypeHandler->objects)
|
2022-12-20 18:35:40 +02:00
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
if(object->shortIdentifier == code)
|
2022-12-20 18:35:40 +02:00
|
|
|
return const_cast<TerrainType *>(object.get());
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
RiverType * CMapFormatJson::getRiverByCode(const std::string & code)
|
2022-12-20 18:35:40 +02:00
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
for(const auto & object : VLC->riverTypeHandler->objects)
|
2022-12-20 18:35:40 +02:00
|
|
|
{
|
2022-12-21 00:45:35 +02:00
|
|
|
if (object->shortIdentifier == code)
|
2022-12-20 18:35:40 +02:00
|
|
|
return const_cast<RiverType *>(object.get());
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
RoadType * CMapFormatJson::getRoadByCode(const std::string & code)
|
2022-12-20 18:35:40 +02:00
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
for(const auto & object : VLC->roadTypeHandler->objects)
|
2022-12-20 18:35:40 +02:00
|
|
|
{
|
2022-12-21 00:45:35 +02:00
|
|
|
if (object->shortIdentifier == code)
|
2022-12-20 18:35:40 +02:00
|
|
|
return const_cast<RoadType *>(object.get());
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-04-09 17:26:32 +02:00
|
|
|
void CMapFormatJson::serializeAllowedFactions(JsonSerializeFormat & handler, std::set<FactionID> & value) const
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
|
|
|
//TODO: unify allowed factions with others - make them std::vector<bool>
|
|
|
|
|
|
|
|
std::vector<bool> temp;
|
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
|
|
|
temp.resize(VLC->townh->size(), false);
|
2016-02-14 13:22:46 +02:00
|
|
|
auto standard = VLC->townh->getDefaultAllowed();
|
|
|
|
|
2023-01-04 15:17:50 +02:00
|
|
|
if(handler.saving)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
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
|
|
|
for(auto faction : VLC->townh->objects)
|
2023-01-04 15:17:50 +02:00
|
|
|
if(faction->town && vstd::contains(value, faction->getIndex()))
|
2023-02-11 18:30:06 +02:00
|
|
|
temp[static_cast<std::size_t>(faction->getIndex())] = true;
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
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.serializeLIC("allowedFactions", &FactionID::decode, &FactionID::encode, standard, temp);
|
2016-02-14 13:22:46 +02:00
|
|
|
|
|
|
|
if(!handler.saving)
|
|
|
|
{
|
|
|
|
value.clear();
|
|
|
|
for (std::size_t i=0; i<temp.size(); i++)
|
|
|
|
if(temp[i])
|
2023-04-09 17:26:32 +02:00
|
|
|
value.insert(static_cast<FactionID>(i));
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::serializeHeader(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
handler.serializeString("name", mapHeader->name);
|
|
|
|
handler.serializeString("description", mapHeader->description);
|
2016-11-13 12:38:42 +02:00
|
|
|
handler.serializeInt("heroLevelLimit", mapHeader->levelLimit, 0);
|
2016-02-14 13:22:46 +02:00
|
|
|
|
|
|
|
//todo: support arbitrary percentage
|
2019-01-26 16:04:17 +02:00
|
|
|
handler.serializeEnum("difficulty", mapHeader->difficulty, HeaderDetail::difficultyMap);
|
2016-02-14 13:22:46 +02:00
|
|
|
|
|
|
|
serializePlayerInfo(handler);
|
2016-02-21 21:13:20 +02:00
|
|
|
|
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.serializeLIC("allowedHeroes", &HeroTypeID::decode, &HeroTypeID::encode, VLC->heroh->getDefaultAllowed(), mapHeader->allowedHeroes);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
handler.serializeString("victoryString", mapHeader->victoryMessage);
|
|
|
|
handler.serializeInt("victoryIconIndex", mapHeader->victoryIconIndex);
|
|
|
|
|
|
|
|
handler.serializeString("defeatString", mapHeader->defeatMessage);
|
|
|
|
handler.serializeInt("defeatIconIndex", mapHeader->defeatIconIndex);
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::serializePlayerInfo(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
auto playersData = handler.enterStruct("players");
|
|
|
|
|
|
|
|
for(int player = 0; player < PlayerColor::PLAYER_LIMIT_I; player++)
|
|
|
|
{
|
|
|
|
PlayerInfo & info = mapHeader->players[player];
|
|
|
|
|
|
|
|
if(handler.saving)
|
|
|
|
{
|
|
|
|
if(!info.canAnyonePlay())
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-15 17:16:45 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
auto playerData = handler.enterStruct(GameConstants::PLAYER_COLOR_NAMES[player]);
|
2016-02-15 17:16:45 +02:00
|
|
|
|
|
|
|
if(!handler.saving)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
if(handler.getCurrent().isNull())
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
|
|
|
info.canComputerPlay = false;
|
|
|
|
info.canHumanPlay = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serializeAllowedFactions(handler, info.allowedFactions);
|
|
|
|
|
2017-11-15 23:03:27 +02:00
|
|
|
HeaderDetail::ECanPlay canPlay = HeaderDetail::ECanPlay::NONE;
|
|
|
|
|
|
|
|
if(handler.saving)
|
|
|
|
{
|
|
|
|
if(info.canComputerPlay)
|
|
|
|
{
|
|
|
|
canPlay = info.canHumanPlay ? HeaderDetail::ECanPlay::PLAYER_OR_AI : HeaderDetail::ECanPlay::AI_ONLY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
canPlay = info.canHumanPlay ? HeaderDetail::ECanPlay::PLAYER_ONLY : HeaderDetail::ECanPlay::NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.serializeEnum("canPlay", canPlay, HeaderDetail::canPlayMap);
|
|
|
|
|
|
|
|
if(!handler.saving)
|
|
|
|
{
|
|
|
|
switch(canPlay)
|
|
|
|
{
|
|
|
|
case HeaderDetail::ECanPlay::PLAYER_OR_AI:
|
|
|
|
info.canComputerPlay = true;
|
|
|
|
info.canHumanPlay = true;
|
|
|
|
break;
|
|
|
|
case HeaderDetail::ECanPlay::PLAYER_ONLY:
|
|
|
|
info.canComputerPlay = false;
|
|
|
|
info.canHumanPlay = true;
|
|
|
|
break;
|
|
|
|
case HeaderDetail::ECanPlay::AI_ONLY:
|
|
|
|
info.canComputerPlay = true;
|
|
|
|
info.canHumanPlay = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
info.canComputerPlay = false;
|
|
|
|
info.canHumanPlay = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 13:22:46 +02:00
|
|
|
|
2016-02-23 22:23:39 +02:00
|
|
|
//saving whole structure only if position is valid
|
|
|
|
if(!handler.saving || info.posOfMainTown.valid())
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2016-02-23 22:23:39 +02:00
|
|
|
auto mainTown = handler.enterStruct("mainTown");
|
|
|
|
handler.serializeBool("generateHero", info.generateHeroAtMainTown);
|
2016-11-13 12:38:42 +02:00
|
|
|
handler.serializeInt("x", info.posOfMainTown.x, -1);
|
|
|
|
handler.serializeInt("y", info.posOfMainTown.y, -1);
|
|
|
|
handler.serializeInt("l", info.posOfMainTown.z, -1);
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
2016-02-23 22:23:39 +02:00
|
|
|
if(!handler.saving)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2016-02-23 22:23:39 +02:00
|
|
|
info.hasMainTown = info.posOfMainTown.valid();
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
handler.serializeString("mainHero", info.mainHeroInstance);//must be before "heroes"
|
2016-02-14 13:22:46 +02:00
|
|
|
|
|
|
|
//heroes
|
2016-11-13 12:38:42 +02:00
|
|
|
if(handler.saving)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
//ignoring heroesNames and saving from actual map objects
|
|
|
|
//TODO: optimize
|
|
|
|
for(auto & obj : map->objects)
|
|
|
|
{
|
|
|
|
if((obj->ID == Obj::HERO || obj->ID == Obj::RANDOM_HERO) && obj->tempOwner == PlayerColor(player))
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
auto * hero = dynamic_cast<CGHeroInstance *>(obj.get());
|
2016-02-14 13:22:46 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
auto heroes = handler.enterStruct("heroes");
|
2016-11-13 12:38:42 +02:00
|
|
|
if(hero)
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
auto heroData = handler.enterStruct(hero->instanceName);
|
2023-01-02 13:27:03 +02:00
|
|
|
heroData->serializeString("name", hero->nameCustom);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
if(hero->ID == Obj::HERO)
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
std::string temp;
|
2016-11-13 12:38:42 +02:00
|
|
|
if(hero->type)
|
|
|
|
{
|
2023-01-02 13:27:03 +02:00
|
|
|
temp = hero->type->getJsonKey();
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-02 13:27:03 +02:00
|
|
|
temp = VLC->heroh->objects[hero->subID]->getJsonKey();
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
handler.serializeString("type", temp);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
else
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
info.heroesNames.clear();
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
auto heroes = handler.enterStruct("heroes");
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
for(const auto & hero : handler.getCurrent().Struct())
|
|
|
|
{
|
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
|
|
|
const JsonNode & data = hero.second;
|
|
|
|
const std::string instanceName = hero.first;
|
2016-11-13 12:38:42 +02:00
|
|
|
|
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
|
|
|
SHeroName hname;
|
2016-11-13 12:38:42 +02:00
|
|
|
hname.heroId = -1;
|
|
|
|
std::string rawId = data["type"].String();
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
if(!rawId.empty())
|
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
|
|
|
hname.heroId = HeroTypeID::decode(rawId);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
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
|
|
|
hname.heroName = data["name"].String();
|
2016-11-13 12:38:42 +02:00
|
|
|
|
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
|
|
|
if(instanceName == info.mainHeroInstance)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
//this is main hero
|
|
|
|
info.mainCustomHeroName = hname.heroName;
|
|
|
|
info.hasRandomHero = (hname.heroId == -1);
|
|
|
|
info.mainCustomHeroId = hname.heroId;
|
|
|
|
info.mainCustomHeroPortrait = -1;
|
|
|
|
//todo:mainHeroPortrait
|
|
|
|
}
|
|
|
|
|
|
|
|
info.heroesNames.push_back(hname);
|
|
|
|
}
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
handler.serializeBool("randomFaction", info.isFactionRandom);
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::readTeams(JsonDeserializer & handler)
|
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
auto teams = handler.enterArray("teams");
|
2017-07-20 06:08:49 +02:00
|
|
|
const JsonNode & src = teams->getCurrent();
|
2016-02-14 13:22:46 +02:00
|
|
|
|
2017-11-26 23:18:18 +02:00
|
|
|
if(src.getType() != JsonNode::JsonType::DATA_VECTOR)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
|
|
|
// No alliances
|
2017-11-26 23:18:18 +02:00
|
|
|
if(src.getType() != JsonNode::JsonType::DATA_NULL)
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->error("Invalid teams field type");
|
2016-02-14 13:22:46 +02:00
|
|
|
|
|
|
|
mapHeader->howManyTeams = 0;
|
|
|
|
for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
|
|
|
|
if(mapHeader->players[i].canComputerPlay || mapHeader->players[i].canHumanPlay)
|
|
|
|
mapHeader->players[i].team = TeamID(mapHeader->howManyTeams++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const JsonVector & srcVector = src.Vector();
|
2020-10-01 10:38:06 +02:00
|
|
|
mapHeader->howManyTeams = static_cast<ui8>(srcVector.size());
|
2016-02-14 13:22:46 +02:00
|
|
|
|
|
|
|
for(int team = 0; team < mapHeader->howManyTeams; team++)
|
|
|
|
for(const JsonNode & playerData : srcVector[team].Vector())
|
|
|
|
{
|
|
|
|
PlayerColor player = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, playerData.String()));
|
|
|
|
if(player.isValidPlayer())
|
|
|
|
if(mapHeader->players[player.getNum()].canAnyonePlay())
|
|
|
|
mapHeader->players[player.getNum()].team = TeamID(team);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(PlayerInfo & player : mapHeader->players)
|
|
|
|
if(player.canAnyonePlay() && player.team == TeamID::NO_TEAM)
|
|
|
|
player.team = TeamID(mapHeader->howManyTeams++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::writeTeams(JsonSerializer & handler)
|
|
|
|
{
|
|
|
|
std::vector<std::set<PlayerColor>> teamsData;
|
|
|
|
|
|
|
|
teamsData.resize(mapHeader->howManyTeams);
|
|
|
|
|
|
|
|
//get raw data
|
|
|
|
for(int idx = 0; idx < mapHeader->players.size(); idx++)
|
|
|
|
{
|
|
|
|
const PlayerInfo & player = mapHeader->players.at(idx);
|
|
|
|
int team = player.team.getNum();
|
|
|
|
if(vstd::iswithin(team, 0, mapHeader->howManyTeams-1) && player.canAnyonePlay())
|
|
|
|
teamsData.at(team).insert(PlayerColor(idx));
|
|
|
|
}
|
|
|
|
|
|
|
|
//remove single-member teams
|
|
|
|
vstd::erase_if(teamsData, [](std::set<PlayerColor> & elem) -> bool
|
|
|
|
{
|
|
|
|
return elem.size() <= 1;
|
|
|
|
});
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
if(!teamsData.empty())
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
JsonNode dest;
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
//construct output
|
2017-11-26 23:18:18 +02:00
|
|
|
dest.setType(JsonNode::JsonType::DATA_VECTOR);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
for(const std::set<PlayerColor> & teamData : teamsData)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode team(JsonNode::JsonType::DATA_VECTOR);
|
2016-11-13 12:38:42 +02:00
|
|
|
for(const PlayerColor & player : teamData)
|
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode member(JsonNode::JsonType::DATA_STRING);
|
2016-11-13 12:38:42 +02:00
|
|
|
member.String() = GameConstants::PLAYER_COLOR_NAMES[player.getNum()];
|
|
|
|
team.Vector().push_back(std::move(member));
|
|
|
|
}
|
|
|
|
dest.Vector().push_back(std::move(team));
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
2023-04-16 19:42:56 +02:00
|
|
|
handler.serializeRaw("teams", dest, std::nullopt);
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-13 14:22:26 +02:00
|
|
|
void CMapFormatJson::readTriggeredEvents(JsonDeserializer & handler)
|
2015-08-07 21:33:44 +02:00
|
|
|
{
|
2016-02-13 14:22:26 +02:00
|
|
|
const JsonNode & input = handler.getCurrent();
|
2015-08-07 21:33:44 +02:00
|
|
|
|
|
|
|
mapHeader->triggeredEvents.clear();
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
for(const auto & entry : input["triggeredEvents"].Struct())
|
2015-08-07 21:33:44 +02:00
|
|
|
{
|
|
|
|
TriggeredEvent event;
|
|
|
|
event.identifier = entry.first;
|
|
|
|
readTriggeredEvent(event, entry.second);
|
|
|
|
mapHeader->triggeredEvents.push_back(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
void CMapFormatJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode & source) const
|
2015-08-07 21:33:44 +02:00
|
|
|
{
|
2015-08-18 00:56:16 +02:00
|
|
|
using namespace TriggeredEventsDetail;
|
|
|
|
|
2015-08-07 21:33:44 +02:00
|
|
|
event.onFulfill = source["message"].String();
|
|
|
|
event.description = source["description"].String();
|
|
|
|
event.effect.type = vstd::find_pos(typeNames, source["effect"]["type"].String());
|
|
|
|
event.effect.toOtherMessage = source["effect"]["messageToSend"].String();
|
|
|
|
event.trigger = EventExpression(source["condition"], JsonToCondition); // logical expression
|
|
|
|
}
|
|
|
|
|
2016-02-14 13:22:46 +02:00
|
|
|
void CMapFormatJson::writeTriggeredEvents(JsonSerializer & handler)
|
2015-08-14 04:22:24 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
JsonNode triggeredEvents(JsonNode::JsonType::DATA_STRUCT);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
for(const auto & event : mapHeader->triggeredEvents)
|
2015-08-15 17:09:39 +02:00
|
|
|
writeTriggeredEvent(event, triggeredEvents[event.identifier]);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
handler.serializeRaw("triggeredEvents", triggeredEvents, std::nullopt);
|
2015-08-14 04:22:24 +02:00
|
|
|
}
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
void CMapFormatJson::writeTriggeredEvent(const TriggeredEvent & event, JsonNode & dest) const
|
2015-08-14 04:22:24 +02:00
|
|
|
{
|
2015-08-18 00:56:16 +02:00
|
|
|
using namespace TriggeredEventsDetail;
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
if(!event.onFulfill.empty())
|
|
|
|
dest["message"].String() = event.onFulfill;
|
|
|
|
|
|
|
|
if(!event.description.empty())
|
|
|
|
dest["description"].String() = event.description;
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
dest["effect"]["type"].String() = typeNames.at(static_cast<size_t>(event.effect.type));
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
if(!event.effect.toOtherMessage.empty())
|
|
|
|
dest["effect"]["messageToSend"].String() = event.effect.toOtherMessage;
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-15 17:09:39 +02:00
|
|
|
dest["condition"] = event.trigger.toJson(ConditionToJson);
|
2015-08-14 04:22:24 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void CMapFormatJson::readDisposedHeroes(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
auto definitions = handler.enterStruct("predefinedHeroes");//DisposedHeroes are part of predefinedHeroes in VCMI map format
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
const JsonNode & data = handler.getCurrent();
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
for(const auto & entry : data.Struct())
|
|
|
|
{
|
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
|
|
|
HeroTypeID type(HeroTypeID::decode(entry.first));
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
ui8 mask = 0;
|
|
|
|
|
|
|
|
for(const JsonNode & playerData : entry.second["availableFor"].Vector())
|
|
|
|
{
|
|
|
|
PlayerColor player = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, playerData.String()));
|
|
|
|
if(player.isValidPlayer())
|
|
|
|
{
|
|
|
|
mask |= 1 << player.getNum();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mask != 0 && mask != GameConstants::ALL_PLAYERS && type.getNum() >= 0)
|
|
|
|
{
|
|
|
|
DisposedHero hero;
|
|
|
|
|
|
|
|
hero.heroId = type.getNum();
|
|
|
|
hero.players = mask;
|
|
|
|
//name and portrait are not used
|
|
|
|
|
|
|
|
map->disposedHeroes.push_back(hero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::writeDisposedHeroes(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
if(map->disposedHeroes.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto definitions = handler.enterStruct("predefinedHeroes");//DisposedHeroes are part of predefinedHeroes in VCMI map format
|
|
|
|
|
|
|
|
for(const DisposedHero & hero : map->disposedHeroes)
|
|
|
|
{
|
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
|
|
|
std::string type = HeroTypeID::encode(hero.heroId);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
auto definition = definitions->enterStruct(type);
|
|
|
|
|
|
|
|
JsonNode players(JsonNode::JsonType::DATA_VECTOR);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
for(int playerNum = 0; playerNum < PlayerColor::PLAYER_LIMIT_I; playerNum++)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
if((1 << playerNum) & hero.players)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode player(JsonNode::JsonType::DATA_STRING);
|
2016-11-13 12:38:42 +02:00
|
|
|
player.String() = GameConstants::PLAYER_COLOR_NAMES[playerNum];
|
2017-07-20 06:08:49 +02:00
|
|
|
players.Vector().push_back(player);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
2023-04-16 19:42:56 +02:00
|
|
|
definition->serializeRaw("availableFor", players, std::nullopt);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::serializeRumors(JsonSerializeFormat & handler)
|
2016-02-21 19:58:09 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
auto rumors = handler.enterArray("rumors");
|
|
|
|
rumors.serializeStruct(map->rumors);
|
|
|
|
}
|
2016-02-21 19:58:09 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void CMapFormatJson::serializePredefinedHeroes(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
//todo:serializePredefinedHeroes
|
2016-02-21 19:58:09 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
if(handler.saving)
|
|
|
|
{
|
|
|
|
if(!map->predefinedHeroes.empty())
|
|
|
|
{
|
|
|
|
auto predefinedHeroes = handler.enterStruct("predefinedHeroes");
|
|
|
|
|
|
|
|
for(auto & hero : map->predefinedHeroes)
|
|
|
|
{
|
|
|
|
auto predefinedHero = handler.enterStruct(hero->getHeroTypeName());
|
|
|
|
|
|
|
|
hero->serializeJsonDefinition(handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto predefinedHeroes = handler.enterStruct("predefinedHeroes");
|
|
|
|
|
|
|
|
const JsonNode & data = handler.getCurrent();
|
|
|
|
|
|
|
|
for(const auto & p : data.Struct())
|
|
|
|
{
|
|
|
|
auto predefinedHero = handler.enterStruct(p.first);
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
auto * hero = new CGHeroInstance();
|
2016-11-13 12:38:42 +02:00
|
|
|
hero->ID = Obj::HERO;
|
2023-02-11 18:30:06 +02:00
|
|
|
hero->setHeroTypeName(p.first);
|
|
|
|
hero->serializeJsonDefinition(handler);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
map->predefinedHeroes.emplace_back(hero);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::serializeOptions(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
serializeRumors(handler);
|
|
|
|
|
|
|
|
serializePredefinedHeroes(handler);
|
2016-02-21 19:58:09 +02:00
|
|
|
|
2018-03-31 07:56:40 +02:00
|
|
|
handler.serializeLIC("allowedAbilities", &CSkillHandler::decodeSkill, &CSkillHandler::encodeSkill, VLC->skillh->getDefaultAllowed(), map->allowedAbilities);
|
2016-02-21 19:58:09 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
handler.serializeLIC("allowedArtifacts", &ArtifactID::decode, &ArtifactID::encode, VLC->arth->getDefaultAllowed(), map->allowedArtifact);
|
2016-02-21 19:58:09 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
handler.serializeLIC("allowedSpells", &SpellID::decode, &SpellID::encode, VLC->spellh->getDefaultAllowed(), map->allowedSpell);
|
2016-02-21 19:58:09 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
//todo:events
|
2016-02-21 19:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::readOptions(JsonDeserializer & handler)
|
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
readDisposedHeroes(handler);
|
2016-02-21 19:58:09 +02:00
|
|
|
serializeOptions(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::writeOptions(JsonSerializer & handler)
|
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
writeDisposedHeroes(handler);
|
2016-02-21 19:58:09 +02:00
|
|
|
serializeOptions(handler);
|
|
|
|
}
|
|
|
|
|
2015-08-07 21:33:44 +02:00
|
|
|
///CMapPatcher
|
2023-02-11 18:30:06 +02:00
|
|
|
CMapPatcher::CMapPatcher(const JsonNode & stream): input(stream)
|
2013-12-31 02:09:58 +03:00
|
|
|
{
|
2016-02-22 22:43:57 +02:00
|
|
|
//todo: update map patches and change this
|
|
|
|
fileVersionMajor = 0;
|
|
|
|
fileVersionMinor = 0;
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 21:33:44 +02:00
|
|
|
void CMapPatcher::patchMapHeader(std::unique_ptr<CMapHeader> & header)
|
|
|
|
{
|
2016-02-13 14:22:26 +02:00
|
|
|
map = nullptr;
|
|
|
|
mapHeader = header.get();
|
2015-08-07 21:33:44 +02:00
|
|
|
if (!input.isNull())
|
|
|
|
readPatchData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapPatcher::readPatchData()
|
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonDeserializer handler(mapObjectResolver.get(), input);
|
2016-02-13 14:22:26 +02:00
|
|
|
readTriggeredEvents(handler);
|
2015-08-07 21:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///CMapLoaderJson
|
2023-02-11 18:30:06 +02:00
|
|
|
CMapLoaderJson::CMapLoaderJson(CInputStream * stream)
|
|
|
|
: buffer(stream)
|
|
|
|
, ioApi(new CProxyROIOApi(buffer))
|
|
|
|
, loader("", "_", ioApi)
|
2015-08-07 21:33:44 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<CMap> CMapLoaderJson::loadMap()
|
|
|
|
{
|
2016-02-10 07:59:24 +02:00
|
|
|
LOG_TRACE(logGlobal);
|
2023-02-11 18:30:06 +02:00
|
|
|
std::unique_ptr<CMap> result = std::make_unique<CMap>();
|
2016-02-13 14:22:26 +02:00
|
|
|
map = result.get();
|
|
|
|
mapHeader = map;
|
2013-12-31 02:09:58 +03:00
|
|
|
readMap();
|
2019-01-19 12:52:02 +02:00
|
|
|
return result;
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CMapHeader> CMapLoaderJson::loadMapHeader()
|
|
|
|
{
|
2016-02-10 07:59:24 +02:00
|
|
|
LOG_TRACE(logGlobal);
|
2016-02-13 14:22:26 +02:00
|
|
|
map = nullptr;
|
2023-02-11 18:30:06 +02:00
|
|
|
std::unique_ptr<CMapHeader> result = std::make_unique<CMapHeader>();
|
2016-02-13 14:22:26 +02:00
|
|
|
mapHeader = result.get();
|
2016-02-21 19:58:09 +02:00
|
|
|
readHeader(false);
|
2019-01-19 12:52:02 +02:00
|
|
|
return result;
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
|
|
|
|
2016-02-22 01:37:19 +02:00
|
|
|
JsonNode CMapLoaderJson::getFromArchive(const std::string & archiveFilename)
|
2015-08-18 00:56:16 +02:00
|
|
|
{
|
|
|
|
ResourceID resource(archiveFilename, EResType::TEXT);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
if(!loader.existsResource(resource))
|
2018-10-31 08:27:54 +02:00
|
|
|
throw std::runtime_error(archiveFilename+" not found");
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
auto data = loader.load(resource)->readAll();
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
JsonNode res(reinterpret_cast<char*>(data.first.get()), data.second);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2019-01-19 12:52:02 +02:00
|
|
|
return res;
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
void CMapLoaderJson::readMap()
|
|
|
|
{
|
2016-02-10 07:59:24 +02:00
|
|
|
LOG_TRACE(logGlobal);
|
2016-02-21 19:58:09 +02:00
|
|
|
readHeader(true);
|
2015-08-12 02:31:06 +02:00
|
|
|
map->initTerrain();
|
2015-08-18 00:56:16 +02:00
|
|
|
readTerrain();
|
2015-11-13 16:47:47 +02:00
|
|
|
readObjects();
|
2015-11-14 15:50:29 +02:00
|
|
|
|
|
|
|
map->calculateGuardingGreaturePositions();
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
|
|
|
|
2016-02-21 19:58:09 +02:00
|
|
|
void CMapLoaderJson::readHeader(const bool complete)
|
2013-12-31 02:09:58 +03:00
|
|
|
{
|
2015-08-12 02:31:06 +02:00
|
|
|
//do not use map field here, use only mapHeader
|
2016-02-13 09:47:40 +02:00
|
|
|
JsonNode header = getFromArchive(HEADER_FILE_NAME);
|
2016-02-14 13:22:46 +02:00
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
fileVersionMajor = static_cast<int>(header["versionMajor"].Integer());
|
2016-02-14 13:22:46 +02:00
|
|
|
|
2016-02-22 22:43:57 +02:00
|
|
|
if(fileVersionMajor != VERSION_MAJOR)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Unsupported map format version: %d", fileVersionMajor);
|
2016-02-14 13:22:46 +02:00
|
|
|
throw std::runtime_error("Unsupported map format version");
|
|
|
|
}
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
fileVersionMinor = static_cast<int>(header["versionMinor"].Integer());
|
2016-02-14 13:22:46 +02:00
|
|
|
|
2016-02-22 22:43:57 +02:00
|
|
|
if(fileVersionMinor > VERSION_MINOR)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->warn("Too new map format revision: %d. This map should work but some of map features may be ignored.", fileVersionMinor);
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonDeserializer handler(mapObjectResolver.get(), header);
|
2015-08-12 02:31:06 +02:00
|
|
|
|
2016-02-09 19:20:03 +02:00
|
|
|
mapHeader->version = EMapFormat::VCMI;//todo: new version field
|
2023-04-12 00:59:55 +02:00
|
|
|
|
|
|
|
//loading mods
|
|
|
|
if(!header["mods"].isNull())
|
|
|
|
{
|
|
|
|
for(auto & mod : header["mods"].Vector())
|
|
|
|
mapHeader->mods[mod["name"].String()] = CModInfo::Version::fromString(mod["version"].String());
|
|
|
|
}
|
2015-08-12 02:31:06 +02:00
|
|
|
|
2015-08-18 01:21:35 +02:00
|
|
|
//todo: multilevel map load support
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
|
|
|
auto levels = handler.enterStruct("mapLevels");
|
|
|
|
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
auto surface = handler.enterStruct("surface");
|
|
|
|
handler.serializeInt("height", mapHeader->height);
|
|
|
|
handler.serializeInt("width", mapHeader->width);
|
2016-02-13 09:47:40 +02:00
|
|
|
}
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
auto underground = handler.enterStruct("underground");
|
|
|
|
mapHeader->twoLevel = !underground->getCurrent().isNull();
|
2016-02-13 09:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-12 02:31:06 +02:00
|
|
|
|
2016-02-14 13:22:46 +02:00
|
|
|
serializeHeader(handler);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-02-13 14:22:26 +02:00
|
|
|
readTriggeredEvents(handler);
|
2016-02-13 09:47:40 +02:00
|
|
|
|
|
|
|
readTeams(handler);
|
2016-11-13 12:38:42 +02:00
|
|
|
//TODO: check mods
|
2016-02-21 19:58:09 +02:00
|
|
|
|
|
|
|
if(complete)
|
|
|
|
readOptions(handler);
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void CMapLoaderJson::readTerrainTile(const std::string & src, TerrainTile & tile)
|
2015-08-18 00:56:16 +02:00
|
|
|
{
|
2022-09-23 16:24:01 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
using namespace TerrainDetail;
|
|
|
|
{//terrain type
|
|
|
|
const std::string typeCode = src.substr(0, 2);
|
2022-12-20 18:35:40 +02:00
|
|
|
tile.terType = getTerrainByCode(typeCode);
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
int startPos = 2; //0+typeCode fixed length
|
|
|
|
{//terrain view
|
|
|
|
int pos = startPos;
|
|
|
|
while (isdigit(src.at(pos)))
|
|
|
|
pos++;
|
|
|
|
int len = pos - startPos;
|
|
|
|
if (len <= 0)
|
|
|
|
throw std::runtime_error("Invalid terrain view in " + src);
|
|
|
|
const std::string rawCode = src.substr(startPos, len);
|
|
|
|
tile.terView = atoi(rawCode.c_str());
|
|
|
|
startPos += len;
|
|
|
|
}
|
|
|
|
{//terrain flip
|
|
|
|
int terrainFlip = vstd::find_pos(flipCodes, src.at(startPos++));
|
|
|
|
if (terrainFlip < 0)
|
|
|
|
throw std::runtime_error("Invalid terrain flip in " + src);
|
2015-08-18 00:56:16 +02:00
|
|
|
else
|
2022-09-23 16:24:01 +02:00
|
|
|
tile.extTileFlags = terrainFlip;
|
|
|
|
}
|
|
|
|
if (startPos >= src.size())
|
|
|
|
return;
|
|
|
|
bool hasRoad = true;
|
|
|
|
{//road type
|
|
|
|
const std::string typeCode = src.substr(startPos, 2);
|
|
|
|
startPos += 2;
|
2023-03-01 14:11:59 +02:00
|
|
|
tile.roadType = getRoadByCode(typeCode);
|
|
|
|
if(!tile.roadType) //it's not a road, it's a river
|
2015-08-18 00:56:16 +02:00
|
|
|
{
|
2023-03-01 14:11:59 +02:00
|
|
|
tile.roadType = VLC->roadTypeHandler->getById(Road::NO_ROAD);
|
|
|
|
tile.riverType = getRiverByCode(typeCode);
|
|
|
|
hasRoad = false;
|
|
|
|
if(!tile.riverType)
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid river type in " + src);
|
|
|
|
}
|
2023-03-01 14:11:59 +02:00
|
|
|
}
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
if (hasRoad)
|
|
|
|
{//road dir
|
|
|
|
int pos = startPos;
|
|
|
|
while (isdigit(src.at(pos)))
|
|
|
|
pos++;
|
|
|
|
int len = pos - startPos;
|
|
|
|
if (len <= 0)
|
|
|
|
throw std::runtime_error("Invalid road dir in " + src);
|
|
|
|
const std::string rawCode = src.substr(startPos, len);
|
|
|
|
tile.roadDir = atoi(rawCode.c_str());
|
|
|
|
startPos += len;
|
|
|
|
}
|
|
|
|
if (hasRoad)
|
|
|
|
{//road flip
|
|
|
|
int flip = vstd::find_pos(flipCodes, src.at(startPos++));
|
|
|
|
if (flip < 0)
|
|
|
|
throw std::runtime_error("Invalid road flip in " + src);
|
|
|
|
else
|
|
|
|
tile.extTileFlags |= (flip << 4);
|
|
|
|
}
|
|
|
|
if (startPos >= src.size())
|
|
|
|
return;
|
|
|
|
if (hasRoad)
|
|
|
|
{//river type
|
|
|
|
const std::string typeCode = src.substr(startPos, 2);
|
|
|
|
startPos += 2;
|
2022-12-20 18:35:40 +02:00
|
|
|
tile.riverType = getRiverByCode(typeCode);
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
{//river dir
|
|
|
|
int pos = startPos;
|
|
|
|
while (isdigit(src.at(pos)))
|
|
|
|
pos++;
|
|
|
|
int len = pos - startPos;
|
|
|
|
if (len <= 0)
|
|
|
|
throw std::runtime_error("Invalid river dir in " + src);
|
|
|
|
const std::string rawCode = src.substr(startPos, len);
|
|
|
|
tile.riverDir = atoi(rawCode.c_str());
|
|
|
|
startPos += len;
|
|
|
|
}
|
|
|
|
{//river flip
|
|
|
|
int flip = vstd::find_pos(flipCodes, src.at(startPos++));
|
|
|
|
if (flip < 0)
|
|
|
|
throw std::runtime_error("Invalid road flip in " + src);
|
|
|
|
else
|
|
|
|
tile.extTileFlags |= (flip << 2);
|
2015-08-18 01:21:35 +02:00
|
|
|
}
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
2022-12-08 23:20:42 +02:00
|
|
|
catch (const std::exception &)
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
|
|
|
logGlobal->error("Failed to read terrain tile: %s");
|
2015-08-18 01:21:35 +02:00
|
|
|
}
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void CMapLoaderJson::readTerrainLevel(const JsonNode & src, const int index)
|
2015-08-18 00:56:16 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
int3 pos(0, 0, index);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
const JsonVector & rows = src.Vector();
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
if(rows.size() != map->height)
|
2018-10-31 08:27:54 +02:00
|
|
|
throw std::runtime_error("Invalid terrain data");
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
for(pos.y = 0; pos.y < map->height; pos.y++)
|
|
|
|
{
|
|
|
|
const JsonVector & tiles = rows[pos.y].Vector();
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
if(tiles.size() != map->width)
|
2018-10-31 08:27:54 +02:00
|
|
|
throw std::runtime_error("Invalid terrain data");
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
for(pos.x = 0; pos.x < map->width; pos.x++)
|
|
|
|
readTerrainTile(tiles[pos.x].String(), map->getTile(pos));
|
2015-08-18 01:21:35 +02:00
|
|
|
}
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapLoaderJson::readTerrain()
|
|
|
|
{
|
|
|
|
{
|
2016-02-13 09:47:40 +02:00
|
|
|
const JsonNode surface = getFromArchive("surface_terrain.json");
|
2015-08-18 01:21:35 +02:00
|
|
|
readTerrainLevel(surface, 0);
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
if(map->twoLevel)
|
|
|
|
{
|
2016-02-13 09:47:40 +02:00
|
|
|
const JsonNode underground = getFromArchive("underground_terrain.json");
|
2015-08-18 01:21:35 +02:00
|
|
|
readTerrainLevel(underground, 1);
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
CMapLoaderJson::MapObjectLoader::MapObjectLoader(CMapLoaderJson * _owner, JsonMap::value_type & json):
|
|
|
|
owner(_owner), instance(nullptr), id(-1), jsonKey(json.first), configuration(json.second)
|
2015-11-13 16:47:47 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapLoaderJson::MapObjectLoader::construct()
|
|
|
|
{
|
2015-11-14 15:50:29 +02:00
|
|
|
//TODO:consider move to ObjectTypeHandler
|
2015-11-13 16:47:47 +02:00
|
|
|
//find type handler
|
2023-02-11 18:30:06 +02:00
|
|
|
std::string typeName = configuration["type"].String();
|
|
|
|
std::string subtypeName = configuration["subtype"].String();
|
2015-11-13 16:47:47 +02:00
|
|
|
if(typeName.empty())
|
|
|
|
{
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->error("Object type missing");
|
2017-08-11 15:27:42 +02:00
|
|
|
logGlobal->debug(configuration.toJson());
|
2015-11-13 16:47:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-02-13 16:16:00 +02:00
|
|
|
|
2016-02-23 22:46:33 +02:00
|
|
|
int3 pos;
|
2020-10-01 10:38:06 +02:00
|
|
|
pos.x = static_cast<si32>(configuration["x"].Float());
|
|
|
|
pos.y = static_cast<si32>(configuration["y"].Float());
|
|
|
|
pos.z = static_cast<si32>(configuration["l"].Float());
|
2015-11-13 16:47:47 +02:00
|
|
|
|
2016-01-24 01:26:32 +02:00
|
|
|
//special case for grail
|
2022-12-31 15:01:19 +02:00
|
|
|
if(typeName == "grail")
|
2016-01-24 01:26:32 +02:00
|
|
|
{
|
2016-02-23 22:46:33 +02:00
|
|
|
owner->map->grailPos = pos;
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
owner->map->grailRadius = static_cast<int>(configuration["options"]["grailRadius"].Float());
|
2016-02-13 16:16:00 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-02-23 22:46:33 +02:00
|
|
|
else if(subtypeName.empty())
|
2016-02-13 16:16:00 +02:00
|
|
|
{
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->error("Object subtype missing");
|
2017-08-11 15:27:42 +02:00
|
|
|
logGlobal->debug(configuration.toJson());
|
2016-02-13 16:16:00 +02:00
|
|
|
return;
|
2016-01-24 01:26:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-07 15:18:19 +02:00
|
|
|
auto handler = VLC->objtypeh->getHandlerFor( CModHandler::scopeMap(), typeName, subtypeName);
|
2016-02-23 22:46:33 +02:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
auto * appearance = new ObjectTemplate;
|
2016-02-23 22:46:33 +02:00
|
|
|
|
2023-01-19 00:54:19 +02:00
|
|
|
appearance->id = Obj(handler->getIndex());
|
|
|
|
appearance->subid = handler->getSubIndex();
|
2022-09-11 15:12:35 +02:00
|
|
|
appearance->readJson(configuration["template"], false);
|
2015-11-14 15:50:29 +02:00
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
// Will be destroyed soon and replaced with shared template
|
|
|
|
instance = handler->create(std::shared_ptr<const ObjectTemplate>(appearance));
|
2016-02-26 01:03:14 +02:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
instance->id = ObjectInstanceID(static_cast<si32>(owner->map->objects.size()));
|
2016-02-22 18:26:42 +02:00
|
|
|
instance->instanceName = jsonKey;
|
2016-02-23 22:46:33 +02:00
|
|
|
instance->pos = pos;
|
2016-02-22 18:26:42 +02:00
|
|
|
owner->map->addNewObject(instance);
|
2015-11-13 16:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapLoaderJson::MapObjectLoader::configure()
|
|
|
|
{
|
2016-01-24 01:26:32 +02:00
|
|
|
if(nullptr == instance)
|
|
|
|
return;
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonDeserializer handler(owner->mapObjectResolver.get(), configuration);
|
2016-02-22 01:37:19 +02:00
|
|
|
|
|
|
|
instance->serializeJson(handler);
|
2015-12-05 11:04:00 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
//artifact instance serialization requires access to Map object, handle it here for now
|
|
|
|
//todo: find better solution for artifact instance serialization
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
if(auto * art = dynamic_cast<CGArtifact *>(instance))
|
2016-02-13 18:43:05 +02:00
|
|
|
{
|
2022-11-29 00:11:46 +02:00
|
|
|
auto artID = ArtifactID::NONE;
|
2016-02-13 18:43:05 +02:00
|
|
|
int spellID = -1;
|
|
|
|
|
|
|
|
if(art->ID == Obj::SPELL_SCROLL)
|
|
|
|
{
|
|
|
|
auto spellIdentifier = configuration["options"]["spell"].String();
|
2022-12-07 15:18:19 +02:00
|
|
|
auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeBuiltin(), "spell", spellIdentifier);
|
2016-02-13 18:43:05 +02:00
|
|
|
if(rawId)
|
2023-04-16 19:42:56 +02:00
|
|
|
spellID = rawId.value();
|
2016-02-13 18:43:05 +02:00
|
|
|
else
|
|
|
|
spellID = 0;
|
|
|
|
artID = ArtifactID::SPELL_SCROLL;
|
|
|
|
}
|
|
|
|
else if(art->ID == Obj::ARTIFACT)
|
|
|
|
{
|
|
|
|
//specific artifact
|
2022-11-29 00:11:46 +02:00
|
|
|
artID = ArtifactID(art->subID);
|
2016-02-13 18:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
art->storedArtifact = CArtifactInstance::createArtifact(owner->map, artID, spellID);
|
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
if(auto * hero = dynamic_cast<CGHeroInstance *>(instance))
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
auto o = handler.enterStruct("options");
|
|
|
|
hero->serializeJsonArtifacts(handler, "artifacts", owner->map);
|
|
|
|
}
|
2015-11-13 16:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapLoaderJson::readObjects()
|
|
|
|
{
|
|
|
|
LOG_TRACE(logGlobal);
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<MapObjectLoader>> loaders;//todo: optimize MapObjectLoader memory layout
|
|
|
|
|
2016-02-22 01:37:19 +02:00
|
|
|
JsonNode data = getFromArchive(OBJECTS_FILE_NAME);
|
2015-11-13 16:47:47 +02:00
|
|
|
|
|
|
|
//get raw data
|
2016-02-22 01:37:19 +02:00
|
|
|
for(auto & p : data.Struct())
|
2022-12-07 23:36:20 +02:00
|
|
|
loaders.push_back(std::make_unique<MapObjectLoader>(this, p));
|
2015-11-13 16:47:47 +02:00
|
|
|
|
|
|
|
for(auto & ptr : loaders)
|
|
|
|
ptr->construct();
|
|
|
|
|
|
|
|
//configure objects after all objects are constructed so we may resolve internal IDs even to actual pointers OTF
|
|
|
|
for(auto & ptr : loaders)
|
|
|
|
ptr->configure();
|
2016-02-22 03:53:14 +02:00
|
|
|
|
|
|
|
std::sort(map->heroesOnMap.begin(), map->heroesOnMap.end(), [](const ConstTransitivePtr<CGHeroInstance> & a, const ConstTransitivePtr<CGHeroInstance> & b)
|
|
|
|
{
|
|
|
|
return a->subID < b->subID;
|
|
|
|
});
|
2015-11-13 16:47:47 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 16:30:19 +02:00
|
|
|
///CMapSaverJson
|
2023-02-11 18:30:06 +02:00
|
|
|
CMapSaverJson::CMapSaverJson(CInputOutputStream * stream)
|
|
|
|
: buffer(stream)
|
|
|
|
, ioApi(new CProxyIOApi(buffer))
|
|
|
|
, saver(ioApi, "_")
|
2015-08-08 16:30:19 +02:00
|
|
|
{
|
2016-02-22 22:43:57 +02:00
|
|
|
fileVersionMajor = VERSION_MAJOR;
|
|
|
|
fileVersionMinor = VERSION_MINOR;
|
2015-08-08 16:30:19 +02:00
|
|
|
}
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
//must be instantiated in .cpp file for access to complete types of all member fields
|
|
|
|
CMapSaverJson::~CMapSaverJson() = default;
|
2015-08-08 17:35:54 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void CMapSaverJson::addToArchive(const JsonNode & data, const std::string & filename)
|
2015-08-18 00:56:16 +02:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
2017-08-11 15:27:42 +02:00
|
|
|
JsonWriter writer(out);
|
|
|
|
writer.writeNode(data);
|
2015-08-18 00:56:16 +02:00
|
|
|
out.flush();
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
{
|
|
|
|
auto s = out.str();
|
|
|
|
std::unique_ptr<COutputStream> stream = saver.addFile(filename);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
if(stream->write(reinterpret_cast<const ui8 *>(s.c_str()), s.size()) != s.size())
|
2018-10-31 08:27:54 +02:00
|
|
|
throw std::runtime_error("CMapSaverJson::saveHeader() zip compression failed.");
|
2015-08-18 01:21:35 +02:00
|
|
|
}
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 17:35:54 +02:00
|
|
|
void CMapSaverJson::saveMap(const std::unique_ptr<CMap>& map)
|
|
|
|
{
|
2015-08-11 20:20:13 +02:00
|
|
|
this->map = map.get();
|
2016-02-13 14:22:26 +02:00
|
|
|
this->mapHeader = this->map;
|
2015-08-18 01:21:35 +02:00
|
|
|
writeHeader();
|
2015-08-18 00:56:16 +02:00
|
|
|
writeTerrain();
|
2015-11-13 16:47:47 +02:00
|
|
|
writeObjects();
|
2015-08-08 17:35:54 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
void CMapSaverJson::writeHeader()
|
2015-08-11 20:20:13 +02:00
|
|
|
{
|
2017-06-04 19:33:28 +02:00
|
|
|
logGlobal->trace("Saving header");
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
JsonNode header;
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonSerializer handler(mapObjectResolver.get(), header);
|
2016-02-13 09:47:40 +02:00
|
|
|
|
2015-08-12 02:31:06 +02:00
|
|
|
header["versionMajor"].Float() = VERSION_MAJOR;
|
2015-08-18 01:21:35 +02:00
|
|
|
header["versionMinor"].Float() = VERSION_MINOR;
|
2023-04-12 00:59:55 +02:00
|
|
|
|
|
|
|
//write mods
|
|
|
|
JsonNode & mods = header["mods"];
|
|
|
|
for(const auto & mod : mapHeader->mods)
|
|
|
|
{
|
|
|
|
JsonNode modWriter;
|
|
|
|
modWriter["name"].String() = mod.first;
|
|
|
|
modWriter["version"].String() = mod.second.toString();
|
|
|
|
mods.Vector().push_back(modWriter);
|
|
|
|
}
|
2015-08-18 01:21:35 +02:00
|
|
|
|
|
|
|
//todo: multilevel map save support
|
2015-08-13 23:42:01 +02:00
|
|
|
JsonNode & levels = header["mapLevels"];
|
2016-02-13 14:22:26 +02:00
|
|
|
levels["surface"]["height"].Float() = mapHeader->height;
|
|
|
|
levels["surface"]["width"].Float() = mapHeader->width;
|
2015-08-12 02:31:06 +02:00
|
|
|
levels["surface"]["index"].Float() = 0;
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-02-13 14:22:26 +02:00
|
|
|
if(mapHeader->twoLevel)
|
2015-08-12 02:31:06 +02:00
|
|
|
{
|
2016-02-13 14:22:26 +02:00
|
|
|
levels["underground"]["height"].Float() = mapHeader->height;
|
|
|
|
levels["underground"]["width"].Float() = mapHeader->width;
|
2015-08-12 02:31:06 +02:00
|
|
|
levels["underground"]["index"].Float() = 1;
|
|
|
|
}
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-02-14 13:22:46 +02:00
|
|
|
serializeHeader(handler);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-02-14 13:22:46 +02:00
|
|
|
writeTriggeredEvents(handler);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-02-14 13:22:46 +02:00
|
|
|
writeTeams(handler);
|
2015-11-13 03:01:14 +02:00
|
|
|
|
2016-02-21 19:58:09 +02:00
|
|
|
writeOptions(handler);
|
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
addToArchive(header, HEADER_FILE_NAME);
|
|
|
|
}
|
|
|
|
|
2017-08-03 12:50:54 +02:00
|
|
|
std::string CMapSaverJson::writeTerrainTile(const TerrainTile & tile)
|
2015-08-18 00:56:16 +02:00
|
|
|
{
|
|
|
|
using namespace TerrainDetail;
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
std::ostringstream out;
|
2015-08-18 00:56:16 +02:00
|
|
|
out.setf(std::ios::dec, std::ios::basefield);
|
|
|
|
out.unsetf(std::ios::showbase);
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
out << tile.terType->shortIdentifier << static_cast<int>(tile.terView) << flipCodes[tile.extTileFlags % 4];
|
2015-08-18 00:56:16 +02:00
|
|
|
|
2023-01-01 17:10:47 +02:00
|
|
|
if(tile.roadType->getId() != Road::NO_ROAD)
|
2023-02-11 18:30:06 +02:00
|
|
|
out << tile.roadType->shortIdentifier << static_cast<int>(tile.roadDir) << flipCodes[(tile.extTileFlags >> 4) % 4];
|
2015-08-18 00:56:16 +02:00
|
|
|
|
2023-01-01 17:10:47 +02:00
|
|
|
if(tile.riverType->getId() != River::NO_RIVER)
|
2023-02-11 18:30:06 +02:00
|
|
|
out << tile.riverType->shortIdentifier << static_cast<int>(tile.riverDir) << flipCodes[(tile.extTileFlags >> 2) % 4];
|
2015-08-18 00:56:16 +02:00
|
|
|
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CMapSaverJson::writeTerrainLevel(const int index)
|
|
|
|
{
|
|
|
|
JsonNode data;
|
|
|
|
int3 pos(0,0,index);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
data.Vector().resize(map->height);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
for(pos.y = 0; pos.y < map->height; pos.y++)
|
2015-08-11 20:20:13 +02:00
|
|
|
{
|
2015-08-18 00:56:16 +02:00
|
|
|
JsonNode & row = data.Vector()[pos.y];
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
row.Vector().resize(map->width);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
for(pos.x = 0; pos.x < map->width; pos.x++)
|
|
|
|
row.Vector()[pos.x].String() = writeTerrainTile(map->getTile(pos));
|
|
|
|
}
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2019-01-19 12:52:02 +02:00
|
|
|
return data;
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapSaverJson::writeTerrain()
|
|
|
|
{
|
2017-06-04 19:33:28 +02:00
|
|
|
logGlobal->trace("Saving terrain");
|
2015-08-18 01:21:35 +02:00
|
|
|
//todo: multilevel map save support
|
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
JsonNode surface = writeTerrainLevel(0);
|
2015-08-20 05:16:31 +02:00
|
|
|
addToArchive(surface, "surface_terrain.json");
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-18 00:56:16 +02:00
|
|
|
if(map->twoLevel)
|
|
|
|
{
|
|
|
|
JsonNode underground = writeTerrainLevel(1);
|
2015-08-20 05:16:31 +02:00
|
|
|
addToArchive(underground, "underground_terrain.json");
|
2015-08-18 00:56:16 +02:00
|
|
|
}
|
2015-08-11 20:20:13 +02:00
|
|
|
}
|
2015-11-13 16:47:47 +02:00
|
|
|
|
|
|
|
void CMapSaverJson::writeObjects()
|
|
|
|
{
|
2017-06-04 19:33:28 +02:00
|
|
|
logGlobal->trace("Saving objects");
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode data(JsonNode::JsonType::DATA_STRUCT);
|
2015-11-13 16:47:47 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonSerializer handler(mapObjectResolver.get(), data);
|
2016-02-22 01:37:19 +02:00
|
|
|
|
|
|
|
for(CGObjectInstance * obj : map->objects)
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
//logGlobal->trace("\t%s", obj->instanceName);
|
2016-02-22 18:26:42 +02:00
|
|
|
auto temp = handler.enterStruct(obj->instanceName);
|
2016-02-22 01:37:19 +02:00
|
|
|
|
|
|
|
obj->serializeJson(handler);
|
|
|
|
}
|
2015-11-13 16:47:47 +02:00
|
|
|
|
2016-01-24 01:26:32 +02:00
|
|
|
if(map->grailPos.valid())
|
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode grail(JsonNode::JsonType::DATA_STRUCT);
|
2016-01-24 01:26:32 +02:00
|
|
|
grail["type"].String() = "grail";
|
|
|
|
|
|
|
|
grail["x"].Float() = map->grailPos.x;
|
|
|
|
grail["y"].Float() = map->grailPos.y;
|
|
|
|
grail["l"].Float() = map->grailPos.z;
|
|
|
|
|
2016-02-04 11:28:12 +02:00
|
|
|
grail["options"]["radius"].Float() = map->grailRadius;
|
2016-01-24 01:26:32 +02:00
|
|
|
|
|
|
|
std::string grailId = boost::str(boost::format("grail_%d") % map->objects.size());
|
|
|
|
|
|
|
|
data[grailId] = grail;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
//cleanup empty options
|
|
|
|
for(auto & p : data.Struct())
|
|
|
|
{
|
|
|
|
JsonNode & obj = p.second;
|
|
|
|
if(obj["options"].Struct().empty())
|
|
|
|
obj.Struct().erase("options");
|
|
|
|
}
|
|
|
|
|
2015-11-13 16:47:47 +02:00
|
|
|
addToArchive(data, OBJECTS_FILE_NAME);
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|