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