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