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