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