diff --git a/data/scripts/default.nut b/data/scripts/default.nut index 082c35831cc..f2d05f7fa0b 100644 --- a/data/scripts/default.nut +++ b/data/scripts/default.nut @@ -6,6 +6,7 @@ //Create Level table Level <- { finish=Level_finish, + has_active_sequence=Level_has_active_sequence, spawn=Level_spawn, set_start_point=Level_set_start_point, set_start_pos=Level_set_start_pos, diff --git a/src/scripting/level.cpp b/src/scripting/level.cpp index f513ccc07f8..99b8546a837 100644 --- a/src/scripting/level.cpp +++ b/src/scripting/level.cpp @@ -25,49 +25,56 @@ namespace scripting { void Level_finish(bool win) { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.finish(win); } +bool +Level_has_active_sequence() +{ + SCRIPT_GUARD_GAMESESSION(false); + return game_session.has_active_sequence(); +} + void Level_spawn(const std::string& sector, const std::string& spawnpoint) { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.respawn(sector, spawnpoint); } void Level_set_start_point(const std::string& sector, const std::string& spawnpoint) { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.set_start_point(sector, spawnpoint); } void Level_set_start_pos(const std::string& sector, float x, float y) { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.set_start_pos(sector, Vector(x, y)); } void Level_set_respawn_point(const std::string& sector, const std::string& spawnpoint) { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.set_respawn_point(sector, spawnpoint); } void Level_set_respawn_pos(const std::string& sector, float x, float y) { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.set_respawn_pos(sector, Vector(x, y)); } void Level_flip_vertically() { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); FlipLevelTransformer flip_transformer; flip_transformer.transform(game_session.get_current_level()); } @@ -75,28 +82,28 @@ Level_flip_vertically() void Level_toggle_pause() { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.toggle_pause(); } void Level_edit(bool edit_mode) { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.set_editmode(edit_mode); } void Level_pause_target_timer() { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.set_target_timer_paused(true); } void Level_resume_target_timer() { - SCRIPT_GUARD_GAMESESSION; + SCRIPT_GUARD_GAMESESSION(); game_session.set_target_timer_paused(false); } diff --git a/src/scripting/level.hpp b/src/scripting/level.hpp index a35e1dafc15..4ec5b94b6f4 100644 --- a/src/scripting/level.hpp +++ b/src/scripting/level.hpp @@ -23,8 +23,8 @@ #include /** Macro to help easily check if there is a current GameSession and define it, if so. **/ -#define SCRIPT_GUARD_GAMESESSION \ - if (!GameSession::current()) return; \ +#define SCRIPT_GUARD_GAMESESSION(returnvalue) \ + if (!GameSession::current()) return returnvalue; \ GameSession& game_session = *GameSession::current() #endif @@ -51,6 +51,10 @@ class Level * @param bool $win If ""true"", the level is marked as completed if launched from a worldmap. */ void Level_finish(bool win); +/** + * Gets whether an end sequence has started. (AKA when the stats at the end are visible) + */ +bool Level_has_active_sequence(); /** * Respawns Tux in sector named ""sector"" at spawnpoint named ""spawnpoint"".${SRG_TABLENEWPARAGRAPH} diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index cada6241e81..d8d213f64b0 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -12530,6 +12530,25 @@ static SQInteger Level_finish_wrapper(HSQUIRRELVM vm) } +static SQInteger Level_has_active_sequence_wrapper(HSQUIRRELVM vm) +{ + + try { + bool return_value = scripting::Level_has_active_sequence(); + + sq_pushbool(vm, return_value); + return 1; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'Level_has_active_sequence'")); + return SQ_ERROR; + } + +} + static SQInteger Level_spawn_wrapper(HSQUIRRELVM vm) { const SQChar* arg0; @@ -13999,6 +14018,13 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, "Couldn't register function 'Level_finish'"); } + sq_pushstring(v, "Level_has_active_sequence", -1); + sq_newclosure(v, &Level_has_active_sequence_wrapper, 0); + sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "."); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'Level_has_active_sequence'"); + } + sq_pushstring(v, "Level_spawn", -1); sq_newclosure(v, &Level_spawn_wrapper, 0); sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".ss"); diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 31fbb7b02c1..2c7ed265737 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -722,6 +722,12 @@ GameSession::get_working_directory() const return FileSystem::dirname(m_levelfile); } +bool +GameSession::has_active_sequence() const +{ + return m_end_sequence; +} + void GameSession::start_sequence(Player* caller, Sequence seq, const SequenceData* data) { diff --git a/src/supertux/game_session.hpp b/src/supertux/game_session.hpp index 75ed63dd953..c4e5785b13a 100644 --- a/src/supertux/game_session.hpp +++ b/src/supertux/game_session.hpp @@ -115,6 +115,7 @@ class GameSession final : public Screen, * resources for the current level/world */ std::string get_working_directory() const; + bool has_active_sequence() const; int restart_level(bool after_death = false); bool reset_button; bool reset_checkpoint_button;