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