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