- FILE * fi;
- lisp_object_t* root_obj = 0;
- fi = fopen(filename, "r");
- if (fi == NULL)
- {
- perror(filename);
- return -1;
- }
-
- lisp_stream_t stream;
- lisp_stream_init_file (&stream, fi);
- root_obj = lisp_read (&stream);
-
- if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
- {
- printf("World: Parse Error in file %s", filename);
- }
-
- vector<int> ia_tm;
- vector<int> bg_tm;
- vector<int> fg_tm;
-
- int version = 0;
- if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
- {
- LispReader reader(lisp_cdr(root_obj));
-
- reader.read_int("version", &version);
- reader.read_int("width", &plevel->width);
- reader.read_int("time", &plevel->time_left);
- reader.read_int("bkgd_red", &plevel->bkgd_red);
- reader.read_int("bkgd_green", &plevel->bkgd_green);
- reader.read_int("bkgd_blue", &plevel->bkgd_blue);
- reader.read_float("gravity", &plevel->gravity);
- reader.read_string("name", &plevel->name);
- reader.read_string("theme", &plevel->theme);
- reader.read_string("music", &plevel->song_title);
- reader.read_string("background", &plevel->bkgd_image);
- reader.read_string("particle_system", &plevel->particle_system);
- reader.read_int_vector("background-tm", &bg_tm);
-
- if (!reader.read_int_vector("interactive-tm", &ia_tm))
- reader.read_int_vector("tilemap", &ia_tm);
-
- reader.read_int_vector("foreground-tm", &fg_tm);
-
- {
- lisp_object_t* cur = 0;
- if (reader.read_lisp("objects", &cur))
- {
- while (!lisp_nil_p(cur))
- {
- lisp_object_t* data = lisp_car(cur);
-
- BadGuyData bg_data;
- bg_data.kind = badguykind_from_string(lisp_symbol(lisp_car(data)));
- LispReader reader(lisp_cdr(data));
- reader.read_int("x", &bg_data.x);
- reader.read_int("y", &bg_data.y);
-
- plevel->badguy_data.push_back(bg_data);
-
- cur = lisp_cdr(cur);
- }
- }
- }
-
- // Convert old levels to the new tile numbers
- if (version == 0)
- {
- std::map<char, int> transtable;
- transtable['.'] = 0;
- transtable['x'] = 104;
- transtable['X'] = 77;
- transtable['y'] = 78;
- transtable['Y'] = 105;
- transtable['A'] = 83;
- transtable['B'] = 102;
- transtable['!'] = 103;
- transtable['a'] = 84;
- transtable['C'] = 85;
- transtable['D'] = 86;
- transtable['E'] = 87;
- transtable['F'] = 88;
- transtable['c'] = 89;
- transtable['d'] = 90;
- transtable['e'] = 91;
- transtable['f'] = 92;
-
- transtable['G'] = 93;
- transtable['H'] = 94;
- transtable['I'] = 95;
- transtable['J'] = 96;
-
- transtable['g'] = 97;
- transtable['h'] = 98;
- transtable['i'] = 99;
- transtable['j'] = 100
- ;
- transtable['#'] = 11;
- transtable['['] = 13;
- transtable['='] = 14;
- transtable[']'] = 15;
- transtable['$'] = 82;
- transtable['^'] = 76;
- transtable['*'] = 80;
- transtable['|'] = 79;
- transtable['\\'] = 81;
- transtable['&'] = 75;
-
- int x = 0;
- int y = 0;
- for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
- {
- if (*i == '0' || *i == '1' || *i == '2')
- {
- plevel->badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
- x*32, y*32));
- *i = 0;
- }
- else
- {
- std::map<char, int>::iterator j = transtable.find(*i);
- if (j != transtable.end())
- *i = j->second;
- else
- printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
- }
- ++x;
- if (x >= plevel->width)
- {
- x = 0;
- ++y;
- }
- }
- }
- }
-
- for(int i = 0; i < 15; ++i)
- {
- plevel->ia_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
- plevel->bg_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
- plevel->fg_tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
- }
-
- int i = 0;
- int j = 0;
- for(vector<int>::iterator it = ia_tm.begin(); it != ia_tm.end(); ++it, ++i)
- {
- plevel->ia_tiles[j][i] = (*it);
- if(i == plevel->width - 1)
- {
- i = -1;
- ++j;
- }
- }
-
- i = j = 0;
- for(vector<int>::iterator it = bg_tm.begin(); it != bg_tm.end(); ++it, ++i)
- {
-
- plevel->bg_tiles[j][i] = (*it);
- if(i == plevel->width - 1)
- {
- i = -1;
- ++j;
- }
- }
-
- i = j = 0;
- for(vector<int>::iterator it = fg_tm.begin(); it != fg_tm.end(); ++it, ++i)
- {
-
- plevel->fg_tiles[j][i] = (*it);
- if(i == plevel->width - 1)
- {
- i = -1;
- ++j;
- }
- }
-
- /* Set the global gravity to the latest loaded level's gravity */
- gravity = plevel->gravity;
-
- // Mark the end position of this level!
- // FIXME: -10 is a rather random value, we still need some kind of
- // real levelend gola
- endpos = 32*(plevel->width-10);
-
- fclose(fi);
- return 0;