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