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 "script_manager.hpp"
33 #include "resources.hpp"
34 #include "gettext.hpp"
36 #include "mainloop.hpp"
37 #include "worldmap/worldmap.hpp"
40 #include "gameconfig.hpp"
41 #include "object/player.hpp"
42 #include "object/tilemap.hpp"
44 #include "object/camera.hpp"
45 #include "flip_level_transformer.hpp"
47 #include "squirrel_error.hpp"
48 #include "wrapper_util.hpp"
53 int display(HSQUIRRELVM vm)
55 Console::output << squirrel2string(vm, -1) << std::endl;
59 void wait(HSQUIRRELVM vm, float seconds)
61 SQUserPointer ptr = sq_getforeignptr(vm);
62 ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
63 script_manager->set_wakeup_event(vm, ScriptManager::TIME, seconds);
66 void wait_for_screenswitch(HSQUIRRELVM vm)
68 SQUserPointer ptr = sq_getforeignptr(vm);
69 ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
70 script_manager->set_wakeup_event(vm, ScriptManager::SCREEN_SWITCHED);
75 main_loop->exit_screen();
78 std::string translate(const std::string& text)
80 return dictionary_manager.get_dictionary().translate(text);
83 void display_text_file(const std::string& filename)
85 main_loop->push_screen(new TextScroller(filename));
88 void load_worldmap(const std::string& filename)
90 using namespace WorldMapNS;
92 main_loop->push_screen(new WorldMap(filename));
95 void load_level(const std::string& filename)
97 main_loop->push_screen(new GameSession(filename));
100 static SQInteger squirrel_read_char(SQUserPointer file)
102 std::istream* in = reinterpret_cast<std::istream*> (file);
111 void import(HSQUIRRELVM vm, const std::string& filename)
113 IFileStream in(filename);
115 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
116 filename.c_str(), SQTrue)))
117 throw SquirrelError(vm, "Couldn't parse script");
119 sq_pushroottable(vm);
120 if(SQ_FAILED(sq_call(vm, 1, SQFalse))) {
122 throw SquirrelError(vm, "Couldn't execute script");
127 void add_key(int new_key)
129 player_status->set_keys(new_key);
132 void debug_collrects(bool enable)
134 Sector::show_collrects = enable;
137 void debug_draw_fps(bool enable)
139 config->show_fps = enable;
142 void debug_draw_solids_only(bool enable)
144 Sector::draw_solids_only = enable;
149 if(World::current() == NULL)
150 throw std::runtime_error("Can't save state without active World");
152 World::current()->save_state();
155 // not added to header, function to only be used by others
157 bool validate_sector_player()
159 if (Sector::current() == 0)
161 log_info << "No current sector." << std::endl;
165 if (Sector::current()->player == 0)
167 log_info << "No player." << std::endl;
175 if (!validate_sector_player()) return;
176 ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
177 tux->physic.set_velocity_x(tux->physic.get_velocity_x()*3);
182 if (!validate_sector_player()) return;
183 ::Player* tux = Sector::current()->player;
184 tux->invincible_timer.start(10000);
189 if (!validate_sector_player()) return;
190 ::Player* tux = Sector::current()->player;
191 tux->invincible_timer.stop();
196 if (!validate_sector_player()) return;
197 ::Player* tux = Sector::current()->player;
198 tux->kill(tux->SHRINK);
203 if (!validate_sector_player()) return;
204 ::Player* tux = Sector::current()->player;
205 tux->kill(tux->KILL);
210 if (GameSession::current() == 0)
212 log_info << "No game session" << std::endl;
215 GameSession::current()->restart_level();
220 if (!validate_sector_player()) return;
221 ::Player* tux = Sector::current()->player;
222 log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
227 if (!validate_sector_player()) return;
228 ::Player* tux = Sector::current()->player;
230 (Sector::current()->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
231 Sector::current()->camera->reset(
232 Vector(tux->get_pos().x, tux->get_pos().y));
237 if (!validate_sector_player()) return;
238 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;