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