Forgot <stdexcept>... again.
[supertux.git] / src / main.cpp
index c1bd127..94045d6 100644 (file)
@@ -53,6 +53,8 @@
 #include "scripting/squirrel_util.hpp"
 #include "file_system.hpp"
 #include "physfs/physfs_sdl.hpp"
+#include "random_generator.hpp"
+#include "worldmap/worldmap.hpp"
 
 SDL_Surface* screen = 0;
 JoystickKeyboardController* main_controller = 0;
@@ -287,6 +289,15 @@ static void init_sdl()
       ;
 }
 
+static void init_rand()
+{
+  const char *how = config->random_seed? ", user fixed.": ", from time().";
+
+  config->random_seed = systemRandom.srand(config->random_seed);
+
+  log_info << "Using random seed " << config->random_seed << how << std::endl;
+}
+
 void init_video()
 {
   if(texture_manager != NULL)
@@ -434,7 +445,7 @@ int main(int argc, char** argv)
     
   try {
     Console::instance = new Console();
-    srand(time(0));
+//  srand(time(0));            // this breaks repeatability in random numbers
     init_physfs(argv[0]);
     init_sdl();
     
@@ -464,18 +475,31 @@ int main(int argc, char** argv)
       // So we simply mount that path here...
       std::string dir = FileSystem::dirname(config->start_level);
       PHYSFS_addToSearchPath(dir.c_str(), true);
-      std::auto_ptr<GameSession> session
-        (new GameSession(FileSystem::basename(config->start_level)));
-      if(config->start_demo != "")
-        session->play_demo(config->start_demo);
-      if(config->record_demo != "")
-        session->record_demo(config->record_demo);
-      main_loop->push_screen(session.release());
+
+      init_rand();        // play_demo sets seed, record_demo uses it
+
+      if(config->start_level.size() > 4 &&
+              config->start_level.compare(config->start_level.size() - 5, 5, ".stwm") == 0) {
+          main_loop->push_screen(new WorldMapNS::WorldMap(
+                      FileSystem::basename(config->start_level)));
+      } else {
+        std::auto_ptr<GameSession> session (
+                new GameSession(FileSystem::basename(config->start_level)));
+        
+        if(config->start_demo != "")
+          session->play_demo(config->start_demo);
+
+        if(config->record_demo != "")
+          session->record_demo(config->record_demo);
+        main_loop->push_screen(session.release());
+      }
     } else {
+      init_rand();
       main_loop->push_screen(new TitleScreen());
     }
 
     main_loop->run();
+
   } catch(std::exception& e) {
     log_fatal << "Unexpected exception: " << e.what() << std::endl;
     result = 1;