- Avoid some expensive SDL_GetTicks() calls
[supertux.git] / src / scripting / functions.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include <memory>
23 #include <stdio.h>
24 #include <string>
25 #include <squirrel.h>
26 #include <sqstdio.h>
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"
34 #include "log.hpp"
35 #include "mainloop.hpp"
36 #include "worldmap/worldmap.hpp"
37 #include "world.hpp"
38 #include "sector.hpp"
39 #include "gameconfig.hpp"
40 #include "object/player.hpp"
41 #include "object/tilemap.hpp"
42 #include "main.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
49 #include "squirrel_error.hpp"
50 #include "squirrel_util.hpp"
51 #include "time_scheduler.hpp"
52
53 namespace Scripting
54 {
55
56 int display(HSQUIRRELVM vm)
57 {
58   Console::output << squirrel2string(vm, -1) << std::endl;
59   return 0;
60 }
61
62 void print_stacktrace(HSQUIRRELVM vm)
63 {
64   print_squirrel_stack(vm);
65 }
66
67 int get_current_thread(HSQUIRRELVM vm)
68 {
69   sq_pushobject(vm, vm_to_object(vm));
70   return 1;
71 }
72
73 void wait(HSQUIRRELVM vm, float seconds)
74 {
75   TimeScheduler::instance->schedule_thread(vm, game_time + seconds);
76 }
77
78 void wait_for_screenswitch(HSQUIRRELVM vm)
79 {
80   main_loop->waiting_threads.add(vm);
81 }
82
83 void exit_screen()
84 {
85   main_loop->exit_screen();
86 }
87
88 void fadeout_screen(float seconds)
89 {
90   main_loop->set_screen_fade(new FadeOut(seconds));
91 }
92
93 void shrink_screen(float dest_x, float dest_y, float seconds)
94 {
95   main_loop->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
96 }
97
98 std::string translate(const std::string& text)
99 {
100   return dictionary_manager.get_dictionary().translate(text);
101 }
102
103 void display_text_file(const std::string& filename)
104 {
105   main_loop->push_screen(new TextScroller(filename));
106 }
107
108 void load_worldmap(const std::string& filename)
109 {
110   using namespace WorldMapNS;
111
112   main_loop->push_screen(new WorldMap(filename));
113 }
114
115 void load_level(const std::string& filename)
116 {
117   main_loop->push_screen(new GameSession(filename));
118 }
119
120 static SQInteger squirrel_read_char(SQUserPointer file)
121 {
122   std::istream* in = reinterpret_cast<std::istream*> (file);
123   char c = in->get();
124   if(in->eof())
125     return 0;
126
127   return c;
128 }
129
130 void import(HSQUIRRELVM vm, const std::string& filename)
131 {
132   IFileStream in(filename);
133     
134   if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
135           filename.c_str(), SQTrue)))
136     throw SquirrelError(vm, "Couldn't parse script");
137     
138   sq_pushroottable(vm);
139   if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
140     sq_pop(vm, 1);
141     throw SquirrelError(vm, "Couldn't execute script");
142   }
143   sq_pop(vm, 1);
144 }
145
146 void add_key(int new_key)
147 {
148   player_status->set_keys(new_key);
149 }
150
151 void debug_collrects(bool enable)
152 {
153   Sector::show_collrects = enable;
154 }
155
156 void debug_draw_fps(bool enable)
157 {
158   config->show_fps = enable;
159 }
160
161 void debug_draw_solids_only(bool enable)
162 {
163   Sector::draw_solids_only = enable;
164 }
165
166 void save_state()
167 {
168   using namespace WorldMapNS;
169   
170   if(World::current() == NULL)
171     throw std::runtime_error("Can't save state without active World");
172
173   if(WorldMap::current() != NULL)
174     WorldMap::current()->save_state();
175   World::current()->save_state();
176 }
177
178 // not added to header, function to only be used by others
179 // in this file
180 bool validate_sector_player()
181 {
182   if (Sector::current() == 0)
183   {
184     log_info << "No current sector." << std::endl;
185         return false;
186   }
187
188   if (Sector::current()->player == 0)
189   {
190     log_info << "No player." << std::endl;
191         return false;
192   }
193   return true;
194 }
195
196 void play_music(const std::string& filename)
197 {
198   sound_manager->play_music(filename);
199 }
200
201 void play_sound(const std::string& filename)
202 {
203   sound_manager->play(filename);
204 }
205
206 void grease()
207 {
208   if (!validate_sector_player()) return;
209   ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
210   tux->physic.set_velocity_x(tux->physic.get_velocity_x()*3);
211 }
212
213 void invincible()
214 {
215   if (!validate_sector_player()) return;
216   ::Player* tux = Sector::current()->player;
217   tux->invincible_timer.start(10000);
218 }
219
220 void mortal()
221 {
222   if (!validate_sector_player()) return;
223   ::Player* tux = Sector::current()->player;
224   tux->invincible_timer.stop();
225 }
226
227 void shrink()
228 {
229   if (!validate_sector_player()) return;
230   ::Player* tux = Sector::current()->player;
231   tux->kill(tux->SHRINK);
232 }
233
234 void kill()
235 {
236   if (!validate_sector_player()) return;
237   ::Player* tux = Sector::current()->player;
238   tux->kill(tux->KILL);
239 }
240
241 void restart()
242 {
243   if (GameSession::current() == 0)
244   {
245     log_info << "No game session" << std::endl;
246     return;
247   }
248   GameSession::current()->restart_level();
249 }
250
251 void whereami()
252 {
253   if (!validate_sector_player()) return;
254   ::Player* tux = Sector::current()->player;
255   log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
256 }
257
258 void gotoend()
259 {
260   if (!validate_sector_player()) return;
261   ::Player* tux = Sector::current()->player;
262   tux->move(Vector(
263           (Sector::current()->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
264   Sector::current()->camera->reset(
265         Vector(tux->get_pos().x, tux->get_pos().y));
266 }
267
268 void camera()
269 {
270   if (!validate_sector_player()) return;
271   log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
272 }
273
274 void quit()
275 {
276   main_loop->quit();
277 }
278
279 }
280