- make sure all log messages get displayed, even when the console is not
[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/worldmap.hpp"
38 #include "world.hpp"
39 #include "sector.hpp"
40 #include "gameconfig.hpp"
41 #include "object/player.hpp"
42 #include "object/tilemap.hpp"
43 #include "main.hpp"
44 #include "fadeout.hpp"
45 #include "shrinkfade.hpp"
46 #include "object/camera.hpp"
47 #include "flip_level_transformer.hpp"
48
49 #include "squirrel_error.hpp"
50 #include "wrapper_util.hpp"
51
52 namespace Scripting
53 {
54
55 int display(HSQUIRRELVM vm)
56 {
57   Console::output << squirrel2string(vm, -1) << std::endl;
58   return 0;
59 }
60
61 void print_stacktrace(HSQUIRRELVM vm)
62 {
63   print_squirrel_stack(vm);
64 }
65
66 int get_current_thread(HSQUIRRELVM vm)
67 {
68   SQObject object;
69   sq_resetobject(&object);
70   object._unVal.pThread = vm;
71   object._type = OT_THREAD;
72   sq_pushobject(vm, object);
73
74   return 1;
75 }
76
77 void wait(HSQUIRRELVM vm, float seconds)
78 {
79   SQUserPointer ptr = sq_getforeignptr(vm);
80   ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
81   script_manager->set_wakeup_event(vm, ScriptManager::TIME, seconds);
82 }
83
84 void wait_for_screenswitch(HSQUIRRELVM vm)
85 {
86   SQUserPointer ptr = sq_getforeignptr(vm);
87   ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
88   script_manager->set_wakeup_event(vm, ScriptManager::SCREEN_SWITCHED);
89 }
90
91 void exit_screen()
92 {
93   main_loop->exit_screen();
94 }
95
96 void fadeout_screen(float seconds)
97 {
98   main_loop->set_screen_fade(new FadeOut(seconds));
99 }
100
101 void shrink_screen(float dest_x, float dest_y, float seconds)
102 {
103   main_loop->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
104 }
105
106 std::string translate(const std::string& text)
107 {
108   return dictionary_manager.get_dictionary().translate(text);
109 }
110
111 void display_text_file(const std::string& filename)
112 {
113   main_loop->push_screen(new TextScroller(filename));
114 }
115
116 void load_worldmap(const std::string& filename)
117 {
118   using namespace WorldMapNS;
119
120   main_loop->push_screen(new WorldMap(filename));
121 }
122
123 void load_level(const std::string& filename)
124 {
125   main_loop->push_screen(new GameSession(filename));
126 }
127
128 static SQInteger squirrel_read_char(SQUserPointer file)
129 {
130   std::istream* in = reinterpret_cast<std::istream*> (file);
131   char c = in->get();
132   if(in->eof())
133     return 0;
134
135   return c;
136 }
137
138 void import(HSQUIRRELVM vm, const std::string& filename)
139 {
140   IFileStream in(filename);
141     
142   if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
143           filename.c_str(), SQTrue)))
144     throw SquirrelError(vm, "Couldn't parse script");
145     
146   sq_pushroottable(vm);
147   if(SQ_FAILED(sq_call(vm, 1, SQFalse))) {
148     sq_pop(vm, 1);
149     throw SquirrelError(vm, "Couldn't execute script");
150   }
151   sq_pop(vm, 1);
152 }
153
154 void add_key(int new_key)
155 {
156   player_status->set_keys(new_key);
157 }
158
159 void debug_collrects(bool enable)
160 {
161   Sector::show_collrects = enable;
162 }
163
164 void debug_draw_fps(bool enable)
165 {
166   config->show_fps = enable;
167 }
168
169 void debug_draw_solids_only(bool enable)
170 {
171   Sector::draw_solids_only = enable;
172 }
173
174 void save_state()
175 {
176   using namespace WorldMapNS;
177   
178   if(World::current() == NULL)
179     throw std::runtime_error("Can't save state without active World");
180
181   if(WorldMap::current() != NULL)
182     WorldMap::current()->save_state();
183   World::current()->save_state();
184 }
185
186 // not added to header, function to only be used by others
187 // in this file
188 bool validate_sector_player()
189 {
190   if (Sector::current() == 0)
191   {
192     log_info << "No current sector." << std::endl;
193         return false;
194   }
195
196   if (Sector::current()->player == 0)
197   {
198     log_info << "No player." << std::endl;
199         return false;
200   }
201   return true;
202 }
203
204 void grease()
205 {
206   if (!validate_sector_player()) return;
207   ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
208   tux->physic.set_velocity_x(tux->physic.get_velocity_x()*3);
209 }
210
211 void invincible()
212 {
213   if (!validate_sector_player()) return;
214   ::Player* tux = Sector::current()->player;
215   tux->invincible_timer.start(10000);
216 }
217
218 void mortal()
219 {
220   if (!validate_sector_player()) return;
221   ::Player* tux = Sector::current()->player;
222   tux->invincible_timer.stop();
223 }
224
225 void shrink()
226 {
227   if (!validate_sector_player()) return;
228   ::Player* tux = Sector::current()->player;
229   tux->kill(tux->SHRINK);
230 }
231
232 void kill()
233 {
234   if (!validate_sector_player()) return;
235   ::Player* tux = Sector::current()->player;
236   tux->kill(tux->KILL);
237 }
238
239 void restart()
240 {
241   if (GameSession::current() == 0)
242   {
243     log_info << "No game session" << std::endl;
244     return;
245   }
246   GameSession::current()->restart_level();
247 }
248
249 void whereami()
250 {
251   if (!validate_sector_player()) return;
252   ::Player* tux = Sector::current()->player;
253   log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
254 }
255
256 void gotoend()
257 {
258   if (!validate_sector_player()) return;
259   ::Player* tux = Sector::current()->player;
260   tux->move(Vector(
261           (Sector::current()->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
262   Sector::current()->camera->reset(
263         Vector(tux->get_pos().x, tux->get_pos().y));
264 }
265
266 void camera()
267 {
268   if (!validate_sector_player()) return;
269   log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
270 }
271
272 void quit()
273 {
274   main_loop->quit();
275 }
276
277 }
278