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