summary refs log tree commit diff stats
path: root/tools/mapedit/src/world.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/mapedit/src/world.cpp')
-rw-r--r--tools/mapedit/src/world.cpp109
1 files changed, 82 insertions, 27 deletions
diff --git a/tools/mapedit/src/world.cpp b/tools/mapedit/src/world.cpp index 01225cf..79f5a58 100644 --- a/tools/mapedit/src/world.cpp +++ b/tools/mapedit/src/world.cpp
@@ -22,18 +22,18 @@ World::World(std::string filename)
22 xmlDocPtr doc = xmlParseFile(filename.c_str()); 22 xmlDocPtr doc = xmlParseFile(filename.c_str());
23 if (doc == nullptr) 23 if (doc == nullptr)
24 { 24 {
25 throw MapLoadException(filename); 25 throw MapLoadException("file not found");
26 } 26 }
27 27
28 xmlNodePtr top = xmlDocGetRootElement(doc); 28 xmlNodePtr top = xmlDocGetRootElement(doc);
29 if (top == nullptr) 29 if (top == nullptr)
30 { 30 {
31 throw MapLoadException(filename); 31 throw MapLoadException("no root element");
32 } 32 }
33 33
34 if (xmlStrcmp(top->name, (const xmlChar*) "world")) 34 if (xmlStrcmp(top->name, (const xmlChar*) "world"))
35 { 35 {
36 throw MapLoadException(filename); 36 throw MapLoadException("no world element");
37 } 37 }
38 38
39 xmlChar* nextmapKey = xmlGetProp(top, (xmlChar*) "nextmap"); 39 xmlChar* nextmapKey = xmlGetProp(top, (xmlChar*) "nextmap");
@@ -51,17 +51,17 @@ World::World(std::string filename)
51 xmlFree(lastmapKey); 51 xmlFree(lastmapKey);
52 52
53 xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx"); 53 xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx");
54 if (startxKey == 0) throw MapLoadException(filename); 54 if (startxKey == 0) throw MapLoadException("world missing startx attribute");
55 startingPosition.first = atoi((char*) startxKey); 55 startingPosition.first = atoi((char*) startxKey);
56 xmlFree(startxKey); 56 xmlFree(startxKey);
57 57
58 xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty"); 58 xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty");
59 if (startyKey == 0) throw MapLoadException(filename); 59 if (startyKey == 0) throw MapLoadException("world missing starty attribute");
60 startingPosition.second = atoi((char*) startyKey); 60 startingPosition.second = atoi((char*) startyKey);
61 xmlFree(startyKey); 61 xmlFree(startyKey);
62 62
63 xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap"); 63 xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap");
64 if (startxKey == 0) throw MapLoadException(filename); 64 if (startxKey == 0) throw MapLoadException("world missing startmap attribute");
65 startingMap = atoi((char*) startmapKey); 65 startingMap = atoi((char*) startmapKey);
66 xmlFree(startmapKey); 66 xmlFree(startmapKey);
67 67
@@ -69,14 +69,14 @@ World::World(std::string filename)
69 { 69 {
70 if (!xmlStrcmp(node->name, (const xmlChar*) "root")) 70 if (!xmlStrcmp(node->name, (const xmlChar*) "root"))
71 { 71 {
72 xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); 72 xmlChar* key = xmlNodeGetContent(node);
73 if (key == 0) throw MapLoadException(filename); 73 if (key == 0) throw MapLoadException("root missing content");
74 rootChildren.push_back(atoi((char*) key)); 74 rootChildren.push_back(atoi((char*) key));
75 xmlFree(key); 75 xmlFree(key);
76 } else if (!xmlStrcmp(node->name, (const xmlChar*) "map")) 76 } else if (!xmlStrcmp(node->name, (const xmlChar*) "map"))
77 { 77 {
78 xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); 78 xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id");
79 if (idKey == 0) throw MapLoadException(filename); 79 if (idKey == 0) throw MapLoadException("map missing id attribute");
80 int id = atoi((char*) idKey); 80 int id = atoi((char*) idKey);
81 xmlFree(idKey); 81 xmlFree(idKey);
82 82
@@ -90,7 +90,7 @@ World::World(std::string filename)
90 xmlFree(expandKey); 90 xmlFree(expandKey);
91 91
92 xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title"); 92 xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title");
93 if (titleKey == 0) throw MapLoadException(filename); 93 if (titleKey == 0) throw MapLoadException("map missing title attribute");
94 map->setTitle((char*) titleKey, false); 94 map->setTitle((char*) titleKey, false);
95 xmlFree(titleKey); 95 xmlFree(titleKey);
96 96
@@ -98,7 +98,8 @@ World::World(std::string filename)
98 { 98 {
99 if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) 99 if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment"))
100 { 100 {
101 xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); 101 xmlChar* key = xmlNodeGetContent(mapNode);
102 if (key == 0) throw MapLoadException("map missing environment content");
102 int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); 103 int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
103 mapdata[0] = atoi(strtok((char*) key, ",\n")); 104 mapdata[0] = atoi(strtok((char*) key, ",\n"));
104 for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) 105 for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++)
@@ -109,24 +110,52 @@ World::World(std::string filename)
109 xmlFree(key); 110 xmlFree(key);
110 } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entity")) 111 } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entity"))
111 { 112 {
112 auto data = std::make_shared<MapObjectEntry>();
113
114 xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); 113 xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type");
115 if (typeKey == 0) throw MapLoadException(filename); 114 if (typeKey == 0) throw MapLoadException("entity missing type attribute");
116 data->object = MapObject::getAllObjects().at((char*) typeKey).get(); 115 const MapObject& obj = MapObject::getAllObjects().at((char*) typeKey);
117 xmlFree(typeKey); 116 xmlFree(typeKey);
118 117
119 xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x"); 118 xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x");
120 if (xKey == 0) throw MapLoadException(filename); 119 if (xKey == 0) throw MapLoadException("entity missing x attribute");
121 data->position.first = atoi((char*) xKey); 120 int xpos = atoi((char*) xKey);
122 xmlFree(xKey); 121 xmlFree(xKey);
123 122
124 xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y"); 123 xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y");
125 if (yKey == 0) throw MapLoadException(filename); 124 if (yKey == 0) throw MapLoadException("entity missing y attribute");
126 data->position.second = atoi((char*) yKey); 125 int ypos = atoi((char*) yKey);
127 xmlFree(yKey); 126 xmlFree(yKey);
128 127
128 auto data = std::make_shared<MapObjectEntry>(obj, xpos, ypos);
129
129 map->addObject(data, false); 130 map->addObject(data, false);
131
132 for (xmlNodePtr objectNode = mapNode->xmlChildrenNode; objectNode != NULL; objectNode = objectNode->next)
133 {
134 if (!xmlStrcmp(objectNode->name, (const xmlChar*) "item"))
135 {
136 xmlChar* key = xmlGetProp(objectNode, (const xmlChar*) "id");
137 if (key == 0) throw MapLoadException("item missing id attribute");
138 std::string itemID = (char*) key;
139 xmlFree(key);
140
141 MapObjectEntry::Item item;
142 item.type = data->getObject().getInput(itemID).type;
143
144 key = xmlNodeGetContent(objectNode);
145 if (key == 0) throw MapLoadException("item missing content");
146 switch (item.type)
147 {
148 case MapObject::Input::Type::Choice:
149 case MapObject::Input::Type::Slider:
150 {
151 item.intvalue = atoi((char*) key);
152 break;
153 }
154 }
155
156 data->addItem(itemID, item);
157 }
158 }
130 } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent")) 159 } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent"))
131 { 160 {
132 Map::MoveDir direction; 161 Map::MoveDir direction;
@@ -134,12 +163,12 @@ World::World(std::string filename)
134 int mapId = 0; 163 int mapId = 0;
135 164
136 xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir"); 165 xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir");
137 if (dirKey == 0) throw MapLoadException(filename); 166 if (dirKey == 0) throw MapLoadException("adjacent missing dir attribute");
138 direction = Map::moveDirForShort((char*) dirKey); 167 direction = Map::moveDirForShort((char*) dirKey);
139 xmlFree(dirKey); 168 xmlFree(dirKey);
140 169
141 xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); 170 xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type");
142 if (typeKey == 0) throw MapLoadException(filename); 171 if (typeKey == 0) throw MapLoadException("adjacent missing type attribute");
143 moveType = Map::moveTypeForShort((char*) typeKey); 172 moveType = Map::moveTypeForShort((char*) typeKey);
144 xmlFree(typeKey); 173 xmlFree(typeKey);
145 174
@@ -153,7 +182,7 @@ World::World(std::string filename)
153 map->setAdjacent(direction, moveType, mapId, false); 182 map->setAdjacent(direction, moveType, mapId, false);
154 } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "child")) 183 } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "child"))
155 { 184 {
156 xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); 185 xmlChar* key = xmlNodeGetContent(mapNode);
157 if (key != 0) 186 if (key != 0)
158 { 187 {
159 map->addChild(atoi((char*) key)); 188 map->addChild(atoi((char*) key));
@@ -281,7 +310,7 @@ void World::save(std::string name, wxTreeCtrl* mapTree)
281 } 310 }
282 311
283 // title= 312 // title=
284 rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "name", (xmlChar*) map.getTitle().c_str()); 313 rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "title", (xmlChar*) map.getTitle().c_str());
285 if (rc < 0) throw MapWriteException(name); 314 if (rc < 0) throw MapWriteException(name);
286 315
287 // <environment 316 // <environment
@@ -304,7 +333,7 @@ void World::save(std::string name, wxTreeCtrl* mapTree)
304 mapdata_out << std::endl; 333 mapdata_out << std::endl;
305 } 334 }
306 335
307 rc = xmlTextWriterWriteElement(writer, (xmlChar*) "environment", (xmlChar*) mapdata_out.str().c_str()); 336 rc = xmlTextWriterWriteString(writer, (xmlChar*) mapdata_out.str().c_str());
308 if (rc < 0) throw MapWriteException(name); 337 if (rc < 0) throw MapWriteException(name);
309 338
310 // </environment> 339 // </environment>
@@ -318,16 +347,42 @@ void World::save(std::string name, wxTreeCtrl* mapTree)
318 if (rc < 0) throw MapWriteException(name); 347 if (rc < 0) throw MapWriteException(name);
319 348
320 // type= 349 // type=
321 rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "type", (xmlChar*) object->object->getType().c_str()); 350 rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "type", (xmlChar*) object->getObject().getID().c_str());
322 if (rc < 0) throw MapWriteException(name); 351 if (rc < 0) throw MapWriteException(name);
323 352
324 // x= 353 // x=
325 rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "x", "%d", object->position.first); 354 rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "x", "%d", object->getPosition().first);
326 if (rc < 0) throw MapWriteException(name); 355 if (rc < 0) throw MapWriteException(name);
327 356
328 // y= 357 // y=
329 rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "y", "%d", object->position.second); 358 rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "y", "%d", object->getPosition().second);
330 if (rc < 0) throw MapWriteException(name); 359 if (rc < 0) throw MapWriteException(name);
360
361 for (auto item : object->getItems())
362 {
363 // <item
364 rc = xmlTextWriterStartElement(writer, (xmlChar*) "item");
365 if (rc < 0) throw MapWriteException(name);
366
367 // id=
368 rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "id", (xmlChar*) item.first.c_str());
369 if (rc < 0) throw MapWriteException(name);
370
371 // >
372 switch (item.second.type)
373 {
374 case MapObject::Input::Type::Slider:
375 case MapObject::Input::Type::Choice:
376 {
377 rc = xmlTextWriterWriteFormatString(writer, "%d", item.second.intvalue);
378 break;
379 }
380 }
381
382 // </item>
383 rc = xmlTextWriterEndElement(writer);
384 if (rc < 0) throw MapWriteException(name);
385 }
331 386
332 // </entity> 387 // </entity>
333 rc = xmlTextWriterEndElement(writer); 388 rc = xmlTextWriterEndElement(writer);