2013-12-31 02:09:58 +03: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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "MapFormatJson.h"
|
|
|
|
|
2015-08-08 17:35:54 +02:00
|
|
|
#include "../filesystem/CInputStream.h"
|
|
|
|
#include "../filesystem/COutputStream.h"
|
2013-12-31 02:09:58 +03:00
|
|
|
#include "CMap.h"
|
|
|
|
#include "../CModHandler.h"
|
|
|
|
#include "../VCMI_Lib.h"
|
|
|
|
|
2015-08-14 04:22:24 +02:00
|
|
|
static const std::array<std::string, 12> conditionNames = {
|
2014-02-09 15:10:02 +03:00
|
|
|
"haveArtifact", "haveCreatures", "haveResources", "haveBuilding",
|
|
|
|
"control", "destroy", "transport", "daysPassed",
|
|
|
|
"isHuman", "daysWithoutTown", "standardWin", "constValue"
|
2013-12-31 02:09:58 +03:00
|
|
|
};
|
|
|
|
|
2015-08-14 04:22:24 +02:00
|
|
|
static const std::array<std::string, 2> typeNames = { "victory", "defeat" };
|
2013-12-31 02:09:58 +03:00
|
|
|
|
2015-08-07 21:33:44 +02:00
|
|
|
static EventCondition JsonToCondition(const JsonNode & node)
|
|
|
|
{
|
|
|
|
EventCondition event;
|
|
|
|
event.condition = EventCondition::EWinLoseType(vstd::find_pos(conditionNames, node.Vector()[0].String()));
|
|
|
|
if (node.Vector().size() > 1)
|
|
|
|
{
|
|
|
|
const JsonNode & data = node.Vector()[1];
|
|
|
|
if (data["type"].getType() == JsonNode::DATA_STRING)
|
|
|
|
event.objectType = VLC->modh->identifiers.getIdentifier(data["type"]).get();
|
|
|
|
if (data["type"].getType() == JsonNode::DATA_FLOAT)
|
|
|
|
event.objectType = data["type"].Float();
|
|
|
|
|
|
|
|
if (!data["value"].isNull())
|
|
|
|
event.value = data["value"].Float();
|
|
|
|
|
|
|
|
if (!data["position"].isNull())
|
|
|
|
{
|
2015-08-15 17:46:29 +02:00
|
|
|
auto & position = data["position"].Vector();
|
|
|
|
event.position.x = position.at(0).Float();
|
|
|
|
event.position.y = position.at(1).Float();
|
|
|
|
event.position.z = position.at(2).Float();
|
2015-08-07 21:33:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2015-08-15 17:09:39 +02:00
|
|
|
static JsonNode ConditionToJson(const EventCondition& event)
|
2015-08-14 04:22:24 +02:00
|
|
|
{
|
2015-08-15 17:09:39 +02:00
|
|
|
JsonNode json;
|
2015-08-14 04:22:24 +02:00
|
|
|
|
2015-08-15 17:09:39 +02:00
|
|
|
JsonVector& asVector = json.Vector();
|
|
|
|
|
|
|
|
JsonNode condition;
|
|
|
|
condition.String() = conditionNames.at(event.condition);
|
|
|
|
asVector.push_back(condition);
|
|
|
|
|
|
|
|
JsonNode data;
|
|
|
|
|
|
|
|
//todo: save identifier
|
|
|
|
|
|
|
|
if(event.value != -1)
|
|
|
|
data["value"].Float() = event.value;
|
|
|
|
|
|
|
|
if(event.position != int3(-1,-1,-1))
|
|
|
|
{
|
|
|
|
auto & position = data["position"].Vector();
|
2015-08-15 17:46:29 +02:00
|
|
|
position.resize(3);
|
|
|
|
position[0].Float() = event.position.x;
|
|
|
|
position[1].Float() = event.position.y;
|
|
|
|
position[2].Float() = event.position.z;
|
2015-08-15 17:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
asVector.push_back(data);
|
|
|
|
|
|
|
|
return std::move(json);
|
2015-08-14 04:22:24 +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;
|
|
|
|
const int CMapFormatJson::VERSION_MINOR = 0;
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
const std::string CMapFormatJson::HEADER_FILE_NAME = "header.json";
|
|
|
|
|
2015-08-07 21:33:44 +02:00
|
|
|
void CMapFormatJson::readTriggeredEvents(const JsonNode & input)
|
|
|
|
{
|
|
|
|
mapHeader->victoryMessage = input["victoryString"].String();
|
|
|
|
mapHeader->victoryIconIndex = input["victoryIconIndex"].Float();
|
|
|
|
|
|
|
|
mapHeader->defeatMessage = input["defeatString"].String();
|
|
|
|
mapHeader->defeatIconIndex = input["defeatIconIndex"].Float();
|
|
|
|
|
|
|
|
mapHeader->triggeredEvents.clear();
|
|
|
|
|
|
|
|
for (auto & entry : input["triggeredEvents"].Struct())
|
|
|
|
{
|
|
|
|
TriggeredEvent event;
|
|
|
|
event.identifier = entry.first;
|
|
|
|
readTriggeredEvent(event, entry.second);
|
|
|
|
mapHeader->triggeredEvents.push_back(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode & source)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:22:24 +02:00
|
|
|
void CMapFormatJson::writeTriggeredEvents(JsonNode& output)
|
|
|
|
{
|
|
|
|
output["victoryString"].String() = map->victoryMessage;
|
|
|
|
output["victoryIconIndex"].Float() = map->victoryIconIndex;
|
|
|
|
|
|
|
|
output["defeatString"].String() = map->defeatMessage;
|
|
|
|
output["defeatIconIndex"].Float() = map->defeatIconIndex;
|
|
|
|
|
2015-08-15 17:09:39 +02:00
|
|
|
JsonMap & triggeredEvents = output["triggeredEvents"].Struct();
|
|
|
|
|
|
|
|
for(auto event : map->triggeredEvents)
|
|
|
|
writeTriggeredEvent(event, triggeredEvents[event.identifier]);
|
2015-08-14 04:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapFormatJson::writeTriggeredEvent(const TriggeredEvent& event, JsonNode& dest)
|
|
|
|
{
|
|
|
|
dest["message"].String() = event.onFulfill;
|
|
|
|
dest["description"].String() = event.description;
|
|
|
|
|
2015-08-15 17:46:29 +02:00
|
|
|
dest["effect"]["type"].String() = typeNames.at(size_t(event.effect.type));
|
2015-08-14 04:22:24 +02:00
|
|
|
dest["effect"]["messageToSend"].String() = event.effect.toOtherMessage;
|
|
|
|
|
2015-08-15 17:09:39 +02:00
|
|
|
dest["condition"] = event.trigger.toJson(ConditionToJson);
|
2015-08-14 04:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-07 21:33:44 +02:00
|
|
|
///CMapPatcher
|
|
|
|
CMapPatcher::CMapPatcher(JsonNode stream):
|
2013-12-31 02:09:58 +03:00
|
|
|
input(stream)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-07 21:33:44 +02:00
|
|
|
void CMapPatcher::patchMapHeader(std::unique_ptr<CMapHeader> & header)
|
|
|
|
{
|
|
|
|
header.swap(mapHeader);
|
|
|
|
if (!input.isNull())
|
|
|
|
readPatchData();
|
|
|
|
header.swap(mapHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapPatcher::readPatchData()
|
|
|
|
{
|
|
|
|
readTriggeredEvents(input);
|
|
|
|
}
|
|
|
|
|
2015-08-12 02:31:06 +02:00
|
|
|
///CMapFormatZip
|
|
|
|
CMapFormatZip::CMapFormatZip(CInputOutputStream * stream):
|
|
|
|
buffer(stream),
|
|
|
|
ioApi(new CProxyIOApi(buffer))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-07 21:33:44 +02:00
|
|
|
///CMapLoaderJson
|
2015-08-12 02:31:06 +02:00
|
|
|
CMapLoaderJson::CMapLoaderJson(CInputOutputStream * stream):
|
|
|
|
CMapFormatZip(stream),
|
|
|
|
loader("", "_", ioApi)
|
2015-08-07 21:33:44 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
std::unique_ptr<CMap> CMapLoaderJson::loadMap()
|
|
|
|
{
|
|
|
|
map = new CMap();
|
2015-08-08 18:16:33 +02:00
|
|
|
mapHeader = std::unique_ptr<CMapHeader>(dynamic_cast<CMapHeader *>(map));
|
2013-12-31 02:09:58 +03:00
|
|
|
readMap();
|
2015-08-08 18:16:33 +02:00
|
|
|
return std::unique_ptr<CMap>(dynamic_cast<CMap *>(mapHeader.release()));
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CMapHeader> CMapLoaderJson::loadMapHeader()
|
|
|
|
{
|
|
|
|
mapHeader.reset(new CMapHeader);
|
|
|
|
readHeader();
|
|
|
|
return std::move(mapHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapLoaderJson::readMap()
|
|
|
|
{
|
|
|
|
readHeader();
|
2015-08-12 02:31:06 +02:00
|
|
|
map->initTerrain();
|
2015-08-08 18:16:33 +02:00
|
|
|
//TODO:readMap
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapLoaderJson::readHeader()
|
|
|
|
{
|
2015-08-12 02:31:06 +02:00
|
|
|
//do not use map field here, use only mapHeader
|
|
|
|
ResourceID headerID(HEADER_FILE_NAME, EResType::TEXT);
|
|
|
|
|
|
|
|
if(!loader.existsResource(headerID))
|
|
|
|
throw new std::runtime_error(HEADER_FILE_NAME+" not found");
|
|
|
|
|
|
|
|
auto headerData = loader.load(headerID)->readAll();
|
|
|
|
|
|
|
|
const JsonNode header(reinterpret_cast<char*>(headerData.first.get()), headerData.second);
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
//TODO: read such data like map name & size
|
2015-08-12 02:31:06 +02:00
|
|
|
//mapHeader->version = ??? //todo: new version field
|
|
|
|
|
|
|
|
//todo: multilevel map load support
|
|
|
|
const JsonNode levels = header["mapLevels"];
|
|
|
|
mapHeader->height = levels["surface"]["height"].Float();
|
|
|
|
mapHeader->width = levels["surface"]["width"].Float();
|
|
|
|
mapHeader->twoLevel = !levels["underground"].isNull();
|
|
|
|
|
|
|
|
mapHeader->name = header["name"].String();
|
|
|
|
mapHeader->description = header["description"].String();
|
|
|
|
|
|
|
|
//todo: support arbitrary percentage
|
|
|
|
|
|
|
|
static const std::map<std::string, ui8> difficultyMap =
|
|
|
|
{
|
2015-08-13 23:16:31 +02:00
|
|
|
{"", 1},
|
2015-08-12 02:31:06 +02:00
|
|
|
{"EASY", 0},
|
|
|
|
{"NORMAL", 1},
|
|
|
|
{"HARD", 2},
|
|
|
|
{"EXPERT", 3},
|
|
|
|
{"IMPOSSIBLE", 4}
|
|
|
|
};
|
|
|
|
|
|
|
|
mapHeader->difficulty = difficultyMap.at(header["difficulty"].String());
|
|
|
|
mapHeader->levelLimit = header["levelLimit"].Float();
|
|
|
|
|
|
|
|
|
|
|
|
// std::vector<bool> allowedHeroes;
|
|
|
|
// std::vector<ui16> placeholdedHeroes;
|
|
|
|
|
|
|
|
readTriggeredEvents(header);
|
2013-12-31 02:09:58 +03:00
|
|
|
readPlayerInfo();
|
2015-08-08 18:16:33 +02:00
|
|
|
//TODO: readHeader
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapLoaderJson::readPlayerInfo()
|
|
|
|
{
|
2015-08-12 02:31:06 +02:00
|
|
|
//ui8 howManyTeams;
|
2015-08-08 18:16:33 +02:00
|
|
|
//TODO: readPlayerInfo
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
2015-08-08 16:30:19 +02:00
|
|
|
|
|
|
|
///CMapSaverJson
|
2015-08-11 20:20:13 +02:00
|
|
|
CMapSaverJson::CMapSaverJson(CInputOutputStream * stream):
|
2015-08-12 02:31:06 +02:00
|
|
|
CMapFormatZip(stream),
|
2015-08-11 20:20:13 +02:00
|
|
|
saver(ioApi, "_")
|
2015-08-08 16:30:19 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
CMapSaverJson::~CMapSaverJson()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2015-08-08 17:35:54 +02:00
|
|
|
|
|
|
|
void CMapSaverJson::saveMap(const std::unique_ptr<CMap>& map)
|
|
|
|
{
|
2015-08-08 18:16:33 +02:00
|
|
|
//TODO: saveMap
|
2015-08-11 20:20:13 +02:00
|
|
|
this->map = map.get();
|
2015-08-12 02:31:06 +02:00
|
|
|
saveHeader();
|
|
|
|
|
2015-08-08 17:35:54 +02:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
void CMapSaverJson::saveHeader()
|
|
|
|
{
|
|
|
|
JsonNode header;
|
2015-08-12 02:31:06 +02:00
|
|
|
header["versionMajor"].Float() = VERSION_MAJOR;
|
|
|
|
header["versionMinor"].Float() = VERSION_MINOR;
|
|
|
|
|
|
|
|
//todo: multilevel map save support
|
2015-08-13 23:42:01 +02:00
|
|
|
JsonNode & levels = header["mapLevels"];
|
2015-08-12 02:31:06 +02:00
|
|
|
levels["surface"]["height"].Float() = map->height;
|
|
|
|
levels["surface"]["width"].Float() = map->width;
|
|
|
|
levels["surface"]["index"].Float() = 0;
|
|
|
|
|
|
|
|
if(map->twoLevel)
|
|
|
|
{
|
|
|
|
levels["underground"]["height"].Float() = map->height;
|
|
|
|
levels["underground"]["width"].Float() = map->width;
|
|
|
|
levels["underground"]["index"].Float() = 1;
|
|
|
|
}
|
2015-08-11 20:20:13 +02:00
|
|
|
|
|
|
|
header["name"].String() = map->name;
|
2015-08-12 02:31:06 +02:00
|
|
|
header["description"].String() = map->description;
|
|
|
|
|
|
|
|
|
|
|
|
//todo: support arbitrary percentage
|
|
|
|
static const std::map<ui8, std::string> difficultyMap =
|
|
|
|
{
|
|
|
|
{0, "EASY"},
|
|
|
|
{1, "NORMAL"},
|
|
|
|
{2, "HARD"},
|
|
|
|
{3, "EXPERT"},
|
|
|
|
{4, "IMPOSSIBLE"}
|
|
|
|
};
|
|
|
|
|
|
|
|
header["difficulty"].String() = difficultyMap.at(map->difficulty);
|
|
|
|
header["levelLimit"].Float() = map->levelLimit;
|
|
|
|
|
2015-08-14 04:22:24 +02:00
|
|
|
writeTriggeredEvents(header);
|
|
|
|
|
|
|
|
//todo: player info
|
|
|
|
|
2015-08-12 02:31:06 +02:00
|
|
|
//todo: allowedHeroes;
|
|
|
|
//todo: placeholdedHeroes;
|
2015-08-11 20:20:13 +02:00
|
|
|
|
|
|
|
std::ostringstream out;
|
|
|
|
out << header;
|
|
|
|
out.flush();
|
|
|
|
|
|
|
|
{
|
|
|
|
auto s = out.str();
|
2015-08-12 02:31:06 +02:00
|
|
|
std::unique_ptr<COutputStream> stream = saver.addFile(HEADER_FILE_NAME);
|
2015-08-11 20:20:13 +02:00
|
|
|
|
2015-08-13 02:29:13 +02:00
|
|
|
if (stream->write((const ui8*)s.c_str(), s.size()) != s.size())
|
|
|
|
throw new std::runtime_error("CMapSaverJson::saveHeader() zip compression failed.");
|
2015-08-11 20:20:13 +02:00
|
|
|
}
|
|
|
|
}
|