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