At least my compiler (g++ (GCC) 3.3.2) needs this include.
[supertux.git] / src / level.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
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
19 //  02111-1307, USA.
20
21 #include <map>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <iostream>
26 #include "globals.h"
27 #include "setup.h"
28 #include "screen.h"
29 #include "level.h"
30 #include "physic.h"
31 #include "scene.h"
32 #include "tile.h"
33 #include "lispreader.h"
34 #include "resources.h"
35 #include "music_manager.h"
36
37 using namespace std;
38
39 st_subset::st_subset()
40 {
41   levels = 0;
42 }
43
44 void st_subset::create(const std::string& subset_name)
45 {
46   Level new_lev;
47   st_subset new_subset;
48   new_subset.name = subset_name;
49   new_subset.title = "Unknown Title";
50   new_subset.description = "No description so far.";
51   new_subset.save();
52   new_lev.init_defaults();
53   new_lev.save(subset_name.c_str(),1);
54 }
55
56 void st_subset::parse (lisp_object_t* cursor)
57 {
58   while(!lisp_nil_p(cursor))
59     {
60       lisp_object_t* cur = lisp_car(cursor);
61       char *s;
62
63       if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
64         {
65           printf("Not good");
66         }
67       else
68         {
69           if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
70             {
71               if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
72                 {
73                   title = s;
74                 }
75             }
76           else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
77             {
78               if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
79                 {
80                   description = s;
81                 }
82             }
83         }
84       cursor = lisp_cdr (cursor);
85     }
86 }
87
88 void st_subset::load(char *subset)
89 {
90   FILE* fi;
91   char filename[1024];
92   char str[1024];
93   int i;
94   lisp_object_t* root_obj = 0;
95
96   name = subset;
97
98   snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
99   if(!faccessible(filename))
100     snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
101   if(faccessible(filename))
102     {
103       fi = fopen(filename, "r");
104       if (fi == NULL)
105         {
106           perror(filename);
107         }
108       lisp_stream_t stream;
109       lisp_stream_init_file (&stream, fi);
110       root_obj = lisp_read (&stream);
111
112       if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
113         {
114           printf("World: Parse Error in file %s", filename);
115         }
116
117       lisp_object_t* cur = lisp_car(root_obj);
118
119       if (!lisp_symbol_p (cur))
120         {
121           printf("World: Read error in %s",filename);
122         }
123
124       if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
125         {
126           parse(lisp_cdr(root_obj));
127
128         }
129
130       fclose(fi);
131
132       snprintf(str, 1024, "%s.png", filename);
133       if(faccessible(str))
134         {
135           image = new Surface(str,IGNORE_ALPHA);
136         }
137       else
138         {
139           snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
140           image = new Surface(filename,IGNORE_ALPHA);
141         }
142     }
143
144   for(i=1; i != -1; ++i)
145     {
146       /* Get the number of levels in this subset */
147       snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
148       if(!faccessible(filename))
149         {
150           snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
151           if(!faccessible(filename))
152             break;
153         }
154     }
155   levels = --i;
156 }
157
158 void st_subset::save()
159 {
160   FILE* fi;
161   string filename;
162
163   /* Save data file: */
164   filename = "/levels/" + name + "/";
165
166   fcreatedir(filename.c_str());
167   filename = string(st_dir) + "/levels/" + name + "/info";
168   if(!fwriteable(filename.c_str()))
169     filename = datadir + "/levels/" + name + "/info";
170   if(fwriteable(filename.c_str()))
171     {
172       fi = fopen(filename.c_str(), "w");
173       if (fi == NULL)
174         {
175           perror(filename.c_str());
176         }
177
178       /* Write header: */
179       fprintf(fi,";SuperTux-Level-Subset\n");
180       fprintf(fi,"(supertux-level-subset\n");
181
182       /* Save title info: */
183       fprintf(fi,"  (title \"%s\")\n", title.c_str());
184
185       /* Save the description: */
186       fprintf(fi,"  (description \"%s\")\n", description.c_str());
187
188       fprintf( fi,")");
189       fclose(fi);
190
191     }
192 }
193
194 void st_subset::free()
195 {
196   title.clear();
197   description.clear();
198   name.clear();
199   delete image;
200   levels = 0;
201 }
202
203 Level::Level()
204   : img_bkgd(0)
205 {
206 }
207
208 Level::Level(const std::string& subset, int level)
209   : img_bkgd(0)
210 {
211   load(subset, level);
212 }
213
214 Level::Level(const std::string& filename)
215   : img_bkgd(0)
216 {
217   load(filename);
218 }
219
220 Level::~Level()
221 {
222   free_gfx();
223 }
224
225 void
226 Level::init_defaults()
227 {
228   name       = "UnNamed";
229   author     = "UnNamed";
230   theme      = "antarctica";
231   song_title = "Mortimers_chipdisko.mod";
232   bkgd_image = "arctis.png";
233   width      = 21;
234   start_pos_x = 100;
235   start_pos_y = 170;
236   time_left  = 100;
237   gravity    = 10.;
238   bkgd_top.red   = 0;
239   bkgd_top.green = 0;
240   bkgd_top.blue  = 0;
241   bkgd_bottom.red   = 255;
242   bkgd_bottom.green = 255;
243   bkgd_bottom.blue  = 255;
244   endpos     = 0;
245   use_endsequence = false;
246
247   for(int i = 0; i < 15; ++i)
248     {
249       ia_tiles[i].resize(width+1, 0);
250       ia_tiles[i][width] = (unsigned int) '\0';
251
252       for(int y = 0; y < width; ++y)
253         ia_tiles[i][y] = 0;
254
255       bg_tiles[i].resize(width+1, 0);
256       bg_tiles[i][width] = (unsigned int) '\0';
257       for(int y = 0; y < width; ++y)
258         bg_tiles[i][y] = 0;
259
260       fg_tiles[i].resize(width+1, 0);
261       fg_tiles[i][width] = (unsigned int) '\0';
262       for(int y = 0; y < width; ++y)
263         fg_tiles[i][y] = 0;
264     }
265 }
266
267 int
268 Level::load(const std::string& subset, int level)
269 {
270   char filename[1024];
271
272   // Load data file:
273   snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(), level);
274   if(!faccessible(filename))
275     snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset.c_str(), level);
276
277   return load(filename);
278 }
279
280 int 
281 Level::load(const std::string& filename)
282 {
283   FILE * fi;
284   lisp_object_t* root_obj = 0;
285   fi = fopen(filename.c_str(), "r");
286   if (fi == NULL)
287     {
288       perror(filename.c_str());
289       return -1;
290     }
291
292   lisp_stream_t stream;
293   lisp_stream_init_file (&stream, fi);
294   root_obj = lisp_read (&stream);
295
296   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
297     {
298       printf("World: Parse Error in file %s", filename.c_str());
299     }
300
301   vector<int> ia_tm;
302   vector<int> bg_tm;
303   vector<int> fg_tm;
304
305   int version = 0;
306   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
307     {
308       LispReader reader(lisp_cdr(root_obj));
309       reader.read_int("version",  &version);
310       reader.read_bool("use-endsequence", &use_endsequence);
311       reader.read_int("width",  &width);
312       if (!reader.read_int("start_pos_x", &start_pos_x)) start_pos_x = 100;
313       if (!reader.read_int("start_pos_y", &start_pos_y)) start_pos_y = 170;
314       reader.read_int("time",  &time_left);
315
316       reader.read_int("bkgd_top_red",  &bkgd_top.red);
317       reader.read_int("bkgd_top_green",  &bkgd_top.green);
318       reader.read_int("bkgd_top_blue",  &bkgd_top.blue);
319
320       reader.read_int("bkgd_bottom_red",  &bkgd_bottom.red);
321       reader.read_int("bkgd_bottom_green",  &bkgd_bottom.green);
322       reader.read_int("bkgd_bottom_blue",  &bkgd_bottom.blue);
323
324       reader.read_float("gravity",  &gravity);
325       reader.read_string("name",  &name);
326       reader.read_string("author", &author);
327       reader.read_string("theme",  &theme);
328       reader.read_string("music",  &song_title);
329       reader.read_string("background",  &bkgd_image);
330       reader.read_string("particle_system", &particle_system);
331       reader.read_int_vector("background-tm",  &bg_tm);
332
333       if (!reader.read_int_vector("interactive-tm", &ia_tm))
334         reader.read_int_vector("tilemap", &ia_tm);
335
336       reader.read_int_vector("foreground-tm",  &fg_tm);
337
338       { // Read ResetPoints
339         lisp_object_t* cur = 0;
340         if (reader.read_lisp("reset-points",  &cur))
341           {
342             while (!lisp_nil_p(cur))
343               {
344                 lisp_object_t* data = lisp_car(cur);
345
346                 ResetPoint pos;
347
348                 LispReader reader(lisp_cdr(data));
349                 if (reader.read_int("x", &pos.x)
350                     && reader.read_int("y", &pos.y))
351                   {
352                     reset_points.push_back(pos);
353                   }
354
355                 cur = lisp_cdr(cur);
356               }
357           }
358       }
359
360       { // Read BadGuys
361         lisp_object_t* cur = 0;
362         if (reader.read_lisp("objects",  &cur))
363           {
364             while (!lisp_nil_p(cur))
365               {
366                 lisp_object_t* data = lisp_car(cur);
367
368                 BadGuyData bg_data;
369                 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
370                 LispReader reader(lisp_cdr(data));
371                 reader.read_int("x", &bg_data.x);
372                 reader.read_int("y", &bg_data.y);
373                 reader.read_bool("stay-on-platform", &bg_data.stay_on_platform);
374
375                 badguy_data.push_back(bg_data);
376
377                 cur = lisp_cdr(cur);
378               }
379           }
380       }
381
382       // Convert old levels to the new tile numbers
383       if (version == 0)
384         {
385           std::map<char, int> transtable;
386           transtable['.'] = 0;
387           transtable['x'] = 104;
388           transtable['X'] = 77;
389           transtable['y'] = 78;
390           transtable['Y'] = 105;
391           transtable['A'] = 83;
392           transtable['B'] = 102;
393           transtable['!'] = 103;
394           transtable['a'] = 84;
395           transtable['C'] = 85;
396           transtable['D'] = 86;
397           transtable['E'] = 87;
398           transtable['F'] = 88;
399           transtable['c'] = 89;
400           transtable['d'] = 90;
401           transtable['e'] = 91;
402           transtable['f'] = 92;
403
404           transtable['G'] = 93;
405           transtable['H'] = 94;
406           transtable['I'] = 95;
407           transtable['J'] = 96;
408
409           transtable['g'] = 97;
410           transtable['h'] = 98;
411           transtable['i'] = 99;
412           transtable['j'] = 100
413                             ;
414           transtable['#'] = 11;
415           transtable['['] = 13;
416           transtable['='] = 14;
417           transtable[']'] = 15;
418           transtable['$'] = 82;
419           transtable['^'] = 76;
420           transtable['*'] = 80;
421           transtable['|'] = 79;
422           transtable['\\'] = 81;
423           transtable['&'] = 75;
424
425           int x = 0;
426           int y = 0;
427           for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
428             {
429               if (*i == '0' || *i == '1' || *i == '2')
430                 {
431                   badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
432                                                    x*32, y*32, false));
433                   *i = 0;
434                 }
435               else
436                 {
437                   std::map<char, int>::iterator j = transtable.find(*i);
438                   if (j != transtable.end())
439                     *i = j->second;
440                   else
441                     printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
442                 }
443               ++x;
444               if (x >= width)
445                 {
446                   x = 0;
447                   ++y;
448                 }
449             }
450         }
451     }
452
453   for(int i = 0; i < 15; ++i)
454     {
455       ia_tiles[i].resize(width + 1, 0);
456       bg_tiles[i].resize(width + 1, 0);
457       fg_tiles[i].resize(width + 1, 0);
458     }
459
460   int i = 0;
461   int j = 0;
462   for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
463     {
464       ia_tiles[j][i] = (*it);
465       if(i == width - 1)
466         {
467           i = -1;
468           ++j;
469         }
470     }
471
472   i = j = 0;
473   for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
474     {
475
476       bg_tiles[j][i] = (*it);
477       if(i == width - 1)
478         {
479           i = -1;
480           ++j;
481         }
482     }
483
484   i = j = 0;
485   for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
486     {
487
488       fg_tiles[j][i] = (*it);
489       if(i == width - 1)
490         {
491           i = -1;
492           ++j;
493         }
494     }
495
496   //  Mark the end position of this level!
497   // FIXME: -10 is a rather random value, we still need some kind of
498   // real levelend gola
499   if (use_endsequence)
500     endpos = 32*(width-20);
501   else
502     endpos = 32*(width-15);
503
504   fclose(fi);
505   return 0;
506 }
507
508 /* Save data for level: */
509
510 void 
511 Level::save(const  char * subset, int level)
512 {
513   char filename[1024];
514   char str[80];
515
516   /* Save data file: */
517   sprintf(str, "/levels/%s/", subset);
518   fcreatedir(str);
519   snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
520   if(!fwriteable(filename))
521     snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
522
523   FILE * fi = fopen(filename, "w");
524   if (fi == NULL)
525     {
526       perror(filename);
527       st_shutdown();
528       exit(-1);
529     }
530
531
532   /* Write header: */
533   fprintf(fi,";SuperTux-Level\n");
534   fprintf(fi,"(supertux-level\n");
535
536   fprintf(fi,"  (version %d)\n", 1);
537   fprintf(fi,"  (name \"%s\")\n", name.c_str());
538   fprintf(fi,"  (author \"%s\")\n", author.c_str());
539   fprintf(fi,"  (theme \"%s\")\n", theme.c_str());
540   fprintf(fi,"  (music \"%s\")\n", song_title.c_str());
541   fprintf(fi,"  (background \"%s\")\n", bkgd_image.c_str());
542   fprintf(fi,"  (particle_system \"%s\")\n", particle_system.c_str());
543   fprintf(fi,"  (bkgd_top_red %d)\n", bkgd_top.red);
544   fprintf(fi,"  (bkgd_top_green %d)\n", bkgd_top.green);
545   fprintf(fi,"  (bkgd_top_blue %d)\n", bkgd_top.blue);
546   fprintf(fi,"  (bkgd_bottom_red %d)\n", bkgd_bottom.red);
547   fprintf(fi,"  (bkgd_bottom_green %d)\n", bkgd_bottom.green);
548   fprintf(fi,"  (bkgd_bottom_blue %d)\n", bkgd_bottom.blue);
549   fprintf(fi,"  (time %d)\n", time_left);
550   fprintf(fi,"  (width %d)\n", width);
551   fprintf(fi,"  (gravity %2.1f)\n", gravity);
552   fprintf(fi,"  (background-tm ");
553
554   for(int y = 0; y < 15; ++y)
555     {
556       for(int i = 0; i < width; ++i)
557         fprintf(fi," %d ", bg_tiles[y][i]);
558     }
559
560   fprintf( fi,")\n");
561   fprintf(fi,"  (interactive-tm ");
562
563   for(int y = 0; y < 15; ++y)
564     {
565       for(int i = 0; i < width; ++i)
566         fprintf(fi," %d ", ia_tiles[y][i]);
567     }
568
569   fprintf( fi,")\n");
570   fprintf(fi,"  (foreground-tm ");
571
572   for(int y = 0; y < 15; ++y)
573     {
574       for(int i = 0; i < width; ++i)
575         fprintf(fi," %d ", fg_tiles[y][i]);
576     }
577
578   fprintf( fi,")\n");
579
580   fprintf( fi,"(reset-points\n");
581   for(std::vector<ResetPoint>::iterator i = reset_points.begin();
582       i != reset_points.end(); ++i)
583     fprintf( fi,"(point (x %d) (y %d))\n",i->x, i->y);
584   fprintf( fi,")\n");
585
586   fprintf( fi,"(objects\n");
587
588   for(std::vector<BadGuyData>::iterator it = badguy_data.begin();
589       it != badguy_data.end();
590       ++it)
591     fprintf( fi,"(%s (x %d) (y %d) (stay-on-platform %s))\n",
592              badguykind_to_string((*it).kind).c_str(),(*it).x,(*it).y,
593              it->stay_on_platform ? "#t" : "#f");
594
595   fprintf( fi,")\n");
596
597   fprintf( fi,")\n");
598
599   fclose(fi);
600 }
601
602
603 /* Unload data for this level: */
604
605 void
606 Level::cleanup()
607 {
608   for(int i=0; i < 15; ++i)
609     {
610       bg_tiles[i].clear();
611       ia_tiles[i].clear();
612       fg_tiles[i].clear();
613     }
614
615   reset_points.clear();
616   name.clear();
617   author.clear();
618   theme.clear();
619   song_title.clear();
620   bkgd_image.clear();
621
622   badguy_data.clear();
623 }
624
625 void 
626 Level::load_gfx()
627 {
628   if(!bkgd_image.empty())
629     {
630       char fname[1024];
631       snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
632       if(!faccessible(fname))
633         snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
634       img_bkgd = new Surface(fname, IGNORE_ALPHA);
635     }
636   else
637     {
638       /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */
639       load_image(&img_bkgd, theme,"solid0.png", IGNORE_ALPHA);
640     }
641 }
642
643 void
644 Level::free_gfx()
645 {
646   delete img_bkgd;
647 }
648
649 /* Load a level-specific graphic... */
650 void
651 Level::load_image(Surface** ptexture, string theme,const  char * file, int use_alpha)
652 {
653   char fname[1024];
654
655   snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
656   if(!faccessible(fname))
657     snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
658
659   *ptexture = new Surface(fname, use_alpha);
660 }
661
662 /* Change the size of a level (width) */
663 void 
664 Level::change_size (int new_width)
665 {
666   if(new_width < 21)
667     new_width = 21;
668
669   for(int y = 0; y < 15; ++y)
670     {
671       ia_tiles[y].resize(new_width, 0);
672       bg_tiles[y].resize(new_width, 0);
673       fg_tiles[y].resize(new_width, 0);
674     }
675
676   width = new_width;
677 }
678
679 void
680 Level::change(float x, float y, int tm, unsigned int c)
681 {
682   int yy = ((int)y / 32);
683   int xx = ((int)x / 32);
684
685   if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
686     {
687       switch(tm)
688         {
689         case TM_BG:
690           bg_tiles[yy][xx] = c;
691           break;
692         case TM_IA:
693           ia_tiles[yy][xx] = c;
694           break;
695         case TM_FG:
696           fg_tiles[yy][xx] = c;
697           break;
698         }
699     }
700 }
701
702 void
703 Level::load_song()
704 {
705   char* song_path;
706   char* song_subtitle;
707
708   level_song = music_manager->load_music(datadir + "/music/" + song_title);
709
710   song_path = (char *) malloc(sizeof(char) * datadir.length() +
711                               strlen(song_title.c_str()) + 8 + 5);
712   song_subtitle = strdup(song_title.c_str());
713   strcpy(strstr(song_subtitle, "."), "\0");
714   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), 
715           song_subtitle, strstr(song_title.c_str(), "."));
716   if(!music_manager->exists_music(song_path)) {
717     level_song_fast = level_song;
718   } else {
719     level_song_fast = music_manager->load_music(song_path);
720   }
721   free(song_subtitle);
722   free(song_path);
723 }
724
725 MusicRef
726 Level::get_level_music()
727 {
728   return level_song;
729 }
730
731 MusicRef
732 Level::get_level_music_fast()
733 {
734   return level_song_fast;
735 }
736
737 unsigned int 
738 Level::gettileid(float x, float y)
739 {
740   int xx, yy;
741   unsigned int c;
742
743   yy = ((int)y / 32);
744   xx = ((int)x / 32);
745
746   if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
747     c = ia_tiles[yy][xx];
748   else
749     c = 0;
750
751   return c;
752 }
753
754 /* EOF */