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