Make it build with -DCOMPILE_AMALGATION=ON. Still not certain how intern_draw/next_po...
[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 worldmap;
101
102   if(World::current() == NULL)
103     throw std::runtime_error("Can't start WorldMap without active world.");
104
105   g_screen_manager->push_screen(new WorldMap(filename, World::current()->get_player_status()));
106 }
107
108 void load_level(const std::string& filename)
109 {
110   if(GameSession::current() == NULL)
111     throw std::runtime_error("Can't start level without active level.");
112
113   g_screen_manager->push_screen(new GameSession(filename, GameSession::current()->get_player_status()));
114 }
115
116 void import(HSQUIRRELVM vm, const std::string& filename)
117 {
118   IFileStream in(filename);
119
120   if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
121                           filename.c_str(), SQTrue)))
122     throw SquirrelError(vm, "Couldn't parse script");
123
124   sq_pushroottable(vm);
125   if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
126     sq_pop(vm, 1);
127     throw SquirrelError(vm, "Couldn't execute script");
128   }
129   sq_pop(vm, 1);
130 }
131
132 void debug_collrects(bool enable)
133 {
134   Sector::show_collrects = enable;
135 }
136
137 void debug_show_fps(bool enable)
138 {
139   g_config->show_fps = enable;
140 }
141
142 void debug_draw_solids_only(bool enable)
143 {
144   Sector::draw_solids_only = enable;
145 }
146
147 void debug_worldmap_ghost(bool enable)
148 {
149   using namespace worldmap;
150
151   if(WorldMap::current() == NULL)
152     throw std::runtime_error("Can't change ghost mode without active WorldMap");
153
154   WorldMap::current()->get_tux()->set_ghost_mode(enable);
155 }
156
157 void save_state()
158 {
159   using namespace worldmap;
160
161   if(World::current() == NULL || WorldMap::current() == NULL)
162     throw std::runtime_error("Can't save state without active World");
163
164   WorldMap::current()->save_state();
165   World::current()->save_state();
166 }
167
168 void update_worldmap()
169 {
170   using namespace worldmap;
171
172   if(WorldMap::current() == NULL)
173     throw std::runtime_error("Can't update Worldmap: none active");
174
175   WorldMap::current()->load_state();
176 }
177
178 // not added to header, function to only be used by others
179 // in this file
180 bool validate_sector_player()
181 {
182   if (Sector::current() == 0)
183   {
184     log_info << "No current sector." << std::endl;
185     return false;
186   }
187
188   if (Sector::current()->player == 0)
189   {
190     log_info << "No player." << std::endl;
191     return false;
192   }
193   return true;
194 }
195
196 void play_music(const std::string& filename)
197 {
198   sound_manager->play_music(filename);
199 }
200
201 void play_sound(const std::string& filename)
202 {
203   sound_manager->play(filename);
204 }
205
206 void grease()
207 {
208   if (!validate_sector_player()) return;
209   ::Player* tux = Sector::current()->player; // scripting::Player != ::Player
210   tux->get_physic().set_velocity_x(tux->get_physic().get_velocity_x()*3);
211 }
212
213 void invincible()
214 {
215   if (!validate_sector_player()) return;
216   ::Player* tux = Sector::current()->player;
217   tux->invincible_timer.start(10000);
218 }
219
220 void ghost()
221 {
222   if (!validate_sector_player()) return;
223   ::Player* tux = Sector::current()->player;
224   tux->set_ghost_mode(true);
225 }
226
227 void mortal()
228 {
229   if (!validate_sector_player()) return;
230   ::Player* tux = Sector::current()->player;
231   tux->invincible_timer.stop();
232   tux->set_ghost_mode(false);
233 }
234
235 void restart()
236 {
237   if (GameSession::current() == 0)
238   {
239     log_info << "No game session" << std::endl;
240     return;
241   }
242   GameSession::current()->restart_level();
243 }
244
245 void whereami()
246 {
247   if (!validate_sector_player()) return;
248   ::Player* tux = Sector::current()->player;
249   log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
250 }
251
252 void gotoend()
253 {
254   if (!validate_sector_player()) return;
255   ::Player* tux = Sector::current()->player;
256   tux->move(Vector(
257               (Sector::current()->get_width()) - (SCREEN_WIDTH*2), 0));
258   Sector::current()->camera->reset(
259     Vector(tux->get_pos().x, tux->get_pos().y));
260 }
261
262 void camera()
263 {
264   if (!validate_sector_player()) return;
265   log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
266 }
267
268 void set_gamma(float gamma) {
269   SDL_SetGamma(gamma, gamma, gamma);
270 }
271
272 void quit()
273 {
274   g_screen_manager->quit();
275 }
276
277 int rand()
278 {
279   return systemRandom.rand();
280 }
281
282 void set_game_speed(float speed)
283 {
284   ::g_game_speed = speed;
285 }
286
287 void record_demo(const std::string& filename)
288 {
289   if (GameSession::current() == 0)
290   {
291     log_info << "No game session" << std::endl;
292     return;
293   }
294   GameSession::current()->restart_level();
295   GameSession::current()->record_demo(filename);
296 }
297
298 void play_demo(const std::string& filename)
299 {
300   if (GameSession::current() == 0)
301   {
302     log_info << "No game session" << std::endl;
303     return;
304   }
305   // Reset random seed
306   g_config->random_seed = GameSession::current()->get_demo_random_seed(filename);
307   g_config->random_seed = systemRandom.srand(g_config->random_seed);
308   GameSession::current()->restart_level();
309   GameSession::current()->play_demo(filename);
310 }
311
312 }
313
314 /* EOF */