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