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