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

« back to all changes in this revision

Viewing changes to src/worldmap/PingusWorldMapGraph.cc

  • 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: PingusWorldMapGraph.cc,v 1.26 2002/01/16 23:51:00 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 <ClanLib/core.h>
21
 
#include <ClanLib/display.h>
22
 
 
23
 
#include "../my_gettext.hh"
24
 
#include "../globals.hh"
25
 
#include "../PingusError.hh"
26
 
#include "../PathManager.hh"
27
 
#include "../XMLPLF.hh"
28
 
#include "../Console.hh"
29
 
#include "../XMLhelper.hh"
30
 
#include "../StringConverter.hh"
31
 
#include "../PingusSound.hh"
32
 
#include "../PingusGameSession.hh"
33
 
#include "PingusWorldMapManager.hh"
34
 
#include "PingusWorldMapGraph.hh"
35
 
 
36
 
PingusWorldMapTubeNode::PingusWorldMapTubeNode ()
37
 
  : worldmap_name ("pacman.xml"),
38
 
    tube ("worldmap/tube", "core")
39
 
{
40
 
  tube.set_align (-16, -32);
41
 
}
42
 
 
43
 
void 
44
 
PingusWorldMapTubeNode::on_click ()
45
 
{
46
 
  std::cout << "Not Supported" << std::endl;
47
 
  PingusWorldMapManager::get_current ()->change_map (worldmap_name, link_node);
48
 
}
49
 
 
50
 
void 
51
 
PingusWorldMapTubeNode::draw (CL_Vector offset)
52
 
{
53
 
  tube.put_screen (pos + offset);
54
 
}
55
 
 
56
 
std::map<std::string, std::string>
57
 
PingusWorldMapTubeNode::get_string ()
58
 
{
59
 
  std::map<std::string, std::string> str;
60
 
  str["en"] = "Tube";
61
 
 
62
 
  return str;
63
 
}
64
 
 
65
 
void 
66
 
PingusWorldMapLevelNode::on_click ()
67
 
{
68
 
  if (maintainer_mode) 
69
 
    std::cout << "Start a level...: " << levelname << std::endl;
70
 
                  
71
 
  PingusSound::play_sound(path_manager.complete("sounds/letsgo.wav"));
72
 
  PingusGameSession game (path_manager.complete(levelname));
73
 
      
74
 
  // Launch the game and wait until it is finished
75
 
  game.start ();
76
 
 
77
 
  if (game.get_result ().finished ())
78
 
    {
79
 
      finished = true;  
80
 
    }
81
 
  else
82
 
    {
83
 
      console.puts("Please try again!");
84
 
    }
85
 
}
86
 
 
87
 
void 
88
 
PingusWorldMapLevelNode::mark (bool value)
89
 
{
90
 
  finished = value;
91
 
}
92
 
 
93
 
void 
94
 
PingusWorldMapLevelNode::draw (CL_Vector offset)
95
 
{
96
 
  if (!levelname.empty())
97
 
    {
98
 
      if (accessible) 
99
 
        {
100
 
          green_dot.put_screen (pos + offset);
101
 
          if (finished) {
102
 
            green_flag.put_screen (pos + offset);
103
 
          }
104
 
        }
105
 
      else
106
 
        {
107
 
          red_dot.put_screen (pos + offset);
108
 
        }
109
 
    }
110
 
}
111
 
 
112
 
std::map<std::string, std::string>
113
 
PingusWorldMapLevelNode::get_string ()
114
 
{
115
 
  return get_plf ()->get_levelname ();
116
 
}
117
 
 
118
 
boost::shared_ptr<PLF>
119
 
PingusWorldMapLevelNode::get_plf ()
120
 
{
121
 
  if (plf.get () == 0) 
122
 
    {
123
 
      //console << "Loading " << levelname << std::endl;
124
 
      try {
125
 
        plf = boost::shared_ptr<PLF> (new XMLPLF (path_manager.complete (levelname)));
126
 
      } catch (PingusError e) {
127
 
        std::cout << "PingusWorldMapGraph: Caught PingusError (" << e.get_message () << ")" << std::endl;
128
 
        std::cout << "PingusWorldMapGraph: Failed to load '" << levelname << "', fallback to level1.xml" << std::endl;
129
 
        plf = boost::shared_ptr<PLF> (new XMLPLF (path_manager.complete ("levels/level1.xml")));
130
 
      }
131
 
    }
132
 
  
133
 
  return plf;
134
 
}
135
 
 
136
 
PingusWorldMapGraph::PingusWorldMapGraph ()
137
 
{
138
 
  music = "pingus-1.it";
139
 
  //graph = 0;
140
 
}
141
 
 
142
 
PingusWorldMapGraph::~PingusWorldMapGraph ()
143
 
{
144
 
  //llif (graph) delete graph;
145
 
}
146
 
  
147
 
void
148
 
PingusWorldMapGraph::parse_file (std::string filename)
149
 
