diff options
Diffstat (limited to 'src/world.cpp')
| -rw-r--r-- | src/world.cpp | 195 |
1 files changed, 72 insertions, 123 deletions
| diff --git a/src/world.cpp b/src/world.cpp index 0c61c47..9b1e4f6 100644 --- a/src/world.cpp +++ b/src/world.cpp | |||
| @@ -1,150 +1,99 @@ | |||
| 1 | #include "world.h" | 1 | #include "world.h" |
| 2 | #include <libxml/parser.h> | 2 | #include <libxml/parser.h> |
| 3 | #include <stdexcept> | ||
| 4 | #include <cstring> | ||
| 3 | #include "consts.h" | 5 | #include "consts.h" |
| 4 | 6 | ||
| 5 | World::World(const char* filename) | 7 | inline xmlChar* getProp(xmlNodePtr node, const char* attr) |
| 6 | { | 8 | { |
| 7 | xmlDocPtr doc = xmlParseFile(filename); | 9 | xmlChar* key = xmlGetProp(node, reinterpret_cast<const xmlChar*>(attr)); |
| 10 | if (key == nullptr) | ||
| 11 | { | ||
| 12 | throw std::invalid_argument("Error parsing world file"); | ||
| 13 | } | ||
| 14 | |||
| 15 | return key; | ||
| 16 | } | ||
| 17 | |||
| 18 | World::World(std::string filename) | ||
| 19 | { | ||
| 20 | xmlDocPtr doc = xmlParseFile(filename.c_str()); | ||
| 8 | if (doc == nullptr) | 21 | if (doc == nullptr) |
| 9 | { | 22 | { |
| 10 | exit(2); | 23 | throw std::invalid_argument("Cannot find world file"); |
| 11 | } | 24 | } |
| 12 | 25 | ||
| 13 | xmlNodePtr top = xmlDocGetRootElement(doc); | 26 | xmlNodePtr top = xmlDocGetRootElement(doc); |
| 14 | if (top == nullptr) | 27 | if (top == nullptr) |
| 15 | { | 28 | { |
| 16 | exit(2); | 29 | throw std::invalid_argument("Error parsing world file"); |
| 17 | } | 30 | } |
| 18 | 31 | ||
| 19 | if (xmlStrcmp(top->name, (const xmlChar*) "world")) | 32 | if (xmlStrcmp(top->name, reinterpret_cast<const xmlChar*>("world"))) |
| 20 | { | 33 | { |
| 21 | exit(2); | 34 | throw std::invalid_argument("Error parsing world file"); |
| 22 | } | 35 | } |
| 23 | 36 | ||
| 24 | xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx"); | 37 | xmlChar* key = nullptr; |
| 25 | if (startxKey == 0) exit(2); | 38 | |
| 26 | startX = atoi((char*) startxKey); | 39 | key = getProp(top, "startx"); |
| 27 | xmlFree(startxKey); | 40 | startX_ = atoi(reinterpret_cast<char*>(key)); |
| 28 | 41 | xmlFree(key); | |
| 29 | xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty"); | 42 | |
| 30 | if (startyKey == 0) exit(2); | 43 | key = getProp(top, "starty"); |
| 31 | startY = atoi((char*) startyKey); | 44 | startY_ = atoi(reinterpret_cast<char*>(key)); |
| 32 | xmlFree(startyKey); | 45 | xmlFree(key); |
| 33 | 46 | ||
| 34 | xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap"); | 47 | key = getProp(top, "startmap"); |
| 35 | if (startxKey == 0) exit(2); | 48 | startMap_ = atoi(reinterpret_cast<char*>(key)); |
| 36 | startMap = atoi((char*) startmapKey); | 49 | xmlFree(key); |
| 37 | xmlFree(startmapKey); | 50 | |
| 38 | 51 | for (xmlNodePtr node = top->xmlChildrenNode; | |
| 39 | for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) | 52 | node != nullptr; |
| 53 | node = node->next) | ||
| 40 | { | 54 | { |
| 41 | if (!xmlStrcmp(node->name, (const xmlChar*) "map")) | 55 | if (!xmlStrcmp(node->name, reinterpret_cast<const xmlChar*>("map"))) |
| 42 | { | 56 | { |
| 43 | xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); | 57 | key = getProp(node, "id"); |
| 44 | if (idKey == 0) exit(2); | 58 | size_t mapId = atoi(reinterpret_cast<char*>(key)); |
| 45 | int theId = atoi((char*) idKey); | 59 | xmlFree(key); |
| 46 | xmlFree(idKey); | 60 | |
| 47 | 61 | key = getProp(node, "title"); | |
| 48 | maps.emplace(theId, theId); | 62 | std::string mapTitle(reinterpret_cast<char*>(key)); |
| 49 | Map& map = maps[theId]; | 63 | xmlFree(key); |
| 50 | 64 | ||
| 51 | xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title"); | 65 | std::vector<int> mapTiles; |
| 52 | if (titleKey == 0) exit(2); | 66 | |
| 53 | map.setTitle((char*) titleKey); | 67 | for (xmlNodePtr mapNode = node->xmlChildrenNode; |
| 54 | xmlFree(titleKey); | 68 | mapNode != nullptr; |
| 55 | 69 | mapNode = mapNode->next) | |
| 56 | for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) | ||
| 57 | { | 70 | { |
| 58 | if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) | 71 | if (!xmlStrcmp( |
| 72 | mapNode->name, | ||
| 73 | reinterpret_cast<const xmlChar*>("environment"))) | ||
| 59 | { | 74 | { |
| 60 | xmlChar* key = xmlNodeGetContent(mapNode); | 75 | key = xmlNodeGetContent(mapNode); |
| 61 | int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | 76 | |
| 62 | mapdata[0] = atoi(strtok((char*) key, ",\n")); | 77 | mapTiles.clear(); |
| 63 | for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) | 78 | mapTiles.push_back(atoi(strtok(reinterpret_cast<char*>(key), ",\n"))); |
| 79 | for (size_t i = 1; i < (MAP_WIDTH * MAP_HEIGHT); i++) | ||
| 64 | { | 80 | { |
| 65 | mapdata[i] = atoi(strtok(NULL, ",\n")); | 81 | mapTiles.push_back(atoi(strtok(nullptr, ",\n"))); |
| 66 | } | 82 | } |
| 67 | map.setMapdata(mapdata); | 83 | |
| 68 | xmlFree(key); | 84 | xmlFree(key); |
| 69 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entity")) | ||
| 70 | { | ||
| 71 | Map::EntityData data; | ||
| 72 | |||
| 73 | xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); | ||
| 74 | if (typeKey == 0) exit(2); | ||
| 75 | data.name = (char*) typeKey; | ||
| 76 | xmlFree(typeKey); | ||
| 77 | |||
| 78 | xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x"); | ||
| 79 | if (xKey == 0) exit(2); | ||
| 80 | data.position.first = atoi((char*) xKey); | ||
| 81 | xmlFree(xKey); | ||
| 82 | |||
| 83 | xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y"); | ||
| 84 | if (yKey == 0) exit(2); | ||
| 85 | data.position.second = atoi((char*) yKey); | ||
| 86 | xmlFree(yKey); | ||
| 87 | |||
| 88 | for (xmlNodePtr entityNode = mapNode->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) | ||
| 89 | { | ||
| 90 | if (!xmlStrcmp(entityNode->name, (xmlChar*) "item")) | ||
| 91 | { | ||
| 92 | xmlChar* itemIdKey = xmlGetProp(entityNode, (const xmlChar*) "id"); | ||
| 93 | if (itemIdKey == 0) exit(2); | ||
| 94 | std::string itemId = (char*) itemIdKey; | ||
| 95 | xmlFree(itemIdKey); | ||
| 96 | |||
| 97 | xmlChar* itemIdVal = xmlNodeGetContent(entityNode); | ||
| 98 | if (itemIdVal == 0) exit(2); | ||
| 99 | data.items[itemId] = atoi((char*) itemIdVal); | ||
| 100 | xmlFree(itemIdVal); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | map.addEntity(data); | ||
| 105 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent")) | ||
| 106 | { | ||
| 107 | Map::MoveDir direction; | ||
| 108 | Map::MoveType moveType; | ||
| 109 | int mapId = 0; | ||
| 110 | |||
| 111 | xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir"); | ||
| 112 | if (dirKey == 0) exit(2); | ||
| 113 | direction = Map::moveDirForShort((char*) dirKey); | ||
| 114 | xmlFree(dirKey); | ||
| 115 | |||
| 116 | xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); | ||
| 117 | if (typeKey == 0) exit(2); | ||
| 118 | moveType = Map::moveTypeForShort((char*) typeKey); | ||
| 119 | xmlFree(typeKey); | ||
| 120 | |||
| 121 | xmlChar* mapIdKey = xmlGetProp(mapNode, (const xmlChar*) "map"); | ||
| 122 | if (mapIdKey != 0) | ||
| 123 | { | ||
| 124 | mapId = atoi((char*) mapIdKey); | ||
| 125 | } | ||
| 126 | xmlFree(mapIdKey); | ||
| 127 | |||
| 128 | map.setAdjacent(direction, moveType, mapId); | ||
| 129 | } | 85 | } |
| 130 | } | 86 | } |
| 87 | |||
| 88 | maps_.emplace( | ||
| 89 | std::piecewise_construct, | ||
| 90 | std::forward_as_tuple(mapId), | ||
| 91 | std::forward_as_tuple( | ||
| 92 | mapId, | ||
| 93 | std::move(mapTiles), | ||
| 94 | std::move(mapTitle))); | ||
| 131 | } | 95 | } |
| 132 | } | 96 | } |
| 133 | |||
| 134 | xmlFreeDoc(doc); | ||
| 135 | } | ||
| 136 | 97 | ||
| 137 | const Map& World::getMap(int id) const | 98 | xmlFreeDoc(doc); |
| 138 | { | ||
| 139 | return maps.at(id); | ||
| 140 | } | ||
| 141 | |||
| 142 | const Map& World::getStartingMap() const | ||
| 143 | { | ||
| 144 | return maps.at(startMap); | ||
| 145 | } | ||
| 146 | |||
| 147 | std::pair<int, int> World::getStartingPosition() const | ||
| 148 | { | ||
| 149 | return std::make_pair(startX, startY); | ||
| 150 | } | 99 | } |
