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