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