{
150
 
  doc = xmlParseFile(filename.c_str());
151
 
 
152
 
  if (!doc) {
153
 
    throw PingusError (_("PingusWorldMapGraph: File not found: ") + filename);
154
 
  }    
155
 
 
156
 
  xmlNodePtr cur = doc->ROOT;
157
 
 
158
 
  if (cur != NULL && strcmp((const char*)cur->name, "pingus-worldmap") == 0)
159
 
    {
160
 
      cur = cur->children;
161
 
      
162
 
      while (cur != NULL)
163
 
        {
164
 
          if (xmlIsBlankNode(cur)) 
165
 
            {
166
 
              cur = cur->next;
167
 
              continue;
168
 
            }
169
 
 
170
 
          if (strcmp ((char*)cur->name, "node-list") == 0)
171
 
            {
172
 
              parse_node_list (cur);
173
 
            }
174
 
          else if (strcmp ((char*)cur->name, "surface") == 0)
175
 
            {
176
 
              parse_background (cur);
177
 
            }
178
 
          else if (strcmp ((char*)cur->name, "music") == 0)
179
 
            {
180
 
              parse_music (cur);
181
 
            }
182
 
          else
183
 
            {
184
 
              printf("PingusWorldMapGraph: Unhandled: %s\n", (char*)cur->name);
185
 
            }
186
 
          cur = cur->next;
187
 
        }      
188
 
    }
189
 
  
190
 
  xmlFreeDoc(doc);
191
 
}
192
 
 
193
 
void
194
 
PingusWorldMapGraph::parse_node_list (xmlNodePtr cur)
195
 
{
196
 
  cur = cur->children;
197
 
  while (cur != NULL)
198
 
    {
199
 
      if (xmlIsBlankNode(cur)) 
200
 
        {
201
 
          cur = cur->next;
202
 
          continue;
203
 
        }
204
 
 
205
 
      if (strcmp((char*)cur->name, "node") == 0)
206
 
        {
207
 
          parse_node (cur);
208
 
        }
209
 
      else if (strcmp((char*)cur->name, "tube") == 0)
210
 
        {
211
 
          parse_tube (cur);
212
 
        }
213
 
      else if (strcmp((char*)cur->name, "comment") == 0)
214
 
        {
215
 
          // ignore
216
 
        }
217
 
      else
218
 
        {
219
 
          printf("PingusWorldMapGraph:parse_node_list: Unhandled: %s\n", (char*)cur->name);       
220
 
        }
221
 
      cur = cur->next;
222
 
    } 
223
 
}
224
 
 
225
 
void 
226
 
PingusWorldMapGraph::parse_tube (xmlNodePtr cur)
227
 
{
228
 
  PingusWorldMapTubeNode* node = new PingusWorldMapTubeNode ();
229
 
 
230
 
  char* id = (char*)xmlGetProp(cur, (xmlChar*)"id");
231
 
  if (id)
232
 
    node->id = StringConverter::to_int (id);
233
 
  else
234
 
    std::cout << "PingusWorldMapGraph::parse_tube: no node 'id' given" << std::endl;
235
 
 
236
 
  cur = cur->children;
237
 
  
238
 
  while (cur != NULL)
239
 
    {
240
 
      if (xmlIsBlankNode(cur)) 
241
 
        {
242
 
          cur = cur->next;
243
 
          continue;
244
 
        }
245
 
 
246
 
      if (strcmp((char*)cur->name, "position") == 0)
247
 
        {
248
 
          node->pos = XMLhelper::parse_vector (doc, cur);
249
 
        }
250
 
      else if (strcmp((char*)cur->name, "worldmap") == 0)
251
 
        {
252
 
          char* link_node = (char*)xmlGetProp(cur, (xmlChar*)"linknode");
253
 
          if (link_node)
254
 
            from_string (link_node, node->link_node);
255
 
          else
256
 
            std::cout << "PingusWorldMapGraph::parse_tube: no node 'linknode' given" << std::endl;
257
 
 
258
 
          node->worldmap_name = XMLhelper::parse_string (doc, cur);
259
 
        }
260
 
      else if (strcmp((char*)cur->name, "link") == 0)
261
 
        {
262
 
          char* id = (char*)xmlGetProp(cur, (xmlChar*)"id");
263
 
          if (id)
264
 
            node->links.push_back(StringConverter::to_int (id));
265
 
          else
266
 
            std::cout << "PingusWorldMapGraph::parse_node: no id given" << std::endl;       
267
 
        }
268
 
 
269
 
      cur = cur->next;
270
 
    }
271
 
  
272
 
  nodes.push_back (boost::shared_ptr<class PingusWorldMapNode>(node));
273
 
}
274
 
 
275
 
void
276
 
PingusWorldMapGraph::parse_node (xmlNodePtr cur)
277
 
