summaryrefslogtreecommitdiffstats
path: root/src/Level.cpp
blob: 2867ab96709fa48c6368ee195ec4775a4f60524d (plain)
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
/*
 * Level.cpp
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Level.h"
#include "Texture.h"
#include <libxml/tree.h>
#include <libxml/valid.h>

#include <iostream>

namespace Zoom {

boost::shared_ptr<Level> Level::loadLevel(const std::string &filename) {
  boost::shared_ptr<Level> level;

  xmlDocPtr doc = xmlParseFile(("levels/"+filename).c_str());

  if(doc) {
    if(validateLevel(doc)) {
      xmlNodePtr root = xmlDocGetRootElement(doc);
      if(root && !xmlStrcmp(root->name, (xmlChar*)"level")) {
        level = boost::shared_ptr<Level>(new Level);

        for(xmlNodePtr node = root->children; node != 0; node = node->next) {
          if(node->type != XML_ELEMENT_NODE)
            continue;

          if(!xmlStrcmp(node->name, (xmlChar*)"info")) {
            level->loadLevelInfo(node);
          }
          else if(!xmlStrcmp(node->name, (xmlChar*)"rooms")) {
            level->loadRooms(node);
          }
          else if(!xmlStrcmp(node->name, (xmlChar*)"textures")) {
            level->loadTextures(node);
          }
        }

        for(std::list<Room>::iterator room = level->rooms.begin(); room != level->rooms.end(); ++room) {
          for(std::list<BSPTree::TriangleRecord>::iterator wall = room->walls.begin(); wall != room->walls.end(); ++wall) {
            boost::shared_ptr<WallData> wallData = boost::dynamic_pointer_cast<WallData>(wall->data);

            if(!wallData->texture.empty()) {
              std::map<std::string, unsigned>::iterator texture = level->textures.find(wallData->texture);

              if(texture != level->textures.end())
                wall->triangle.setTexture(texture->second);
            }
          }
        }
      }
    }

    xmlFreeDoc(doc);
  }

  xmlCleanupParser();

  return level;
}

void Level::loadLevelInfo(xmlNodePtr infoNode) {
  for(xmlNodePtr node = infoNode->children; node != 0; node = node->next) {
    if(node->type != XML_ELEMENT_NODE)
      continue;

    if(!xmlStrcmp(node->name, (xmlChar*)"name")) {
      xmlChar *data = xmlNodeGetContent(node);
      name = (char*)data;
      xmlFree(data);
    }
    else if(!xmlStrcmp(node->name, (xmlChar*)"desc")) {
      xmlChar *data = xmlNodeGetContent(node);
      description = (char*)data;
      xmlFree(data);
    }
    else if(!xmlStrcmp(node->name, (xmlChar*)"start")) {
      startPoint = loadVector(node);
    }
  }
}

void Level::loadRooms(xmlNodePtr roomsNode) {
  for(xmlNodePtr node = roomsNode->children; node != 0; node = node->next) {
    if(node->type == XML_ELEMENT_NODE && !xmlStrcmp(node->name, (xmlChar*)"room")) {
      loadRoom(node);
    }
  }
}

void Level::loadRoom(xmlNodePtr roomNode) {
  rooms.push_back(Room());

  xmlChar *data = xmlGetProp(roomNode, (xmlChar*)"id");
  if(data) {
    rooms.back().id = (char*)data;

    xmlFree(data);
  }

  for(xmlNodePtr node = roomNode->children; node != 0; node = node->next) {
    if(node->type == XML_ELEMENT_NODE && !xmlStrcmp(node->name, (xmlChar*)"triangle")) {
      rooms.back().walls.push_back(loadWall(node));
    }
  }
}

void Level::loadTextures(xmlNodePtr texturesNode) {
  for(xmlNodePtr node = texturesNode->children; node != 0; node = node->next) {
    if(node->type == XML_ELEMENT_NODE && !xmlStrcmp(node->name, (xmlChar*)"texture")) {
      std::string id, name;

      xmlChar *data = xmlGetProp(node, (xmlChar*)"id");
      if(data) {
        id = (char*)data;
        xmlFree(data);
      }

      data = xmlGetProp(node, (xmlChar*)"name");
      if(data) {
        name = (char*)data;
        xmlFree(data);
      }

      if(!id.empty() && !name.empty()) {
        unsigned texture = Texture::loadTexture(name);

        if(texture) {
          textures.insert(std::make_pair(id, texture));
        }
      }
    }
  }
}

BSPTree::TriangleRecord Level::loadWall(xmlNodePtr wallNode) {
  boost::shared_ptr<WallData> wallData(new WallData);

  BSPTree::TriangleRecord wall;
  wall.data = wallData;


  xmlChar *data = xmlGetProp(wallNode, (xmlChar*)"texture");
  if(data) {
    wallData->texture = (char*)data;
    xmlFree(data);
  }

  int vertexNum = -1;

  for(xmlNodePtr node = wallNode->children; node != 0; node = node->next) {
    if(node->type != XML_ELEMENT_NODE)
      continue;

    if(!xmlStrcmp(node->name, (xmlChar*)"vertex")) {
      if(++vertexNum > 2)
        break;

      wall.triangle.setVertex(vertexNum, loadVector(node));
    }
    else if(!xmlStrcmp(node->name, (xmlChar*)"normal")) {
      if(vertexNum < 0)
        continue;

      wall.triangle.setNormal(vertexNum, loadVector(node));
    }
    else if(!xmlStrcmp(node->name, (xmlChar*)"texcoords")) {
      if(vertexNum < 0) continue;

      data = xmlGetProp(node, (xmlChar*)"s");
      if(data) {
        wall.triangle.getTexCoords(vertexNum)[0] = atof((char*)data);
        xmlFree(data);
      }

      data = xmlGetProp(node, (xmlChar*)"t");
      if(data) {
        wall.triangle.getTexCoords(vertexNum)[1] = atof((char*)data);
        xmlFree(data);
      }
    }
  }

  vmml::vec3f normal = wall.triangle.computeNormal();

  if(normal.squared_length() > 0) {
    normal.normalize();

    for(int i = 0; i < 3; ++i) {
      if(wall.triangle.getNormal(i).squared_length() == 0)
        wall.triangle.setNormal(i, normal);
    }
  }


  return wall;
}

vmml::vec3f Level::loadVector(xmlNodePtr node) {
  vmml::vec3f ret(vmml::vec3f::ZERO);

  xmlChar *data = xmlGetProp(node, (xmlChar*)"x");
  if(data) {
    ret.x() = atof((char*)data);
    xmlFree(data);
  }

  data = xmlGetProp(node, (xmlChar*)"y");
  if(data) {
    ret.y() = atof((char*)data);
    xmlFree(data);
  }

  data = xmlGetProp(node, (xmlChar*)"z");
  if(data) {
    ret.z() = atof((char*)data);
    xmlFree(data);
  }

  return ret;
}

bool Level::validateLevel(xmlDocPtr doc) {
  bool ret = false;

  xmlDtdPtr dtd = xmlParseDTD((xmlChar*)"-//libzoom//DTD level 0.1//EN", (xmlChar*)"levels/level.dtd");

  if(dtd) {
    xmlValidCtxtPtr validCtxt = xmlNewValidCtxt();

    if(validCtxt) {
      if(xmlValidateDtd(validCtxt, doc, dtd))
        ret = true;

      xmlFreeValidCtxt(validCtxt);
    }

    xmlFreeDtd(dtd);
  }

  return ret;
}

}