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