From: Ingo Ruhnke Date: Thu, 14 Aug 2014 23:20:38 +0000 (+0200) Subject: Reintroduced load_worldmap(), load_level() and save_state(), as some addon levels... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=af45bf47b8faf73a17f834e40cd4df81c8e969ba;p=supertux.git Reintroduced load_worldmap(), load_level() and save_state(), as some addon levels are using them (untested) --- diff --git a/src/scripting/functions.cpp b/src/scripting/functions.cpp index 5e2aace38..6baa7e4a2 100644 --- a/src/scripting/functions.cpp +++ b/src/scripting/functions.cpp @@ -34,6 +34,7 @@ #include "util/gettext.hpp" #include "video/renderer.hpp" #include "worldmap/tux.hpp" +#include "worldmap/worldmap.hpp" #include "scripting/squirrel_util.hpp" #include "scripting/time_scheduler.hpp" @@ -97,6 +98,32 @@ void display_text_file(const std::string& filename) g_screen_manager->push_screen(std::unique_ptr(new TextScroller(filename))); } +void load_worldmap(const std::string& filename) +{ + using namespace worldmap; + + if (!WorldMap::current()) + { + throw std::runtime_error("Can't start Worldmap without active WorldMap"); + } + else + { + g_screen_manager->push_screen(std::unique_ptr(new WorldMap(filename, WorldMap::current()->get_savegame()))); + } +} + +void load_level(const std::string& filename) +{ + if (!GameSession::current()) + { + throw std::runtime_error("Can't start level without active level."); + } + else + { + g_screen_manager->push_screen(std::unique_ptr(new GameSession(filename, GameSession::current()->get_savegame()))); + } +} + void import(HSQUIRRELVM vm, const std::string& filename) { IFileStream in(filename); @@ -143,6 +170,20 @@ void debug_worldmap_ghost(bool enable) WorldMap::current()->get_tux()->set_ghost_mode(enable); } +void save_state() +{ + using worldmap::WorldMap; + + if (!WorldMap::current()) + { + throw std::runtime_error("Can't save state without active Worldmap"); + } + else + { + WorldMap::current()->save_state(); + } +} + // not added to header, function to only be used by others // in this file bool validate_sector_player() diff --git a/src/scripting/functions.hpp b/src/scripting/functions.hpp index 638d77e18..cd139c25c 100644 --- a/src/scripting/functions.hpp +++ b/src/scripting/functions.hpp @@ -48,6 +48,16 @@ SQInteger get_current_thread(HSQUIRRELVM vm) __custom("t"); void display_text_file(const std::string& filename); /** + * Load and display a worldmap (on next screenswitch) + */ +void load_worldmap(const std::string& filename); + +/** + * Load and display a level (on next screenswitch) + */ +void load_level(const std::string& filename); + +/** * Suspend the script execution for the specified number of seconds */ void wait(HSQUIRRELVM vm, float seconds) __suspend; @@ -93,6 +103,11 @@ std::string translate(const std::string& text); void import(HSQUIRRELVM v, const std::string& filename); /** + * Save world state to savegame + */ +void save_state(); + +/** * enable/disable drawing of collision rectangles */ void debug_collrects(bool enable); diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index 7f5aa4b12..7b74d7ade 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -3135,6 +3135,52 @@ static SQInteger display_text_file_wrapper(HSQUIRRELVM vm) } +static SQInteger load_worldmap_wrapper(HSQUIRRELVM vm) +{ + const SQChar* arg0; + if(SQ_FAILED(sq_getstring(vm, 2, &arg0))) { + sq_throwerror(vm, _SC("Argument 1 not a string")); + return SQ_ERROR; + } + + try { + scripting::load_worldmap(arg0); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'load_worldmap'")); + return SQ_ERROR; + } + +} + +static SQInteger load_level_wrapper(HSQUIRRELVM vm) +{ + const SQChar* arg0; + if(SQ_FAILED(sq_getstring(vm, 2, &arg0))) { + sq_throwerror(vm, _SC("Argument 1 not a string")); + return SQ_ERROR; + } + + try { + scripting::load_level(arg0); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'load_level'")); + return SQ_ERROR; + } + +} + static SQInteger wait_wrapper(HSQUIRRELVM vm) { HSQUIRRELVM arg0 = vm; @@ -3320,6 +3366,25 @@ static SQInteger import_wrapper(HSQUIRRELVM vm) } +static SQInteger save_state_wrapper(HSQUIRRELVM vm) +{ + (void) vm; + + try { + scripting::save_state(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'save_state'")); + return SQ_ERROR; + } + +} + static SQInteger debug_collrects_wrapper(HSQUIRRELVM vm) { SQBool arg0; @@ -4352,6 +4417,20 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, "Couldn't register function 'display_text_file'"); } + sq_pushstring(v, "load_worldmap", -1); + sq_newclosure(v, &load_worldmap_wrapper, 0); + sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|ts"); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'load_worldmap'"); + } + + sq_pushstring(v, "load_level", -1); + sq_newclosure(v, &load_level_wrapper, 0); + sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|ts"); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'load_level'"); + } + sq_pushstring(v, "wait", -1); sq_newclosure(v, &wait_wrapper, 0); sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|tn"); @@ -4408,6 +4487,13 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, "Couldn't register function 'import'"); } + sq_pushstring(v, "save_state", -1); + sq_newclosure(v, &save_state_wrapper, 0); + sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|t"); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'save_state'"); + } + sq_pushstring(v, "debug_collrects", -1); sq_newclosure(v, &debug_collrects_wrapper, 0); sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|tb");