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"
40 LevelSubset::LevelSubset()
45 LevelSubset::~LevelSubset()
50 void LevelSubset::create(const std::string& subset_name)
53 LevelSubset new_subset;
54 new_subset.name = subset_name;
55 new_subset.title = "Unknown Title";
56 new_subset.description = "No description so far.";
58 new_lev.init_defaults();
59 new_lev.save(subset_name, 1);
62 void LevelSubset::parse (lisp_object_t* cursor)
64 while(!lisp_nil_p(cursor))
66 lisp_object_t* cur = lisp_car(cursor);
69 if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
75 if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
77 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
82 else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
84 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
90 cursor = lisp_cdr (cursor);
94 void LevelSubset::load(char *subset)
100 lisp_object_t* root_obj = 0;
104 snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
105 if(!faccessible(filename))
106 snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
107 if(faccessible(filename))
109 fi = fopen(filename, "r");
114 lisp_stream_t stream;
115 lisp_stream_init_file (&stream, fi);
116 root_obj = lisp_read (&stream);
118 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
120 printf("World: Parse Error in file %s", filename);
123 lisp_object_t* cur = lisp_car(root_obj);
125 if (!lisp_symbol_p (cur))
127 printf("World: Read error in %s",filename);
130 if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
132 parse(lisp_cdr(root_obj));
139 snprintf(str, 1024, "%s.png", filename);
143 image = new Surface(str,IGNORE_ALPHA);
147 snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
149 image = new Surface(filename,IGNORE_ALPHA);
153 for(i=1; i != -1; ++i)
155 /* Get the number of levels in this subset */
156 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
157 if(!faccessible(filename))
159 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
160 if(!faccessible(filename))
167 void LevelSubset::save()
172 /* Save data file: */
173 filename = "/levels/" + name + "/";
175 fcreatedir(filename.c_str());
176 filename = string(st_dir) + "/levels/" + name + "/info";
177 if(!fwriteable(filename.c_str()))
178 filename = datadir + "/levels/" + name + "/info";
179 if(fwriteable(filename.c_str()))
181 fi = fopen(filename.c_str(), "w");
184 perror(filename.c_str());
188 fprintf(fi,";SuperTux-Level-Subset\n");
189 fprintf(fi,"(supertux-level-subset\n");
191 /* Save title info: */
192 fprintf(fi," (title \"%s\")\n", title.c_str());
194 /* Save the description: */
195 fprintf(fi," (description \"%s\")\n", description.c_str());
209 Level::Level(const std::string& subset, int level)
212 if(load(subset, level) < 0)
213 st_abort("Couldn't load level from subset", subset.c_str());
216 Level::Level(const std::string& filename)
219 if(load(filename) < 0)
220 st_abort("Couldn't load level " , filename.c_str());
229 Level::init_defaults()
233 song_title = "Mortimers_chipdisko.mod";
234 bkgd_image = "arctis.png";
241 back_scrolling = false;
242 hor_autoscroll_speed = 0;
247 bkgd_bottom.red = 255;
248 bkgd_bottom.green = 255;
249 bkgd_bottom.blue = 255;
251 bg_tiles.resize(height+1, std::vector<unsigned int>(width, 0));
252 ia_tiles.resize(height+1, std::vector<unsigned int>(width, 0));
253 fg_tiles.resize(height+1, std::vector<unsigned int>(width, 0));
255 for(int i = 0; i < height; ++i)
257 ia_tiles[i].resize(width+1, 0);
258 ia_tiles[i][width] = (unsigned int) '\0';
260 for(int y = 0; y < width; ++y)
263 bg_tiles[i].resize(width+1, 0);
264 bg_tiles[i][width] = (unsigned int) '\0';
265 for(int y = 0; y < width; ++y)
268 fg_tiles[i].resize(width+1, 0);
269 fg_tiles[i][width] = (unsigned int) '\0';
270 for(int y = 0; y < width; ++y)
276 Level::load(const std::string& subset, int level)
281 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(), level);
282 if(!faccessible(filename))
283 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset.c_str(), level);
285 return load(filename);
289 Level::load(const std::string& filename)
291 lisp_object_t* root_obj = lisp_read_from_file(filename);
294 std::cout << "Level: Couldn't load file: " << filename << std::endl;
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());
309 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
311 LispReader reader(lisp_cdr(root_obj));
313 reader.read_int("version", &version);
314 if(!reader.read_int("width", &width))
315 st_abort("No width specified for level.", "");
316 if (!reader.read_int("start_pos_x", &start_pos_x)) start_pos_x = 100;
317 if (!reader.read_int("start_pos_y", &start_pos_y)) start_pos_y = 170;
319 if(!reader.read_int("time", &time_left)) {
320 printf("Warning no time specified for level.\n");
324 reader.read_int("height", &height);
326 back_scrolling = false;
327 reader.read_bool("back_scrolling", &back_scrolling);
329 hor_autoscroll_speed = 0;
330 reader.read_float("hor_autoscroll_speed", &hor_autoscroll_speed);
333 reader.read_int("bkgd_speed", &bkgd_speed);
336 bkgd_top.red = bkgd_top.green = bkgd_top.blue = 0;
337 reader.read_int("bkgd_red_top", &bkgd_top.red);
338 reader.read_int("bkgd_green_top", &bkgd_top.green);
339 reader.read_int("bkgd_blue_top", &bkgd_top.blue);
341 bkgd_bottom.red = bkgd_bottom.green = bkgd_bottom.blue = 0;
342 reader.read_int("bkgd_red_bottom", &bkgd_bottom.red);
343 reader.read_int("bkgd_green_bottom", &bkgd_bottom.green);
344 reader.read_int("bkgd_blue_bottom", &bkgd_bottom.blue);
347 reader.read_float("gravity", &gravity);
349 reader.read_string("name", &name);
350 author = "unknown author";
351 reader.read_string("author", &author);
353 reader.read_string("music", &song_title);
355 reader.read_string("background", &bkgd_image);
356 particle_system = "";
357 reader.read_string("particle_system", &particle_system);
359 reader.read_int_vector("background-tm", &bg_tm);
361 if (!reader.read_int_vector("interactive-tm", &ia_tm))
362 reader.read_int_vector("tilemap", &ia_tm);
364 reader.read_int_vector("foreground-tm", &fg_tm);
366 { // Read ResetPoints
367 lisp_object_t* cur = 0;
368 if (reader.read_lisp("reset-points", &cur))
370 while (!lisp_nil_p(cur))
372 lisp_object_t* data = lisp_car(cur);
376 LispReader reader(lisp_cdr(data));
377 if (reader.read_int("x", &pos.x)
378 && reader.read_int("y", &pos.y))
380 reset_points.push_back(pos);
389 lisp_object_t* cur = 0;
390 if (reader.read_lisp("objects", &cur))
392 while (!lisp_nil_p(cur))
394 lisp_object_t* data = lisp_car(cur);
395 std::string object_type = "";
397 LispReader reader(lisp_cdr(data));
398 reader.read_string("type", &object_type);
400 if (object_type == "badguy" || object_type == "")
403 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
404 reader.read_int("x", &bg_data.x);
405 reader.read_int("y", &bg_data.y);
406 reader.read_bool("stay-on-platform", &bg_data.stay_on_platform);
408 badguy_data.push_back(bg_data);
412 if (strcmp(lisp_symbol(lisp_car(data)),"trampoline") == 0)
414 ObjectData<TrampolineData> _trampoline_data;
416 _trampoline_data.type = OBJ_TRAMPOLINE;
417 reader.read_int("x", &_trampoline_data.x);
418 reader.read_int("y", &_trampoline_data.y);
419 reader.read_int("power", &_trampoline_data.type_specific.power);
421 trampoline_data.push_back(_trampoline_data);
430 // Convert old levels to the new tile numbers
433 std::map<char, int> transtable;
435 transtable['x'] = 104;
436 transtable['X'] = 77;
437 transtable['y'] = 78;
438 transtable['Y'] = 105;
439 transtable['A'] = 83;
440 transtable['B'] = 102;
441 transtable['!'] = 103;
442 transtable['a'] = 84;
443 transtable['C'] = 85;
444 transtable['D'] = 86;
445 transtable['E'] = 87;
446 transtable['F'] = 88;
447 transtable['c'] = 89;
448 transtable['d'] = 90;
449 transtable['e'] = 91;
450 transtable['f'] = 92;
452 transtable['G'] = 93;
453 transtable['H'] = 94;
454 transtable['I'] = 95;
455 transtable['J'] = 96;
457 transtable['g'] = 97;
458 transtable['h'] = 98;
459 transtable['i'] = 99;
460 transtable['j'] = 100
462 transtable['#'] = 11;
463 transtable['['] = 13;
464 transtable['='] = 14;
465 transtable[']'] = 15;
466 transtable['$'] = 82;
467 transtable['^'] = 76;
468 transtable['*'] = 80;
469 transtable['|'] = 79;
470 transtable['\\'] = 81;
471 transtable['&'] = 75;
475 for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
477 if (*i == '0' || *i == '1' || *i == '2')
479 badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
485 std::map<char, int>::iterator j = transtable.find(*i);
486 if (j != transtable.end())
489 printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
501 bg_tiles.resize(height+1, std::vector<unsigned int>(width, 0));
502 ia_tiles.resize(height+1, std::vector<unsigned int>(width, 0));
503 fg_tiles.resize(height+1, std::vector<unsigned int>(width, 0));
505 for(int i = 0; i < height; ++i)
507 ia_tiles[i].resize(width + 1, 0);
508 bg_tiles[i].resize(width + 1, 0);
509 fg_tiles[i].resize(width + 1, 0);
514 for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
516 ia_tiles[j][i] = (*it);
525 for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
528 bg_tiles[j][i] = (*it);
537 for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
540 fg_tiles[j][i] = (*it);
552 /* Save data for level: */
555 Level::save(const std::string& subset, int level)
560 /* Save data file: */
561 sprintf(str, "/levels/%s/", subset.c_str());
563 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(),
565 if(!fwriteable(filename))
566 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(),
567 subset.c_str(), level);
569 FILE * fi = fopen(filename, "w");
579 fprintf(fi,";SuperTux level made using the built-in leveleditor\n");
580 fprintf(fi,"(supertux-level\n");
582 fprintf(fi," (version %d)\n", 1);
583 fprintf(fi," (name \"%s\")\n", name.c_str());
584 fprintf(fi," (author \"%s\")\n", author.c_str());
585 fprintf(fi," (music \"%s\")\n", song_title.c_str());
586 fprintf(fi," (background \"%s\")\n", bkgd_image.c_str());
587 fprintf(fi," (particle_system \"%s\")\n", particle_system.c_str());
588 fprintf(fi," (bkgd_speed %d)\n", bkgd_speed);
589 fprintf(fi," (bkgd_red_top %d)\n", bkgd_top.red);
590 fprintf(fi," (bkgd_green_top %d)\n", bkgd_top.green);
591 fprintf(fi," (bkgd_blue_top %d)\n", bkgd_top.blue);
592 fprintf(fi," (bkgd_red_bottom %d)\n", bkgd_bottom.red);
593 fprintf(fi," (bkgd_green_bottom %d)\n", bkgd_bottom.green);
594 fprintf(fi," (bkgd_blue_bottom %d)\n", bkgd_bottom.blue);
595 fprintf(fi," (time %d)\n", time_left);
596 fprintf(fi," (width %d)\n", width);
597 fprintf(fi," (height %d)\n", height);
599 fprintf(fi," (back_scrolling #t)\n");
601 fprintf(fi," (back_scrolling #f)\n");
602 fprintf(fi," (hor_autoscroll_speed %2.1f)\n", hor_autoscroll_speed);
603 fprintf(fi," (gravity %2.1f)\n", gravity);
604 fprintf(fi," (background-tm ");
606 for(int y = 0; y < height; ++y)
608 for(int i = 0; i < width; ++i)
609 fprintf(fi," %d ", bg_tiles[y][i]);
614 fprintf(fi," (interactive-tm ");
616 for(int y = 0; y < height; ++y)
618 for(int i = 0; i < width; ++i)
619 fprintf(fi," %d ", ia_tiles[y][i]);
624 fprintf(fi," (foreground-tm ");
626 for(int y = 0; y < height; ++y)
628 for(int i = 0; i < width; ++i)
629 fprintf(fi," %d ", fg_tiles[y][i]);
635 fprintf( fi,"(reset-points\n");
636 for(std::vector<ResetPoint>::iterator i = reset_points.begin();
637 i != reset_points.end(); ++i)
638 fprintf( fi,"(point (x %d) (y %d))\n",i->x, i->y);
641 fprintf( fi,"(objects\n");
643 for(std::vector<BadGuyData>::iterator it = badguy_data.begin();
644 it != badguy_data.end();
646 fprintf( fi," (%s (x %d) (y %d) (stay-on-platform %s))\n",
647 badguykind_to_string((*it).kind).c_str(),(*it).x,(*it).y,
648 it->stay_on_platform ? "#t" : "#f");
658 /* Unload data for this level: */
663 for(int i=0; i < 15; ++i)
670 reset_points.clear();
682 if(!bkgd_image.empty())
685 snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
686 if(!faccessible(fname))
687 snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
689 img_bkgd = new Surface(fname, IGNORE_ALPHA);
698 /* Load a level-specific graphic... */
699 void Level::load_image(Surface** ptexture, string theme,const char * file, int use_alpha)
703 snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
704 if(!faccessible(fname))
705 snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
707 *ptexture = new Surface(fname, use_alpha);
710 /* Change the size of a level */
712 Level::change_width (int new_width)
717 for(int y = 0; y < height; ++y)
719 ia_tiles[y].resize(new_width, 0);
720 bg_tiles[y].resize(new_width, 0);
721 fg_tiles[y].resize(new_width, 0);
728 Level::change_height (int new_height)
733 bg_tiles.resize(height+1, std::vector<unsigned int>(width, 0));
734 ia_tiles.resize(height+1, std::vector<unsigned int>(width, 0));
735 fg_tiles.resize(height+1, std::vector<unsigned int>(width, 0));
741 Level::change(float x, float y, int tm, unsigned int c)
743 int yy = ((int)y / 32);
744 int xx = ((int)x / 32);
746 if (yy >= 0 && yy < height && xx >= 0 && xx <= width)
751 bg_tiles[yy][xx] = c;
754 ia_tiles[yy][xx] = c;
757 fg_tiles[yy][xx] = c;
763 void Level::draw_bg()
765 // Tile background horizontally
766 int sx = (int)((float)scroll_x * ((float)bkgd_speed/100.0f)) % img_bkgd->w;
767 for (int i = 0; (i-1)*img_bkgd->w <= screen->w; i++)
768 img_bkgd->draw_part(i == 0 ? sx : 0, 0,
769 i == 0 ? 0 : (img_bkgd->w * i) - sx, 0,
770 i == 0 ? img_bkgd->w - sx : img_bkgd->w, img_bkgd->h);
779 level_song = music_manager->load_music(datadir + "/music/" + song_title);
781 song_path = (char *) malloc(sizeof(char) * datadir.length() +
782 strlen(song_title.c_str()) + 8 + 5);
783 song_subtitle = strdup(song_title.c_str());
784 strcpy(strstr(song_subtitle, "."), "\0");
785 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
786 song_subtitle, strstr(song_title.c_str(), "."));
787 if(!music_manager->exists_music(song_path)) {
788 level_song_fast = level_song;
790 level_song_fast = music_manager->load_music(song_path);
797 Level::get_level_music()
803 Level::get_level_music_fast()
805 return level_song_fast;
809 Level::gettileid(float x, float y) const
817 if (yy >= 0 && yy < height && xx >= 0 && xx <= width)
818 c = ia_tiles[yy][xx];
826 Level::get_tile_at(int x, int y) const
828 if(x < 0 || x > width || y < 0 || y > height)
831 return ia_tiles[y][x];