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