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