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