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.cpp67
1 files changed, 54 insertions, 13 deletions
diff --git a/src/world.cpp b/src/world.cpp index 3b6bd41..d239067 100644 --- a/src/world.cpp +++ b/src/world.cpp
@@ -1,19 +1,8 @@
1#include "world.h" 1#include "world.h"
2#include <libxml/parser.h>
3#include <stdexcept> 2#include <stdexcept>
4#include <cstring> 3#include <cstring>
5#include "consts.h" 4#include "consts.h"
6 5#include "xml.h"
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 6
18World::World(std::string filename) 7World::World(std::string filename)
19{ 8{
@@ -67,6 +56,7 @@ World::World(std::string filename)
67 Map::Adjacent rightAdj; 56 Map::Adjacent rightAdj;
68 Map::Adjacent upAdj; 57 Map::Adjacent upAdj;
69 Map::Adjacent downAdj; 58 Map::Adjacent downAdj;
59 std::list<Map::Object> objects;
70 60
71 for (xmlNodePtr mapNode = node->xmlChildrenNode; 61 for (xmlNodePtr mapNode = node->xmlChildrenNode;
72 mapNode != nullptr; 62 mapNode != nullptr;
@@ -77,6 +67,10 @@ World::World(std::string filename)
77 reinterpret_cast<const xmlChar*>("environment"))) 67 reinterpret_cast<const xmlChar*>("environment")))
78 { 68 {
79 key = xmlNodeGetContent(mapNode); 69 key = xmlNodeGetContent(mapNode);
70 if (key == nullptr)
71 {
72 throw std::invalid_argument("Error parsing XML file");
73 }
80 74
81 mapTiles.clear(); 75 mapTiles.clear();
82 mapTiles.push_back(atoi(strtok(reinterpret_cast<char*>(key), ",\n"))); 76 mapTiles.push_back(atoi(strtok(reinterpret_cast<char*>(key), ",\n")));
@@ -88,6 +82,52 @@ World::World(std::string filename)
88 xmlFree(key); 82 xmlFree(key);
89 } else if (!xmlStrcmp( 83 } else if (!xmlStrcmp(
90 mapNode->name, 84 mapNode->name,
85 reinterpret_cast<const xmlChar*>("entity")))
86 {
87 key = getProp(mapNode, "type");
88 std::string entType(reinterpret_cast<char*>(key));
89 xmlFree(key);
90
91 key = getProp(mapNode, "x");
92 int entX = atoi(reinterpret_cast<char*>(key));
93 xmlFree(key);
94
95 key = getProp(mapNode, "y");
96 int entY = atoi(reinterpret_cast<char*>(key));
97 xmlFree(key);
98
99 key = getProp(mapNode, "index");
100 int entIndex = atoi(reinterpret_cast<char*>(key));
101 xmlFree(key);
102
103 std::map<std::string, int> items;
104
105 for (xmlNodePtr entityNode = mapNode->xmlChildrenNode;
106 entityNode != nullptr;
107 entityNode = entityNode->next)
108 {
109 key = getProp(entityNode, "id");
110 std::string itemId(reinterpret_cast<char*>(key));
111 xmlFree(key);
112
113 key = xmlNodeGetContent(entityNode);
114 if (key == nullptr)
115 {
116 throw std::invalid_argument("Error parsing XML file");
117 }
118
119 items[itemId] = atoi(reinterpret_cast<char*>(key));
120 xmlFree(key);
121 }
122
123 objects.emplace_back(
124 std::move(entType),
125 entX,
126 entY,
127 entIndex,
128 std::move(items));
129 } else if (!xmlStrcmp(
130 mapNode->name,
91 reinterpret_cast<const xmlChar*>("adjacent"))) 131 reinterpret_cast<const xmlChar*>("adjacent")))
92 { 132 {
93 key = getProp(mapNode, "type"); 133 key = getProp(mapNode, "type");
@@ -147,7 +187,8 @@ World::World(std::string filename)
147 leftAdj, 187 leftAdj,
148 rightAdj, 188 rightAdj,
149 upAdj, 189 upAdj,
150 downAdj)); 190 downAdj,
191 std::move(objects)));
151 } 192 }
152 } 193 }
153 194