hopefully fixed the crash on exit, keep sectors script bundled in the sector and...
[supertux.git] / src / world.cpp
index 04c2de5..bdd4407 100644 (file)
@@ -19,6 +19,7 @@
 //  02111-1307, USA.
 #include <config.h>
 
+#include <stddef.h>
 #include <physfs.h>
 #include <stdexcept>
 
@@ -27,7 +28,9 @@
 #include "lisp/parser.hpp"
 #include "lisp/lisp.hpp"
 #include "physfs/physfs_stream.hpp"
-#include "scripting/script_interpreter.hpp"
+#include "script_manager.hpp"
+#include "scripting/wrapper_util.hpp"
+#include "scripting/serialize.hpp"
 #include "msg.hpp"
 
 static bool has_suffix(const std::string& data, const std::string& suffix)
@@ -49,6 +52,12 @@ World::~World()
 }
 
 void
+World::set_savegame_filename(const std::string& filename)
+{
+  this->savegame_filename = filename;
+}
+
+void
 World::load(const std::string& filename)
 {
   basedir = FileSystem::dirname(filename);
@@ -92,15 +101,47 @@ World::load(const std::string& filename)
 void
 World::run()
 {
-  try {
-    std::string filename = basedir + "/world.nut";
-    std::auto_ptr<ScriptInterpreter> interpeter (new ScriptInterpreter(basedir));
-    IFileStream in(filename);
+  // create new squirrel table for persisten game state
+  HSQUIRRELVM vm = ScriptManager::instance->get_vm();
+
+  sq_pushroottable(vm);
+  sq_pushstring(vm, "state", -1);
+  sq_newtable(vm);
+  if(SQ_FAILED(sq_createslot(vm, -3)))
+    throw Scripting::SquirrelError(vm, "Couldn't create state table");
+  sq_pop(vm, 1);
   
-    interpeter->run_script(in, filename, true);
-  } catch(std::exception& e) {
-    msg_warning << "Problem running world script: " << e.what() << std::endl;
+  std::string filename = basedir + "/world.nut";
+  IFileStream in(filename);
+
+  HSQUIRRELVM new_vm = ScriptManager::instance->create_thread();
+  Scripting::compile_and_run(new_vm, in, filename);
+}
+
+void
+World::save()
+{
+  lisp::Writer writer(savegame_filename);
+
+  writer.start_list("supertux-savegame");
+  writer.write_int("version", 1);
+
+  writer.start_list("tux");
+  player_status->write(writer);
+  writer.end_list("tux");
+
+  writer.start_list("state");
+  HSQUIRRELVM vm = ScriptManager::instance->get_vm();
+  sq_pushroottable(vm);
+  sq_pushstring(vm, "state", -1);
+  if(SQ_SUCCEEDED(sq_get(vm, -2))) {
+    Scripting::save_squirrel_table(vm, -1, writer);
+    sq_pop(vm, 1);
   }
+  sq_pop(vm, 1);
+  writer.end_list("state");
+  
+  writer.end_list("supertux-savegame");
 }
 
 const std::string&