2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "scripting/functions.hpp"
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"
36 #include "scripting/squirrel_util.hpp"
37 #include "scripting/time_scheduler.hpp"
41 SQInteger display(HSQUIRRELVM vm)
43 Console::output << squirrel2string(vm, -1) << std::endl;
47 void print_stacktrace(HSQUIRRELVM vm)
49 print_squirrel_stack(vm);
52 SQInteger get_current_thread(HSQUIRRELVM vm)
54 sq_pushobject(vm, vm_to_object(vm));
58 void wait(HSQUIRRELVM vm, float seconds)
60 TimeScheduler::instance->schedule_thread(vm, game_time + seconds);
63 void wait_for_screenswitch(HSQUIRRELVM vm)
65 g_screen_manager->waiting_threads.add(vm);
70 g_screen_manager->exit_screen();
73 void fadeout_screen(float seconds)
75 g_screen_manager->set_screen_fade(new FadeOut(seconds));
78 void shrink_screen(float dest_x, float dest_y, float seconds)
80 g_screen_manager->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
83 void abort_screenfade()
85 g_screen_manager->set_screen_fade(NULL);
88 std::string translate(const std::string& text)
90 return dictionary_manager->get_dictionary().translate(text);
93 void display_text_file(const std::string& filename)
95 g_screen_manager->push_screen(new TextScroller(filename));
98 void load_worldmap(const std::string& filename)
100 using namespace worldmap;
102 if(World::current() == NULL)
103 throw std::runtime_error("Can't start WorldMap without active world.");
105 g_screen_manager->push_screen(new WorldMap(filename, World::current()->get_player_status()));
108 void load_level(const std::string& filename)
110 if(GameSession::current() == NULL)
111 throw std::runtime_error("Can't start level without active level.");
113 g_screen_manager->push_screen(new GameSession(filename, GameSession::current()->get_player_status()));
116 static SQInteger squirrel_read_char(SQUserPointer file)
118 std::istream* in = reinterpret_cast<std::istream*> (file);
126 void import(HSQUIRRELVM vm, const std::string& filename)
128 IFileStream in(filename);
130 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
131 filename.c_str(), SQTrue)))
132 throw SquirrelError(vm, "Couldn't parse script");
134 sq_pushroottable(vm);
135 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
137 throw SquirrelError(vm, "Couldn't execute script");
142 void debug_collrects(bool enable)
144 Sector::show_collrects = enable;
147 void debug_show_fps(bool enable)
149 g_config->show_fps = enable;
152 void debug_draw_solids_only(bool enable)
154 Sector::draw_solids_only = enable;
157 void debug_worldmap_ghost(bool enable)
159 using namespace worldmap;
161 if(WorldMap::current() == NULL)
162 throw std::runtime_error("Can't change ghost mode without active WorldMap");
164 WorldMap::current()->get_tux()->set_ghost_mode(enable);
169 using namespace worldmap;
171 if(World::current() == NULL || WorldMap::current() == NULL)
172 throw std::runtime_error("Can't save state without active World");
174 WorldMap::current()->save_state();
175 World::current()->save_state();
178 void update_worldmap()
180 using namespace worldmap;
182 if(WorldMap::current() == NULL)
183 throw std::runtime_error("Can't update Worldmap: none active");
185 WorldMap::current()->load_state();
188 // not added to header, function to only be used by others
190 bool validate_sector_player()
192 if (Sector::current() == 0)
194 log_info << "No current sector." << std::endl;
198 if (Sector::current()->player == 0)
200 log_info << "No player." << std::endl;
206 void play_music(const std::string& filename)
208 sound_manager->play_music(filename);
211 void play_sound(const std::string& filename)
213 sound_manager->play(filename);
218 if (!validate_sector_player()) return;
219 ::Player* tux = Sector::current()->player; // scripting::Player != ::Player
220 tux->get_physic().set_velocity_x(tux->get_physic().get_velocity_x()*3);
225 if (!validate_sector_player()) return;
226 ::Player* tux = Sector::current()->player;
227 tux->invincible_timer.start(10000);
232 if (!validate_sector_player()) return;
233 ::Player* tux = Sector::current()->player;
234 tux->set_ghost_mode(true);
239 if (!validate_sector_player()) return;
240 ::Player* tux = Sector::current()->player;
241 tux->invincible_timer.stop();
242 tux->set_ghost_mode(false);
247 if (GameSession::current() == 0)
249 log_info << "No game session" << std::endl;
252 GameSession::current()->restart_level();
257 if (!validate_sector_player()) return;
258 ::Player* tux = Sector::current()->player;
259 log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
264 if (!validate_sector_player()) return;
265 ::Player* tux = Sector::current()->player;
267 (Sector::current()->get_width()) - (SCREEN_WIDTH*2), 0));
268 Sector::current()->camera->reset(
269 Vector(tux->get_pos().x, tux->get_pos().y));
274 if (!validate_sector_player()) return;
275 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
278 void set_gamma(float gamma) {
279 SDL_SetGamma(gamma, gamma, gamma);
284 g_screen_manager->quit();
289 return systemRandom.rand();
292 void set_game_speed(float speed)
294 ::g_game_speed = speed;
297 void record_demo(const std::string& filename)
299 if (GameSession::current() == 0)
301 log_info << "No game session" << std::endl;
304 GameSession::current()->restart_level();
305 GameSession::current()->record_demo(filename);
308 void play_demo(const std::string& filename)
310 if (GameSession::current() == 0)
312 log_info << "No game session" << std::endl;
316 g_config->random_seed = GameSession::current()->get_demo_random_seed(filename);
317 g_config->random_seed = systemRandom.srand(g_config->random_seed);
318 GameSession::current()->restart_level();
319 GameSession::current()->play_demo(filename);