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