{
278
 
  PingusWorldMapLevelNode* node = new PingusWorldMapLevelNode ();
279
 
 
280
 
  char* id = (char*)xmlGetProp(cur, (xmlChar*)"id");
281
 
  if (id)
282
 
    node->id = StringConverter::to_int (id);
283
 
  else
284
 
    std::cout << "PingusWorldMapGraph::parse_node: no node id given" << std::endl;
285
 
  
286
 
  char* accessible = (char*)xmlGetProp(cur, (xmlChar*)"accessible");
287
 
  if (accessible)
288
 
    node->accessible = StringConverter::to_int (accessible);
289
 
  
290
 
  cur = cur->children;
291
 
 
292
 
  while (cur != NULL)
293
 
    {
294
 
      if (xmlIsBlankNode(cur)) 
295
 
        {
296
 
          cur = cur->next;
297
 
          continue;
298
 
        }
299
 
 
300
 
      if (strcmp((char*)cur->name, "level") == 0)
301
 
        {
302
 
          char* level = (char*)xmlGetProp(cur, (xmlChar*)"name");
303
 
          if (level)
304
 
            node->levelname = std::string("levels/") + level;
305
 
          else
306
 
            std::cout << "PingusWorldMapGraph::parse_node: no levelname given" << std::endl;
307
 
        }
308
 
      else if (strcmp((char*)cur->name, "position") == 0)
309
 
        {
310
 
          node->pos = XMLhelper::parse_vector (doc, cur);
311
 
        }
312
 
      else if (strcmp((char*)cur->name, "link") == 0)
313
 
        {
314
 
          char* id = (char*)xmlGetProp(cur, (xmlChar*)"id");
315
 
          if (id)
316
 
            node->links.push_back(StringConverter::to_int (id));
317
 
          else
318
 
            std::cout << "PingusWorldMapGraph::parse_node: no id given" << std::endl;       
319
 
        }
320
 
      else
321
 
        {
322
 
          printf("PingusWorldMapGraph:parse_node: Unhandled: %s\n", (char*)cur->name);    
323
 
        }
324
 
 
325
 
      cur = cur->next;
326
 
    }  
327
 
 
328
 
  nodes.push_back (boost::shared_ptr<class PingusWorldMapNode>(node));
329
 
  // FIXME: Add the node to the graph
330
 
}
331
 
 
332
 
void
333
 
PingusWorldMapGraph::parse_background (xmlNodePtr cur)
334
 
{
335
 
  bg_desc = XMLhelper::parse_surface(doc, cur);
336
 
}
337
 
 
338
 
void
339
 
PingusWorldMapGraph::parse_music (xmlNodePtr cur)
340
 
{
341
 
  char* file = (char*)xmlGetProp(cur, (xmlChar*)"file");
342
 
 
343
 
  if (file)
344
 
    music = file;
345
 
  else
346
 
    {
347
 
      std::cout << "PingusWorldMapGraph: No music file given" << std::endl;
348
 
    }
349
 
}
350
 
 
351
 
ResDescriptor 
352
 
PingusWorldMapGraph::get_background ()
353
 
{
354
 
  return bg_desc;
355
 
}
356
 
 
357
 
std::string 
358
 
PingusWorldMapGraph::get_music ()
359
 
{
360
 
  return music;
361
 
}
362
 
 
363
 
/*Graph<PingusWorldMapNode>* 
364
 
PingusWorldMapGraph::get_graph ()
365
 
{
366
 
  return graph;
367
 
}*/
368
 
 
369
 
void
370
 
PingusWorldMapGraph::draw (const CL_Vector& offset)
371
 
{
372
 
  //float x_scale = CL_Display::get_width () / 800.0;
373
 
  //float y_scale = CL_Display::get_height () / 600.0;
374
 
 
375
 
  for (iterator i = nodes.begin();
376
 
       i != nodes.end();
377
 
       ++i)
378
 
    {
379
 
      for (iterator j = nodes.begin();
380
 
           j != nodes.end();
381
 
           ++j)
382
 
        {
383
 
          for (std::list<int>::iterator k = (*i)->links.begin();
384
 
               k != (*i)->links.end();
385
 
               ++k)
386
 
            if ((*j)->id == *k)
387
 
              CL_Display::draw_line ((int)((*j)->pos.x + offset.x), (int)((*j)->pos.y + offset.y),
388
 
                                     (int)((*i)->pos.x + offset.x), (int)((*i)->pos.y + offset.y),
389
 
                                     1.0, 1.0, 1.0, 1.0);
390
 
        }
391
 
    }
392
 
}
393
 
 
394
 
      /* Fade out, fixme, doesn't work at the moment 
395
 
         CL_SurfaceProvider* provider = new TargetProvider (target);
396
 
         CL_Surface* sur = CL_Surface::create (provider);
397
 
 
398
 
         for (int y = 0; y < CL_Display::get_height(); 
399
 
         y += CL_Display::get_height() / 40)
400
 
         {
401
 
         CL_System::keep_alive ();
402
 
         CL_Display::clear_display ();
403
 
         sur->put_screen (0, y);
404
 
         Display::flip_display ();
405
 
         }
406
 
                      
407
 
         delete sur;
408
 
         delete provider;
409
 
      */
410
 
 
411
 
/* EOF */