rnd-19990125-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   DrawText(SX+16, SY+16, "SETUP",FS_BIG,FC_YELLOW);
1040
1041   for(i=SETUP_SCREEN_POS_START;i<=SETUP_SCREEN_POS_END;i++)
1042   {
1043     int base = i - SETUP_SCREEN_POS_START;
1044
1045     if (!(i >= SETUP_SCREEN_POS_EMPTY1 && i <= SETUP_SCREEN_POS_EMPTY2))
1046     {
1047       DrawGraphic(0,i,GFX_KUGEL_BLAU);
1048       DrawText(SX+32,SY+i*32, setup_info[base].text, FS_BIG,FC_GREEN);
1049     }
1050
1051     if (setup_info[base].value)
1052     {
1053       int setting_value = *setup_info[base].value;
1054
1055       DrawText(SX+14*32, SY+i*32, (setting_value ? "on" : "off"),
1056                FS_BIG, (setting_value ? FC_YELLOW : FC_BLUE));
1057     }
1058   }
1059
1060   FadeToFront();
1061   InitAnimation();
1062   HandleSetupScreen(0,0,0,0,MB_MENU_INITIALIZE);
1063 }
1064
1065 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
1066 {
1067   static int choice = 3;
1068   static int redraw = TRUE;
1069   int x = (mx+32-SX)/32, y = (my+32-SY)/32;
1070   int pos_start  = SETUP_SCREEN_POS_START  + 1;
1071   int pos_empty1 = SETUP_SCREEN_POS_EMPTY1 + 1;
1072   int pos_empty2 = SETUP_SCREEN_POS_EMPTY2 + 1;
1073   int pos_end    = SETUP_SCREEN_POS_END    + 1;
1074
1075   if (button == MB_MENU_INITIALIZE)
1076     redraw = TRUE;
1077
1078   if (redraw)
1079   {
1080     DrawGraphic(0,choice-1,GFX_KUGEL_ROT);
1081     redraw = FALSE;
1082   }
1083
1084   if (button == MB_MENU_INITIALIZE)
1085     return;
1086
1087   if (dx || dy)
1088   {
1089     if (dy)
1090     {
1091       x = 1;
1092       y = choice+dy;
1093     }
1094     else
1095       x = y = 0;
1096
1097     if (y >= pos_empty1 && y <= pos_empty2)
1098       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
1099
1100     if (y < pos_start)
1101       y = pos_start;
1102     else if (y > pos_end)
1103       y = pos_end;
1104   }
1105
1106   if (!mx && !my && !dx && !dy)
1107   {
1108     x = 1;
1109     y = choice;
1110   }
1111
1112   if (x==1 && y >= pos_start && y <= pos_end &&
1113       !(y >= pos_empty1 && y <= pos_empty2))
1114   {
1115     if (button)
1116     {
1117       if (y!=choice)
1118       {
1119         DrawGraphic(0,y-1,GFX_KUGEL_ROT);
1120         DrawGraphic(0,choice-1,GFX_KUGEL_BLAU);
1121       }
1122       choice = y;
1123     }
1124     else
1125     {
1126       int yy = y-1;
1127
1128       if (y==3 && sound_status==SOUND_AVAILABLE)
1129       {
1130         if (setup.sound)
1131         {
1132           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1133           DrawText(SX+14*32, SY+(yy+1)*32,"off",FS_BIG,FC_BLUE);
1134           DrawText(SX+14*32, SY+(yy+2)*32,"off",FS_BIG,FC_BLUE);
1135           setup.sound_loops = FALSE;
1136           setup.sound_music = FALSE;
1137         }
1138         else
1139           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1140         setup.sound = !setup.sound;
1141       }
1142       else if (y==4 && sound_loops_allowed)
1143       {
1144         if (setup.sound_loops)
1145           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1146         else
1147         {
1148           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1149           DrawText(SX+14*32, SY+(yy-1)*32,"on ",FS_BIG,FC_YELLOW);
1150           setup.sound = TRUE;
1151         }
1152         setup.sound_loops = !setup.sound_loops;
1153       }
1154       else if (y==5 && sound_loops_allowed)
1155       {
1156         if (setup.sound_music)
1157           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1158         else
1159         {
1160           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1161           DrawText(SX+14*32, SY+(yy-2)*32,"on ",FS_BIG,FC_YELLOW);
1162           setup.sound = TRUE;
1163         }
1164         setup.sound_music = !setup.sound_music;
1165       }
1166       else if (y==6)
1167       {
1168         if (setup.toons)
1169           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1170         else
1171           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1172         setup.toons = !setup.toons;
1173       }
1174       else if (y==7)
1175       {
1176 #if 0
1177         if (setup.double_buffering)
1178           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1179         else
1180           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1181         setup.double_buffering = !setup.double_buffering;
1182         setup.direct_draw = !setup.double_buffering;
1183 #else
1184         DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1185         setup.double_buffering = TRUE;
1186         setup.direct_draw = !setup.double_buffering;
1187 #endif
1188       }
1189       else if (y==8)
1190       {
1191         if (setup.scroll_delay)
1192           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1193         else
1194           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1195         setup.scroll_delay = !setup.scroll_delay;
1196       }
1197       else if (y==9)
1198       {
1199         if (setup.soft_scrolling)
1200           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1201         else
1202           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1203         setup.soft_scrolling = !setup.soft_scrolling;
1204       }
1205       else if (y==10)
1206       {
1207         if (setup.fading)
1208           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1209         else
1210           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1211         setup.fading = !setup.fading;
1212       }
1213       else if (y==11)
1214       {
1215         if (setup.quick_doors)
1216           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1217         else
1218           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1219         setup.quick_doors = !setup.quick_doors;
1220       }
1221       else if (y==12)
1222       {
1223         if (setup.autorecord)
1224           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1225         else
1226           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1227         setup.autorecord = !setup.autorecord;
1228       }
1229       else if (y==13)
1230       {
1231         if (setup.team_mode)
1232           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1233         else
1234           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1235         setup.team_mode = !setup.team_mode;
1236       }
1237       else if (y==14)
1238       {
1239         game_status = SETUPINPUT;
1240         DrawSetupInputScreen();
1241         redraw = TRUE;
1242       }
1243       else if (y==pos_end-1 || y==pos_end)
1244       {
1245         if (y==pos_end)
1246         {
1247           SaveSetup();
1248
1249           /*
1250           SaveJoystickData();
1251           */
1252
1253 #ifdef MSDOS
1254           save_joystick_data(JOYSTICK_FILENAME);
1255 #endif
1256
1257
1258         }
1259
1260         game_status = MAINMENU;
1261         DrawMainMenu();
1262         redraw = TRUE;
1263       }
1264     }
1265   }
1266   BackToFront();
1267
1268   if (game_status==SETUP)
1269     DoAnimation();
1270 }
1271
1272 void DrawSetupInputScreen()
1273 {
1274   ClearWindow();
1275   DrawText(SX+16, SY+16, "SETUP INPUT", FS_BIG, FC_YELLOW);
1276
1277   DrawGraphic(0, 2, GFX_KUGEL_BLAU);
1278   DrawGraphic(0, 3, GFX_KUGEL_BLAU);
1279   DrawGraphic(0, 4, GFX_KUGEL_BLAU);
1280   DrawGraphic(0, 15, GFX_KUGEL_BLAU);
1281   DrawGraphic(10, 2, GFX_PFEIL_L);
1282   DrawGraphic(12, 2, GFX_PFEIL_R);
1283
1284   DrawText(SX+32, SY+2*32, "Player:", FS_BIG, FC_GREEN);
1285   DrawText(SX+32, SY+3*32, "Device:", FS_BIG, FC_GREEN);
1286   DrawText(SX+32, SY+15*32, "Exit", FS_BIG, FC_GREEN);
1287
1288   DrawTextFCentered(SYSIZE - 20, FC_BLUE,
1289                     "Joysticks deactivated on this screen");
1290
1291   HandleSetupInputScreen(0,0, 0,0, MB_MENU_INITIALIZE);
1292   FadeToFront();
1293   InitAnimation();
1294 }
1295
1296 static void setJoystickDeviceToNr(char *device_name, int device_nr)
1297 {
1298   if (device_name == NULL)
1299     return;
1300
1301   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
1302     device_nr = 0;
1303
1304   if (strlen(device_name) > 1)
1305   {
1306     char c1 = device_name[strlen(device_name) - 1];
1307     char c2 = device_name[strlen(device_name) - 2];
1308
1309     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
1310       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
1311   }
1312   else
1313     strncpy(device_name, joystick_device_name[device_nr], strlen(device_name));
1314 }
1315
1316 static void drawPlayerSetupInputInfo(int player_nr)
1317 {
1318   int i;
1319   static struct SetupKeyboardInfo custom_key;
1320   static struct
1321   {
1322     KeySym *keysym;
1323     char *text;
1324   } custom[] =
1325   {
1326     { &custom_key.left,  "Joystick Left"  },
1327     { &custom_key.right, "Joystick Right" },
1328     { &custom_key.up,    "Joystick Up"    },
1329     { &custom_key.down,  "Joystick Down"  },
1330     { &custom_key.snap,  "Button 1"       },
1331     { &custom_key.bomb,  "Button 2"       }
1332   };
1333   static char *joystick_name[MAX_PLAYERS] =
1334   {
1335     "Joystick1",
1336     "Joystick2",
1337     "Joystick3",
1338     "Joystick4"
1339   };
1340
1341   custom_key = setup.input[player_nr].key;
1342
1343   DrawText(SX+11*32, SY+2*32, int2str(player_nr + 1, 1), FS_BIG, FC_RED);
1344   DrawGraphic(8, 2, GFX_SPIELER1 + player_nr);
1345
1346   if (setup.input[player_nr].use_joystick)
1347   {
1348     char *device_name = setup.input[player_nr].joy.device_name;
1349
1350     DrawText(SX+8*32, SY+3*32,
1351              joystick_name[getJoystickNrFromDeviceName(device_name)],
1352              FS_BIG, FC_YELLOW);
1353     DrawText(SX+32, SY+4*32, "Calibrate", FS_BIG, FC_GREEN);
1354   }
1355   else
1356   {
1357     DrawText(SX+8*32, SY+3*32, "Keyboard ", FS_BIG, FC_YELLOW);
1358     DrawText(SX+32, SY+4*32, "Customize", FS_BIG, FC_GREEN);
1359   }
1360
1361   DrawText(SX+32, SY+5*32, "Actual Settings:", FS_BIG, FC_GREEN);
1362   DrawGraphic(1, 6, GFX_PFEIL_L);
1363   DrawGraphic(1, 7, GFX_PFEIL_R);
1364   DrawGraphic(1, 8, GFX_PFEIL_O);
1365   DrawGraphic(1, 9, GFX_PFEIL_U);
1366   DrawText(SX+2*32, SY+6*32, ":", FS_BIG, FC_BLUE);
1367   DrawText(SX+2*32, SY+7*32, ":", FS_BIG, FC_BLUE);
1368   DrawText(SX+2*32, SY+8*32, ":", FS_BIG, FC_BLUE);
1369   DrawText(SX+2*32, SY+9*32, ":", FS_BIG, FC_BLUE);
1370   DrawText(SX+32, SY+10*32, "Snap Field:", FS_BIG, FC_BLUE);
1371   DrawText(SX+32, SY+12*32, "Place Bomb:", FS_BIG, FC_BLUE);
1372
1373   for (i=0; i<6; i++)
1374   {
1375     int ypos = 6 + i + (i > 3 ? i-3 : 0);
1376
1377     DrawText(SX + 3*32, SY + ypos*32,
1378              "              ", FS_BIG, FC_YELLOW);
1379     DrawText(SX + 3*32, SY + ypos*32,
1380              (setup.input[player_nr].use_joystick ?
1381               custom[i].text :
1382               getKeyNameFromKeySym(*custom[i].keysym)),
1383              FS_BIG, FC_YELLOW);
1384   }
1385 }
1386
1387 void HandleSetupInputScreen(int mx, int my, int dx, int dy, int button)
1388 {
1389   static int choice = 3;
1390   static int player_nr = 0;
1391   static int redraw = TRUE;
1392   int x = (mx+32-SX)/32, y = (my+32-SY)/32;
1393   int pos_start  = SETUPINPUT_SCREEN_POS_START  + 1;
1394   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1 + 1;
1395   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2 + 1;
1396   int pos_end    = SETUPINPUT_SCREEN_POS_END    + 1;
1397
1398   if (button == MB_MENU_INITIALIZE)
1399   {
1400     drawPlayerSetupInputInfo(player_nr);
1401     redraw = TRUE;
1402   }
1403
1404   if (redraw)
1405   {
1406     DrawGraphic(0,choice-1,GFX_KUGEL_ROT);
1407     redraw = FALSE;
1408   }
1409
1410   if (button == MB_MENU_INITIALIZE)
1411     return;
1412
1413   if (dx || dy)
1414   {
1415     if (dx && choice == 3)
1416     {
1417       x = (dx < 0 ? 11 : 13);
1418       y = 3;
1419     }
1420     else if (dx && choice == 4)
1421     {
1422       button = MB_MENU_CHOICE;
1423       x = 1;
1424       y = 4;
1425     }
1426     else if (dy)
1427     {
1428       x = 1;
1429       y = choice + dy;
1430     }
1431     else
1432       x = y = 0;
1433
1434     if (y >= pos_empty1 && y <= pos_empty2)
1435       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
1436
1437     if (y < pos_start)
1438       y = pos_start;
1439     else if (y > pos_end)
1440       y = pos_end;
1441   }
1442
1443   if (!mx && !my && !dx && !dy)
1444   {
1445     x = 1;
1446     y = choice;
1447   }
1448
1449   if (y == 3 && ((x == 1 && !button) || ((x == 11 || x == 13) && button)))
1450   {
1451     static unsigned long delay = 0;
1452
1453     if (!DelayReached(&delay, GADGET_FRAME_DELAY))
1454       goto out;
1455
1456     player_nr = (player_nr + (x == 11 ? -1 : +1) + MAX_PLAYERS) % MAX_PLAYERS;
1457
1458     drawPlayerSetupInputInfo(player_nr);
1459   }
1460   else if (x==1 && y >= pos_start && y <= pos_end &&
1461            !(y >= pos_empty1 && y <= pos_empty2))
1462   {
1463     if (button)
1464     {
1465       if (y != choice)
1466       {
1467         DrawGraphic(0, y-1, GFX_KUGEL_ROT);
1468         DrawGraphic(0, choice-1, GFX_KUGEL_BLAU);
1469       }
1470       choice = y;
1471     }
1472     else
1473     {
1474       if (y == 4)
1475       {
1476         char *device_name = setup.input[player_nr].joy.device_name;
1477
1478         if (!setup.input[player_nr].use_joystick)
1479         {
1480           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
1481
1482           setJoystickDeviceToNr(device_name, new_device_nr);
1483           setup.input[player_nr].use_joystick = TRUE;
1484         }
1485         else
1486         {
1487           int device_nr = getJoystickNrFromDeviceName(device_name);
1488           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
1489
1490           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
1491             setup.input[player_nr].use_joystick = FALSE;
1492           else
1493             setJoystickDeviceToNr(device_name, new_device_nr);
1494         }
1495
1496
1497         /*
1498         InitJoysticks();
1499         */
1500
1501
1502 #if 0
1503         int one_joystick_nr       = (dx >= 0 ? 0 : 1);
1504         int the_other_joystick_nr = (dx >= 0 ? 1 : 0);
1505
1506         if (setup.input[player_nr].use_joystick)
1507         {
1508           if (setup.input[player_nr].joystick_nr == one_joystick_nr)
1509             setup.input[player_nr].joystick_nr = the_other_joystick_nr;
1510           else
1511             setup.input[player_nr].use_joystick = FALSE;
1512         }
1513         else
1514         {
1515           setup.input[player_nr].use_joystick = TRUE;
1516           setup.input[player_nr].joystick_nr = one_joystick_nr;
1517         }
1518 #endif
1519
1520         drawPlayerSetupInputInfo(player_nr);
1521       }
1522       else if (y == 5)
1523       {
1524         if (setup.input[player_nr].use_joystick)
1525         {
1526           InitJoysticks();
1527           game_status = CALIBRATION;
1528           CalibrateJoystick(player_nr);
1529           game_status = SETUPINPUT;
1530         }
1531         else
1532           CustomizeKeyboard(player_nr);
1533
1534         redraw = TRUE;
1535       }
1536       else if (y == pos_end)
1537       {
1538         InitJoysticks();
1539
1540         game_status = SETUP;
1541         DrawSetupScreen();
1542         redraw = TRUE;
1543       }
1544     }
1545   }
1546   BackToFront();
1547
1548   out:
1549
1550   if (game_status == SETUPINPUT)
1551     DoAnimation();
1552 }
1553
1554 void CustomizeKeyboard(int player_nr)
1555 {
1556   int i;
1557   int step_nr;
1558   boolean finished = FALSE;
1559   static struct SetupKeyboardInfo custom_key;
1560   static struct
1561   {
1562     KeySym *keysym;
1563     char *text;
1564   } customize_step[] =
1565   {
1566     { &custom_key.left,  "Move Left"  },
1567     { &custom_key.right, "Move Right" },
1568     { &custom_key.up,    "Move Up"    },
1569     { &custom_key.down,  "Move Down"  },
1570     { &custom_key.snap,  "Snap Field" },
1571     { &custom_key.bomb,  "Place Bomb" }
1572   };
1573
1574   /* read existing key bindings from player setup */
1575   custom_key = setup.input[player_nr].key;
1576
1577   ClearWindow();
1578   DrawText(SX + 16, SY + 16, "Keyboard Input", FS_BIG, FC_YELLOW);
1579
1580   BackToFront();
1581   InitAnimation();
1582
1583   step_nr = 0;
1584   DrawText(SX, SY + (2+2*step_nr)*32,
1585            customize_step[step_nr].text, FS_BIG, FC_RED);
1586   DrawText(SX, SY + (2+2*step_nr+1)*32,
1587            "Key:", FS_BIG, FC_RED);
1588   DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
1589            getKeyNameFromKeySym(*customize_step[step_nr].keysym),
1590            FS_BIG, FC_BLUE);
1591
1592   while(!finished)
1593   {
1594     if (XPending(display))      /* got event from X server */
1595     {
1596       XEvent event;
1597
1598       XNextEvent(display, &event);
1599
1600       switch(event.type)
1601       {
1602         case KeyPress:
1603           {
1604             KeySym key = XLookupKeysym((XKeyEvent *)&event,
1605                                        ((XKeyEvent *)&event)->state);
1606
1607             if (key == XK_Escape || (key == XK_Return && step_nr == 6))
1608             {
1609               finished = TRUE;
1610               break;
1611             }
1612
1613             /* press 'Enter' to keep the existing key binding */
1614             if (key == XK_Return || step_nr == 6)
1615               key = *customize_step[step_nr].keysym;
1616
1617             /* check if key already used */
1618             for (i=0; i<step_nr; i++)
1619               if (*customize_step[i].keysym == key)
1620                 break;
1621             if (i < step_nr)
1622               break;
1623
1624             /* got new key binding */
1625             *customize_step[step_nr].keysym = key;
1626             DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
1627                      "             ", FS_BIG, FC_YELLOW);
1628             DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
1629                      getKeyNameFromKeySym(key), FS_BIG, FC_YELLOW);
1630             step_nr++;
1631
1632             /* un-highlight last query */
1633             DrawText(SX, SY+(2+2*(step_nr-1))*32,
1634                      customize_step[step_nr-1].text, FS_BIG, FC_GREEN);
1635             DrawText(SX, SY+(2+2*(step_nr-1)+1)*32,
1636                      "Key:", FS_BIG, FC_GREEN);
1637
1638             /* press 'Enter' to leave */
1639             if (step_nr == 6)
1640             {
1641               DrawText(SX + 16, SY + 15*32+16,
1642                        "Press Enter", FS_BIG, FC_YELLOW);
1643               break;
1644             }
1645
1646             /* query next key binding */
1647             DrawText(SX, SY+(2+2*step_nr)*32,
1648                      customize_step[step_nr].text, FS_BIG, FC_RED);
1649             DrawText(SX, SY+(2+2*step_nr+1)*32,
1650                      "Key:", FS_BIG, FC_RED);
1651             DrawText(SX + 4*32, SY+(2+2*step_nr+1)*32,
1652                      getKeyNameFromKeySym(*customize_step[step_nr].keysym),
1653                      FS_BIG, FC_BLUE);
1654           }
1655           break;
1656
1657         case KeyRelease:
1658           key_joystick_mapping = 0;
1659           break;
1660
1661         default:
1662           HandleOtherEvents(&event);
1663           break;
1664       }
1665     }
1666
1667     BackToFront();
1668     DoAnimation();
1669
1670     /* don't eat all CPU time */
1671     Delay(10);
1672   }
1673
1674   /* write new key bindings back to player setup */
1675   setup.input[player_nr].key = custom_key;
1676
1677   StopAnimation();
1678   DrawSetupInputScreen();
1679 }
1680
1681 void CalibrateJoystick(int player_nr)
1682 {
1683 #ifdef __FreeBSD__
1684   struct joystick joy_ctrl;
1685 #else
1686   struct joystick_control
1687   {
1688     int buttons;
1689     int x;
1690     int y;
1691   } joy_ctrl;
1692 #endif
1693
1694 #ifndef MSDOS
1695   int new_joystick_xleft = 128, new_joystick_xright = 128;
1696   int new_joystick_yupper = 128, new_joystick_ylower = 128;
1697   int new_joystick_xmiddle, new_joystick_ymiddle;
1698 #else
1699   int calibration_step = 1;
1700 #endif
1701
1702   int joystick_fd = stored_player[player_nr].joystick_fd;
1703   int x, y, last_x, last_y, xpos = 8, ypos = 3;
1704   boolean check[3][3];
1705   int check_remaining;
1706   int joy_value;
1707   int result = -1;
1708
1709   if (joystick_status == JOYSTICK_OFF ||
1710       joystick_fd < 0 ||
1711       !setup.input[player_nr].use_joystick)
1712     goto error_out;
1713
1714   ClearWindow();
1715
1716 #ifndef MSDOS
1717   DrawText(SX,      SY +  6*32, " ROTATE JOYSTICK ", FS_BIG, FC_YELLOW);
1718   DrawText(SX,      SY +  7*32, "IN ALL DIRECTIONS", FS_BIG, FC_YELLOW);
1719   DrawText(SX + 16, SY +  9*32, "  IF ALL BALLS  ",  FS_BIG, FC_YELLOW);
1720   DrawText(SX,      SY + 10*32, "   ARE YELLOW,   ", FS_BIG, FC_YELLOW);
1721   DrawText(SX,      SY + 11*32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW);
1722   check_remaining = 3 * 3;
1723 #else
1724   DrawText(SX,      SY +  7*32, "  MOVE JOYSTICK  ", FS_BIG, FC_YELLOW);
1725   DrawText(SX + 16, SY +  8*32, "       TO       ",  FS_BIG, FC_YELLOW);
1726   DrawText(SX,      SY +  9*32, " CENTER POSITION ",  FS_BIG, FC_YELLOW);
1727   DrawText(SX,      SY + 10*32, "       AND       ", FS_BIG, FC_YELLOW);
1728   DrawText(SX,      SY + 11*32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW);
1729   check_remaining = 0;
1730 #endif
1731
1732   for(y=0; y<3; y++)
1733   {
1734     for(x=0; x<3; x++)
1735     {
1736       check[x][y] = FALSE;
1737       DrawGraphic(xpos + x - 1, ypos + y - 1, GFX_KUGEL_BLAU);
1738     }
1739   }
1740
1741   joy_value = Joystick(player_nr);
1742   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
1743   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
1744   DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_ROT);
1745
1746   BackToFront();
1747
1748 #ifdef __FreeBSD__
1749   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1750 #else
1751   joy_ctrl.buttons = 0;
1752 #endif
1753
1754   while(Joystick(player_nr) & JOY_BUTTON);
1755
1756   InitAnimation();
1757
1758   while(result < 0)
1759   {
1760     if (XPending(display))      /* got event from X server */
1761     {
1762       XEvent event;
1763
1764       XNextEvent(display, &event);
1765
1766       switch(event.type)
1767       {
1768         case KeyPress:
1769           switch(XLookupKeysym((XKeyEvent *)&event,
1770                                ((XKeyEvent *)&event)->state))
1771           {
1772             case XK_Return:
1773               if (check_remaining == 0)
1774                 result = 1;
1775               break;
1776
1777             case XK_Escape:
1778               result = 0;
1779               break;
1780
1781             default:
1782               break;
1783           }
1784           break;
1785
1786         case KeyRelease:
1787           key_joystick_mapping = 0;
1788           break;
1789
1790         default:
1791           HandleOtherEvents(&event);
1792           break;
1793       }
1794     }
1795
1796 #ifndef MSDOS
1797     if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1798     {
1799       joystick_status = JOYSTICK_OFF;
1800       goto error_out;
1801     }
1802
1803     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_ctrl.x);
1804     new_joystick_xright = MAX(new_joystick_xright, joy_ctrl.x);
1805     new_joystick_yupper = MIN(new_joystick_yupper, joy_ctrl.y);
1806     new_joystick_ylower = MAX(new_joystick_ylower, joy_ctrl.y);
1807
1808     new_joystick_xmiddle =
1809       new_joystick_xleft + (new_joystick_xright - new_joystick_xleft) / 2;
1810     new_joystick_ymiddle =
1811       new_joystick_yupper + (new_joystick_ylower - new_joystick_yupper) / 2;
1812
1813     setup.input[player_nr].joy.xleft = new_joystick_xleft;
1814     setup.input[player_nr].joy.yupper = new_joystick_yupper;
1815     setup.input[player_nr].joy.xright = new_joystick_xright;
1816     setup.input[player_nr].joy.ylower = new_joystick_ylower;
1817     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
1818     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
1819
1820     CheckJoystickData();
1821 #endif
1822
1823     joy_value = Joystick(player_nr);
1824
1825     if (joy_value & JOY_BUTTON && check_remaining == 0)
1826     {
1827       result = 1;
1828
1829 #ifdef MSDOS
1830       if (calibration_step == 1)
1831       {
1832         remove_joystick();
1833         InitJoysticks();
1834       }
1835       else if (calibrate_joystick(joystick_fd) != 0)
1836       {
1837         joystick_status = JOYSTICK_OFF;
1838         goto error_out;
1839       }
1840
1841       if (joy[joystick_fd].flags & JOYFLAG_CALIBRATE)
1842       {
1843         calibration_step++;
1844         result = -1;
1845
1846         DrawText(SX,      SY +  7*32, "  MOVE JOYSTICK  ", FS_BIG, FC_YELLOW);
1847         DrawText(SX + 16, SY +  8*32, "       TO       ",  FS_BIG, FC_YELLOW);
1848
1849         if (calibration_step == 2)
1850           DrawText(SX + 16, SY + 9*32," THE UPPER LEFT ",  FS_BIG, FC_YELLOW);
1851         else
1852           DrawText(SX,      SY + 9*32," THE LOWER RIGHT ", FS_BIG, FC_YELLOW);
1853
1854         DrawText(SX,      SY + 10*32, "       AND       ", FS_BIG, FC_YELLOW);
1855         DrawText(SX,      SY + 11*32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW);
1856
1857         BackToFront();
1858
1859         while(Joystick(player_nr) & JOY_BUTTON)
1860           DoAnimation();
1861       }
1862 #endif
1863     }
1864
1865     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
1866     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
1867
1868     if (x != last_x || y != last_y)
1869     {
1870 #ifndef MSDOS
1871       DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_GELB);
1872 #else
1873       DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_BLAU);
1874 #endif
1875       DrawGraphic(xpos + x,      ypos + y,      GFX_KUGEL_ROT);
1876
1877       last_x = x;
1878       last_y = y;
1879
1880       if (check_remaining > 0 && !check[x+1][y+1])
1881       {
1882         check[x+1][y+1] = TRUE;
1883         check_remaining--;
1884       }
1885
1886 #if 0
1887       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
1888              setup.input[player_nr].joy.xleft,
1889              setup.input[player_nr].joy.xmiddle,
1890              setup.input[player_nr].joy.xright);
1891       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
1892              setup.input[player_nr].joy.yupper,
1893              setup.input[player_nr].joy.ymiddle,
1894              setup.input[player_nr].joy.ylower);
1895 #endif
1896
1897     }
1898
1899     BackToFront();
1900     DoAnimation();
1901
1902     /* don't eat all CPU time */
1903     Delay(10);
1904   }
1905
1906   StopAnimation();
1907
1908   DrawSetupInputScreen();
1909   while(Joystick(player_nr) & JOY_BUTTON);
1910   return;
1911
1912   error_out:
1913
1914   ClearWindow();
1915   DrawText(SX + 16, SY + 6*32, "  JOYSTICK NOT  ", FS_BIG, FC_YELLOW);
1916   DrawText(SX,      SY + 7*32, "    AVAILABLE    ", FS_BIG, FC_YELLOW);
1917   BackToFront();
1918   Delay(2000);
1919   DrawSetupInputScreen();
1920 }
1921
1922
1923
1924 #if 0
1925
1926 void CalibrateJoystick_OLD()
1927 {
1928 #ifdef __FreeBSD__
1929   struct joystick joy_ctrl;
1930 #else
1931   struct joystick_control
1932   {
1933     int buttons;
1934     int x;
1935     int y;
1936   } joy_ctrl;
1937 #endif
1938
1939 #ifdef MSDOS
1940   char joy_nr[4];
1941 #endif
1942
1943   int joystick_nr = setup.input[0].joystick_nr;
1944   int new_joystick_xleft, new_joystick_xright, new_joystick_xmiddle;
1945   int new_joystick_yupper, new_joystick_ylower, new_joystick_ymiddle;
1946
1947   if (joystick_status == JOYSTICK_OFF)
1948     goto error_out;
1949
1950 #ifndef MSDOS
1951   ClearWindow();
1952   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1953   DrawText(SX+16, SY+8*32, " THE UPPER LEFT ",FS_BIG,FC_YELLOW);
1954   DrawText(SX+16, SY+9*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1955   BackToFront();
1956
1957 #ifdef __FreeBSD__
1958   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1959 #else
1960   joy_ctrl.buttons = 0;
1961 #endif
1962   while(Joystick() & JOY_BUTTON);
1963 #ifdef __FreeBSD__
1964   while(!(joy_ctrl.b1 || joy_ctrl.b2))
1965 #else
1966   while(!joy_ctrl.buttons)
1967 #endif
1968   {
1969     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1970     {
1971       joystick_status=JOYSTICK_OFF;
1972       goto error_out;
1973     }
1974     Delay(10);
1975   }
1976
1977   new_joystick_xleft = joy_ctrl.x;
1978   new_joystick_yupper = joy_ctrl.y;
1979
1980   ClearWindow();
1981   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1982   DrawText(SX+32, SY+8*32, "THE LOWER RIGHT",FS_BIG,FC_YELLOW);
1983   DrawText(SX+16, SY+9*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1984   BackToFront();
1985
1986 #ifdef __FreeBSD__
1987   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1988 #else
1989   joy_ctrl.buttons = 0;
1990 #endif
1991   while(Joystick() & JOY_BUTTON);
1992 #ifdef __FreeBSD__
1993   while(!(joy_ctrl.b1 || joy_ctrl.b2))
1994 #else
1995   while(!joy_ctrl.buttons)
1996 #endif
1997   {
1998     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1999     {
2000       joystick_status=JOYSTICK_OFF;
2001       goto error_out;
2002     }
2003     Delay(10);
2004   }
2005
2006   new_joystick_xright = joy_ctrl.x;
2007   new_joystick_ylower = joy_ctrl.y;
2008
2009   ClearWindow();
2010   DrawText(SX+32, SY+16+7*32, "CENTER JOYSTICK",FS_BIG,FC_YELLOW);
2011   DrawText(SX+16, SY+16+8*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
2012   BackToFront();
2013
2014 #ifdef __FreeBSD__
2015   joy_ctrl.b1 = joy_ctrl.b2 = 0;
2016 #else
2017   joy_ctrl.buttons = 0;
2018 #endif
2019   while(Joystick() & JOY_BUTTON);
2020 #ifdef __FreeBSD__
2021   while(!(joy_ctrl.b1 || joy_ctrl.b2))
2022 #else
2023   while(!joy_ctrl.buttons)
2024 #endif
2025   {
2026     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
2027     {
2028       joystick_status=JOYSTICK_OFF;
2029       goto error_out;
2030     }
2031     Delay(10);
2032   }
2033
2034   new_joystick_xmiddle = joy_ctrl.x;
2035   new_joystick_ymiddle = joy_ctrl.y;
2036
2037   setup.input[player_nr].joy.xleft = new_joystick_xleft;
2038   setup.input[player_nr].joy.yupper = new_joystick_yupper;
2039   setup.input[player_nr].joy.xright = new_joystick_xright;
2040   setup.input[player_nr].joy.ylower = new_joystick_ylower;
2041   setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
2042   setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
2043
2044   CheckJoystickData();
2045
2046   DrawSetupScreen();
2047   while(Joystick() & JOY_BUTTON);
2048   return;
2049
2050 #endif
2051   error_out:
2052
2053 #ifdef MSDOS
2054   joy_nr[0] = '#';
2055   joy_nr[1] = SETUP_2ND_JOYSTICK_ON(local_player->setup)+49;
2056   joy_nr[2] = '\0';
2057
2058   remove_joystick();
2059   ClearWindow();
2060   DrawText(SX+32, SY+7*32, "CENTER JOYSTICK",FS_BIG,FC_YELLOW);
2061   DrawText(SX+16+7*32, SY+8*32, joy_nr, FS_BIG,FC_YELLOW);
2062   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
2063   BackToFront();
2064
2065   for(clear_keybuf();!keypressed(););
2066   install_joystick(JOY_TYPE_2PADS);
2067
2068   ClearWindow();
2069   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
2070   DrawText(SX+16, SY+8*32, " THE UPPER LEFT ",FS_BIG,FC_YELLOW);
2071   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
2072   BackToFront();
2073
2074   for(clear_keybuf();!keypressed(););
2075   calibrate_joystick(SETUP_2ND_JOYSTICK_ON(local_player->setup));
2076
2077   ClearWindow();
2078   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
2079   DrawText(SX+32, SY+8*32, "THE LOWER RIGHT",FS_BIG,FC_YELLOW);
2080   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
2081   BackToFront();
2082
2083   for(clear_keybuf();!keypressed(););
2084   calibrate_joystick(SETUP_2ND_JOYSTICK_ON(local_player->setup));
2085
2086   DrawSetupScreen();
2087   return;
2088 #endif
2089
2090   ClearWindow();
2091   DrawText(SX+16, SY+16, "NO JOYSTICK",FS_BIG,FC_YELLOW);
2092   DrawText(SX+16, SY+48, " AVAILABLE ",FS_BIG,FC_YELLOW);
2093   BackToFront();
2094   Delay(3000);
2095   DrawSetupScreen();
2096 }
2097
2098 #endif
2099
2100
2101
2102 void HandleGameActions()
2103 {
2104   if (game_status != PLAYING)
2105     return;
2106
2107   if (local_player->LevelSolved)
2108     GameWon();
2109
2110   if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
2111     TapeStop();
2112
2113   GameActions();
2114
2115   BackToFront();
2116 }
2117
2118 void HandleVideoButtons(int mx, int my, int button)
2119 {
2120   return;
2121
2122
2123
2124
2125   if (game_status != MAINMENU && game_status != PLAYING)
2126     return;
2127
2128   switch(CheckVideoButtons(mx,my,button))
2129   {
2130     case BUTTON_VIDEO_EJECT:
2131       TapeStop();
2132       if (TAPE_IS_EMPTY(tape))
2133       {
2134         LoadTape(level_nr);
2135         if (TAPE_IS_EMPTY(tape))
2136           Request("No tape for this level !",REQ_CONFIRM);
2137       }
2138       else
2139       {
2140         if (tape.changed)
2141           SaveTape(tape.level_nr);
2142         TapeErase();
2143       }
2144       DrawCompleteVideoDisplay();
2145       break;
2146
2147     case BUTTON_VIDEO_STOP:
2148       TapeStop();
2149       break;
2150
2151     case BUTTON_VIDEO_PAUSE:
2152       TapeTogglePause();
2153       break;
2154
2155     case BUTTON_VIDEO_REC:
2156       if (TAPE_IS_STOPPED(tape))
2157       {
2158         TapeStartRecording();
2159
2160 #ifndef MSDOS
2161         if (options.network)
2162           SendToServer_StartPlaying();
2163         else
2164 #endif
2165         {
2166           game_status = PLAYING;
2167           InitGame();
2168         }
2169       }
2170       else if (tape.pausing)
2171       {
2172         if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
2173         {
2174           tape.pos[tape.counter].delay = tape.delay_played;
2175           tape.playing = FALSE;
2176           tape.recording = TRUE;
2177           tape.changed = TRUE;
2178
2179           DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON,0);
2180         }
2181         else
2182           TapeTogglePause();
2183       }
2184       break;
2185
2186     case BUTTON_VIDEO_PLAY:
2187       if (TAPE_IS_EMPTY(tape))
2188         break;
2189
2190       if (TAPE_IS_STOPPED(tape))
2191       {
2192         TapeStartPlaying();
2193
2194         game_status = PLAYING;
2195         InitGame();
2196       }
2197       else if (tape.playing)
2198       {
2199         if (tape.pausing)                       /* PAUSE -> PLAY */
2200           TapeTogglePause();
2201         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
2202         {
2203           tape.fast_forward = TRUE;
2204           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
2205         }
2206         else if (!tape.pause_before_death)      /* FFWD PLAY -> + AUTO PAUSE */
2207         {
2208           tape.pause_before_death = TRUE;
2209           DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
2210         }
2211         else                                    /* -> NORMAL PLAY */
2212         {
2213           tape.fast_forward = FALSE;
2214           tape.pause_before_death = FALSE;
2215           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_OFF, 0);
2216         }
2217       }
2218       break;
2219
2220     default:
2221       break;
2222   }
2223
2224   BackToFront();
2225 }
2226
2227 void HandleSoundButtons(int mx, int my, int button)
2228 {
2229
2230
2231
2232   return;
2233
2234
2235
2236   if (game_status != PLAYING)
2237     return;
2238
2239   switch(CheckSoundButtons(mx,my,button))
2240   {
2241     case BUTTON_SOUND_MUSIC:
2242       if (setup.sound_music)
2243       { 
2244         setup.sound_music = FALSE;
2245         FadeSound(background_loop[level_nr % num_bg_loops]);
2246         DrawSoundDisplay(BUTTON_SOUND_MUSIC_OFF);
2247       }
2248       else if (sound_loops_allowed)
2249       { 
2250         setup.sound = setup.sound_music = TRUE;
2251         PlaySoundLoop(background_loop[level_nr % num_bg_loops]);
2252         DrawSoundDisplay(BUTTON_SOUND_MUSIC_ON);
2253       }
2254       else
2255         DrawSoundDisplay(BUTTON_SOUND_MUSIC_OFF);
2256       break;
2257
2258     case BUTTON_SOUND_LOOPS:
2259       if (setup.sound_loops)
2260       { 
2261         setup.sound_loops = FALSE;
2262         DrawSoundDisplay(BUTTON_SOUND_LOOPS_OFF);
2263       }
2264       else if (sound_loops_allowed)
2265       { 
2266         setup.sound = setup.sound_loops = TRUE;
2267         DrawSoundDisplay(BUTTON_SOUND_LOOPS_ON);
2268       }
2269       else
2270         DrawSoundDisplay(BUTTON_SOUND_LOOPS_OFF);
2271       break;
2272
2273     case BUTTON_SOUND_SIMPLE:
2274       if (setup.sound_simple)
2275       { 
2276         setup.sound_simple = FALSE;
2277         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_OFF);
2278       }
2279       else if (sound_status==SOUND_AVAILABLE)
2280       { 
2281         setup.sound = setup.sound_simple = TRUE;
2282         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_ON);
2283       }
2284       else
2285         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_OFF);
2286       break;
2287
2288     default:
2289       break;
2290   }
2291
2292   BackToFront();
2293 }
2294
2295 void HandleGameButtons(int mx, int my, int button)
2296 {
2297
2298
2299
2300   return;
2301
2302
2303
2304   if (game_status != PLAYING)
2305     return;
2306
2307   switch(CheckGameButtons(mx,my,button))
2308   {
2309     case BUTTON_GAME_STOP:
2310       if (AllPlayersGone)
2311       {
2312         CloseDoor(DOOR_CLOSE_1);
2313         game_status = MAINMENU;
2314         DrawMainMenu();
2315         break;
2316       }
2317
2318       if (Request("Do you really want to quit the game ?",
2319                   REQ_ASK | REQ_STAY_CLOSED))
2320       { 
2321 #ifndef MSDOS
2322         if (options.network)
2323           SendToServer_StopPlaying();
2324         else
2325 #endif
2326         {
2327           game_status = MAINMENU;
2328           DrawMainMenu();
2329         }
2330       }
2331       else
2332         OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
2333       break;
2334
2335     case BUTTON_GAME_PAUSE:
2336       if (options.network)
2337       {
2338 #ifndef MSDOS
2339         if (tape.pausing)
2340           SendToServer_ContinuePlaying();
2341         else
2342           SendToServer_PausePlaying();
2343 #endif
2344       }
2345       else
2346         TapeTogglePause();
2347       break;
2348
2349     case BUTTON_GAME_PLAY:
2350       if (tape.pausing)
2351       {
2352 #ifndef MSDOS
2353         if (options.network)
2354           SendToServer_ContinuePlaying();
2355         else
2356 #endif
2357         {
2358           tape.pausing = FALSE;
2359           DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
2360         }
2361       }
2362       break;
2363
2364     default:
2365       break;
2366   }
2367
2368   BackToFront();
2369 }