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

inline xmlChar* getProp(xmlNodePtr node, const char* attr)
{
  xmlChar* key = xmlGetProp(node, reinterpret_cast<const xmlChar*>(attr));
  if (key == nullptr)
  {
    throw std::invalid_argument("Error parsing world file");
  }

  return key;
}

World::World(std::string filename)
{
  xmlDocPtr doc = xmlParseFile(filename.c_str());
  if (doc == nullptr)
  {
    throw std::invalid_argument("Cannot find world file");
  }

  xmlNodePtr top = xmlDocGetRootElement(doc);
  if (top == nullptr)
  {
    throw std::invalid_argument("Error parsing world file");
  }

  if (xmlStrcmp(top->name, reinterpret_cast<const xmlChar*>("world")))
  {
    throw std::invalid_argument("Error parsing world file");
  }

  xmlChar* key = nullptr;

  key = getProp(top, "startx");
  startX_ = atoi(reinterpret_cast<char*>(key));
  xmlFree(key);

  key = getProp(top, "starty");
  startY_ = atoi(reinterpret_cast<char*>(key));
  xmlFree(key);

  key = getProp(top, "startmap");
  startMap_ = atoi(reinterpret_cast<char*>(key));
  xmlFree(key);

  for (xmlNodePtr node = top->xmlChildrenNode;
    node != nullptr;
    node = node->next)
  {
    if (!xmlStrcmp(node->name, reinterpret_cast<const xmlChar*>("map")))
    {
      key = getProp(node, "id");
      size_t mapId = atoi(reinterpret_cast<char*>(key));
      xmlFree(key);

      key = getProp(node, "title");
      std::string mapTitle(reinterpret_cast<char*>(key));
      xmlFree(key);

      std::vector<int> mapTiles;
      Map::Adjacent leftAdj;
      Map::Adjacent rightAdj;
      Map::Adjacent upAdj;
      Map::Adjacent downAdj;

      for (xmlNodePtr mapNode = node->xmlChildrenNode;
        mapNode != nullptr;
        mapNode = mapNode->next)
      {
        if (!xmlStrcmp(
          mapNode->name,
          reinterpret_cast<const xmlChar*>("environment")))
        {
          key = xmlNodeGetContent(mapNode);

          mapTiles.clear();
          mapTiles.push_back(atoi(strtok(reinterpret_cast<char*>(key), ",\n")));
          for (size_t i = 1; i < (MAP_WIDTH * MAP_HEIGHT); i++)
          {
            mapTiles.push_back(atoi(strtok(nullptr, ",\n")));
          }

          xmlFree(key);
        } else if (!xmlStrcmp(
          mapNode->name,
          reinterpret_cast<const xmlChar*>("adjacent")))
        {
          key = getProp(mapNode, "type");
          std::string adjTypeStr(reinterpret_cast<char*>(key));
          xmlFree(key);

          Map::Adjacent::Type adjType;
          if (adjTypeStr == "wall")
          {
            adjType = Map::Adjacent::Type::wall;
          } else if (adjTypeStr == "wrap")
          {
            adjType = Map::Adjacent::Type::wrap;
          } else if (adjTypeStr == "warp")
          {
            adjType = Map::Adjacent::Type::warp;
          } else if (adjTypeStr == "reverseWarp")
          {
            adjType = Map::Adjacent::Type::reverse;
          } else {
            throw std::logic_error("Invalid adjacency type");
          }

          key = getProp(mapNode, "map");
          int adjMapId = atoi(reinterpret_cast<char*>(key));
          xmlFree(key);

          key = getProp(mapNode, "dir");
          std::string adjDir(reinterpret_cast<char*>(key));
          xmlFree(key);

          if (adjDir == "left")
          {
            leftAdj = {adjType, adjMapId};
          } else if (adjDir == "right")
          {
            rightAdj = {adjType, adjMapId};
          } else if (adjDir == "up")
          {
            upAdj = {adjType, adjMapId};
          } else if (adjDir == "down")
          {
            downAdj = {adjType, adjMapId};
          } else {
            throw std::logic_error("Invalid adjacency direction");
          }
        }
      }

      maps_.emplace(
        std::piecewise_construct,
        std::forward_as_tuple(mapId),
        std::forward_as_tuple(
          mapId,
          std::move(mapTiles),
          std::move(mapTitle),
          leftAdj,
          rightAdj,
          upAdj,
          downAdj));
    }
  }

  xmlFreeDoc(doc);
}