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