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