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