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"
49 #include "squirrel_error.hpp"
50 #include "squirrel_util.hpp"
51 #include "time_scheduler.hpp"
56 int display(HSQUIRRELVM vm)
58 Console::output << squirrel2string(vm, -1) << std::endl;
62 void print_stacktrace(HSQUIRRELVM vm)
64 print_squirrel_stack(vm);
67 int get_current_thread(HSQUIRRELVM vm)
69 sq_pushobject(vm, vm_to_object(vm));
73 void wait(HSQUIRRELVM vm, float seconds)
75 TimeScheduler::instance->schedule_thread(vm, game_time + seconds);
78 void wait_for_screenswitch(HSQUIRRELVM vm)
80 main_loop->waiting_threads.add(vm);
85 main_loop->exit_screen();
88 void fadeout_screen(float seconds)
90 main_loop->set_screen_fade(new FadeOut(seconds));
93 void shrink_screen(float dest_x, float dest_y, float seconds)
95 main_loop->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
98 std::string translate(const std::string& text)
100 return dictionary_manager.get_dictionary().translate(text);
103 void display_text_file(const std::string& filename)
105 main_loop->push_screen(new TextScroller(filename));
108 void load_worldmap(const std::string& filename)
110 using namespace WorldMapNS;
112 main_loop->push_screen(new WorldMap(filename));
115 void load_level(const std::string& filename)
117 main_loop->push_screen(new GameSession(filename));
120 static SQInteger squirrel_read_char(SQUserPointer file)
122 std::istream* in = reinterpret_cast<std::istream*> (file);
130 void import(HSQUIRRELVM vm, const std::string& filename)
132 IFileStream in(filename);
134 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
135 filename.c_str(), SQTrue)))
136 throw SquirrelError(vm, "Couldn't parse script");
138 sq_pushroottable(vm);
139 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
141 throw SquirrelError(vm, "Couldn't execute script");
146 void debug_collrects(bool enable)
148 Sector::show_collrects = enable;
151 void debug_draw_fps(bool enable)
153 config->show_fps = enable;
156 void debug_draw_solids_only(bool enable)
158 Sector::draw_solids_only = enable;
163 using namespace WorldMapNS;
165 if(World::current() == NULL)
166 throw std::runtime_error("Can't save state without active World");
168 if(WorldMap::current() != NULL)
169 WorldMap::current()->save_state();
170 World::current()->save_state();
173 // not added to header, function to only be used by others
175 bool validate_sector_player()
177 if (Sector::current() == 0)
179 log_info << "No current sector." << std::endl;
183 if (Sector::current()->player == 0)
185 log_info << "No player." << std::endl;
191 void play_music(const std::string& filename)
193 sound_manager->play_music(filename);
196 void play_sound(const std::string& filename)
198 sound_manager->play(filename);
203 if (!validate_sector_player()) return;
204 ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
205 tux->physic.set_velocity_x(tux->physic.get_velocity_x()*3);
210 if (!validate_sector_player()) return;
211 ::Player* tux = Sector::current()->player;
212 tux->invincible_timer.start(10000);
217 if (!validate_sector_player()) return;
218 ::Player* tux = Sector::current()->player;
219 tux->invincible_timer.stop();
224 if (!validate_sector_player()) return;
225 ::Player* tux = Sector::current()->player;
226 tux->kill(tux->SHRINK);
231 if (!validate_sector_player()) return;
232 ::Player* tux = Sector::current()->player;
233 tux->kill(tux->KILL);
238 if (GameSession::current() == 0)
240 log_info << "No game session" << std::endl;
243 GameSession::current()->restart_level();
248 if (!validate_sector_player()) return;
249 ::Player* tux = Sector::current()->player;
250 log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
255 if (!validate_sector_player()) return;
256 ::Player* tux = Sector::current()->player;
258 (Sector::current()->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
259 Sector::current()->camera->reset(
260 Vector(tux->get_pos().x, tux->get_pos().y));
265 if (!validate_sector_player()) return;
266 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;