adbda0da2bd2ee927b9dd54fc5f3d26353361997
[rocksndiamonds.git] / src / screens.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  (c) 1995-98 Artsoft Entertainment                       *
5 *              Holger Schemel                              *
6 *              Oststrasse 11a                              *
7 *              33604 Bielefeld                             *
8 *              phone: ++49 +521 290471                     *
9 *              email: aeglos@valinor.owl.de                *
10 *----------------------------------------------------------*
11 *  screens.c                                               *
12 ***********************************************************/
13
14 #include "screens.h"
15 #include "events.h"
16 #include "sound.h"
17 #include "game.h"
18 #include "tools.h"
19 #include "editor.h"
20 #include "misc.h"
21 #include "files.h"
22 #include "buttons.h"
23 #include "tape.h"
24 #include "joystick.h"
25 #include "cartoons.h"
26 #include "network.h"
27 #include "init.h"
28
29 /* for DrawSetupScreen(), HandleSetupScreen() */
30 #define SETUP_SCREEN_POS_START          2
31 #define SETUP_SCREEN_POS_END            16
32 #define SETUP_SCREEN_POS_EMPTY1         (SETUP_SCREEN_POS_END - 2)
33 #define SETUP_SCREEN_POS_EMPTY2         (SETUP_SCREEN_POS_END - 2)
34
35 /* for HandleSetupInputScreen() */
36 #define SETUPINPUT_SCREEN_POS_START     2
37 #define SETUPINPUT_SCREEN_POS_END       15
38 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
39 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
40
41 /* for HandleChooseLevel() */
42 #define MAX_LEVEL_SERIES_ON_SCREEN      15
43
44 #ifdef MSDOS
45 extern unsigned char get_ascii(KeySym);
46 #endif
47
48 void DrawHeadline()
49 {
50   int x = SX + (SXSIZE - strlen(GAMETITLE_STRING) * FONT1_XSIZE) / 2;
51
52   DrawText(x, SY + 8, GAMETITLE_STRING, FS_BIG, FC_YELLOW);
53   DrawTextFCentered(46, FC_RED, COPYRIGHT_STRING);
54 }
55
56 void DrawMainMenu()
57 {
58   int i;
59   char *name_text = (!options.network && setup.team_mode ? "Team:" : "Name:");
60
61   FadeSounds();
62   GetPlayerConfig();
63   LoadLevel(level_nr);
64
65   ClearWindow();
66   DrawHeadline();
67   DrawText(SX + 32,    SY + 2*32, name_text, FS_BIG, FC_GREEN);
68   DrawText(SX + 6*32,  SY + 2*32, setup.player_name, FS_BIG, FC_RED);
69   DrawText(SX + 32,    SY + 3*32, "Level:", FS_BIG, FC_GREEN);
70   DrawText(SX + 11*32, SY + 3*32, int2str(level_nr,3), FS_BIG,
71            (leveldir[leveldir_nr].readonly ? FC_RED : FC_YELLOW));
72   DrawText(SX + 32,    SY + 4*32, "Hall Of Fame", FS_BIG, FC_GREEN);
73   DrawText(SX + 32,    SY + 5*32, "Level Creator", FS_BIG, FC_GREEN);
74   DrawText(SY + 32,    SY + 6*32, "Info Screen", FS_BIG, FC_GREEN);
75   DrawText(SX + 32,    SY + 7*32, "Start Game", FS_BIG, FC_GREEN);
76   DrawText(SX + 32,    SY + 8*32, "Setup", FS_BIG, FC_GREEN);
77   DrawText(SX + 32,    SY + 9*32, "Quit", FS_BIG, FC_GREEN);
78
79   DrawMicroLevel(MICROLEV_XPOS,MICROLEV_YPOS);
80
81   for(i=2; i<10; i++)
82     DrawGraphic(0, i, GFX_KUGEL_BLAU);
83   DrawGraphic(10, 3, GFX_PFEIL_L);
84   DrawGraphic(14, 3, GFX_PFEIL_R);
85
86   DrawTextF(15*32 + 6, 3*32 + 9, FC_RED, "%d", leveldir[leveldir_nr].levels);
87
88   DrawText(SX + 56, SY + 326, "A Game by Artsoft Entertainment",
89            FS_SMALL, FC_RED);
90
91   if (leveldir[leveldir_nr].name)
92   {
93     int len = strlen(leveldir[leveldir_nr].name);
94     int lxpos = SX + (SXSIZE - len * FONT4_XSIZE) / 2;
95     int lypos = SY + 352;
96
97     DrawText(lxpos, lypos, leveldir[leveldir_nr].name, FS_SMALL, FC_SPECIAL2);
98   }
99
100   FadeToFront();
101   InitAnimation();
102   HandleMainMenu(0,0, 0,0, MB_MENU_INITIALIZE);
103
104   TapeStop();
105   if (TAPE_IS_EMPTY(tape))
106     LoadTape(level_nr);
107   DrawCompleteVideoDisplay();
108
109   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
110
111   ClearEventQueue();
112   XAutoRepeatOn(display);
113 }
114
115 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
116 {
117   static int choice = 3;
118   static int redraw = TRUE;
119   int x = (mx + 32 - SX) / 32, y = (my + 32 - SY) / 32;
120
121   if (redraw || button == MB_MENU_INITIALIZE)
122   {
123     DrawGraphic(0, choice - 1, GFX_KUGEL_ROT);
124     redraw = FALSE;
125   }
126
127   if (button == MB_MENU_INITIALIZE)
128     return;
129
130   if (dx || dy)
131   {
132     if (dx && choice == 4)
133     {
134       x = (dx < 0 ? 11 : 15);
135       y = 4;
136     }
137     else if (dy)
138     {
139       x = 1;
140       y = choice + dy;
141     }
142     else
143       x = y = 0;
144
145     if (y < 3)
146       y = 3;
147     else if (y > 10)
148       y = 10;
149   }
150
151   if (!mx && !my && !dx && !dy)
152   {
153     x = 1;
154     y = choice;
155   }
156
157   if (y == 4 && ((x == 11 && level_nr > 0) ||
158                  (x == 15 && level_nr < leveldir[leveldir_nr].levels - 1)) &&
159       button)
160   {
161     static unsigned long level_delay = 0;
162     int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
163     int new_level_nr, old_level_nr = level_nr;
164     int font_color = (leveldir[leveldir_nr].readonly ? FC_RED : FC_YELLOW);
165
166     new_level_nr = level_nr + (x == 11 ? -step : +step);
167     if (new_level_nr < 0)
168       new_level_nr = 0;
169     if (new_level_nr > leveldir[leveldir_nr].levels - 1)
170       new_level_nr = leveldir[leveldir_nr].levels - 1;
171
172     if (old_level_nr == new_level_nr || !DelayReached(&level_delay, 150))
173       goto out;
174
175     level_nr = new_level_nr;
176
177     DrawTextExt(drawto, gc, SX + 11 * 32, SY + 3 * 32,
178                 int2str(level_nr, 3), FS_BIG, font_color);
179     DrawTextExt(window, gc, SX + 11 * 32, SY + 3 * 32,
180                 int2str(level_nr, 3), FS_BIG, font_color);
181
182     LoadLevel(level_nr);
183     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS);
184
185     TapeErase();
186     LoadTape(level_nr);
187     DrawCompleteVideoDisplay();
188
189     /* needed because DrawMicroLevel() takes some time */
190     BackToFront();
191     XSync(display, FALSE);
192     DelayReached(&level_delay, 0);      /* reset delay counter */
193   }
194   else if (x == 1 && y >= 3 && y <= 10)
195   {
196     if (button)
197     {
198       if (y != choice)
199       {
200         DrawGraphic(0, y-1, GFX_KUGEL_ROT);
201         DrawGraphic(0, choice - 1, GFX_KUGEL_BLAU);
202         choice = y;
203       }
204     }
205     else
206     {
207       if (y == 3)
208       {
209         game_status = TYPENAME;
210         HandleTypeName(strlen(setup.player_name), 0);
211       }
212       else if (y == 4)
213       {
214         if (num_leveldirs)
215         {
216           game_status = CHOOSELEVEL;
217           SaveLevelSetup();
218           DrawChooseLevel();
219         }
220       }
221       else if (y == 5)
222       {
223         game_status = HALLOFFAME;
224         DrawHallOfFame(-1);
225       }
226       else if (y == 6)
227       {
228         if (leveldir[leveldir_nr].readonly)
229           Request("This level is read only !", REQ_CONFIRM);
230         game_status = LEVELED;
231         DrawLevelEd();
232       }
233       else if (y == 7)
234       {
235         game_status = HELPSCREEN;
236         DrawHelpScreen();
237       }
238       else if (y == 8)
239       {
240         if (setup.autorecord)
241           TapeStartRecording();
242
243 #ifndef MSDOS
244         if (options.network)
245           SendToServer_StartPlaying();
246         else
247 #endif
248         {
249           game_status = PLAYING;
250           InitGame();
251         }
252       }
253       else if (y == 9)
254       {
255         game_status = SETUP;
256         DrawSetupScreen();
257       }
258       else if (y == 10)
259       {
260         SaveLevelSetup();
261         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
262           game_status = EXITGAME;
263       }
264
265       redraw = TRUE;
266     }
267   }
268   BackToFront();
269
270   out:
271
272   if (game_status == MAINMENU)
273     DoAnimation();
274 }
275
276 #define MAX_HELPSCREEN_ELS      10
277 #define HA_NEXT                 -999
278 #define HA_END                  -1000
279
280 static long helpscreen_state;
281 static int helpscreen_step[MAX_HELPSCREEN_ELS];
282 static int helpscreen_frame[MAX_HELPSCREEN_ELS];
283 static int helpscreen_delay[MAX_HELPSCREEN_ELS];
284 static int helpscreen_action[] =
285 {
286   GFX_SPIELER1_DOWN,4,2,
287   GFX_SPIELER1_UP,4,2,
288   GFX_SPIELER1_LEFT,4,2,
289   GFX_SPIELER1_RIGHT,4,2,
290   GFX_SPIELER1_PUSH_LEFT,4,2,
291   GFX_SPIELER1_PUSH_RIGHT,4,2,                                  HA_NEXT,
292   GFX_ERDREICH,1,100,                                           HA_NEXT,
293   GFX_LEERRAUM,1,100,                                           HA_NEXT,
294   GFX_MORAST_LEER,1,100,                                        HA_NEXT,
295   GFX_BETON,1,100,                                              HA_NEXT,
296   GFX_MAUERWERK,1,100,                                          HA_NEXT,
297   GFX_MAUER_R1,3,4, GFX_MAUERWERK,1,20, GFX_LEERRAUM,1,10,
298   GFX_MAUER_L1,3,4, GFX_MAUERWERK,1,20, GFX_LEERRAUM,1,10,      HA_NEXT,
299   GFX_UNSICHTBAR,1,100,                                         HA_NEXT,
300   GFX_FELSBODEN,1,100,                                          HA_NEXT,
301   GFX_CHAR_A,30,4, GFX_CHAR_AUSRUF,32,4,                        HA_NEXT,
302   GFX_EDELSTEIN,2,5,                                            HA_NEXT,
303   GFX_DIAMANT,2,5,                                              HA_NEXT,
304   GFX_EDELSTEIN_BD,2,5,                                         HA_NEXT,
305   GFX_EDELSTEIN_GELB,2,5, GFX_EDELSTEIN_ROT,2,5,
306   GFX_EDELSTEIN_LILA,2,5,                                       HA_NEXT,
307   GFX_FELSBROCKEN,4,5,                                          HA_NEXT,
308   GFX_BOMBE,1,50, GFX_EXPLOSION,8,1, GFX_LEERRAUM,1,10,         HA_NEXT,
309   GFX_KOKOSNUSS,1,50, GFX_CRACKINGNUT,3,1, GFX_EDELSTEIN,1,10,  HA_NEXT,
310   GFX_ERZ_EDEL,1,50, GFX_EXPLOSION,8,1, GFX_EDELSTEIN,1,10,     HA_NEXT,
311   GFX_ERZ_DIAM,1,50, GFX_EXPLOSION,8,1, GFX_DIAMANT,1,10,       HA_NEXT,
312   GFX_ERZ_EDEL_BD,1,50, GFX_EXPLOSION,8,1,GFX_EDELSTEIN_BD,1,10,HA_NEXT,
313   GFX_ERZ_EDEL_GELB,1,50, GFX_EXPLOSION,8,1,
314   GFX_EDELSTEIN_GELB,1,10, GFX_ERZ_EDEL_ROT,1,50,
315   GFX_EXPLOSION,8,1, GFX_EDELSTEIN_ROT,1,10,
316   GFX_ERZ_EDEL_LILA,1,50, GFX_EXPLOSION,8,1,
317   GFX_EDELSTEIN_LILA,1,10,                                      HA_NEXT,
318   GFX_GEBLUBBER,4,4,                                            HA_NEXT,
319   GFX_SCHLUESSEL1,4,25,                                         HA_NEXT,
320   GFX_PFORTE1,4,25,                                             HA_NEXT,
321   GFX_PFORTE1X,4,25,                                            HA_NEXT,
322   GFX_DYNAMIT_AUS,1,100,                                        HA_NEXT,
323   GFX_DYNAMIT,7,6, GFX_EXPLOSION,8,1, GFX_LEERRAUM,1,10,        HA_NEXT,
324   GFX_DYNABOMB+0,4,3, GFX_DYNABOMB+3,1,3, GFX_DYNABOMB+2,1,3,
325   GFX_DYNABOMB+1,1,3, GFX_DYNABOMB+0,1,3, GFX_EXPLOSION,8,1,
326   GFX_LEERRAUM,1,10,                                            HA_NEXT,
327   GFX_DYNABOMB_NR,1,100,                                        HA_NEXT,
328   GFX_DYNABOMB_SZ,1,100,                                        HA_NEXT,
329   GFX_FLIEGER+4,1,3, GFX_FLIEGER+0,1,3, GFX_FLIEGER+4,1,3,
330   GFX_FLIEGER+5,1,3, GFX_FLIEGER+1,1,3, GFX_FLIEGER+5,1,3,
331   GFX_FLIEGER+6,1,3, GFX_FLIEGER+2,1,3, GFX_FLIEGER+6,1,3,
332   GFX_FLIEGER+7,1,3, GFX_FLIEGER+3,1,3, GFX_FLIEGER+7,1,3,      HA_NEXT,
333   GFX_KAEFER+4,1,1, GFX_KAEFER+0,1,1, GFX_KAEFER+4,1,1,
334   GFX_KAEFER+5,1,1, GFX_KAEFER+1,1,1, GFX_KAEFER+5,1,1,
335   GFX_KAEFER+6,1,1, GFX_KAEFER+2,1,1, GFX_KAEFER+6,1,1,
336   GFX_KAEFER+7,1,1, GFX_KAEFER+3,1,1, GFX_KAEFER+7,1,1,         HA_NEXT,
337   GFX_BUTTERFLY,2,2,                                            HA_NEXT,
338   GFX_FIREFLY,2,2,                                              HA_NEXT,
339   GFX_PACMAN+0,1,3, GFX_PACMAN+4,1,2, GFX_PACMAN+0,1,3,
340   GFX_PACMAN+1,1,3, GFX_PACMAN+5,1,2, GFX_PACMAN+1,1,3,
341   GFX_PACMAN+2,1,3, GFX_PACMAN+6,1,2, GFX_PACMAN+2,1,3,
342   GFX_PACMAN+3,1,3, GFX_PACMAN+7,1,2, GFX_PACMAN+3,1,3,         HA_NEXT,
343   GFX_MAMPFER+0,4,1, GFX_MAMPFER+3,1,1, GFX_MAMPFER+2,1,1,
344   GFX_MAMPFER+1,1,1, GFX_MAMPFER+0,1,1,                         HA_NEXT,
345   GFX_MAMPFER2+0,4,1, GFX_MAMPFER2+3,1,1, GFX_MAMPFER2+2,1,1,
346   GFX_MAMPFER2+1,1,1, GFX_MAMPFER2+0,1,1,                       HA_NEXT,
347   GFX_ROBOT+0,4,1, GFX_ROBOT+3,1,1, GFX_ROBOT+2,1,1,
348   GFX_ROBOT+1,1,1, GFX_ROBOT+0,1,1,                             HA_NEXT,
349   GFX_MAULWURF_DOWN,4,2,
350   GFX_MAULWURF_UP,4,2,
351   GFX_MAULWURF_LEFT,4,2,
352   GFX_MAULWURF_RIGHT,4,2,                                       HA_NEXT,
353   GFX_PINGUIN_DOWN,4,2,
354   GFX_PINGUIN_UP,4,2,
355   GFX_PINGUIN_LEFT,4,2,
356   GFX_PINGUIN_RIGHT,4,2,                                        HA_NEXT,
357   GFX_SCHWEIN_DOWN,4,2,
358   GFX_SCHWEIN_UP,4,2,
359   GFX_SCHWEIN_LEFT,4,2,
360   GFX_SCHWEIN_RIGHT,4,2,                                        HA_NEXT,
361   GFX_DRACHE_DOWN,4,2,
362   GFX_DRACHE_UP,4,2,
363   GFX_DRACHE_LEFT,4,2,
364   GFX_DRACHE_RIGHT,4,2,                                         HA_NEXT,
365   GFX_SONDE_START,8,1,                                          HA_NEXT,
366   GFX_ABLENK,4,1,                                               HA_NEXT,
367   GFX_BIRNE_AUS,1,25, GFX_BIRNE_EIN,1,25,                       HA_NEXT,
368   GFX_ZEIT_VOLL,1,25, GFX_ZEIT_LEER,1,25,                       HA_NEXT,
369   GFX_TROPFEN,1,25, GFX_AMOEBING,4,1, GFX_AMOEBE_LEBT,1,10,     HA_NEXT,
370   GFX_AMOEBE_TOT+2,2,50, GFX_AMOEBE_TOT,2,50,                   HA_NEXT,
371   GFX_AMOEBE_LEBT,4,40,                                         HA_NEXT,
372   GFX_AMOEBE_LEBT,1,10, GFX_AMOEBING,4,2,                       HA_NEXT,
373   GFX_AMOEBE_LEBT,1,25, GFX_AMOEBE_TOT,1,25, GFX_EXPLOSION,8,1,
374   GFX_DIAMANT,1,10,                                             HA_NEXT,
375   GFX_LIFE,1,100,                                               HA_NEXT,
376   GFX_LIFE_ASYNC,1,100,                                         HA_NEXT,
377   GFX_SIEB_LEER,4,2,                                            HA_NEXT,
378   GFX_SIEB2_LEER,4,2,                                           HA_NEXT,
379   GFX_AUSGANG_ZU,1,100, GFX_AUSGANG_ACT,4,2,
380   GFX_AUSGANG_AUF+0,4,2, GFX_AUSGANG_AUF+3,1,2,
381   GFX_AUSGANG_AUF+2,1,2, GFX_AUSGANG_AUF+1,1,2,                 HA_NEXT,
382   GFX_AUSGANG_AUF+0,4,2, GFX_AUSGANG_AUF+3,1,2,
383   GFX_AUSGANG_AUF+2,1,2, GFX_AUSGANG_AUF+1,1,2,                 HA_NEXT,
384   HA_END
385 };
386 static char *helpscreen_eltext[][2] =
387 {
388  {"THE HERO:",                          "(Is _this_ guy good old Rockford?)"},
389  {"Normal sand:",                       "You can dig through it"},
390  {"Empty field:",                       "You can walk through it"},
391  {"Quicksand: You cannot pass it,",     "but rocks can fall though it"},
392  {"Massive Wall:",                      "Nothing can go through it"},
393  {"Normal Wall: You can't go through",  "it, but you can bomb it away"},
394  {"Growing Wall: Grows to the left or", "right if there is an empty field"},
395  {"Invisible Wall: Behaves like normal","wall, but is invisible"},
396  {"Old Wall: Like normal wall, but",    "some things can fall down from it"},
397  {"Letter Wall: Looks like a letter,",  "behaves like a normal wall"},
398  {"Emerald: You must collect enough of","them to finish a level"},
399  {"Diamond: Counts as 3 emeralds, but", "can be destroyed by rocks"},
400  {"Diamond (BD style): Counts like one","emerald and behaves a bit different"},
401  {"Colorful Gems:",                     "Seem to behave like Emeralds"},
402  {"Rock: Smashes several things;",      "Can be moved by the player"},
403  {"Bomb: You can move it, but be",      "careful when dropping it"},
404  {"Nut: Throw a rock on it to open it;","Each nut contains an emerald"},
405  {"Wall with an emerald inside:",       "Bomb the wall away to get it"},
406  {"Wall with a diamond inside:",        "Bomb the wall away to get it"},
407  {"Wall with BD style diamond inside:", "Bomb the wall away to get it"},
408  {"Wall with colorful gem inside:",     "Bomb the wall away to get it"},
409  {"Acid: Things that fall in are gone", "forever (including our hero)"},
410  {"Key: Opens the door that has the",   "same color (red/yellow/green/blue)"},
411  {"Door: Can be opened by the key",     "with the same color"},
412  {"Door: You have to find out the",     "right color of the key for it"},
413  {"Dynamite: Collect it and use it to", "destroy walls or kill enemies"},
414  {"Dynamite: This one explodes after",  "a few seconds"},
415  {"Dyna Bomb: Explodes in 4 directions","with variable explosion size"},
416  {"Dyna Bomb: Increases the number of", "dyna bombs available at a time"},
417  {"Dyna Bomb: Increases the size of",   "explosion of dyna bombs"},
418  {"Spaceship: Moves at the left side",  "of walls; don't touch it!"},
419  {"Bug: Moves at the right side",       "of walls; don't touch it!"},
420  {"Butterfly: Moves at the right side", "of walls; don't touch it!"},
421  {"Firefly: Moves at the left side",    "of walls; don't touch it!"},
422  {"Pacman: Eats the amoeba and you,",   "if you're not careful"},
423  {"Cruncher: Eats diamonds and you,",   "if you're not careful"},
424  {"Cruncher (BD style):",               "Eats almost everything"},
425  {"Robot: Tries to kill the player",    ""},
426  {"The mole: You must guide him savely","to the exit; he will follow you"},
427  {"The penguin: Guide him to the exit,","but keep him away from monsters!"},
428  {"The Pig: Harmless, but eats all",    "gems it can get"},
429  {"The Dragon: Breathes fire,",         "especially to some monsters"},
430  {"Sonde: Follows you everywhere;",     "harmless, but may block your way"},
431  {"Magic Wheel: Touch it to get rid of","the robots for some seconds"},
432  {"Light Bulb: All of them must be",    "switched on to finish a level"},
433  {"Extra Time Orb: Adds some seconds",  "to the time available for the level"},
434  {"Amoeba Drop: Grows to an amoeba on", "the ground - don't touch it"},
435  {"Dead Amoeba: Does not grow, but",    "can still kill bugs and spaceships"},
436  {"Normal Amoeba: Grows through empty", "fields, sand and quicksand"},
437  {"Dropping Amoeba: This one makes",    "drops that grow to a new amoeba"},
438  {"Living Amoeba (BD style): Contains", "other element, when surrounded"},
439  {"Game Of Life: Behaves like the well","known 'Game Of Life' (2333 style)"},
440  {"Biomaze: A bit like the 'Game Of",   "Life', but builds crazy mazes"},
441  {"Magic Wall: Changes rocks, emeralds","and diamonds when they pass it"},
442  {"Magic Wall (BD style):",             "Changes rocks and BD style diamonds"},
443  {"Exit door: Opens if you have enough","emeralds to finish the level"},
444  {"Open exit door: Enter here to leave","the level and exit the actual game"},
445 };
446 static int num_helpscreen_els = sizeof(helpscreen_eltext)/(2*sizeof(char *));
447
448 static char *helpscreen_music[][3] =
449 {
450   { "Alchemy",                  "Ian Boddy",            "Drive" },
451   { "The Chase",                "Propaganda",           "A Secret Wish" },
452   { "Network 23",               "Tangerine Dream",      "Exit" },
453   { "Czardasz",                 "Robert Pieculewicz",   "Czardasz" },
454   { "21st Century Common Man",  "Tangerine Dream",      "Tyger" },
455   { "Voyager",                  "The Alan Parsons Project","Pyramid" },
456   { "Twilight Painter",         "Tangerine Dream",      "Heartbreakers" }
457 };
458 static int helpscreen_musicpos;
459
460 void DrawHelpScreenElAction(int start)
461 {
462   int i = 0, j = 0;
463   int frame, graphic;
464   int xstart = SX+16, ystart = SY+64+2*32, ystep = TILEY+4;
465
466   while(helpscreen_action[j] != HA_END)
467   {
468     if (i>=start+MAX_HELPSCREEN_ELS || i>=num_helpscreen_els)
469       break;
470     else if (i<start || helpscreen_delay[i-start])
471     {
472       if (i>=start && helpscreen_delay[i-start])
473         helpscreen_delay[i-start]--;
474
475       while(helpscreen_action[j] != HA_NEXT)
476         j++;
477       j++;
478       i++;
479       continue;
480     }
481
482     j += 3*helpscreen_step[i-start];
483     graphic = helpscreen_action[j++];
484
485     if (helpscreen_frame[i-start])
486     {
487       frame = helpscreen_action[j++] - helpscreen_frame[i-start];
488       helpscreen_frame[i-start]--;
489     }
490     else
491     {
492       frame = 0;
493       helpscreen_frame[i-start] = helpscreen_action[j++]-1;
494     }
495
496     helpscreen_delay[i-start] = helpscreen_action[j++] - 1;
497
498     if (helpscreen_action[j] == HA_NEXT)
499     {
500       if (!helpscreen_frame[i-start])
501         helpscreen_step[i-start] = 0;
502     }
503     else
504     {
505       if (!helpscreen_frame[i-start])
506         helpscreen_step[i-start]++;
507       while(helpscreen_action[j] != HA_NEXT)
508         j++;
509     }
510     j++;
511
512     DrawGraphicExt(drawto, gc, xstart, ystart+(i-start)*ystep, graphic+frame);
513     i++;
514   }
515
516   for(i=2;i<16;i++)
517   {
518     MarkTileDirty(0,i);
519     MarkTileDirty(1,i);
520   }
521 }
522
523 void DrawHelpScreenElText(int start)
524 {
525   int i;
526   int xstart = SX + 56, ystart = SY + 65 + 2 * 32, ystep = TILEY + 4;
527   int ybottom = SYSIZE - 20;
528
529   ClearWindow();
530   DrawHeadline();
531
532   DrawTextFCentered(100, FC_GREEN, "The game elements:");
533
534   for(i=start; i < start + MAX_HELPSCREEN_ELS && i < num_helpscreen_els; i++)
535   {
536     DrawText(xstart,
537              ystart + (i - start) * ystep + (*helpscreen_eltext[i][1] ? 0 : 8),
538              helpscreen_eltext[i][0], FS_SMALL, FC_YELLOW);
539     DrawText(xstart, ystart + (i - start) * ystep + 16,
540              helpscreen_eltext[i][1], FS_SMALL, FC_YELLOW);
541   }
542
543   DrawTextFCentered(ybottom, FC_BLUE, "Press any key or button for next page");
544 }
545
546 void DrawHelpScreenMusicText(int num)
547 {
548   int ystart = 150, ystep = 30;
549   int ybottom = SYSIZE - 20;
550
551   FadeSounds();
552   ClearWindow();
553   DrawHeadline();
554
555   DrawTextFCentered(100, FC_GREEN, "The game background music loops:");
556
557   DrawTextFCentered(ystart + 0 * ystep, FC_YELLOW,
558                     "Excerpt from");
559   DrawTextFCentered(ystart + 1 * ystep, FC_RED, "\"%s\"",
560                     helpscreen_music[num][0]);
561   DrawTextFCentered(ystart + 2 * ystep, FC_YELLOW,
562                     "by");
563   DrawTextFCentered(ystart + 3 * ystep, FC_RED,
564                     "%s", helpscreen_music[num][1]);
565   DrawTextFCentered(ystart + 4 * ystep, FC_YELLOW,
566                     "from the album");
567   DrawTextFCentered(ystart + 5 * ystep, FC_RED, "\"%s\"",
568                     helpscreen_music[num][2]);
569
570   DrawTextFCentered(ybottom, FC_BLUE, "Press any key or button for next page");
571
572   PlaySoundLoop(background_loop[num]);
573 }
574
575 void DrawHelpScreenCreditsText()
576 {
577   int ystart = 150, ystep = 30;
578   int ybottom = SYSIZE - 20;
579
580   FadeSounds();
581   ClearWindow();
582   DrawHeadline();
583
584   DrawTextFCentered(100, FC_GREEN,
585                     "Credits:");
586   DrawTextFCentered(ystart + 0 * ystep, FC_YELLOW,
587                     "DOS/Windows port of the game:");
588   DrawTextFCentered(ystart + 1 * ystep, FC_RED,
589                     "Guido Schulz");
590   DrawTextFCentered(ystart + 2 * ystep, FC_YELLOW,
591                     "Additional toons:");
592   DrawTextFCentered(ystart + 3 * ystep, FC_RED,
593                     "Karl Hörnell");
594   DrawTextFCentered(ystart + 5 * ystep, FC_YELLOW,
595                     "...and many thanks to all contributors");
596   DrawTextFCentered(ystart + 6 * ystep, FC_YELLOW,
597                     "of new levels!");
598
599   DrawTextFCentered(ybottom, FC_BLUE, "Press any key or button for next page");
600 }
601
602 void DrawHelpScreenContactText()
603 {
604   int ystart = 150, ystep = 30;
605   int ybottom = SYSIZE - 20;
606
607   ClearWindow();
608   DrawHeadline();
609
610   DrawTextFCentered(100, FC_GREEN, "Program information:");
611
612   DrawTextFCentered(ystart + 0 * ystep, FC_YELLOW,
613                     "This game is Freeware!");
614   DrawTextFCentered(ystart + 1 * ystep, FC_YELLOW,
615                     "If you like it, send e-mail to:");
616   DrawTextFCentered(ystart + 2 * ystep, FC_RED,
617                     "aeglos@valinor.owl.de");
618   DrawTextFCentered(ystart + 3 * ystep, FC_YELLOW,
619                     "or SnailMail to:");
620   DrawTextFCentered(ystart + 4 * ystep + 0, FC_RED,
621                     "Holger Schemel");
622   DrawTextFCentered(ystart + 4 * ystep + 20, FC_RED,
623                     "Oststrasse 11a");
624   DrawTextFCentered(ystart + 4 * ystep + 40, FC_RED,
625                     "33604 Bielefeld");
626   DrawTextFCentered(ystart + 4 * ystep + 60, FC_RED,
627                     "Germany");
628
629   DrawTextFCentered(ystart + 7 * ystep, FC_YELLOW,
630                     "If you have created new levels,");
631   DrawTextFCentered(ystart + 8 * ystep, FC_YELLOW,
632                     "send them to me to include them!");
633   DrawTextFCentered(ystart + 9 * ystep, FC_YELLOW,
634                     ":-)");
635
636   DrawTextFCentered(ybottom, FC_BLUE, "Press any key or button for main menu");
637 }
638
639 void DrawHelpScreen()
640 {
641   int i;
642
643   CloseDoor(DOOR_CLOSE_2);
644
645   for(i=0;i<MAX_HELPSCREEN_ELS;i++)
646     helpscreen_step[i] = helpscreen_frame[i] = helpscreen_delay[i] = 0;
647   helpscreen_musicpos = 0;
648   helpscreen_state = 0;
649   DrawHelpScreenElText(0);
650   DrawHelpScreenElAction(0);
651
652   FadeToFront();
653   InitAnimation();
654   PlaySoundLoop(SND_RHYTHMLOOP);
655 }
656
657 void HandleHelpScreen(int button)
658 {
659   static unsigned long hs_delay = 0;
660   int num_helpscreen_els_pages =
661     (num_helpscreen_els + MAX_HELPSCREEN_ELS-1) / MAX_HELPSCREEN_ELS;
662   int button_released = !button;
663   int i;
664
665   if (button_released)
666   {
667     if (helpscreen_state < num_helpscreen_els_pages - 1)
668     {
669       for(i=0;i<MAX_HELPSCREEN_ELS;i++)
670         helpscreen_step[i] = helpscreen_frame[i] = helpscreen_delay[i] = 0;
671       helpscreen_state++;
672       DrawHelpScreenElText(helpscreen_state*MAX_HELPSCREEN_ELS);
673       DrawHelpScreenElAction(helpscreen_state*MAX_HELPSCREEN_ELS);
674     }
675     else if (helpscreen_state < num_helpscreen_els_pages + num_bg_loops - 1)
676     {
677       helpscreen_state++;
678       DrawHelpScreenMusicText(helpscreen_state - num_helpscreen_els_pages);
679     }
680     else if (helpscreen_state == num_helpscreen_els_pages + num_bg_loops - 1)
681     {
682       helpscreen_state++;
683       DrawHelpScreenCreditsText();
684     }
685     else if (helpscreen_state == num_helpscreen_els_pages + num_bg_loops)
686     {
687       helpscreen_state++;
688       DrawHelpScreenContactText();
689     }
690     else
691     {
692       FadeSounds();
693       DrawMainMenu();
694       game_status = MAINMENU;
695     }
696   }
697   else
698   {
699     if (DelayReached(&hs_delay,GAME_FRAME_DELAY * 2))
700     {
701       if (helpscreen_state<num_helpscreen_els_pages)
702         DrawHelpScreenElAction(helpscreen_state*MAX_HELPSCREEN_ELS);
703     }
704     DoAnimation();
705   }
706
707   BackToFront();
708 }
709
710 void HandleTypeName(int newxpos, KeySym key)
711 {
712   static int xpos = 0, ypos = 2;
713
714   if (newxpos)
715   {
716     xpos = newxpos;
717     DrawText(SX + 6*32, SY + ypos*32, setup.player_name, FS_BIG, FC_YELLOW);
718     DrawGraphic(xpos + 6, ypos, GFX_KUGEL_ROT);
719     return;
720   }
721
722   if (((key >= XK_A && key <= XK_Z) || (key >= XK_a && key <= XK_z)) && 
723       xpos < MAX_NAMELEN - 1)
724   {
725     char ascii;
726
727     if (key >= XK_A && key <= XK_Z)
728       ascii = 'A' + (char)(key - XK_A);
729     else
730       ascii = 'a' + (char)(key - XK_a);
731
732     setup.player_name[xpos] = ascii;
733     setup.player_name[xpos + 1] = 0;
734     xpos++;
735     DrawTextExt(drawto, gc, SX + 6*32, SY + ypos*32,
736                 setup.player_name, FS_BIG, FC_YELLOW);
737     DrawTextExt(window, gc, SX + 6*32, SY + ypos*32,
738                 setup.player_name, FS_BIG, FC_YELLOW);
739     DrawGraphic(xpos + 6, ypos, GFX_KUGEL_ROT);
740   }
741   else if ((key == XK_Delete || key == XK_BackSpace) && xpos > 0)
742   {
743     xpos--;
744     setup.player_name[xpos] = 0;
745     DrawGraphic(xpos + 6, ypos, GFX_KUGEL_ROT);
746     DrawGraphic(xpos + 7, ypos, GFX_LEERRAUM);
747   }
748   else if (key == XK_Return && xpos > 0)
749   {
750     DrawText(SX + 6*32, SY + ypos*32, setup.player_name, FS_BIG, FC_RED);
751     DrawGraphic(xpos + 6, ypos, GFX_LEERRAUM);
752
753     SaveSetup();
754     game_status = MAINMENU;
755   }
756
757   BackToFront();
758 }
759
760 void DrawChooseLevel()
761 {
762   CloseDoor(DOOR_CLOSE_2);
763
764   FadeToFront();
765   InitAnimation();
766   HandleChooseLevel(0,0, 0,0, MB_MENU_INITIALIZE);
767 }
768
769 static void drawChooseLevelList(int first_entry, int num_page_entries)
770 {
771   int i;
772   char buffer[SCR_FIELDX];
773
774   ClearWindow();
775   DrawText(SX, SY, "Level Directories", FS_BIG, FC_GREEN);
776
777   for(i=0; i<num_page_entries; i++)
778   {
779     strncpy(buffer, leveldir[first_entry + i].name , SCR_FIELDX - 1);
780     buffer[SCR_FIELDX - 1] = '\0';
781     DrawText(SX + 32, SY + (i + 2) * 32, buffer, FS_BIG, FC_YELLOW);
782     DrawGraphic(0, i + 2, GFX_KUGEL_BLAU);
783   }
784
785   if (first_entry > 0)
786     DrawGraphic(0, 1, GFX_PFEIL_O);
787
788   if (first_entry + num_page_entries < num_leveldirs)
789     DrawGraphic(0, MAX_LEVEL_SERIES_ON_SCREEN + 1, GFX_PFEIL_U);
790 }
791
792 static void drawChooseLevelInfo(int leveldir_nr)
793 {
794   XFillRectangle(display, drawto, gc, SX + 32, SY + 32, SXSIZE - 32, 32);
795   DrawTextFCentered(40, FC_RED, "%3d levels (%s)",
796                     leveldir[leveldir_nr].levels,
797                     leveldir[leveldir_nr].readonly ? "readonly" : "writable");
798 }
799
800 void HandleChooseLevel(int mx, int my, int dx, int dy, int button)
801 {
802   static int choice = 3;
803   static int first_entry = 0;
804   static unsigned long choose_delay = 0;
805   static int redraw = TRUE;
806   int x = (mx + 32 - SX) / 32, y = (my + 32 - SY) / 32;
807   int num_page_entries;
808
809   if (num_leveldirs <= MAX_LEVEL_SERIES_ON_SCREEN)
810     num_page_entries = num_leveldirs;
811   else
812     num_page_entries = MAX_LEVEL_SERIES_ON_SCREEN - 1;
813
814   if (button == MB_MENU_INITIALIZE)
815   {
816     redraw = TRUE;
817     choice = leveldir_nr + 3 - first_entry;
818
819     if (choice > num_page_entries + 2)
820     {
821       choice = num_page_entries + 2;
822       first_entry = num_leveldirs - num_page_entries;
823     }
824
825     drawChooseLevelList(first_entry, num_page_entries);
826     drawChooseLevelInfo(leveldir_nr);
827   }
828
829   if (redraw)
830   {
831     DrawGraphic(0, choice - 1, GFX_KUGEL_ROT);
832     redraw = FALSE;
833   }
834
835   if (button == MB_MENU_INITIALIZE)
836     return;
837
838   if (dx || dy)
839   {
840     if (dy)
841     {
842       x = 1;
843       y = choice + dy;
844     }
845     else
846       x = y = 0;
847   }
848
849   if (x == 1 && y == 2)
850   {
851     if (first_entry > 0 &&
852         (dy || DelayReached(&choose_delay, 150)))
853     {
854       first_entry--;
855       drawChooseLevelList(first_entry, num_page_entries);
856       drawChooseLevelInfo(first_entry);
857       DrawGraphic(0, choice - 1, GFX_KUGEL_ROT);
858       return;
859     }
860   }
861   else if (x == 1 && y > num_page_entries + 2)
862   {
863     if (first_entry + num_page_entries < num_leveldirs &&
864         (dy || DelayReached(&choose_delay, 150)))
865     {
866       first_entry++;
867       drawChooseLevelList(first_entry, num_page_entries);
868       drawChooseLevelInfo(first_entry + num_page_entries - 1);
869       DrawGraphic(0, choice - 1, GFX_KUGEL_ROT);
870       return;
871     }
872   }
873
874   if (!mx && !my && !dx && !dy)
875   {
876     x = 1;
877     y = choice;
878   }
879
880   if (x == 1 && y >= 3 && y <= num_page_entries + 2)
881   {
882     if (button)
883     {
884       if (y != choice)
885       {
886         DrawGraphic(0, y - 1, GFX_KUGEL_ROT);
887         DrawGraphic(0, choice - 1, GFX_KUGEL_BLAU);
888         drawChooseLevelInfo(first_entry + y - 3);
889         choice = y;
890       }
891     }
892     else
893     {
894       leveldir_nr = first_entry + y - 3;
895       level_nr =
896         getLastPlayedLevelOfLevelSeries(leveldir[leveldir_nr].filename);
897
898       SaveLevelSetup();
899       TapeErase();
900
901       game_status = MAINMENU;
902       DrawMainMenu();
903       redraw = TRUE;
904     }
905   }
906
907   BackToFront();
908
909   if (game_status == CHOOSELEVEL)
910     DoAnimation();
911 }
912
913 void DrawHallOfFame(int highlight_position)
914 {
915   int i;
916
917   CloseDoor(DOOR_CLOSE_2);
918
919   if (highlight_position < 0) 
920     LoadScore(level_nr);
921
922   ClearWindow();
923
924   DrawText(SX + 80, SY + 8, "Hall Of Fame", FS_BIG, FC_YELLOW);
925   DrawTextFCentered(46, FC_RED, "HighScores of Level %d", level_nr);
926
927   for(i=0; i<MAX_LEVEL_SERIES_ON_SCREEN; i++)
928   {
929     DrawText(SX, SY + 64 + i * 32, ".................", FS_BIG,
930              (i == highlight_position ? FC_RED : FC_GREEN));
931     DrawText(SX, SY + 64 + i * 32, highscore[i].Name, FS_BIG,
932              (i == highlight_position ? FC_RED : FC_GREEN));
933     DrawText(SX + 12 * 32, SY + 64 + i * 32,
934              int2str(highscore[i].Score, 5), FS_BIG,
935              (i == highlight_position ? FC_RED : FC_GREEN));
936   }
937
938   FadeToFront();
939   InitAnimation();
940   PlaySound(SND_HALLOFFAME);
941 }
942
943 void HandleHallOfFame(int button)
944 {
945   int button_released = !button;
946
947   if (button_released)
948   {
949     FadeSound(SND_HALLOFFAME);
950     game_status = MAINMENU;
951     DrawMainMenu();
952     BackToFront();
953   }
954   else
955     DoAnimation();
956 }
957
958 void DrawSetupScreen()
959 {
960   int i;
961   static struct setup
962   {
963     boolean *value;
964     char *text;
965   } setup_info[] =
966   {
967     { &setup.sound,             "Sound:",       },
968     { &setup.sound_loops,       " Sound Loops:" },
969     { &setup.sound_music,       " Game Music:"  },
970     { &setup.toons,             "Toons:"        },
971     { &setup.double_buffering,  "Buffered gfx:" },
972     { &setup.scroll_delay,      "Scroll Delay:" },
973     { &setup.soft_scrolling,    "Soft Scroll.:" },
974     { &setup.fading,            "Fading:"       },
975     { &setup.quick_doors,       "Quick Doors:"  },
976     { &setup.autorecord,        "Auto-Record:"  },
977     { &setup.team_mode,         "Team-Mode:"    },
978     { NULL,                     "Input Devices" },
979     { NULL,                     ""              },
980     { NULL,                     "Exit"          },
981     { NULL,                     "Save and exit" }
982   };
983
984   CloseDoor(DOOR_CLOSE_2);
985   ClearWindow();
986   DrawText(SX+16, SY+16, "SETUP",FS_BIG,FC_YELLOW);
987
988   for(i=SETUP_SCREEN_POS_START;i<=SETUP_SCREEN_POS_END;i++)
989   {
990     int base = i - SETUP_SCREEN_POS_START;
991
992     if (!(i >= SETUP_SCREEN_POS_EMPTY1 && i <= SETUP_SCREEN_POS_EMPTY2))
993     {
994       DrawGraphic(0,i,GFX_KUGEL_BLAU);
995       DrawText(SX+32,SY+i*32, setup_info[base].text, FS_BIG,FC_GREEN);
996     }
997
998     if (setup_info[base].value)
999     {
1000       int setting_value = *setup_info[base].value;
1001
1002       DrawText(SX+14*32, SY+i*32, (setting_value ? "on" : "off"),
1003                FS_BIG, (setting_value ? FC_YELLOW : FC_BLUE));
1004     }
1005   }
1006
1007   FadeToFront();
1008   InitAnimation();
1009   HandleSetupScreen(0,0,0,0,MB_MENU_INITIALIZE);
1010 }
1011
1012 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
1013 {
1014   static int choice = 3;
1015   static int redraw = TRUE;
1016   int x = (mx+32-SX)/32, y = (my+32-SY)/32;
1017   int pos_start  = SETUP_SCREEN_POS_START  + 1;
1018   int pos_empty1 = SETUP_SCREEN_POS_EMPTY1 + 1;
1019   int pos_empty2 = SETUP_SCREEN_POS_EMPTY2 + 1;
1020   int pos_end    = SETUP_SCREEN_POS_END    + 1;
1021
1022   if (button == MB_MENU_INITIALIZE)
1023     redraw = TRUE;
1024
1025   if (redraw)
1026   {
1027     DrawGraphic(0,choice-1,GFX_KUGEL_ROT);
1028     redraw = FALSE;
1029   }
1030
1031   if (button == MB_MENU_INITIALIZE)
1032     return;
1033
1034   if (dx || dy)
1035   {
1036     if (dy)
1037     {
1038       x = 1;
1039       y = choice+dy;
1040     }
1041     else
1042       x = y = 0;
1043
1044     if (y >= pos_empty1 && y <= pos_empty2)
1045       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
1046
1047     if (y < pos_start)
1048       y = pos_start;
1049     else if (y > pos_end)
1050       y = pos_end;
1051   }
1052
1053   if (!mx && !my && !dx && !dy)
1054   {
1055     x = 1;
1056     y = choice;
1057   }
1058
1059   if (x==1 && y >= pos_start && y <= pos_end &&
1060       !(y >= pos_empty1 && y <= pos_empty2))
1061   {
1062     if (button)
1063     {
1064       if (y!=choice)
1065       {
1066         DrawGraphic(0,y-1,GFX_KUGEL_ROT);
1067         DrawGraphic(0,choice-1,GFX_KUGEL_BLAU);
1068       }
1069       choice = y;
1070     }
1071     else
1072     {
1073       int yy = y-1;
1074
1075       if (y==3 && sound_status==SOUND_AVAILABLE)
1076       {
1077         if (setup.sound)
1078         {
1079           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1080           DrawText(SX+14*32, SY+(yy+1)*32,"off",FS_BIG,FC_BLUE);
1081           DrawText(SX+14*32, SY+(yy+2)*32,"off",FS_BIG,FC_BLUE);
1082           setup.sound_loops = FALSE;
1083           setup.sound_music = FALSE;
1084         }
1085         else
1086           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1087         setup.sound = !setup.sound;
1088       }
1089       else if (y==4 && sound_loops_allowed)
1090       {
1091         if (setup.sound_loops)
1092           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1093         else
1094         {
1095           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1096           DrawText(SX+14*32, SY+(yy-1)*32,"on ",FS_BIG,FC_YELLOW);
1097           setup.sound = TRUE;
1098         }
1099         setup.sound_loops = !setup.sound_loops;
1100       }
1101       else if (y==5 && sound_loops_allowed)
1102       {
1103         if (setup.sound_music)
1104           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1105         else
1106         {
1107           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1108           DrawText(SX+14*32, SY+(yy-2)*32,"on ",FS_BIG,FC_YELLOW);
1109           setup.sound = TRUE;
1110         }
1111         setup.sound_music = !setup.sound_music;
1112       }
1113       else if (y==6)
1114       {
1115         if (setup.toons)
1116           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1117         else
1118           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1119         setup.toons = !setup.toons;
1120       }
1121       else if (y==7)
1122       {
1123         if (setup.double_buffering)
1124           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1125         else
1126           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1127         setup.double_buffering = !setup.double_buffering;
1128         setup.direct_draw = !setup.double_buffering;
1129       }
1130       else if (y==8)
1131       {
1132         if (setup.scroll_delay)
1133           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1134         else
1135           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1136         setup.scroll_delay = !setup.scroll_delay;
1137       }
1138       else if (y==9)
1139       {
1140         if (setup.soft_scrolling)
1141           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1142         else
1143           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1144         setup.soft_scrolling = !setup.soft_scrolling;
1145       }
1146       else if (y==10)
1147       {
1148         if (setup.fading)
1149           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1150         else
1151           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1152         setup.fading = !setup.fading;
1153       }
1154       else if (y==11)
1155       {
1156         if (setup.quick_doors)
1157           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1158         else
1159           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1160         setup.quick_doors = !setup.quick_doors;
1161       }
1162       else if (y==12)
1163       {
1164         if (setup.autorecord)
1165           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1166         else
1167           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1168         setup.autorecord = !setup.autorecord;
1169       }
1170       else if (y==13)
1171       {
1172         if (setup.team_mode)
1173           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1174         else
1175           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1176         setup.team_mode = !setup.team_mode;
1177       }
1178       else if (y==14)
1179       {
1180         game_status = SETUPINPUT;
1181         DrawSetupInputScreen();
1182         redraw = TRUE;
1183       }
1184       else if (y==pos_end-1 || y==pos_end)
1185       {
1186         if (y==pos_end)
1187         {
1188           SaveSetup();
1189
1190           /*
1191           SaveJoystickData();
1192           */
1193
1194 #ifdef MSDOS
1195           save_joystick_data(JOYSTICK_FILENAME);
1196 #endif
1197
1198
1199         }
1200
1201         game_status = MAINMENU;
1202         DrawMainMenu();
1203         redraw = TRUE;
1204       }
1205     }
1206   }
1207   BackToFront();
1208
1209   if (game_status==SETUP)
1210     DoAnimation();
1211 }
1212
1213 void DrawSetupInputScreen()
1214 {
1215   ClearWindow();
1216   DrawText(SX+16, SY+16, "SETUP INPUT", FS_BIG, FC_YELLOW);
1217
1218   DrawGraphic(0, 2, GFX_KUGEL_BLAU);
1219   DrawGraphic(0, 3, GFX_KUGEL_BLAU);
1220   DrawGraphic(0, 4, GFX_KUGEL_BLAU);
1221   DrawGraphic(0, 15, GFX_KUGEL_BLAU);
1222   DrawGraphic(10, 2, GFX_PFEIL_L);
1223   DrawGraphic(12, 2, GFX_PFEIL_R);
1224
1225   DrawText(SX+32, SY+2*32, "Player:", FS_BIG, FC_GREEN);
1226   DrawText(SX+32, SY+3*32, "Device:", FS_BIG, FC_GREEN);
1227   DrawText(SX+32, SY+15*32, "Exit", FS_BIG, FC_GREEN);
1228
1229   DrawTextFCentered(SYSIZE - 20, FC_BLUE,
1230                     "Joysticks deactivated on this screen");
1231
1232   HandleSetupInputScreen(0,0, 0,0, MB_MENU_INITIALIZE);
1233   FadeToFront();
1234   InitAnimation();
1235 }
1236
1237 static void setJoystickDeviceToNr(char *device_name, int device_nr)
1238 {
1239   if (device_name == NULL)
1240     return;
1241
1242   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
1243     device_nr = 0;
1244
1245   if (strlen(device_name) > 1)
1246   {
1247     char c1 = device_name[strlen(device_name) - 1];
1248     char c2 = device_name[strlen(device_name) - 2];
1249
1250     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
1251       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
1252   }
1253   else
1254     strncpy(device_name, joystick_device_name[device_nr], strlen(device_name));
1255 }
1256
1257 static void drawPlayerSetupInputInfo(int player_nr)
1258 {
1259   int i;
1260   static struct SetupKeyboardInfo custom_key;
1261   static struct
1262   {
1263     KeySym *keysym;
1264     char *text;
1265   } custom[] =
1266   {
1267     { &custom_key.left,  "Joystick Left"  },
1268     { &custom_key.right, "Joystick Right" },
1269     { &custom_key.up,    "Joystick Up"    },
1270     { &custom_key.down,  "Joystick Down"  },
1271     { &custom_key.snap,  "Button 1"       },
1272     { &custom_key.bomb,  "Button 2"       }
1273   };
1274   static char *joystick_name[MAX_PLAYERS] =
1275   {
1276     "Joystick1",
1277     "Joystick2",
1278     "Joystick3",
1279     "Joystick4"
1280   };
1281
1282   custom_key = setup.input[player_nr].key;
1283
1284   DrawText(SX+11*32, SY+2*32, int2str(player_nr + 1, 1), FS_BIG, FC_RED);
1285   DrawGraphic(8, 2, GFX_SPIELER1 + player_nr);
1286
1287   if (setup.input[player_nr].use_joystick)
1288   {
1289     char *device_name = setup.input[player_nr].joy.device_name;
1290
1291     DrawText(SX+8*32, SY+3*32,
1292              joystick_name[getJoystickNrFromDeviceName(device_name)],
1293              FS_BIG, FC_YELLOW);
1294     DrawText(SX+32, SY+4*32, "Calibrate", FS_BIG, FC_GREEN);
1295   }
1296   else
1297   {
1298     DrawText(SX+8*32, SY+3*32, "Keyboard ", FS_BIG, FC_YELLOW);
1299     DrawText(SX+32, SY+4*32, "Customize", FS_BIG, FC_GREEN);
1300   }
1301
1302   DrawText(SX+32, SY+5*32, "Actual Settings:", FS_BIG, FC_GREEN);
1303   DrawGraphic(1, 6, GFX_PFEIL_L);
1304   DrawGraphic(1, 7, GFX_PFEIL_R);
1305   DrawGraphic(1, 8, GFX_PFEIL_O);
1306   DrawGraphic(1, 9, GFX_PFEIL_U);
1307   DrawText(SX+2*32, SY+6*32, ":", FS_BIG, FC_BLUE);
1308   DrawText(SX+2*32, SY+7*32, ":", FS_BIG, FC_BLUE);
1309   DrawText(SX+2*32, SY+8*32, ":", FS_BIG, FC_BLUE);
1310   DrawText(SX+2*32, SY+9*32, ":", FS_BIG, FC_BLUE);
1311   DrawText(SX+32, SY+10*32, "Snap Field:", FS_BIG, FC_BLUE);
1312   DrawText(SX+32, SY+12*32, "Place Bomb:", FS_BIG, FC_BLUE);
1313
1314   for (i=0; i<6; i++)
1315   {
1316     int ypos = 6 + i + (i > 3 ? i-3 : 0);
1317
1318     DrawText(SX + 3*32, SY + ypos*32,
1319              "              ", FS_BIG, FC_YELLOW);
1320     DrawText(SX + 3*32, SY + ypos*32,
1321              (setup.input[player_nr].use_joystick ?
1322               custom[i].text :
1323               getKeyNameFromKeySym(*custom[i].keysym)),
1324              FS_BIG, FC_YELLOW);
1325   }
1326 }
1327
1328 void HandleSetupInputScreen(int mx, int my, int dx, int dy, int button)
1329 {
1330   static int choice = 3;
1331   static int player_nr = 0;
1332   static int redraw = TRUE;
1333   int x = (mx+32-SX)/32, y = (my+32-SY)/32;
1334   int pos_start  = SETUPINPUT_SCREEN_POS_START  + 1;
1335   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1 + 1;
1336   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2 + 1;
1337   int pos_end    = SETUPINPUT_SCREEN_POS_END    + 1;
1338
1339   if (button == MB_MENU_INITIALIZE)
1340   {
1341     drawPlayerSetupInputInfo(player_nr);
1342     redraw = TRUE;
1343   }
1344
1345   if (redraw)
1346   {
1347     DrawGraphic(0,choice-1,GFX_KUGEL_ROT);
1348     redraw = FALSE;
1349   }
1350
1351   if (button == MB_MENU_INITIALIZE)
1352     return;
1353
1354   if (dx || dy)
1355   {
1356     if (dx && choice == 3)
1357     {
1358       x = (dx < 0 ? 11 : 13);
1359       y = 3;
1360     }
1361     else if (dx && choice == 4)
1362     {
1363       button = MB_MENU_CHOICE;
1364       x = 1;
1365       y = 4;
1366     }
1367     else if (dy)
1368     {
1369       x = 1;
1370       y = choice + dy;
1371     }
1372     else
1373       x = y = 0;
1374
1375     if (y >= pos_empty1 && y <= pos_empty2)
1376       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
1377
1378     if (y < pos_start)
1379       y = pos_start;
1380     else if (y > pos_end)
1381       y = pos_end;
1382   }
1383
1384   if (!mx && !my && !dx && !dy)
1385   {
1386     x = 1;
1387     y = choice;
1388   }
1389
1390   if (y == 3 && ((x == 1 && !button) || ((x == 11 || x == 13) && button)))
1391   {
1392     static unsigned long delay = 0;
1393
1394     if (!DelayReached(&delay, 150))
1395       goto out;
1396
1397     player_nr = (player_nr + (x == 11 ? -1 : +1) + MAX_PLAYERS) % MAX_PLAYERS;
1398
1399     drawPlayerSetupInputInfo(player_nr);
1400   }
1401   else if (x==1 && y >= pos_start && y <= pos_end &&
1402            !(y >= pos_empty1 && y <= pos_empty2))
1403   {
1404     if (button)
1405     {
1406       if (y != choice)
1407       {
1408         DrawGraphic(0, y-1, GFX_KUGEL_ROT);
1409         DrawGraphic(0, choice-1, GFX_KUGEL_BLAU);
1410       }
1411       choice = y;
1412     }
1413     else
1414     {
1415       if (y == 4)
1416       {
1417         char *device_name = setup.input[player_nr].joy.device_name;
1418
1419         if (!setup.input[player_nr].use_joystick)
1420         {
1421           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
1422
1423           setJoystickDeviceToNr(device_name, new_device_nr);
1424           setup.input[player_nr].use_joystick = TRUE;
1425         }
1426         else
1427         {
1428           int device_nr = getJoystickNrFromDeviceName(device_name);
1429           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
1430
1431           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
1432             setup.input[player_nr].use_joystick = FALSE;
1433           else
1434             setJoystickDeviceToNr(device_name, new_device_nr);
1435         }
1436
1437
1438         /*
1439         InitJoysticks();
1440         */
1441
1442
1443 #if 0
1444         int one_joystick_nr       = (dx >= 0 ? 0 : 1);
1445         int the_other_joystick_nr = (dx >= 0 ? 1 : 0);
1446
1447         if (setup.input[player_nr].use_joystick)
1448         {
1449           if (setup.input[player_nr].joystick_nr == one_joystick_nr)
1450             setup.input[player_nr].joystick_nr = the_other_joystick_nr;
1451           else
1452             setup.input[player_nr].use_joystick = FALSE;
1453         }
1454         else
1455         {
1456           setup.input[player_nr].use_joystick = TRUE;
1457           setup.input[player_nr].joystick_nr = one_joystick_nr;
1458         }
1459 #endif
1460
1461         drawPlayerSetupInputInfo(player_nr);
1462       }
1463       else if (y == 5)
1464       {
1465         if (setup.input[player_nr].use_joystick)
1466         {
1467           InitJoysticks();
1468           game_status = CALIBRATION;
1469           CalibrateJoystick(player_nr);
1470           game_status = SETUPINPUT;
1471         }
1472         else
1473           CustomizeKeyboard(player_nr);
1474
1475         redraw = TRUE;
1476       }
1477       else if (y == pos_end)
1478       {
1479         InitJoysticks();
1480
1481         game_status = SETUP;
1482         DrawSetupScreen();
1483         redraw = TRUE;
1484       }
1485     }
1486   }
1487   BackToFront();
1488
1489   out:
1490
1491   if (game_status == SETUPINPUT)
1492     DoAnimation();
1493 }
1494
1495 void CustomizeKeyboard(int player_nr)
1496 {
1497   int i;
1498   int step_nr;
1499   boolean finished = FALSE;
1500   static struct SetupKeyboardInfo custom_key;
1501   static struct
1502   {
1503     KeySym *keysym;
1504     char *text;
1505   } customize_step[] =
1506   {
1507     { &custom_key.left,  "Move Left"  },
1508     { &custom_key.right, "Move Right" },
1509     { &custom_key.up,    "Move Up"    },
1510     { &custom_key.down,  "Move Down"  },
1511     { &custom_key.snap,  "Snap Field" },
1512     { &custom_key.bomb,  "Place Bomb" }
1513   };
1514
1515   /* read existing key bindings from player setup */
1516   custom_key = setup.input[player_nr].key;
1517
1518   ClearWindow();
1519   DrawText(SX + 16, SY + 16, "Keyboard Input", FS_BIG, FC_YELLOW);
1520
1521   BackToFront();
1522   InitAnimation();
1523
1524   step_nr = 0;
1525   DrawText(SX, SY + (2+2*step_nr)*32,
1526            customize_step[step_nr].text, FS_BIG, FC_RED);
1527   DrawText(SX, SY + (2+2*step_nr+1)*32,
1528            "Key:", FS_BIG, FC_RED);
1529   DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
1530            getKeyNameFromKeySym(*customize_step[step_nr].keysym),
1531            FS_BIG, FC_BLUE);
1532
1533   while(!finished)
1534   {
1535     if (XPending(display))      /* got event from X server */
1536     {
1537       XEvent event;
1538
1539       XNextEvent(display, &event);
1540
1541       switch(event.type)
1542       {
1543         case KeyPress:
1544           {
1545             KeySym key = XLookupKeysym((XKeyEvent *)&event,
1546                                        ((XKeyEvent *)&event)->state);
1547
1548             if (key == XK_Escape || (key == XK_Return && step_nr == 6))
1549             {
1550               finished = TRUE;
1551               break;
1552             }
1553
1554             /* press 'Enter' to keep the existing key binding */
1555             if (key == XK_Return || step_nr == 6)
1556               key = *customize_step[step_nr].keysym;
1557
1558             /* check if key already used */
1559             for (i=0; i<step_nr; i++)
1560               if (*customize_step[i].keysym == key)
1561                 break;
1562             if (i < step_nr)
1563               break;
1564
1565             /* got new key binding */
1566             *customize_step[step_nr].keysym = key;
1567             DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
1568                      "             ", FS_BIG, FC_YELLOW);
1569             DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
1570                      getKeyNameFromKeySym(key), FS_BIG, FC_YELLOW);
1571             step_nr++;
1572
1573             /* un-highlight last query */
1574             DrawText(SX, SY+(2+2*(step_nr-1))*32,
1575                      customize_step[step_nr-1].text, FS_BIG, FC_GREEN);
1576             DrawText(SX, SY+(2+2*(step_nr-1)+1)*32,
1577                      "Key:", FS_BIG, FC_GREEN);
1578
1579             /* press 'Enter' to leave */
1580             if (step_nr == 6)
1581             {
1582               DrawText(SX + 16, SY + 15*32+16,
1583                        "Press Enter", FS_BIG, FC_YELLOW);
1584               break;
1585             }
1586
1587             /* query next key binding */
1588             DrawText(SX, SY+(2+2*step_nr)*32,
1589                      customize_step[step_nr].text, FS_BIG, FC_RED);
1590             DrawText(SX, SY+(2+2*step_nr+1)*32,
1591                      "Key:", FS_BIG, FC_RED);
1592             DrawText(SX + 4*32, SY+(2+2*step_nr+1)*32,
1593                      getKeyNameFromKeySym(*customize_step[step_nr].keysym),
1594                      FS_BIG, FC_BLUE);
1595           }
1596           break;
1597
1598         case KeyRelease:
1599           key_joystick_mapping = 0;
1600           break;
1601
1602         default:
1603           HandleOtherEvents(&event);
1604           break;
1605       }
1606     }
1607
1608     BackToFront();
1609     DoAnimation();
1610
1611     /* don't eat all CPU time */
1612     Delay(10);
1613   }
1614
1615   /* write new key bindings back to player setup */
1616   setup.input[player_nr].key = custom_key;
1617
1618   StopAnimation();
1619   DrawSetupInputScreen();
1620 }
1621
1622 void CalibrateJoystick(int player_nr)
1623 {
1624 #ifdef __FreeBSD__
1625   struct joystick joy_ctrl;
1626 #else
1627   struct joystick_control
1628   {
1629     int buttons;
1630     int x;
1631     int y;
1632   } joy_ctrl;
1633 #endif
1634
1635 #ifndef MSDOS
1636   int new_joystick_xleft = 128, new_joystick_xright = 128;
1637   int new_joystick_yupper = 128, new_joystick_ylower = 128;
1638   int new_joystick_xmiddle, new_joystick_ymiddle;
1639 #else
1640   int calibration_step = 1;
1641 #endif
1642
1643   int joystick_fd = stored_player[player_nr].joystick_fd;
1644   int x, y, last_x, last_y, xpos = 8, ypos = 3;
1645   boolean check[3][3];
1646   int check_remaining;
1647   int joy_value;
1648   int result = -1;
1649
1650   if (joystick_status == JOYSTICK_OFF ||
1651       joystick_fd < 0 ||
1652       !setup.input[player_nr].use_joystick)
1653     goto error_out;
1654
1655   ClearWindow();
1656
1657 #ifndef MSDOS
1658   DrawText(SX,      SY +  6*32, " ROTATE JOYSTICK ", FS_BIG, FC_YELLOW);
1659   DrawText(SX,      SY +  7*32, "IN ALL DIRECTIONS", FS_BIG, FC_YELLOW);
1660   DrawText(SX + 16, SY +  9*32, "  IF ALL BALLS  ",  FS_BIG, FC_YELLOW);
1661   DrawText(SX,      SY + 10*32, "   ARE YELLOW,   ", FS_BIG, FC_YELLOW);
1662   DrawText(SX,      SY + 11*32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW);
1663   check_remaining = 3 * 3;
1664 #else
1665   DrawText(SX,      SY +  7*32, "  MOVE JOYSTICK  ", FS_BIG, FC_YELLOW);
1666   DrawText(SX + 16, SY +  8*32, "       TO       ",  FS_BIG, FC_YELLOW);
1667   DrawText(SX,      SY +  9*32, " CENTER POSITION ",  FS_BIG, FC_YELLOW);
1668   DrawText(SX,      SY + 10*32, "       AND       ", FS_BIG, FC_YELLOW);
1669   DrawText(SX,      SY + 11*32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW);
1670   check_remaining = 0;
1671 #endif
1672
1673   for(y=0; y<3; y++)
1674   {
1675     for(x=0; x<3; x++)
1676     {
1677       check[x][y] = FALSE;
1678       DrawGraphic(xpos + x - 1, ypos + y - 1, GFX_KUGEL_BLAU);
1679     }
1680   }
1681
1682   joy_value = Joystick(player_nr);
1683   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
1684   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
1685   DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_ROT);
1686
1687   BackToFront();
1688
1689 #ifdef __FreeBSD__
1690   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1691 #else
1692   joy_ctrl.buttons = 0;
1693 #endif
1694
1695   while(Joystick(player_nr) & JOY_BUTTON);
1696
1697   InitAnimation();
1698
1699   while(result < 0)
1700   {
1701     if (XPending(display))      /* got event from X server */
1702     {
1703       XEvent event;
1704
1705       XNextEvent(display, &event);
1706
1707       switch(event.type)
1708       {
1709         case KeyPress:
1710           switch(XLookupKeysym((XKeyEvent *)&event,
1711                                ((XKeyEvent *)&event)->state))
1712           {
1713             case XK_Return:
1714               if (check_remaining == 0)
1715                 result = 1;
1716               break;
1717
1718             case XK_Escape:
1719               result = 0;
1720               break;
1721
1722             default:
1723               break;
1724           }
1725           break;
1726
1727         case KeyRelease:
1728           key_joystick_mapping = 0;
1729           break;
1730
1731         default:
1732           HandleOtherEvents(&event);
1733           break;
1734       }
1735     }
1736
1737 #ifndef MSDOS
1738     if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1739     {
1740       joystick_status = JOYSTICK_OFF;
1741       goto error_out;
1742     }
1743
1744     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_ctrl.x);
1745     new_joystick_xright = MAX(new_joystick_xright, joy_ctrl.x);
1746     new_joystick_yupper = MIN(new_joystick_yupper, joy_ctrl.y);
1747     new_joystick_ylower = MAX(new_joystick_ylower, joy_ctrl.y);
1748
1749     new_joystick_xmiddle =
1750       new_joystick_xleft + (new_joystick_xright - new_joystick_xleft) / 2;
1751     new_joystick_ymiddle =
1752       new_joystick_yupper + (new_joystick_ylower - new_joystick_yupper) / 2;
1753
1754     setup.input[player_nr].joy.xleft = new_joystick_xleft;
1755     setup.input[player_nr].joy.yupper = new_joystick_yupper;
1756     setup.input[player_nr].joy.xright = new_joystick_xright;
1757     setup.input[player_nr].joy.ylower = new_joystick_ylower;
1758     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
1759     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
1760
1761     CheckJoystickData();
1762 #endif
1763
1764     joy_value = Joystick(player_nr);
1765
1766     if (joy_value & JOY_BUTTON && check_remaining == 0)
1767     {
1768       result = 1;
1769
1770 #ifdef MSDOS
1771       if (calibration_step == 1)
1772       {
1773         remove_joystick();
1774         InitJoysticks();
1775       }
1776       else if (calibrate_joystick(joystick_fd) != 0)
1777       {
1778         joystick_status = JOYSTICK_OFF;
1779         goto error_out;
1780       }
1781
1782       if (joy[joystick_fd].flags & JOYFLAG_CALIBRATE)
1783       {
1784         calibration_step++;
1785         result = -1;
1786
1787         DrawText(SX,      SY +  7*32, "  MOVE JOYSTICK  ", FS_BIG, FC_YELLOW);
1788         DrawText(SX + 16, SY +  8*32, "       TO       ",  FS_BIG, FC_YELLOW);
1789
1790         if (calibration_step == 2)
1791           DrawText(SX + 16, SY + 9*32," THE UPPER LEFT ",  FS_BIG, FC_YELLOW);
1792         else
1793           DrawText(SX,      SY + 9*32," THE LOWER RIGHT ", FS_BIG, FC_YELLOW);
1794
1795         DrawText(SX,      SY + 10*32, "       AND       ", FS_BIG, FC_YELLOW);
1796         DrawText(SX,      SY + 11*32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW);
1797
1798         BackToFront();
1799
1800         while(Joystick(player_nr) & JOY_BUTTON)
1801           DoAnimation();
1802       }
1803 #endif
1804     }
1805
1806     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
1807     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
1808
1809     if (x != last_x || y != last_y)
1810     {
1811 #ifndef MSDOS
1812       DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_GELB);
1813 #else
1814       DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_BLAU);
1815 #endif
1816       DrawGraphic(xpos + x,      ypos + y,      GFX_KUGEL_ROT);
1817
1818       last_x = x;
1819       last_y = y;
1820
1821       if (check_remaining > 0 && !check[x+1][y+1])
1822       {
1823         check[x+1][y+1] = TRUE;
1824         check_remaining--;
1825       }
1826
1827 #if 0
1828       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
1829              setup.input[player_nr].joy.xleft,
1830              setup.input[player_nr].joy.xmiddle,
1831              setup.input[player_nr].joy.xright);
1832       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
1833              setup.input[player_nr].joy.yupper,
1834              setup.input[player_nr].joy.ymiddle,
1835              setup.input[player_nr].joy.ylower);
1836 #endif
1837
1838     }
1839
1840     BackToFront();
1841     DoAnimation();
1842
1843     /* don't eat all CPU time */
1844     Delay(10);
1845   }
1846
1847   StopAnimation();
1848
1849   DrawSetupInputScreen();
1850   while(Joystick(player_nr) & JOY_BUTTON);
1851   return;
1852
1853   error_out:
1854
1855   ClearWindow();
1856   DrawText(SX + 16, SY + 6*32, "  JOYSTICK NOT  ", FS_BIG, FC_YELLOW);
1857   DrawText(SX,      SY + 7*32, "    AVAILABLE    ", FS_BIG, FC_YELLOW);
1858   BackToFront();
1859   Delay(2000);
1860   DrawSetupInputScreen();
1861 }
1862
1863
1864
1865 #if 0
1866
1867 void CalibrateJoystick_OLD()
1868 {
1869 #ifdef __FreeBSD__
1870   struct joystick joy_ctrl;
1871 #else
1872   struct joystick_control
1873   {
1874     int buttons;
1875     int x;
1876     int y;
1877   } joy_ctrl;
1878 #endif
1879
1880 #ifdef MSDOS
1881   char joy_nr[4];
1882 #endif
1883
1884   int joystick_nr = setup.input[0].joystick_nr;
1885   int new_joystick_xleft, new_joystick_xright, new_joystick_xmiddle;
1886   int new_joystick_yupper, new_joystick_ylower, new_joystick_ymiddle;
1887
1888   if (joystick_status == JOYSTICK_OFF)
1889     goto error_out;
1890
1891 #ifndef MSDOS
1892   ClearWindow();
1893   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1894   DrawText(SX+16, SY+8*32, " THE UPPER LEFT ",FS_BIG,FC_YELLOW);
1895   DrawText(SX+16, SY+9*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1896   BackToFront();
1897
1898 #ifdef __FreeBSD__
1899   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1900 #else
1901   joy_ctrl.buttons = 0;
1902 #endif
1903   while(Joystick() & JOY_BUTTON);
1904 #ifdef __FreeBSD__
1905   while(!(joy_ctrl.b1 || joy_ctrl.b2))
1906 #else
1907   while(!joy_ctrl.buttons)
1908 #endif
1909   {
1910     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1911     {
1912       joystick_status=JOYSTICK_OFF;
1913       goto error_out;
1914     }
1915     Delay(10);
1916   }
1917
1918   new_joystick_xleft = joy_ctrl.x;
1919   new_joystick_yupper = joy_ctrl.y;
1920
1921   ClearWindow();
1922   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1923   DrawText(SX+32, SY+8*32, "THE LOWER RIGHT",FS_BIG,FC_YELLOW);
1924   DrawText(SX+16, SY+9*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1925   BackToFront();
1926
1927 #ifdef __FreeBSD__
1928   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1929 #else
1930   joy_ctrl.buttons = 0;
1931 #endif
1932   while(Joystick() & JOY_BUTTON);
1933 #ifdef __FreeBSD__
1934   while(!(joy_ctrl.b1 || joy_ctrl.b2))
1935 #else
1936   while(!joy_ctrl.buttons)
1937 #endif
1938   {
1939     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1940     {
1941       joystick_status=JOYSTICK_OFF;
1942       goto error_out;
1943     }
1944     Delay(10);
1945   }
1946
1947   new_joystick_xright = joy_ctrl.x;
1948   new_joystick_ylower = joy_ctrl.y;
1949
1950   ClearWindow();
1951   DrawText(SX+32, SY+16+7*32, "CENTER JOYSTICK",FS_BIG,FC_YELLOW);
1952   DrawText(SX+16, SY+16+8*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1953   BackToFront();
1954
1955 #ifdef __FreeBSD__
1956   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1957 #else
1958   joy_ctrl.buttons = 0;
1959 #endif
1960   while(Joystick() & JOY_BUTTON);
1961 #ifdef __FreeBSD__
1962   while(!(joy_ctrl.b1 || joy_ctrl.b2))
1963 #else
1964   while(!joy_ctrl.buttons)
1965 #endif
1966   {
1967     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1968     {
1969       joystick_status=JOYSTICK_OFF;
1970       goto error_out;
1971     }
1972     Delay(10);
1973   }
1974
1975   new_joystick_xmiddle = joy_ctrl.x;
1976   new_joystick_ymiddle = joy_ctrl.y;
1977
1978   setup.input[player_nr].joy.xleft = new_joystick_xleft;
1979   setup.input[player_nr].joy.yupper = new_joystick_yupper;
1980   setup.input[player_nr].joy.xright = new_joystick_xright;
1981   setup.input[player_nr].joy.ylower = new_joystick_ylower;
1982   setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
1983   setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
1984
1985   CheckJoystickData();
1986
1987   DrawSetupScreen();
1988   while(Joystick() & JOY_BUTTON);
1989   return;
1990
1991 #endif
1992   error_out:
1993
1994 #ifdef MSDOS
1995   joy_nr[0] = '#';
1996   joy_nr[1] = SETUP_2ND_JOYSTICK_ON(local_player->setup)+49;
1997   joy_nr[2] = '\0';
1998
1999   remove_joystick();
2000   ClearWindow();
2001   DrawText(SX+32, SY+7*32, "CENTER JOYSTICK",FS_BIG,FC_YELLOW);
2002   DrawText(SX+16+7*32, SY+8*32, joy_nr, FS_BIG,FC_YELLOW);
2003   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
2004   BackToFront();
2005
2006   for(clear_keybuf();!keypressed(););
2007   install_joystick(JOY_TYPE_2PADS);
2008
2009   ClearWindow();
2010   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
2011   DrawText(SX+16, SY+8*32, " THE UPPER LEFT ",FS_BIG,FC_YELLOW);
2012   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
2013   BackToFront();
2014
2015   for(clear_keybuf();!keypressed(););
2016   calibrate_joystick(SETUP_2ND_JOYSTICK_ON(local_player->setup));
2017
2018   ClearWindow();
2019   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
2020   DrawText(SX+32, SY+8*32, "THE LOWER RIGHT",FS_BIG,FC_YELLOW);
2021   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
2022   BackToFront();
2023
2024   for(clear_keybuf();!keypressed(););
2025   calibrate_joystick(SETUP_2ND_JOYSTICK_ON(local_player->setup));
2026
2027   DrawSetupScreen();
2028   return;
2029 #endif
2030
2031   ClearWindow();
2032   DrawText(SX+16, SY+16, "NO JOYSTICK",FS_BIG,FC_YELLOW);
2033   DrawText(SX+16, SY+48, " AVAILABLE ",FS_BIG,FC_YELLOW);
2034   BackToFront();
2035   Delay(3000);
2036   DrawSetupScreen();
2037 }
2038
2039 #endif
2040
2041
2042
2043 void HandleGameActions()
2044 {
2045   if (game_status != PLAYING)
2046     return;
2047
2048   if (local_player->LevelSolved)
2049     GameWon();
2050
2051   if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
2052     TapeStop();
2053
2054   GameActions();
2055
2056   BackToFront();
2057 }
2058
2059 void HandleVideoButtons(int mx, int my, int button)
2060 {
2061   if (game_status != MAINMENU && game_status != PLAYING)
2062     return;
2063
2064   switch(CheckVideoButtons(mx,my,button))
2065   {
2066     case BUTTON_VIDEO_EJECT:
2067       TapeStop();
2068       if (TAPE_IS_EMPTY(tape))
2069       {
2070         LoadTape(level_nr);
2071         if (TAPE_IS_EMPTY(tape))
2072           Request("No tape for this level !",REQ_CONFIRM);
2073       }
2074       else
2075       {
2076         if (tape.changed)
2077           SaveTape(tape.level_nr);
2078         TapeErase();
2079       }
2080       DrawCompleteVideoDisplay();
2081       break;
2082
2083     case BUTTON_VIDEO_STOP:
2084       TapeStop();
2085       break;
2086
2087     case BUTTON_VIDEO_PAUSE:
2088       TapeTogglePause();
2089       break;
2090
2091     case BUTTON_VIDEO_REC:
2092       if (TAPE_IS_STOPPED(tape))
2093       {
2094         TapeStartRecording();
2095
2096 #ifndef MSDOS
2097         if (options.network)
2098           SendToServer_StartPlaying();
2099         else
2100 #endif
2101         {
2102           game_status = PLAYING;
2103           InitGame();
2104         }
2105       }
2106       else if (tape.pausing)
2107       {
2108         if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
2109         {
2110           tape.pos[tape.counter].delay = tape.delay_played;
2111           tape.playing = FALSE;
2112           tape.recording = TRUE;
2113           tape.changed = TRUE;
2114
2115           DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON,0);
2116         }
2117         else
2118           TapeTogglePause();
2119       }
2120       break;
2121
2122     case BUTTON_VIDEO_PLAY:
2123       if (TAPE_IS_EMPTY(tape))
2124         break;
2125
2126       if (TAPE_IS_STOPPED(tape))
2127       {
2128         TapeStartPlaying();
2129
2130         game_status = PLAYING;
2131         InitGame();
2132       }
2133       else if (tape.playing)
2134       {
2135         if (tape.pausing)                       /* PAUSE -> PLAY */
2136           TapeTogglePause();
2137         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
2138         {
2139           tape.fast_forward = TRUE;
2140           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
2141         }
2142         else if (!tape.pause_before_death)      /* FFWD PLAY -> + AUTO PAUSE */
2143         {
2144           tape.pause_before_death = TRUE;
2145           DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
2146         }
2147         else                                    /* -> NORMAL PLAY */
2148         {
2149           tape.fast_forward = FALSE;
2150           tape.pause_before_death = FALSE;
2151           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_OFF, 0);
2152         }
2153       }
2154       break;
2155
2156     default:
2157       break;
2158   }
2159
2160   BackToFront();
2161 }
2162
2163 void HandleSoundButtons(int mx, int my, int button)
2164 {
2165   if (game_status != PLAYING)
2166     return;
2167
2168   switch(CheckSoundButtons(mx,my,button))
2169   {
2170     case BUTTON_SOUND_MUSIC:
2171       if (setup.sound_music)
2172       { 
2173         setup.sound_music = FALSE;
2174         FadeSound(background_loop[level_nr % num_bg_loops]);
2175         DrawSoundDisplay(BUTTON_SOUND_MUSIC_OFF);
2176       }
2177       else if (sound_loops_allowed)
2178       { 
2179         setup.sound = setup.sound_music = TRUE;
2180         PlaySoundLoop(background_loop[level_nr % num_bg_loops]);
2181         DrawSoundDisplay(BUTTON_SOUND_MUSIC_ON);
2182       }
2183       else
2184         DrawSoundDisplay(BUTTON_SOUND_MUSIC_OFF);
2185       break;
2186
2187     case BUTTON_SOUND_LOOPS:
2188       if (setup.sound_loops)
2189       { 
2190         setup.sound_loops = FALSE;
2191         DrawSoundDisplay(BUTTON_SOUND_LOOPS_OFF);
2192       }
2193       else if (sound_loops_allowed)
2194       { 
2195         setup.sound = setup.sound_loops = TRUE;
2196         DrawSoundDisplay(BUTTON_SOUND_LOOPS_ON);
2197       }
2198       else
2199         DrawSoundDisplay(BUTTON_SOUND_LOOPS_OFF);
2200       break;
2201
2202     case BUTTON_SOUND_SIMPLE:
2203       if (setup.sound_simple)
2204       { 
2205         setup.sound_simple = FALSE;
2206         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_OFF);
2207       }
2208       else if (sound_status==SOUND_AVAILABLE)
2209       { 
2210         setup.sound = setup.sound_simple = TRUE;
2211         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_ON);
2212       }
2213       else
2214         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_OFF);
2215       break;
2216
2217     default:
2218       break;
2219   }
2220
2221   BackToFront();
2222 }
2223
2224 void HandleGameButtons(int mx, int my, int button)
2225 {
2226   if (game_status != PLAYING)
2227     return;
2228
2229   switch(CheckGameButtons(mx,my,button))
2230   {
2231     case BUTTON_GAME_STOP:
2232       if (AllPlayersGone)
2233       {
2234         CloseDoor(DOOR_CLOSE_1);
2235         game_status = MAINMENU;
2236         DrawMainMenu();
2237         break;
2238       }
2239
2240       if (Request("Do you really want to quit the game ?",
2241                   REQ_ASK | REQ_STAY_CLOSED))
2242       { 
2243 #ifndef MSDOS
2244         if (options.network)
2245           SendToServer_StopPlaying();
2246         else
2247 #endif
2248         {
2249           game_status = MAINMENU;
2250           DrawMainMenu();
2251         }
2252       }
2253       else
2254         OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
2255       break;
2256
2257     case BUTTON_GAME_PAUSE:
2258       if (options.network)
2259       {
2260 #ifndef MSDOS
2261         if (tape.pausing)
2262           SendToServer_ContinuePlaying();
2263         else
2264           SendToServer_PausePlaying();
2265 #endif
2266       }
2267       else
2268         TapeTogglePause();
2269       break;
2270
2271     case BUTTON_GAME_PLAY:
2272       if (tape.pausing)
2273       {
2274 #ifndef MSDOS
2275         if (options.network)
2276           SendToServer_ContinuePlaying();
2277         else
2278 #endif
2279         {
2280           tape.pausing = FALSE;
2281           DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
2282         }
2283       }
2284       break;
2285
2286     default:
2287       break;
2288   }
2289
2290   BackToFront();
2291 }