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