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"
34 #include "resources.h"
35 #include "music_manager.h"
39 st_subset::st_subset()
44 void st_subset::create(const std::string& subset_name)
48 new_subset.name = subset_name;
49 new_subset.title = "Unknown Title";
50 new_subset.description = "No description so far.";
52 new_lev.init_defaults();
53 new_lev.save(subset_name.c_str(),1);
56 void st_subset::parse (lisp_object_t* cursor)
58 while(!lisp_nil_p(cursor))
60 lisp_object_t* cur = lisp_car(cursor);
63 if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
69 if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
71 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
76 else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
78 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
84 cursor = lisp_cdr (cursor);
88 void st_subset::load(char *subset)
94 lisp_object_t* root_obj = 0;
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))
103 fi = fopen(filename, "r");
108 lisp_stream_t stream;
109 lisp_stream_init_file (&stream, fi);
110 root_obj = lisp_read (&stream);
112 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
114 printf("World: Parse Error in file %s", filename);
117 lisp_object_t* cur = lisp_car(root_obj);
119 if (!lisp_symbol_p (cur))
121 printf("World: Read error in %s",filename);
124 if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
126 parse(lisp_cdr(root_obj));
133 snprintf(str, 1024, "%s.png", filename);
136 image = new Surface(str,IGNORE_ALPHA);
140 snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
141 image = new Surface(filename,IGNORE_ALPHA);
145 for(i=1; i != -1; ++i)
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))
151 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
152 if(!faccessible(filename))
159 void st_subset::save()
164 /* Save data file: */
165 filename = "/levels/" + name + "/";
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()))
173 fi = fopen(filename.c_str(), "w");
176 perror(filename.c_str());
180 fprintf(fi,";SuperTux-Level-Subset\n");
181 fprintf(fi,"(supertux-level-subset\n");
183 /* Save title info: */
184 fprintf(fi," (title \"%s\")\n", title.c_str());
186 /* Save the description: */
187 fprintf(fi," (description \"%s\")\n", description.c_str());
195 void st_subset::free()
210 Level::Level(const std::string& subset, int level)
216 Level::Level(const std::string& filename)
228 Level::init_defaults()
232 theme = "antarctica";
233 song_title = "Mortimers_chipdisko.mod";
234 bkgd_image = "arctis.png";
243 bkgd_bottom.red = 255;
244 bkgd_bottom.green = 255;
245 bkgd_bottom.blue = 255;
247 use_endsequence = false;
249 for(int i = 0; i < 15; ++i)
251 ia_tiles[i].resize(width+1, 0);
252 ia_tiles[i][width] = (unsigned int) '\0';
254 for(int y = 0; y < width; ++y)
257 bg_tiles[i].resize(width+1, 0);
258 bg_tiles[i][width] = (unsigned int) '\0';
259 for(int y = 0; y < width; ++y)
262 fg_tiles[i].resize(width+1, 0);
263 fg_tiles[i][width] = (unsigned int) '\0';
264 for(int y = 0; y < width; ++y)
270 Level::load(const std::string& subset, int level)
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);
279 return load(filename);
283 Level::load(const std::string& filename)
286 lisp_object_t* root_obj = 0;
287 fi = fopen(filename.c_str(), "r");
290 perror(filename.c_str());
294 lisp_stream_t stream;
295 lisp_stream_init_file (&stream, fi);
296 root_obj = lisp_read (&stream);
298 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
300 printf("World: Parse Error in file %s", filename.c_str());
308 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
310 LispReader reader(lisp_cdr(root_obj));
311 reader.read_int("version", &version);
312 reader.read_bool("use-endsequence", &use_endsequence);
313 reader.read_int("width", &width);
314 if (!reader.read_int("start_pos_x", &start_pos_x)) start_pos_x = 100;
315 if (!reader.read_int("start_pos_y", &start_pos_y)) start_pos_y = 170;
316 reader.read_int("time", &time_left);
318 reader.read_int("bkgd_red_top", &bkgd_top.red);
319 reader.read_int("bkgd_green_top", &bkgd_top.green);
320 reader.read_int("bkgd_blue_top", &bkgd_top.blue);
322 reader.read_int("bkgd_red_bottom", &bkgd_bottom.red);
323 reader.read_int("bkgd_green_bottom", &bkgd_bottom.green);
324 reader.read_int("bkgd_blue_bottom", &bkgd_bottom.blue);
326 reader.read_float("gravity", &gravity);
327 reader.read_string("name", &name);
328 reader.read_string("author", &author);
329 reader.read_string("theme", &theme);
330 reader.read_string("music", &song_title);
331 reader.read_string("background", &bkgd_image);
332 reader.read_string("particle_system", &particle_system);
333 reader.read_int_vector("background-tm", &bg_tm);
335 if (!reader.read_int_vector("interactive-tm", &ia_tm))
336 reader.read_int_vector("tilemap", &ia_tm);
338 reader.read_int_vector("foreground-tm", &fg_tm);
340 { // Read ResetPoints
341 lisp_object_t* cur = 0;
342 if (reader.read_lisp("reset-points", &cur))
344 while (!lisp_nil_p(cur))
346 lisp_object_t* data = lisp_car(cur);
350 LispReader reader(lisp_cdr(data));
351 if (reader.read_int("x", &pos.x)
352 && reader.read_int("y", &pos.y))
354 reset_points.push_back(pos);
363 lisp_object_t* cur = 0;
364 if (reader.read_lisp("objects", &cur))
366 while (!lisp_nil_p(cur))
368 lisp_object_t* data = lisp_car(cur);
371 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
372 LispReader reader(lisp_cdr(data));
373 reader.read_int("x", &bg_data.x);
374 reader.read_int("y", &bg_data.y);
375 reader.read_bool("stay-on-platform", &bg_data.stay_on_platform);
377 badguy_data.push_back(bg_data);
384 // Convert old levels to the new tile numbers
387 std::map<char, int> transtable;
389 transtable['x'] = 104;
390 transtable['X'] = 77;
391 transtable['y'] = 78;
392 transtable['Y'] = 105;
393 transtable['A'] = 83;
394 transtable['B'] = 102;
395 transtable['!'] = 103;
396 transtable['a'] = 84;
397 transtable['C'] = 85;
398 transtable['D'] = 86;
399 transtable['E'] = 87;
400 transtable['F'] = 88;
401 transtable['c'] = 89;
402 transtable['d'] = 90;
403 transtable['e'] = 91;
404 transtable['f'] = 92;
406 transtable['G'] = 93;
407 transtable['H'] = 94;
408 transtable['I'] = 95;
409 transtable['J'] = 96;
411 transtable['g'] = 97;
412 transtable['h'] = 98;
413 transtable['i'] = 99;
414 transtable['j'] = 100
416 transtable['#'] = 11;
417 transtable['['] = 13;
418 transtable['='] = 14;
419 transtable[']'] = 15;
420 transtable['$'] = 82;
421 transtable['^'] = 76;
422 transtable['*'] = 80;
423 transtable['|'] = 79;
424 transtable['\\'] = 81;
425 transtable['&'] = 75;
429 for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
431 if (*i == '0' || *i == '1' || *i == '2')
433 badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
439 std::map<char, int>::iterator j = transtable.find(*i);
440 if (j != transtable.end())
443 printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
455 for(int i = 0; i < 15; ++i)
457 ia_tiles[i].resize(width + 1, 0);
458 bg_tiles[i].resize(width + 1, 0);
459 fg_tiles[i].resize(width + 1, 0);
464 for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
466 ia_tiles[j][i] = (*it);
475 for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
478 bg_tiles[j][i] = (*it);
487 for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
490 fg_tiles[j][i] = (*it);
498 // Mark the end position of this level!
499 // FIXME: -10 is a rather random value, we still need some kind of
500 // real levelend gola
502 endpos = 32*(width-30);
504 endpos = 32*(width-15);
511 /* Save data for level: */
514 Level::save(const char * subset, int level)
519 /* Save data file: */
520 sprintf(str, "/levels/%s/", subset);
522 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
523 if(!fwriteable(filename))
524 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
526 FILE * fi = fopen(filename, "w");
536 fprintf(fi,";SuperTux-Level\n");
537 fprintf(fi,"(supertux-level\n");
539 fprintf(fi," (version %d)\n", 1);
540 fprintf(fi," (name \"%s\")\n", name.c_str());
541 fprintf(fi," (author \"%s\")\n", author.c_str());
542 fprintf(fi," (theme \"%s\")\n", theme.c_str());
543 fprintf(fi," (music \"%s\")\n", song_title.c_str());
544 fprintf(fi," (background \"%s\")\n", bkgd_image.c_str());
545 fprintf(fi," (particle_system \"%s\")\n", particle_system.c_str());
546 fprintf(fi," (bkgd_red_top %d)\n", bkgd_top.red);
547 fprintf(fi," (bkgd_green_top %d)\n", bkgd_top.green);
548 fprintf(fi," (bkgd_blue_top %d)\n", bkgd_top.blue);
549 fprintf(fi," (bkgd_red_bottom %d)\n", bkgd_bottom.red);
550 fprintf(fi," (bkgd_green_bottom %d)\n", bkgd_bottom.green);
551 fprintf(fi," (bkgd_blue_bottom %d)\n", bkgd_bottom.blue);
552 fprintf(fi," (time %d)\n", time_left);
553 fprintf(fi," (width %d)\n", width);
554 fprintf(fi," (gravity %2.1f)\n", gravity);
555 fprintf(fi," (background-tm ");
557 for(int y = 0; y < 15; ++y)
559 for(int i = 0; i < width; ++i)
560 fprintf(fi," %d ", bg_tiles[y][i]);
564 fprintf(fi," (interactive-tm ");
566 for(int y = 0; y < 15; ++y)
568 for(int i = 0; i < width; ++i)
569 fprintf(fi," %d ", ia_tiles[y][i]);
573 fprintf(fi," (foreground-tm ");
575 for(int y = 0; y < 15; ++y)
577 for(int i = 0; i < width; ++i)
578 fprintf(fi," %d ", fg_tiles[y][i]);
583 fprintf( fi,"(reset-points\n");
584 for(std::vector<ResetPoint>::iterator i = reset_points.begin();
585 i != reset_points.end(); ++i)
586 fprintf( fi,"(point (x %d) (y %d))\n",i->x, i->y);
589 fprintf( fi,"(objects\n");
591 for(std::vector<BadGuyData>::iterator it = badguy_data.begin();
592 it != badguy_data.end();
594 fprintf( fi,"(%s (x %d) (y %d) (stay-on-platform %s))\n",
595 badguykind_to_string((*it).kind).c_str(),(*it).x,(*it).y,
596 it->stay_on_platform ? "#t" : "#f");
606 /* Unload data for this level: */
611 for(int i=0; i < 15; ++i)
618 reset_points.clear();
631 if(!bkgd_image.empty())
634 snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
635 if(!faccessible(fname))
636 snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
637 img_bkgd = new Surface(fname, IGNORE_ALPHA);
651 /* Load a level-specific graphic... */
653 Level::load_image(Surface** ptexture, string theme,const char * file, int use_alpha)
657 snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
658 if(!faccessible(fname))
659 snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
661 *ptexture = new Surface(fname, use_alpha);
664 /* Change the size of a level (width) */
666 Level::change_size (int new_width)
671 for(int y = 0; y < 15; ++y)
673 ia_tiles[y].resize(new_width, 0);
674 bg_tiles[y].resize(new_width, 0);
675 fg_tiles[y].resize(new_width, 0);
682 Level::change(float x, float y, int tm, unsigned int c)
684 int yy = ((int)y / 32);
685 int xx = ((int)x / 32);
687 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
692 bg_tiles[yy][xx] = c;
695 ia_tiles[yy][xx] = c;
698 fg_tiles[yy][xx] = c;
710 level_song = music_manager->load_music(datadir + "/music/" + song_title);
712 song_path = (char *) malloc(sizeof(char) * datadir.length() +
713 strlen(song_title.c_str()) + 8 + 5);
714 song_subtitle = strdup(song_title.c_str());
715 strcpy(strstr(song_subtitle, "."), "\0");
716 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
717 song_subtitle, strstr(song_title.c_str(), "."));
718 if(!music_manager->exists_music(song_path)) {
719 level_song_fast = level_song;
721 level_song_fast = music_manager->load_music(song_path);
728 Level::get_level_music()
734 Level::get_level_music_fast()
736 return level_song_fast;
740 Level::gettileid(float x, float y)
748 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
749 c = ia_tiles[yy][xx];