e8940e64d100dd0cf01f2b6e4811b2445bc24aee
[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       plevel->dn_tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
229       plevel->dn_tiles[i][plevel->width] = (unsigned int) '\0';
230       for(y = 0; y < plevel->width; ++y)
231         plevel->dn_tiles[i][y] = (unsigned int) '.';
232       plevel->dn_tiles[i][plevel->width] = (unsigned int) '\0';
233     }
234 }
235
236 /* Load data for this level: */
237 /* Returns -1, if the loading of the level failed. */
238 int level_load(st_level* plevel, const  char *subset, int level)
239 {
240   char filename[1024];
241
242   /* Load data file: */
243
244   snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
245   if(!faccessible(filename))
246     snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
247
248   return level_load(plevel, filename);
249 }
250
251 int level_load(st_level* plevel, const char* filename)
252 {
253   FILE * fi;
254   lisp_object_t* root_obj = 0;
255   fi = fopen(filename, "r");
256   if (fi == NULL)
257     {
258       perror(filename);
259       return -1;
260     }
261
262   lisp_stream_t stream;
263   lisp_stream_init_file (&stream, fi);
264   root_obj = lisp_read (&stream);
265
266   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
267     {
268       printf("World: Parse Error in file %s", filename);
269     }
270
271   vector<int> ia_tm;
272   vector<int> dn_tm;
273   vector<int> bg_tm;
274   vector<int> fg_tm;
275
276   int version = 0;
277   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
278     {
279       LispReader reader(lisp_cdr(root_obj));
280
281       reader.read_int("version",  &version);
282       reader.read_int("width",  &plevel->width);
283       reader.read_int("time",  &plevel->time_left);
284       reader.read_int("bkgd_red",  &plevel->bkgd_red);
285       reader.read_int("bkgd_green",  &plevel->bkgd_green);
286       reader.read_int("bkgd_blue",  &plevel->bkgd_blue);
287       reader.read_float("gravity",  &plevel->gravity);
288       reader.read_string("name",  &plevel->name);
289       reader.read_string("theme",  &plevel->theme);
290       reader.read_string("music",  &plevel->song_title);
291       reader.read_string("background",  &plevel->bkgd_image);
292       reader.read_string("particle_system", &plevel->particle_system);
293       reader.read_int_vector("background-tm",  &bg_tm);
294
295       if (!reader.read_int_vector("interactive-tm", &ia_tm))
296         reader.read_int_vector("tilemap", &ia_tm);
297
298       reader.read_int_vector("dynamic-tm",     &dn_tm);
299       reader.read_int_vector("foreground-tm",  &fg_tm);
300
301       {
302         lisp_object_t* cur = 0;
303         if (reader.read_lisp("objects",  &cur))
304           {
305             while (!lisp_nil_p(cur))
306               {
307                 lisp_object_t* data = lisp_car(cur);
308
309                 BadGuyData bg_data;
310                 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
311                 LispReader reader(lisp_cdr(data));
312                 reader.read_int("x", &bg_data.x);
313                 reader.read_int("y", &bg_data.y);
314
315                 plevel->badguy_data.push_back(bg_data);
316  
317                 cur = lisp_cdr(cur);
318               }
319           }        
320       }
321
322       // Convert old levels to the new tile numbers
323       if (version == 0)
324         {
325           std::map<char, int> transtable;
326           transtable['.'] = 0;
327           transtable['0'] = 1000;
328           transtable['1'] = 1001;
329           transtable['2'] = 1002;
330           transtable['x'] = 104;
331           transtable['X'] = 77;
332           transtable['y'] = 78;
333           transtable['Y'] = 105;
334           transtable['A'] = 83;
335           transtable['B'] = 102;
336           transtable['!'] = 103;
337           transtable['a'] = 84;
338           transtable['C'] = 85;
339           transtable['D'] = 86;
340           transtable['E'] = 87;
341           transtable['F'] = 88;
342           transtable['c'] = 89;
343           transtable['d'] = 90;
344           transtable['e'] = 91;
345           transtable['f'] = 92;
346
347           transtable['G'] = 93;
348           transtable['H'] = 94;
349           transtable['I'] = 95;
350           transtable['J'] = 96;
351
352           transtable['g'] = 97;
353           transtable['h'] = 98;
354           transtable['i'] = 99;
355           transtable['j'] = 100
356             ;
357           transtable['#'] = 11;
358           transtable['['] = 13; 
359           transtable['='] = 14;
360           transtable[']'] = 15;
361           transtable['$'] = 82;
362           transtable['^'] = 76;
363           transtable['*'] = 80;
364           transtable['|'] = 79;
365           transtable['\\'] = 81;
366           transtable['&'] = 75;
367
368           for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
369             {
370               std::map<char, int>::iterator j = transtable.find(*i);
371               if (j != transtable.end())
372                 *i = j->second;
373               else
374                 printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
375             }
376         }
377     }
378
379   for(int i = 0; i < 15; ++i)
380     {
381       plevel->dn_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
382       plevel->ia_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
383       plevel->bg_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
384       plevel->fg_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
385     }
386
387   int i = 0;
388   int j = 0;
389   for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
390     {
391       plevel->ia_tiles[j][i] = (*it);
392       if(i == plevel->width - 1)
393         {
394           i = -1;
395           ++j;
396         }
397     }
398
399   i = j = 0;
400   for(vector<int>::iterator it = dn_tm.begin(); it != dn_tm.end(); ++it, ++i)
401     {
402
403       plevel->dn_tiles[j][i] = (*it);
404       if(i == plevel->width - 1)
405         {
406           i = -1;
407           ++j;
408         }
409     }
410
411   i = j = 0;
412   for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
413     {
414
415       plevel->bg_tiles[j][i] = (*it);
416       if(i == plevel->width - 1)
417         {
418           i = -1;
419           ++j;
420         }
421     }
422
423   i = j = 0;
424   for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
425     {
426
427       plevel->fg_tiles[j][i] = (*it);
428       if(i == plevel->width - 1)
429         {
430           i = -1;
431           ++j;
432         }
433     }
434
435   /* Set the global gravity to the latest loaded level's gravity */
436   gravity = plevel->gravity;
437
438   /*  Mark the end position of this level! - Is a bit wrong here thought * /
439
440   for (y = 0; y < 15; ++y)
441   {
442   for (x = 0; x < plevel->width; ++x)
443   {
444   if(plevel->tiles[y][x] == '|')
445   {
446   if(x*32 > endpos)
447   endpos = x*32;
448   }
449   }
450   }*/
451
452   fclose(fi);
453   return 0;
454 }
455
456 /* Save data for level: */
457
458 void level_save(st_level* plevel,const  char * subset, int level)
459 {
460   FILE * fi;
461   char filename[1024];
462   int y,i;
463   char str[80];
464
465   /* Save data file: */
466   sprintf(str, "/levels/%s/", subset);
467   fcreatedir(str);
468   snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
469   if(!fwriteable(filename))
470     snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
471
472   fi = fopen(filename, "w");
473   if (fi == NULL)
474     {
475       perror(filename);
476       st_shutdown();
477       exit(-1);
478     }
479
480
481   /* Write header: */
482   fprintf(fi,";SuperTux-Level\n");
483   fprintf(fi,"(supertux-level\n");
484
485   fprintf(fi,"  (version %d)\n", 1);
486   fprintf(fi,"  (name \"%s\")\n", plevel->name.c_str());
487   fprintf(fi,"  (theme \"%s\")\n", plevel->theme.c_str());
488   fprintf(fi,"  (music \"%s\")\n", plevel->song_title.c_str());
489   fprintf(fi,"  (background \"%s\")\n", plevel->bkgd_image.c_str());
490   fprintf(fi,"  (particle_system \"%s\")\n", plevel->particle_system.c_str());
491   fprintf(fi,"  (bkgd_red %d)\n", plevel->bkgd_red);
492   fprintf(fi,"  (bkgd_green %d)\n", plevel->bkgd_green);
493   fprintf(fi,"  (bkgd_blue %d)\n", plevel->bkgd_blue);
494   fprintf(fi,"  (time %d)\n", plevel->time_left);
495   fprintf(fi,"  (width %d)\n", plevel->width);
496   fprintf(fi,"  (gravity %2.1f)\n", plevel->gravity);
497   fprintf(fi,"  (background-tm ");
498
499   for(y = 0; y < 15; ++y)
500     {
501       for(i = 0; i < plevel->width; ++i)
502         fprintf(fi," %d ", plevel->bg_tiles[y][i]);
503     }
504
505   fprintf( fi,")\n");
506   fprintf(fi,"  (interactive-tm ");
507
508   for(y = 0; y < 15; ++y)
509     {
510       for(i = 0; i < plevel->width; ++i)
511         fprintf(fi," %d ", plevel->ia_tiles[y][i]);
512     }
513
514   fprintf( fi,")\n");
515   fprintf(fi,"  (dynamic-tm ");
516
517   for(y = 0; y < 15; ++y)
518     {
519       for(i = 0; i < plevel->width; ++i)
520         fprintf(fi," %d ", plevel->dn_tiles[y][i]);
521     }
522
523   fprintf( fi,")\n");
524   fprintf(fi,"  (foreground-tm ");
525
526   for(y = 0; y < 15; ++y)
527     {
528       for(i = 0; i < plevel->width; ++i)
529         fprintf(fi," %d ", plevel->fg_tiles[y][i]);
530     }
531
532   fprintf( fi,")");
533   fprintf( fi,")\n");
534
535   fclose(fi);
536 }
537
538
539 /* Unload data for this level: */
540
541 void level_free(st_level* plevel)
542 {
543   int i;
544   for(i=0; i < 15; ++i)
545     free(plevel->bg_tiles[i]);
546   for(i=0; i < 15; ++i)
547     free(plevel->ia_tiles[i]);
548   for(i=0; i < 15; ++i)
549     free(plevel->dn_tiles[i]);
550   for(i=0; i < 15; ++i)
551     free(plevel->fg_tiles[i]);
552
553   plevel->name.clear();
554   plevel->theme.clear();
555   plevel->song_title.clear();
556   plevel->bkgd_image.clear();
557 }
558
559 /* Load graphics: */
560
561 void level_load_gfx(st_level *plevel)
562 {
563   level_load_image(&img_brick[0],plevel->theme,"brick0.png", IGNORE_ALPHA);
564   level_load_image(&img_brick[1],plevel->theme,"brick1.png", IGNORE_ALPHA);
565
566   level_load_image(&img_solid[0],plevel->theme,"solid0.png", USE_ALPHA);
567   level_load_image(&img_solid[1],plevel->theme,"solid1.png", USE_ALPHA);
568   level_load_image(&img_solid[2],plevel->theme,"solid2.png", USE_ALPHA);
569   level_load_image(&img_solid[3],plevel->theme,"solid3.png", USE_ALPHA);
570
571   level_load_image(&img_bkgd_tile[0][0],plevel->theme,"bkgd-00.png", USE_ALPHA);
572   level_load_image(&img_bkgd_tile[0][1],plevel->theme,"bkgd-01.png", USE_ALPHA);
573   level_load_image(&img_bkgd_tile[0][2],plevel->theme,"bkgd-02.png", USE_ALPHA);
574   level_load_image(&img_bkgd_tile[0][3],plevel->theme,"bkgd-03.png", USE_ALPHA);
575
576   level_load_image(&img_bkgd_tile[1][0],plevel->theme,"bkgd-10.png", USE_ALPHA);
577   level_load_image(&img_bkgd_tile[1][1],plevel->theme,"bkgd-11.png", USE_ALPHA);
578   level_load_image(&img_bkgd_tile[1][2],plevel->theme,"bkgd-12.png", USE_ALPHA);
579   level_load_image(&img_bkgd_tile[1][3],plevel->theme,"bkgd-13.png", USE_ALPHA);
580
581   if(!plevel->bkgd_image.empty())
582     {
583       char fname[1024];
584       snprintf(fname, 1024, "%s/background/%s", st_dir, plevel->bkgd_image.c_str());
585       if(!faccessible(fname))
586         snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), plevel->bkgd_image.c_str());
587       texture_load(&img_bkgd, fname, IGNORE_ALPHA);
588     }
589   else
590     {
591       /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */#
592       level_load_image(&img_bkgd, plevel->theme,"solid0.png", IGNORE_ALPHA);
593     }
594 }
595
596 /* Free graphics data for this level: */
597
598 void level_free_gfx(void)
599 {
600   int i;
601
602   for (i = 0; i < 2; i++)
603     {
604       texture_free(&img_brick[i]);
605     }
606   for (i = 0; i < 4; i++)
607     {
608       texture_free(&img_solid[i]);
609       texture_free(&img_bkgd_tile[0][i]);
610       texture_free(&img_bkgd_tile[1][i]);
611     }
612
613   texture_free(&img_bkgd);
614 }
615
616 /* Load a level-specific graphic... */
617
618 void level_load_image(texture_type* ptexture, string theme,const  char * file, int use_alpha)
619 {
620   char fname[1024];
621
622   snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
623   if(!faccessible(fname))
624     snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
625
626   texture_load(ptexture, fname, use_alpha);
627 }
628
629 void tilemap_change_size(unsigned int** tilemap[15], int w, int old_w)
630 {
631   int j,y;
632   for(y = 0; y < 15; ++y)
633     {
634       *tilemap[y] = (unsigned int*) realloc(*tilemap[y],(w+1)*sizeof(unsigned int));
635       if(w > old_w)
636         for(j = 0; j < w - old_w; ++j)
637           *tilemap[y][old_w+j] = 0;
638       *tilemap[y][w] = 0;
639     }
640 }
641
642 /* Change the size of a level (width) */
643 void level_change_size (st_level* plevel, int new_width)
644 {
645   if(new_width < 21)
646     new_width = 21;
647   tilemap_change_size((unsigned int***)&plevel->ia_tiles,new_width,plevel->width);
648   tilemap_change_size((unsigned int***)&plevel->dn_tiles,new_width,plevel->width);
649   tilemap_change_size((unsigned int***)&plevel->bg_tiles,new_width,plevel->width);
650   tilemap_change_size((unsigned int***)&plevel->fg_tiles,new_width,plevel->width);
651   plevel->width = new_width;
652
653 }
654
655 /* Edit a piece of the map! */
656
657 void level_change(st_level* plevel, float x, float y, int tm, unsigned int c)
658 {
659   int xx, yy;
660
661   yy = ((int)y / 32);
662   xx = ((int)x / 32);
663
664   if (yy >= 0 && yy < 15 && xx >= 0 && xx <= plevel->width)
665     {
666       switch(tm)
667         {
668         case 0:
669           plevel->bg_tiles[yy][xx] = c;
670         case 1:
671           plevel->ia_tiles[yy][xx] = c;
672         case 2:
673           plevel->dn_tiles[yy][xx] = c;
674         case 4:
675           plevel->fg_tiles[yy][xx] = c;
676         }
677     }
678 }
679
680 /* Free music data for this level: */
681
682 void level_free_song(void)
683 {
684   free_music(level_song);
685   free_music(level_song_fast);
686 }
687
688 /* Load music: */
689
690 void level_load_song(st_level* plevel)
691 {
692
693   char * song_path;
694   char * song_subtitle;
695
696   level_song = load_song(datadir + "/music/" + plevel->song_title);
697
698   song_path = (char *) malloc(sizeof(char) * datadir.length() +
699                               strlen(plevel->song_title.c_str()) + 8 + 5);
700   song_subtitle = strdup(plevel->song_title.c_str());
701   strcpy(strstr(song_subtitle, "."), "\0");
702   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), song_subtitle, strstr(plevel->song_title.c_str(), "."));
703   level_song_fast = load_song(song_path);
704   free(song_subtitle);
705   free(song_path);
706 }