From 8a979d016f4c0e64f6ed4b4ddf0989c28efda0fd Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Sat, 14 Nov 2015 23:57:22 +0300 Subject: [PATCH] ObjectTemplate serialization (+) generated map displayed correctly in editor --- lib/mapObjects/CObjectHandler.cpp | 4 +- lib/mapObjects/ObjectTemplate.cpp | 96 +++++++++++++++++++++++++++++-- lib/mapObjects/ObjectTemplate.h | 4 +- 3 files changed, 94 insertions(+), 10 deletions(-) diff --git a/lib/mapObjects/CObjectHandler.cpp b/lib/mapObjects/CObjectHandler.cpp index bc4169a61..a6b3182ff 100644 --- a/lib/mapObjects/CObjectHandler.cpp +++ b/lib/mapObjects/CObjectHandler.cpp @@ -335,7 +335,7 @@ void CGObjectInstance::writeJson(JsonNode & json, bool withState) const json["y"].Float() = pos.y; json["l"].Float() = pos.z; - appearance.writeJson(json["template"]); + appearance.writeJson(json["template"], false); writeJsonOptions(json["options"]); if(withState) writeJsonState(json["state"]); @@ -352,7 +352,7 @@ void CGObjectInstance::readJson(const JsonNode & json, bool withState) pos.y = json["y"].Float(); pos.z = json["l"].Float(); - appearance.readJson(json["template"]); + appearance.readJson(json["template"], false); readJsonOptions(json["options"]); if(withState) readJsonState(json["state"]); diff --git a/lib/mapObjects/ObjectTemplate.cpp b/lib/mapObjects/ObjectTemplate.cpp index eb0bd584f..bf87a4b2a 100644 --- a/lib/mapObjects/ObjectTemplate.cpp +++ b/lib/mapObjects/ObjectTemplate.cpp @@ -183,7 +183,7 @@ void ObjectTemplate::readMap(CBinaryReader & reader) } } -void ObjectTemplate::readJson(const JsonNode &node) +void ObjectTemplate::readJson(const JsonNode &node, const bool withTerrain) { animationFile = node["animation"].String(); @@ -202,7 +202,7 @@ void ObjectTemplate::readJson(const JsonNode &node) else visitDir = 0x00; - if (!node["allowedTerrains"].isNull()) + if(withTerrain && !node["allowedTerrains"].isNull()) { for (auto & entry : node["allowedTerrains"].Vector()) allowedTerrains.insert(ETerrainType(vstd::find_pos(GameConstants::TERRAIN_NAMES, entry.String()))); @@ -211,11 +211,14 @@ void ObjectTemplate::readJson(const JsonNode &node) { for (size_t i=0; i< GameConstants::TERRAIN_TYPES; i++) allowedTerrains.insert(ETerrainType(i)); + + allowedTerrains.erase(ETerrainType::ROCK); } - if (allowedTerrains.empty()) + if(withTerrain && allowedTerrains.empty()) logGlobal->warnStream() << "Loaded template without allowed terrains!"; + auto charToTile = [&](const char & ch) -> ui8 { switch (ch) @@ -256,10 +259,91 @@ void ObjectTemplate::readJson(const JsonNode &node) printPriority = 0; //default value } -void ObjectTemplate::writeJson(JsonNode & node) const +void ObjectTemplate::writeJson(JsonNode & node, const bool withTerrain) const { - node.setType(JsonNode::DATA_STRUCT); - //todo: ObjectTemplate::writeJson + node["animation"].String() = animationFile; + + if(visitDir != 0x0) + { + JsonVector & visitDirs = node["visitableFrom"].Vector(); + visitDirs.resize(3); + + visitDirs[0].String().resize(3); + visitDirs[1].String().resize(3); + visitDirs[2].String().resize(3); + + visitDirs[0].String()[0] = (visitDir & 1) ? '+' : '-'; + visitDirs[0].String()[1] = (visitDir & 2) ? '+' : '-'; + visitDirs[0].String()[2] = (visitDir & 4) ? '+' : '-'; + visitDirs[1].String()[2] = (visitDir & 8) ? '+' : '-'; + visitDirs[2].String()[2] = (visitDir & 16) ? '+' : '-'; + visitDirs[2].String()[1] = (visitDir & 32) ? '+' : '-'; + visitDirs[2].String()[0] = (visitDir & 64) ? '+' : '-'; + visitDirs[1].String()[0] = (visitDir & 128) ? '+' : '-'; + + visitDirs[1].String()[1] = '-';//??? center + } + + if(withTerrain) + { + //assumed that ROCK terrain not included + if(allowedTerrains.size() < (GameConstants::TERRAIN_TYPES - 1)) + { + JsonVector & data = node["allowedTerrains"].Vector(); + + for(auto type : allowedTerrains) + { + JsonNode value(JsonNode::DATA_STRING); + value.String() = GameConstants::TERRAIN_NAMES[type.num]; + data.push_back(value); + } + } + } +//todo: use lookup? + auto tileToChar = [&](const ui8 & tile) -> char + { + if(tile & VISIBLE) + { + if(tile & BLOCKED) + { + if(tile & VISITABLE) + return 'A'; + else + return 'B'; + } + else + return 'V'; + } + else + { + if(tile & BLOCKED) + { + if(tile & VISITABLE) + return 'T'; + else + return 'H'; + } + else + return '0'; + } + }; + + size_t height = getHeight(); + size_t width = getWidth(); + + JsonVector & mask = node["mask"].Vector(); + + + for(size_t i=0; i < height; i++) + { + JsonNode lineNode(JsonNode::DATA_STRING); + + std::string & line = lineNode.String(); + line.resize(width); + for(size_t j=0; j < width; j++) + line[j] = tileToChar(usedTiles[height - 1 - i][width - 1 - j]); + mask.push_back(lineNode); + } } ui32 ObjectTemplate::getWidth() const diff --git a/lib/mapObjects/ObjectTemplate.h b/lib/mapObjects/ObjectTemplate.h index 0e175bd0f..bbb3522b9 100644 --- a/lib/mapObjects/ObjectTemplate.h +++ b/lib/mapObjects/ObjectTemplate.h @@ -73,8 +73,8 @@ public: void readTxt(CLegacyConfigParser & parser); void readMsk(); void readMap(CBinaryReader & reader); - void readJson(const JsonNode & node); - void writeJson(JsonNode & node) const; + void readJson(const JsonNode & node, const bool withTerrain = true); + void writeJson(JsonNode & node, const bool withTerrain = true) const; bool operator==(const ObjectTemplate& ot) const { return (id == ot.id && subid == ot.subid); }