~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/xml_plf.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Goulais
  • Date: 2004-08-09 10:26:00 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040809102600-lg2q9lfars0q1p42
Tags: 0.6.0-8
Applied patch from Andreas Jochens (Closes: #263992)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: xml_plf.cxx,v 1.37 2003/04/05 23:24:32 grumbel Exp $
 
2
// 
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
 
5
//
 
6
//  This program is free software; you can redistribute it and/or
 
7
//  modify it under the terms of the GNU General Public License
 
8
//  as published by the Free Software Foundation; either version 2
 
9
//  of the License, or (at your option) any later version.
 
10
//
 
11
//  This program is distributed in the hope that it will be useful,
 
12
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//  GNU General Public License for more details.
 
15
// 
 
16
//  You should have received a copy of the GNU General Public License
 
17
//  along with this program; if not, write to the Free Software
 
18
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
#include <iostream>
 
21
#include "xml_helper.hxx"
 
22
#include "xml_file_reader.hxx"
 
23
#include "xml_plf.hxx"
 
24
#include "globals.hxx"
 
25
#include "system.hxx"
 
26
#include "pingus_error.hxx"
 
27
#include "worldobj_data_factory.hxx"
 
28
#include "worldobjsdata/entrance_data.hxx"
 
29
#include "worldobjsdata/exit_data.hxx"
 
30
#include "worldobjsdata/hotspot_data.hxx"
 
31
#include "worldobjsdata/liquid_data.hxx"
 
32
 
 
33
using Actions::action_from_string;
 
34
 
 
35
XMLPLF::XMLPLF (const std::string& arg_filename)
 
36
{
 
37
  filename = arg_filename;
 
38
  std::string str = System::checksum (filename);
 
39
 
 
40
  doc = xmlParseFile(filename.c_str());
 
41
 
 
42
  if (doc == NULL)
 
43
    PingusError::raise("XMLPLF: Couldn't open \"" + filename + "\"");
 
44
 
 
45
  resname = System::basename(System::dirname(filename))
 
46
    + "/" + System::basename(filename.substr(0, filename.length()-4));
 
47
 
 
48
  parse_file();
 
49
}
 
50
 
 
51
XMLPLF::~XMLPLF()
 
52
{
 
53
  xmlFreeDoc(doc);
 
54
  /*
 
55
  // Free all the allocated memory
 
56
  for(vector<BackgroundData*>::iterator i = backgrounds.begin ();
 
57
  i != backgrounds.end ();
 
58
  i++)
 
59
  delete *i;
 
60
 
 
61
  for(vector<WorldObjData*>::iterator i = worldobjs_data.begin ();
 
62
  i != worldobjs_data.end ();
 
63
  i++)
 
64
  delete *i;
 
65
  */
 
66
}
 
67
 
 
68
void
 
69
XMLPLF::parse_file()
 
70
{
 
71
  //std::cout << "parsing file" << std::endl;
 
72
  xmlNodePtr cur = doc->ROOT;
 
73
 
 
74
  cur = XMLhelper::skip_blank(cur);
 
75
 
 
76
  if (cur && XMLhelper::equal_str(cur->name, "pingus-level"))
 
77
    {
 
78
      //std::cout << "parse_file...." << std::endl;
 
79
 
 
80
      cur = XMLhelper::skip_blank(cur);
 
81
 
 
82
      if (!cur->children)
 
83
        std::cout << "XMLPLF: node: " << cur->name << std::endl;
 
84
      
 
85
      cur = cur->children;
 
86
      cur = XMLhelper::skip_blank(cur);
 
87
      
 
88
      while (cur)
 
89
        {
 
90
          if (xmlIsBlankNode(cur)) 
 
91
            {
 
92
              cur = cur->next;
 
93
              continue;
 
94
            }
 
95
 
 
96
          if (XMLhelper::equal_str(cur->name, "version"))
 
97
            {
 
98
              // FIXME: ignoring version at the moment
 
99
            }
 
100
          // FIXME: This can mostly be unified with the
 
101
          // WorldObjDataFactory, exit, backgrounds, etc. are all
 
102
          // WorldObjs
 
103
          else if (XMLhelper::equal_str(cur->name, "global"))
 
104
            {
 
105
              parse_global(cur);
 
106
            }
 
107
          else if (XMLhelper::equal_str(cur->name, "action-list"))
 
108
            {
 
109
              parse_actions(cur);
 
110
            }
 
111
          else if (XMLhelper::equal_str(cur->name, "background"))
 
112
            {
 
113
              parse_background(cur);
 
114
            }
 
115
          else if (XMLhelper::equal_str(cur->name, "groundpiece"))
 
116
            {
 
117
#ifndef OLD_GROUNDPIECES
 
118
              // FIXME: This is *not* backward compatible and wreck the levels
 
119
              // worldobjs_data.push_back(WorldObjDataFactory::instance()->create (doc, cur));
 
120
 
 
121
              // This probally is backward compatible
 
122
              //groundpieces.push_back(WorldObjsData::GroundpieceData (doc, cur));
 
123
              worldobjs_data.push_back(new WorldObjsData::GroundpieceData (doc, cur));
 
124
#else
 
125
              parse_groundpiece(cur);
 
126
#endif
 
127
            }
 
128
          else if (XMLhelper::equal_str(cur->name, "exit"))
 
129
            {
 
130
              worldobjs_data.push_back (new WorldObjsData::ExitData(doc, cur));
 
131
            }
 
132
          else if (XMLhelper::equal_str(cur->name, "entrance"))
 
133
            {
 
134
              worldobjs_data.push_back (new WorldObjsData::EntranceData(doc, cur));
 
135
            }
 
136
          else if (XMLhelper::equal_str(cur->name, "trap"))
 
137
            {
 
138
              parse_traps(cur);
 
139
            }
 
140
          else if (XMLhelper::equal_str(cur->name, "hotspot"))
 
141
            {
 
142
              worldobjs_data.push_back(new WorldObjsData::HotspotData(doc, cur));
 
143
            }
 
144
          else if (XMLhelper::equal_str(cur->name, "liquid"))
 
145
            {
 
146
              worldobjs_data.push_back(new WorldObjsData::LiquidData(doc, cur));
 
147
            }
 
148
          else if (XMLhelper::equal_str(cur->name, "worldobj"))
 
149
            {
 
150
              worldobjs_data.push_back(WorldObjDataFactory::instance()->create(doc, cur));
 
151
            }
 
152
          else if (XMLhelper::equal_str(cur->name, "prefab"))
 
153
            {
 
154
              worldobjs_data.push_back(WorldObjDataFactory::instance()->create(doc, cur));
 
155
            }
 
156
          else if (XMLhelper::equal_str(cur->name, "group"))
 
157
            {
 
158
              worldobjs_data.push_back(WorldObjDataFactory::instance()->create(doc, cur));
 
159
              //worldobjs_data.push_back (new WorldObjsData::WorldObjGroupData (doc, cur));
 
160
              //parse_group (cur);
 
161
            }
 
162
          else if (XMLhelper::equal_str(cur->name, "start-position"))
 
163
            {
 
164
              parse_start_pos(cur);
 
165
            }
 
166
          else if (XMLhelper::equal_str(cur->name, "weather"))
 
167
            {
 
168
              worldobjs_data.push_back(WorldObjDataFactory::instance()->create (doc, cur));
 
169
            }     
 
170
          else
 
171
            {
 
172
              printf("XMLPLF: Unhandled: %s\n", reinterpret_cast<const char*>(cur->name));
 
173
            }
 
174
          cur = cur->next;
 
175
        }
 
176
      //puts("global done");
 
177
    } else {
 
178
      PingusError::raise("XMLPLF: This is no valid Pingus level");
 
179
    }
 
180
}
 
181
 
 
182
void
 
183
XMLPLF::parse_group (xmlNodePtr cur)
 
184
{
 
185
  cur = cur->children;
 
186
  
 
187
  while (cur)
 
188
    {
 
189
      if (XMLhelper::equal_str(cur->name, "background"))
 
190
        {
 
191
          parse_background(cur);
 
192
        }
 
193
      else if (XMLhelper::equal_str(cur->name, "groundpiece"))
 
194
        {
 
195
          parse_groundpiece(cur);
 
196
        }
 
197
      else if (XMLhelper::equal_str(cur->name, "exit"))
 
198
        {
 
199
          worldobjs_data.push_back(new WorldObjsData::ExitData(doc, cur));
 
200
        }
 
201
      else if (XMLhelper::equal_str(cur->name, "entrance"))
 
202
        {
 
203
          worldobjs_data.push_back(new WorldObjsData::EntranceData(doc, cur));
 
204
        }
 
205
      else if (XMLhelper::equal_str(cur->name, "trap"))
 
206
        {
 
207
          parse_traps(cur);
 
208
        }
 
209
      else if (XMLhelper::equal_str(cur->name, "hotspot"))
 
210
        {
 
211
          worldobjs_data.push_back(new WorldObjsData::HotspotData (doc, cur));
 
212
        }
 
213
      else if (XMLhelper::equal_str(cur->name, "liquid"))
 
214
        {
 
215
          worldobjs_data.push_back(new WorldObjsData::LiquidData (doc, cur));
 
216
        }
 
217
      else if (XMLhelper::equal_str(cur->name, "worldobj"))
 
218
        {
 
219
          worldobjs_data.push_back(WorldObjDataFactory::instance()->create (doc, cur));
 
220
        }
 
221
      else if (XMLhelper::equal_str(cur->name, "group"))
 
222
        {
 
223
          //worldobjs_data.push_back (new WorldObjsData::WorldObjGroupData (doc, cur));
 
224
          parse_group (cur);
 
225
        }
 
226
      else if (XMLhelper::equal_str(cur->name, "start-position"))
 
227
        {
 
228
          parse_start_pos(cur);
 
229
        }
 
230
      else if (XMLhelper::equal_str(cur->name, "weather"))
 
231
        {
 
232
          worldobjs_data.push_back(WorldObjDataFactory::instance()->create (doc, cur));
 
233
        }
 
234
      else
 
235
        {
 
236
          printf("XMLPLF: Unhandled parse_group: %s\n", reinterpret_cast<const char*>(cur->name));
 
237
        }
 
238
      cur = cur->next;
 
239
    }
 
240
}
 
241
 
 
242
void
 
243
XMLPLF::parse_start_pos (xmlNodePtr cur)
 
244
{
 
245
  cur = cur->children;
 
246
 
 
247
  while (cur)
 
248
    {
 
249
      if (xmlIsBlankNode(cur)) 
 
250
        {
 
251
          cur = cur->next;
 
252
          continue;
 
253
        }
 
254
      
 
255
      if (XMLhelper::equal_str(cur->name, "position"))
 
256
        {
 
257
          Vector pos = XMLhelper::parse_vector(doc, cur);
 
258
          start_x_pos = static_cast<int>(pos.x);
 
259
          start_y_pos = static_cast<int>(pos.y);
 
260
        }
 
261
      else
 
262
        {
 
263
          std::cout << "XMLPLF::parse_start_pos: " << cur->name << std::endl;
 
264
        }
 
265
      cur = cur->next;
 
266
    }
 
267
}
 
268
 
 
269
void 
 
270
XMLPLF::parse_background (xmlNodePtr cur)
 
271
{
 
272
  // The allocated objects are delete'd in the destructor
 
273
  //FIXME: Repair me backgrounds.push_back(BackgroundData::create (doc, cur));
 
274
 
 
275
  std::string type;
 
276
  if (XMLhelper::get_prop(cur, "type", type))
 
277
    {
 
278
      worldobjs_data.push_back(WorldObjDataFactory::instance()
 
279
                               ->create (type + "-background", doc, cur));
 
280
    }
 
281
  else
 
282
    {
 
283
      worldobjs_data.push_back(WorldObjDataFactory::instance()
 
284
                               ->create ("surface-background", doc, cur));
 
285
    }
 
286
}
 
287
 
 
288
void 
 
289
XMLPLF::parse_actions (xmlNodePtr cur)
 
290
{
 
291
  cur = cur->children;
 
292
 
 
293
  while (cur)
 
294
    {
 
295
      if (xmlIsBlankNode(cur)) 
 
296
        {
 
297
          cur = cur->next;
 
298
          continue;
 
299
        }
 
300
 
 
301
      ActionData button;
 
302
      button.name = action_from_string(reinterpret_cast<const char*>(cur->name));
 
303
 
 
304
      if (!XMLhelper::get_prop(cur, "count", button.number_of))
 
305
        {
 
306
          //std::cout << "XMLPLF::parse_actions (): No 'count' given, fallback to the old format" << std::endl;
 
307
          if (!XMLhelper::node_list_get_string(doc, cur->children, 1, button.number_of))
 
308
            std::cout << "XMLPLF:parse_actions: no action number given" << std::endl;
 
309
        }
 
310
 
 
311
      actions.push_back(button);
 
312
      
 
313
      cur = cur->next;
 
314
    }      
 
315
}
 
316
 
 
317
void
 
318
XMLPLF::parse_global (xmlNodePtr cur)
 
319
{
 
320
  XMLFileReader reader(doc, cur);
 
321
  
 
322
  reader.read_string("author", author);
 
323
  reader.read_int("number-of-pingus", number_of_pingus);
 
324
  reader.read_int("number-to-save", number_to_save);
 
325
  reader.read_int("time", max_time);
 
326
  reader.read_int("difficulty", difficulty);
 
327
  reader.read_bool("playable", playable);
 
328
  reader.read_string("comment", comment);
 
329
  reader.read_string("music", music);
 
330
  reader.read_int("width", width);
 
331
  reader.read_int("height", height);
 
332
 
 
333
  cur = cur->children;
 
334
  while (cur)
 
335
    {
 
336
      if (xmlIsBlankNode(cur)) 
 
337
        {
 
338
          cur = cur->next;
 
339
          continue;
 
340
        }
 
341
 
 
342
      if (XMLhelper::equal_str(cur->name, "levelname"))
 
343
        {
 
344
          std::string name;
 
345
          std::string lang;
 
346
 
 
347
          if (XMLhelper::node_list_get_string(doc, cur->children, 1, name)) {
 
348
            if (XMLhelper::get_prop(cur, "lang", lang))
 
349
              levelname[lang] = name;
 
350
            else
 
351
              levelname[default_language] = name;
 
352
          }
 
353
        }
 
354
      else if (XMLhelper::equal_str(cur->name, "description"))
 
355
        {
 
356
          std::string desc;
 
357
          std::string lang;
 
358
 
 
359
          if (XMLhelper::node_list_get_string(doc, cur->children, 1, desc)) {
 
360
            if (XMLhelper::get_prop(cur, "lang", lang))             
 
361
              description[lang] = desc;
 
362
            else
 
363
              description[default_language] = desc;
 
364
          }
 
365
        }
 
366
      cur = cur->next;
 
367
    }
 
368
}
 
369
 
 
370
void 
 
371
XMLPLF::parse_groundpiece (xmlNodePtr cur)
 
372
{
 
373
  groundpieces.push_back(WorldObjsData::GroundpieceData (doc, cur));
 
374
}
 
375
 
 
376
void
 
377
XMLPLF::parse_traps (xmlNodePtr cur)
 
378
{
 
379
  xmlNodePtr child_cur = XMLhelper::skip_blank(cur->children);
 
380
  
 
381
  if (XMLhelper::equal_str(child_cur->name, "type"))
 
382
    {
 
383
      std::string name;
 
384
      if (XMLhelper::node_list_get_string(doc, child_cur->children, 1, name))
 
385
        {
 
386
          worldobjs_data.push_back(WorldObjDataFactory::instance()->create(name, doc, cur));
 
387
          return;
 
388
        }
 
389
    }
 
390
 
 
391
  std::cout << "XMLPLF::parse_traps: Invalid data structure" << std::endl;
 
392
}
 
393
 
 
394
/* EOF */