8ab40789174cdf5d0a617f748d25f465e5e5d66a
[supertux.git] / src / level.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include <map>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <iostream>
26 #include "globals.h"
27 #include "setup.h"
28 #include "screen.h"
29 #include "level.h"
30 #include "physic.h"
31 #include "scene.h"
32 #include "tile.h"
33 #include "lispreader.h"
34 #include "resources.h"
35 #include "music_manager.h"
36
37 using namespace std;
38
39 st_subset::st_subset()
40 {
41   levels = 0;
42 }
43
44 void st_subset::create(const std::string& subset_name)
45 {
46   Level new_lev;
47   st_subset new_subset;
48   new_subset.name = subset_name;
49   new_subset.title = "Unknown Title";
50   new_subset.description = "No description so far.";
51   new_subset.save();
52   new_lev.init_defaults();
53   new_lev.save(subset_name.c_str(),1);
54 }
55
56 void st_subset::parse (lisp_object_t* cursor)
57 {
58   while(!lisp_nil_p(cursor))
59     {
60       lisp_object_t* cur = lisp_car(cursor);
61       char *s;
62
63       if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
64         {
65           printf("Not good");
66         }
67       else
68         {
69           if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
70             {
71               if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
72                 {
73                   title = s;
74                 }
75             }
76           else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
77             {
78               if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
79                 {
80                   description = s;
81                 }
82             }
83         }
84       cursor = lisp_cdr (cursor);
85     }
86 }
87
88 void st_subset::load(char *subset)
89 {
90   FILE* fi;
91   char filename[1024];
92   char str[1024];
93   int i;
94   lisp_object_t* root_obj = 0;
95
96   name = subset;
97
98   snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
99   if(!faccessible(filename))
100     snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
101   if(faccessible(filename))
102     {
103       fi = fopen(filename, "r");
104       if (fi == NULL)
105         {
106           perror(filename);
107         }
108       lisp_stream_t stream;
109       lisp_stream_init_file (&stream, fi);
110       root_obj = lisp_read (&stream);
111
112       if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
113         {
114           printf("World: Parse Error in file %s", filename);
115         }
116
117       lisp_object_t* cur = lisp_car(root_obj);
118
119       if (!lisp_symbol_p (cur))
120         {
121           printf("World: Read error in %s",filename);
122         }
123
124       if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
125         {
126           parse(lisp_cdr(root_obj));
127
128         }
129
130       lisp_free(root_obj);
131       fclose(fi);
132
133       snprintf(str, 1024, "%s.png", filename);
134       if(faccessible(str))
135         {
136           image = new Surface(str,IGNORE_ALPHA);
137         }
138       else
139         {
140           snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
141           image = new Surface(filename,IGNORE_ALPHA);
142         }
143     }
144
145   for(i=1; i != -1; ++i)
146     {
147       /* Get the number of levels in this subset */
148       snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
149       if(!faccessible(filename))
150         {
151           snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
152           if(!faccessible(filename))
153             break;
154         }
155     }
156   levels = --i;
157 }
158
159 void st_subset::save()
160 {
161   FILE* fi;
162   string filename;
163
164   /* Save data file: */
165   filename = "/levels/" + name + "/";
166
167   fcreatedir(filename.c_str());
168   filename = string(st_dir) + "/levels/" + name + "/info";
169   if(!fwriteable(filename.c_str()))
170     filename = datadir + "/levels/" + name + "/info";
171   if(fwriteable(filename.c_str()))
172     {
173       fi = fopen(filename.c_str(), "w");
174       if (fi == NULL)
175         {
176           perror(filename.c_str());
177         }
178
179       /* Write header: */
180       fprintf(fi,";SuperTux-Level-Subset\n");
181       fprintf(fi,"(supertux-level-subset\n");
182
183       /* Save title info: */
184       fprintf(fi,"  (title \"%s\")\n", title.c_str());
185
186       /* Save the description: */
187       fprintf(fi,"  (description \"%s\")\n", description.c_str());
188
189       fprintf( fi,")");
190       fclose(fi);
191
192     }
193 }
194
195 void st_subset::free()
196 {
197   title.clear();
198   description.clear();
199   name.clear();
200   delete image;
201   levels = 0;
202 }
203
204 Level::Level()
205   : img_bkgd(0)
206 {
207   init_defaults();
208 }
209
210 Level::Level(const std::string& subset, int level)
211   : img_bkgd(0)
212 {
213   if(load(subset, level) < 0)
214     st_abort("Couldn't load level from subset", subset.c_str());
215 }
216
217 Level::Level(const std::string& filename)
218   : img_bkgd(0)
219 {
220   if(load(filename) < 0)
221     st_abort("Couldn't load level " , filename.c_str());
222 }
223
224 Level::~Level()
225 {
226   free_gfx();
227 }
228
229 void
230 Level::init_defaults()
231 {
232   name       = "UnNamed";
233   author     = "UnNamed";
234   theme      = "antarctica";
235   song_title = "Mortimers_chipdisko.mod";
236   bkgd_image = "arctis.png";
237   width      = 21;
238   start_pos_x = 100;
239   start_pos_y = 170;
240   time_left  = 100;
241   gravity    = 10.;
242   bkgd_top.red   = 0;
243   bkgd_top.green = 0;
244   bkgd_top.blue  = 0;
245   bkgd_bottom.red   = 255;
246   bkgd_bottom.green = 255;
247   bkgd_bottom.blue  = 255;
248
249   for(int i = 0; i < 15; ++i)
250     {
251       ia_tiles[i].resize(width+1, 0);
252       ia_tiles[i][width] = (unsigned int) '\0';
253
254       for(int y = 0; y < width; ++y)
255         ia_tiles[i][y] = 0;
256
257       bg_tiles[i].resize(width+1, 0);
258       bg_tiles[i][width] = (unsigned int) '\0';
259       for(int y = 0; y < width; ++y)
260         bg_tiles[i][y] = 0;
261
262       fg_tiles[i].resize(width+1, 0);
263       fg_tiles[i][width] = (unsigned int) '\0';
264       for(int y = 0; y < width; ++y)
265         fg_tiles[i][y] = 0;
266     }
267 }
268
269 int
270 Level::load(const std::string& subset, int level)
271 {
272   char filename[1024];
273
274   // Load data file:
275   snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(), level);
276   if(!faccessible(filename))
277     snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset.c_str(), level);
278
279   return load(filename);
280 }
281
282 int 
283 Level::load(const std::string& filename)
284 {
285   FILE * fi;
286   lisp_object_t* root_obj = 0;
287   fi = fopen(filename.c_str(), "r");
288   if (fi == NULL)
289     {
290       perror(filename.c_str());
291       return -1;
292     }
293
294   lisp_stream_t stream;
295   lisp_stream_init_file (&stream, fi);
296   root_obj = lisp_read (&stream);
297
298   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
299     {
300       printf("World: Parse Error in file %s", filename.c_str());
301     }
302
303   vector<int> ia_tm;
304   vector<int> bg_tm;
305   vector<int> fg_tm;
306
307   int version = 0;
308   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
309     {
310       LispReader reader(lisp_cdr(root_obj));
311       version = 0;
312       reader.read_int("version",  &version);
313       if(!reader.read_int("width",  &width))
314         st_abort("No width specified for level.", "");
315       if (!reader.read_int("start_pos_x", &start_pos_x)) start_pos_x = 100;
316       if (!reader.read_int("start_pos_y", &start_pos_y)) start_pos_y = 170;
317       time_left = 500;
318       if(!reader.read_int("time",  &time_left)) {
319         printf("Warning no time specified for level.\n");
320       }
321
322       bkgd_top.red = bkgd_top.green = bkgd_top.blue = 0;
323       reader.read_int("bkgd_red_top",  &bkgd_top.red);
324       reader.read_int("bkgd_green_top",  &bkgd_top.green);
325       reader.read_int("bkgd_blue_top",  &bkgd_top.blue);
326
327       bkgd_bottom.red = bkgd_bottom.green = bkgd_bottom.blue = 0;
328       reader.read_int("bkgd_red_bottom",  &bkgd_bottom.red);
329       reader.read_int("bkgd_green_bottom",  &bkgd_bottom.green);
330       reader.read_int("bkgd_blue_bottom",  &bkgd_bottom.blue);
331
332       gravity = 10;
333       reader.read_float("gravity",  &gravity);
334       name = "Noname";
335       reader.read_string("name",  &name);
336       author = "unknown author";
337       reader.read_string("author", &author);
338       if(!reader.read_string("theme",  &theme))
339         st_abort("No theme specified in level file", "");
340       song_title = "";
341       reader.read_string("music",  &song_title);
342       bkgd_image = "";
343       reader.read_string("background",  &bkgd_image);
344       particle_system = "";
345       reader.read_string("particle_system", &particle_system);
346
347       reader.read_int_vector("background-tm",  &bg_tm);
348
349       if (!reader.read_int_vector("interactive-tm", &ia_tm))
350         reader.read_int_vector("tilemap", &ia_tm);
351
352       reader.read_int_vector("foreground-tm",  &fg_tm);
353
354       { // Read ResetPoints
355         lisp_object_t* cur = 0;
356         if (reader.read_lisp("reset-points",  &cur))
357           {
358             while (!lisp_nil_p(cur))
359               {
360                 lisp_object_t* data = lisp_car(cur);
361
362                 ResetPoint pos;
363
364                 LispReader reader(lisp_cdr(data));
365                 if (reader.read_int("x", &pos.x)
366                     && reader.read_int("y", &pos.y))
367                   {
368                     reset_points.push_back(pos);
369                   }
370
371                 cur = lisp_cdr(cur);
372               }
373           }
374       }
375
376       { // Read BadGuys
377         lisp_object_t* cur = 0;
378         if (reader.read_lisp("objects",  &cur))
379           {
380             while (!lisp_nil_p(cur))
381               {
382                 lisp_object_t* data = lisp_car(cur);
383
384                 BadGuyData bg_data;
385                 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
386                 LispReader reader(lisp_cdr(data));
387                 reader.read_int("x", &bg_data.x);
388                 reader.read_int("y", &bg_data.y);
389                 reader.read_bool("stay-on-platform", &bg_data.stay_on_platform);
390
391                 badguy_data.push_back(bg_data);
392
393                 cur = lisp_cdr(cur);
394               }
395           }
396       }
397
398       // Convert old levels to the new tile numbers
399       if (version == 0)
400         {
401           std::map<char, int> transtable;
402           transtable['.'] = 0;
403           transtable['x'] = 104;
404           transtable['X'] = 77;
405           transtable['y'] = 78;
406           transtable['Y'] = 105;
407           transtable['A'] = 83;
408           transtable['B'] = 102;
409           transtable['!'] = 103;
410           transtable['a'] = 84;
411           transtable['C'] = 85;
412           transtable['D'] = 86;
413           transtable['E'] = 87;
414           transtable['F'] = 88;
415           transtable['c'] = 89;
416           transtable['d'] = 90;
417           transtable['e'] = 91;
418           transtable['f'] = 92;
419
420           transtable['G'] = 93;
421           transtable['H'] = 94;
422           transtable['I'] = 95;
423           transtable['J'] = 96;
424
425           transtable['g'] = 97;
426           transtable['h'] = 98;
427           transtable['i'] = 99;
428           transtable['j'] = 100
429                             ;
430           transtable['#'] = 11;
431           transtable['['] = 13;
432           transtable['='] = 14;
433           transtable[']'] = 15;
434           transtable['$'] = 82;
435           transtable['^'] = 76;
436           transtable['*'] = 80;
437           transtable['|'] = 79;
438           transtable['\\'] = 81;
439           transtable['&'] = 75;
440
441           int x = 0;
442           int y = 0;
443           for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
444             {
445               if (*i == '0' || *i == '1' || *i == '2')
446                 {
447                   badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
448                                                    x*32, y*32, false));
449                   *i = 0;
450                 }
451               else
452                 {
453                   std::map<char, int>::iterator j = transtable.find(*i);
454                   if (j != transtable.end())
455                     *i = j->second;
456                   else
457                     printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
458                 }
459               ++x;
460               if (x >= width)
461                 {
462                   x = 0;
463                   ++y;
464                 }
465             }
466         }
467     }
468
469   for(int i = 0; i < 15; ++i)
470     {
471       ia_tiles[i].resize(width + 1, 0);
472       bg_tiles[i].resize(width + 1, 0);
473       fg_tiles[i].resize(width + 1, 0);
474     }
475
476   int i = 0;
477   int j = 0;
478   for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
479     {
480       ia_tiles[j][i] = (*it);
481       if(i == width - 1)
482         {
483           i = -1;
484           ++j;
485         }
486     }
487
488   i = j = 0;
489   for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
490     {
491
492       bg_tiles[j][i] = (*it);
493       if(i == width - 1)
494         {
495           i = -1;
496           ++j;
497         }
498     }
499
500   i = j = 0;
501   for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
502     {
503
504       fg_tiles[j][i] = (*it);
505       if(i == width - 1)
506         {
507           i = -1;
508           ++j;
509         }
510     }
511
512   lisp_free(root_obj);
513   fclose(fi);
514   return 0;
515 }
516
517 /* Save data for level: */
518
519 void 
520 Level::save(const  char * subset, int level)
521 {
522   char filename[1024];
523   char str[80];
524
525   /* Save data file: */
526   sprintf(str, "/levels/%s/", subset);
527   fcreatedir(str);
528   snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
529   if(!fwriteable(filename))
530     snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
531
532   FILE * fi = fopen(filename, "w");
533   if (fi == NULL)
534     {
535       perror(filename);
536       st_shutdown();
537       exit(-1);
538     }
539
540
541   /* Write header: */
542   fprintf(fi,";SuperTux-Level\n");
543   fprintf(fi,"(supertux-level\n");
544
545   fprintf(fi,"  (version %d)\n", 1);
546   fprintf(fi,"  (name \"%s\")\n", name.c_str());
547   fprintf(fi,"  (author \"%s\")\n", author.c_str());
548   fprintf(fi,"  (theme \"%s\")\n", theme.c_str());
549   fprintf(fi,"  (music \"%s\")\n", song_title.c_str());
550   fprintf(fi,"  (background \"%s\")\n", bkgd_image.c_str());
551   fprintf(fi,"  (particle_system \"%s\")\n", particle_system.c_str());
552   fprintf(fi,"  (bkgd_red_top %d)\n", bkgd_top.red);
553   fprintf(fi,"  (bkgd_green_top %d)\n", bkgd_top.green);
554   fprintf(fi,"  (bkgd_blue_top %d)\n", bkgd_top.blue);
555   fprintf(fi,"  (bkgd_red_bottom %d)\n", bkgd_bottom.red);
556   fprintf(fi,"  (bkgd_green_bottom %d)\n", bkgd_bottom.green);
557   fprintf(fi,"  (bkgd_blue_bottom %d)\n", bkgd_bottom.blue);
558   fprintf(fi,"  (time %d)\n", time_left);
559   fprintf(fi,"  (width %d)\n", width);
560   fprintf(fi,"  (gravity %2.1f)\n", gravity);
561   fprintf(fi,"  (background-tm ");
562
563   for(int y = 0; y < 15; ++y)
564     {
565       for(int i = 0; i < width; ++i)
566         fprintf(fi," %d ", bg_tiles[y][i]);
567     }
568
569   fprintf( fi,")\n");
570   fprintf(fi,"  (interactive-tm ");
571
572   for(int y = 0; y < 15; ++y)
573     {
574       for(int i = 0; i < width; ++i)
575         fprintf(fi," %d ", ia_tiles[y][i]);
576     }
577
578   fprintf( fi,")\n");
579   fprintf(fi,"  (foreground-tm ");
580
581   for(int y = 0; y < 15; ++y)
582     {
583       for(int i = 0; i < width; ++i)
584         fprintf(fi," %d ", fg_tiles[y][i]);
585     }
586
587   fprintf( fi,")\n");
588
589   fprintf( fi,"(reset-points\n");
590   for(std::vector<ResetPoint>::iterator i = reset_points.begin();
591       i != reset_points.end(); ++i)
592     fprintf( fi,"(point (x %d) (y %d))\n",i->x, i->y);
593   fprintf( fi,")\n");
594
595   fprintf( fi,"(objects\n");
596
597   for(std::vector<BadGuyData>::iterator it = badguy_data.begin();
598       it != badguy_data.end();
599       ++it)
600     fprintf( fi,"(%s (x %d) (y %d) (stay-on-platform %s))\n",
601              badguykind_to_string((*it).kind).c_str(),(*it).x,(*it).y,
602              it->stay_on_platform ? "#t" : "#f");
603
604   fprintf( fi,")\n");
605
606   fprintf( fi,")\n");
607
608   fclose(fi);
609 }
610
611
612 /* Unload data for this level: */
613
614 void
615 Level::cleanup()
616 {
617   for(int i=0; i < 15; ++i)
618     {
619       bg_tiles[i].clear();
620       ia_tiles[i].clear();
621       fg_tiles[i].clear();
622     }
623
624   reset_points.clear();
625   name.clear();
626   author.clear();
627   theme.clear();
628   song_title.clear();
629   bkgd_image.clear();
630
631   badguy_data.clear();
632 }
633
634 void 
635 Level::load_gfx()
636 {
637   if(!bkgd_image.empty())
638     {
639       char fname[1024];
640       snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
641       if(!faccessible(fname))
642         snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
643       img_bkgd = new Surface(fname, IGNORE_ALPHA);
644     }
645   else
646     {
647       img_bkgd = 0;
648     }
649 }
650
651 void
652 Level::free_gfx()
653 {
654   delete img_bkgd;
655 }
656
657 /* Load a level-specific graphic... */
658 void
659 Level::load_image(Surface** ptexture, string theme,const  char * file, int use_alpha)
660 {
661   char fname[1024];
662
663   snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
664   if(!faccessible(fname))
665     snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
666
667   *ptexture = new Surface(fname, use_alpha);
668 }
669
670 /* Change the size of a level (width) */
671 void 
672 Level::change_size (int new_width)
673 {
674   if(new_width < 21)
675     new_width = 21;
676
677   for(int y = 0; y < 15; ++y)
678     {
679       ia_tiles[y].resize(new_width, 0);
680       bg_tiles[y].resize(new_width, 0);
681       fg_tiles[y].resize(new_width, 0);
682     }
683
684   width = new_width;
685 }
686
687 void
688 Level::change(float x, float y, int tm, unsigned int c)
689 {
690   int yy = ((int)y / 32);
691   int xx = ((int)x / 32);
692
693   if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
694     {
695       switch(tm)
696         {
697         case TM_BG:
698           bg_tiles[yy][xx] = c;
699           break;
700         case TM_IA:
701           ia_tiles[yy][xx] = c;
702           break;
703         case TM_FG:
704           fg_tiles[yy][xx] = c;
705           break;
706         }
707     }
708 }
709
710 void
711 Level::load_song()
712 {
713   char* song_path;
714   char* song_subtitle;
715
716   level_song = music_manager->load_music(datadir + "/music/" + song_title);
717
718   song_path = (char *) malloc(sizeof(char) * datadir.length() +
719                               strlen(song_title.c_str()) + 8 + 5);
720   song_subtitle = strdup(song_title.c_str());
721   strcpy(strstr(song_subtitle, "."), "\0");
722   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), 
723           song_subtitle, strstr(song_title.c_str(), "."));
724   if(!music_manager->exists_music(song_path)) {
725     level_song_fast = level_song;
726   } else {
727     level_song_fast = music_manager->load_music(song_path);
728   }
729   free(song_subtitle);
730   free(song_path);
731 }
732
733 MusicRef
734 Level::get_level_music()
735 {
736   return level_song;
737 }
738
739 MusicRef
740 Level::get_level_music_fast()
741 {
742   return level_song_fast;
743 }
744
745 unsigned int 
746 Level::gettileid(float x, float y)
747 {
748   int xx, yy;
749   unsigned int c;
750
751   yy = ((int)y / 32);
752   xx = ((int)x / 32);
753
754   if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
755     c = ia_tiles[yy][xx];
756   else
757     c = 0;
758
759   return c;
760 }
761
762 /* EOF */