* Small miniswig update to use less dependencies
[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 "resources.hpp"
33 #include "gettext.hpp"
34 #include "log.hpp"
35 #include "mainloop.hpp"
36 #include "worldmap/worldmap.hpp"
37 #include "worldmap/tux.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 #include "audio/sound_manager.hpp"
49 #include "random_generator.hpp"
50
51 #include "squirrel_error.hpp"
52 #include "squirrel_util.hpp"
53 #include "time_scheduler.hpp"
54
55 extern float game_speed;
56
57 namespace Scripting
58 {
59
60 SQInteger display(HSQUIRRELVM vm)
61 {
62   Console::output << squirrel2string(vm, -1) << std::endl;
63   return 0;
64 }
65
66 void print_stacktrace(HSQUIRRELVM vm)
67 {
68   print_squirrel_stack(vm);
69 }
70
71 SQInteger get_current_thread(HSQUIRRELVM vm)
72 {
73   sq_pushobject(vm, vm_to_object(vm));
74   return 1;
75 }
76
77 void wait(HSQUIRRELVM vm, float seconds)
78 {
79   TimeScheduler::instance->schedule_thread(vm, game_time + seconds);
80 }
81
82 void wait_for_screenswitch(HSQUIRRELVM vm)
83 {
84   main_loop->waiting_threads.add(vm);
85 }
86
87 void exit_screen()
88 {
89   main_loop->exit_screen();
90 }
91
92 void fadeout_screen(float seconds)
93 {
94   main_loop->set_screen_fade(new FadeOut(seconds));
95 }
96
97 void shrink_screen(float dest_x, float dest_y, float seconds)
98 {
99   main_loop->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
100 }
101
102 void abort_screenfade()
103 {
104   main_loop->set_screen_fade(NULL);
105 }
106
107 std::string translate(const std::string& text)
108 {
109   return dictionary_manager.get_dictionary().translate(text);
110 }
111
112 void display_text_file(const std::string& filename)
113 {
114   main_loop->push_screen(new TextScroller(filename));
115 }
116
117 void load_worldmap(const std::string& filename)
118 {
119   using namespace WorldMapNS;
120
121   main_loop->push_screen(new WorldMap(filename));
122 }
123
124 void load_level(const std::string& filename)
125 {
126   main_loop->push_screen(new GameSession(filename));
127 }
128
129 static SQInteger squirrel_read_char(SQUserPointer file)
130 {
131   std::istream* in = reinterpret_cast<std::istream*> (file);
132   char c = in->get();
133   if(in->eof())
134     return 0;
135
136   return c;
137 }
138
139 void import(HSQUIRRELVM vm, const std::string& filename)
140 {
141   IFileStream in(filename);
142
143   if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
144           filename.c_str(), SQTrue)))
145     throw SquirrelError(vm, "Couldn't parse script");
146
147   sq_pushroottable(vm);
148   if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
149     sq_pop(vm, 1);
150     throw SquirrelError(vm, "Couldn't execute script");
151   }
152   sq_pop(vm, 1);
153 }
154
155 void debug_collrects(bool enable)
156 {
157   Sector::show_collrects = enable;
158 }
159
160 void debug_show_fps(bool enable)
161 {
162   config->show_fps = enable;
163 }
164
165 void debug_draw_solids_only(bool enable)
166 {
167   Sector::draw_solids_only = enable;
168 }
169
170 void debug_worldmap_ghost(bool enable)
171 {
172   using namespace WorldMapNS;
173
174   if(WorldMap::current() == NULL)
175     throw std::runtime_error("Can't change ghost mode without active WorldMap");
176
177   WorldMap::current()->get_tux()->set_ghost_mode(enable);
178 }
179
180 void save_state()
181 {
182   using namespace WorldMapNS;
183
184   if(World::current() == NULL || WorldMap::current() == NULL)
185     throw std::runtime_error("Can't save state without active World");
186
187   WorldMap::current()->save_state();
188   World::current()->save_state();
189 }
190
191 void update_worldmap()
192 {
193   using namespace WorldMapNS;
194
195   if(WorldMap::current() == NULL)
196     throw std::runtime_error("Can't update Worldmap: none active");
197
198   WorldMap::current()->load_state();
199 }
200
201 // not added to header, function to only be used by others
202 // in this file
203 bool validate_sector_player()
204 {
205   if (Sector::current() == 0)
206   {
207     log_info << "No current sector." << std::endl;
208     return false;
209   }
210
211   if (Sector::current()->player == 0)
212   {
213     log_info << "No player." << std::endl;
214     return false;
215   }
216   return true;
217 }
218
219 void play_music(const std::string& filename)
220 {
221   sound_manager->play_music(filename);
222 }
223
224 void play_sound(const std::string& filename)
225 {
226   sound_manager->play(filename);
227 }
228
229 void grease()
230 {
231   if (!validate_sector_player()) return;
232   ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
233   tux->physic.set_velocity_x(tux->physic.get_velocity_x()*3);
234 }
235
236 void invincible()
237 {
238   if (!validate_sector_player()) return;
239   ::Player* tux = Sector::current()->player;
240   tux->invincible_timer.start(10000);
241 }
242
243 void ghost()
244 {
245   if (!validate_sector_player()) return;
246   ::Player* tux = Sector::current()->player;
247   tux->set_ghost_mode(true);
248 }
249
250 void mortal()
251 {
252   if (!validate_sector_player()) return;
253   ::Player* tux = Sector::current()->player;
254   tux->invincible_timer.stop();
255   tux->set_ghost_mode(false);
256 }
257
258 void restart()
259 {
260   if (GameSession::current() == 0)
261   {
262     log_info << "No game session" << std::endl;
263     return;
264   }
265   GameSession::current()->restart_level();
266 }
267
268 void whereami()
269 {
270   if (!validate_sector_player()) return;
271   ::Player* tux = Sector::current()->player;
272   log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
273 }
274
275 void gotoend()
276 {
277   if (!validate_sector_player()) return;
278   ::Player* tux = Sector::current()->player;
279   tux->move(Vector(
280           (Sector::current()->get_width()) - (SCREEN_WIDTH*2), 0));
281   Sector::current()->camera->reset(
282         Vector(tux->get_pos().x, tux->get_pos().y));
283 }
284
285 void camera()
286 {
287   if (!validate_sector_player()) return;
288   log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
289 }
290
291 void set_gamma(float gamma) {
292   SDL_SetGamma(gamma, gamma, gamma);
293 }
294
295 void quit()
296 {
297   main_loop->quit();
298 }
299
300 int rand()
301 {
302   return systemRandom.rand();
303 }
304
305 void set_game_speed(float speed)
306 {
307   ::game_speed = speed;
308 }
309
310 void record_demo(const std::string& filename)
311 {
312   if (GameSession::current() == 0)
313   {
314     log_info << "No game session" << std::endl;
315     return;
316   }
317   GameSession::current()->restart_level();
318   GameSession::current()->record_demo(filename);
319 }
320
321 void play_demo(const std::string& filename)
322 {
323   if (GameSession::current() == 0)
324   {
325     log_info << "No game session" << std::endl;
326     return;
327   }
328   // Reset random seed
329   config->random_seed = GameSession::current()->get_demo_random_seed(filename);
330   config->random_seed = systemRandom.srand(config->random_seed);
331   GameSession::current()->restart_level();
332   GameSession::current()->play_demo(filename);
333 }
334
335 }