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