6baa7e4a26d4f416db8ff04f4b38be22e98f7466
[supertux.git] / src / scripting / functions.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "scripting/functions.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "math/random_generator.hpp"
21 #include "object/camera.hpp"
22 #include "object/player.hpp"
23 #include "physfs/ifile_stream.hpp"
24 #include "supertux/fadeout.hpp"
25 #include "supertux/game_session.hpp"
26 #include "supertux/gameconfig.hpp"
27 #include "supertux/globals.hpp"
28 #include "supertux/screen_manager.hpp"
29 #include "supertux/sector.hpp"
30 #include "supertux/shrinkfade.hpp"
31 #include "supertux/textscroller.hpp"
32 #include "supertux/tile.hpp"
33 #include "supertux/world.hpp"
34 #include "util/gettext.hpp"
35 #include "video/renderer.hpp"
36 #include "worldmap/tux.hpp"
37 #include "worldmap/worldmap.hpp"
38
39 #include "scripting/squirrel_util.hpp"
40 #include "scripting/time_scheduler.hpp"
41
42 namespace scripting {
43
44 SQInteger display(HSQUIRRELVM vm)
45 {
46   Console::output << squirrel2string(vm, -1) << std::endl;
47   return 0;
48 }
49
50 void print_stacktrace(HSQUIRRELVM vm)
51 {
52   print_squirrel_stack(vm);
53 }
54
55 SQInteger get_current_thread(HSQUIRRELVM vm)
56 {
57   sq_pushobject(vm, vm_to_object(vm));
58   return 1;
59 }
60
61 void wait(HSQUIRRELVM vm, float seconds)
62 {
63   TimeScheduler::instance->schedule_thread(vm, game_time + seconds);
64 }
65
66 void wait_for_screenswitch(HSQUIRRELVM vm)
67 {
68   g_screen_manager->m_waiting_threads.add(vm);
69 }
70
71 void exit_screen()
72 {
73   g_screen_manager->pop_screen();
74 }
75
76 void fadeout_screen(float seconds)
77 {
78   g_screen_manager->set_screen_fade(std::unique_ptr<ScreenFade>(new FadeOut(seconds)));
79 }
80
81 void shrink_screen(float dest_x, float dest_y, float seconds)
82 {
83   g_screen_manager->set_screen_fade(std::unique_ptr<ScreenFade>(new ShrinkFade(Vector(dest_x, dest_y), seconds)));
84 }
85
86 void abort_screenfade()
87 {
88   g_screen_manager->set_screen_fade(std::unique_ptr<ScreenFade>());
89 }
90
91 std::string translate(const std::string& text)
92 {
93   return dictionary_manager->get_dictionary().translate(text);
94 }
95
96 void display_text_file(const std::string& filename)
97 {
98   g_screen_manager->push_screen(std::unique_ptr<Screen>(new TextScroller(filename)));
99 }
100
101 void load_worldmap(const std::string& filename)
102 {
103   using namespace worldmap;
104
105   if (!WorldMap::current())
106   {
107     throw std::runtime_error("Can't start Worldmap without active WorldMap");
108   }
109   else
110   {
111     g_screen_manager->push_screen(std::unique_ptr<Screen>(new WorldMap(filename, WorldMap::current()->get_savegame())));
112   }
113 }
114
115 void load_level(const std::string& filename)
116 {
117   if (!GameSession::current())
118   {
119     throw std::runtime_error("Can't start level without active level.");
120   }
121   else
122   {
123     g_screen_manager->push_screen(std::unique_ptr<Screen>(new GameSession(filename, GameSession::current()->get_savegame())));
124   }
125 }
126
127 void import(HSQUIRRELVM vm, const std::string& filename)
128 {
129   IFileStream in(filename);
130
131   if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
132                           filename.c_str(), SQTrue)))
133     throw SquirrelError(vm, "Couldn't parse script");
134
135   sq_pushroottable(vm);
136   if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
137     sq_pop(vm, 1);
138     throw SquirrelError(vm, "Couldn't execute script");
139   }
140   sq_pop(vm, 1);
141 }
142
143 void debug_collrects(bool enable)
144 {
145   Sector::show_collrects = enable;
146 }
147
148 void debug_show_fps(bool enable)
149 {
150   g_config->show_fps = enable;
151 }
152
153 void debug_draw_solids_only(bool enable)
154 {
155   Sector::draw_solids_only = enable;
156 }
157
158 void debug_draw_editor_images(bool enable)
159 {
160   Tile::draw_editor_images = enable;
161 }
162
163 void debug_worldmap_ghost(bool enable)
164 {
165   using namespace worldmap;
166
167   if(WorldMap::current() == NULL)
168     throw std::runtime_error("Can't change ghost mode without active WorldMap");
169
170   WorldMap::current()->get_tux()->set_ghost_mode(enable);
171 }
172
173 void save_state()
174 {
175   using worldmap::WorldMap;
176
177   if (!WorldMap::current())
178   {
179     throw std::runtime_error("Can't save state without active Worldmap");
180   }
181   else
182   {
183     WorldMap::current()->save_state();
184   }
185 }
186
187 // not added to header, function to only be used by others
188 // in this file
189 bool validate_sector_player()
190 {
191   if (Sector::current() == 0)
192   {
193     log_info << "No current sector." << std::endl;
194     return false;
195   }
196
197   if (Sector::current()->player == 0)
198   {
199     log_info << "No player." << std::endl;
200     return false;
201   }
202   return true;
203 }
204
205 void play_music(const std::string& filename)
206 {
207   sound_manager->play_music(filename);
208 }
209
210 void play_sound(const std::string& filename)
211 {
212   sound_manager->play(filename);
213 }
214
215 void grease()
216 {
217   if (!validate_sector_player()) return;
218   ::Player* tux = Sector::current()->player; // scripting::Player != ::Player
219   tux->get_physic().set_velocity_x(tux->get_physic().get_velocity_x()*3);
220 }
221
222 void invincible()
223 {
224   if (!validate_sector_player()) return;
225   ::Player* tux = Sector::current()->player;
226   tux->invincible_timer.start(10000);
227 }
228
229 void ghost()
230 {
231   if (!validate_sector_player()) return;
232   ::Player* tux = Sector::current()->player;
233   tux->set_ghost_mode(true);
234 }
235
236 void mortal()
237 {
238   if (!validate_sector_player()) return;
239   ::Player* tux = Sector::current()->player;
240   tux->invincible_timer.stop();
241   tux->set_ghost_mode(false);
242 }
243
244 void restart()
245 {
246   if (GameSession::current() == 0)
247   {
248     log_info << "No game session" << std::endl;
249     return;
250   }
251   GameSession::current()->restart_level();
252 }
253
254 void whereami()
255 {
256   if (!validate_sector_player()) return;
257   ::Player* tux = Sector::current()->player;
258   log_info << "You are at x " << ((int) tux->get_pos().x) << ", y " << ((int) tux->get_pos().y) << std::endl;
259 }
260
261 void gotoend()
262 {
263   if (!validate_sector_player()) return;
264   ::Player* tux = Sector::current()->player;
265   tux->move(Vector(
266               (Sector::current()->get_width()) - (SCREEN_WIDTH*2), 0));
267   Sector::current()->camera->reset(
268     Vector(tux->get_pos().x, tux->get_pos().y));
269 }
270
271 void camera()
272 {
273   if (!validate_sector_player()) return;
274   log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
275 }
276
277 void set_gamma(float gamma)
278 {
279   Renderer::instance()->set_gamma(gamma);
280 }
281
282 void quit()
283 {
284   g_screen_manager->quit();
285 }
286
287 int rand()
288 {
289   return gameRandom.rand();
290 }
291
292 void set_game_speed(float speed)
293 {
294   ::g_game_speed = speed;
295 }
296
297 void record_demo(const std::string& filename)
298 {
299   if (GameSession::current() == 0)
300   {
301     log_info << "No game session" << std::endl;
302     return;
303   }
304   GameSession::current()->restart_level();
305   GameSession::current()->record_demo(filename);
306 }
307
308 void play_demo(const std::string& filename)
309 {
310   if (GameSession::current() == 0)
311   {
312     log_info << "No game session" << std::endl;
313     return;
314   }
315   // Reset random seed
316   g_config->random_seed = GameSession::current()->get_demo_random_seed(filename);
317   g_config->random_seed = gameRandom.srand(g_config->random_seed);
318   GameSession::current()->restart_level();
319   GameSession::current()->play_demo(filename);
320 }
321
322 }
323
324 /* EOF */