X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fscripting%2Ffunctions.cpp;h=d872740a1b546e79946ab277bd3c595b3c310654;hb=c0c4838b917943354c150d56ab970ca249267037;hp=2e51cad6502e1d01a50af19c4a1959b1e2566c7e;hpb=067991730e8404058d2827f502fb4b2c8330d99d;p=supertux.git diff --git a/src/scripting/functions.cpp b/src/scripting/functions.cpp index 2e51cad65..d872740a1 100644 --- a/src/scripting/functions.cpp +++ b/src/scripting/functions.cpp @@ -1,20 +1,72 @@ +// $Id: main.cpp 3275 2006-04-09 00:32:34Z sommer $ +// +// SuperTux +// Copyright (C) 2005 Matthias Braun +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +#include + +#include #include #include #include #include #include "textscroller.hpp" #include "functions.hpp" -#include "script_interpreter.hpp" +#include "game_session.hpp" #include "tinygettext/tinygettext.hpp" +#include "physfs/physfs_stream.hpp" +#include "script_manager.hpp" #include "resources.hpp" #include "gettext.hpp" +#include "log.hpp" +#include "mainloop.hpp" +#include "worldmap.hpp" +#include "world.hpp" +#include "sector.hpp" + +#include "squirrel_error.hpp" +#include "wrapper_util.hpp" namespace Scripting { -void wait(float seconds) +int display(HSQUIRRELVM vm) +{ + Console::output << squirrel2string(vm, -1) << std::endl; + return 0; +} + +void wait(HSQUIRRELVM vm, float seconds) { - ScriptInterpreter::current()->set_wakeup_time(seconds); + SQUserPointer ptr = sq_getforeignptr(vm); + ScriptManager* script_manager = reinterpret_cast (ptr); + script_manager->set_wakeup_event(vm, ScriptManager::TIME, seconds); +} + +void wait_for_screenswitch(HSQUIRRELVM vm) +{ + SQUserPointer ptr = sq_getforeignptr(vm); + ScriptManager* script_manager = reinterpret_cast (ptr); + script_manager->set_wakeup_event(vm, ScriptManager::SCREEN_SWITCHED); +} + +void exit_screen() +{ + main_loop->exit_screen(); } std::string translate(const std::string& text) @@ -24,27 +76,66 @@ std::string translate(const std::string& text) void display_text_file(const std::string& filename) { - std::string file - = ScriptInterpreter::current()->get_working_directory() + filename; - ::display_text_file(file); + main_loop->push_screen(new TextScroller(filename)); } -void import(HSQUIRRELVM v, const std::string& filename) +void load_worldmap(const std::string& filename) { - std::string file - = ScriptInterpreter::current()->get_working_directory() + filename; - if(sqstd_loadfile(v, file.c_str(), true) < 0) { - std::cerr << "Warning couldn't load script '" << filename << "' (" - << file << ").\n"; - return; - } + using namespace WorldMapNS; + + std::auto_ptr worldmap(new WorldMap()); + worldmap->loadmap(filename); + main_loop->push_screen(worldmap.release()); +} + +void load_level(const std::string& filename) +{ + main_loop->push_screen(new GameSession(filename, ST_GL_PLAY)); +} - sq_push(v, -2); - if(sq_call(v, 1, false) < 0) { - std::cerr << "Couldn't execute script '" << filename << "' (" - << file << ").\n"; - return; +static SQInteger squirrel_read_char(SQUserPointer file) +{ + std::istream* in = reinterpret_cast (file); + char c = in->get(); + if(in->eof()) + return 0; + + return c; +} + + +void import(HSQUIRRELVM vm, const std::string& filename) +{ + IFileStream in(filename); + + if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in, + filename.c_str(), SQTrue))) + throw SquirrelError(vm, "Couldn't parse script"); + + sq_pushroottable(vm); + if(SQ_FAILED(sq_call(vm, 1, SQFalse))) { + sq_pop(vm, 1); + throw SquirrelError(vm, "Couldn't execute script"); } + sq_pop(vm, 1); +} + +void add_key(int new_key) +{ + player_status->set_keys(new_key); +} + +void debug_collrects(bool enable) +{ + Sector::show_collrects = enable; +} + +void save_state() +{ + if(World::current() == NULL) + throw std::runtime_error("Can't save state without active World"); + + World::current()->save_state(); } }