summary refs log tree commit diff stats
path: root/tools/mapedit/src/world.cpp
blob: 79f5a584530a69051581cd59a79a89b967d824d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include "world.h"
#include <libxml/parser.h>
#include <libxml/xmlwriter.h>
#include <sstream>
#include "frame.h"
#include "map.h"
#include "object.h"
#include "consts.h"

World::World()
{
  newMap();
  
  rootChildren.push_back(0);
  empty = true;
}

World::World(std::string filename)
{
  this->filename = filename;
  
  xmlDocPtr doc = xmlParseFile(filename.c_str());
  if (doc == nullptr)
  {
    throw MapLoadException("file not found");
  }
  
  xmlNodePtr top = xmlDocGetRootElement(doc);
  if (top == nullptr)
  {
    throw MapLoadException("no root element");
  }
  
  if (xmlStrcmp(top->name, (const xmlChar*) "world"))
  {
    throw MapLoadException("no world element");
  }
  
  xmlChar* nextmapKey = xmlGetProp(top, (xmlChar*) "nextmap");
  if (nextmapKey != 0)
  {
    nextMapID = atoi((char*) nextmapKey);
  }
  xmlFree(nextmapKey);
  
  xmlChar* lastmapKey = xmlGetProp(top, (xmlChar*) "lastmap");
  if (lastmapKey != 0)
  {
    lastmap = atoi((char*) lastmapKey);
  }
  xmlFree(lastmapKey);
  
  xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx");
  if (startxKey == 0) throw MapLoadException("world missing startx attribute");
  startingPosition.first = atoi((char*) startxKey);
  xmlFree(startxKey);
  
  xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty");
  if (startyKey == 0) throw MapLoadException("world missing starty attribute");
  startingPosition.second = atoi((char*) startyKey);
  xmlFree(startyKey);
  
  xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap");
  if (startxKey == 0) throw MapLoadException("world missing startmap attribute");
  startingMap = atoi((char*) startmapKey);
  xmlFree(startmapKey);
  
  for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next)
  {
    if (!xmlStrcmp(node->name, (const xmlChar*) "root"))
    {
      xmlChar* key = xmlNodeGetContent(node);
      if (key == 0) throw MapLoadException("root missing content");
      rootChildren.push_back(atoi((char*) key));
      xmlFree(key);
    } else if (!xmlStrcmp(node->name, (const xmlChar*) "map"))
    {
      xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id");
      if (idKey == 0) throw MapLoadException("map missing id attribute");
      int id = atoi((char*) idKey);
      xmlFree(idKey);
      
      auto map = std::make_shared<Map>(id, this);
      
      xmlChar* expandKey = xmlGetProp(node, (xmlChar*) "expanded");
      if ((expandKey != 0) && (!xmlStrcmp(expandKey, (const xmlChar*) "true")))
      {
        map->setExpanded(true);
      }
      xmlFree(expandKey);
      
      xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title");
      if (titleKey == 0) throw MapLoadException("map missing title attribute");
      map->setTitle((char*) titleKey, false);
      xmlFree(titleKey);
      
      for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next)
      {
        if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment"))
        {
          xmlChar* key = xmlNodeGetContent(mapNode);
          if (key == 0) throw MapLoadException("map missing environment content");
          int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
          mapdata[0] = atoi(strtok((char*) key, ",\n"));
          for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++)
          {
            mapdata[i] = atoi(strtok(NULL, ",\n"));
          }
          map->setMapdata(mapdata, false);
          xmlFree(key);
        } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entity"))
        {
          xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type");
          if (typeKey == 0) throw MapLoadException("entity missing type attribute");
          const MapObject& obj = MapObject::getAllObjects().at((char*) typeKey);
          xmlFree(typeKey);
          
          xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x");
          if (xKey == 0) throw MapLoadException("entity missing x attribute");
          int xpos = atoi((char*) xKey);
          xmlFree(xKey);
          
          xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y");
          if (yKey == 0) throw MapLoadException("entity missing y attribute");
          int ypos = atoi((char*) yKey);
          xmlFree(yKey);
          
          auto data = std::make_shared<MapObjectEntry>(obj, xpos, ypos);
          
          map->addObject(data, false);
          
          for (xmlNodePtr objectNode = mapNode->xmlChildrenNode; objectNode != NULL; objectNode = objectNode->next)
          {
            if (!xmlStrcmp(objectNode->name, (const xmlChar*) "item"))
            {
              xmlChar* key = xmlGetProp(objectNode, (const xmlChar*) "id");
              if (key == 0) throw MapLoadException("item missing id attribute");
              std::string itemID = (char*) key;
              xmlFree(key);
              
              MapObjectEntry::Item item;
              item.type = data->getObject().getInput(itemID).type;
              
              key = xmlNodeGetContent(objectNode);
              if (key == 0) throw MapLoadException("item missing content");
              switch (item.type)
              {
                case MapObject::Input::Type::Choice:
                case MapObject::Input::Type::Slider:
                {
                  item.intvalue = atoi((char*) key);
                  break;
                }
              }
              
              data->addItem(itemID, item);
            }
          }
        } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent"))
        {
          Map::MoveDir direction;
          Map::MoveType moveType;
          int mapId = 0;
          
          xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir");
          if (dirKey == 0) throw MapLoadException("adjacent missing dir attribute");
          direction = Map::moveDirForShort((char*) dirKey);
          xmlFree(dirKey);
          
          xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type");
          if (typeKey == 0) throw MapLoadException("adjacent missing type attribute");
          moveType = Map::moveTypeForShort((char*) typeKey);
          xmlFree(typeKey);
          
          xmlChar* mapIdKey = xmlGetProp(mapNode, (const xmlChar*) "map");
          if (mapIdKey != 0)
          {
            mapId = atoi((char*) mapIdKey);
          }
          xmlFree(mapIdKey);
          
          map->setAdjacent(direction, moveType, mapId, false);
        } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "child"))
        {
          xmlChar* key = xmlNodeGetContent(mapNode);
          if (key != 0)
          {
            map->addChild(atoi((char*) key));
          }
          xmlFree(key);
        }
      }
      
      maps[map->getID()] = map;
    }
  }
  
  xmlFreeDoc(doc);
}

