2 // C Implementation: level
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2003
9 // Copyright: See COPYING file that comes with this distribution
25 #include "lispreader.h"
29 st_subset::st_subset()
34 void st_subset::create(const std::string& subset_name)
38 new_subset.name = subset_name;
39 new_subset.title = "Unknown Title";
40 new_subset.description = "No description so far.";
42 new_lev.init_defaults();
43 new_lev.save(subset_name.c_str(),1);
46 void st_subset::parse (lisp_object_t* cursor)
48 while(!lisp_nil_p(cursor))
50 lisp_object_t* cur = lisp_car(cursor);
53 if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
59 if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
61 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
66 else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
68 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
74 cursor = lisp_cdr (cursor);
78 void st_subset::load(char *subset)
84 lisp_object_t* root_obj = 0;
88 snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
89 if(!faccessible(filename))
90 snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
91 if(faccessible(filename))
93 fi = fopen(filename, "r");
99 lisp_stream_init_file (&stream, fi);
100 root_obj = lisp_read (&stream);
102 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
104 printf("World: Parse Error in file %s", filename);
107 lisp_object_t* cur = lisp_car(root_obj);
109 if (!lisp_symbol_p (cur))
111 printf("World: Read error in %s",filename);
114 if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
116 parse(lisp_cdr(root_obj));
122 snprintf(str, 1024, "%s.png", filename);
125 image = new Surface(str,IGNORE_ALPHA);
129 snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
130 image = new Surface(filename,IGNORE_ALPHA);
134 for(i=1; i != -1; ++i)
136 /* Get the number of levels in this subset */
137 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
138 if(!faccessible(filename))
140 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
141 if(!faccessible(filename))
148 void st_subset::save()
153 /* Save data file: */
154 filename = "/levels/" + name + "/";
156 fcreatedir(filename.c_str());
157 filename = string(st_dir) + "/levels/" + name + "/info";
158 if(!fwriteable(filename.c_str()))
159 filename = datadir + "/levels/" + name + "/info";
160 if(fwriteable(filename.c_str()))
162 fi = fopen(filename.c_str(), "w");
165 perror(filename.c_str());
169 fprintf(fi,";SuperTux-Level-Subset\n");
170 fprintf(fi,"(supertux-level-subset\n");
172 /* Save title info: */
173 fprintf(fi," (title \"%s\")\n", title.c_str());
175 /* Save the description: */
176 fprintf(fi," (description \"%s\")\n", description.c_str());
184 void st_subset::free()
197 Level::Level(const std::string& subset, int level)
202 Level::Level(const std::string& filename)
208 Level::init_defaults()
212 theme = "antarctica";
213 song_title = "Mortimers_chipdisko.mod";
214 bkgd_image = "arctis.png";
223 bkgd_bottom.red = 255;
224 bkgd_bottom.green = 255;
225 bkgd_bottom.blue = 255;
228 for(int i = 0; i < 15; ++i)
230 ia_tiles[i].resize(width+1, 0);
231 ia_tiles[i][width] = (unsigned int) '\0';
233 for(int y = 0; y < width; ++y)
236 bg_tiles[i].resize(width+1, 0);
237 bg_tiles[i][width] = (unsigned int) '\0';
238 for(int y = 0; y < width; ++y)
241 fg_tiles[i].resize(width+1, 0);
242 fg_tiles[i][width] = (unsigned int) '\0';
243 for(int y = 0; y < width; ++y)
249 Level::load(const std::string& subset, int level)
254 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(), level);
255 if(!faccessible(filename))
256 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset.c_str(), level);
258 return load(filename);
262 Level::load(const std::string& filename)
265 lisp_object_t* root_obj = 0;
266 fi = fopen(filename.c_str(), "r");
269 perror(filename.c_str());
273 lisp_stream_t stream;
274 lisp_stream_init_file (&stream, fi);
275 root_obj = lisp_read (&stream);
277 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
279 printf("World: Parse Error in file %s", filename.c_str());
287 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
289 LispReader reader(lisp_cdr(root_obj));
290 reader.read_int("version", &version);
291 reader.read_int("width", &width);
292 if (!reader.read_int("start_pos_x", &start_pos_x)) start_pos_x = 100;
293 if (!reader.read_int("start_pos_y", &start_pos_y)) start_pos_y = 170;
294 reader.read_int("time", &time_left);
296 reader.read_int("bkgd_top_red", &bkgd_top.red);
297 reader.read_int("bkgd_top_green", &bkgd_top.green);
298 reader.read_int("bkgd_top_blue", &bkgd_top.blue);
300 reader.read_int("bkgd_bottom_red", &bkgd_bottom.red);
301 reader.read_int("bkgd_bottom_green", &bkgd_bottom.green);
302 reader.read_int("bkgd_bottom_blue", &bkgd_bottom.blue);
304 reader.read_float("gravity", &gravity);
305 reader.read_string("name", &name);
306 reader.read_string("author", &author);
307 reader.read_string("theme", &theme);
308 reader.read_string("music", &song_title);
309 reader.read_string("background", &bkgd_image);
310 reader.read_string("particle_system", &particle_system);
311 reader.read_int_vector("background-tm", &bg_tm);
313 if (!reader.read_int_vector("interactive-tm", &ia_tm))
314 reader.read_int_vector("tilemap", &ia_tm);
316 reader.read_int_vector("foreground-tm", &fg_tm);
319 lisp_object_t* cur = 0;
320 if (reader.read_lisp("objects", &cur))
322 while (!lisp_nil_p(cur))
324 lisp_object_t* data = lisp_car(cur);
327 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
328 LispReader reader(lisp_cdr(data));
329 reader.read_int("x", &bg_data.x);
330 reader.read_int("y", &bg_data.y);
332 badguy_data.push_back(bg_data);
339 // Convert old levels to the new tile numbers
342 std::map<char, int> transtable;
344 transtable['x'] = 104;
345 transtable['X'] = 77;
346 transtable['y'] = 78;
347 transtable['Y'] = 105;
348 transtable['A'] = 83;
349 transtable['B'] = 102;
350 transtable['!'] = 103;
351 transtable['a'] = 84;
352 transtable['C'] = 85;
353 transtable['D'] = 86;
354 transtable['E'] = 87;
355 transtable['F'] = 88;
356 transtable['c'] = 89;
357 transtable['d'] = 90;
358 transtable['e'] = 91;
359 transtable['f'] = 92;
361 transtable['G'] = 93;
362 transtable['H'] = 94;
363 transtable['I'] = 95;
364 transtable['J'] = 96;
366 transtable['g'] = 97;
367 transtable['h'] = 98;
368 transtable['i'] = 99;
369 transtable['j'] = 100
371 transtable['#'] = 11;
372 transtable['['] = 13;
373 transtable['='] = 14;
374 transtable[']'] = 15;
375 transtable['$'] = 82;
376 transtable['^'] = 76;
377 transtable['*'] = 80;
378 transtable['|'] = 79;
379 transtable['\\'] = 81;
380 transtable['&'] = 75;
384 for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
386 if (*i == '0' || *i == '1' || *i == '2')
388 badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
394 std::map<char, int>::iterator j = transtable.find(*i);
395 if (j != transtable.end())
398 printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
410 for(int i = 0; i < 15; ++i)
412 ia_tiles[i].resize(width + 1, 0);
413 bg_tiles[i].resize(width + 1, 0);
414 fg_tiles[i].resize(width + 1, 0);
419 for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
421 ia_tiles[j][i] = (*it);
430 for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
433 bg_tiles[j][i] = (*it);
442 for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
445 fg_tiles[j][i] = (*it);
453 // Mark the end position of this level!
454 // FIXME: -10 is a rather random value, we still need some kind of
455 // real levelend gola
456 endpos = 32*(width-10);
462 /* Save data for level: */
465 Level::save(const char * subset, int level)
470 /* Save data file: */
471 sprintf(str, "/levels/%s/", subset);
473 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
474 if(!fwriteable(filename))
475 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
477 FILE * fi = fopen(filename, "w");
487 fprintf(fi,";SuperTux-Level\n");
488 fprintf(fi,"(supertux-level\n");
490 fprintf(fi," (version %d)\n", 1);
491 fprintf(fi," (name \"%s\")\n", name.c_str());
492 fprintf(fi," (author \"%s\")\n", author.c_str());
493 fprintf(fi," (theme \"%s\")\n", theme.c_str());
494 fprintf(fi," (music \"%s\")\n", song_title.c_str());
495 fprintf(fi," (background \"%s\")\n", bkgd_image.c_str());
496 fprintf(fi," (particle_system \"%s\")\n", particle_system.c_str());
497 fprintf(fi," (bkgd_top_red %d)\n", bkgd_top.red);
498 fprintf(fi," (bkgd_top_green %d)\n", bkgd_top.green);
499 fprintf(fi," (bkgd_top_blue %d)\n", bkgd_top.blue);
500 fprintf(fi," (bkgd_bottom_red %d)\n", bkgd_bottom.red);
501 fprintf(fi," (bkgd_bottom_green %d)\n", bkgd_bottom.green);
502 fprintf(fi," (bkgd_bottom_blue %d)\n", bkgd_bottom.blue);
503 fprintf(fi," (time %d)\n", time_left);
504 fprintf(fi," (width %d)\n", width);
505 fprintf(fi," (gravity %2.1f)\n", gravity);
506 fprintf(fi," (background-tm ");
508 for(int y = 0; y < 15; ++y)
510 for(int i = 0; i < width; ++i)
511 fprintf(fi," %d ", bg_tiles[y][i]);
515 fprintf(fi," (interactive-tm ");
517 for(int y = 0; y < 15; ++y)
519 for(int i = 0; i < width; ++i)
520 fprintf(fi," %d ", ia_tiles[y][i]);
524 fprintf(fi," (foreground-tm ");
526 for(int y = 0; y < 15; ++y)
528 for(int i = 0; i < width; ++i)
529 fprintf(fi," %d ", fg_tiles[y][i]);
533 fprintf( fi,"(objects\n");
535 for(std::vector<BadGuyData>::iterator it = badguy_data.begin();
536 it != badguy_data.end();
538 fprintf( fi,"(%s (x %d) (y %d))\n",badguykind_to_string((*it).kind).c_str(),(*it).x,(*it).y);
548 /* Unload data for this level: */
553 for(int i=0; i < 15; ++i)
572 if(!bkgd_image.empty())
575 snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
576 if(!faccessible(fname))
577 snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
578 img_bkgd = new Surface(fname, IGNORE_ALPHA);
582 /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */
583 load_image(&img_bkgd, theme,"solid0.png", IGNORE_ALPHA);
593 /* Load a level-specific graphic... */
595 Level::load_image(Surface** ptexture, string theme,const char * file, int use_alpha)
599 snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
600 if(!faccessible(fname))
601 snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
603 *ptexture = new Surface(fname, use_alpha);
606 /* Change the size of a level (width) */
608 Level::change_size (int new_width)
613 for(int y = 0; y < 15; ++y)
615 ia_tiles[y].resize(new_width, 0);
616 bg_tiles[y].resize(new_width, 0);
617 fg_tiles[y].resize(new_width, 0);
624 Level::change(float x, float y, int tm, unsigned int c)
626 int yy = ((int)y / 32);
627 int xx = ((int)x / 32);
629 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
634 bg_tiles[yy][xx] = c;
637 ia_tiles[yy][xx] = c;
640 fg_tiles[yy][xx] = c;
647 Level::free_song(void)
649 free_music(level_song);
650 free_music(level_song_fast);
659 level_song = ::load_song(datadir + "/music/" + song_title);
661 song_path = (char *) malloc(sizeof(char) * datadir.length() +
662 strlen(song_title.c_str()) + 8 + 5);
663 song_subtitle = strdup(song_title.c_str());
664 strcpy(strstr(song_subtitle, "."), "\0");
665 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
666 song_subtitle, strstr(song_title.c_str(), "."));
667 level_song_fast = ::load_song(song_path);
673 Level::gettileid(float x, float y)
681 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= width)
682 c = ia_tiles[yy][xx];