rnd-19981029-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
48   FadeSounds();
49   GetPlayerConfig();
50   LoadLevel(level_nr);
51
52   ClearWindow();
53   DrawHeadline();
54   DrawText(SX + 32,    SY + 2*32, "Name:", FS_BIG, FC_GREEN);
55   DrawText(SX + 6*32,  SY + 2*32, setup.alias_name, FS_BIG, FC_RED);
56   DrawText(SX + 32,    SY + 3*32, "Level:", FS_BIG, FC_GREEN);
57   DrawText(SX + 11*32, SY + 3*32, int2str(level_nr,3), FS_BIG,
58            (level_nr<leveldir[leveldir_nr].levels ? FC_RED : FC_YELLOW));
59   DrawText(SX + 32,    SY + 4*32, "Hall Of Fame", FS_BIG, FC_GREEN);
60   DrawText(SX + 32,    SY + 5*32, "Level Creator", FS_BIG, FC_GREEN);
61   DrawText(SY + 32,    SY + 6*32, "Info Screen", FS_BIG, FC_GREEN);
62   DrawText(SX + 32,    SY + 7*32, "Start Game", FS_BIG, FC_GREEN);
63   DrawText(SX + 32,    SY + 8*32, "Setup", FS_BIG, FC_GREEN);
64   DrawText(SX + 32,    SY + 9*32, "Quit", FS_BIG, FC_GREEN);
65
66   DrawMicroLevel(MICROLEV_XPOS,MICROLEV_YPOS);
67
68   for(i=2;i<10;i++)
69     DrawGraphic(0,i,GFX_KUGEL_BLAU);
70   DrawGraphic(10,3,GFX_PFEIL_L);
71   DrawGraphic(14,3,GFX_PFEIL_R);
72
73   DrawText(SX+40+16,SY+326,"A Game by Artsoft Entertainment",FS_SMALL,FC_BLUE);
74
75   /*
76   DrawText(SX+40+16,SY+344,"Graphics: Deluxe Paint IV Amiga",
77            FS_SMALL,FC_BLUE);
78   DrawText(SX+60+16,SY+362,"Sounds: AudioMaster IV Amiga",
79            FS_SMALL,FC_BLUE);
80   */
81
82   if (leveldir[leveldir_nr].name)
83   {
84     int len = strlen(leveldir[leveldir_nr].name);
85     int lxpos = SX+(SXSIZE-len*FONT4_XSIZE)/2;
86     int lypos = SY+352;
87
88     DrawText(lxpos,lypos,leveldir[leveldir_nr].name,FS_SMALL,FC_SPECIAL2);
89   }
90
91   FadeToFront();
92   InitAnimation();
93   HandleMainMenu(0,0,0,0,MB_MENU_INITIALIZE);
94
95   TapeStop();
96   if (TAPE_IS_EMPTY(tape))
97     LoadLevelTape(level_nr);
98   DrawCompleteVideoDisplay();
99
100   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
101
102   ClearEventQueue();
103   XAutoRepeatOn(display);
104 }
105
106 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
107 {
108   static int choice = 3;
109   static int redraw = TRUE;
110   int x = (mx+32-SX)/32, y = (my+32-SY)/32;
111
112   if (redraw || button == MB_MENU_INITIALIZE)
113   {
114     DrawGraphic(0,choice-1,GFX_KUGEL_ROT);
115     redraw = FALSE;
116   }
117
118   if (button == MB_MENU_INITIALIZE)
119     return;
120
121   if (dx || dy)
122   {
123     if (dx && choice==4)
124     {
125       x = (dx<0 ? 11 : 15);
126       y = 4;
127     }
128     else if (dy)
129     {
130       x = 1;
131       y = choice+dy;
132     }
133     else
134       x = y = 0;
135
136     if (y<3)
137       y = 3;
138     else if (y>10)
139       y = 10;
140   }
141
142   if (!mx && !my && !dx && !dy)
143   {
144     x = 1;
145     y = choice;
146   }
147
148   if (y==4 && ((x==11 && level_nr>0) ||
149                (x==15 && level_nr<leveldir[leveldir_nr].levels-1)) &&
150       button)
151   {
152     static long level_delay = 0;
153     int step = (button==1 ? 1 : button==2 ? 5 : 10);
154     int new_level_nr, old_level_nr = level_nr;
155
156     new_level_nr = level_nr + (x==11 ? -step : +step);
157     if (new_level_nr<0)
158       new_level_nr = 0;
159     if (new_level_nr>leveldir[leveldir_nr].levels-1)
160       new_level_nr = leveldir[leveldir_nr].levels-1;
161
162     if (old_level_nr==new_level_nr || !DelayReached(&level_delay,150))
163       goto out;
164
165     level_nr = new_level_nr;
166
167     DrawTextExt(drawto,gc,SX+11*32,SY+3*32,
168                 int2str(level_nr,3), FS_BIG,FC_RED);
169     DrawTextExt(window,gc,SX+11*32,SY+3*32,
170                 int2str(level_nr,3), FS_BIG,FC_RED);
171
172     LoadLevel(level_nr);
173     DrawMicroLevel(MICROLEV_XPOS,MICROLEV_YPOS);
174
175     TapeErase();
176     LoadLevelTape(level_nr);
177     DrawCompleteVideoDisplay();
178
179     /* needed because DrawMicroLevel() takes some time */
180     BackToFront();
181     XSync(display,FALSE);
182     DelayReached(&level_delay,0);       /* reset delay counter */
183   }
184   else if (x==1 && y>=3 && y<=10)
185   {
186     if (button)
187     {
188       if (y!=choice)
189       {
190         DrawGraphic(0,y-1,GFX_KUGEL_ROT);
191         DrawGraphic(0,choice-1,GFX_KUGEL_BLAU);
192       }
193       choice = y;
194     }
195     else
196     {
197       if (y==3)
198       {
199         game_status = TYPENAME;
200         HandleTypeName(strlen(setup.alias_name),0);
201       }
202       else if (y==4)
203       {
204         if (num_leveldirs)
205         {
206           game_status = CHOOSELEVEL;
207           SaveLevelSetup();
208           DrawChooseLevel();
209         }
210       }
211       else if (y==5)
212       {
213         game_status = HALLOFFAME;
214         DrawHallOfFame(-1);
215       }
216       else if (y==6)
217       {
218         game_status = LEVELED;
219         if (leveldir[leveldir_nr].readonly)
220           Request("This level is read only !",REQ_CONFIRM);
221         DrawLevelEd();
222       }
223       else if (y==7)
224       {
225         game_status = HELPSCREEN;
226         DrawHelpScreen();
227       }
228       else if (y==8)
229       {
230         if (setup.autorecord)
231           TapeStartRecording();
232
233         if (options.network)
234           SendToServer_StartPlaying();
235         else
236         {
237           game_status = PLAYING;
238           InitGame();
239         }
240       }
241       else if (y==9)
242       {
243         game_status = SETUP;
244         DrawSetupScreen();
245       }
246       else if (y==10)
247       {
248         SaveLevelSetup();
249         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
250           game_status = EXITGAME;
251       }
252
253       if (!button)
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     { NULL,                     "Input Devices" },
909     { NULL,                     ""              },
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         game_status = SETUPINPUT;
1104         DrawSetupInputScreen();
1105         redraw = TRUE;
1106       }
1107       else if (y==pos_end-1 || y==pos_end)
1108       {
1109         if (y==pos_end)
1110         {
1111           SaveSetup();
1112
1113           /*
1114           SaveJoystickData();
1115           */
1116
1117         }
1118
1119         game_status = MAINMENU;
1120         DrawMainMenu();
1121         redraw = TRUE;
1122       }
1123     }
1124   }
1125   BackToFront();
1126
1127   if (game_status==SETUP)
1128     DoAnimation();
1129 }
1130
1131 void DrawSetupInputScreen()
1132 {
1133   ClearWindow();
1134   DrawText(SX+16, SY+16, "SETUP INPUT", FS_BIG, FC_YELLOW);
1135
1136   DrawGraphic(0, 2, GFX_KUGEL_BLAU);
1137   DrawGraphic(0, 3, GFX_KUGEL_BLAU);
1138   DrawGraphic(0, 4, GFX_KUGEL_BLAU);
1139   DrawGraphic(0, 15, GFX_KUGEL_BLAU);
1140   DrawGraphic(10, 2, GFX_PFEIL_L);
1141   DrawGraphic(12, 2, GFX_PFEIL_R);
1142
1143   DrawText(SX+32, SY+2*32, "Player:", FS_BIG, FC_GREEN);
1144   DrawText(SX+32, SY+3*32, "Device:", FS_BIG, FC_GREEN);
1145   DrawText(SX+32, SY+15*32, "Exit", FS_BIG, FC_GREEN);
1146
1147   FadeToFront();
1148   InitAnimation();
1149   HandleSetupInputScreen(0,0,0,0,MB_MENU_INITIALIZE);
1150 }
1151
1152 static int getJoystickNrFromDeviceName(char *device_name)
1153 {
1154   char c;
1155   int joystick_nr = 0;
1156
1157   if (device_name == NULL || device_name[0] == '\0')
1158     return 0;
1159
1160   c = device_name[strlen(device_name) - 1];
1161
1162   if (c >= '0' && c <= '9')
1163     joystick_nr = (int)(c - '0');
1164
1165   if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
1166     joystick_nr = 0;
1167
1168   return joystick_nr;
1169 }
1170
1171 static void setJoystickDeviceToNr(char *device_name, int device_nr)
1172 {
1173   if (device_name == NULL)
1174     return;
1175
1176   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
1177     device_nr = 0;
1178
1179   if (strlen(device_name) > 1)
1180   {
1181     char c1 = device_name[strlen(device_name) - 1];
1182     char c2 = device_name[strlen(device_name) - 2];
1183
1184     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
1185       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
1186   }
1187   else
1188     strcpy(device_name, joystick_device_name[device_nr]);
1189 }
1190
1191 static void drawPlayerSetupInputInfo(int player_nr)
1192 {
1193   int i;
1194   static struct SetupKeyboardInfo custom_key;
1195   static struct
1196   {
1197     KeySym *keysym;
1198     char *text;
1199   } custom[] =
1200   {
1201     { &custom_key.left,  "Joystick Left"  },
1202     { &custom_key.right, "Joystick Right" },
1203     { &custom_key.up,    "Joystick Up"    },
1204     { &custom_key.down,  "Joystick Down"  },
1205     { &custom_key.snap,  "Button 1"       },
1206     { &custom_key.bomb,  "Button 2"       }
1207   };
1208   static char *joystick_name[MAX_PLAYERS] =
1209   {
1210     "Joystick1",
1211     "Joystick2",
1212     "Joystick3",
1213     "Joystick4"
1214   };
1215
1216   custom_key = setup.input[player_nr].key;
1217
1218   DrawText(SX+11*32, SY+2*32, int2str(player_nr + 1, 1), FS_BIG, FC_RED);
1219   DrawGraphic(8, 2, GFX_SPIELER1 + player_nr);
1220
1221   if (setup.input[player_nr].use_joystick)
1222   {
1223     char *device_name = setup.input[player_nr].joy.device_name;
1224
1225     DrawText(SX+8*32, SY+3*32,
1226              joystick_name[getJoystickNrFromDeviceName(device_name)],
1227              FS_BIG, FC_YELLOW);
1228     DrawText(SX+32, SY+4*32, "Calibrate", FS_BIG, FC_GREEN);
1229   }
1230   else
1231   {
1232     DrawText(SX+8*32, SY+3*32, "Keyboard ", FS_BIG, FC_YELLOW);
1233     DrawText(SX+32, SY+4*32, "Customize", FS_BIG, FC_GREEN);
1234   }
1235
1236   DrawText(SX+32, SY+5*32, "Actual Settings:", FS_BIG, FC_GREEN);
1237   DrawGraphic(1, 6, GFX_PFEIL_L);
1238   DrawGraphic(1, 7, GFX_PFEIL_R);
1239   DrawGraphic(1, 8, GFX_PFEIL_O);
1240   DrawGraphic(1, 9, GFX_PFEIL_U);
1241   DrawText(SX+2*32, SY+6*32, ":", FS_BIG, FC_BLUE);
1242   DrawText(SX+2*32, SY+7*32, ":", FS_BIG, FC_BLUE);
1243   DrawText(SX+2*32, SY+8*32, ":", FS_BIG, FC_BLUE);
1244   DrawText(SX+2*32, SY+9*32, ":", FS_BIG, FC_BLUE);
1245   DrawText(SX+32, SY+10*32, "Snap Field:", FS_BIG, FC_BLUE);
1246   DrawText(SX+32, SY+12*32, "Place Bomb:", FS_BIG, FC_BLUE);
1247
1248   for (i=0; i<6; i++)
1249   {
1250     int ypos = 6 + i + (i > 3 ? i-3 : 0);
1251
1252     DrawText(SX + 3*32, SY + ypos*32,
1253              "              ", FS_BIG, FC_YELLOW);
1254     DrawText(SX + 3*32, SY + ypos*32,
1255              (setup.input[player_nr].use_joystick ?
1256               custom[i].text :
1257               getKeyNameFromKeySym(*custom[i].keysym)),
1258              FS_BIG, FC_YELLOW);
1259   }
1260 }
1261
1262 void HandleSetupInputScreen(int mx, int my, int dx, int dy, int button)
1263 {
1264   static int choice = 3;
1265   static int player_nr = 0;
1266   static int redraw = TRUE;
1267   int x = (mx+32-SX)/32, y = (my+32-SY)/32;
1268   int pos_start  = SETUPINPUT_SCREEN_POS_START  + 1;
1269   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1 + 1;
1270   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2 + 1;
1271   int pos_end    = SETUPINPUT_SCREEN_POS_END    + 1;
1272
1273   if (button == MB_MENU_INITIALIZE)
1274   {
1275     drawPlayerSetupInputInfo(player_nr);
1276     redraw = TRUE;
1277   }
1278
1279   if (redraw)
1280   {
1281     DrawGraphic(0,choice-1,GFX_KUGEL_ROT);
1282     redraw = FALSE;
1283   }
1284
1285   if (button == MB_MENU_INITIALIZE)
1286     return;
1287
1288   if (dx || dy)
1289   {
1290     if (dx && choice == 3)
1291     {
1292       x = (dx < 0 ? 11 : 13);
1293       y = 3;
1294     }
1295     else if (dx && choice == 4)
1296     {
1297       button = MB_MENU_CHOICE;
1298       x = 1;
1299       y = 4;
1300     }
1301     else if (dy)
1302     {
1303       x = 1;
1304       y = choice + dy;
1305     }
1306     else
1307       x = y = 0;
1308
1309     if (y >= pos_empty1 && y <= pos_empty2)
1310       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
1311
1312     if (y < pos_start)
1313       y = pos_start;
1314     else if (y > pos_end)
1315       y = pos_end;
1316   }
1317
1318   if (!mx && !my && !dx && !dy)
1319   {
1320     x = 1;
1321     y = choice;
1322   }
1323
1324   if (y == 3 && ((x == 1 && !button) || ((x == 11 || x == 13) && button)))
1325   {
1326     static long delay = 0;
1327
1328     if (!DelayReached(&delay, 150))
1329       goto out;
1330
1331     player_nr = (player_nr + (x == 11 ? -1 : +1) + MAX_PLAYERS) % MAX_PLAYERS;
1332
1333     drawPlayerSetupInputInfo(player_nr);
1334   }
1335   else if (x==1 && y >= pos_start && y <= pos_end &&
1336            !(y >= pos_empty1 && y <= pos_empty2))
1337   {
1338     if (button)
1339     {
1340       if (y != choice)
1341       {
1342         DrawGraphic(0, y-1, GFX_KUGEL_ROT);
1343         DrawGraphic(0, choice-1, GFX_KUGEL_BLAU);
1344       }
1345       choice = y;
1346     }
1347     else
1348     {
1349       if (y == 4)
1350       {
1351         char *device_name = setup.input[player_nr].joy.device_name;
1352
1353         if (!setup.input[player_nr].use_joystick)
1354         {
1355           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
1356
1357           setJoystickDeviceToNr(device_name, new_device_nr);
1358           setup.input[player_nr].use_joystick = TRUE;
1359         }
1360         else
1361         {
1362           int device_nr = getJoystickNrFromDeviceName(device_name);
1363           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
1364
1365           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
1366             setup.input[player_nr].use_joystick = FALSE;
1367           else
1368             setJoystickDeviceToNr(device_name, new_device_nr);
1369         }
1370
1371
1372         /*
1373         InitJoysticks();
1374         */
1375
1376
1377 #if 0
1378         int one_joystick_nr       = (dx >= 0 ? 0 : 1);
1379         int the_other_joystick_nr = (dx >= 0 ? 1 : 0);
1380
1381         if (setup.input[player_nr].use_joystick)
1382         {
1383           if (setup.input[player_nr].joystick_nr == one_joystick_nr)
1384             setup.input[player_nr].joystick_nr = the_other_joystick_nr;
1385           else
1386             setup.input[player_nr].use_joystick = FALSE;
1387         }
1388         else
1389         {
1390           setup.input[player_nr].use_joystick = TRUE;
1391           setup.input[player_nr].joystick_nr = one_joystick_nr;
1392         }
1393 #endif
1394
1395         drawPlayerSetupInputInfo(player_nr);
1396       }
1397       else if (y == 5)
1398       {
1399         if (setup.input[player_nr].use_joystick)
1400         {
1401           InitJoysticks();
1402           CalibrateJoystick(player_nr);
1403         }
1404         else
1405           CustomizeKeyboard(player_nr);
1406
1407         redraw = TRUE;
1408       }
1409       else if (y == pos_end)
1410       {
1411         InitJoysticks();
1412
1413         game_status = SETUP;
1414         DrawSetupScreen();
1415         redraw = TRUE;
1416       }
1417     }
1418   }
1419   BackToFront();
1420
1421   out:
1422
1423   if (game_status == SETUPINPUT)
1424     DoAnimation();
1425 }
1426
1427 void CustomizeKeyboard(int player_nr)
1428 {
1429   int i;
1430   int step_nr;
1431   boolean finished = FALSE;
1432   static struct SetupKeyboardInfo custom_key;
1433   static struct
1434   {
1435     KeySym *keysym;
1436     char *text;
1437   } customize_step[] =
1438   {
1439     { &custom_key.left,  "Move Left"  },
1440     { &custom_key.right, "Move Right" },
1441     { &custom_key.up,    "Move Up"    },
1442     { &custom_key.down,  "Move Down"  },
1443     { &custom_key.snap,  "Snap Field" },
1444     { &custom_key.bomb,  "Place Bomb" }
1445   };
1446
1447   /* read existing key bindings from player setup */
1448   custom_key = setup.input[player_nr].key;
1449
1450   ClearWindow();
1451   DrawText(SX + 16, SY + 16, "Keyboard Input", FS_BIG, FC_YELLOW);
1452
1453   BackToFront();
1454   InitAnimation();
1455
1456   step_nr = 0;
1457   DrawText(SX, SY + (2+2*step_nr)*32,
1458            customize_step[step_nr].text, FS_BIG, FC_RED);
1459   DrawText(SX, SY + (2+2*step_nr+1)*32,
1460            "Key:", FS_BIG, FC_RED);
1461   DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
1462            getKeyNameFromKeySym(*customize_step[step_nr].keysym),
1463            FS_BIG, FC_BLUE);
1464
1465   while(!finished)
1466   {
1467     if (XPending(display))      /* got event from X server */
1468     {
1469       XEvent event;
1470
1471       XNextEvent(display, &event);
1472
1473       switch(event.type)
1474       {
1475         case KeyPress:
1476           {
1477             KeySym key = XLookupKeysym((XKeyEvent *)&event,
1478                                        ((XKeyEvent *)&event)->state);
1479
1480             if (key == XK_Escape || (key == XK_Return && step_nr == 6))
1481             {
1482               finished = TRUE;
1483               break;
1484             }
1485
1486             /* press 'Enter' to keep the existing key binding */
1487             if (key == XK_Return || step_nr == 6)
1488               key = *customize_step[step_nr].keysym;
1489
1490             /* check if key already used */
1491             for (i=0; i<step_nr; i++)
1492               if (*customize_step[i].keysym == key)
1493                 break;
1494             if (i < step_nr)
1495               break;
1496
1497             /* got new key binding */
1498             *customize_step[step_nr].keysym = key;
1499             DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
1500                      "             ", FS_BIG, FC_YELLOW);
1501             DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
1502                      getKeyNameFromKeySym(key), FS_BIG, FC_YELLOW);
1503             step_nr++;
1504
1505             /* un-highlight last query */
1506             DrawText(SX, SY+(2+2*(step_nr-1))*32,
1507                      customize_step[step_nr-1].text, FS_BIG, FC_GREEN);
1508             DrawText(SX, SY+(2+2*(step_nr-1)+1)*32,
1509                      "Key:", FS_BIG, FC_GREEN);
1510
1511             /* press 'Enter' to leave */
1512             if (step_nr == 6)
1513             {
1514               DrawText(SX + 16, SY + 15*32+16,
1515                        "Press Enter", FS_BIG, FC_YELLOW);
1516               break;
1517             }
1518
1519             /* query next key binding */
1520             DrawText(SX, SY+(2+2*step_nr)*32,
1521                      customize_step[step_nr].text, FS_BIG, FC_RED);
1522             DrawText(SX, SY+(2+2*step_nr+1)*32,
1523                      "Key:", FS_BIG, FC_RED);
1524             DrawText(SX + 4*32, SY+(2+2*step_nr+1)*32,
1525                      getKeyNameFromKeySym(*customize_step[step_nr].keysym),
1526                      FS_BIG, FC_BLUE);
1527           }
1528           break;
1529
1530         case KeyRelease:
1531           key_joystick_mapping = 0;
1532           break;
1533
1534         default:
1535           HandleOtherEvents(&event);
1536           break;
1537       }
1538     }
1539
1540     BackToFront();
1541     DoAnimation();
1542
1543     /* don't eat all CPU time */
1544     Delay(10);
1545   }
1546
1547   /* write new key bindings back to player setup */
1548   setup.input[player_nr].key = custom_key;
1549
1550   StopAnimation();
1551   DrawSetupInputScreen();
1552 }
1553
1554 void CalibrateJoystick(int player_nr)
1555 {
1556 #ifdef __FreeBSD__
1557   struct joystick joy_ctrl;
1558 #else
1559   struct joystick_control
1560   {
1561     int buttons;
1562     int x;
1563     int y;
1564   } joy_ctrl;
1565 #endif
1566
1567 #ifdef MSDOS
1568   char joy_nr[4];
1569 #endif
1570
1571   int joystick_fd = stored_player[player_nr].joystick_fd;
1572   int new_joystick_xleft = 128, new_joystick_xright = 128;
1573   int new_joystick_yupper = 128, new_joystick_ylower = 128;
1574   int new_joystick_xmiddle, new_joystick_ymiddle;
1575   int x, y, last_x, last_y, xpos = 8, ypos = 3;
1576   boolean check[3][3];
1577   int check_remaining = 3 * 3;
1578   int joy;
1579   int result = -1;
1580
1581   if (joystick_status == JOYSTICK_OFF)
1582     return;
1583
1584   if (!setup.input[player_nr].use_joystick || joystick_fd < 0)
1585     return;
1586
1587   ClearWindow();
1588   DrawText(SX,      SY +  6*32, " ROTATE JOYSTICK ",FS_BIG,FC_YELLOW);
1589   DrawText(SX,      SY +  7*32, "IN ALL DIRECTIONS",FS_BIG,FC_YELLOW);
1590   DrawText(SX + 16, SY +  9*32, "  IF ALL BALLS  ",FS_BIG,FC_YELLOW);
1591   DrawText(SX,      SY + 10*32, "   ARE YELLOW,   ",FS_BIG,FC_YELLOW);
1592   DrawText(SX,      SY + 11*32, "  PRESS BUTTON!  ",FS_BIG,FC_YELLOW);
1593
1594   for(y=0; y<3; y++)
1595   {
1596     for(x=0; x<3; x++)
1597     {
1598       check[x][y] = FALSE;
1599       DrawGraphic(xpos + x - 1, ypos + y - 1, GFX_KUGEL_BLAU);
1600     }
1601   }
1602
1603   joy = Joystick(player_nr);
1604   last_x = (joy & JOY_LEFT ? -1 : joy & JOY_RIGHT ? +1 : 0);
1605   last_y = (joy & JOY_UP   ? -1 : joy & JOY_DOWN  ? +1 : 0);
1606   DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_ROT);
1607
1608   BackToFront();
1609
1610 #ifdef __FreeBSD__
1611   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1612 #else
1613   joy_ctrl.buttons = 0;
1614 #endif
1615
1616   while(Joystick(player_nr) & JOY_BUTTON);
1617
1618   InitAnimation();
1619
1620   while(result < 0)
1621   {
1622     if (XPending(display))      /* got event from X server */
1623     {
1624       XEvent event;
1625
1626       XNextEvent(display, &event);
1627
1628       switch(event.type)
1629       {
1630         case KeyPress:
1631           switch(XLookupKeysym((XKeyEvent *)&event,
1632                                ((XKeyEvent *)&event)->state))
1633           {
1634             case XK_Return:
1635               if (check_remaining == 0)
1636                 result = 1;
1637               break;
1638
1639             case XK_Escape:
1640               result = 0;
1641               break;
1642
1643             default:
1644               break;
1645           }
1646           break;
1647
1648         case KeyRelease:
1649           key_joystick_mapping = 0;
1650           break;
1651
1652         default:
1653           HandleOtherEvents(&event);
1654           break;
1655       }
1656     }
1657
1658     if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1659     {
1660       joystick_status = JOYSTICK_OFF;
1661       goto error_out;
1662     }
1663
1664     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_ctrl.x);
1665     new_joystick_xright = MAX(new_joystick_xright, joy_ctrl.x);
1666     new_joystick_yupper = MIN(new_joystick_yupper, joy_ctrl.y);
1667     new_joystick_ylower = MAX(new_joystick_ylower, joy_ctrl.y);
1668
1669     new_joystick_xmiddle =
1670       new_joystick_xleft + (new_joystick_xright - new_joystick_xleft) / 2;
1671     new_joystick_ymiddle =
1672       new_joystick_yupper + (new_joystick_ylower - new_joystick_yupper) / 2;
1673
1674     setup.input[player_nr].joy.xleft = new_joystick_xleft;
1675     setup.input[player_nr].joy.yupper = new_joystick_yupper;
1676     setup.input[player_nr].joy.xright = new_joystick_xright;
1677     setup.input[player_nr].joy.ylower = new_joystick_ylower;
1678     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
1679     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
1680
1681     CheckJoystickData();
1682
1683     joy = Joystick(player_nr);
1684
1685     if (joy & JOY_BUTTON && check_remaining == 0)
1686       result = 1;
1687
1688     x = (joy & JOY_LEFT ? -1 : joy & JOY_RIGHT ? +1 : 0);
1689     y = (joy & JOY_UP   ? -1 : joy & JOY_DOWN  ? +1 : 0);
1690
1691     if (x != last_x || y != last_y)
1692     {
1693       DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_GELB);
1694       DrawGraphic(xpos + x,      ypos + y,      GFX_KUGEL_ROT);
1695
1696       last_x = x;
1697       last_y = y;
1698
1699       if (check_remaining > 0 && !check[x+1][y+1])
1700       {
1701         check[x+1][y+1] = TRUE;
1702         check_remaining--;
1703       }
1704
1705 #if 0
1706       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
1707              setup.input[player_nr].joy.xleft,
1708              setup.input[player_nr].joy.xmiddle,
1709              setup.input[player_nr].joy.xright);
1710       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
1711              setup.input[player_nr].joy.yupper,
1712              setup.input[player_nr].joy.ymiddle,
1713              setup.input[player_nr].joy.ylower);
1714 #endif
1715     }
1716
1717     BackToFront();
1718     DoAnimation();
1719
1720     /* don't eat all CPU time */
1721     Delay(10);
1722   }
1723
1724   StopAnimation();
1725
1726   DrawSetupInputScreen();
1727   while(Joystick(player_nr) & JOY_BUTTON);
1728   return;
1729
1730   error_out:
1731
1732   ClearWindow();
1733   DrawText(SX+16, SY+16, "NO JOYSTICK",FS_BIG,FC_YELLOW);
1734   DrawText(SX+16, SY+48, " AVAILABLE ",FS_BIG,FC_YELLOW);
1735   BackToFront();
1736   Delay(3000);
1737   DrawSetupInputScreen();
1738 }
1739
1740
1741
1742 #if 0
1743
1744 void CalibrateJoystick_OLD()
1745 {
1746 #ifdef __FreeBSD__
1747   struct joystick joy_ctrl;
1748 #else
1749   struct joystick_control
1750   {
1751     int buttons;
1752     int x;
1753     int y;
1754   } joy_ctrl;
1755 #endif
1756
1757 #ifdef MSDOS
1758   char joy_nr[4];
1759 #endif
1760
1761   int joystick_nr = setup.input[0].joystick_nr;
1762   int new_joystick_xleft, new_joystick_xright, new_joystick_xmiddle;
1763   int new_joystick_yupper, new_joystick_ylower, new_joystick_ymiddle;
1764
1765   if (joystick_status == JOYSTICK_OFF)
1766     goto error_out;
1767
1768 #ifndef MSDOS
1769   ClearWindow();
1770   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1771   DrawText(SX+16, SY+8*32, " THE UPPER LEFT ",FS_BIG,FC_YELLOW);
1772   DrawText(SX+16, SY+9*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1773   BackToFront();
1774
1775 #ifdef __FreeBSD__
1776   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1777 #else
1778   joy_ctrl.buttons = 0;
1779 #endif
1780   while(Joystick() & JOY_BUTTON);
1781 #ifdef __FreeBSD__
1782   while(!(joy_ctrl.b1 || joy_ctrl.b2))
1783 #else
1784   while(!joy_ctrl.buttons)
1785 #endif
1786   {
1787     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1788     {
1789       joystick_status=JOYSTICK_OFF;
1790       goto error_out;
1791     }
1792     Delay(10);
1793   }
1794
1795   new_joystick_xleft = joy_ctrl.x;
1796   new_joystick_yupper = joy_ctrl.y;
1797
1798   ClearWindow();
1799   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1800   DrawText(SX+32, SY+8*32, "THE LOWER RIGHT",FS_BIG,FC_YELLOW);
1801   DrawText(SX+16, SY+9*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1802   BackToFront();
1803
1804 #ifdef __FreeBSD__
1805   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1806 #else
1807   joy_ctrl.buttons = 0;
1808 #endif
1809   while(Joystick() & JOY_BUTTON);
1810 #ifdef __FreeBSD__
1811   while(!(joy_ctrl.b1 || joy_ctrl.b2))
1812 #else
1813   while(!joy_ctrl.buttons)
1814 #endif
1815   {
1816     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1817     {
1818       joystick_status=JOYSTICK_OFF;
1819       goto error_out;
1820     }
1821     Delay(10);
1822   }
1823
1824   new_joystick_xright = joy_ctrl.x;
1825   new_joystick_ylower = joy_ctrl.y;
1826
1827   ClearWindow();
1828   DrawText(SX+32, SY+16+7*32, "CENTER JOYSTICK",FS_BIG,FC_YELLOW);
1829   DrawText(SX+16, SY+16+8*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1830   BackToFront();
1831
1832 #ifdef __FreeBSD__
1833   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1834 #else
1835   joy_ctrl.buttons = 0;
1836 #endif
1837   while(Joystick() & JOY_BUTTON);
1838 #ifdef __FreeBSD__
1839   while(!(joy_ctrl.b1 || joy_ctrl.b2))
1840 #else
1841   while(!joy_ctrl.buttons)
1842 #endif
1843   {
1844     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1845     {
1846       joystick_status=JOYSTICK_OFF;
1847       goto error_out;
1848     }
1849     Delay(10);
1850   }
1851
1852   new_joystick_xmiddle = joy_ctrl.x;
1853   new_joystick_ymiddle = joy_ctrl.y;
1854
1855   setup.input[player_nr].joy.xleft = new_joystick_xleft;
1856   setup.input[player_nr].joy.yupper = new_joystick_yupper;
1857   setup.input[player_nr].joy.xright = new_joystick_xright;
1858   setup.input[player_nr].joy.ylower = new_joystick_ylower;
1859   setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
1860   setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
1861
1862   CheckJoystickData();
1863
1864   DrawSetupScreen();
1865   while(Joystick() & JOY_BUTTON);
1866   return;
1867
1868 #endif
1869   error_out:
1870
1871 #ifdef MSDOS
1872   joy_nr[0] = '#';
1873   joy_nr[1] = SETUP_2ND_JOYSTICK_ON(local_player->setup)+49;
1874   joy_nr[2] = '\0';
1875
1876   remove_joystick();
1877   ClearWindow();
1878   DrawText(SX+32, SY+7*32, "CENTER JOYSTICK",FS_BIG,FC_YELLOW);
1879   DrawText(SX+16+7*32, SY+8*32, joy_nr, FS_BIG,FC_YELLOW);
1880   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
1881   BackToFront();
1882
1883   for(clear_keybuf();!keypressed(););
1884   install_joystick(JOY_TYPE_2PADS);
1885
1886   ClearWindow();
1887   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1888   DrawText(SX+16, SY+8*32, " THE UPPER LEFT ",FS_BIG,FC_YELLOW);
1889   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
1890   BackToFront();
1891
1892   for(clear_keybuf();!keypressed(););
1893   calibrate_joystick(SETUP_2ND_JOYSTICK_ON(local_player->setup));
1894
1895   ClearWindow();
1896   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1897   DrawText(SX+32, SY+8*32, "THE LOWER RIGHT",FS_BIG,FC_YELLOW);
1898   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
1899   BackToFront();
1900
1901   for(clear_keybuf();!keypressed(););
1902   calibrate_joystick(SETUP_2ND_JOYSTICK_ON(local_player->setup));
1903
1904   DrawSetupScreen();
1905   return;
1906 #endif
1907
1908   ClearWindow();
1909   DrawText(SX+16, SY+16, "NO JOYSTICK",FS_BIG,FC_YELLOW);
1910   DrawText(SX+16, SY+48, " AVAILABLE ",FS_BIG,FC_YELLOW);
1911   BackToFront();
1912   Delay(3000);
1913   DrawSetupScreen();
1914 }
1915
1916 #endif
1917
1918
1919
1920 void HandleGameActions()
1921 {
1922   if (game_status != PLAYING)
1923     return;
1924
1925   if (local_player->LevelSolved)
1926     GameWon();
1927
1928   if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
1929     TapeStop();
1930
1931   GameActions();
1932
1933   BackToFront();
1934 }
1935
1936 void HandleVideoButtons(int mx, int my, int button)
1937 {
1938   if (game_status != MAINMENU && game_status != PLAYING)
1939     return;
1940
1941   switch(CheckVideoButtons(mx,my,button))
1942   {
1943     case BUTTON_VIDEO_EJECT:
1944       TapeStop();
1945       if (TAPE_IS_EMPTY(tape))
1946       {
1947         LoadLevelTape(level_nr);
1948         if (TAPE_IS_EMPTY(tape))
1949           Request("No tape for this level !",REQ_CONFIRM);
1950       }
1951       else
1952       {
1953         if (tape.changed)
1954           SaveLevelTape(tape.level_nr);
1955         TapeErase();
1956       }
1957       DrawCompleteVideoDisplay();
1958       break;
1959
1960     case BUTTON_VIDEO_STOP:
1961       TapeStop();
1962       break;
1963
1964     case BUTTON_VIDEO_PAUSE:
1965       TapeTogglePause();
1966       break;
1967
1968     case BUTTON_VIDEO_REC:
1969       if (TAPE_IS_STOPPED(tape))
1970       {
1971         TapeStartRecording();
1972
1973         if (options.network)
1974           SendToServer_StartPlaying();
1975         else
1976         {
1977           game_status = PLAYING;
1978           InitGame();
1979         }
1980       }
1981       else if (tape.pausing)
1982       {
1983         if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
1984         {
1985           tape.pos[tape.counter].delay = tape.delay_played;
1986           tape.playing = FALSE;
1987           tape.recording = TRUE;
1988           tape.changed = TRUE;
1989
1990           DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON,0);
1991         }
1992         else
1993           TapeTogglePause();
1994       }
1995       break;
1996
1997     case BUTTON_VIDEO_PLAY:
1998       if (TAPE_IS_EMPTY(tape))
1999         break;
2000
2001       if (TAPE_IS_STOPPED(tape))
2002       {
2003         TapeStartPlaying();
2004
2005         game_status = PLAYING;
2006         InitGame();
2007       }
2008       else if (tape.playing)
2009       {
2010         if (tape.pausing)                       /* PAUSE -> PLAY */
2011           TapeTogglePause();
2012         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
2013         {
2014           tape.fast_forward = TRUE;
2015           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
2016         }
2017         else if (!tape.pause_before_death)      /* FFWD PLAY -> + AUTO PAUSE */
2018         {
2019           tape.pause_before_death = TRUE;
2020           DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, VIDEO_DISPLAY_LABEL_ONLY);
2021         }
2022         else                                    /* -> NORMAL PLAY */
2023         {
2024           tape.fast_forward = FALSE;
2025           tape.pause_before_death = FALSE;
2026           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PAUSE_OFF, 0);
2027         }
2028       }
2029       break;
2030
2031     default:
2032       break;
2033   }
2034
2035   BackToFront();
2036 }
2037
2038 void HandleSoundButtons(int mx, int my, int button)
2039 {
2040   if (game_status != PLAYING)
2041     return;
2042
2043   switch(CheckSoundButtons(mx,my,button))
2044   {
2045     case BUTTON_SOUND_MUSIC:
2046       if (setup.sound_music)
2047       { 
2048         setup.sound_music = FALSE;
2049         FadeSound(background_loop[level_nr % num_bg_loops]);
2050         DrawSoundDisplay(BUTTON_SOUND_MUSIC_OFF);
2051       }
2052       else if (sound_loops_allowed)
2053       { 
2054         setup.sound = setup.sound_music = TRUE;
2055         PlaySoundLoop(background_loop[level_nr % num_bg_loops]);
2056         DrawSoundDisplay(BUTTON_SOUND_MUSIC_ON);
2057       }
2058       else
2059         DrawSoundDisplay(BUTTON_SOUND_MUSIC_OFF);
2060       break;
2061
2062     case BUTTON_SOUND_LOOPS:
2063       if (setup.sound_loops)
2064       { 
2065         setup.sound_loops = FALSE;
2066         DrawSoundDisplay(BUTTON_SOUND_LOOPS_OFF);
2067       }
2068       else if (sound_loops_allowed)
2069       { 
2070         setup.sound = setup.sound_loops = TRUE;
2071         DrawSoundDisplay(BUTTON_SOUND_LOOPS_ON);
2072       }
2073       else
2074         DrawSoundDisplay(BUTTON_SOUND_LOOPS_OFF);
2075       break;
2076
2077     case BUTTON_SOUND_SIMPLE:
2078       if (setup.sound_simple)
2079       { 
2080         setup.sound_simple = FALSE;
2081         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_OFF);
2082       }
2083       else if (sound_status==SOUND_AVAILABLE)
2084       { 
2085         setup.sound = setup.sound_simple = TRUE;
2086         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_ON);
2087       }
2088       else
2089         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_OFF);
2090       break;
2091
2092     default:
2093       break;
2094   }
2095
2096   BackToFront();
2097 }
2098
2099 void HandleGameButtons(int mx, int my, int button)
2100 {
2101   if (game_status != PLAYING)
2102     return;
2103
2104   switch(CheckGameButtons(mx,my,button))
2105   {
2106     case BUTTON_GAME_STOP:
2107       if (AllPlayersGone)
2108       {
2109         CloseDoor(DOOR_CLOSE_1);
2110         game_status = MAINMENU;
2111         DrawMainMenu();
2112         break;
2113       }
2114
2115       if (Request("Do you really want to quit the game ?",
2116                   REQ_ASK | REQ_STAY_CLOSED))
2117       { 
2118         if (options.network)
2119           SendToServer_StopPlaying();
2120         else
2121         {
2122           game_status = MAINMENU;
2123           DrawMainMenu();
2124         }
2125       }
2126       else
2127         OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
2128       break;
2129
2130     case BUTTON_GAME_PAUSE:
2131       if (options.network)
2132       {
2133         if (tape.pausing)
2134           SendToServer_ContinuePlaying();
2135         else
2136           SendToServer_PausePlaying();
2137       }
2138       else
2139         TapeTogglePause();
2140       break;
2141
2142     case BUTTON_GAME_PLAY:
2143       if (tape.pausing)
2144       {
2145         if (options.network)
2146           SendToServer_ContinuePlaying();
2147         else
2148         {
2149           tape.pausing = FALSE;
2150           DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
2151         }
2152       }
2153       break;
2154
2155     default:
2156       break;
2157   }
2158
2159   BackToFront();
2160 }