std::shared_ptr<Map> World::newMap()
{
  auto nm = std::make_shared<Map>(nextMapID++, this);
  maps[nm->getID()] = nm;
  return nm;
}

std::shared_ptr<Map> World::getMap(int id) const
{
  return maps.at(id);
}

void World::setDirty(bool dirty)
{
  if (dirty) empty = false;
  this->dirty = dirty;
  parent->MapDirtyDidChange(dirty);
}

bool World::getDirty() const
{
  return dirty;
}

std::string World::getFilename() const
{
  return filename;
}

void World::setParent(MapeditFrame* parent)
{
  this->parent = parent;
}

Map* World::getLastMap() const
{
  return getMap(lastmap).get();
}

#define MY_ENCODING "ISO-8859-1"

void World::save(std::string name, wxTreeCtrl* mapTree)
{
  int rc;
  
  xmlTextWriterPtr writer = xmlNewTextWriterFilename(name.c_str(), 0);
  if (writer == NULL) throw MapWriteException(name);

  rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
  if (rc < 0) throw MapWriteException(name);
  
  // <world>
  rc = xmlTextWriterStartElement(writer, (xmlChar*) "world");
  if (rc < 0) throw MapWriteException(name);
  
  //   nextmap=
  rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "nextmap", "%d", nextMapID);
  if (rc < 0) throw MapWriteException(name);
  
  //   lastmap=
  rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "lastmap", "%d", lastmap);
  if (rc < 0) throw MapWriteException(name);
  
  //   startx=
  rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "startx", "%d", startingPosition.first);
  if (rc < 0) throw MapWriteException(name);
  
  //   starty=
  rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "starty", "%d", startingPosition.second);
  if (rc < 0) throw MapWriteException(name);
  
  //   startmap=
  rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "startmap", "%d", startingMap);
  if (rc < 0) throw MapWriteException(name);
  
  // ASSUMPTION: There will always be at least one child of the invisible root element. i.e. you cannot delete to zero maps.
  wxTreeItemId root = mapTree->GetRootItem();
  wxTreeItemIdValue cookie1;
  for (wxTreeItemId it = mapTree->GetFirstChild(root, cookie1); it.IsOk(); it = mapTree->GetNextChild(root, cookie1))
  {
    // <root>
    MapPtrCtr* ctl = (MapPtrCtr*) mapTree->GetItemData(it);
    rc = xmlTextWriterWriteFormatElement(writer, (xmlChar*) "root", "%d", ctl->map->getID());
    if (rc < 0) throw MapWriteException(name);
  }
  
  for (auto mapPair : maps)
  {
    Map& map = *mapPair.second;
    
    if (map.getHidden()) continue;
    
    // <map>
    rc = xmlTextWriterStartElement(writer, (xmlChar*) "map");
    if (rc < 0) throw MapWriteException(name);
    
    // id=
    rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "id", "%d", map.getID());
    if (rc < 0) throw MapWriteException(name);
    
    // expanded=
    wxTreeItemId node = map.getTreeItemId();
    if (mapTree->IsExpanded(node))
    {
      rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "expanded", (xmlChar*) "true");
      if (rc < 0) throw MapWriteException(name);
    } else {
      rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "expanded", (xmlChar*) "false");
      if (rc < 0) throw MapWriteException(name);
    }
    
    // title=
    rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "title", (xmlChar*) map.getTitle().c_str());
    if (rc < 0) throw MapWriteException(name);
  
    //   <environment
    rc = xmlTextWriterStartElement(writer, (xmlChar*) "environment");
    if (rc < 0) throw MapWriteException(name);
    
    //   type=
    rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "type", (xmlChar*) "0");
    if (rc < 0) throw MapWriteException(name);
    
    //   >
    std::ostringstream mapdata_out;
    for (int y=0; y<MAP_HEIGHT; y++)
    {
      for (int x=0; x<MAP_WIDTH; x++)
      {
        mapdata_out << map.getTileAt(x,y) << ",";
      }
    
      mapdata_out << std::endl;
    }
  
    rc = xmlTextWriterWriteString(writer, (xmlChar*) mapdata_out.str().c_str());
    if (rc < 0) throw MapWriteException(name);
    
    //   </environment>
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) throw MapWriteException(name);
    
    for (auto object : map.getObjects())
    {
      // <entity>
      rc = xmlTextWriterStartElement(writer, (xmlChar*) "entity");
      if (rc < 0) throw MapWriteException(name);
    
      // type=
      rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "type", (xmlChar*) object->getObject().getID().c_str());
      if (rc < 0) throw MapWriteException(name);
    
      // x=
      rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "x", "%d", object->getPosition().first);
      if (rc < 0) throw MapWriteException(name);
      
      // y=
      rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "y", "%d", object->getPosition().second);
      if (rc < 0) throw MapWriteException(name);
      
      for (auto item : object->getItems())
      {
        // <item
        rc = xmlTextWriterStartElement(writer, (xmlChar*) "item");
        if (rc < 0) throw MapWriteException(name);
        
        // id=
        rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "id", (xmlChar*) item.first.c_str());
        if (rc < 0) throw MapWriteException(name);
        
        // >
        switch (item.second.type)
        {
          case MapObject::Input::Type::Slider:
          case MapObject::Input::Type::Choice:
          {
            rc = xmlTextWriterWriteFormatString(writer, "%d", item.second.intvalue);
            break;
          }
        }
        
        // </item>
        rc = xmlTextWriterEndElement(writer);
        if (rc < 0) throw MapWriteException(name);
      }
    
      // </entity>
      rc = xmlTextWriterEndElement(writer);
      if (rc < 0) throw MapWriteException(name);
    }
    
    for (auto adjacent : map.getAdjacents())
    {
      // <adjacent>
      rc = xmlTextWriterStartElement(writer, (xmlChar*) "adjacent");
      if (rc < 0) throw MapWriteException(name);
      
      // dir=
      rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "dir", (xmlChar*) Map::shortForMoveDir(adjacent.first).c_str());
      if (rc < 0) throw MapWriteException(name);
      
      // type=
      rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "type", (xmlChar*) Map::shortForMoveType(adjacent.second.type).c_str());
      if (rc < 0) throw MapWriteException(name);
      
      // map=
      if (Map::moveTypeTakesMap(adjacent.second.type))
      {
        rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "map", "%d", adjacent.second.map);
        if (rc < 0) throw MapWriteException(name);
      }
      
      // </adjacent>
      rc = xmlTextWriterEndElement(writer);
      if (rc < 0) throw MapWriteException(name);
    }
    
    if (mapTree->ItemHasChildren(node))
    {
      wxTreeItemIdValue cookie2;
      for (wxTreeItemId it = mapTree->GetFirstChild(node, cookie2); it.IsOk(); it = mapTree->GetNextChild(node, cookie2))
      {
      // <child/>
        MapPtrCtr* ctl = (MapPtrCtr*) mapTree->GetItemData(it);
        rc = xmlTextWriterWriteFormatElement(writer, (xmlChar*) "child", "%d", ctl->map->getID());
        if (rc < 0) throw MapWriteException(name);
      }
    }
    
    // </map>
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) throw MapWriteException(name);
  }
  
  // </world>
  rc = xmlTextWriterEndDocument(writer);
  if (rc < 0) throw MapWriteException(name);
  
  xmlFreeTextWriter(writer);
  
  setDirty(false);
}

std::list<std::shared_ptr<Map>> World::getRootMaps() const
{
  std::list<std::shared_ptr<Map>> ret;
  
  for (auto id : rootChildren)
  {
    ret.push_back(getMap(id));
  }
  
  return ret;
}

const std::map<int, std::shared_ptr<Map>> World::getMaps() const
{
  return maps;
}

void World::setLastMap(Map* map)
{
  lastmap = map->getID();
}

bool World::getEmpty() const
{
  return empty;
}

Map* World::getStartingMap() const
{
  return getMap(startingMap).get();
}

std::pair<double, double> World::getStartingPosition() const
{
  return startingPosition;
}

void World::setStart(Map* map, std::pair<double, double> startPos)
{
  startingMap = map->getID();
  startingPosition = startPos;
  
  setDirty(true);
}