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