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