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