// 02111-1307, USA.
#include <config.h>
+#include <stddef.h>
#include <physfs.h>
#include <stdexcept>
#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)
}
void
+World::set_savegame_filename(const std::string& filename)
+{
+ this->savegame_filename = filename;
+}
+
+void
World::load(const std::string& filename)
{
basedir = FileSystem::dirname(filename);
std::string path = basedir + "/";
char** files = PHYSFS_enumerateFiles(path.c_str());
if(!files) {
- msg_warning("Couldn't read subset dir '"
- << path << "'");
+ msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
return;
}
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::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&