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