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"
39 #include "gameconfig.hpp"
40 #include "object/player.hpp"
41 #include "object/tilemap.hpp"
43 #include "fadeout.hpp"
44 #include "shrinkfade.hpp"
45 #include "object/camera.hpp"
46 #include "flip_level_transformer.hpp"
47 #include "audio/sound_manager.hpp"
48 #include "random_generator.hpp"
50 #include "squirrel_error.hpp"
51 #include "squirrel_util.hpp"
52 #include "time_scheduler.hpp"
54 extern float game_speed;
59 SQInteger display(HSQUIRRELVM vm)
61 Console::output << squirrel2string(vm, -1) << std::endl;
65 void print_stacktrace(HSQUIRRELVM vm)
67 print_squirrel_stack(vm);
70 SQInteger get_current_thread(HSQUIRRELVM vm)
72 sq_pushobject(vm, vm_to_object(vm));
76 void wait(HSQUIRRELVM vm, float seconds)
78 TimeScheduler::instance->schedule_thread(vm, game_time + seconds);
81 void wait_for_screenswitch(HSQUIRRELVM vm)
83 main_loop->waiting_threads.add(vm);
88 main_loop->exit_screen();
91 void fadeout_screen(float seconds)
93 main_loop->set_screen_fade(new FadeOut(seconds));
96 void shrink_screen(float dest_x, float dest_y, float seconds)
98 main_loop->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
101 void abort_screenfade()
103 main_loop->set_screen_fade(NULL);
106 std::string translate(const std::string& text)
108 return dictionary_manager.get_dictionary().translate(text);
111 void display_text_file(const std::string& filename)
113 main_loop->push_screen(new TextScroller(filename));
116 void load_worldmap(const std::string& filename)
118 using namespace WorldMapNS;
120 main_loop->push_screen(new WorldMap(filename));
123 void load_level(const std::string& filename)
125 main_loop->push_screen(new GameSession(filename));
128 static SQInteger squirrel_read_char(SQUserPointer file)
130 std::istream* in = reinterpret_cast<std::istream*> (file);
138 void import(HSQUIRRELVM vm, const std::string& filename)
140 IFileStream in(filename);
142 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
143 filename.c_str(), SQTrue)))
144 throw SquirrelError(vm, "Couldn't parse script");
146 sq_pushroottable(vm);
147 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
149 throw SquirrelError(vm, "Couldn't execute script");
154 void debug_collrects(bool enable)
156 Sector::show_collrects = enable;
159 void debug_show_fps(bool enable)
161 config->show_fps = enable;
164 void debug_draw_solids_only(bool enable)
166 Sector::draw_solids_only = enable;
171 using namespace WorldMapNS;
173 if(World::current() == NULL || WorldMap::current() == NULL)
174 throw std::runtime_error("Can't save state without active World");
176 WorldMap::current()->save_state();
177 World::current()->save_state();
180 void update_worldmap()
182 using namespace WorldMapNS;
184 if(WorldMap::current() == NULL)
185 throw std::runtime_error("Can't update Worldmap: none active");
187 WorldMap::current()->load_state();
190 // not added to header, function to only be used by others
192 bool validate_sector_player()
194 if (Sector::current() == 0)
196 log_info << "No current sector." << std::endl;
200 if (Sector::current()->player == 0)
202 log_info << "No player." << std::endl;
208 void play_music(const std::string& filename)
210 sound_manager->play_music(filename);
213 void play_sound(const std::string& filename)
215 sound_manager->play(filename);
220 if (!validate_sector_player()) return;
221 ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
222 tux->physic.set_velocity_x(tux->physic.get_velocity_x()*3);
227 if (!validate_sector_player()) return;
228 ::Player* tux = Sector::current()->player;
229 tux->invincible_timer.start(10000);
234 if (!validate_sector_player()) return;
235 ::Player* tux = Sector::current()->player;
236 tux->set_ghost_mode(true);
241 if (!validate_sector_player()) return;
242 ::Player* tux = Sector::current()->player;
243 tux->invincible_timer.stop();
244 tux->set_ghost_mode(false);
249 if (GameSession::current() == 0)
251 log_info << "No game session" << std::endl;
254 GameSession::current()->restart_level();
259 if (!validate_sector_player()) return;
260 ::Player* tux = Sector::current()->player;
261 log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
266 if (!validate_sector_player()) return;
267 ::Player* tux = Sector::current()->player;
269 (Sector::current()->get_width()) - (SCREEN_WIDTH*2), 0));
270 Sector::current()->camera->reset(
271 Vector(tux->get_pos().x, tux->get_pos().y));
276 if (!validate_sector_player()) return;
277 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
287 return systemRandom.rand();
290 void set_game_speed(float speed)
292 ::game_speed = speed;