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"
57 SQInteger display(HSQUIRRELVM vm)
59 Console::output << squirrel2string(vm, -1) << std::endl;
63 void print_stacktrace(HSQUIRRELVM vm)
65 print_squirrel_stack(vm);
68 SQInteger get_current_thread(HSQUIRRELVM vm)
70 sq_pushobject(vm, vm_to_object(vm));
74 void wait(HSQUIRRELVM vm, float seconds)
76 TimeScheduler::instance->schedule_thread(vm, game_time + seconds);
79 void wait_for_screenswitch(HSQUIRRELVM vm)
81 main_loop->waiting_threads.add(vm);
86 main_loop->exit_screen();
89 void fadeout_screen(float seconds)
91 main_loop->set_screen_fade(new FadeOut(seconds));
94 void shrink_screen(float dest_x, float dest_y, float seconds)
96 main_loop->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
99 std::string translate(const std::string& text)
101 return dictionary_manager.get_dictionary().translate(text);
104 void display_text_file(const std::string& filename)
106 main_loop->push_screen(new TextScroller(filename));
109 void load_worldmap(const std::string& filename)
111 using namespace WorldMapNS;
113 main_loop->push_screen(new WorldMap(filename));
116 void load_level(const std::string& filename)
118 main_loop->push_screen(new GameSession(filename));
121 static SQInteger squirrel_read_char(SQUserPointer file)
123 std::istream* in = reinterpret_cast<std::istream*> (file);
131 void import(HSQUIRRELVM vm, const std::string& filename)
133 IFileStream in(filename);
135 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
136 filename.c_str(), SQTrue)))
137 throw SquirrelError(vm, "Couldn't parse script");
139 sq_pushroottable(vm);
140 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
142 throw SquirrelError(vm, "Couldn't execute script");
147 void debug_collrects(bool enable)
149 Sector::show_collrects = enable;
152 void debug_show_fps(bool enable)
154 config->show_fps = enable;
157 void debug_draw_solids_only(bool enable)
159 Sector::draw_solids_only = enable;
164 using namespace WorldMapNS;
166 if(World::current() == NULL)
167 throw std::runtime_error("Can't save state without active World");
169 if(WorldMap::current() != NULL)
170 WorldMap::current()->save_state();
171 World::current()->save_state();
174 // not added to header, function to only be used by others
176 bool validate_sector_player()
178 if (Sector::current() == 0)
180 log_info << "No current sector." << std::endl;
184 if (Sector::current()->player == 0)
186 log_info << "No player." << std::endl;
192 void play_music(const std::string& filename)
194 sound_manager->play_music(filename);
197 void play_sound(const std::string& filename)
199 sound_manager->play(filename);
204 if (!validate_sector_player()) return;
205 ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
206 tux->physic.vx = tux->physic.vx*3;
211 if (!validate_sector_player()) return;
212 ::Player* tux = Sector::current()->player;
213 tux->invincible_timer.start(10000);
218 if (!validate_sector_player()) return;
219 ::Player* tux = Sector::current()->player;
220 tux->set_ghost_mode(true);
225 if (!validate_sector_player()) return;
226 ::Player* tux = Sector::current()->player;
227 tux->invincible_timer.stop();
228 tux->set_ghost_mode(false);
233 if (GameSession::current() == 0)
235 log_info << "No game session" << std::endl;
238 GameSession::current()->restart_level();
243 if (!validate_sector_player()) return;
244 ::Player* tux = Sector::current()->player;
245 log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
250 if (!validate_sector_player()) return;
251 ::Player* tux = Sector::current()->player;
253 (Sector::current()->get_width()) - (SCREEN_WIDTH*2), 0));
254 Sector::current()->camera->reset(
255 Vector(tux->get_pos().x, tux->get_pos().y));
260 if (!validate_sector_player()) return;
261 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
271 return systemRandom.rand();