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