diff options
Diffstat (limited to 'src/world.cpp')
| -rw-r--r-- | src/world.cpp | 134 |
1 files changed, 134 insertions, 0 deletions
| 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 @@ | |||
| 1 | #include "world.h" | ||
| 2 | #include <libxml/parser.h> | ||
| 3 | #include "consts.h" | ||
| 4 | |||
| 5 | World::World(const char* filename) | ||
| 6 | { | ||
| 7 | xmlDocPtr doc = xmlParseFile(filename); | ||
| 8 | if (doc == nullptr) | ||
| 9 | { | ||
| 10 | exit(2); | ||
| 11 | } | ||
| 12 | |||
| 13 | xmlNodePtr top = xmlDocGetRootElement(doc); | ||
| 14 | if (top == nullptr) | ||
| 15 | { | ||
| 16 | exit(2); | ||
| 17 | } | ||
| 18 | |||
| 19 | if (xmlStrcmp(top->name, (const xmlChar*) "world")) | ||
| 20 | { | ||
| 21 | exit(2); | ||
| 22 | } | ||
| 23 | |||
| 24 | xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx"); | ||
| 25 | if (startxKey == 0) exit(2); | ||
| 26 | startX = atoi((char*) startxKey); | ||
| 27 | xmlFree(startxKey); | ||
| 28 | |||
| 29 | xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty"); | ||
| 30 | if (startyKey == 0) exit(2); | ||
| 31 | startY = atoi((char*) startyKey); | ||
| 32 | xmlFree(startyKey); | ||
| 33 | |||
| 34 | xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap"); | ||
| 35 | if (startxKey == 0) exit(2); | ||
| 36 | startMap = atoi((char*) startmapKey); | ||
| 37 | xmlFree(startmapKey); | ||
| 38 | |||
| 39 | for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) | ||
| 40 | { | ||
| 41 | if (!xmlStrcmp(node->name, (const xmlChar*) "map")) | ||
| 42 | { | ||
| 43 | xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); | ||
| 44 | if (idKey == 0) exit(2); | ||
| 45 | int theId = atoi((char*) idKey); | ||
| 46 | xmlFree(idKey); | ||
| 47 | |||
| 48 | maps.emplace(theId, theId); | ||
| 49 | Map& map = maps[theId]; | ||
| 50 | |||
| 51 | xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title"); | ||
| 52 | if (titleKey == 0) exit(2); | ||
| 53 | map.setTitle((char*) titleKey); | ||
| 54 | xmlFree(titleKey); | ||
| 55 | |||
| 56 | for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) | ||
| 57 | { | ||
| 58 | if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) | ||
| 59 | { | ||
| 60 | xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); | ||
| 61 | int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | ||
| 62 | mapdata[0] = atoi(strtok((char*) key, ",\n")); | ||
| 63 | for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) | ||
| 64 | { | ||
| 65 | mapdata[i] = atoi(strtok(NULL, ",\n")); | ||
| 66 | } | ||
| 67 | map.setMapdata(mapdata); | ||
| 68 | 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 | map.addEntity(data); | ||
| 89 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent")) | ||
| 90 | { | ||
| 91 | Map::MoveDir direction; | ||
| 92 | Map::MoveType moveType; | ||
| 93 | int mapId = 0; | ||
| 94 | |||
| 95 | xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir"); | ||
| 96 | if (dirKey == 0) exit(2); | ||
| 97 | direction = Map::moveDirForShort((char*) dirKey); | ||
| 98 | xmlFree(dirKey); | ||
| 99 | |||
| 100 | xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); | ||
| 101 | if (typeKey == 0) exit(2); | ||
| 102 | moveType = Map::moveTypeForShort((char*) typeKey); | ||
| 103 | xmlFree(typeKey); | ||
| 104 | |||
| 105 | xmlChar* mapIdKey = xmlGetProp(mapNode, (const xmlChar*) "map"); | ||
| 106 | if (mapIdKey != 0) | ||
| 107 | { | ||
| 108 | mapId = atoi((char*) mapIdKey); | ||
| 109 | } | ||
| 110 | xmlFree(mapIdKey); | ||
| 111 | |||
| 112 | map.setAdjacent(direction, moveType, mapId); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | xmlFreeDoc(doc); | ||
| 119 | } | ||
| 120 | |||
| 121 | const Map& World::getMap(int id) const | ||
| 122 | { | ||
| 123 | return maps.at(id); | ||
| 124 | } | ||
| 125 | |||
| 126 | const Map& World::getStartingMap() const | ||
| 127 | { | ||
| 128 | return maps.at(startMap); | ||
| 129 | } | ||
| 130 | |||
| 131 | std::pair<int, int> World::getStartingPosition() const | ||
| 132 | { | ||
| 133 | return std::make_pair(startX, startY); | ||
| 134 | } | ||
