Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[supertux.git] / src / scripting / functions.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <memory>
23 #include <stdio.h>
24 #include <string>
25 #include <squirrel.h>
26 #include <sqstdio.h>
27 #include "textscroller.hpp"
28 #include "functions.hpp"
29 #include "game_session.hpp"
30 #include "tinygettext/tinygettext.hpp"
31 #include "physfs/physfs_stream.hpp"
32 #include "script_manager.hpp"
33 #include "resources.hpp"
34 #include "gettext.hpp"
35 #include "log.hpp"
36 #include "mainloop.hpp"
37 #include "worldmap.hpp"
38 #include "world.hpp"
39 #include "sector.hpp"
40
41 #include "squirrel_error.hpp"
42 #include "wrapper_util.hpp"
43
44 namespace Scripting
45 {
46
47 int display(HSQUIRRELVM vm)
48 {
49   Console::output << squirrel2string(vm, -1) << std::endl;
50   return 0;
51 }
52
53 void wait(HSQUIRRELVM vm, float seconds)
54 {
55   SQUserPointer ptr = sq_getforeignptr(vm);
56   ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
57   script_manager->set_wakeup_event(vm, ScriptManager::TIME, seconds);
58 }
59
60 void wait_for_screenswitch(HSQUIRRELVM vm)
61 {
62   SQUserPointer ptr = sq_getforeignptr(vm);
63   ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
64   script_manager->set_wakeup_event(vm, ScriptManager::SCREEN_SWITCHED);
65 }
66
67 void exit_screen()
68 {
69   main_loop->exit_screen();
70 }
71
72 std::string translate(const std::string& text)
73 {
74   return dictionary_manager.get_dictionary().translate(text);
75 }
76
77 void display_text_file(const std::string& filename)
78 {
79   main_loop->push_screen(new TextScroller(filename));
80 }
81
82 void load_worldmap(const std::string& filename)
83 {
84   using namespace WorldMapNS;
85
86   std::auto_ptr<WorldMap> worldmap(new WorldMap());
87   worldmap->loadmap(filename);
88   main_loop->push_screen(worldmap.release());
89 }
90
91 void load_level(const std::string& filename)
92 {
93   main_loop->push_screen(new GameSession(filename, ST_GL_PLAY));
94 }
95
96 static SQInteger squirrel_read_char(SQUserPointer file)
97 {
98   std::istream* in = reinterpret_cast<std::istream*> (file);
99   char c = in->get();
100   if(in->eof())
101     return 0;
102
103   return c;
104 }
105
106
107 void import(HSQUIRRELVM vm, const std::string& filename)
108 {
109   IFileStream in(filename);
110     
111   if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
112           filename.c_str(), SQTrue)))
113     throw SquirrelError(vm, "Couldn't parse script");
114     
115   sq_pushroottable(vm);
116   if(SQ_FAILED(sq_call(vm, 1, SQFalse))) {
117     sq_pop(vm, 1);
118     throw SquirrelError(vm, "Couldn't execute script");
119   }
120   sq_pop(vm, 1);
121 }
122
123 void add_key(int new_key)
124 {
125   player_status->set_keys(new_key);
126 }
127
128 void debug_collrects(bool enable)
129 {
130   Sector::show_collrects = enable;
131 }
132
133 void save_state()
134 {
135   if(World::current() == NULL)
136     throw std::runtime_error("Can't save state without active World");
137
138   World::current()->save_state();
139 }
140
141 }
142