From 00b15f2c9ea01a7bd73a912ac5ac3369cb6b6d90 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Wed, 12 Apr 2006 01:07:25 +0000 Subject: [PATCH] more work on worlds SVN-Revision: 3311 --- data/levels/world1/world.nut | 2 +- src/file_system.cpp | 2 +- src/title.cpp | 75 ++++++++++++++++++++------------------------ src/title.hpp | 4 +-- src/world.cpp | 30 ++++++++++++++++-- src/worldmap.cpp | 24 ++++++++++++++ src/worldmap.hpp | 5 ++- 7 files changed, 93 insertions(+), 49 deletions(-) diff --git a/data/levels/world1/world.nut b/data/levels/world1/world.nut index 9d5880a16..41db5c98c 100644 --- a/data/levels/world1/world.nut +++ b/data/levels/world1/world.nut @@ -1,4 +1,3 @@ -display(state); if(! ("intro_displayed" in state)) { load_level("levels/world1/intro.stl"); wait_for_screenswitch(); @@ -8,6 +7,7 @@ if(! ("intro_displayed" in state)) { } load_worldmap("levels/world1/worldmap.stwm"); wait_for_screenswitch(); +save_state(); wait_for_screenswitch(); save_state(); print("Goodbye from world1\n"); diff --git a/src/file_system.cpp b/src/file_system.cpp index 57bcec407..d1303078b 100644 --- a/src/file_system.cpp +++ b/src/file_system.cpp @@ -25,7 +25,7 @@ std::string basename(const std::string& filename) if(p == std::string::npos) return filename; - return filename.substr(p, filename.size()-p); + return filename.substr(p+1, filename.size()-p-1); } std::string normalize(const std::string& filename) diff --git a/src/title.cpp b/src/title.cpp index be49dfc9b..187b6f0e1 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -86,8 +86,6 @@ TitleScreen::free_contrib_menu() delete *i; contrib_worlds.clear(); - current_contrib_world = 0; - current_world = -1; } void @@ -158,28 +156,21 @@ TitleScreen::check_levels_contrib_menu() if (index == -1) return; - World& world = * (contrib_worlds[index]); - - if(!world.is_levelset) { - world.set_savegame_filename("save/test.save"); - world.run(); - } - - if (current_world != index) { - current_world = index; - World& world = * (contrib_worlds[index]); - - current_contrib_world = &world; + current_world = contrib_worlds[index]; + if(!current_world->is_levelset) { + update_load_game_menu(); + Menu::push_current(load_game_menu.get()); + } else { contrib_world_menu.reset(new Menu()); - contrib_world_menu->add_label(world.title); + contrib_world_menu->add_label(current_world->title); contrib_world_menu->add_hl(); - for (unsigned int i = 0; i < world.get_num_levels(); ++i) + for (unsigned int i = 0; i < current_world->get_num_levels(); ++i) { /** get level's title */ - std::string filename = world.get_level_filename(i); + std::string filename = current_world->get_level_filename(i); std::string title = get_level_name(filename); contrib_world_menu->add_entry(i, title); } @@ -200,7 +191,7 @@ TitleScreen::check_contrib_world_menu() sound_manager->stop_music(); GameSession* session = new GameSession( - current_contrib_world->get_level_filename(index), ST_GL_PLAY); + current_world->get_level_filename(index), ST_GL_PLAY); main_loop->push_screen(session); } } @@ -335,6 +326,11 @@ TitleScreen::update(float elapsed_time) switch (main_menu->check()) { case MNID_STARTGAME: // Start Game, ie. goto the slots menu + if(main_world.get() == NULL) { + main_world.reset(new World()); + main_world->load("levels/world1/info"); + } + current_world = main_world.get(); update_load_game_menu(); Menu::push_current(load_game_menu.get()); break; @@ -387,11 +383,14 @@ std::string TitleScreen::get_slotinfo(int slot) { std::string tmp; - std::string slotfile; std::string title; - std::stringstream stream; - stream << slot; - slotfile = "save/slot" + stream.str() + ".stsg"; + + std::string basename = current_world->get_basedir(); + basename = basename.substr(0, basename.length()-1); + std::string worlddirname = FileSystem::basename(basename); + std::ostringstream stream; + stream << "save/" << worlddirname << "_" << slot << ".stsg"; + std::string slotfile = stream.str(); try { lisp::Parser parser; @@ -403,11 +402,14 @@ TitleScreen::get_slotinfo(int slot) savegame->get("title", title); } catch(std::exception& e) { - return std::string(_("Slot")) + " " + stream.str() + " - " + - std::string(_("Free")); + std::ostringstream slottitle; + slottitle << _("Slot") << " " << slot << " - " << _("Free"); + return slottitle.str(); } - return std::string("Slot ") + stream.str() + " - " + title; + std::ostringstream slottitle; + slottitle << _("Slot") << " " << slot << " - " << title; + return slottitle.str(); } bool @@ -421,27 +423,18 @@ TitleScreen::process_load_game_menu() if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION) return false; + std::string basename = current_world->get_basedir(); + basename = basename.substr(0, basename.length()-1); + std::string worlddirname = FileSystem::basename(basename); std::stringstream stream; - stream << slot; - std::string slotfile = "save/slot" + stream.str() + ".stsg"; + stream << "save/" << worlddirname << "_" << slot << ".stsg"; + std::string slotfile = stream.str(); sound_manager->stop_music(); fadeout(256); - DrawingContext context; - context.draw_text(white_text, "Loading...", - Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), - CENTER_ALLIGN, LAYER_FOREGROUND1); - context.do_drawing(); - - WorldMapNS::WorldMap* worldmap = new WorldMapNS::WorldMap(); - - worldmap->set_map_filename("/levels/world1/worldmap.stwm"); - // Load the game or at least set the savegame_file variable - //worldmap->loadgame(slotfile); - - main_loop->push_screen(worldmap); - //Menu::set_current(main_menu); + current_world->set_savegame_filename(slotfile); + current_world->run(); return true; } diff --git a/src/title.hpp b/src/title.hpp index 5ff13a119..8854f83b9 100644 --- a/src/title.hpp +++ b/src/title.hpp @@ -57,9 +57,9 @@ private: std::auto_ptr load_game_menu; std::auto_ptr contrib_menu; std::auto_ptr contrib_world_menu; + std::auto_ptr main_world; std::vector contrib_worlds; - int current_world; - World* current_contrib_world; + World* current_world; std::auto_ptr controller; std::auto_ptr titlesession; diff --git a/src/world.cpp b/src/world.cpp index fb8209625..85f3d9c9f 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -32,6 +32,8 @@ #include "scripting/wrapper_util.hpp" #include "scripting/serialize.hpp" #include "msg.hpp" +#include "worldmap.hpp" +#include "mainloop.hpp" static bool has_suffix(const std::string& data, const std::string& suffix) { @@ -120,10 +122,18 @@ World::run() load_state(); std::string filename = basedir + "/world.nut"; - IFileStream in(filename); + try { + IFileStream in(filename); - HSQUIRRELVM new_vm = ScriptManager::instance->create_thread(); - Scripting::compile_and_run(new_vm, in, filename); + HSQUIRRELVM new_vm = ScriptManager::instance->create_thread(); + Scripting::compile_and_run(new_vm, in, filename); + } catch(std::exception& e) { + using namespace WorldMapNS; + // fallback try to load worldmap + std::auto_ptr worldmap (new WorldMap); + worldmap->loadmap(basedir + "worldmap.stwm"); + main_loop->push_screen(worldmap.release()); + } } void @@ -133,6 +143,15 @@ World::save_state() writer.start_list("supertux-savegame"); writer.write_int("version", 1); + + using namespace WorldMapNS; + if(WorldMap::current() != NULL) { + std::ostringstream title; + title << WorldMap::current()->get_title(); + title << " (" << WorldMap::current()->solved_level_count() + << "/" << WorldMap::current()->level_count() << ")"; + writer.write_string("title", title.str()); + } writer.start_list("tux"); player_status->write(writer); @@ -206,3 +225,8 @@ World::get_num_levels() const return levels.size(); } +const std::string& +World::get_basedir() const +{ + return basedir; +} diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 65eb5759e..25274b0a7 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -48,6 +48,7 @@ #include "resources.hpp" #include "misc.hpp" #include "msg.hpp" +#include "world.hpp" #include "player_status.hpp" #include "textscroller.hpp" #include "main.hpp" @@ -363,6 +364,9 @@ WorldMap::WorldMap() WorldMap::~WorldMap() { + if(current_ == this) + current_ = NULL; + clear_objects(); for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) { @@ -685,6 +689,8 @@ WorldMap::finished_level(const std::string& filename) calculate_total_stats(); save_state(); + if(World::current() != NULL) + World::current()->save_state(); if (old_level_state != level->solved && level->auto_path) { // Try to detect the next direction to which we should walk @@ -1194,6 +1200,24 @@ WorldMap::load_state() } sq_settop(vm, oldtop); } + +size_t +WorldMap::level_count() +{ + return levels.size(); +} + +size_t +WorldMap::solved_level_count() +{ + size_t count = 0; + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) { + if(i->solved) + count++; + } + + return count; +} void WorldMap::loadmap(const std::string& filename) diff --git a/src/worldmap.hpp b/src/worldmap.hpp index 67b697e0b..ec36ac6bc 100644 --- a/src/worldmap.hpp +++ b/src/worldmap.hpp @@ -246,6 +246,9 @@ public: Vector get_next_tile(Vector pos, Direction direction); const Tile* at(Vector pos); + size_t level_count(); + size_t solved_level_count(); + /** * gets called from the GameSession when a level has been successfully * finished @@ -272,7 +275,7 @@ public: */ void loadmap(const std::string& filename); - const std::string& get_world_title() const + const std::string& get_title() const { return name; } void set_map_filename(std::string filename) -- 2.11.0