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