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.hpp"
40 #include "object/player.hpp"
41 #include "object/tilemap.hpp"
43 #include "object/camera.hpp"
44 #include "flip_level_transformer.hpp"
46 #include "squirrel_error.hpp"
47 #include "wrapper_util.hpp"
52 int display(HSQUIRRELVM vm)
54 Console::output << squirrel2string(vm, -1) << std::endl;
58 void wait(HSQUIRRELVM vm, float seconds)
60 SQUserPointer ptr = sq_getforeignptr(vm);
61 ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
62 script_manager->set_wakeup_event(vm, ScriptManager::TIME, seconds);
65 void wait_for_screenswitch(HSQUIRRELVM vm)
67 SQUserPointer ptr = sq_getforeignptr(vm);
68 ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
69 script_manager->set_wakeup_event(vm, ScriptManager::SCREEN_SWITCHED);
74 main_loop->exit_screen();
77 std::string translate(const std::string& text)
79 return dictionary_manager.get_dictionary().translate(text);
82 void display_text_file(const std::string& filename)
84 main_loop->push_screen(new TextScroller(filename));
87 void load_worldmap(const std::string& filename)
89 using namespace WorldMapNS;
91 std::auto_ptr<WorldMap> worldmap(new WorldMap());
92 worldmap->loadmap(filename);
93 main_loop->push_screen(worldmap.release());
96 void load_level(const std::string& filename)
98 main_loop->push_screen(new GameSession(filename, ST_GL_PLAY));
101 static SQInteger squirrel_read_char(SQUserPointer file)
103 std::istream* in = reinterpret_cast<std::istream*> (file);
112 void import(HSQUIRRELVM vm, const std::string& filename)
114 IFileStream in(filename);
116 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
117 filename.c_str(), SQTrue)))
118 throw SquirrelError(vm, "Couldn't parse script");
120 sq_pushroottable(vm);
121 if(SQ_FAILED(sq_call(vm, 1, SQFalse))) {
123 throw SquirrelError(vm, "Couldn't execute script");
128 void add_key(int new_key)
130 player_status->set_keys(new_key);
133 void debug_collrects(bool enable)
135 Sector::show_collrects = enable;
138 void draw_solids_only(bool enable)
140 Sector::draw_solids_only = enable;
145 if(World::current() == NULL)
146 throw std::runtime_error("Can't save state without active World");
148 World::current()->save_state();
151 // not added to header, function to only be used by others
153 bool validate_sector_player()
155 if (Sector::current() == 0)
157 log_info << "No current sector." << std::endl;
161 if (Sector::current()->player == 0)
163 log_info << "No player." << std::endl;
171 if (!validate_sector_player()) return;
172 ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
173 tux->physic.set_velocity_x(tux->physic.get_velocity_x()*3);
178 if (!validate_sector_player()) return;
179 ::Player* tux = Sector::current()->player;
180 tux->invincible_timer.start(10000);
185 if (!validate_sector_player()) return;
186 ::Player* tux = Sector::current()->player;
187 tux->invincible_timer.stop();
192 if (!validate_sector_player()) return;
193 ::Player* tux = Sector::current()->player;
194 tux->kill(tux->SHRINK);
199 if (!validate_sector_player()) return;
200 ::Player* tux = Sector::current()->player;
201 tux->kill(tux->KILL);
206 if (GameSession::current() == 0)
208 log_info << "No game session" << std::endl;
211 GameSession::current()->restart_level();
216 if (!validate_sector_player()) return;
217 ::Player* tux = Sector::current()->player;
218 log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
223 if (!validate_sector_player()) return;
224 ::Player* tux = Sector::current()->player;
226 (Sector::current()->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
227 Sector::current()->camera->reset(
228 Vector(tux->get_pos().x, tux->get_pos().y));
233 if (GameSession::current() == 0)
235 log_info << "No game session" << std::endl;
238 if (GameSession::current()->get_current_level() == 0)
240 log_info << "No current level" << std::endl;
243 FlipLevelTransformer flip_transformer;
244 flip_transformer.transform(GameSession::current()->get_current_level());
249 if (GameSession::current() == 0)
251 log_info << "No game session" << std::endl;
254 GameSession::current()->finish(true);
259 if (!validate_sector_player()) return;
260 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;