From 103587c2d5f9deb20e549a86cdf5023b429cc6a1 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 18 Mar 2015 18:23:54 -0400 Subject: Wrote an XML Schema describing maps file and also changed the spec a bit --- CMakeLists.txt | 1 + res/maps.xml | 8 +- res/maps.xsd | 92 +++++++++ src/components/map_collision.cpp | 16 +- src/game.cpp | 164 +--------------- src/game.h | 7 +- src/map.cpp | 107 ++--------- src/map.h | 35 ++-- src/world.cpp | 134 +++++++++++++ src/world.h | 21 +++ tools/mapedit/src/frame.cpp | 188 +++++++++--------- tools/mapedit/src/map.cpp | 162 ++++------------ tools/mapedit/src/map.h | 44 ++--- tools/mapedit/src/widget.cpp | 16 +- tools/mapedit/src/world.cpp | 397 +++++++++++++++------------------------ 15 files changed, 620 insertions(+), 772 deletions(-) create mode 100644 res/maps.xsd create mode 100644 src/world.cpp create mode 100644 src/world.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c9084e..c5e8f71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ add_executable(Aromatherapy src/game.cpp src/muxer.cpp src/entityfactory.cpp + src/world.cpp src/components/map_collision.cpp src/components/map_render.cpp src/components/physics_body.cpp diff --git a/res/maps.xml b/res/maps.xml index 594cd18..444e59f 100644 --- a/res/maps.xml +++ b/res/maps.xml @@ -1,5 +1,5 @@ -540Everything Is Embarrassing0,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,20,0,0,0,0,0,0,0,18,9,8,10,8,11,8,10,10,8,11,8,9,10,21,0, +00,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,20,0,0,0,0,0,0,0,18,9,8,10,8,11,8,10,10,8,11,8,9,10,21,0, 0,0,0,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,8,8,17,0,0,0,0,0,0,0,0,0,0,0,0,0,22,21, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,8,8,8,8,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12, 0,19,0,0,0,0,0,0,0,0,0,0,0,18,8,8,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12, @@ -23,7 +23,7 @@ 0,0,0,0,24,3,1,2,1,1,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,19,0,27,1,3,2,26,0,0,0,0,0,0, 1,2,3,1,25,0,0,0,0,0,20,0,0,0,0,0,0,19,0,0,0,0,0,0,20,0,0,0,0,0,0,0,0,27,1,2,2,4,3,1, 0,0,0,0,0,0,19,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,0,0,0,0,0,0,0,0,0,0, -141It's A Trap!0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +140,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,0,0,0,0,0,0,0,0,20,0,0,0,0,0,0,0, 0,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,19,0,0,0,0,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, @@ -47,7 +47,7 @@ 0,0,0,27,2,2,3,3,3,4,26,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,20,0,0,0,0,0,19,0,0,27,1,1,1,2,2,2,26,0,0,0,0,0,0,0,0,0,0,24,4,2,2,1,3,2,2,1,2,2,3, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,19,0,0,0,0,0, -checkpoint262,156The Visitors0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,19,0,0,0,0,0,0,0,0,0,14,0,0,0,0,0,0,0,0,0,0,13,0,0,0,0,0,0,20,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,20,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,19,0,0,0,0,0,0,0,0,0,20,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, @@ -71,4 +71,4 @@ 0,0,0,0,0,0,20,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,0,0,15,0,0,0,0,0,0,0,0,0,0,13,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, - + diff --git a/res/maps.xsd b/res/maps.xsd new file mode 100644 index 0000000..9d36fab --- /dev/null +++ b/res/maps.xsd @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/components/map_collision.cpp b/src/components/map_collision.cpp index 9adad26..83ad33d 100644 --- a/src/components/map_collision.cpp +++ b/src/components/map_collision.cpp @@ -5,10 +5,10 @@ MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) { - addCollision(-6, 0, GAME_HEIGHT, Direction::left, collisionFromMoveType(map.getLeftMoveType())); - addCollision(GAME_WIDTH+6, 0, GAME_HEIGHT, Direction::right, collisionFromMoveType(map.getRightMoveType())); - addCollision(-7, 0, GAME_WIDTH, Direction::up, collisionFromMoveType(map.getUpMoveType())); - addCollision(GAME_HEIGHT+6, 0, GAME_WIDTH, Direction::down, collisionFromMoveType(map.getDownMoveType())); + addCollision(-6, 0, GAME_HEIGHT, Direction::left, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Left).type)); + addCollision(GAME_WIDTH+6, 0, GAME_HEIGHT, Direction::right, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Right).type)); + addCollision(-7, 0, GAME_WIDTH, Direction::up, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Up).type)); + addCollision(GAME_HEIGHT+6, 0, GAME_WIDTH, Direction::down, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Down).type)); for (int i=0; i -#include #include "renderer.h" #include "muxer.h" #include "map.h" @@ -11,164 +10,11 @@ #include "components/map_collision.h" #include "consts.h" -Game::Game(const char* mapfile) +Game::Game(const char* mapfile) : world(mapfile) { - // Load maps - xmlDocPtr doc = xmlParseFile(mapfile); - if (doc == nullptr) - { - exit(2); - } - - xmlNodePtr top = xmlDocGetRootElement(doc); - if (top == nullptr) - { - exit(2); - } - - if (xmlStrcmp(top->name, (const xmlChar*) "world")) - { - exit(2); - } - - for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) - { - if (!xmlStrcmp(node->name, (const xmlChar*) "startpos")) - { - xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); - if (idKey == 0) exit(2); - startMap = atoi((char*) idKey); - xmlFree(idKey); - - xmlChar* posKey = xmlGetProp(node, (xmlChar*) "pos"); - if (posKey == 0) exit(2); - sscanf((char*) posKey, "%d,%d", &startPos.first, &startPos.second); - xmlFree(posKey); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "map")) - { - xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); - if (idKey == 0) exit(2); - int theId = atoi((char*) idKey); - xmlFree(idKey); - - Map map {theId}; - - for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) - { - if (!xmlStrcmp(mapNode->name, (const xmlChar*) "name")) - { - xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); - if (key != 0) - { - map.setTitle((char*) key); - } - - xmlFree(key); - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) - { - xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); - int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); - mapdata[0] = atoi(strtok((char*) key, ",\n")); - for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) - { - mapdata[i] = atoi(strtok(NULL, ",\n")); - } - map.setMapdata(mapdata); - xmlFree(key); - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "leftmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) exit(2); - map.setLeftMoveType(Map::moveTypeForShort((char*) typeKey)); - xmlFree(typeKey); - - if (Map::moveTypeTakesMap(map.getLeftMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) exit(2); - map.setLeftMapID(atoi((char*) idKey)); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "rightmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) exit(2); - map.setRightMoveType(Map::moveTypeForShort((char*) typeKey)); - xmlFree(typeKey); - - if (Map::moveTypeTakesMap(map.getRightMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) exit(2); - map.setRightMapID(atoi((char*) idKey)); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "upmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) exit(2); - map.setUpMoveType(Map::moveTypeForShort((char*) typeKey)); - xmlFree(typeKey); - - if (Map::moveTypeTakesMap(map.getUpMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) exit(2); - map.setUpMapID(atoi((char*) idKey)); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "downmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) exit(2); - map.setDownMoveType(Map::moveTypeForShort((char*) typeKey)); - xmlFree(typeKey); - - if (Map::moveTypeTakesMap(map.getDownMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) exit(2); - map.setDownMapID(atoi((char*) idKey)); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entities")) - { - for (xmlNodePtr entityNode = mapNode->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) - { - if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) - { - Map::EntityData data; - - for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) - { - if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - data.name = (char*) key; - xmlFree(key); - } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - sscanf((char*) key, "%d,%d", &data.position.first, &data.position.second); - xmlFree(key); - } - } - - map.addEntity(data); - } - } - } - } - - maps[theId] = map; - } - } - - xmlFreeDoc(doc); - // Set up entities player = std::make_shared(); - player->position = startPos; + player->position = world.getStartingPosition(); player->size = std::make_pair(10.0,12.0); auto player_input = std::make_shared(); @@ -180,7 +26,7 @@ Game::Game(const char* mapfile) auto player_anim = std::make_shared(); player->addComponent(player_anim); - Map& startingMap = maps[startMap]; + const Map& startingMap = world.getStartingMap(); save = {&startingMap, player->position}; loadMap(startingMap, player->position); @@ -326,7 +172,7 @@ void Game::playerDie() }); } -const Map& Game::getMap(int id) const +const World& Game::getWorld() const { - return maps.at(id); + return world; } diff --git a/src/game.h b/src/game.h index 3f0fcc8..dd4b2f7 100644 --- a/src/game.h +++ b/src/game.h @@ -6,6 +6,7 @@ #include #include #include "map.h" +#include "world.h" class Entity; struct GLFWwindow; @@ -24,14 +25,11 @@ class Game { void saveGame(); void schedule(double time, std::function callback); void playerDie(); - const Map& getMap(int id) const; + const World& getWorld() const; private: friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); - std::map maps; - int startMap; - std::pair startPos; std::list> entities; std::list> nextEntities; std::pair nextPosition; @@ -41,6 +39,7 @@ class Game { Savefile save; std::list>> scheduled; bool shouldQuit = false; + World world; }; #endif diff --git a/src/map.cpp b/src/map.cpp index fa940ef..c4f31d1 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -20,14 +20,7 @@ Map::Map(const Map& map) id = map.id; title = map.title; - leftMap = map.leftMap; - rightMap = map.rightMap; - downMap = map.downMap; - upMap = map.upMap; - leftType = map.leftType; - rightType = map.rightType; - upType = map.upType; - downType = map.downType; + adjacents = map.adjacents; entities = map.entities; } @@ -52,14 +45,7 @@ void swap(Map& first, Map& second) { std::swap(first.mapdata, second.mapdata); std::swap(first.title, second.title); - std::swap(first.leftMap, second.leftMap); - std::swap(first.rightMap, second.rightMap); - std::swap(first.downMap, second.downMap); - std::swap(first.upMap, second.upMap); - std::swap(first.leftType, second.leftType); - std::swap(first.rightType, second.rightType); - std::swap(first.upType, second.upType); - std::swap(first.downType, second.downType); + std::swap(first.adjacents, second.adjacents); std::swap(first.id, second.id); std::swap(first.entities, second.entities); } @@ -109,44 +95,24 @@ Map::MoveType Map::moveTypeForShort(std::string str) return MoveType::Wall; } -Map::MoveType Map::getLeftMoveType() const +Map::MoveDir Map::moveDirForShort(std::string str) { - return leftType; -} - -Map::MoveType Map::getRightMoveType() const -{ - return rightType; -} - -Map::MoveType Map::getUpMoveType() const -{ - return upType; -} - -Map::MoveType Map::getDownMoveType() const -{ - return downType; -} - -int Map::getLeftMapID() const -{ - return leftMap; -} - -int Map::getRightMapID() const -{ - return rightMap; -} - -int Map::getUpMapID() const -{ - return upMap; + if (str == "right") return MoveDir::Right; + if (str == "up") return MoveDir::Up; + if (str == "down") return MoveDir::Down; + + return MoveDir::Left; } -int Map::getDownMapID() const +static const Map::Adjacent defaultAdjacent {}; +const Map::Adjacent& Map::getAdjacent(MoveDir dir) const { - return downMap; + if (adjacents.count(dir) > 0) + { + return adjacents.at(dir); + } else { + return defaultAdjacent; + } } bool Map::moveTypeTakesMap(MoveType type) @@ -171,44 +137,11 @@ void Map::setTitle(std::string title) this->title = title; } -void Map::setLeftMoveType(MoveType type) -{ - leftType = type; -} - -void Map::setRightMoveType(MoveType type) -{ - rightType = type; -} - -void Map::setUpMoveType(MoveType type) -{ - upType = type; -} - -void Map::setDownMoveType(MoveType type) -{ - downType = type; -} - -void Map::setLeftMapID(int id) -{ - leftMap = id; -} - -void Map::setRightMapID(int id) -{ - rightMap = id; -} - -void Map::setUpMapID(int id) -{ - upMap = id; -} - -void Map::setDownMapID(int id) +void Map::setAdjacent(MoveDir dir, MoveType type, int map) { - downMap = id; + Adjacent& cur = adjacents[dir]; + cur.type = type; + if (map != -1) cur.map = map; } void Map::addEntity(EntityData& data) diff --git a/src/map.h b/src/map.h index 1234dbb..4e661ab 100644 --- a/src/map.h +++ b/src/map.h @@ -3,6 +3,7 @@ #include #include +#include class Entity; @@ -23,25 +24,31 @@ class Map { ReverseWarp }; + enum class MoveDir { + Left, + Right, + Up, + Down + }; + struct EntityData { std::string name; std::pair position; }; + struct Adjacent { + MoveType type = MoveType::Wall; + int map = -1; + }; + static MoveType moveTypeForShort(std::string str); + static MoveDir moveDirForShort(std::string str); static bool moveTypeTakesMap(MoveType type); int getID() const; const int* getMapdata() const; std::string getTitle() const; - MoveType getLeftMoveType() const; - MoveType getRightMoveType() const; - MoveType getUpMoveType() const; - MoveType getDownMoveType() const; - int getLeftMapID() const; - int getRightMapID() const; - int getUpMapID() const; - int getDownMapID() const; + const Adjacent& getAdjacent(MoveDir dir) const; void createEntities(std::list>& entities) const; bool operator==(const Map& other) const; @@ -49,22 +56,14 @@ class Map { void setMapdata(int* mapdata); void setTitle(std::string title); - void setLeftMoveType(MoveType type); - void setRightMoveType(MoveType type); - void setUpMoveType(MoveType type); - void setDownMoveType(MoveType type); - void setLeftMapID(int id); - void setRightMapID(int id); - void setUpMapID(int id); - void setDownMapID(int id); + void setAdjacent(MoveDir dir, MoveType type, int map); void addEntity(EntityData& data); private: int* mapdata; std::string title; int id; std::list entities; - MoveType leftType, rightType, upType, downType; - int leftMap, rightMap, upMap, downMap; + std::map adjacents; }; #endif diff --git a/src/world.cpp b/src/world.cpp new file mode 100644 index 0000000..17288fa --- /dev/null +++ b/src/world.cpp @@ -0,0 +1,134 @@ +#include "world.h" +#include +#include "consts.h" + +World::World(const char* filename) +{ + xmlDocPtr doc = xmlParseFile(filename); + if (doc == nullptr) + { + exit(2); + } + + xmlNodePtr top = xmlDocGetRootElement(doc); + if (top == nullptr) + { + exit(2); + } + + if (xmlStrcmp(top->name, (const xmlChar*) "world")) + { + exit(2); + } + + xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx"); + if (startxKey == 0) exit(2); + startX = atoi((char*) startxKey); + xmlFree(startxKey); + + xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty"); + if (startyKey == 0) exit(2); + startY = atoi((char*) startyKey); + xmlFree(startyKey); + + xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap"); + if (startxKey == 0) exit(2); + startMap = atoi((char*) startmapKey); + xmlFree(startmapKey); + + for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) + { + if (!xmlStrcmp(node->name, (const xmlChar*) "map")) + { + xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); + if (idKey == 0) exit(2); + int theId = atoi((char*) idKey); + xmlFree(idKey); + + maps.emplace(theId, theId); + Map& map = maps[theId]; + + xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title"); + if (titleKey == 0) exit(2); + map.setTitle((char*) titleKey); + xmlFree(titleKey); + + for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) + { + if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) + { + xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); + int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); + mapdata[0] = atoi(strtok((char*) key, ",\n")); + for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) + { + mapdata[i] = atoi(strtok(NULL, ",\n")); + } + map.setMapdata(mapdata); + xmlFree(key); + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entity")) + { + Map::EntityData data; + + xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); + if (typeKey == 0) exit(2); + data.name = (char*) typeKey; + xmlFree(typeKey); + + xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x"); + if (xKey == 0) exit(2); + data.position.first = atoi((char*) xKey); + xmlFree(xKey); + + xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y"); + if (yKey == 0) exit(2); + data.position.second = atoi((char*) yKey); + xmlFree(yKey); + + map.addEntity(data); + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent")) + { + Map::MoveDir direction; + Map::MoveType moveType; + int mapId = 0; + + xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir"); + if (dirKey == 0) exit(2); + direction = Map::moveDirForShort((char*) dirKey); + xmlFree(dirKey); + + xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); + if (typeKey == 0) exit(2); + moveType = Map::moveTypeForShort((char*) typeKey); + xmlFree(typeKey); + + xmlChar* mapIdKey = xmlGetProp(mapNode, (const xmlChar*) "map"); + if (mapIdKey != 0) + { + mapId = atoi((char*) mapIdKey); + } + xmlFree(mapIdKey); + + map.setAdjacent(direction, moveType, mapId); + } + } + } + } + + xmlFreeDoc(doc); +} + +const Map& World::getMap(int id) const +{ + return maps.at(id); +} + +const Map& World::getStartingMap() const +{ + return maps.at(startMap); +} + +std::pair World::getStartingPosition() const +{ + return std::make_pair(startX, startY); +} diff --git a/src/world.h b/src/world.h new file mode 100644 index 0000000..f566487 --- /dev/null +++ b/src/world.h @@ -0,0 +1,21 @@ +#ifndef WORLD_H +#define WORLD_H + +#include +#include "map.h" + +class World { + public: + World(const char* filename); + const Map& getMap(int id) const; + const Map& getStartingMap() const; + std::pair getStartingPosition() const; + + private: + std::map maps; + int startMap; + int startX; + int startY; +}; + +#endif /* end of include guard: WORLD_H */ diff --git a/tools/mapedit/src/frame.cpp b/tools/mapedit/src/frame.cpp index cc45c29..06102f7 100644 --- a/tools/mapedit/src/frame.cpp +++ b/tools/mapedit/src/frame.cpp @@ -190,22 +190,22 @@ MapeditFrame::MapeditFrame(World* world) : wxFrame(NULL, wxID_ANY, "Map Editor") wxStaticText* leftmapLabel = new wxStaticText(propertyEditor, wxID_ANY, "Leftmap Action:"); wxChoice* leftmapChoice = new wxChoice(propertyEditor, LEFTMAP_TYPE_CHOICE); wxComboCtrl* leftmapCombo = new wxComboCtrl(propertyEditor, LEFTMAP_MAP_CHOICE, "", wxDefaultPosition, wxDefaultSize, wxCB_READONLY); - leftmapCombo->SetPopupControl(new MapSelectComboPopup(mapTree, currentMap->getLeftMoveMapID())); + leftmapCombo->SetPopupControl(new MapSelectComboPopup(mapTree, currentMap->getAdjacent(Map::MoveDir::Left).map)); wxStaticText* rightmapLabel = new wxStaticText(propertyEditor, wxID_ANY, "Rightmap Action:"); wxChoice* rightmapChoice = new wxChoice(propertyEditor, RIGHTMAP_TYPE_CHOICE); wxComboCtrl* rightmapCombo = new wxComboCtrl(propertyEditor, RIGHTMAP_MAP_CHOICE, "", wxDefaultPosition, wxDefaultSize, wxCB_READONLY); - rightmapCombo->SetPopupControl(new MapSelectComboPopup(mapTree, currentMap->getRightMoveMapID())); + rightmapCombo->SetPopupControl(new MapSelectComboPopup(mapTree, currentMap->getAdjacent(Map::MoveDir::Right).map)); wxStaticText* upmapLabel = new wxStaticText(propertyEditor, wxID_ANY, "Upmap Action:"); wxChoice* upmapChoice = new wxChoice(propertyEditor, UPMAP_TYPE_CHOICE); wxComboCtrl* upmapCombo = new wxComboCtrl(propertyEditor, UPMAP_MAP_CHOICE, "", wxDefaultPosition, wxDefaultSize, wxCB_READONLY); - upmapCombo->SetPopupControl(new MapSelectComboPopup(mapTree, currentMap->getUpMoveMapID())); + upmapCombo->SetPopupControl(new MapSelectComboPopup(mapTree, currentMap->getAdjacent(Map::MoveDir::Up).map)); wxStaticText* downmapLabel = new wxStaticText(propertyEditor, wxID_ANY, "Downmap Action:"); wxChoice* downmapChoice = new wxChoice(propertyEditor, DOWNMAP_TYPE_CHOICE); wxComboCtrl* downmapCombo = new wxComboCtrl(propertyEditor, DOWNMAP_MAP_CHOICE, "", wxDefaultPosition, wxDefaultSize, wxCB_READONLY); - downmapCombo->SetPopupControl(new MapSelectComboPopup(mapTree, currentMap->getDownMoveMapID())); + downmapCombo->SetPopupControl(new MapSelectComboPopup(mapTree, currentMap->getAdjacent(Map::MoveDir::Down).map)); for (auto type : Map::listMoveTypes()) { @@ -674,157 +674,169 @@ void MapeditFrame::OnThreeMovingSash(wxSplitterEvent& event) void MapeditFrame::OnSetLeftmapType(wxCommandEvent&) { - wxChoice* leftmapChoice = (wxChoice*) wxWindow::FindWindowById(LEFTMAP_TYPE_CHOICE, this); - wxComboCtrl* leftmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(LEFTMAP_MAP_CHOICE, this); - - Map::MoveType old = currentMap->getLeftMoveType(); - Map::MoveType newt = ((MoveTypeCtr*) leftmapChoice->GetClientData(leftmapChoice->GetSelection()))->type; + wxChoice* choice = (wxChoice*) wxWindow::FindWindowById(LEFTMAP_TYPE_CHOICE, this); + wxComboCtrl* combo = (wxComboCtrl*) wxChoice::FindWindowById(LEFTMAP_MAP_CHOICE, this); + Map::MoveDir dir = Map::MoveDir::Left; + Map::Adjacent adjacent = currentMap->getAdjacent(dir); + Map::MoveType newt = ((MoveTypeCtr*) choice->GetClientData(choice->GetSelection()))->type; commitAction(std::make_shared("Set Leftmap Action", [=] () { - leftmapChoice->SetSelection(leftmapChoice->FindString(Map::stringForMoveType(newt))); - currentMap->setLeftMoveType(newt); - leftmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getLeftMoveType())); + choice->SetSelection(choice->FindString(Map::stringForMoveType(newt))); + currentMap->setAdjacent(dir, newt); + combo->Enable(Map::moveTypeTakesMap(newt)); }, [=] () { - leftmapChoice->SetSelection(leftmapChoice->FindString(Map::stringForMoveType(old))); - currentMap->setLeftMoveType(old); - leftmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getLeftMoveType())); + choice->SetSelection(choice->FindString(Map::stringForMoveType(adjacent.type))); + currentMap->setAdjacent(dir, adjacent.type); + combo->Enable(Map::moveTypeTakesMap(adjacent.type)); })); } void MapeditFrame::OnSetLeftmapMap(wxCommandEvent&) { - wxComboCtrl* leftmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(LEFTMAP_MAP_CHOICE, this); - MapSelectComboPopup* popup = (MapSelectComboPopup*) leftmapCombo->GetPopupControl(); - int old = currentMap->getLeftMoveMapID(); + wxComboCtrl* combo = (wxComboCtrl*) wxWindow::FindWindowById(LEFTMAP_MAP_CHOICE, this); + Map::MoveDir dir = Map::MoveDir::Left; + + Map::Adjacent adjacent = currentMap->getAdjacent(dir); + MapSelectComboPopup* popup = (MapSelectComboPopup*) combo->GetPopupControl(); + int old = adjacent.map; int newt = popup->GetSelectedMapID(); if (old == newt) return; commitAction(std::make_shared("Set Leftmap Map", [=] () { popup->SetSelectedMapID(newt); - leftmapCombo->SetValue(world->getMap(newt)->getTitle()); - currentMap->setLeftMoveMapID(newt); + combo->SetValue(world->getMap(newt)->getTitle()); + currentMap->setAdjacent(dir, adjacent.type, newt); }, [=] () { popup->SetSelectedMapID(old); - leftmapCombo->SetValue(world->getMap(old)->getTitle()); - currentMap->setLeftMoveMapID(old); + combo->SetValue(world->getMap(old)->getTitle()); + currentMap->setAdjacent(dir, adjacent.type, old); })); } void MapeditFrame::OnSetRightmapType(wxCommandEvent&) { - wxChoice* rightmapChoice = (wxChoice*) wxWindow::FindWindowById(RIGHTMAP_TYPE_CHOICE, this); - wxComboCtrl* rightmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(RIGHTMAP_MAP_CHOICE, this); - - Map::MoveType old = currentMap->getRightMoveType(); - Map::MoveType newt = ((MoveTypeCtr*) rightmapChoice->GetClientData(rightmapChoice->GetSelection()))->type; + wxChoice* choice = (wxChoice*) wxWindow::FindWindowById(RIGHTMAP_TYPE_CHOICE, this); + wxComboCtrl* combo = (wxComboCtrl*) wxChoice::FindWindowById(RIGHTMAP_MAP_CHOICE, this); + Map::MoveDir dir = Map::MoveDir::Right; + Map::Adjacent adjacent = currentMap->getAdjacent(dir); + Map::MoveType newt = ((MoveTypeCtr*) choice->GetClientData(choice->GetSelection()))->type; commitAction(std::make_shared("Set Rightmap Action", [=] () { - rightmapChoice->SetSelection(rightmapChoice->FindString(Map::stringForMoveType(newt))); - currentMap->setRightMoveType(newt); - rightmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getRightMoveType())); + choice->SetSelection(choice->FindString(Map::stringForMoveType(newt))); + currentMap->setAdjacent(dir, newt); + combo->Enable(Map::moveTypeTakesMap(newt)); }, [=] () { - rightmapChoice->SetSelection(rightmapChoice->FindString(Map::stringForMoveType(old))); - currentMap->setRightMoveType(old); - rightmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getRightMoveType())); + choice->SetSelection(choice->FindString(Map::stringForMoveType(adjacent.type))); + currentMap->setAdjacent(dir, adjacent.type); + combo->Enable(Map::moveTypeTakesMap(adjacent.type)); })); } void MapeditFrame::OnSetRightmapMap(wxCommandEvent&) { - wxComboCtrl* rightmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(RIGHTMAP_MAP_CHOICE, this); - MapSelectComboPopup* popup = (MapSelectComboPopup*) rightmapCombo->GetPopupControl(); - int old = currentMap->getRightMoveMapID(); + wxComboCtrl* combo = (wxComboCtrl*) wxWindow::FindWindowById(RIGHTMAP_MAP_CHOICE, this); + Map::MoveDir dir = Map::MoveDir::Right; + + Map::Adjacent adjacent = currentMap->getAdjacent(dir); + MapSelectComboPopup* popup = (MapSelectComboPopup*) combo->GetPopupControl(); + int old = adjacent.map; int newt = popup->GetSelectedMapID(); if (old == newt) return; commitAction(std::make_shared("Set Rightmap Map", [=] () { popup->SetSelectedMapID(newt); - rightmapCombo->SetValue(world->getMap(newt)->getTitle()); - currentMap->setRightMoveMapID(newt); + combo->SetValue(world->getMap(newt)->getTitle()); + currentMap->setAdjacent(dir, adjacent.type, newt); }, [=] () { popup->SetSelectedMapID(old); - rightmapCombo->SetValue(world->getMap(old)->getTitle()); - currentMap->setRightMoveMapID(old); + combo->SetValue(world->getMap(old)->getTitle()); + currentMap->setAdjacent(dir, adjacent.type, old); })); } void MapeditFrame::OnSetUpmapType(wxCommandEvent&) { - wxChoice* upmapChoice = (wxChoice*) wxWindow::FindWindowById(UPMAP_TYPE_CHOICE, this); - wxComboCtrl* upmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(UPMAP_MAP_CHOICE, this); - - Map::MoveType old = currentMap->getUpMoveType(); - Map::MoveType newt = ((MoveTypeCtr*) upmapChoice->GetClientData(upmapChoice->GetSelection()))->type; + wxChoice* choice = (wxChoice*) wxWindow::FindWindowById(UPMAP_TYPE_CHOICE, this); + wxComboCtrl* combo = (wxComboCtrl*) wxChoice::FindWindowById(UPMAP_MAP_CHOICE, this); + Map::MoveDir dir = Map::MoveDir::Up; + Map::Adjacent adjacent = currentMap->getAdjacent(dir); + Map::MoveType newt = ((MoveTypeCtr*) choice->GetClientData(choice->GetSelection()))->type; commitAction(std::make_shared("Set Upmap Action", [=] () { - upmapChoice->SetSelection(upmapChoice->FindString(Map::stringForMoveType(newt))); - currentMap->setUpMoveType(newt); - upmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getUpMoveType())); + choice->SetSelection(choice->FindString(Map::stringForMoveType(newt))); + currentMap->setAdjacent(dir, newt); + combo->Enable(Map::moveTypeTakesMap(newt)); }, [=] () { - upmapChoice->SetSelection(upmapChoice->FindString(Map::stringForMoveType(old))); - currentMap->setUpMoveType(old); - upmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getUpMoveType())); + choice->SetSelection(choice->FindString(Map::stringForMoveType(adjacent.type))); + currentMap->setAdjacent(dir, adjacent.type); + combo->Enable(Map::moveTypeTakesMap(adjacent.type)); })); } void MapeditFrame::OnSetUpmapMap(wxCommandEvent&) { - wxComboCtrl* upmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(UPMAP_MAP_CHOICE, this); - MapSelectComboPopup* popup = (MapSelectComboPopup*) upmapCombo->GetPopupControl(); - int old = currentMap->getUpMoveMapID(); + wxComboCtrl* combo = (wxComboCtrl*) wxWindow::FindWindowById(UPMAP_MAP_CHOICE, this); + Map::MoveDir dir = Map::MoveDir::Up; + + Map::Adjacent adjacent = currentMap->getAdjacent(dir); + MapSelectComboPopup* popup = (MapSelectComboPopup*) combo->GetPopupControl(); + int old = adjacent.map; int newt = popup->GetSelectedMapID(); if (old == newt) return; commitAction(std::make_shared("Set Upmap Map", [=] () { popup->SetSelectedMapID(newt); - upmapCombo->SetValue(world->getMap(newt)->getTitle()); - currentMap->setUpMoveMapID(newt); + combo->SetValue(world->getMap(newt)->getTitle()); + currentMap->setAdjacent(dir, adjacent.type, newt); }, [=] () { popup->SetSelectedMapID(old); - upmapCombo->SetValue(world->getMap(old)->getTitle()); - currentMap->setUpMoveMapID(old); + combo->SetValue(world->getMap(old)->getTitle()); + currentMap->setAdjacent(dir, adjacent.type, old); })); } void MapeditFrame::OnSetDownmapType(wxCommandEvent&) { - wxChoice* downmapChoice = (wxChoice*) wxWindow::FindWindowById(DOWNMAP_TYPE_CHOICE, this); - wxComboCtrl* downmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(DOWNMAP_MAP_CHOICE, this); - - Map::MoveType old = currentMap->getDownMoveType(); - Map::MoveType newt = ((MoveTypeCtr*) downmapChoice->GetClientData(downmapChoice->GetSelection()))->type; + wxChoice* choice = (wxChoice*) wxWindow::FindWindowById(DOWNMAP_TYPE_CHOICE, this); + wxComboCtrl* combo = (wxComboCtrl*) wxChoice::FindWindowById(DOWNMAP_MAP_CHOICE, this); + Map::MoveDir dir = Map::MoveDir::Down; + Map::Adjacent adjacent = currentMap->getAdjacent(dir); + Map::MoveType newt = ((MoveTypeCtr*) choice->GetClientData(choice->GetSelection()))->type; commitAction(std::make_shared("Set Downmap Action", [=] () { - downmapChoice->SetSelection(downmapChoice->FindString(Map::stringForMoveType(newt))); - currentMap->setDownMoveType(newt); - downmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getDownMoveType())); + choice->SetSelection(choice->FindString(Map::stringForMoveType(newt))); + currentMap->setAdjacent(dir, newt); + combo->Enable(Map::moveTypeTakesMap(newt)); }, [=] () { - downmapChoice->SetSelection(downmapChoice->FindString(Map::stringForMoveType(old))); - currentMap->setDownMoveType(old); - downmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getDownMoveType())); + choice->SetSelection(choice->FindString(Map::stringForMoveType(adjacent.type))); + currentMap->setAdjacent(dir, adjacent.type); + combo->Enable(Map::moveTypeTakesMap(adjacent.type)); })); } void MapeditFrame::OnSetDownmapMap(wxCommandEvent&) { - wxComboCtrl* downmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(DOWNMAP_MAP_CHOICE, this); - MapSelectComboPopup* popup = (MapSelectComboPopup*) downmapCombo->GetPopupControl(); - int old = currentMap->getDownMoveMapID(); + wxComboCtrl* combo = (wxComboCtrl*) wxWindow::FindWindowById(DOWNMAP_MAP_CHOICE, this); + Map::MoveDir dir = Map::MoveDir::Down; + + Map::Adjacent adjacent = currentMap->getAdjacent(dir); + MapSelectComboPopup* popup = (MapSelectComboPopup*) combo->GetPopupControl(); + int old = adjacent.map; int newt = popup->GetSelectedMapID(); if (old == newt) return; commitAction(std::make_shared("Set Downmap Map", [=] () { popup->SetSelectedMapID(newt); - downmapCombo->SetValue(world->getMap(newt)->getTitle()); - currentMap->setDownMoveMapID(newt); + combo->SetValue(world->getMap(newt)->getTitle()); + currentMap->setAdjacent(dir, adjacent.type, newt); }, [=] () { popup->SetSelectedMapID(old); - downmapCombo->SetValue(world->getMap(old)->getTitle()); - currentMap->setDownMoveMapID(old); + combo->SetValue(world->getMap(old)->getTitle()); + currentMap->setAdjacent(dir, adjacent.type, old); })); } @@ -924,20 +936,20 @@ void MapeditFrame::SelectMap(Map* map) wxComboCtrl* upmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(UPMAP_MAP_CHOICE, this); wxComboCtrl* downmapCombo = (wxComboCtrl*) wxWindow::FindWindowById(DOWNMAP_MAP_CHOICE, this); - leftmapChoice->SetSelection(leftmapChoice->FindString(Map::stringForMoveType(currentMap->getLeftMoveType()))); - rightmapChoice->SetSelection(rightmapChoice->FindString(Map::stringForMoveType(currentMap->getRightMoveType()))); - upmapChoice->SetSelection(upmapChoice->FindString(Map::stringForMoveType(currentMap->getUpMoveType()))); - downmapChoice->SetSelection(downmapChoice->FindString(Map::stringForMoveType(currentMap->getDownMoveType()))); + leftmapChoice->SetSelection(leftmapChoice->FindString(Map::stringForMoveType(currentMap->getAdjacent(Map::MoveDir::Left).type))); + rightmapChoice->SetSelection(rightmapChoice->FindString(Map::stringForMoveType(currentMap->getAdjacent(Map::MoveDir::Right).type))); + upmapChoice->SetSelection(upmapChoice->FindString(Map::stringForMoveType(currentMap->getAdjacent(Map::MoveDir::Up).type))); + downmapChoice->SetSelection(downmapChoice->FindString(Map::stringForMoveType(currentMap->getAdjacent(Map::MoveDir::Down).type))); - leftmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getLeftMoveType())); - rightmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getRightMoveType())); - upmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getUpMoveType())); - downmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getDownMoveType())); + leftmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getAdjacent(Map::MoveDir::Left).type)); + rightmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getAdjacent(Map::MoveDir::Right).type)); + upmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getAdjacent(Map::MoveDir::Up).type)); + downmapCombo->Enable(Map::moveTypeTakesMap(currentMap->getAdjacent(Map::MoveDir::Down).type)); - leftmapCombo->SetValue(world->getMap(currentMap->getLeftMoveMapID())->getTitle()); - rightmapCombo->SetValue(world->getMap(currentMap->getRightMoveMapID())->getTitle()); - upmapCombo->SetValue(world->getMap(currentMap->getUpMoveMapID())->getTitle()); - downmapCombo->SetValue(world->getMap(currentMap->getDownMoveMapID())->getTitle()); + leftmapCombo->SetValue(world->getMap(currentMap->getAdjacent(Map::MoveDir::Left).map)->getTitle()); + rightmapCombo->SetValue(world->getMap(currentMap->getAdjacent(Map::MoveDir::Right).map)->getTitle()); + upmapCombo->SetValue(world->getMap(currentMap->getAdjacent(Map::MoveDir::Up).map)->getTitle()); + downmapCombo->SetValue(world->getMap(currentMap->getAdjacent(Map::MoveDir::Down).map)->getTitle()); } wxTreeItemId MapeditFrame::MoveTreeNode(wxTreeItemId toCopy, wxTreeItemId newParent) diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp index fb675e8..a099e29 100644 --- a/tools/mapedit/src/map.cpp +++ b/tools/mapedit/src/map.cpp @@ -21,14 +21,7 @@ Map::Map(const Map& map) treeItemId = map.treeItemId; children = map.children; hidden = map.hidden; - leftType = map.leftType; - rightType = map.rightType; - upType = map.upType; - downType = map.downType; - leftMap = map.leftMap; - rightMap = map.rightMap; - downMap = map.downMap; - upMap = map.upMap; + adjacents = map.adjacents; } Map::Map(Map&& map) : Map(-1, map.world) @@ -58,14 +51,7 @@ void swap(Map& first, Map& second) std::swap(first.treeItemId, second.treeItemId); std::swap(first.children, second.children); std::swap(first.hidden, second.hidden); - std::swap(first.leftType, second.leftType); - std::swap(first.rightType, second.rightType); - std::swap(first.upType, second.upType); - std::swap(first.downType, second.downType); - std::swap(first.leftMap, second.leftMap); - std::swap(first.rightMap, second.rightMap); - std::swap(first.downMap, second.downMap); - std::swap(first.upMap, second.upMap); + std::swap(first.adjacents, second.adjacents); } std::list Map::listMoveTypes() @@ -78,9 +64,9 @@ std::string Map::stringForMoveType(MoveType type) switch (type) { case MoveType::Wall: return "Wall"; - case MoveType::Warp: return "Warp"; + case MoveType::Warp: return "Teleport"; case MoveType::Wrap: return "Wrap"; - case MoveType::ReverseWarp: return "Reverse Warp"; + case MoveType::ReverseWarp: return "Reverse Teleport"; } } @@ -106,6 +92,17 @@ std::string Map::shortForMoveType(MoveType type) } } +std::string Map::shortForMoveDir(MoveDir dir) +{ + switch (dir) + { + case MoveDir::Left: return "left"; + case MoveDir::Right: return "right"; + case MoveDir::Up: return "up"; + case MoveDir::Down: return "down"; + } +} + Map::MoveType Map::moveTypeForShort(std::string str) { if (str == "wrap") return MoveType::Wrap; @@ -115,6 +112,15 @@ Map::MoveType Map::moveTypeForShort(std::string str) return MoveType::Wall; } +Map::MoveDir Map::moveDirForShort(std::string str) +{ + if (str == "right") return MoveDir::Right; + if (str == "up") return MoveDir::Up; + if (str == "down") return MoveDir::Down; + + return MoveDir::Left; +} + int Map::getID() const { return id; @@ -167,47 +173,21 @@ bool Map::getHidden() const return hidden; } -Map::MoveType Map::getLeftMoveType() const -{ - return leftType; -} - -Map::MoveType Map::getRightMoveType() const -{ - return rightType; -} - -Map::MoveType Map::getUpMoveType() const +const std::map& Map::getAdjacents() const { - return upType; + return adjacents; } -Map::MoveType Map::getDownMoveType() const +const Map::Adjacent& Map::getAdjacent(MoveDir direction) const { - return downType; -} - -int Map::getLeftMoveMapID() const -{ - return leftMap; -} - -int Map::getRightMoveMapID() const -{ - return rightMap; -} - -int Map::getUpMoveMapID() const -{ - return upMap; -} - -int Map::getDownMoveMapID() const -{ - return downMap; + if (adjacents.count(direction) > 0) + { + return adjacents.at(direction); + } else { + return defaultAdjacent; + } } - void Map::setTitle(std::string title, bool dirty) { this->title = title; @@ -279,79 +259,11 @@ void Map::setHidden(bool hid) hidden = hid; } -void Map::setLeftMoveType(Map::MoveType move, bool dirty) -{ - leftType = move; - - if (dirty) - { - world->setDirty(true); - } -} - -void Map::setRightMoveType(Map::MoveType move, bool dirty) -{ - rightType = move; - - if (dirty) - { - world->setDirty(true); - } -} - -void Map::setUpMoveType(Map::MoveType move, bool dirty) -{ - upType = move; - - if (dirty) - { - world->setDirty(true); - } -} - -void Map::setDownMoveType(Map::MoveType move, bool dirty) -{ - downType = move; - - if (dirty) - { - world->setDirty(true); - } -} - -void Map::setLeftMoveMapID(int id, bool dirty) -{ - leftMap = id; - - if (dirty) - { - world->setDirty(true); - } -} - -void Map::setRightMoveMapID(int id, bool dirty) -{ - rightMap = id; - - if (dirty) - { - world->setDirty(true); - } -} - -void Map::setUpMoveMapID(int id, bool dirty) -{ - upMap = id; - - if (dirty) - { - world->setDirty(true); - } -} - -void Map::setDownMoveMapID(int id, bool dirty) +void Map::setAdjacent(MoveDir direction, MoveType type, int map, bool dirty) { - downMap = id; + Adjacent& cur = adjacents[direction]; + cur.type = type; + if (map != -1) cur.map = map; if (dirty) { diff --git a/tools/mapedit/src/map.h b/tools/mapedit/src/map.h index 46e5790..c7f5b30 100644 --- a/tools/mapedit/src/map.h +++ b/tools/mapedit/src/map.h @@ -7,6 +7,7 @@ #include #include #include +#include class MapObject; class World; @@ -64,6 +65,13 @@ class Map { Map& operator= (Map other); friend void swap(Map& first, Map& second); + enum class MoveDir { + Left, + Right, + Up, + Down + }; + enum class MoveType { Wall, Wrap, @@ -71,11 +79,18 @@ class Map { ReverseWarp }; + struct Adjacent { + MoveType type = MoveType::Wall; + int map = 0; + }; + static std::list listMoveTypes(); static std::string stringForMoveType(MoveType type); static bool moveTypeTakesMap(MoveType type); static std::string shortForMoveType(MoveType type); + static std::string shortForMoveDir(MoveDir dir); static MoveType moveTypeForShort(std::string str); + static MoveDir moveDirForShort(std::string str); int getID() const; std::string getTitle() const; @@ -86,14 +101,8 @@ class Map { bool getExpanded() const; World* getWorld() const; bool getHidden() const; - MoveType getLeftMoveType() const; - MoveType getRightMoveType() const; - MoveType getUpMoveType() const; - MoveType getDownMoveType() const; - int getLeftMoveMapID() const; - int getRightMoveMapID() const; - int getUpMoveMapID() const; - int getDownMoveMapID() const; + const std::map& getAdjacents() const; + const Adjacent& getAdjacent(MoveDir direction) const; void setTitle(std::string title, bool dirty = true); void setTileAt(int x, int y, int tile, bool dirty = true); @@ -104,14 +113,7 @@ class Map { void addChild(int id); void setExpanded(bool exp); void setHidden(bool hid); - void setLeftMoveType(MoveType move, bool dirty = true); - void setRightMoveType(MoveType move, bool dirty = true); - void setUpMoveType(MoveType move, bool dirty = true); - void setDownMoveType(MoveType move, bool dirty = true); - void setLeftMoveMapID(int id, bool dirty = true); - void setRightMoveMapID(int id, bool dirty = true); - void setUpMoveMapID(int id, bool dirty = true); - void setDownMoveMapID(int id, bool dirty = true); + void setAdjacent(MoveDir direction, MoveType type, int map = -1, bool dirty = true); private: int id; @@ -123,14 +125,8 @@ class Map { wxTreeItemId treeItemId; bool expanded = false; bool hidden = false; - MoveType leftType = MoveType::Wall; - MoveType rightType = MoveType::Wall; - MoveType upType = MoveType::Wall; - MoveType downType = MoveType::Wall; - int leftMap = 0; - int rightMap = 0; - int upMap = 0; - int downMap = 0; + std::map adjacents; + const Adjacent defaultAdjacent {}; }; class MapPtrCtr : public wxTreeItemData { diff --git a/tools/mapedit/src/widget.cpp b/tools/mapedit/src/widget.cpp index 7f90880..fa0af39 100644 --- a/tools/mapedit/src/widget.cpp +++ b/tools/mapedit/src/widget.cpp @@ -75,30 +75,30 @@ void MapeditWidget::OnPaint(wxPaintEvent&) RenderMap(map, dc, tiles_dc); - if (map->getLeftMoveType() == Map::MoveType::Warp) + if (map->getAdjacent(Map::MoveDir::Left).type == Map::MoveType::Warp) { - auto tomap = map->getWorld()->getMap(map->getLeftMoveMapID()); + auto tomap = map->getWorld()->getMap(map->getAdjacent(Map::MoveDir::Left).map); RenderMap(tomap.get(), dc, tiles_dc, -EDITOR_SPACING_X, EDITOR_SPACING_Y, false); } - if (map->getRightMoveType() == Map::MoveType::Warp) + if (map->getAdjacent(Map::MoveDir::Right).type == Map::MoveType::Warp) { - auto tomap = map->getWorld()->getMap(map->getRightMoveMapID()); + auto tomap = map->getWorld()->getMap(map->getAdjacent(Map::MoveDir::Right).map); RenderMap(tomap.get(), dc, tiles_dc, EDITOR_WIDTH-EDITOR_SPACING_X, EDITOR_SPACING_Y, false); } - if (map->getUpMoveType() == Map::MoveType::Warp) + if (map->getAdjacent(Map::MoveDir::Up).type == Map::MoveType::Warp) { - auto tomap = map->getWorld()->getMap(map->getUpMoveMapID()); + auto tomap = map->getWorld()->getMap(map->getAdjacent(Map::MoveDir::Up).map); RenderMap(tomap.get(), dc, tiles_dc, EDITOR_SPACING_X, -EDITOR_SPACING_Y, false); } - if (map->getDownMoveType() == Map::MoveType::Warp) + if (map->getAdjacent(Map::MoveDir::Down).type == Map::MoveType::Warp) { - auto tomap = map->getWorld()->getMap(map->getDownMoveMapID()); + auto tomap = map->getWorld()->getMap(map->getAdjacent(Map::MoveDir::Down).map); RenderMap(tomap.get(), dc, tiles_dc, EDITOR_SPACING_X, EDITOR_HEIGHT-EDITOR_SPACING_Y, false); } diff --git a/tools/mapedit/src/world.cpp b/tools/mapedit/src/world.cpp index 043cf8a..01225cf 100644 --- a/tools/mapedit/src/world.cpp +++ b/tools/mapedit/src/world.cpp @@ -36,43 +36,43 @@ World::World(std::string filename) throw MapLoadException(filename); } + xmlChar* nextmapKey = xmlGetProp(top, (xmlChar*) "nextmap"); + if (nextmapKey != 0) + { + nextMapID = atoi((char*) nextmapKey); + } + xmlFree(nextmapKey); + + xmlChar* lastmapKey = xmlGetProp(top, (xmlChar*) "lastmap"); + if (lastmapKey != 0) + { + lastmap = atoi((char*) lastmapKey); + } + xmlFree(lastmapKey); + + xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx"); + if (startxKey == 0) throw MapLoadException(filename); + startingPosition.first = atoi((char*) startxKey); + xmlFree(startxKey); + + xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty"); + if (startyKey == 0) throw MapLoadException(filename); + startingPosition.second = atoi((char*) startyKey); + xmlFree(startyKey); + + xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap"); + if (startxKey == 0) throw MapLoadException(filename); + startingMap = atoi((char*) startmapKey); + xmlFree(startmapKey); + for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) { - if (!xmlStrcmp(node->name, (const xmlChar*) "nextmapid")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - if (key != 0) - { - nextMapID = atoi((char*) key); - } - xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "lastmap")) + if (!xmlStrcmp(node->name, (const xmlChar*) "root")) { xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - if (key != 0) - { - lastmap = atoi((char*) key); - } + if (key == 0) throw MapLoadException(filename); + rootChildren.push_back(atoi((char*) key)); xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "root")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - if (key != 0) - { - rootChildren.push_back(atoi((char*) key)); - } - xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "startpos")) - { - xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); - if (idKey == 0) throw MapLoadException(filename); - startingMap = atoi((char*) idKey); - xmlFree(idKey); - - xmlChar* posKey = xmlGetProp(node, (xmlChar*) "pos"); - if (posKey == 0) throw MapLoadException(filename); - sscanf((char*) posKey, "%d,%d", &startingPosition.first, &startingPosition.second); - xmlFree(posKey); } else if (!xmlStrcmp(node->name, (const xmlChar*) "map")) { xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); @@ -82,18 +82,21 @@ World::World(std::string filename) auto map = std::make_shared(id, this); + xmlChar* expandKey = xmlGetProp(node, (xmlChar*) "expanded"); + if ((expandKey != 0) && (!xmlStrcmp(expandKey, (const xmlChar*) "true"))) + { + map->setExpanded(true); + } + xmlFree(expandKey); + + xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title"); + if (titleKey == 0) throw MapLoadException(filename); + map->setTitle((char*) titleKey, false); + xmlFree(titleKey); + for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) { - if (!xmlStrcmp(mapNode->name, (const xmlChar*) "name")) - { - xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); - if (key != 0) - { - map->setTitle((char*) key, false); - } - - xmlFree(key); - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) + if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) { xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); @@ -104,88 +107,50 @@ World::World(std::string filename) } map->setMapdata(mapdata, false); xmlFree(key); - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "leftmap")) + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entity")) { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) throw MapLoadException(filename); - map->setLeftMoveType(Map::moveTypeForShort((char*) typeKey), false); - xmlFree(typeKey); + auto data = std::make_shared(); - if (Map::moveTypeTakesMap(map->getLeftMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) throw MapLoadException(filename); - map->setLeftMoveMapID(atoi((char*) idKey), false); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "rightmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); + xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); if (typeKey == 0) throw MapLoadException(filename); - map->setRightMoveType(Map::moveTypeForShort((char*) typeKey), false); + data->object = MapObject::getAllObjects().at((char*) typeKey).get(); xmlFree(typeKey); - if (Map::moveTypeTakesMap(map->getRightMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) throw MapLoadException(filename); - map->setRightMoveMapID(atoi((char*) idKey), false); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "upmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) throw MapLoadException(filename); - map->setUpMoveType(Map::moveTypeForShort((char*) typeKey), false); - xmlFree(typeKey); + xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x"); + if (xKey == 0) throw MapLoadException(filename); + data->position.first = atoi((char*) xKey); + xmlFree(xKey); - if (Map::moveTypeTakesMap(map->getUpMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) throw MapLoadException(filename); - map->setUpMoveMapID(atoi((char*) idKey), false); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "downmap")) + xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y"); + if (yKey == 0) throw MapLoadException(filename); + data->position.second = atoi((char*) yKey); + xmlFree(yKey); + + map->addObject(data, false); + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent")) { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); + Map::MoveDir direction; + Map::MoveType moveType; + int mapId = 0; + + xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir"); + if (dirKey == 0) throw MapLoadException(filename); + direction = Map::moveDirForShort((char*) dirKey); + xmlFree(dirKey); + + xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); if (typeKey == 0) throw MapLoadException(filename); - map->setDownMoveType(Map::moveTypeForShort((char*) typeKey), false); + moveType = Map::moveTypeForShort((char*) typeKey); xmlFree(typeKey); - if (Map::moveTypeTakesMap(map->getDownMoveType())) + xmlChar* mapIdKey = xmlGetProp(mapNode, (const xmlChar*) "map"); + if (mapIdKey != 0) { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) throw MapLoadException(filename); - map->setDownMoveMapID(atoi((char*) idKey), false); - xmlFree(idKey); + mapId = atoi((char*) mapIdKey); } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entities")) - { - for (xmlNodePtr entityNode = mapNode->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) - { - if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) - { - auto data = std::make_shared(); + xmlFree(mapIdKey); - for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) - { - if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - data->object = MapObject::getAllObjects().at((char*) key).get(); - xmlFree(key); - } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - sscanf((char*) key, "%d,%d", &data->position.first, &data->position.second); - xmlFree(key); - } - } - - map->addObject(data, false); - } - } + map->setAdjacent(direction, moveType, mapId, false); } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "child")) { xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); @@ -194,13 +159,6 @@ World::World(std::string filename) map->addChild(atoi((char*) key)); } xmlFree(key); - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "expanded")) - { - xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); - if ((key != 0) && ((char) key[0] == '1')) - { - map->setExpanded(true); - } } } @@ -266,16 +224,24 @@ void World::save(std::string name, wxTreeCtrl* mapTree) rc = xmlTextWriterStartElement(writer, (xmlChar*) "world"); if (rc < 0) throw MapWriteException(name); - // - std::ostringstream nextMap_out; - nextMap_out << nextMapID; - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "nextmapid", (xmlChar*) nextMap_out.str().c_str()); + // nextmap= + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "nextmap", "%d", nextMapID); + if (rc < 0) throw MapWriteException(name); + + // lastmap= + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "lastmap", "%d", lastmap); + if (rc < 0) throw MapWriteException(name); + + // startx= + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "startx", "%d", startingPosition.first); if (rc < 0) throw MapWriteException(name); - // - std::ostringstream lastMap_out; - lastMap_out << lastmap; - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "lastmap", (xmlChar*) lastMap_out.str().c_str()); + // starty= + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "starty", "%d", startingPosition.second); + if (rc < 0) throw MapWriteException(name); + + // startmap= + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "startmap", "%d", startingMap); if (rc < 0) throw MapWriteException(name); // ASSUMPTION: There will always be at least one child of the invisible root element. i.e. you cannot delete to zero maps. @@ -285,28 +251,10 @@ void World::save(std::string name, wxTreeCtrl* mapTree) { // MapPtrCtr* ctl = (MapPtrCtr*) mapTree->GetItemData(it); - std::ostringstream rootid_out; - rootid_out << ctl->map->getID(); - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "root", (xmlChar*) rootid_out.str().c_str()); + rc = xmlTextWriterWriteFormatElement(writer, (xmlChar*) "root", "%d", ctl->map->getID()); if (rc < 0) throw MapWriteException(name); } - // - rc = xmlTextWriterStartElement(writer, (xmlChar*) "startpos"); - if (rc < 0) throw MapWriteException(name); - - // id= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "id", "%d", startingMap); - if (rc < 0) throw MapWriteException(name); - - // pos= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "pos", "%d,%d", startingPosition.first, startingPosition.second); - if (rc < 0) throw MapWriteException(name); - - // - rc = xmlTextWriterEndElement(writer); - if (rc < 0) throw MapWriteException(name); - for (auto mapPair : maps) { Map& map = *mapPair.second; @@ -318,16 +266,33 @@ void World::save(std::string name, wxTreeCtrl* mapTree) if (rc < 0) throw MapWriteException(name); // id= - std::ostringstream id_out; - id_out << map.getID(); - rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "id", (xmlChar*) id_out.str().c_str()); + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "id", "%d", map.getID()); if (rc < 0) throw MapWriteException(name); - // - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "name", (xmlChar*) map.getTitle().c_str()); + // expanded= + wxTreeItemId node = map.getTreeItemId(); + if (mapTree->IsExpanded(node)) + { + rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "expanded", (xmlChar*) "true"); + if (rc < 0) throw MapWriteException(name); + } else { + rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "expanded", (xmlChar*) "false"); + if (rc < 0) throw MapWriteException(name); + } + + // title= + rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "name", (xmlChar*) map.getTitle().c_str()); if (rc < 0) throw MapWriteException(name); - // + // std::ostringstream mapdata_out; for (int y=0; y - rc = xmlTextWriterStartElement(writer, (xmlChar*) "leftmap"); - if (rc < 0) throw MapWriteException(name); - // type= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "type", "%s", Map::shortForMoveType(map.getLeftMoveType()).c_str()); - if (rc < 0) throw MapWriteException(name); - - if (Map::moveTypeTakesMap(map.getLeftMoveType())) - { - // map= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "map", "%d", map.getLeftMoveMapID()); - if (rc < 0) throw MapWriteException(name); - } - - // + // rc = xmlTextWriterEndElement(writer); if (rc < 0) throw MapWriteException(name); - // - rc = xmlTextWriterStartElement(writer, (xmlChar*) "rightmap"); - if (rc < 0) throw MapWriteException(name); - - // type= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "type", "%s", Map::shortForMoveType(map.getRightMoveType()).c_str()); - if (rc < 0) throw MapWriteException(name); - - if (Map::moveTypeTakesMap(map.getRightMoveType())) + for (auto object : map.getObjects()) { - // map= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "map", "%d", map.getRightMoveMapID()); + // + rc = xmlTextWriterStartElement(writer, (xmlChar*) "entity"); if (rc < 0) throw MapWriteException(name); - } - - // - rc = xmlTextWriterEndElement(writer); - if (rc < 0) throw MapWriteException(name); - // - rc = xmlTextWriterStartElement(writer, (xmlChar*) "upmap"); - if (rc < 0) throw MapWriteException(name); - - // type= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "type", "%s", Map::shortForMoveType(map.getUpMoveType()).c_str()); - if (rc < 0) throw MapWriteException(name); - - if (Map::moveTypeTakesMap(map.getUpMoveType())) - { - // map= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "map", "%d", map.getUpMoveMapID()); + // type= + rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "type", (xmlChar*) object->object->getType().c_str()); if (rc < 0) throw MapWriteException(name); - } - - // - rc = xmlTextWriterEndElement(writer); - if (rc < 0) throw MapWriteException(name); - // - rc = xmlTextWriterStartElement(writer, (xmlChar*) "downmap"); - if (rc < 0) throw MapWriteException(name); - - // type= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "type", "%s", Map::shortForMoveType(map.getDownMoveType()).c_str()); - if (rc < 0) throw MapWriteException(name); + // x= + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "x", "%d", object->position.first); + if (rc < 0) throw MapWriteException(name); + + // y= + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "y", "%d", object->position.second); + if (rc < 0) throw MapWriteException(name); - if (Map::moveTypeTakesMap(map.getDownMoveType())) - { - // map= - rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "map", "%d", map.getDownMoveMapID()); + // + rc = xmlTextWriterEndElement(writer); if (rc < 0) throw MapWriteException(name); } - // - rc = xmlTextWriterEndElement(writer); - if (rc < 0) throw MapWriteException(name); - - // - rc = xmlTextWriterStartElement(writer, (xmlChar*) "entities"); - if (rc < 0) throw MapWriteException(name); - - for (auto object : map.getObjects()) + for (auto adjacent : map.getAdjacents()) { - // - rc = xmlTextWriterStartElement(writer, (xmlChar*) "entity"); + // + rc = xmlTextWriterStartElement(writer, (xmlChar*) "adjacent"); if (rc < 0) throw MapWriteException(name); - - // - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-type", (xmlChar*) object->object->getType().c_str()); + + // dir= + rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "dir", (xmlChar*) Map::shortForMoveDir(adjacent.first).c_str()); if (rc < 0) throw MapWriteException(name); - - // - std::ostringstream entpos_out; - entpos_out << object->position.first << "," << object->position.second; - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-position", (xmlChar*) entpos_out.str().c_str()); + + // type= + rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "type", (xmlChar*) Map::shortForMoveType(adjacent.second.type).c_str()); if (rc < 0) throw MapWriteException(name); - - // + + // map= + if (Map::moveTypeTakesMap(adjacent.second.type)) + { + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "map", "%d", adjacent.second.map); + if (rc < 0) throw MapWriteException(name); + } + + // rc = xmlTextWriterEndElement(writer); if (rc < 0) throw MapWriteException(name); } - - // - rc = xmlTextWriterEndElement(writer); - if (rc < 0) throw MapWriteException(name); - wxTreeItemId node = map.getTreeItemId(); if (mapTree->ItemHasChildren(node)) { wxTreeItemIdValue cookie2; @@ -455,16 +367,7 @@ void World::save(std::string name, wxTreeCtrl* mapTree) { // MapPtrCtr* ctl = (MapPtrCtr*) mapTree->GetItemData(it); - std::ostringstream childid_out; - childid_out << ctl->map->getID(); - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "child", (xmlChar*) childid_out.str().c_str()); - if (rc < 0) throw MapWriteException(name); - } - - if (mapTree->IsExpanded(node)) - { - // - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "expanded", (xmlChar*) "1"); + rc = xmlTextWriterWriteFormatElement(writer, (xmlChar*) "child", "%d", ctl->map->getID()); if (rc < 0) throw MapWriteException(name); } } -- cgit 1.4.1