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