the boat works now
[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 "script_manager.hpp"
33 #include "resources.hpp"
34 #include "gettext.hpp"
35 #include "log.hpp"
36 #include "mainloop.hpp"
37 #include "worldmap/worldmap.hpp"
38 #include "world.hpp"
39 #include "sector.hpp"
40 #include "gameconfig.hpp"
41 #include "object/player.hpp"
42 #include "object/tilemap.hpp"
43 #include "main.hpp"
44 #include "object/camera.hpp"
45 #include "flip_level_transformer.hpp"
46
47 #include "squirrel_error.hpp"
48 #include "wrapper_util.hpp"
49
50 namespace Scripting
51 {
52
53 int display(HSQUIRRELVM vm)
54 {
55   Console::output << squirrel2string(vm, -1) << std::endl;
56   return 0;
57 }
58
59 void wait(HSQUIRRELVM vm, float seconds)
60 {
61   SQUserPointer ptr = sq_getforeignptr(vm);
62   ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
63   script_manager->set_wakeup_event(vm, ScriptManager::TIME, seconds);
64 }
65
66 void wait_for_screenswitch(HSQUIRRELVM vm)
67 {
68   SQUserPointer ptr = sq_getforeignptr(vm);
69   ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
70   script_manager->set_wakeup_event(vm, ScriptManager::SCREEN_SWITCHED);
71 }
72
73 void exit_screen()
74 {
75   main_loop->exit_screen();
76 }
77
78 std::string translate(const std::string& text)
79 {
80   return dictionary_manager.get_dictionary().translate(text);
81 }
82
83 void display_text_file(const std::string& filename)
84 {
85   main_loop->push_screen(new TextScroller(filename));
86 }
87
88 void load_worldmap(const std::string& filename)
89 {
90   using namespace WorldMapNS;
91
92   main_loop->push_screen(new WorldMap(filename));
93 }
94
95 void load_level(const std::string& filename)
96 {
97   main_loop->push_screen(new GameSession(filename));
98 }
99
100 static SQInteger squirrel_read_char(SQUserPointer file)
101 {
102   std::istream* in = reinterpret_cast<std::istream*> (file);
103   char c = in->get();
104   if(in->eof())
105     return 0;
106
107   return c;
108 }
109
110
111 void import(HSQUIRRELVM vm, const std::string& filename)
112 {
113   IFileStream in(filename);
114     
115   if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
116           filename.c_str(), SQTrue)))
117     throw SquirrelError(vm, "Couldn't parse script");
118     
119   sq_pushroottable(vm);
120   if(SQ_FAILED(sq_call(vm, 1, SQFalse))) {
121     sq_pop(vm, 1);
122     throw SquirrelError(vm, "Couldn't execute script");
123   }
124   sq_pop(vm, 1);
125 }
126
127 void add_key(int new_key)
128 {
129   player_status->set_keys(new_key);
130 }
131
132 void debug_collrects(bool enable)
133 {
134   Sector::show_collrects = enable;
135 }
136
137 void debug_draw_fps(bool enable)
138 {
139   config->show_fps = enable;
140 }
141
142 void debug_draw_solids_only(bool enable)
143 {
144   Sector::draw_solids_only = enable;
145 }
146
147 void save_state()
148 {
149   if(World::current() == NULL)
150     throw std::runtime_error("Can't save state without active World");
151
152   World::current()->save_state();
153 }
154
155 // not added to header, function to only be used by others
156 // in this file
157 bool validate_sector_player()
158 {
159   if (Sector::current() == 0)
160   {
161     log_info << "No current sector." << std::endl;
162         return false;
163   }
164
165   if (Sector::current()->player == 0)
166   {
167     log_info << "No player." << std::endl;
168         return false;
169   }
170   return true;
171 }
172
173 void grease()
174 {
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);
178 }
179
180 void invincible()
181 {
182   if (!validate_sector_player()) return;
183   ::Player* tux = Sector::current()->player;
184   tux->invincible_timer.start(10000);
185 }
186
187 void mortal()
188 {
189   if (!validate_sector_player()) return;
190   ::Player* tux = Sector::current()->player;
191   tux->invincible_timer.stop();
192 }
193
194 void shrink()
195 {
196   if (!validate_sector_player()) return;
197   ::Player* tux = Sector::current()->player;
198   tux->kill(tux->SHRINK);
199 }
200
201 void kill()
202 {
203   if (!validate_sector_player()) return;
204   ::Player* tux = Sector::current()->player;
205   tux->kill(tux->KILL);
206 }
207
208 void restart()
209 {
210   if (GameSession::current() == 0)
211   {
212     log_info << "No game session" << std::endl;
213     return;
214   }
215   GameSession::current()->restart_level();
216 }
217
218 void whereami()
219 {
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;
223 }
224
225 void gotoend()
226 {
227   if (!validate_sector_player()) return;
228   ::Player* tux = Sector::current()->player;
229   tux->move(Vector(
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));
233 }
234
235 void camera()
236 {
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;
239 }
240
241 void quit()
242 {
243   main_loop->quit();
244 }
245
246 }
247