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