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