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 LevelSubset::LevelSubset()
44 LevelSubset::~LevelSubset()
49 void LevelSubset::create(const std::string& subset_name)
52 LevelSubset new_subset;
53 new_subset.name = subset_name;
54 new_subset.title = "Unknown Title";
55 new_subset.description = "No description so far.";
57 new_lev.init_defaults();
58 new_lev.save(subset_name, 1);
61 void LevelSubset::parse (lisp_object_t* cursor)
63 while(!lisp_nil_p(cursor))
65 lisp_object_t* cur = lisp_car(cursor);
68 if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
74 if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
76 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
81 else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
83 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
89 cursor = lisp_cdr (cursor);
93 void LevelSubset::load(char *subset)
99 lisp_object_t* root_obj = 0;
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))
108 fi = fopen(filename, "r");
113 lisp_stream_t stream;
114 lisp_stream_init_file (&stream, fi);
115 root_obj = lisp_read (&stream);
117 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
119 printf("World: Parse Error in file %s", filename);
122 lisp_object_t* cur = lisp_car(root_obj);
124 if (!lisp_symbol_p (cur))
126 printf("World: Read error in %s",filename);
129 if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
131 parse(lisp_cdr(root_obj));
138 snprintf(str, 1024, "%s.png", filename);
142 image = new Surface(str,IGNORE_ALPHA);
146 snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
148 image = new Surface(filename,IGNORE_ALPHA);
152 for(i=1; i != -1; ++i)
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))
158 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
159 if(!faccessible(filename))
166 void LevelSubset::save()
171 /* Save data file: */
172 filename = "/levels/" + name + "/";
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()))
180 fi = fopen(filename.c_str(), "w");
183 perror(filename.c_str());
187 fprintf(fi,";SuperTux-Level-Subset\n");
188 fprintf(fi,"(supertux-level-subset\n");
190 /* Save title info: */
191 fprintf(fi," (title \"%s\")\n", title.c_str());
193 /* Save the description: */
194 fprintf(fi," (description \"%s\")\n", description.c_str());
208 Level::Level(const std::string& subset, int level)
211 if(load(subset, level) < 0)
212 st_abort("Couldn't load level from subset", subset.c_str());
215 Level::Level(const std::string& filename)
218 if(load(filename) < 0)
219 st_abort("Couldn't load level " , filename.c_str());
228 Level::init_defaults()
232 song_title = "Mortimers_chipdisko.mod";
233 bkgd_image = "arctis.png";
239 back_scrolling = false;
244 bkgd_bottom.red = 255;
245 bkgd_bottom.green = 255;
246 bkgd_bottom.blue = 255;
248 for(int i = 0; i < 15; ++i)
250 ia_tiles[i].resize(width+1, 0);
251 ia_tiles[i][width] = (unsigned int) '\0';
253 for(int y = 0; y < width; ++y)
256 bg_tiles[i].resize(width+1, 0);
257 bg_tiles[i][width] = (unsigned int) '\0';
258 for(int y = 0; y < width; ++y)
261 fg_tiles[i].resize(width+1, 0);
262 fg_tiles[i][width] = (unsigned int) '\0';
263 for(int y = 0; y < width; ++y)
269 Level::load(const std::string& subset, int level)
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);
278 return load(filename);
282 Level::load(const std::string& filename)
284 lisp_object_t* root_obj = lisp_read_from_file(filename);
287 std::cout << "Level: Couldn't load file: " << filename << std::endl;
291 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
293 printf("World: Parse Error in file %s", filename.c_str());
302 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
304 LispReader reader(lisp_cdr(root_obj));
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;
312 if(!reader.read_int("time", &time_left)) {
313 printf("Warning no time specified for level.\n");
316 back_scrolling = false;
317 reader.read_bool("back_scrolling", &back_scrolling);
320 reader.read_int("bkgd_speed", &bkgd_speed);
323 bkgd_top.red = bkgd_top.green = bkgd_top.blue = 0;
324 reader.read_int("bkgd_red_top", &bkgd_top.red);
325 reader.read_int("bkgd_green_top", &bkgd_top.green);
326 reader.read_int("bkgd_blue_top", &bkgd_top.blue);
328 bkgd_bottom.red = bkgd_bottom.green = bkgd_bottom.blue = 0;
329 reader.read_int("bkgd_red_bottom", &bkgd_bottom.red);
330 reader.read_int("bkgd_green_bottom", &bkgd_bottom.green);
331 reader.read_int("bkgd_blue_bottom", &bkgd_bottom.blue);
334 reader.read_float("gravity", &gravity);
336 reader.read_string("name", &name);
337 author = "unknown author";
338 reader.read_string("author", &author);
340 reader.read_string("music", &song_title);
342 reader.read_string("background", &bkgd_image);
343 particle_system = "";
344 reader.read_string("particle_system", &particle_system);
346 reader.read_int_vector("background-tm", &bg_tm);
348 if (!reader.read_int_vector("interactive-tm", &ia_tm))
349 reader.read_int_vector("tilemap", &ia_tm);
351 reader.read_int_vector("foreground-tm", &fg_tm);
353 { // Read ResetPoints
354 lisp_object_t* cur = 0;
355 if (reader.read_lisp("reset-points", &cur))
357 while (!lisp_nil_p(cur))
359 lisp_object_t* data = lisp_car(cur);
363 LispReader reader(lisp_cdr(data));
364 if (reader.read_int("x", &pos.x)
365 && reader.read_int("y", &pos.y))
367 reset_points.push_back(pos);
376 lisp_object_t* cur = 0;
377 if (reader.read_lisp("objects", &cur))
379 while (!lisp_nil_p(cur))
381 lisp_object_t* data = lisp_car(cur);
384 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
385 LispReader reader(lisp_cdr(data));
386 reader.read_int("x", &bg_data.x);
387 reader.read_int("y", &bg_data.y);
388 reader.read_bool("stay-on-platform", &bg_data.stay_on_platform);
390 badguy_data.push_back(bg_data);
397 // Convert old levels to the new tile numbers
400 std::map<char, int> transtable;
402 transtable['x'] = 104;
403 transtable['X'] = 77;
404 transtable['y'] = 78;
405 transtable['Y'] = 105;
406 transtable['A'] = 83;
407 transtable['B'] = 102;
408 transtable['!'] = 103;
409 transtable['a'] = 84;
410 transtable['C'] = 85;
411 transtable['D'] = 86;
412 transtable['E'] = 87;
413 transtable['F'] = 88;
414 transtable['c'] = 89;
415 transtable['d'] = 90;
416 transtable['e'] = 91;
417 transtable['f'] = 92;
419 transtable['G'] = 93;
420 transtable['H'] = 94;
421 transtable['I'] = 95;
422 transtable['J'] = 96;
424 transtable['g'] = 97;
425 transtable['h'] = 98;
426 transtable['i'] = 99;
427 transtable['j'] = 100
429 transtable['#'] = 11;
430 transtable['['] = 13;
431 transtable['='] = 14;
432 transtable[']'] = 15;
433 transtable['$'] = 82;
434 transtable['^'] = 76;
435 transtable['*'] = 80;
436 transtable['|'] = 79;
437 transtable['\\'] = 81;
438 transtable['&'] = 75;
442 for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
444 if (*i == '0' || *i == '1' || *i == '2')
446 badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
452 std::map<char, int>::iterator j = transtable.find(*i);
453 if (j != transtable.end())
456 printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
468 for(int i = 0; i < 15; ++i)
470 ia_tiles[i].resize(width + 1, 0);
471 bg_tiles[i].resize(width + 1, 0);
472 fg_tiles[i].resize(width + 1, 0);
477 for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
479 ia_tiles[j][i] = (*it);
488 for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
491 bg_tiles[j][i] = (*it);
500 for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
503 fg_tiles[j][i] = (*it);
515 /* Save data for level: */
518 Level::save(const std::string& subset, int level)
523 /* Save data file: */
524 sprintf(str, "/levels/%s/", subset.c_str());
526 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(),
528 if(!fwriteable(filename))
529 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(),
530 subset.c_str(), level);
532 FILE * fi = fopen(filename, "w");
542 fprintf(fi,";SuperTux-Level\n");
543 fprintf(fi,"(supertux-level\n");
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," (music \"%s\")\n", song_title.c_str());
549 fprintf(fi," (background \"%s\")\n", bkgd_image.c_str());
550 fprintf(fi," (particle_system \"%s\")\n", particle_system.c_str());
551 fprintf(fi," (bkgd_speed \"%d\")\n", bkgd_speed);
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);
561 fprintf(fi," (back_scrolling #t)\n");
563 fprintf(fi," (back_scrolling #f)\n");
564 fprintf(fi," (gravity %2.1f)\n", gravity);
565 fprintf(fi," (background-tm ");
567 for(int y = 0; y < 15; ++y)
569 for(int i = 0; i < width; ++i)
570 fprintf(fi," %d ", bg_tiles[y][i]);
574 fprintf(fi," (interactive-tm ");
576 for(int y = 0; y < 15; ++y)
578 for(int i = 0; i < width; ++i)
579 fprintf(fi," %d ", ia_tiles[y][i]);
583 fprintf(fi," (foreground-tm ");
585 for(int y = 0; y < 15; ++y)
587 for(int i = 0; i < width; ++i)
588 fprintf(fi," %d ", fg_tiles[y][i]);
593 fprintf( fi,"(reset-points\n");
594 for(std::vector<ResetPoint>::iterator i = reset_points.begin();
595 i != reset_points.end(); ++i)
596 fprintf( fi,"(point (x %d) (y %d))\n",i->x, i->y);
599 fprintf( fi,"(objects\n");
601 for(std::vector<BadGuyData>::iterator it = badguy_data.begin();
602 it != badguy_data.end();
604 fprintf( fi,"(%s (x %d) (y %d) (stay-on-platform %s))\n",
605 badguykind_to_string((*it).kind).c_str(),(*it).x,(*it).y,
606 it->stay_on_platform ? "#t" : "#f");
616 /* Unload data for this level: */
621 for(int i=0; i < 15; ++i)
628 reset_points.clear();
640 if(!bkgd_image.empty())
643 snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
644 if(!faccessible(fname))
645 snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
647 img_bkgd = new Surface(fname, IGNORE_ALPHA);
656 /* Load a level-specific graphic... */
657 void Level::load_image(Surface** ptexture, string theme,const char * file, int use_alpha)
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);
665 *ptexture = new Surface(fname, use_alpha);
668 /* Change the size of a level (width) */
670 Level::change_size (int new_width)
675 for(int y = 0; y < 15; ++y)
677 ia_tiles[y].resize(new_width, 0);
678 bg_tiles[y].resize(new_width, 0);
679 fg_tiles[y].resize(new_width, 0);
686 Level::change(float x, float y, int tm, unsigned int c)
688 int yy = ((int)y / 32);
689 int xx = ((int)x / 32);
691 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
696 bg_tiles[yy][xx] = c;
699 ia_tiles[yy][xx] = c;
702 fg_tiles[yy][xx] = c;
714 level_song = music_manager->load_music(datadir + "/music/" + song_title);
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;
725 level_song_fast = music_manager->load_music(song_path);
732 Level::get_level_music()
738 Level::get_level_music_fast()
740 return level_song_fast;
744 Level::gettileid(float x, float y) const
752 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
753 c = ia_tiles[yy][xx];
761 Level::get_tile_at(int x, int y) const
763 if(x < 0 || x > width || y < 0 || y > 14)
766 return ia_tiles[y][x];