#include <string.h>
#include <iostream>
#include <fstream>
+#include <stdexcept>
#include "globals.h"
#include "setup.h"
-#include "screen.h"
+#include "camera.h"
+#include "screen/screen.h"
#include "level.h"
#include "physic.h"
#include "scene.h"
+#include "sector.h"
#include "tile.h"
#include "lispreader.h"
#include "resources.h"
#include "music_manager.h"
#include "gameobjs.h"
-#include "world.h"
#include "lispwriter.h"
using namespace std;
new_subset.title = "Unknown Title";
new_subset.description = "No description so far.";
new_subset.save();
- new_lev.init_defaults();
- new_lev.save(subset_name, 1, 0);
+ //new_lev.save(subset_name, 1, 0);
}
void LevelSubset::parse (lisp_object_t* cursor)
}
}
-void LevelSubset::load(char *subset)
+void LevelSubset::load(const char* subset)
{
FILE* fi;
char filename[1024];
levels = --i;
}
-void LevelSubset::save()
+void
+LevelSubset::save()
{
FILE* fi;
string filename;
fprintf( fi,")");
fclose(fi);
-
}
}
-Level::Level()
- : img_bkgd(0)
-{
- init_defaults();
-}
-
-Level::~Level()
-{
- delete img_bkgd;
-}
-
-void
-Level::init_defaults()
-{
- name = "UnNamed";
- author = "UnNamed";
- song_title = "Mortimers_chipdisko.mod";
- bkgd_image = "arctis.png";
- width = 0;
- height = 0;
- start_pos_x = 100;
- start_pos_y = 170;
- time_left = 100;
- gravity = 10.;
- back_scrolling = false;
- hor_autoscroll_speed = 0;
- bkgd_speed = 50;
- bkgd_top.red = 0;
- bkgd_top.green = 0;
- bkgd_top.blue = 0;
- bkgd_bottom.red = 255;
- bkgd_bottom.green = 255;
- bkgd_bottom.blue = 255;
-
- resize(21, 19);
-}
-
-int
-Level::load(const std::string& subset, int level, World* world)
+std::string
+LevelSubset::get_level_filename(unsigned int num)
{
char filename[1024];
-
+
// Load data file:
- snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(), level);
+ snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir,
+ name.c_str(), num);
if(!faccessible(filename))
- snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset.c_str(), level);
-
- return load(filename, world);
-}
-
-int
-Level::load(const std::string& filename, World* world)
-{
- lisp_object_t* root_obj = lisp_read_from_file(filename);
- if (!root_obj)
- {
- std::cout << "Level: Couldn't load file: " << filename << std::endl;
- return -1;
- }
-
- if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
- {
- printf("World: Parse Error in file %s", filename.c_str());
- return -1;
- }
-
- int version = 0;
- if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
- {
- LispReader reader(lisp_cdr(root_obj));
- version = 0;
- reader.read_int("version", &version);
- if(!reader.read_int("width", &width))
- st_abort("No width specified for level.", "");
- if (!reader.read_int("start_pos_x", &start_pos_x)) start_pos_x = 100;
- if (!reader.read_int("start_pos_y", &start_pos_y)) start_pos_y = 170;
- time_left = 500;
- if(!reader.read_int("time", &time_left)) {
- printf("Warning no time specified for level.\n");
- }
-
- height = 15;
- reader.read_int("height", &height);
-
- back_scrolling = false;
- reader.read_bool("back_scrolling", &back_scrolling);
-
- hor_autoscroll_speed = 0;
- reader.read_float("hor_autoscroll_speed", &hor_autoscroll_speed);
-
- bkgd_speed = 50;
- reader.read_int("bkgd_speed", &bkgd_speed);
-
-
- bkgd_top.red = bkgd_top.green = bkgd_top.blue = 0;
- reader.read_int("bkgd_red_top", &bkgd_top.red);
- reader.read_int("bkgd_green_top", &bkgd_top.green);
- reader.read_int("bkgd_blue_top", &bkgd_top.blue);
-
- bkgd_bottom.red = bkgd_bottom.green = bkgd_bottom.blue = 0;
- reader.read_int("bkgd_red_bottom", &bkgd_bottom.red);
- reader.read_int("bkgd_green_bottom", &bkgd_bottom.green);
- reader.read_int("bkgd_blue_bottom", &bkgd_bottom.blue);
-
- gravity = 10;
- reader.read_float("gravity", &gravity);
- name = "Noname";
- reader.read_string("name", &name);
- author = "unknown author";
- reader.read_string("author", &author);
- song_title = "";
- reader.read_string("music", &song_title);
- bkgd_image = "";
- reader.read_string("background", &bkgd_image);
- particle_system = "";
- reader.read_string("particle_system", &particle_system);
-
- reader.read_int_vector("background-tm", &bg_tiles);
- if(int(bg_tiles.size()) != width * height)
- st_abort("Wrong size of backgroundtilemap", "");
-
- if (!reader.read_int_vector("interactive-tm", &ia_tiles))
- reader.read_int_vector("tilemap", &ia_tiles);
- if(int(ia_tiles.size()) != width * height)
- st_abort("Wrong size of interactivetilemap", "");
-
- reader.read_int_vector("foreground-tm", &fg_tiles);
- if(int(fg_tiles.size()) != width * height)
- st_abort("Wrong size of foregroundtilemap", "");
-
- { // Read ResetPoints
- lisp_object_t* cur = 0;
- if (reader.read_lisp("reset-points", &cur))
- {
- while (!lisp_nil_p(cur))
- {
- lisp_object_t* data = lisp_car(cur);
-
- ResetPoint pos;
-
- LispReader reader(lisp_cdr(data));
- if (reader.read_int("x", &pos.x)
- && reader.read_int("y", &pos.y))
- {
- reset_points.push_back(pos);
- }
-
- cur = lisp_cdr(cur);
- }
- }
- }
-
- { // Read BadGuys
- lisp_object_t* cur = 0;
- if (reader.read_lisp("objects", &cur))
- {
- if(world)
- world->parse_objects(cur);
- }
- }
-
-#if 0 // TODO fix this or remove it
- // 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')
- {
- badguy_data.push_back(BadGuyData(static_cast<BadGuyKind>(*i-'0'),
- x*32, y*32, false));
- *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 >= width)
- {
- x = 0;
- ++y;
- }
- }
- }
-#endif
- }
-
- lisp_free(root_obj);
- return 0;
-}
-
-/* Save data for level: */
-
-void
-Level::save(const std::string& subset, int level, World* world)
-{
- char filename[1024];
- char str[80];
-
- /* Save data file: */
- sprintf(str, "/levels/%s/", subset.c_str());
- fcreatedir(str);
- snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset.c_str(),
- level);
- if(!fwriteable(filename))
snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(),
- subset.c_str(), level);
-
- std::ofstream out(filename);
- if(!out.good()) {
- st_abort("Couldn't write file.", filename);
- }
- LispWriter writer(out);
-
- /* Write header: */
- writer.writeComment("SuperTux level made using the built-in leveleditor");
- writer.startList("supertux-level");
-
- writer.writeInt("version", 1);
- writer.writeString("name", name);
- writer.writeString("author", author);
- writer.writeString("music", song_title);
- writer.writeString("background", bkgd_image);
- writer.writeString("particle_system", particle_system);
- writer.writeInt("bkgd_speed", bkgd_speed);
- writer.writeInt("bkgd_red_top", bkgd_top.red);
- writer.writeInt("bkgd_green_top", bkgd_top.green);
- writer.writeInt("bkgd_blue_top", bkgd_top.blue);
- writer.writeInt("bkgd_red_bottom", bkgd_bottom.red);
- writer.writeInt("bkgd_green_bottom", bkgd_bottom.green);
- writer.writeInt("bkgd_blue_bottom", bkgd_bottom.blue);
- writer.writeInt("time", time_left);
- writer.writeInt("width", width);
- writer.writeInt("height", height);
- writer.writeBool("back_scrolling", back_scrolling);
- writer.writeFloat("hor_autoscroll_speed", hor_autoscroll_speed);
- writer.writeFloat("gravity", gravity);
-
- writer.writeIntVector("background-tm", bg_tiles);
- writer.writeIntVector("interactive-tm", ia_tiles);
- writer.writeIntVector("foreground-tm", fg_tiles);
-
- writer.startList("reset-points");
- for(std::vector<ResetPoint>::iterator i = reset_points.begin();
- i != reset_points.end(); ++i) {
- writer.startList("point");
- writer.writeInt("x", i->x);
- writer.writeInt("y", i->y);
- }
- writer.endList("reset-points");
-
- // write objects
- writer.startList("objects");
- // pick all objects that can be written into a levelfile
- for(std::vector<_GameObject*>::iterator it = world->gameobjects.begin();
- it != world->gameobjects.end(); ++it) {
- Serializable* serializable = dynamic_cast<Serializable*> (*it);
- if(serializable)
- serializable->write(writer);
- }
- writer.endList("objects");
+ name.c_str(), num);
- writer.endList("supertux-level");
- out.close();
+ return std::string(filename);
}
-/* Unload data for this level: */
-void
-Level::cleanup()
-{
- bg_tiles.clear();
- ia_tiles.clear();
- fg_tiles.clear();
-
- reset_points.clear();
- name = "";
- author = "";
- song_title = "";
- bkgd_image = "";
-}
+//---------------------------------------------------------------------------
-void
-Level::load_gfx()
+Level::Level()
+ : name("noname"), author("mr. x"), time_left(500)
{
- if(!bkgd_image.empty())
- {
- char fname[1024];
- snprintf(fname, 1024, "%s/background/%s", st_dir, bkgd_image.c_str());
- if(!faccessible(fname))
- snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), bkgd_image.c_str());
- delete img_bkgd;
- img_bkgd = new Surface(fname, IGNORE_ALPHA);
- }
- else
- {
- delete img_bkgd;
- img_bkgd = 0;
- }
}
-/* Load a level-specific graphic... */
-void Level::load_image(Surface** ptexture, string theme,const char * file, int use_alpha)
+void
+Level::load(const std::string& filename)
{
- char fname[1024];
+ LispReader* level = LispReader::load(filename, "supertux-level");
- snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
- if(!faccessible(fname))
- snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
-
- *ptexture = new Surface(fname, use_alpha);
-}
+ int version = 1;
+ level->read_int("version", version);
+ if(version == 1) {
+ load_old_format(*level);
+ return;
+ }
-/* Change the size of a level */
-void
-Level::resize(int new_width, int new_height)
-{
- // first: resize height
- ia_tiles.resize(new_height * width, 0);
- bg_tiles.resize(new_height * width, 0);
- fg_tiles.resize(new_height * width, 0);
- height = new_height;
-
- // remap horizontal tiles for new width
- int np = 0;
- for(int y = 0; y < height; ++y) {
- for(int x = 0; x < new_width && x < width; ++x) {
- ia_tiles[np] = ia_tiles[y * width + x];
- bg_tiles[np] = bg_tiles[y * width + x];
- fg_tiles[np] = fg_tiles[y * width + x];
- np++;
+ for(lisp_object_t* cur = level->get_lisp(); !lisp_nil_p(cur);
+ cur = lisp_cdr(cur)) {
+ std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
+ lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
+ LispReader reader(lisp_cdr(lisp_car(cur)));
+
+ if(token == "name") {
+ name = lisp_string(data);
+ } else if(token == "author") {
+ author = lisp_string(data);
+ } else if(token == "time") {
+ time_left = lisp_integer(data);
+ } else if(token == "sector") {
+ Sector* sector = new Sector;
+ sector->parse(reader);
+ add_sector(sector);
+ } else {
+ std::cerr << "Unknown token '" << token << "' in level file.\n";
+ continue;
}
}
-
- ia_tiles.resize(new_height * new_width);
- bg_tiles.resize(new_height * new_width);
- fg_tiles.resize(new_height * new_width);
- width = new_width;
+ delete level;
}
void
-Level::change(float x, float y, int tm, unsigned int c)
+Level::load_old_format(LispReader& reader)
{
- int yy = ((int)y / 32);
- int xx = ((int)x / 32);
+ reader.read_string("name", name);
+ reader.read_string("author", author);
+ reader.read_int("time", time_left);
- if (yy >= 0 && yy < height && xx >= 0 && xx <= width)
- {
- switch(tm)
- {
- case TM_BG:
- bg_tiles[yy * width + xx] = c;
- break;
- case TM_IA:
- ia_tiles[yy * width + xx] = c;
- break;
- case TM_FG:
- fg_tiles[yy * width + xx] = c;
- break;
- }
- }
+ Sector* sector = new Sector;
+ sector->parse_old_format(reader);
+ add_sector(sector);
}
-void Level::draw_bg()
+Level::~Level()
{
- if(img_bkgd)
- {
- // Tile background horizontally
- int sx = (int)((float)scroll_x * ((float)bkgd_speed/100.0f)) % img_bkgd->w;
- int sy = (int)((float)scroll_y * ((float)bkgd_speed/100.0f)) % img_bkgd->h;
- for (int x = 0; (x-1)*img_bkgd->w <= screen->w; x++)
- for (int y = 0; (y-1)*img_bkgd->h <= screen->h; y++)
- img_bkgd->draw_part(x == 0 ? sx : 0, y == 0 ? sy : 0,
- x == 0 ? 0 : (img_bkgd->w * x) - sx, y == 0 ? 0 : (img_bkgd->h * y) - sy,
- x == 0 ? img_bkgd->w - sx : img_bkgd->w, y == 0 ? img_bkgd->h - sy : img_bkgd->h);
- }
- else
- {
- drawgradient(bkgd_top, bkgd_bottom);
- }
+ for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
+ delete i->second;
}
void
-Level::load_song()
+Level::add_sector(Sector* sector)
{
- char* song_path;
- char* song_subtitle;
-
- level_song = music_manager->load_music(datadir + "/music/" + song_title);
-
- song_path = (char *) malloc(sizeof(char) * datadir.length() +
- strlen(song_title.c_str()) + 8 + 5);
- song_subtitle = strdup(song_title.c_str());
- strcpy(strstr(song_subtitle, "."), "\0");
- sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
- song_subtitle, strstr(song_title.c_str(), "."));
- if(!music_manager->exists_music(song_path)) {
- level_song_fast = level_song;
- } else {
- level_song_fast = music_manager->load_music(song_path);
- }
- free(song_subtitle);
- free(song_path);
+ sectors.insert(std::make_pair(sector->get_name(), sector));
}
-MusicRef
-Level::get_level_music()
+Sector*
+Level::get_sector(const std::string& name)
{
- return level_song;
-}
-
-MusicRef
-Level::get_level_music_fast()
-{
- return level_song_fast;
-}
-
-unsigned int
-Level::gettileid(float x, float y) const
-{
- int xx, yy;
- unsigned int c;
-
- yy = ((int)y / 32);
- xx = ((int)x / 32);
-
- if (yy >= 0 && yy < height && xx >= 0 && xx <= width)
- c = ia_tiles[yy * width + xx];
- else
- c = 0;
-
- return c;
-}
-
-unsigned int
-Level::get_tile_at(int x, int y) const
-{
- if(x < 0 || x >= width || y < 0 || y >= height)
+ Sectors::iterator i = sectors.find(name);
+ if(i == sectors.end())
return 0;
-
- return ia_tiles[y * width + x];
+
+ return i->second;
}
-/* EOF */