more work on worlds
authorMatthias Braun <matze@braunis.de>
Wed, 12 Apr 2006 01:07:25 +0000 (01:07 +0000)
committerMatthias Braun <matze@braunis.de>
Wed, 12 Apr 2006 01:07:25 +0000 (01:07 +0000)
SVN-Revision: 3311

data/levels/world1/world.nut
src/file_system.cpp
src/title.cpp
src/title.hpp
src/world.cpp
src/worldmap.cpp
src/worldmap.hpp

index 9d5880a..41db5c9 100644 (file)
@@ -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");
index 57bcec4..d130307 100644 (file)
@@ -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)
index be49dfc..187b6f0 100644 (file)
@@ -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;
 }
index 5ff13a1..8854f83 100644 (file)
@@ -57,9 +57,9 @@ private:
   std::auto_ptr<Menu> load_game_menu;
   std::auto_ptr<Menu> contrib_menu;
   std::auto_ptr<Menu> contrib_world_menu;
+  std::auto_ptr<World> main_world;
   std::vector<World*> contrib_worlds;
-  int current_world;
-  World* current_contrib_world;
+  World* current_world;
   
   std::auto_ptr<CodeController> controller;
   std::auto_ptr<GameSession> titlesession;
index fb82096..85f3d9c 100644 (file)
@@ -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> 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;
+}
index 65eb575..25274b0 100644 (file)
@@ -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)
index 67b697e..ec36ac6 100644 (file)
@@ -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)