2 // C Implementation: level
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2003
9 // Copyright: See COPYING file that comes with this distribution
23 #include "lispreader.h"
27 texture_type img_bkgd, img_bkgd_tile[2][4], img_solid[4], img_brick[2];
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 level_default(&new_lev);
43 level_save(&new_lev,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 texture_load(&image,str,IGNORE_ALPHA);
129 snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
130 texture_load(&image,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()
189 texture_free(&image);
193 void level_default(st_level* plevel)
196 plevel->name = "UnNamed";
197 plevel->theme = "antarctica";
198 plevel->song_title = "Mortimers_chipdisko.mod";
199 plevel->bkgd_image = "arctis.png";
201 plevel->time_left = 100;
202 plevel->gravity = 10.;
203 plevel->bkgd_red = 0;
204 plevel->bkgd_green = 0;
205 plevel->bkgd_blue = 0;
207 for(i = 0; i < 15; ++i)
209 plevel->ia_tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
210 plevel->ia_tiles[i][plevel->width] = (unsigned int) '\0';
211 for(y = 0; y < plevel->width; ++y)
212 plevel->ia_tiles[i][y] = (unsigned int) '.';
213 plevel->ia_tiles[i][plevel->width] = (unsigned int) '\0';
215 plevel->bg_tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
216 plevel->bg_tiles[i][plevel->width] = (unsigned int) '\0';
217 for(y = 0; y < plevel->width; ++y)
218 plevel->bg_tiles[i][y] = (unsigned int) '.';
219 plevel->bg_tiles[i][plevel->width] = (unsigned int) '\0';
221 plevel->fg_tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
222 plevel->fg_tiles[i][plevel->width] = (unsigned int) '\0';
223 for(y = 0; y < plevel->width; ++y)
224 plevel->fg_tiles[i][y] = (unsigned int) '.';
225 plevel->fg_tiles[i][plevel->width] = (unsigned int) '\0';
227 plevel->dn_tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
228 plevel->dn_tiles[i][plevel->width] = (unsigned int) '\0';
229 for(y = 0; y < plevel->width; ++y)
230 plevel->dn_tiles[i][y] = (unsigned int) '.';
231 plevel->dn_tiles[i][plevel->width] = (unsigned int) '\0';
235 /* Load data for this level: */
236 /* Returns -1, if the loading of the level failed. */
237 int level_load(st_level* plevel, const char *subset, int level)
241 /* Load data file: */
243 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
244 if(!faccessible(filename))
245 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
247 return level_load(plevel, filename);
250 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("width", &plevel->width);
281 reader.read_int("time", &plevel->time_left);
282 reader.read_int("bkgd_red", &plevel->bkgd_red);
283 reader.read_int("bkgd_green", &plevel->bkgd_green);
284 reader.read_int("bkgd_blue", &plevel->bkgd_blue);
285 reader.read_float("gravity", &plevel->gravity);
286 reader.read_string("name", &plevel->name);
287 reader.read_string("theme", &plevel->theme);
288 reader.read_string("music", &plevel->song_title);
289 reader.read_string("background", &plevel->bkgd_image);
290 reader.read_string("particle_system", &plevel->particle_system);
291 reader.read_int_vector("background-tm", &bg_tm);
292 reader.read_int_vector("interactive-tm", &ia_tm);
293 reader.read_int_vector("dynamic-tm", &dn_tm);
294 reader.read_int_vector("foreground-tm", &fg_tm);
299 for( i = 0; i < 15; ++i)
301 plevel->dn_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
302 plevel->ia_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
303 plevel->bg_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
304 plevel->fg_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
308 for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
311 plevel->ia_tiles[j][i] = (*it);
312 if(i == plevel->width - 1)
320 for(vector<int>::iterator it = dn_tm.begin(); it != dn_tm.end(); ++it, ++i)
323 plevel->dn_tiles[j][i] = (*it);
324 if(i == plevel->width - 1)
332 for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
335 plevel->bg_tiles[j][i] = (*it);
336 if(i == plevel->width - 1)
344 for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
347 plevel->fg_tiles[j][i] = (*it);
348 if(i == plevel->width - 1)
355 /* Set the global gravity to the latest loaded level's gravity */
356 gravity = plevel->gravity;
358 /* Mark the end position of this level! - Is a bit wrong here thought * /
360 for (y = 0; y < 15; ++y)
362 for (x = 0; x < plevel->width; ++x)
364 if(plevel->tiles[y][x] == '|')
376 /* Save data for level: */
378 void level_save(st_level* plevel,const char * subset, int level)
385 /* Save data file: */
386 sprintf(str, "/levels/%s/", subset);
388 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
389 if(!fwriteable(filename))
390 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
392 fi = fopen(filename, "w");
402 fprintf(fi,";SuperTux-Level\n");
403 fprintf(fi,"(supertux-level\n");
405 fprintf(fi," (name \"%s\")\n", plevel->name.c_str());
406 fprintf(fi," (theme \"%s\")\n", plevel->theme.c_str());
407 fprintf(fi," (music \"%s\")\n", plevel->song_title.c_str());
408 fprintf(fi," (background \"%s\")\n", plevel->bkgd_image.c_str());
409 fprintf(fi," (particle_system \"%s\")\n", plevel->particle_system.c_str());
410 fprintf(fi," (bkgd_red %d)\n", plevel->bkgd_red);
411 fprintf(fi," (bkgd_green %d)\n", plevel->bkgd_green);
412 fprintf(fi," (bkgd_blue %d)\n", plevel->bkgd_blue);
413 fprintf(fi," (time %d)\n", plevel->time_left);
414 fprintf(fi," (width %d)\n", plevel->width);
415 fprintf(fi," (gravity %2.1f)\n", plevel->gravity);
416 fprintf(fi," (background-tm ");
418 for(y = 0; y < 15; ++y)
420 for(i = 0; i < plevel->width; ++i)
421 fprintf(fi," %d ", plevel->bg_tiles[y][i]);
425 fprintf(fi," (interactive-tm ");
427 for(y = 0; y < 15; ++y)
429 for(i = 0; i < plevel->width; ++i)
430 fprintf(fi," %d ", plevel->ia_tiles[y][i]);
434 fprintf(fi," (dynamic-tm ");
436 for(y = 0; y < 15; ++y)
438 for(i = 0; i < plevel->width; ++i)
439 fprintf(fi," %d ", plevel->dn_tiles[y][i]);
443 fprintf(fi," (foreground-tm ");
445 for(y = 0; y < 15; ++y)
447 for(i = 0; i < plevel->width; ++i)
448 fprintf(fi," %d ", plevel->fg_tiles[y][i]);
458 /* Unload data for this level: */
460 void level_free(st_level* plevel)
463 for(i=0; i < 15; ++i)
464 free(plevel->bg_tiles[i]);
465 for(i=0; i < 15; ++i)
466 free(plevel->ia_tiles[i]);
467 for(i=0; i < 15; ++i)
468 free(plevel->dn_tiles[i]);
469 for(i=0; i < 15; ++i)
470 free(plevel->fg_tiles[i]);
472 plevel->name.clear();
473 plevel->theme.clear();
474 plevel->song_title.clear();
475 plevel->bkgd_image.clear();
480 void level_load_gfx(st_level *plevel)
482 level_load_image(&img_brick[0],plevel->theme,"brick0.png", IGNORE_ALPHA);
483 level_load_image(&img_brick[1],plevel->theme,"brick1.png", IGNORE_ALPHA);
485 level_load_image(&img_solid[0],plevel->theme,"solid0.png", USE_ALPHA);
486 level_load_image(&img_solid[1],plevel->theme,"solid1.png", USE_ALPHA);
487 level_load_image(&img_solid[2],plevel->theme,"solid2.png", USE_ALPHA);
488 level_load_image(&img_solid[3],plevel->theme,"solid3.png", USE_ALPHA);
490 level_load_image(&img_bkgd_tile[0][0],plevel->theme,"bkgd-00.png", USE_ALPHA);
491 level_load_image(&img_bkgd_tile[0][1],plevel->theme,"bkgd-01.png", USE_ALPHA);
492 level_load_image(&img_bkgd_tile[0][2],plevel->theme,"bkgd-02.png", USE_ALPHA);
493 level_load_image(&img_bkgd_tile[0][3],plevel->theme,"bkgd-03.png", USE_ALPHA);
495 level_load_image(&img_bkgd_tile[1][0],plevel->theme,"bkgd-10.png", USE_ALPHA);
496 level_load_image(&img_bkgd_tile[1][1],plevel->theme,"bkgd-11.png", USE_ALPHA);
497 level_load_image(&img_bkgd_tile[1][2],plevel->theme,"bkgd-12.png", USE_ALPHA);
498 level_load_image(&img_bkgd_tile[1][3],plevel->theme,"bkgd-13.png", USE_ALPHA);
500 if(!plevel->bkgd_image.empty())
503 snprintf(fname, 1024, "%s/background/%s", st_dir, plevel->bkgd_image.c_str());
504 if(!faccessible(fname))
505 snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), plevel->bkgd_image.c_str());
506 texture_load(&img_bkgd, fname, IGNORE_ALPHA);
510 /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */#
511 level_load_image(&img_bkgd, plevel->theme,"solid0.png", IGNORE_ALPHA);
515 /* Free graphics data for this level: */
517 void level_free_gfx(void)
521 for (i = 0; i < 2; i++)
523 texture_free(&img_brick[i]);
525 for (i = 0; i < 4; i++)
527 texture_free(&img_solid[i]);
528 texture_free(&img_bkgd_tile[0][i]);
529 texture_free(&img_bkgd_tile[1][i]);
532 texture_free(&img_bkgd);
535 /* Load a level-specific graphic... */
537 void level_load_image(texture_type* ptexture, string theme,const char * file, int use_alpha)
541 snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
542 if(!faccessible(fname))
543 snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
545 texture_load(ptexture, fname, use_alpha);
548 void tilemap_change_size(unsigned int** tilemap[15], int w, int old_w)
551 for(y = 0; y < 15; ++y)
553 *tilemap[y] = (unsigned int*) realloc(*tilemap[y],(w+1)*sizeof(unsigned int));
555 for(j = 0; j < w - old_w; ++j)
556 *tilemap[y][old_w+j] = 0;
561 /* Change the size of a level (width) */
562 void level_change_size (st_level* plevel, int new_width)
568 tilemap_change_size((unsigned int***)&plevel->ia_tiles,new_width,plevel->width);
569 tilemap_change_size((unsigned int***)&plevel->dn_tiles,new_width,plevel->width);
570 tilemap_change_size((unsigned int***)&plevel->bg_tiles,new_width,plevel->width);
571 tilemap_change_size((unsigned int***)&plevel->fg_tiles,new_width,plevel->width);
572 plevel->width = new_width;
576 /* Edit a piece of the map! */
578 void level_change(st_level* plevel, float x, float y, int tm, unsigned int c)
585 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= plevel->width)
590 plevel->bg_tiles[yy][xx] = c;
592 plevel->ia_tiles[yy][xx] = c;
594 plevel->dn_tiles[yy][xx] = c;
596 plevel->fg_tiles[yy][xx] = c;
601 /* Free music data for this level: */
603 void level_free_song(void)
605 free_music(level_song);
606 free_music(level_song_fast);
611 void level_load_song(st_level* plevel)
615 char * song_subtitle;
617 level_song = load_song(datadir + "/music/" + plevel->song_title);
619 song_path = (char *) malloc(sizeof(char) * datadir.length() +
620 strlen(plevel->song_title.c_str()) + 8 + 5);
621 song_subtitle = strdup(plevel->song_title.c_str());
622 strcpy(strstr(song_subtitle, "."), "\0");
623 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), song_subtitle, strstr(plevel->song_title.c_str(), "."));
624 level_song_fast = load_song(song_path);