* Fixed an underquoted definition of PKG_CHECK_MODULES
[supertux.git] / docs / scripting / scripting.xml
1 <?xml version='1.0' ?>
2 <!--
3 $Id$
4
5 SuperTux Documentation
6 Copyright (C) 2005 Ondra Hosek <ondra.hosek@gmail.com>
7
8 This document is free; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This document is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program (see the file named 'COPYING'); if not,
20 write to the Free Software Foundation, Inc., 59 Temple Place -
21 Suite 330, Boston, MA 02111-1307, USA.
22
23 -->
24 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.3//EN"
25 "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
26
27 <article xml:lang="en">
28
29 <articleinfo>
30 <title>SuperTux Scripting Documentation</title>
31 <author><firstname>Ondra</firstname><surname>Hosek</surname></author>
32 </articleinfo>
33 <para>Since May 2005, SuperTux sports a Squirrel scripting interface useful for level designers who want to add some interactive pep to their levels. This document poses as a reference article for those who want to explore the various objects of the SuperTux scripting model.</para>
34 <sect1><title>What is Squirrel?</title>
35 <para>One of your first questions might be, &quot;What does a rodent have to do with a penguin?&quot; <ulink url="http://squirrel.sourceforge.net/">Squirrel</ulink> is a language with a syntax not much unlike other C-like languages (C, C++, Java, ...). In the current implementation, it is integrated as elements in the SuperTux level files.</para>
36 </sect1>
37 <sect1><title>Squirrel, Scheme and SuperTux</title>
38 <para>I have no clue if the developers simply chose Squirrel just because the name so nicely integrates into the series of words &quot;SuperTux&quot; and &quot;Scheme&quot;. Currently, the Squirrel code is integrated in string arguments of Scheme elements in SuperTux level files. (Whew.) This is an example code block inside a level:
39 <programlisting>(supertux-level
40   (version 2)
41   (name _("Go Blind"))
42   (author "Team")
43   (sector
44     (name "main")
45     (music "Annoying_penguin_gawking_sounds.mod")
46
47     ;; (Tilemaps, objects, whatever.)
48
49     (init-script "
50 DisplayEffect.fade_out(2.5);
51 ")
52   )
53 )</programlisting>
54 When this level loads, the screen fades out completely during two and a half seconds right after the level is loaded. (Mind you, this would be a frustrating experience for the player if you add a horde of badguys near the spawn point.)</para>
55 </sect1>
56
57 <sect1><title>Object reference</title>
58 <para>If you are interested in an object and what cans of worms you can open with it, this section is for you.</para>
59 <para>&quot;(NYI)&quot; after the function name symbolises functions that haven't been implemented yet. Calling them will result in a line being printed to standard output informing anybody who reads it that the script is using a function that actually doesn't exist. (Win32 users will happily ignore this, because they simply start the application by opening it with Explorer. Unix users are going to be more interested in what SuperTux can actually tell them, so it's better if you don't use non-existent functions in your scripts.)</para>
60
61 <sect2><title>Global Constants</title>
62 <para>These constants can be accessed from anywhere in the game code.</para>
63
64 <sect3><title>KEY_BRASS</title>
65 <para>Type: integer</para>
66 <para>Value: <code>1</code></para>
67 <para>Represents the brass key.</para>
68 <para>Used in: <code>add_key</code> as argument 1</para>
69 </sect3>
70
71 <sect3><title>KEY_IRON</title>
72 <para>Type: integer</para>
73 <para>Value: <code>2</code></para>
74 <para>Represents the iron key.</para>
75 <para>Used in: <code>add_key</code> as argument 1</para>
76 </sect3>
77
78 <sect3><title>KEY_BRONZE</title>
79 <para>Type: integer</para>
80 <para>Value: <code>4</code></para>
81 <para>Represents the bronze key.</para>
82 <para>Used in: <code>add_key</code> as argument 1</para>
83 </sect3>
84
85 <sect3><title>KEY_SILVER</title>
86 <para>Type: integer</para>
87 <para>Value: <code>8</code></para>
88 <para>Represents the silver key.</para>
89 <para>Used in: <code>add_key</code> as argument 1</para>
90 </sect3>
91
92 <sect3><title>KEY_GOLD</title>
93 <para>Type: integer</para>
94 <para>Value: <code>16</code></para>
95 <para>Represents the gold key.</para>
96 <para>Used in: <code>add_key</code> as argument 1</para>
97 </sect3>
98
99 </sect2>
100
101 <sect2><title>Global Functions</title>
102 <para>These global functions access basic or generic methods of SuperTux. They are called without an object name.</para>
103
104 <sect3><title>display_text_file</title>
105 <para>Usage: <code>display_text_file(string filename)</code></para>
106 <para>Displays the SuperTux text file named <code>filename</code>. (The path is relative to the level file.)</para>
107 <para>See also: SuperTux file format reference, SuperTux texts</para>
108 </sect3>
109
110 <sect3><title>wait</title>
111 <para>Usage: <code>wait(float time)</code></para>
112 <para>Pauses execution of the squirrel code for <code>time</code> seconds.</para>
113 </sect3>
114
115 <sect3><title>translate</title>
116 <para>Usage: <code>translate(string text)</code></para>
117 <para>Returns: Translated string</para>
118 <para>Translates <code>text</code> into the user's locale.</para>
119 <para>Note: This construct is unfortunately not yet recognised by XGetText, so translation files have to be written manually.</para>
120 </sect3>
121
122 <sect3><title>import</title>
123 <para>Usage: <code>import(string filename)</code></para>
124 <para>Imports and runs the Squirrel script <code>filename</code>. (The path is relative to the level file.)</para>
125 </sect3>
126
127 <sect3><title>add_key</title>
128 <para>Usage: <code>add_key(int key)</code></para>
129 <para>Adds or removes a key from the player's posession. <code>key</code> should be replaced with one of the <code>KEY_</code> constants.</para>
130 <para>See also: src/player_status.hpp</para>
131 </sect3>
132
133 </sect2>
134
135 <sect2><title>DisplayEffect</title>
136 <para><code>DisplayEffect</code> is an interface for toying with the display.</para>
137
138 <sect3><title>fade_out</title>
139 <para>Usage: <code>DisplayEffect.fade_out(float fadetime)</code></para>
140 <para>Gradually fades out the screen to black for the next <code>fadetime</code> seconds.</para>
141 </sect3>
142
143 <sect3><title>fade_in</title>
144 <para>Usage: <code>DisplayEffect.fade_in(float fadetime)</code></para>
145 <para>Gradually fades in the screen from black for the next <code>fadetime</code> seconds.</para>
146 </sect3>
147
148 <sect3><title>set_black</title>
149 <para>Usage: <code>DisplayEffect.set_black(bool black)</code></para>
150 <para>Blackens or un-blackens the screen (depending on the value of <code>black</code>).</para>
151 </sect3>
152
153 <sect3><title>is_black</title>
154 <para>Usage: <code>DisplayEffect.is_black()</code></para>
155 <para>Returns: <code>bool</code></para>
156 <para>Returns true if the screen has been blackened by <code>set_black</code>. Calling <code>fade_in</code> or <code>fade_out</code> resets the return value to <code>false</code>.</para>
157 </sect3>
158
159 <sect3><title>sixteen_to_nine</title>
160 <para>Usage: <code>DisplayEffect.sixteen_to_nine()</code></para>
161 <para>Sets the display ratio to 16:9, effectively adding black bars at the top and bottom of the screen. Should be used before cutscenes.</para>
162 </sect3>
163
164 <sect3><title>four_to_three</title>
165 <para>Usage: <code>DisplayEffect.four_to_three()</code></para>
166 <para>Sets the display ratio to 4:3, removing the black bars added by <code>sixteen_to_nine()</code>. Should be used after cutscenes.</para>
167 </sect3>
168
169 </sect2>
170
171 <sect2><title>Camera</title>
172 <para><code>Camera</code> is an interface to manipulate the camera.</para>
173
174 <sect3><title>shake (NYI)</title>
175 <para>Usage: <code>Camera.shake(float time, float x, float y)</code></para>
176 <para>Warning: This function has not yet been implemented.</para>
177 </sect3>
178
179 <sect3><title>set_pos (NYI)</title>
180 <para>Usage: <code>Camera.set_pos(float x, float y)</code></para>
181 <para>Warning: This function has not yet been implemented.</para>
182 </sect3>
183
184 <sect3><title>set_mode (NYI)</title>
185 <para>Usage: <code>Camera.set_mode(string modestring)</code></para>
186 <para>Warning: This function has not yet been implemented.</para>
187 </sect3>
188
189 </sect2>
190
191 <sect2><title>Level</title>
192 <para>The <code>Level</code> class provides basic controlling functions for the current level.</para>
193
194 <sect3><title>finish</title>
195 <para>Usage: <code>Level.finish()</code></para>
196 <para>Ends the current level and marks it as completed if launched from a worldmap.</para>
197 <para>Tip: Very useful if you have created a frustrating level and want to, at some point, save the player from agony.</para>
198 </sect3>
199
200 <sect3><title>spawn</title>
201 <para>Usage: <code>Level.spawn(string sectorname, string spawnpointname)</code></para>
202 <para>Respawns Tux in the sector <code>sectorname</code> at the <code>spawnpointname</code> spawnpoint.</para>
203 <para>Exceptions: If <code>sectorname</code> or <code>spawnpointname</code> are empty or the specified sector does not exist, the function will bail out first chance it gets. If the specified spawnpoint doesn't exist, Tux will be spawned at the spawnpoint named <code>main</code>. If this spawnpoint doesn't exist either, Tux will simply end up at the origin (top-left 0, 0).</para>
204 </sect3>
205
206 <sect3><title>flip_vertically</title>
207 <para>Usage: <code>Level.flip_vertically()</code></para>
208 <para>Flips the level vertically (i.e. top is now bottom and vice versa). Call again to revert the effect.</para>
209 <para>Tip: Make sure the player can land on something after the level is flipped!</para>
210 </sect3>
211
212 </sect2>
213
214 <sect2><title>ScriptedObject</title>
215 <para>A <code>ScriptedObject</code> is basically a SuperTux object that can be scripted to move around and animate. This object will be used in the SuperTux cutscenes a whole lot.</para>
216
217 <sect3><title>Usage notes</title>
218 <para>Since a <code>ScriptedObject</code> is a reference object and not a statically declared object, you will have to build it into your level file, where a <code>scriptedobject</code> is a child of <code>sector</code>. This is an example definition:
219 <programlisting>
220 (scripted-object
221   (name "WOOT")
222   (x 420)
223   (y 94)
224   (sprite "snowball")
225   (solid #t)
226   (physic-enabled #f)
227   (visible #t)
228 )
229 </programlisting>
230 </para>
231 <para>Now, the object can be accessed in code using the <code>WOOT</code> identifier like so:
232 <programlisting>
233 WOOT.set_animation("left");
234 </programlisting>
235 </para>
236 </sect3>
237
238 <sect3><title>set_animation</title>
239 <para>Usage: <code>&lt;scriptedobject&gt;.set_animation(string animation_name)</code></para>
240 <para>Activates the sprite's animation specified in <code>animation_name</code>.</para>
241 </sect3>
242
243 <sect3><title>get_animation</title>
244 <para>Usage: <code>&lt;scriptedobject&gt;.get_animation()</code></para>
245 <para>Returns: <code>string</code></para>
246 <para>Returns the name of the sprite's current animation.</para>
247 </sect3>
248
249 <sect3><title>move</title>
250 <para>Usage: <code>&lt;scriptedobject&gt;.move(float x, float y)</code></para>
251 <para>Moves the object by <code>x</code> units to the right and <code>y</code> down <emphasis>relative to its current position</emphasis>.</para>
252 </sect3>
253
254 <sect3><title>set_pos</title>
255 <para>Usage: <code>&lt;scriptedobject&gt;.set_pos(float x, float y)</code></para>
256 <para>Basically identical to <code>move</code>, except its <emphasis>relativity to the sector origin</emphasis>.</para>
257 </sect3>
258
259 <sect3><title>get_pos_x</title>
260 <para>Usage: <code>&lt;scriptedobject&gt;.get_pos_x()</code></para>
261 <para>Returns: <code>float</code></para>
262 <para>Returns the X coordinate of the object's position.</para>
263 </sect3>
264
265 <sect3><title>get_pos_y</title>
266 <para>Usage: <code>&lt;scriptedobject&gt;.get_pos_y()</code></para>
267 <para>Returns: <code>float</code></para>
268 <para>Totally identical to <code>get_pos_x</code> except for its obvious choice of the other (Y) axis.</para>
269 </sect3>
270
271 <sect3><title>set_velocity</title>
272 <para>Usage: <code>&lt;scriptedobject&gt;.set_velocity(float x, float y)</code></para>
273 <para>Makes the object move in a certain direction (with a certain speed) given by the <code>x</code> and <code>y</code> coordinates.</para>
274 </sect3>
275
276 <sect3><title>get_velocity_x</title>
277 <para>Usage: <code>&lt;scriptedobject&gt;.get_velocity_x()</code></para>
278 <para>Returns: <code>float</code></para>
279 <para>Returns the object's velocity in the direction of the X coordinate.</para>
280 </sect3>
281
282 <sect3><title>get_velocity_y</title>
283 <para>Usage: <code>&lt;scriptedobject&gt;.get_velocity_y()</code></para>
284 <para>Returns: <code>float</code></para>
285 <para>The difference between this function and <code>get_velocity_x</code> is just the same like between <code>get_pos_y</code> and <code>get_pos_x</code>: same thing, different coordinates.</para>
286 </sect3>
287
288 <sect3><title>set_visible</title>
289 <para>Usage: <code>&lt;scriptedobject&gt;.set_visible(bool visible)</code></para>
290 <para>Shows or hides the object according to the value of <code>visible</code>.</para>
291 </sect3>
292
293 <sect3><title>is_visible</title>
294 <para>Usage: <code>&lt;scriptedobject&gt;.is_visible()</code></para>
295 <para>Returns: <code>bool</code></para>
296 <para>Returns <code>true</code> if the object is visible. (You've seen this coming, haven't you?)</para>
297 </sect3>
298
299 <sect3><title>get_name</title>
300 <para>Usage: <code>&lt;scriptedobject&gt;.get_name()</code></para>
301 <para>Returns: <code>string</code></para>
302 <para>Simply gives you the name of the scripted object (as if you didn't have it already...)</para>
303 </sect3>
304
305 </sect2>
306
307 <sect2><title>Sound</title>
308 <para>This class provides a very simple interface to the audio subsystem.</para>
309
310 <sect3><title>play_music</title>
311 <para>Usage: <code>Sound.play_music(string track_name)</code></para>
312 <para>Plays the selected music track (automatically prepending the path to the music folder and appending the <code>.ogg</code> extension).</para>
313 </sect3>
314
315 <sect3><title>play_sound</title>
316 <para>Usage: <code>Sound.play_sound(string sound_name)</code></para>
317 <para>Plays the sound specified in <code>sound_name</code> (that is identical to the filename of the sound without the <code>.wav</code> extension).</para>
318 </sect3>
319
320 </sect2>
321
322 <sect2><title>Text</title>
323 <para>This module provides access to methods reponsible for displaying text on-screen.</para>
324
325 <sect3><title>set_text</title>
326 <para>Usage: <code>Text.set_text(string text)</code></para>
327 <para>Sets the text string to be displayed to <code>text</code>.</para>
328 </sect3>
329
330 <sect3><title>set_font</title>
331 <para>Usage: <code>Text.set_font(string font)</code></para>
332 <para>Sets the font of the text to be displayed to <code>text</code>. Currently valid values are <code>gold</code>, <code>white</code>, <code>blue</code>, <code>gray</code>, <code>big</code> and <code>small</code>.</para>
333 </sect3>
334
335 <sect3><title>fade_in</title>
336 <para>Usage: <code>Text.fade_in(float time)</code></para>
337 <para>Fades in the specified text for the next <code>time</code> seconds.</para>
338 </sect3>
339
340 <sect3><title>fade_out</title>
341 <para>Usage: <code>Text.fade_out(float time)</code></para>
342 <para>Just the opposite of <code>fade_out</code>.</para>
343 </sect3>
344
345 <sect3><title>set_visible</title>
346 <para>Usage: <code>Text.set_visible(bool visible)</code></para>
347 <para>Shows or hides the text abruptly (drastic counterpart to <code>fade_in</code> and <code>fade_out</code>).</para>
348 </sect3>
349
350 <sect3><title>set_centered</title>
351 <para>Usage: <code>Text.set_centered(bool centered)</code></para>
352 <para>If <code>centered</code> is <code>true</code>, the text will be centered on the screen. Otherwise, it will be left-aligned.</para>
353 </sect3>
354
355 </sect2>
356
357 <sect2><title>Player</title>
358 <para>This module contains methods controlling the player. (No, SuperTux doesn't use mind control. Player refers to the player object.)</para>
359
360 <sect3><title>make_invincible</title>
361 <para>Usage: <code>Player.make_invincible()</code></para>
362 <para>Makes the player invincible for a predefined amount of time.</para>
363 <para>See also: <code>TUX_INVINCIBLE_TIME</code> in src/object/player.hpp for the amount of seconds that the player becomes invincible.</para>
364 </sect3>
365
366 <sect3><title>deactivate</title>
367 <para>Usage: <code>Player.deactivate()</code></para>
368 <para>Stops the player and blocks the movement controls.</para>
369 <para>Tip: Don't call this in front of a horde of badguys.</para>
370 </sect3>
371
372 <sect3><title>activate</title>
373 <para>Usage: <code>Player.activate()</code></para>
374 <para>Reactivates the player's movement controls.</para>
375 </sect3>
376
377 <sect3><title>walk</title>
378 <para>Usage: <code>Player.walk(float velocity)</code></para>
379 <para>Makes the player move in a certain horizontal velocity (specified by <code>velocity</code>). A negative velocity moves the player to the left.</para>
380 </sect3>
381
382 </sect2>
383
384 </sect1>
385
386 </article>