summary refs log tree commit diff stats
path: root/src/world.cpp
blob: 17288faf59182623c965266517d909185c16eed7 (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
#include "world.h"
#include <libxml/parser.h>
#include "consts.h"

World::World(const char* filename)
{
  xmlDocPtr doc = xmlParseFile(filename);
  if (doc == nullptr)
  {
    exit(2);
  }
  
  xmlNodePtr top = xmlDocGetRootElement(doc);
  if (top == nullptr)
  {
    exit(2);
  }
  
  if (xmlStrcmp(top->name, (const xmlChar*) "world"))
  {
    exit(2);
  }
  
  xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx");
  if (startxKey == 0) exit(2);
  startX = atoi((char*) startxKey);
  xmlFree(startxKey);
  
  xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty");
  if (startyKey == 0) exit(2);
  startY = atoi((char*) startyKey);
  xmlFree(startyKey);
  
  xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap");
  if (startxKey == 0) exit(2);
  startMap = atoi((char*) startmapKey);
  xmlFree(startmapKey);
  
  for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next)
  {
    if (!xmlStrcmp(node->name, (const xmlChar*) "map"))
    {
      xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id");
      if (idKey == 0) exit(2);
      int theId = atoi((char*) idKey);
      xmlFree(idKey);
      
      maps.emplace(theId, theId);
      Map& map = maps[theId];
      
      xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title");
      if (titleKey == 0) exit(2);
      map.setTitle((char*) titleKey);
      xmlFree(titleKey);
      
      for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next)
      {
        if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment"))
        {
          xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1);
          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);
          xmlFree(key);
        } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entity"))
        {
          Map::EntityData data;
          
          xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type");
          if (typeKey == 0) exit(2);
          data.name = (char*) typeKey;
          xmlFree(typeKey);
          
          xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x");
          if (xKey == 0) exit(2);
          data.position.first = atoi((char*) xKey);
          xmlFree(xKey);
          
          xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y");
          if (yKey == 0) exit(2);
          data.position.second = atoi((char*) yKey);
          xmlFree(yKey);
          
          map.addEntity(data);
        } 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) exit(2);
          direction = Map::moveDirForShort((char*) dirKey);
          xmlFree(dirKey);
          
          xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type");
          if (typeKey == 0) exit(2);
          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);
        }
      }
    }
  }
  
  xmlFreeDoc(doc);
}

const Map& World::getMap(int id) const
{
  return maps.at(id);
}

const Map& World::getStartingMap() const
{
  return maps.at(startMap);
}

std::pair<int, int> World::getStartingPosition() const
{
  return std::make_pair(startX, startY);
}