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