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 : img_bkgd(0), level_song(0), level_song_fast(0)
206 Level::Level(const std::string& subset, int level)
207 : img_bkgd(0), level_song(0), level_song_fast(0)
212 Level::Level(const std::string& filename)
213 : img_bkgd(0), 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;
244 use_endsequence = false;
246 for(int i = 0; i < 15; ++i)
248 ia_tiles[i].resize(width+1, 0);
249 ia_tiles[i][width] = (unsigned int) '\0';
251 for(int y = 0; y < width; ++y)
254 bg_tiles[i].resize(width+1, 0);
255 bg_tiles[i][width] = (unsigned int) '\0';
256 for(int y = 0; y < width; ++y)
259 fg_tiles[i].resize(width+1, 0);
260 fg_tiles[i][width] = (unsigned int) '\0';
261 for(int y = 0; y < width; ++y)
267 Level::load(const std::string& subset, int level)
272 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(), level);
273 if(!faccessible(filename))
274 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset.c_str(), level);
276 return load(filename);
280 Level::load(const std::string& filename)
283 lisp_object_t* root_obj = 0;
284 fi = fopen(filename.c_str(), "r");
287 perror(filename.c_str());
291 lisp_stream_t stream;
292 lisp_stream_init_file (&stream, fi);
293 root_obj = lisp_read (&stream);
295 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
297 printf("World: Parse Error in file %s", filename.c_str());
305 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
307 LispReader reader(lisp_cdr(root_obj));
308 reader.read_int("version", &version);
309 reader.read_bool("use-endsequence", &use_endsequence);
310 reader.read_int("width", &width);
311 if (!reader.read_int("start_pos_x", &start_pos_x)) start_pos_x = 100;
312 if (!reader.read_int("start_pos_y", &start_pos_y)) start_pos_y = 170;
313 reader.read_int("time", &time_left);
315 reader.read_int("bkgd_top_red", &bkgd_top.red);
316 reader.read_int("bkgd_top_green", &bkgd_top.green);
317 reader.read_int("bkgd_top_blue", &bkgd_top.blue);
319 reader.read_int("bkgd_bottom_red", &bkgd_bottom.red);
320 reader.read_int("bkgd_bottom_green", &bkgd_bottom.green);
321 reader.read_int("bkgd_bottom_blue", &bkgd_bottom.blue);
323 reader.read_float("gravity", &gravity);
324 reader.read_string("name", &name);
325 reader.read_string("author", &author);
326 reader.read_string("theme", &theme);
327 reader.read_string("music", &song_title);
328 reader.read_string("background", &bkgd_image);
329 reader.read_string("particle_system", &particle_system);
330 reader.read_int_vector("background-tm", &bg_tm);
332 if (!reader.read_int_vector("interactive-tm", &ia_tm))
333 reader.read_int_vector("tilemap", &ia_tm);
335 reader.read_int_vector("foreground-tm", &fg_tm);
337 { // Read ResetPoints
338 lisp_object_t* cur = 0;
339 if (reader.read_lisp("reset-points", &cur))
341 while (!lisp_nil_p(cur))
343 lisp_object_t* data = lisp_car(cur);
347 LispReader reader(lisp_cdr(data));
348 if (reader.read_int("x", &pos.x)
349 && reader.read_int("y", &pos.y))
351 reset_points.push_back(pos);
360 lisp_object_t* cur = 0;
361 if (reader.read_lisp("objects", &cur))
363 while (!lisp_nil_p(cur))
365 lisp_object_t* data = lisp_car(cur);
368 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
369 LispReader reader(lisp_cdr(data));
370 reader.read_int("x", &bg_data.x);
371 reader.read_int("y", &bg_data.y);
372 reader.read_bool("stay-on-platform", &bg_data.stay_on_platform);
374 badguy_data.push_back(bg_data);
381 // Convert old levels to the new tile numbers
384 std::map<char, int> transtable;
386 transtable['x'] = 104;
387 transtable['X'] = 77;
388 transtable['y'] = 78;
389 transtable['Y'] = 105;
390 transtable['A'] = 83;
391 transtable['B'] = 102;
392 transtable['!'] = 103;
393 transtable['a'] = 84;
394 transtable['C'] = 85;
395 transtable['D'] = 86;
396 transtable['E'] = 87;
397 transtable['F'] = 88;
398 transtable['c'] = 89;
399 transtable['d'] = 90;
400 transtable['e'] = 91;
401 transtable['f'] = 92;
403 transtable['G'] = 93;
404 transtable['H'] = 94;
405 transtable['I'] = 95;
406 transtable['J'] = 96;
408 transtable['g'] = 97;
409 transtable['h'] = 98;
410 transtable['i'] = 99;
411 transtable['j'] = 100
413 transtable['#'] = 11;
414 transtable['['] = 13;
415 transtable['='] = 14;
416 transtable[']'] = 15;
417 transtable['$'] = 82;
418 transtable['^'] = 76;
419 transtable['*'] = 80;
420 transtable['|'] = 79;
421 transtable['\\'] = 81;
422 transtable['&'] = 75;
426 for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
428 if (*i == '0' || *i == '1' || *i == '2')
430 badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
436 std::map<char, int>::iterator j = transtable.find(*i);
437 if (j != transtable.end())
440 printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
452 for(int i = 0; i < 15; ++i)
454 ia_tiles[i].resize(width + 1, 0);
455 bg_tiles[i].resize(width + 1, 0);
456 fg_tiles[i].resize(width + 1, 0);
461 for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
463 ia_tiles[j][i] = (*it);
472 for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
475 bg_tiles[j][i] = (*it);
484 for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
487 fg_tiles[j][i] = (*it);
495 // Mark the end position of this level!
496 // FIXME: -10 is a rather random value, we still need some kind of
497 // real levelend gola
499 endpos = 32*(width-20);
501 endpos = 32*(width-15);
507 /* Save data for level: */
510 Level::save(const char * subset, int level)
515 /* Save data file: */
516 sprintf(str, "/levels/%s/", subset);
518 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
519 if(!fwriteable(filename))
520 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
522 FILE * fi = fopen(filename, "w");
532 fprintf(fi,";SuperTux-Level\n");
533 fprintf(fi,"(supertux-level\n");
535 fprintf(fi," (version %d)\n", 1);
536 fprintf(fi," (name \"%s\")\n", name.c_str());
537 fprintf(fi," (author \"%s\")\n", author.c_str());
538 fprintf(fi," (theme \"%s\")\n", theme.c_str());
539 fprintf(fi," (music \"%s\")\n", song_title.c_str());
540 fprintf(fi," (background \"%s\")\n", bkgd_image.c_str());
541 fprintf(fi," (particle_system \"%s\")\n", particle_system.c_str());
542 fprintf(fi," (bkgd_top_red %d)\n", bkgd_top.red);
543 fprintf(fi," (bkgd_top_green %d)\n", bkgd_top.green);
544 fprintf(fi," (bkgd_top_blue %d)\n", bkgd_top.blue);
545 fprintf(fi," (bkgd_bottom_red %d)\n", bkgd_bottom.red);
546 fprintf(fi," (bkgd_bottom_green %d)\n", bkgd_bottom.green);
547 fprintf(fi," (bkgd_bottom_blue %d)\n", bkgd_bottom.blue);
548 fprintf(fi," (time %d)\n", time_left);
549 fprintf(fi," (width %d)\n", width);
550 fprintf(fi," (gravity %2.1f)\n", gravity);
551 fprintf(fi," (background-tm ");
553 for(int y = 0; y < 15; ++y)
555 for(int i = 0; i < width; ++i)
556 fprintf(fi," %d ", bg_tiles[y][i]);
560 fprintf(fi," (interactive-tm ");
562 for(int y = 0; y < 15; ++y)
564 for(int i = 0; i < width; ++i)
565 fprintf(fi," %d ", ia_tiles[y][i]);
569 fprintf(fi," (foreground-tm ");
571 for(int y = 0; y < 15; ++y)
573 for(int i = 0; i < width; ++i)
574 fprintf(fi," %d ", fg_tiles[y][i]);
579 fprintf( fi,"(reset-points\n");
580 for(std::vector<ResetPoint>::iterator i = reset_points.begin();
581 i != reset_points.end(); ++i)
582 fprintf( fi,"(point (x %d) (y %d))\n",i->x, i->y);
585 fprintf( fi,"(objects\n");
587 for(std::vector<BadGuyData>::iterator it = badguy_data.begin();
588 it != badguy_data.end();
590 fprintf( fi,"(%s (x %d) (y %d) (stay-on-platform %s))\n",
591 badguykind_to_string((*it).kind).c_str(),(*it).x,(*it).y,
592 it->stay_on_platform ? "#t" : "#f");
602 /* Unload data for this level: */
607 for(int i=0; i < 15; ++i)
614 reset_points.clear();
627 if(!bkgd_image.empty())
630 snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
631 if(!faccessible(fname))
632 snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
633 img_bkgd = new Surface(fname, IGNORE_ALPHA);
637 /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */
638 load_image(&img_bkgd, theme,"solid0.png", IGNORE_ALPHA);
648 /* Load a level-specific graphic... */
650 Level::load_image(Surface** ptexture, string theme,const char * file, int use_alpha)
654 snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
655 if(!faccessible(fname))
656 snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
658 *ptexture = new Surface(fname, use_alpha);
661 /* Change the size of a level (width) */
663 Level::change_size (int new_width)
668 for(int y = 0; y < 15; ++y)
670 ia_tiles[y].resize(new_width, 0);
671 bg_tiles[y].resize(new_width, 0);
672 fg_tiles[y].resize(new_width, 0);
679 Level::change(float x, float y, int tm, unsigned int c)
681 int yy = ((int)y / 32);
682 int xx = ((int)x / 32);
684 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
689 bg_tiles[yy][xx] = c;
692 ia_tiles[yy][xx] = c;
695 fg_tiles[yy][xx] = c;
702 Level::free_song(void)
704 if(level_song_fast != level_song) {
705 free_music(level_song_fast);
709 free_music(level_song);
721 level_song = ::load_song(datadir + "/music/" + song_title);
723 st_abort("Couldn't load song: " , song_title.c_str());
725 song_path = (char *) malloc(sizeof(char) * datadir.length() +
726 strlen(song_title.c_str()) + 8 + 5);
727 song_subtitle = strdup(song_title.c_str());
728 strcpy(strstr(song_subtitle, "."), "\0");
729 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
730 song_subtitle, strstr(song_title.c_str(), "."));
731 level_song_fast = ::load_song(song_path);
732 if(!level_song_fast) {
733 level_song_fast = level_song;
740 Level::get_level_music()
746 Level::get_level_music_fast()
748 return level_song_fast;
752 Level::gettileid(float x, float y)
760 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
761 c = ia_tiles[yy][xx];