4 // Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
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.
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.
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
33 #include "lispreader.h"
37 st_subset::st_subset()
42 void st_subset::create(const std::string& subset_name)
46 new_subset.name = subset_name;
47 new_subset.title = "Unknown Title";
48 new_subset.description = "No description so far.";
50 new_lev.init_defaults();
51 new_lev.save(subset_name.c_str(),1);
54 void st_subset::parse (lisp_object_t* cursor)
56 while(!lisp_nil_p(cursor))
58 lisp_object_t* cur = lisp_car(cursor);
61 if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
67 if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
69 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
74 else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
76 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
82 cursor = lisp_cdr (cursor);
86 void st_subset::load(char *subset)
92 lisp_object_t* root_obj = 0;
96 snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
97 if(!faccessible(filename))
98 snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
99 if(faccessible(filename))
101 fi = fopen(filename, "r");
106 lisp_stream_t stream;
107 lisp_stream_init_file (&stream, fi);
108 root_obj = lisp_read (&stream);
110 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
112 printf("World: Parse Error in file %s", filename);
115 lisp_object_t* cur = lisp_car(root_obj);
117 if (!lisp_symbol_p (cur))
119 printf("World: Read error in %s",filename);
122 if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
124 parse(lisp_cdr(root_obj));
130 snprintf(str, 1024, "%s.png", filename);
133 image = new Surface(str,IGNORE_ALPHA);
137 snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
138 image = new Surface(filename,IGNORE_ALPHA);
142 for(i=1; i != -1; ++i)
144 /* Get the number of levels in this subset */
145 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
146 if(!faccessible(filename))
148 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
149 if(!faccessible(filename))
156 void st_subset::save()
161 /* Save data file: */
162 filename = "/levels/" + name + "/";
164 fcreatedir(filename.c_str());
165 filename = string(st_dir) + "/levels/" + name + "/info";
166 if(!fwriteable(filename.c_str()))
167 filename = datadir + "/levels/" + name + "/info";
168 if(fwriteable(filename.c_str()))
170 fi = fopen(filename.c_str(), "w");
173 perror(filename.c_str());
177 fprintf(fi,";SuperTux-Level-Subset\n");
178 fprintf(fi,"(supertux-level-subset\n");
180 /* Save title info: */
181 fprintf(fi," (title \"%s\")\n", title.c_str());
183 /* Save the description: */
184 fprintf(fi," (description \"%s\")\n", description.c_str());
192 void st_subset::free()
202 : level_song(0), level_song_fast(0)
206 Level::Level(const std::string& subset, int level)
207 : level_song(0), level_song_fast(0)
212 Level::Level(const std::string& filename)
213 : level_song(0), level_song_fast(0)
225 Level::init_defaults()
229 theme = "antarctica";
230 song_title = "Mortimers_chipdisko.mod";
231 bkgd_image = "arctis.png";
240 bkgd_bottom.red = 255;
241 bkgd_bottom.green = 255;
242 bkgd_bottom.blue = 255;
245 for(int i = 0; i < 15; ++i)
247 ia_tiles[i].resize(width+1, 0);
248 ia_tiles[i][width] = (unsigned int) '\0';
250 for(int y = 0; y < width; ++y)
253 bg_tiles[i].resize(width+1, 0);
254 bg_tiles[i][width] = (unsigned int) '\0';
255 for(int y = 0; y < width; ++y)
258 fg_tiles[i].resize(width+1, 0);
259 fg_tiles[i][width] = (unsigned int) '\0';
260 for(int y = 0; y < width; ++y)
266 Level::load(const std::string& subset, int level)
271 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(), level);
272 if(!faccessible(filename))
273 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset.c_str(), level);
275 return load(filename);
279 Level::load(const std::string& filename)
282 lisp_object_t* root_obj = 0;
283 fi = fopen(filename.c_str(), "r");
286 perror(filename.c_str());
290 lisp_stream_t stream;
291 lisp_stream_init_file (&stream, fi);
292 root_obj = lisp_read (&stream);
294 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
296 printf("World: Parse Error in file %s", filename.c_str());
304 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
306 LispReader reader(lisp_cdr(root_obj));
307 reader.read_int("version", &version);
308 reader.read_int("width", &width);
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 reader.read_int("time", &time_left);
313 reader.read_int("bkgd_top_red", &bkgd_top.red);
314 reader.read_int("bkgd_top_green", &bkgd_top.green);
315 reader.read_int("bkgd_top_blue", &bkgd_top.blue);
317 reader.read_int("bkgd_bottom_red", &bkgd_bottom.red);
318 reader.read_int("bkgd_bottom_green", &bkgd_bottom.green);
319 reader.read_int("bkgd_bottom_blue", &bkgd_bottom.blue);
321 reader.read_float("gravity", &gravity);
322 reader.read_string("name", &name);
323 reader.read_string("author", &author);
324 reader.read_string("theme", &theme);
325 reader.read_string("music", &song_title);
326 reader.read_string("background", &bkgd_image);
327 reader.read_string("particle_system", &particle_system);
328 reader.read_int_vector("background-tm", &bg_tm);
330 if (!reader.read_int_vector("interactive-tm", &ia_tm))
331 reader.read_int_vector("tilemap", &ia_tm);
333 reader.read_int_vector("foreground-tm", &fg_tm);
335 { // Read ResetPoints
336 lisp_object_t* cur = 0;
337 if (reader.read_lisp("reset-points", &cur))
339 while (!lisp_nil_p(cur))
341 lisp_object_t* data = lisp_car(cur);
345 LispReader reader(lisp_cdr(data));
346 if (reader.read_int("x", &pos.x)
347 && reader.read_int("y", &pos.y))
349 reset_points.push_back(pos);
358 lisp_object_t* cur = 0;
359 if (reader.read_lisp("objects", &cur))
361 while (!lisp_nil_p(cur))
363 lisp_object_t* data = lisp_car(cur);
366 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
367 LispReader reader(lisp_cdr(data));
368 reader.read_int("x", &bg_data.x);
369 reader.read_int("y", &bg_data.y);
370 reader.read_bool("stay-on-platform", &bg_data.stay_on_platform);
372 badguy_data.push_back(bg_data);
379 // Convert old levels to the new tile numbers
382 std::map<char, int> transtable;
384 transtable['x'] = 104;
385 transtable['X'] = 77;
386 transtable['y'] = 78;
387 transtable['Y'] = 105;
388 transtable['A'] = 83;
389 transtable['B'] = 102;
390 transtable['!'] = 103;
391 transtable['a'] = 84;
392 transtable['C'] = 85;
393 transtable['D'] = 86;
394 transtable['E'] = 87;
395 transtable['F'] = 88;
396 transtable['c'] = 89;
397 transtable['d'] = 90;
398 transtable['e'] = 91;
399 transtable['f'] = 92;
401 transtable['G'] = 93;
402 transtable['H'] = 94;
403 transtable['I'] = 95;
404 transtable['J'] = 96;
406 transtable['g'] = 97;
407 transtable['h'] = 98;
408 transtable['i'] = 99;
409 transtable['j'] = 100
411 transtable['#'] = 11;
412 transtable['['] = 13;
413 transtable['='] = 14;
414 transtable[']'] = 15;
415 transtable['$'] = 82;
416 transtable['^'] = 76;
417 transtable['*'] = 80;
418 transtable['|'] = 79;
419 transtable['\\'] = 81;
420 transtable['&'] = 75;
424 for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
426 if (*i == '0' || *i == '1' || *i == '2')
428 badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
434 std::map<char, int>::iterator j = transtable.find(*i);
435 if (j != transtable.end())
438 printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
450 for(int i = 0; i < 15; ++i)
452 ia_tiles[i].resize(width + 1, 0);
453 bg_tiles[i].resize(width + 1, 0);
454 fg_tiles[i].resize(width + 1, 0);
459 for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
461 ia_tiles[j][i] = (*it);
470 for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
473 bg_tiles[j][i] = (*it);
482 for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
485 fg_tiles[j][i] = (*it);
493 // Mark the end position of this level!
494 // FIXME: -10 is a rather random value, we still need some kind of
495 // real levelend gola
496 endpos = 32*(width-20);
502 /* Save data for level: */
505 Level::save(const char * subset, int level)
510 /* Save data file: */
511 sprintf(str, "/levels/%s/", subset);
513 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
514 if(!fwriteable(filename))
515 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
517 FILE * fi = fopen(filename, "w");
527 fprintf(fi,";SuperTux-Level\n");
528 fprintf(fi,"(supertux-level\n");
530 fprintf(fi," (version %d)\n", 1);
531 fprintf(fi," (name \"%s\")\n", name.c_str());
532 fprintf(fi," (author \"%s\")\n", author.c_str());
533 fprintf(fi," (theme \"%s\")\n", theme.c_str());
534 fprintf(fi," (music \"%s\")\n", song_title.c_str());
535 fprintf(fi," (background \"%s\")\n", bkgd_image.c_str());
536 fprintf(fi," (particle_system \"%s\")\n", particle_system.c_str());
537 fprintf(fi," (bkgd_top_red %d)\n", bkgd_top.red);
538 fprintf(fi," (bkgd_top_green %d)\n", bkgd_top.green);
539 fprintf(fi," (bkgd_top_blue %d)\n", bkgd_top.blue);
540 fprintf(fi," (bkgd_bottom_red %d)\n", bkgd_bottom.red);
541 fprintf(fi," (bkgd_bottom_green %d)\n", bkgd_bottom.green);
542 fprintf(fi," (bkgd_bottom_blue %d)\n", bkgd_bottom.blue);
543 fprintf(fi," (time %d)\n", time_left);
544 fprintf(fi," (width %d)\n", width);
545 fprintf(fi," (gravity %2.1f)\n", gravity);
546 fprintf(fi," (background-tm ");
548 for(int y = 0; y < 15; ++y)
550 for(int i = 0; i < width; ++i)
551 fprintf(fi," %d ", bg_tiles[y][i]);
555 fprintf(fi," (interactive-tm ");
557 for(int y = 0; y < 15; ++y)
559 for(int i = 0; i < width; ++i)
560 fprintf(fi," %d ", ia_tiles[y][i]);
564 fprintf(fi," (foreground-tm ");
566 for(int y = 0; y < 15; ++y)
568 for(int i = 0; i < width; ++i)
569 fprintf(fi," %d ", fg_tiles[y][i]);
574 fprintf( fi,"(reset-points\n");
575 for(std::vector<ResetPoint>::iterator i = reset_points.begin();
576 i != reset_points.end(); ++i)
577 fprintf( fi,"(point (x %d) (y %d))\n",i->x, i->y);
580 fprintf( fi,"(objects\n");
582 for(std::vector<BadGuyData>::iterator it = badguy_data.begin();
583 it != badguy_data.end();
585 fprintf( fi,"(%s (x %d) (y %d) (stay-on-platform %s))\n",
586 badguykind_to_string((*it).kind).c_str(),(*it).x,(*it).y,
587 it->stay_on_platform ? "#t" : "#f");
597 /* Unload data for this level: */
602 for(int i=0; i < 15; ++i)
609 reset_points.clear();
622 if(!bkgd_image.empty())
625 snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
626 if(!faccessible(fname))
627 snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
628 img_bkgd = new Surface(fname, IGNORE_ALPHA);
632 /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */
633 load_image(&img_bkgd, theme,"solid0.png", IGNORE_ALPHA);
643 /* Load a level-specific graphic... */
645 Level::load_image(Surface** ptexture, string theme,const char * file, int use_alpha)
649 snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
650 if(!faccessible(fname))
651 snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
653 *ptexture = new Surface(fname, use_alpha);
656 /* Change the size of a level (width) */
658 Level::change_size (int new_width)
663 for(int y = 0; y < 15; ++y)
665 ia_tiles[y].resize(new_width, 0);
666 bg_tiles[y].resize(new_width, 0);
667 fg_tiles[y].resize(new_width, 0);
674 Level::change(float x, float y, int tm, unsigned int c)
676 int yy = ((int)y / 32);
677 int xx = ((int)x / 32);
679 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
684 bg_tiles[yy][xx] = c;
687 ia_tiles[yy][xx] = c;
690 fg_tiles[yy][xx] = c;
697 Level::free_song(void)
699 free_music(level_song);
701 free_music(level_song_fast);
713 level_song = ::load_song(datadir + "/music/" + song_title);
715 song_path = (char *) malloc(sizeof(char) * datadir.length() +
716 strlen(song_title.c_str()) + 8 + 5);
717 song_subtitle = strdup(song_title.c_str());
718 strcpy(strstr(song_subtitle, "."), "\0");
719 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
720 song_subtitle, strstr(song_title.c_str(), "."));
721 level_song_fast = ::load_song(song_path);
727 Level::get_level_music()
733 Level::get_level_music_fast()
735 return level_song_fast;
739 Level::gettileid(float x, float y)
747 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
748 c = ia_tiles[yy][xx];