4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "textscroller.hpp"
28 #include "functions.hpp"
29 #include "game_session.hpp"
30 #include "tinygettext/tinygettext.hpp"
31 #include "physfs/physfs_stream.hpp"
32 #include "resources.hpp"
33 #include "gettext.hpp"
35 #include "mainloop.hpp"
36 #include "worldmap/worldmap.hpp"
37 #include "worldmap/tux.hpp"
40 #include "gameconfig.hpp"
41 #include "object/player.hpp"
42 #include "object/tilemap.hpp"
44 #include "fadeout.hpp"
45 #include "shrinkfade.hpp"
46 #include "object/camera.hpp"
47 #include "flip_level_transformer.hpp"
48 #include "audio/sound_manager.hpp"
49 #include "random_generator.hpp"
51 #include "squirrel_error.hpp"
52 #include "squirrel_util.hpp"
53 #include "time_scheduler.hpp"
55 extern float game_speed;
60 SQInteger display(HSQUIRRELVM vm)
62 Console::output << squirrel2string(vm, -1) << std::endl;
66 void print_stacktrace(HSQUIRRELVM vm)
68 print_squirrel_stack(vm);
71 SQInteger get_current_thread(HSQUIRRELVM vm)
73 sq_pushobject(vm, vm_to_object(vm));
77 void wait(HSQUIRRELVM vm, float seconds)
79 TimeScheduler::instance->schedule_thread(vm, game_time + seconds);
82 void wait_for_screenswitch(HSQUIRRELVM vm)
84 main_loop->waiting_threads.add(vm);
89 main_loop->exit_screen();
92 void fadeout_screen(float seconds)
94 main_loop->set_screen_fade(new FadeOut(seconds));
97 void shrink_screen(float dest_x, float dest_y, float seconds)
99 main_loop->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
102 void abort_screenfade()
104 main_loop->set_screen_fade(NULL);
107 std::string translate(const std::string& text)
109 return dictionary_manager.get_dictionary().translate(text);
112 void display_text_file(const std::string& filename)
114 main_loop->push_screen(new TextScroller(filename));
117 void load_worldmap(const std::string& filename)
119 using namespace WorldMapNS;
121 main_loop->push_screen(new WorldMap(filename));
124 void load_level(const std::string& filename)
126 main_loop->push_screen(new GameSession(filename));
129 static SQInteger squirrel_read_char(SQUserPointer file)
131 std::istream* in = reinterpret_cast<std::istream*> (file);
139 void import(HSQUIRRELVM vm, const std::string& filename)
141 IFileStream in(filename);
143 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
144 filename.c_str(), SQTrue)))
145 throw SquirrelError(vm, "Couldn't parse script");
147 sq_pushroottable(vm);
148 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
150 throw SquirrelError(vm, "Couldn't execute script");
155 void debug_collrects(bool enable)
157 Sector::show_collrects = enable;
160 void debug_show_fps(bool enable)
162 config->show_fps = enable;
165 void debug_draw_solids_only(bool enable)
167 Sector::draw_solids_only = enable;
170 void debug_worldmap_ghost(bool enable)
172 using namespace WorldMapNS;
174 if(WorldMap::current() == NULL)
175 throw std::runtime_error("Can't change ghost mode without active WorldMap");
177 WorldMap::current()->get_tux()->set_ghost_mode(enable);
182 using namespace WorldMapNS;
184 if(World::current() == NULL || WorldMap::current() == NULL)
185 throw std::runtime_error("Can't save state without active World");
187 WorldMap::current()->save_state();
188 World::current()->save_state();
191 void update_worldmap()
193 using namespace WorldMapNS;
195 if(WorldMap::current() == NULL)
196 throw std::runtime_error("Can't update Worldmap: none active");
198 WorldMap::current()->load_state();
201 // not added to header, function to only be used by others
203 bool validate_sector_player()
205 if (Sector::current() == 0)
207 log_info << "No current sector." << std::endl;
211 if (Sector::current()->player == 0)
213 log_info << "No player." << std::endl;
219 void play_music(const std::string& filename)
221 sound_manager->play_music(filename);
224 void play_sound(const std::string& filename)
226 sound_manager->play(filename);
231 if (!validate_sector_player()) return;
232 ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
233 tux->physic.set_velocity_x(tux->physic.get_velocity_x()*3);
238 if (!validate_sector_player()) return;
239 ::Player* tux = Sector::current()->player;
240 tux->invincible_timer.start(10000);
245 if (!validate_sector_player()) return;
246 ::Player* tux = Sector::current()->player;
247 tux->set_ghost_mode(true);
252 if (!validate_sector_player()) return;
253 ::Player* tux = Sector::current()->player;
254 tux->invincible_timer.stop();
255 tux->set_ghost_mode(false);
260 if (GameSession::current() == 0)
262 log_info << "No game session" << std::endl;
265 GameSession::current()->restart_level();
270 if (!validate_sector_player()) return;
271 ::Player* tux = Sector::current()->player;
272 log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
277 if (!validate_sector_player()) return;
278 ::Player* tux = Sector::current()->player;
280 (Sector::current()->get_width()) - (SCREEN_WIDTH*2), 0));
281 Sector::current()->camera->reset(
282 Vector(tux->get_pos().x, tux->get_pos().y));
287 if (!validate_sector_player()) return;
288 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
291 void set_gamma(float gamma) {
292 SDL_SetGamma(gamma, gamma, gamma);
302 return systemRandom.rand();
305 void set_game_speed(float speed)
307 ::game_speed = speed;
310 void record_demo(const std::string& filename)
312 if (GameSession::current() == 0)
314 log_info << "No game session" << std::endl;
317 GameSession::current()->restart_level();
318 GameSession::current()->record_demo(filename);
321 void play_demo(const std::string& filename)
323 if (GameSession::current() == 0)
325 log_info << "No game session" << std::endl;
329 config->random_seed = GameSession::current()->get_demo_random_seed(filename);
330 config->random_seed = systemRandom.srand(config->random_seed);
331 GameSession::current()->restart_level();
332 GameSession::current()->play_demo(filename);