2 // C Implementation: level
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2003
9 // Copyright: See COPYING file that comes with this distribution
24 #include "lispreader.h"
28 texture_type img_bkgd, img_bkgd_tile[2][4], img_solid[4], img_brick[2];
30 st_subset::st_subset()
35 void st_subset::create(const std::string& subset_name)
39 new_subset.name = subset_name;
40 new_subset.title = "Unknown Title";
41 new_subset.description = "No description so far.";
43 level_default(&new_lev);
44 level_save(&new_lev,subset_name.c_str(),1);
47 void st_subset::parse (lisp_object_t* cursor)
49 while(!lisp_nil_p(cursor))
51 lisp_object_t* cur = lisp_car(cursor);
54 if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
60 if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
62 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
67 else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
69 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
75 cursor = lisp_cdr (cursor);
79 void st_subset::load(char *subset)
85 lisp_object_t* root_obj = 0;
89 snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
90 if(!faccessible(filename))
91 snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
92 if(faccessible(filename))
94 fi = fopen(filename, "r");
100 lisp_stream_init_file (&stream, fi);
101 root_obj = lisp_read (&stream);
103 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
105 printf("World: Parse Error in file %s", filename);
108 lisp_object_t* cur = lisp_car(root_obj);
110 if (!lisp_symbol_p (cur))
112 printf("World: Read error in %s",filename);
115 if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
117 parse(lisp_cdr(root_obj));
123 snprintf(str, 1024, "%s.png", filename);
126 texture_load(&image,str,IGNORE_ALPHA);
130 snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
131 texture_load(&image,filename,IGNORE_ALPHA);
135 for(i=1; i != -1; ++i)
137 /* Get the number of levels in this subset */
138 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
139 if(!faccessible(filename))
141 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
142 if(!faccessible(filename))
149 void st_subset::save()
154 /* Save data file: */
155 filename = "/levels/" + name + "/";
157 fcreatedir(filename.c_str());
158 filename = string(st_dir) + "/levels/" + name + "/info";
159 if(!fwriteable(filename.c_str()))
160 filename = datadir + "/levels/" + name + "/info";
161 if(fwriteable(filename.c_str()))
163 fi = fopen(filename.c_str(), "w");
166 perror(filename.c_str());
170 fprintf(fi,";SuperTux-Level-Subset\n");
171 fprintf(fi,"(supertux-level-subset\n");
173 /* Save title info: */
174 fprintf(fi," (title \"%s\")\n", title.c_str());
176 /* Save the description: */
177 fprintf(fi," (description \"%s\")\n", description.c_str());
185 void st_subset::free()
190 texture_free(&image);
194 void level_default(st_level* plevel)
197 plevel->name = "UnNamed";
198 plevel->theme = "antarctica";
199 plevel->song_title = "Mortimers_chipdisko.mod";
200 plevel->bkgd_image = "arctis.png";
202 plevel->time_left = 100;
203 plevel->gravity = 10.;
204 plevel->bkgd_red = 0;
205 plevel->bkgd_green = 0;
206 plevel->bkgd_blue = 0;
208 for(i = 0; i < 15; ++i)
210 plevel->ia_tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
211 plevel->ia_tiles[i][plevel->width] = (unsigned int) '\0';
212 for(y = 0; y < plevel->width; ++y)
213 plevel->ia_tiles[i][y] = (unsigned int) '.';
214 plevel->ia_tiles[i][plevel->width] = (unsigned int) '\0';
216 plevel->bg_tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
217 plevel->bg_tiles[i][plevel->width] = (unsigned int) '\0';
218 for(y = 0; y < plevel->width; ++y)
219 plevel->bg_tiles[i][y] = (unsigned int) '.';
220 plevel->bg_tiles[i][plevel->width] = (unsigned int) '\0';
222 plevel->fg_tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
223 plevel->fg_tiles[i][plevel->width] = (unsigned int) '\0';
224 for(y = 0; y < plevel->width; ++y)
225 plevel->fg_tiles[i][y] = (unsigned int) '.';
226 plevel->fg_tiles[i][plevel->width] = (unsigned int) '\0';
228 plevel->dn_tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
229 plevel->dn_tiles[i][plevel->width] = (unsigned int) '\0';
230 for(y = 0; y < plevel->width; ++y)
231 plevel->dn_tiles[i][y] = (unsigned int) '.';
232 plevel->dn_tiles[i][plevel->width] = (unsigned int) '\0';
236 /* Load data for this level: */
237 /* Returns -1, if the loading of the level failed. */
238 int level_load(st_level* plevel, const char *subset, int level)
242 /* Load data file: */
244 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
245 if(!faccessible(filename))
246 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
248 return level_load(plevel, filename);
251 int level_load(st_level* plevel, const char* filename)
254 lisp_object_t* root_obj = 0;
255 fi = fopen(filename, "r");
262 lisp_stream_t stream;
263 lisp_stream_init_file (&stream, fi);
264 root_obj = lisp_read (&stream);
266 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
268 printf("World: Parse Error in file %s", filename);
276 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
278 LispReader reader(lisp_cdr(root_obj));
280 reader.read_int("version", &version);
281 reader.read_int("width", &plevel->width);
282 reader.read_int("time", &plevel->time_left);
283 reader.read_int("bkgd_red", &plevel->bkgd_red);
284 reader.read_int("bkgd_green", &plevel->bkgd_green);
285 reader.read_int("bkgd_blue", &plevel->bkgd_blue);
286 reader.read_float("gravity", &plevel->gravity);
287 reader.read_string("name", &plevel->name);
288 reader.read_string("theme", &plevel->theme);
289 reader.read_string("music", &plevel->song_title);
290 reader.read_string("background", &plevel->bkgd_image);
291 reader.read_string("particle_system", &plevel->particle_system);
292 reader.read_int_vector("background-tm", &bg_tm);
294 if (!reader.read_int_vector("interactive-tm", &ia_tm))
295 reader.read_int_vector("tilemap", &ia_tm);
297 reader.read_int_vector("foreground-tm", &fg_tm);
300 lisp_object_t* cur = 0;
301 if (reader.read_lisp("objects", &cur))
303 while (!lisp_nil_p(cur))
305 lisp_object_t* data = lisp_car(cur);
308 bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
309 LispReader reader(lisp_cdr(data));
310 reader.read_int("x", &bg_data.x);
311 reader.read_int("y", &bg_data.y);
313 plevel->badguy_data.push_back(bg_data);
320 // Convert old levels to the new tile numbers
323 std::map<char, int> transtable;
325 transtable['0'] = 1000;
326 transtable['1'] = 1001;
327 transtable['2'] = 1002;
328 transtable['x'] = 104;
329 transtable['X'] = 77;
330 transtable['y'] = 78;
331 transtable['Y'] = 105;
332 transtable['A'] = 83;
333 transtable['B'] = 102;
334 transtable['!'] = 103;
335 transtable['a'] = 84;
336 transtable['C'] = 85;
337 transtable['D'] = 86;
338 transtable['E'] = 87;
339 transtable['F'] = 88;
340 transtable['c'] = 89;
341 transtable['d'] = 90;
342 transtable['e'] = 91;
343 transtable['f'] = 92;
345 transtable['G'] = 93;
346 transtable['H'] = 94;
347 transtable['I'] = 95;
348 transtable['J'] = 96;
350 transtable['g'] = 97;
351 transtable['h'] = 98;
352 transtable['i'] = 99;
353 transtable['j'] = 100
355 transtable['#'] = 11;
356 transtable['['] = 13;
357 transtable['='] = 14;
358 transtable[']'] = 15;
359 transtable['$'] = 82;
360 transtable['^'] = 76;
361 transtable['*'] = 80;
362 transtable['|'] = 79;
363 transtable['\\'] = 81;
364 transtable['&'] = 75;
366 for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
368 std::map<char, int>::iterator j = transtable.find(*i);
369 if (j != transtable.end())
372 printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
377 for(int i = 0; i < 15; ++i)
379 plevel->dn_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
380 plevel->ia_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
381 plevel->bg_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
382 plevel->fg_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
387 for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
389 plevel->ia_tiles[j][i] = (*it);
390 if(i == plevel->width - 1)
398 for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
401 plevel->bg_tiles[j][i] = (*it);
402 if(i == plevel->width - 1)
410 for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
413 plevel->fg_tiles[j][i] = (*it);
414 if(i == plevel->width - 1)
421 /* Set the global gravity to the latest loaded level's gravity */
422 gravity = plevel->gravity;
424 /* Mark the end position of this level! - Is a bit wrong here thought * /
426 for (y = 0; y < 15; ++y)
428 for (x = 0; x < plevel->width; ++x)
430 if(plevel->tiles[y][x] == '|')
442 /* Save data for level: */
444 void level_save(st_level* plevel,const char * subset, int level)
451 /* Save data file: */
452 sprintf(str, "/levels/%s/", subset);
454 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
455 if(!fwriteable(filename))
456 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
458 fi = fopen(filename, "w");
468 fprintf(fi,";SuperTux-Level\n");
469 fprintf(fi,"(supertux-level\n");
471 fprintf(fi," (version %d)\n", 1);
472 fprintf(fi," (name \"%s\")\n", plevel->name.c_str());
473 fprintf(fi," (theme \"%s\")\n", plevel->theme.c_str());
474 fprintf(fi," (music \"%s\")\n", plevel->song_title.c_str());
475 fprintf(fi," (background \"%s\")\n", plevel->bkgd_image.c_str());
476 fprintf(fi," (particle_system \"%s\")\n", plevel->particle_system.c_str());
477 fprintf(fi," (bkgd_red %d)\n", plevel->bkgd_red);
478 fprintf(fi," (bkgd_green %d)\n", plevel->bkgd_green);
479 fprintf(fi," (bkgd_blue %d)\n", plevel->bkgd_blue);
480 fprintf(fi," (time %d)\n", plevel->time_left);
481 fprintf(fi," (width %d)\n", plevel->width);
482 fprintf(fi," (gravity %2.1f)\n", plevel->gravity);
483 fprintf(fi," (background-tm ");
485 for(y = 0; y < 15; ++y)
487 for(i = 0; i < plevel->width; ++i)
488 fprintf(fi," %d ", plevel->bg_tiles[y][i]);
492 fprintf(fi," (interactive-tm ");
494 for(y = 0; y < 15; ++y)
496 for(i = 0; i < plevel->width; ++i)
497 fprintf(fi," %d ", plevel->ia_tiles[y][i]);
501 fprintf(fi," (dynamic-tm ");
503 for(y = 0; y < 15; ++y)
505 for(i = 0; i < plevel->width; ++i)
506 fprintf(fi," %d ", plevel->dn_tiles[y][i]);
510 fprintf(fi," (foreground-tm ");
512 for(y = 0; y < 15; ++y)
514 for(i = 0; i < plevel->width; ++i)
515 fprintf(fi," %d ", plevel->fg_tiles[y][i]);
525 /* Unload data for this level: */
527 void level_free(st_level* plevel)
530 for(i=0; i < 15; ++i)
531 free(plevel->bg_tiles[i]);
532 for(i=0; i < 15; ++i)
533 free(plevel->ia_tiles[i]);
534 for(i=0; i < 15; ++i)
535 free(plevel->dn_tiles[i]);
536 for(i=0; i < 15; ++i)
537 free(plevel->fg_tiles[i]);
539 plevel->name.clear();
540 plevel->theme.clear();
541 plevel->song_title.clear();
542 plevel->bkgd_image.clear();
547 void level_load_gfx(st_level *plevel)
549 level_load_image(&img_brick[0],plevel->theme,"brick0.png", IGNORE_ALPHA);
550 level_load_image(&img_brick[1],plevel->theme,"brick1.png", IGNORE_ALPHA);
552 level_load_image(&img_solid[0],plevel->theme,"solid0.png", USE_ALPHA);
553 level_load_image(&img_solid[1],plevel->theme,"solid1.png", USE_ALPHA);
554 level_load_image(&img_solid[2],plevel->theme,"solid2.png", USE_ALPHA);
555 level_load_image(&img_solid[3],plevel->theme,"solid3.png", USE_ALPHA);
557 level_load_image(&img_bkgd_tile[0][0],plevel->theme,"bkgd-00.png", USE_ALPHA);
558 level_load_image(&img_bkgd_tile[0][1],plevel->theme,"bkgd-01.png", USE_ALPHA);
559 level_load_image(&img_bkgd_tile[0][2],plevel->theme,"bkgd-02.png", USE_ALPHA);
560 level_load_image(&img_bkgd_tile[0][3],plevel->theme,"bkgd-03.png", USE_ALPHA);
562 level_load_image(&img_bkgd_tile[1][0],plevel->theme,"bkgd-10.png", USE_ALPHA);
563 level_load_image(&img_bkgd_tile[1][1],plevel->theme,"bkgd-11.png", USE_ALPHA);
564 level_load_image(&img_bkgd_tile[1][2],plevel->theme,"bkgd-12.png", USE_ALPHA);
565 level_load_image(&img_bkgd_tile[1][3],plevel->theme,"bkgd-13.png", USE_ALPHA);
567 if(!plevel->bkgd_image.empty())
570 snprintf(fname, 1024, "%s/background/%s", st_dir, plevel->bkgd_image.c_str());
571 if(!faccessible(fname))
572 snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), plevel->bkgd_image.c_str());
573 texture_load(&img_bkgd, fname, IGNORE_ALPHA);
577 /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */#
578 level_load_image(&img_bkgd, plevel->theme,"solid0.png", IGNORE_ALPHA);
582 /* Free graphics data for this level: */
584 void level_free_gfx(void)
588 for (i = 0; i < 2; i++)
590 texture_free(&img_brick[i]);
592 for (i = 0; i < 4; i++)
594 texture_free(&img_solid[i]);
595 texture_free(&img_bkgd_tile[0][i]);
596 texture_free(&img_bkgd_tile[1][i]);
599 texture_free(&img_bkgd);
602 /* Load a level-specific graphic... */
604 void level_load_image(texture_type* ptexture, string theme,const char * file, int use_alpha)
608 snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
609 if(!faccessible(fname))
610 snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
612 texture_load(ptexture, fname, use_alpha);
615 void tilemap_change_size(unsigned int** tilemap[15], int w, int old_w)
618 for(y = 0; y < 15; ++y)
620 *tilemap[y] = (unsigned int*) realloc(*tilemap[y],(w+1)*sizeof(unsigned int));
622 for(j = 0; j < w - old_w; ++j)
623 *tilemap[y][old_w+j] = 0;
628 /* Change the size of a level (width) */
629 void level_change_size (st_level* plevel, int new_width)
633 tilemap_change_size((unsigned int***)&plevel->ia_tiles,new_width,plevel->width);
634 tilemap_change_size((unsigned int***)&plevel->dn_tiles,new_width,plevel->width);
635 tilemap_change_size((unsigned int***)&plevel->bg_tiles,new_width,plevel->width);
636 tilemap_change_size((unsigned int***)&plevel->fg_tiles,new_width,plevel->width);
637 plevel->width = new_width;
641 /* Edit a piece of the map! */
643 void level_change(st_level* plevel, float x, float y, int tm, unsigned int c)
650 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= plevel->width)
655 plevel->bg_tiles[yy][xx] = c;
657 plevel->ia_tiles[yy][xx] = c;
659 plevel->dn_tiles[yy][xx] = c;
661 plevel->fg_tiles[yy][xx] = c;
666 /* Free music data for this level: */
668 void level_free_song(void)
670 free_music(level_song);
671 free_music(level_song_fast);
676 void level_load_song(st_level* plevel)
680 char * song_subtitle;
682 level_song = load_song(datadir + "/music/" + plevel->song_title);
684 song_path = (char *) malloc(sizeof(char) * datadir.length() +
685 strlen(plevel->song_title.c_str()) + 8 + 5);
686 song_subtitle = strdup(plevel->song_title.c_str());
687 strcpy(strstr(song_subtitle, "."), "\0");
688 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), song_subtitle, strstr(plevel->song_title.c_str(), "."));
689 level_song_fast = load_song(song_path);