00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "scripting/functions.hpp"
00018
00019 #include "audio/sound_manager.hpp"
00020 #include "math/random_generator.hpp"
00021 #include "object/camera.hpp"
00022 #include "object/player.hpp"
00023 #include "physfs/ifile_stream.hpp"
00024 #include "supertux/fadeout.hpp"
00025 #include "supertux/game_session.hpp"
00026 #include "supertux/gameconfig.hpp"
00027 #include "supertux/globals.hpp"
00028 #include "supertux/screen_manager.hpp"
00029 #include "supertux/sector.hpp"
00030 #include "supertux/shrinkfade.hpp"
00031 #include "supertux/textscroller.hpp"
00032 #include "supertux/tile.hpp"
00033 #include "supertux/world.hpp"
00034 #include "util/gettext.hpp"
00035 #include "worldmap/tux.hpp"
00036
00037 #include "scripting/squirrel_util.hpp"
00038 #include "scripting/time_scheduler.hpp"
00039
00040 namespace scripting {
00041
00042 SQInteger display(HSQUIRRELVM vm)
00043 {
00044 Console::output << squirrel2string(vm, -1) << std::endl;
00045 return 0;
00046 }
00047
00048 void print_stacktrace(HSQUIRRELVM vm)
00049 {
00050 print_squirrel_stack(vm);
00051 }
00052
00053 SQInteger get_current_thread(HSQUIRRELVM vm)
00054 {
00055 sq_pushobject(vm, vm_to_object(vm));
00056 return 1;
00057 }
00058
00059 void wait(HSQUIRRELVM vm, float seconds)
00060 {
00061 TimeScheduler::instance->schedule_thread(vm, game_time + seconds);
00062 }
00063
00064 void wait_for_screenswitch(HSQUIRRELVM vm)
00065 {
00066 g_screen_manager->waiting_threads.add(vm);
00067 }
00068
00069 void exit_screen()
00070 {
00071 g_screen_manager->exit_screen();
00072 }
00073
00074 void fadeout_screen(float seconds)
00075 {
00076 g_screen_manager->set_screen_fade(new FadeOut(seconds));
00077 }
00078
00079 void shrink_screen(float dest_x, float dest_y, float seconds)
00080 {
00081 g_screen_manager->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
00082 }
00083
00084 void abort_screenfade()
00085 {
00086 g_screen_manager->set_screen_fade(NULL);
00087 }
00088
00089 std::string translate(const std::string& text)
00090 {
00091 return dictionary_manager->get_dictionary().translate(text);
00092 }
00093
00094 void display_text_file(const std::string& filename)
00095 {
00096 g_screen_manager->push_screen(new TextScroller(filename));
00097 }
00098
00099 void load_worldmap(const std::string& filename)
00100 {
00101 using namespace worldmap;
00102
00103 if(World::current() == NULL)
00104 throw std::runtime_error("Can't start WorldMap without active world.");
00105
00106 g_screen_manager->push_screen(new WorldMap(filename, World::current()->get_player_status()));
00107 }
00108
00109 void load_level(const std::string& filename)
00110 {
00111 if(GameSession::current() == NULL)
00112 throw std::runtime_error("Can't start level without active level.");
00113
00114 g_screen_manager->push_screen(new GameSession(filename, GameSession::current()->get_player_status()));
00115 }
00116
00117 void import(HSQUIRRELVM vm, const std::string& filename)
00118 {
00119 IFileStream in(filename);
00120
00121 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
00122 filename.c_str(), SQTrue)))
00123 throw SquirrelError(vm, "Couldn't parse script");
00124
00125 sq_pushroottable(vm);
00126 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
00127 sq_pop(vm, 1);
00128 throw SquirrelError(vm, "Couldn't execute script");
00129 }
00130 sq_pop(vm, 1);
00131 }
00132
00133 void debug_collrects(bool enable)
00134 {
00135 Sector::show_collrects = enable;
00136 }
00137
00138 void debug_show_fps(bool enable)
00139 {
00140 g_config->show_fps = enable;
00141 }
00142
00143 void debug_draw_solids_only(bool enable)
00144 {
00145 Sector::draw_solids_only = enable;
00146 }
00147
00148 void debug_draw_editor_images(bool enable)
00149 {
00150 Tile::draw_editor_images = enable;
00151 }
00152
00153 void debug_worldmap_ghost(bool enable)
00154 {
00155 using namespace worldmap;
00156
00157 if(WorldMap::current() == NULL)
00158 throw std::runtime_error("Can't change ghost mode without active WorldMap");
00159
00160 WorldMap::current()->get_tux()->set_ghost_mode(enable);
00161 }
00162
00163 void save_state()
00164 {
00165 using namespace worldmap;
00166
00167 if(World::current() == NULL || WorldMap::current() == NULL)
00168 throw std::runtime_error("Can't save state without active World");
00169
00170 WorldMap::current()->save_state();
00171 World::current()->save_state();
00172 }
00173
00174 void update_worldmap()
00175 {
00176 using namespace worldmap;
00177
00178 if(WorldMap::current() == NULL)
00179 throw std::runtime_error("Can't update Worldmap: none active");
00180
00181 WorldMap::current()->load_state();
00182 }
00183
00184
00185
00186 bool validate_sector_player()
00187 {
00188 if (Sector::current() == 0)
00189 {
00190 log_info << "No current sector." << std::endl;
00191 return false;
00192 }
00193
00194 if (Sector::current()->player == 0)
00195 {
00196 log_info << "No player." << std::endl;
00197 return false;
00198 }
00199 return true;
00200 }
00201
00202 void play_music(const std::string& filename)
00203 {
00204 sound_manager->play_music(filename);
00205 }
00206
00207 void play_sound(const std::string& filename)
00208 {
00209 sound_manager->play(filename);
00210 }
00211
00212 void grease()
00213 {
00214 if (!validate_sector_player()) return;
00215 ::Player* tux = Sector::current()->player;
00216 tux->get_physic().set_velocity_x(tux->get_physic().get_velocity_x()*3);
00217 }
00218
00219 void invincible()
00220 {
00221 if (!validate_sector_player()) return;
00222 ::Player* tux = Sector::current()->player;
00223 tux->invincible_timer.start(10000);
00224 }
00225
00226 void ghost()
00227 {
00228 if (!validate_sector_player()) return;
00229 ::Player* tux = Sector::current()->player;
00230 tux->set_ghost_mode(true);
00231 }
00232
00233 void mortal()
00234 {
00235 if (!validate_sector_player()) return;
00236 ::Player* tux = Sector::current()->player;
00237 tux->invincible_timer.stop();
00238 tux->set_ghost_mode(false);
00239 }
00240
00241 void restart()
00242 {
00243 if (GameSession::current() == 0)
00244 {
00245 log_info << "No game session" << std::endl;
00246 return;
00247 }
00248 GameSession::current()->restart_level();
00249 }
00250
00251 void whereami()
00252 {
00253 if (!validate_sector_player()) return;
00254 ::Player* tux = Sector::current()->player;
00255 log_info << "You are at x " << ((int) tux->get_pos().x) << ", y " << ((int) tux->get_pos().y) << std::endl;
00256 }
00257
00258 void gotoend()
00259 {
00260 if (!validate_sector_player()) return;
00261 ::Player* tux = Sector::current()->player;
00262 tux->move(Vector(
00263 (Sector::current()->get_width()) - (SCREEN_WIDTH*2), 0));
00264 Sector::current()->camera->reset(
00265 Vector(tux->get_pos().x, tux->get_pos().y));
00266 }
00267
00268 void camera()
00269 {
00270 if (!validate_sector_player()) return;
00271 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
00272 }
00273
00274 void set_gamma(float gamma) {
00275 SDL_SetGamma(gamma, gamma, gamma);
00276 }
00277
00278 void quit()
00279 {
00280 g_screen_manager->quit();
00281 }
00282
00283 int rand()
00284 {
00285 return gameRandom.rand();
00286 }
00287
00288 void set_game_speed(float speed)
00289 {
00290 ::g_game_speed = speed;
00291 }
00292
00293 void record_demo(const std::string& filename)
00294 {
00295 if (GameSession::current() == 0)
00296 {
00297 log_info << "No game session" << std::endl;
00298 return;
00299 }
00300 GameSession::current()->restart_level();
00301 GameSession::current()->record_demo(filename);
00302 }
00303
00304 void play_demo(const std::string& filename)
00305 {
00306 if (GameSession::current() == 0)
00307 {
00308 log_info << "No game session" << std::endl;
00309 return;
00310 }
00311
00312 g_config->random_seed = GameSession::current()->get_demo_random_seed(filename);
00313 g_config->random_seed = gameRandom.srand(g_config->random_seed);
00314 GameSession::current()->restart_level();
00315 GameSession::current()->play_demo(filename);
00316 }
00317
00318 }
00319
00320