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