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()
205 Level::Level(const std::string& subset, int level)
210 Level::Level(const std::string& filename)
216 Level::init_defaults()
220 theme = "antarctica";
221 song_title = "Mortimers_chipdisko.mod";
222 bkgd_image = "arctis.png";
231 bkgd_bottom.red = 255;
232 bkgd_bottom.green = 255;
233 bkgd_bottom.blue = 255;
236 for(int i = 0; i < 15; ++i)
238 ia_tiles[i].resize(width+1, 0);
239 ia_tiles[i][width] = (unsigned int) '\0';
241 for(int y = 0; y < width; ++y)
244 bg_tiles[i].resize(width+1, 0);
245 bg_tiles[i][width] = (unsigned int) '\0';
246 for(int y = 0; y < width; ++y)
249 fg_tiles[i].resize(width+1, 0);
250 fg_tiles[i][width] = (unsigned int) '\0';
251 for(int y = 0; y < width; ++y)
257 Level::load(const std::string& subset, int level)
262 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(), level);
263 if(!faccessible(filename))
264 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset.c_str(), level);
266 return load(filename);
270 Level::load(const std::string& filename)
273 lisp_object_t* root_obj = 0;
274 fi = fopen(filename.c_str(), "r");
277 perror(filename.c_str());
281 lisp_stream_t stream;
282 lisp_stream_init_file (&stream, fi);
283 root_obj = lisp_read (&stream);
285 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
287 printf("World: Parse Error in file %s", filename.c_str());
295 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
297 LispReader reader(lisp_cdr(root_obj));
298 reader.read_int("version", &version);
299 reader.read_int("width", &width);
300 if (!reader.read_int("start_pos_x", &start_pos_x)) start_pos_x = 100;
301 if (!reader.read_int("start_pos_y", &start_pos_y)) start_pos_y = 170;
302 reader.read_int("time", &time_left);
304 reader.read_int("bkgd_top_red", &bkgd_top.red);
305 reader.read_int("bkgd_top_green", &bkgd_top.green);
306 reader.read_int("bkgd_top_blue", &bkgd_top.blue);
308 reader.read_int("bkgd_bottom_red", &bkgd_bottom.red);
309 reader.read_int("bkgd_bottom_green", &bkgd_bottom.green);
310 reader.read_int("bkgd_bottom_blue", &bkgd_bottom.blue);
312 reader.read_float("gravity", &gravity);
313 reader.read_string("name", &name);
314 reader.read_string("author", &author);
315 reader.read_string("theme", &theme);
316 reader.read_string("music", &song_title);
317 reader.read_string("background", &bkgd_image);
318 reader.read_string("particle_system", &particle_system);
319 reader.read_int_vector("background-tm", &bg_tm);
321 if (!reader.read_int_vector("interactive-tm", &ia_tm))
322 reader.read_int_vector("tilemap", &ia_tm);
324 reader.read_int_vector("foreground-tm", &fg_tm);
326 { // Read ResetPoints
327 lisp_object_t* cur = 0;
328 if (reader.read_lisp("reset-points", &cur))
330 while (!lisp_nil_p(cur))
332 lisp_object_t* data = lisp_car(cur);
336 LispReader reader(lisp_cdr(data));
337 if (reader.read_int("x", &pos.x)
338 && reader.read_int("y", &pos.y))
340 reset_points.push_back(pos);
349 lisp_object_t* cur = 0;
350 if (reader.read_lisp("objects", &cur))
352 while (!lisp_nil_p(cur))
354 lisp_object_t* data = lisp_car(cur);
357 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
358 LispReader reader(lisp_cdr(data));
359 reader.read_int("x", &bg_data.x);
360 reader.read_int("y", &bg_data.y);
362 badguy_data.push_back(bg_data);
369 // Convert old levels to the new tile numbers
372 std::map<char, int> transtable;
374 transtable['x'] = 104;
375 transtable['X'] = 77;
376 transtable['y'] = 78;
377 transtable['Y'] = 105;
378 transtable['A'] = 83;
379 transtable['B'] = 102;
380 transtable['!'] = 103;
381 transtable['a'] = 84;
382 transtable['C'] = 85;
383 transtable['D'] = 86;
384 transtable['E'] = 87;
385 transtable['F'] = 88;
386 transtable['c'] = 89;
387 transtable['d'] = 90;
388 transtable['e'] = 91;
389 transtable['f'] = 92;
391 transtable['G'] = 93;
392 transtable['H'] = 94;
393 transtable['I'] = 95;
394 transtable['J'] = 96;
396 transtable['g'] = 97;
397 transtable['h'] = 98;
398 transtable['i'] = 99;
399 transtable['j'] = 100
401 transtable['#'] = 11;
402 transtable['['] = 13;
403 transtable['='] = 14;
404 transtable[']'] = 15;
405 transtable['$'] = 82;
406 transtable['^'] = 76;
407 transtable['*'] = 80;
408 transtable['|'] = 79;
409 transtable['\\'] = 81;
410 transtable['&'] = 75;
414 for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
416 if (*i == '0' || *i == '1' || *i == '2')
418 badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
424 std::map<char, int>::iterator j = transtable.find(*i);
425 if (j != transtable.end())
428 printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
440 for(int i = 0; i < 15; ++i)
442 ia_tiles[i].resize(width + 1, 0);
443 bg_tiles[i].resize(width + 1, 0);
444 fg_tiles[i].resize(width + 1, 0);
449 for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
451 ia_tiles[j][i] = (*it);
460 for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
463 bg_tiles[j][i] = (*it);
472 for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
475 fg_tiles[j][i] = (*it);
483 // Mark the end position of this level!
484 // FIXME: -10 is a rather random value, we still need some kind of
485 // real levelend gola
486 endpos = 32*(width-10);
492 /* Save data for level: */
495 Level::save(const char * subset, int level)
500 /* Save data file: */
501 sprintf(str, "/levels/%s/", subset);
503 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
504 if(!fwriteable(filename))
505 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
507 FILE * fi = fopen(filename, "w");
517 fprintf(fi,";SuperTux-Level\n");
518 fprintf(fi,"(supertux-level\n");
520 fprintf(fi," (version %d)\n", 1);
521 fprintf(fi," (name \"%s\")\n", name.c_str());
522 fprintf(fi," (author \"%s\")\n", author.c_str());
523 fprintf(fi," (theme \"%s\")\n", theme.c_str());
524 fprintf(fi," (music \"%s\")\n", song_title.c_str());
525 fprintf(fi," (background \"%s\")\n", bkgd_image.c_str());
526 fprintf(fi," (particle_system \"%s\")\n", particle_system.c_str());
527 fprintf(fi," (bkgd_top_red %d)\n", bkgd_top.red);
528 fprintf(fi," (bkgd_top_green %d)\n", bkgd_top.green);
529 fprintf(fi," (bkgd_top_blue %d)\n", bkgd_top.blue);
530 fprintf(fi," (bkgd_bottom_red %d)\n", bkgd_bottom.red);
531 fprintf(fi," (bkgd_bottom_green %d)\n", bkgd_bottom.green);
532 fprintf(fi," (bkgd_bottom_blue %d)\n", bkgd_bottom.blue);
533 fprintf(fi," (time %d)\n", time_left);
534 fprintf(fi," (width %d)\n", width);
535 fprintf(fi," (gravity %2.1f)\n", gravity);
536 fprintf(fi," (background-tm ");
538 for(int y = 0; y < 15; ++y)
540 for(int i = 0; i < width; ++i)
541 fprintf(fi," %d ", bg_tiles[y][i]);
545 fprintf(fi," (interactive-tm ");
547 for(int y = 0; y < 15; ++y)
549 for(int i = 0; i < width; ++i)
550 fprintf(fi," %d ", ia_tiles[y][i]);
554 fprintf(fi," (foreground-tm ");
556 for(int y = 0; y < 15; ++y)
558 for(int i = 0; i < width; ++i)
559 fprintf(fi," %d ", fg_tiles[y][i]);
564 fprintf( fi,"(reset-points\n");
565 for(std::vector<ResetPoint>::iterator i = reset_points.begin();
566 i != reset_points.end(); ++i)
567 fprintf( fi,"(point (x %d) (y %d))\n",i->x, i->y);
570 fprintf( fi,"(objects\n");
572 for(std::vector<BadGuyData>::iterator it = badguy_data.begin();
573 it != badguy_data.end();
575 fprintf( fi,"(%s (x %d) (y %d))\n",badguykind_to_string((*it).kind).c_str(),(*it).x,(*it).y);
585 /* Unload data for this level: */
590 for(int i=0; i < 15; ++i)
597 reset_points.clear();
610 if(!bkgd_image.empty())
613 snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
614 if(!faccessible(fname))
615 snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
616 img_bkgd = new Surface(fname, IGNORE_ALPHA);
620 /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */
621 load_image(&img_bkgd, theme,"solid0.png", IGNORE_ALPHA);
631 /* Load a level-specific graphic... */
633 Level::load_image(Surface** ptexture, string theme,const char * file, int use_alpha)
637 snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
638 if(!faccessible(fname))
639 snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
641 *ptexture = new Surface(fname, use_alpha);
644 /* Change the size of a level (width) */
646 Level::change_size (int new_width)
651 for(int y = 0; y < 15; ++y)
653 ia_tiles[y].resize(new_width, 0);
654 bg_tiles[y].resize(new_width, 0);
655 fg_tiles[y].resize(new_width, 0);
662 Level::change(float x, float y, int tm, unsigned int c)
664 int yy = ((int)y / 32);
665 int xx = ((int)x / 32);
667 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
672 bg_tiles[yy][xx] = c;
675 ia_tiles[yy][xx] = c;
678 fg_tiles[yy][xx] = c;
685 Level::free_song(void)
687 free_music(level_song);
688 free_music(level_song_fast);
697 level_song = ::load_song(datadir + "/music/" + song_title);
699 song_path = (char *) malloc(sizeof(char) * datadir.length() +
700 strlen(song_title.c_str()) + 8 + 5);
701 song_subtitle = strdup(song_title.c_str());
702 strcpy(strstr(song_subtitle, "."), "\0");
703 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
704 song_subtitle, strstr(song_title.c_str(), "."));
705 level_song_fast = ::load_song(song_path);
711 Level::gettileid(float x, float y)
719 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
720 c = ia_tiles[yy][xx];