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