summary refs log tree commit diff stats
path: root/src/world.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/world.cpp')
-rw-r--r--src/world.cpp155
1 files changed, 0 insertions, 155 deletions
diff --git a/src/world.cpp b/src/world.cpp deleted file mode 100644 index 3b6bd41..0000000 --- a/src/world.cpp +++ /dev/null
@@ -1,155 +0,0 @@
1#include "world.h"
2#include <libxml/parser.h>
3#include <stdexcept>
4#include <cstring>
5#include "consts.h"
6
7inline xmlChar* getProp(xmlNodePtr node, const char* attr)
8{
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
18World::World(std::string filename)
19{
20 xmlDocPtr doc = xmlParseFile(filename.c_str());
21 if (doc == nullptr)
22 {
23 throw std::invalid_argument("Cannot find world file");
24 }
25
26 xmlNodePtr top = xmlDocGetRootElement(doc);
27 if (top == nullptr)
28 {
29 throw std::invalid_argument("Error parsing world file");
30 }
31
32 if (xmlStrcmp(top->name, reinterpret_cast<const xmlChar*>("world")))
33 {
34 throw std::invalid_argument("Error parsing world file");
35 }
36
37 xmlChar* key = nullptr;
38
39 key = getProp(top, "startx");
40 startX_ = atoi(reinterpret_cast<char*>(key));
41 xmlFree(key);
42
43 key = getProp(top, "starty");
44 startY_ = atoi(reinterpret_cast<char*>(key));
45 xmlFree(key);
46
47 key = getProp(top, "startmap");
48 startMap_ = atoi(reinterpret_cast<char*>(key));
49 xmlFree(key);
50
51 for (xmlNodePtr node = top->xmlChildrenNode;
52 node != nullptr;
53 node = node->next)
54 {
55 if (!xmlStrcmp(node->name, reinterpret_cast<const xmlChar*>("map")))
56 {
57 key = getProp(node, "id");
58 size_t mapId = atoi(reinterpret_cast<char*>(key));
59 xmlFree(key);
60
61 key = getProp(node, "title");
62 std::string mapTitle(reinterpret_cast<char*>(key));
63 xmlFree(key);
64
65 std::vector<int> mapTiles;
66 Map::Adjacent leftAdj;
67 Map::Adjacent rightAdj;
68 Map::Adjacent upAdj;
69 Map::Adjacent downAdj;
70
71 for (xmlNodePtr mapNode = node->xmlChildrenNode;
72 mapNode != nullptr;
73 mapNode = mapNode->next)
74 {
75 if (!xmlStrcmp(
76 mapNode->name,
77 reinterpret_cast<const xmlChar*>("environment")))
78 {
79 key = xmlNodeGetContent(mapNode);
80
81 mapTiles.clear();
82 mapTiles.push_back(atoi(strtok(reinterpret_cast<char*>(key), ",\n")));
83 for (size_t i = 1; i < (MAP_WIDTH * MAP_HEIGHT); i++)
84 {
85 mapTiles.push_back(atoi(strtok(nullptr, ",\n")));
86 }
87
88 xmlFree(key);
89 } else if (!xmlStrcmp(
90 mapNode->name,
91 reinterpret_cast<const xmlChar*>("adjacent")))
92 {
93 key = getProp(mapNode, "type");
94 std::string adjTypeStr(reinterpret_cast<char*>(key));
95 xmlFree(key);
96
97 Map::Adjacent::Type adjType;
98 if (adjTypeStr == "wall")
99 {
100 adjType = Map::Adjacent::Type::wall;
101 } else if (adjTypeStr == "wrap")
102 {
103 adjType = Map::Adjacent::Type::wrap;
104 } else if (adjTypeStr == "warp")
105 {
106 adjType = Map::Adjacent::Type::warp;
107 } else if (adjTypeStr == "reverseWarp")
108 {
109 adjType = Map::Adjacent::Type::reverse;
110 } else {
111 throw std::logic_error("Invalid adjacency type");
112 }
113
114 key = getProp(mapNode, "map");
115 int adjMapId = atoi(reinterpret_cast<char*>(key));
116 xmlFree(key);
117
118 key = getProp(mapNode, "dir");
119 std::string adjDir(reinterpret_cast<char*>(key));
120 xmlFree(key);
121
122 if (adjDir == "left")
123 {
124 leftAdj = {adjType, adjMapId};
125 } else if (adjDir == "right")
126 {
127 rightAdj = {adjType, adjMapId};
128 } else if (adjDir == "up")
129 {
130 upAdj = {adjType, adjMapId};
131 } else if (adjDir == "down")
132 {
133 downAdj = {adjType, adjMapId};
134 } else {
135 throw std::logic_error("Invalid adjacency direction");
136 }
137 }
138 }
139
140 maps_.emplace(
141 std::piecewise_construct,
142 std::forward_as_tuple(mapId),
143 std::forward_as_tuple(
144 mapId,
145 std::move(mapTiles),
146 std::move(mapTitle),
147 leftAdj,
148 rightAdj,
149 upAdj,
150 downAdj));
151 }
152 }
153
154 xmlFreeDoc(doc);
155}