rnd-20030426-2-src
[rocksndiamonds.git] / src / screens.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 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_GAME                 1
30 #define SETUP_MODE_EDITOR               2
31 #define SETUP_MODE_INPUT                3
32 #define SETUP_MODE_SHORTCUT             4
33 #define SETUP_MODE_GRAPHICS             5
34 #define SETUP_MODE_SOUND                6
35 #define SETUP_MODE_ARTWORK              7
36 #define SETUP_MODE_CHOOSE_GRAPHICS      8
37 #define SETUP_MODE_CHOOSE_SOUNDS        9
38 #define SETUP_MODE_CHOOSE_MUSIC         10
39
40 #define MAX_SETUP_MODES                 11
41
42 /* for input setup functions */
43 #define SETUPINPUT_SCREEN_POS_START     0
44 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
45 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
46 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
47
48 /* for various menu stuff  */
49 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - 2)
50 #define MENU_SCREEN_START_YPOS          2
51 #define MENU_SCREEN_VALUE_XPOS          14
52
53 /* buttons and scrollbars identifiers */
54 #define SCREEN_CTRL_ID_SCROLL_UP        0
55 #define SCREEN_CTRL_ID_SCROLL_DOWN      1
56 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  2
57
58 #define NUM_SCREEN_SCROLLBUTTONS        2
59 #define NUM_SCREEN_SCROLLBARS           1
60 #define NUM_SCREEN_GADGETS              3
61
62 /* forward declarations of internal functions */
63 static void HandleScreenGadgets(struct GadgetInfo *);
64 static void HandleSetupScreen_Generic(int, int, int, int, int);
65 static void HandleSetupScreen_Input(int, int, int, int, int);
66 static void CustomizeKeyboard(int);
67 static void CalibrateJoystick(int);
68 static void execSetupArtwork(void);
69 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
70
71 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
72 static int setup_mode = SETUP_MODE_MAIN;
73
74 #if 0
75 static int mSX = SX;
76 static int mSY = SY;
77 #else
78 #define mSX (SX + (game_status == GAME_MODE_MAIN ? global.menu_draw_xoffset_MAIN : \
79                    global.menu_draw_xoffset))
80 #define mSY (SY + (game_status == GAME_MODE_MAIN ? global.menu_draw_yoffset_MAIN : \
81                    global.menu_draw_yoffset))
82 #endif
83
84 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
85 #define NUM_SCROLLBAR_BITMAPS           2
86 static Bitmap *scrollbar_bitmap[NUM_SCROLLBAR_BITMAPS];
87 #endif
88
89
90 static void drawCursorExt(int xpos, int ypos, int color, int graphic)
91 {
92   static int cursor_array[SCR_FIELDY];
93
94   if (xpos == 0)
95   {
96     if (graphic != 0)
97       cursor_array[ypos] = graphic;
98     else
99       graphic = cursor_array[ypos];
100   }
101
102   if (color == FC_RED)
103     graphic = (graphic == IMG_MENU_BUTTON_LEFT  ? IMG_MENU_BUTTON_LEFT_ACTIVE :
104                graphic == IMG_MENU_BUTTON_RIGHT ? IMG_MENU_BUTTON_RIGHT_ACTIVE:
105                IMG_MENU_BUTTON_ACTIVE);
106
107   ypos += MENU_SCREEN_START_YPOS;
108
109 #if 1
110   DrawBackground(mSX + xpos * TILEX, mSY + ypos * TILEY, TILEX, TILEY);
111   DrawGraphicThruMaskExt(drawto, mSX + xpos * TILEX, mSY + ypos * TILEY,
112                          graphic, 0);
113 #else
114   DrawBackground(SX + xpos * 32, SY + ypos * 32, TILEX, TILEY);
115   DrawGraphicThruMask(xpos, ypos, graphic, 0);
116 #endif
117 }
118
119 static void initCursor(int ypos, int graphic)
120 {
121   drawCursorExt(0, ypos, FC_BLUE, graphic);
122 }
123
124 static void drawCursor(int ypos, int color)
125 {
126   drawCursorExt(0, ypos, color, 0);
127 }
128
129 static void drawCursorXY(int xpos, int ypos, int graphic)
130 {
131   drawCursorExt(xpos, ypos, -1, graphic);
132 }
133
134 static void PlaySound_Menu_Start(int sound)
135 {
136   if (sound_info[sound].loop)
137     PlaySoundLoop(sound);
138   else
139     PlaySound(sound);
140 }
141
142 static void PlaySound_Menu_Continue(int sound)
143 {
144   if (sound_info[sound].loop)
145     PlaySoundLoop(sound);
146 }
147
148 void DrawHeadline()
149 {
150   int font1_width = getFontWidth(FONT_TITLE_1);
151   int font2_width = getFontWidth(FONT_TITLE_2);
152   int x1 = SX + (SXSIZE - strlen(PROGRAM_TITLE_STRING)   * font1_width) / 2;
153   int x2 = SX + (SXSIZE - strlen(WINDOW_SUBTITLE_STRING) * font2_width) / 2;
154
155   DrawText(x1, SY + 8,  PROGRAM_TITLE_STRING,   FONT_TITLE_1);
156   DrawText(x2, SY + 46, WINDOW_SUBTITLE_STRING, FONT_TITLE_2);
157 }
158
159 static void ToggleFullscreenIfNeeded()
160 {
161   if (setup.fullscreen != video.fullscreen_enabled)
162   {
163     /* save old door content */
164     BlitBitmap(backbuffer, bitmap_db_door,
165                DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
166
167     /* toggle fullscreen */
168     ChangeVideoModeIfNeeded(setup.fullscreen);
169     setup.fullscreen = video.fullscreen_enabled;
170
171     /* redraw background to newly created backbuffer */
172     BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, backbuffer,
173                0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
174
175     /* restore old door content */
176     BlitBitmap(bitmap_db_door, backbuffer,
177                DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE, DX, DY);
178
179     redraw_mask = REDRAW_ALL;
180   }
181 }
182
183 void DrawMainMenu()
184 {
185   static LevelDirTree *leveldir_last_valid = NULL;
186   char *name_text = (!options.network && setup.team_mode ? "Team:" : "Name:");
187   int font_width = getFontWidth(FONT_MENU_1);
188   int name_width = font_width * strlen("Name:");
189   int level_width = font_width * strlen("Level:");
190   int i;
191
192 #if 0
193   mSX = SX + global.menu_draw_xoffset;
194   mSY = SY + global.menu_draw_yoffset;
195 #endif
196
197   UnmapAllGadgets();
198   FadeSounds();
199
200   KeyboardAutoRepeatOn();
201   ActivateJoystick();
202
203   SetDrawDeactivationMask(REDRAW_NONE);
204   SetDrawBackgroundMask(REDRAW_FIELD);
205
206   audio.sound_deactivated = FALSE;
207
208   /* needed if last screen was the playing screen, invoked from level editor */
209   if (level_editor_test_game)
210   {
211     game_status = GAME_MODE_EDITOR;
212     DrawLevelEd();
213     return;
214   }
215
216   /* needed if last screen was the editor screen */
217   UndrawSpecialEditorDoor();
218
219   /* needed if last screen was the setup screen and fullscreen state changed */
220   ToggleFullscreenIfNeeded();
221
222   /* needed if last screen (level choice) changed graphics, sounds or music */
223   ReloadCustomArtwork();
224
225 #ifdef TARGET_SDL
226   SetDrawtoField(DRAW_BACKBUFFER);
227 #endif
228
229   /* map gadgets for main menu screen */
230   MapTapeButtons();
231
232   /* leveldir_current may be invalid (level group, parent link) */
233   if (!validLevelSeries(leveldir_current))
234     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
235
236   /* store valid level series information */
237   leveldir_last_valid = leveldir_current;
238
239   /* level_nr may have been set to value over handicap with level editor */
240   if (setup.handicap && level_nr > leveldir_current->handicap_level)
241     level_nr = leveldir_current->handicap_level;
242
243   GetPlayerConfig();
244   LoadLevel(level_nr);
245
246   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
247   ClearWindow();
248
249   DrawHeadline();
250
251   DrawText(mSX + 32, mSY + 2*32, name_text, FONT_MENU_1);
252   DrawText(mSX + 32, mSY + 3*32, "Level:", FONT_MENU_1);
253   DrawText(mSX + 32, mSY + 4*32, "Hall Of Fame", FONT_MENU_1);
254   DrawText(mSX + 32, mSY + 5*32, "Level Creator", FONT_MENU_1);
255   DrawText(mSY + 32, mSY + 6*32, "Info Screen", FONT_MENU_1);
256   DrawText(mSX + 32, mSY + 7*32, "Start Game", FONT_MENU_1);
257   DrawText(mSX + 32, mSY + 8*32, "Setup", FONT_MENU_1);
258   DrawText(mSX + 32, mSY + 9*32, "Quit", FONT_MENU_1);
259
260   DrawText(mSX + 32 + name_width, mSY + 2*32, setup.player_name, FONT_INPUT_1);
261   DrawText(mSX + level_width + 5 * 32, mSY + 3*32, int2str(level_nr,3),
262            FONT_VALUE_1);
263
264   DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
265
266   DrawTextF(mSX + 32 + level_width - 2, mSY + 3*32 + 1, FONT_TEXT_3, "%d-%d",
267             leveldir_current->first_level, leveldir_current->last_level);
268
269   /*
270   if (leveldir_current->readonly)
271   */
272   {
273     DrawTextF(mSX + level_width + 9*32 - 2,
274               mSY + 3*32 + 1 - 7, FONT_TEXT_3, "READ");
275     DrawTextF(mSX + level_width + 9*32 - 2,
276               mSY + 3*32 + 1 + 7, FONT_TEXT_3, "ONLY");
277   }
278
279   for(i=0; i<8; i++)
280     initCursor(i, (i == 1 || i == 6 ? IMG_MENU_BUTTON_RIGHT :IMG_MENU_BUTTON));
281
282   drawCursorXY(level_width/32 + 4, 1, IMG_MENU_BUTTON_LEFT);
283   drawCursorXY(level_width/32 + 8, 1, IMG_MENU_BUTTON_RIGHT);
284
285   DrawText(SX + 56, SY + 326, "A Game by Artsoft Entertainment", FONT_TITLE_2);
286
287   FadeToFront();
288   InitAnimation();
289   HandleMainMenu(0,0, 0,0, MB_MENU_INITIALIZE);
290
291   TapeStop();
292   if (TAPE_IS_EMPTY(tape))
293     LoadTape(level_nr);
294   DrawCompleteVideoDisplay();
295
296   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
297
298 #if 0
299   ClearEventQueue();
300 #endif
301 }
302
303 static void gotoTopLevelDir()
304 {
305   /* move upwards to top level directory */
306   while (leveldir_current->node_parent)
307   {
308     /* write a "path" into level tree for easy navigation to last level */
309     if (leveldir_current->node_parent->node_group->cl_first == -1)
310     {
311       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
312       int leveldir_pos = posTreeInfo(leveldir_current);
313       int num_page_entries;
314       int cl_first, cl_cursor;
315
316       if (num_leveldirs <= MAX_MENU_ENTRIES_ON_SCREEN)
317         num_page_entries = num_leveldirs;
318       else
319         num_page_entries = MAX_MENU_ENTRIES_ON_SCREEN - 1;
320
321       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
322       cl_cursor = leveldir_pos - cl_first;
323
324       leveldir_current->node_parent->node_group->cl_first = cl_first;
325       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
326     }
327
328     leveldir_current = leveldir_current->node_parent;
329   }
330 }
331
332 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
333 {
334   static int choice = 0;
335   int x = 0;
336   int y = choice;
337
338   if (button == MB_MENU_INITIALIZE)
339   {
340     drawCursor(choice, FC_RED);
341     return;
342   }
343
344   if (mx || my)         /* mouse input */
345   {
346     x = (mx - SX) / 32;
347     y = (my - SY) / 32 - MENU_SCREEN_START_YPOS;
348   }
349   else if (dx || dy)    /* keyboard input */
350   {
351     if (dx && choice == 1)
352       x = (dx < 0 ? 10 : 14);
353     else if (dy)
354       y = choice + dy;
355   }
356
357   if (y == 1 && ((x == 10 && level_nr > leveldir_current->first_level) ||
358                  (x == 14 && level_nr < leveldir_current->last_level)) &&
359       button)
360   {
361     static unsigned long level_delay = 0;
362     int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
363     int new_level_nr, old_level_nr = level_nr;
364
365     new_level_nr = level_nr + (x == 10 ? -step : +step);
366     if (new_level_nr < leveldir_current->first_level)
367       new_level_nr = leveldir_current->first_level;
368     if (new_level_nr > leveldir_current->last_level)
369       new_level_nr = leveldir_current->last_level;
370
371     if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
372       new_level_nr = leveldir_current->handicap_level;
373
374     if (old_level_nr == new_level_nr ||
375         !DelayReached(&level_delay, GADGET_FRAME_DELAY))
376       goto out;
377
378     level_nr = new_level_nr;
379
380     DrawText(mSX + 11 * 32, mSY + 3 * 32, int2str(level_nr, 3), FONT_VALUE_1);
381
382     LoadLevel(level_nr);
383     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
384
385     TapeErase();
386     LoadTape(level_nr);
387     DrawCompleteVideoDisplay();
388
389     /* needed because DrawMicroLevel() takes some time */
390     BackToFront();
391     SyncDisplay();
392     DelayReached(&level_delay, 0);      /* reset delay counter */
393   }
394   else if (x == 0 && y >= 0 && y <= 7)
395   {
396     if (button)
397     {
398       if (y != choice)
399       {
400         drawCursor(y, FC_RED);
401         drawCursor(choice, FC_BLUE);
402         choice = y;
403       }
404     }
405     else
406     {
407       if (y == 0)
408       {
409         game_status = GAME_MODE_PSEUDO_TYPENAME;
410         HandleTypeName(strlen(setup.player_name), 0);
411       }
412       else if (y == 1)
413       {
414         if (leveldir_first)
415         {
416           game_status = GAME_MODE_LEVELS;
417           SaveLevelSetup_LastSeries();
418           SaveLevelSetup_SeriesInfo();
419
420           gotoTopLevelDir();
421
422           DrawChooseLevel();
423         }
424       }
425       else if (y == 2)
426       {
427         game_status = GAME_MODE_SCORES;
428         DrawHallOfFame(-1);
429       }
430       else if (y == 3)
431       {
432         if (leveldir_current->readonly &&
433             strcmp(setup.player_name, "Artsoft") != 0)
434           Request("This level is read only !", REQ_CONFIRM);
435         game_status = GAME_MODE_EDITOR;
436         DrawLevelEd();
437       }
438       else if (y == 4)
439       {
440         game_status = GAME_MODE_INFO;
441         DrawHelpScreen();
442       }
443       else if (y == 5)
444       {
445         if (setup.autorecord)
446           TapeStartRecording();
447
448 #if defined(PLATFORM_UNIX)
449         if (options.network)
450           SendToServer_StartPlaying();
451         else
452 #endif
453         {
454           game_status = GAME_MODE_PLAYING;
455           StopAnimation();
456           InitGame();
457         }
458       }
459       else if (y == 6)
460       {
461         game_status = GAME_MODE_SETUP;
462         setup_mode = SETUP_MODE_MAIN;
463         DrawSetupScreen();
464       }
465       else if (y == 7)
466       {
467         SaveLevelSetup_LastSeries();
468         SaveLevelSetup_SeriesInfo();
469         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
470           game_status = GAME_MODE_QUIT;
471       }
472     }
473   }
474
475   BackToFront();
476
477   out:
478
479   if (game_status == GAME_MODE_MAIN)
480   {
481     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, FALSE);
482     DoAnimation();
483   }
484 }
485
486
487 #define MAX_HELPSCREEN_ELS      10
488 #define HA_NEXT                 -999
489 #define HA_END                  -1000
490
491 static long helpscreen_state;
492 static int helpscreen_step[MAX_HELPSCREEN_ELS];
493 static int helpscreen_frame[MAX_HELPSCREEN_ELS];
494
495 static int helpscreen_action[] =
496 {
497   IMG_PLAYER_1_MOVING_DOWN,             16,
498   IMG_PLAYER_1_MOVING_UP,               16,
499   IMG_PLAYER_1_MOVING_LEFT,             16,
500   IMG_PLAYER_1_MOVING_RIGHT,            16,
501   IMG_PLAYER_1_PUSHING_LEFT,            16,
502   IMG_PLAYER_1_PUSHING_RIGHT,           16,                     HA_NEXT,
503
504   IMG_SAND,                             -1,                     HA_NEXT,
505
506   IMG_EMPTY_SPACE,                      -1,                     HA_NEXT,
507
508   IMG_QUICKSAND_EMPTY,                  -1,                     HA_NEXT,
509
510   IMG_STEELWALL,                        -1,                     HA_NEXT,
511
512   IMG_WALL,                             -1,                     HA_NEXT,
513
514   IMG_EXPANDABLE_WALL_GROWING_LEFT,     20,
515   IMG_WALL,                             50,
516   IMG_EMPTY_SPACE,                      20,
517   IMG_EXPANDABLE_WALL_GROWING_RIGHT,    20,
518   IMG_WALL,                             50,
519   IMG_EMPTY_SPACE,                      20,
520   IMG_EXPANDABLE_WALL_GROWING_UP,       20,
521   IMG_WALL,                             50,
522   IMG_EMPTY_SPACE,                      20,
523   IMG_EXPANDABLE_WALL_GROWING_DOWN,     20,
524   IMG_WALL,                             50,
525   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
526
527   IMG_INVISIBLE_WALL,                   -1,                     HA_NEXT,
528
529   IMG_WALL_CRUMBLED,                    -1,                     HA_NEXT,
530
531   IMG_FONT_GAME_INFO,                   -1,                     HA_NEXT,
532
533   IMG_EMERALD,                          -1,                     HA_NEXT,
534
535   IMG_DIAMOND,                          -1,                     HA_NEXT,
536
537   IMG_BD_DIAMOND,                       -1,                     HA_NEXT,
538
539   IMG_EMERALD_YELLOW,                   50,
540   IMG_EMERALD_RED,                      50,
541   IMG_EMERALD_PURPLE,                   50,                     HA_NEXT,
542
543   IMG_BD_ROCK,                          -1,                     HA_NEXT,
544
545   IMG_BOMB,                             100,
546   IMG_EXPLOSION,                        16,
547   IMG_EMPTY_SPACE,                      10,                     HA_NEXT,
548
549   IMG_NUT,                              100,
550   IMG_NUT_BREAKING,                     6,
551   IMG_EMERALD,                          20,                     HA_NEXT,
552
553   IMG_WALL_EMERALD,                     100,
554   IMG_EXPLOSION,                        16,
555   IMG_EMERALD,                          20,                     HA_NEXT,
556
557   IMG_WALL_DIAMOND,                     100,
558   IMG_EXPLOSION,                        16,
559   IMG_DIAMOND,                          20,                     HA_NEXT,
560
561   IMG_WALL_BD_DIAMOND,                  100,
562   IMG_EXPLOSION,                        16,
563   IMG_BD_DIAMOND,                       20,                     HA_NEXT,
564
565   IMG_WALL_EMERALD_YELLOW,              100,
566   IMG_EXPLOSION,                        16,
567   IMG_EMERALD_YELLOW,                   20,
568   IMG_WALL_EMERALD_RED,                 100,
569   IMG_EXPLOSION,                        16,
570   IMG_EMERALD_RED,                      20,
571   IMG_WALL_EMERALD_PURPLE,              100,
572   IMG_EXPLOSION,                        16,
573   IMG_EMERALD_PURPLE,                   20,                     HA_NEXT,
574
575   IMG_ACID,                             -1,                     HA_NEXT,
576
577   IMG_KEY_1,                            50,
578   IMG_KEY_2,                            50,
579   IMG_KEY_3,                            50,
580   IMG_KEY_4,                            50,                     HA_NEXT,
581
582   IMG_GATE_1,                           50,
583   IMG_GATE_2,                           50,
584   IMG_GATE_3,                           50,
585   IMG_GATE_4,                           50,                     HA_NEXT,
586
587   IMG_GATE_1_GRAY,                      50,
588   IMG_GATE_2_GRAY,                      50,
589   IMG_GATE_3_GRAY,                      50,
590   IMG_GATE_4_GRAY,                      50,                     HA_NEXT,
591
592   IMG_DYNAMITE,                         -1,                     HA_NEXT,
593
594   IMG_DYNAMITE_ACTIVE,                  96,
595   IMG_EXPLOSION,                        16,
596   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
597
598   IMG_DYNABOMB_ACTIVE,                  100,
599   IMG_EXPLOSION,                        16,
600   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
601
602   IMG_DYNABOMB_INCREASE_NUMBER,         -1,                     HA_NEXT,
603
604   IMG_DYNABOMB_INCREASE_SIZE,           -1,                     HA_NEXT,
605
606   IMG_DYNABOMB_INCREASE_POWER,          -1,                     HA_NEXT,
607
608   IMG_SPACESHIP_RIGHT,                  16,
609   IMG_SPACESHIP_UP,                     16,
610   IMG_SPACESHIP_LEFT,                   16,
611   IMG_SPACESHIP_DOWN,                   16,                     HA_NEXT,
612
613   IMG_BUG_RIGHT,                        16,
614   IMG_BUG_UP,                           16,
615   IMG_BUG_LEFT,                         16,
616   IMG_BUG_DOWN,                         16,                     HA_NEXT,
617
618   IMG_BD_BUTTERFLY,                     -1,                     HA_NEXT,
619
620   IMG_BD_FIREFLY,                       -1,                     HA_NEXT,
621
622   IMG_PACMAN_RIGHT,                     16,
623   IMG_PACMAN_UP,                        16,
624   IMG_PACMAN_LEFT,                      16,
625   IMG_PACMAN_DOWN,                      16,                     HA_NEXT,
626
627   IMG_YAMYAM,                           -1,                     HA_NEXT,
628
629   IMG_DARK_YAMYAM,                      -1,                     HA_NEXT,
630
631   IMG_ROBOT,                            -1,                     HA_NEXT,
632
633   IMG_MOLE_MOVING_RIGHT,                16,
634   IMG_MOLE_MOVING_UP,                   16,
635   IMG_MOLE_MOVING_LEFT,                 16,
636   IMG_MOLE_MOVING_DOWN,                 16,                     HA_NEXT,
637
638   IMG_PENGUIN_MOVING_RIGHT,             16,
639   IMG_PENGUIN_MOVING_UP,                16,
640   IMG_PENGUIN_MOVING_LEFT,              16,
641   IMG_PENGUIN_MOVING_DOWN,              16,                     HA_NEXT,
642
643   IMG_PIG_MOVING_RIGHT,                 16,
644   IMG_PIG_MOVING_UP,                    16,
645   IMG_PIG_MOVING_LEFT,                  16,
646   IMG_PIG_MOVING_DOWN,                  16,                     HA_NEXT,
647
648   IMG_DRAGON_MOVING_RIGHT,              16,
649   IMG_DRAGON_MOVING_UP,                 16,
650   IMG_DRAGON_MOVING_LEFT,               16,
651   IMG_DRAGON_MOVING_DOWN,               16,                     HA_NEXT,
652
653   IMG_SATELLITE,                        -1,                     HA_NEXT,
654
655   IMG_ROBOT_WHEEL,                      50,
656   IMG_ROBOT_WHEEL_ACTIVE,               100,                    HA_NEXT,
657
658   IMG_LAMP,                             50,
659   IMG_LAMP_ACTIVE,                      50,                     HA_NEXT,
660
661   IMG_TIME_ORB_FULL,                    50,
662   IMG_TIME_ORB_EMPTY,                   50,                     HA_NEXT,
663
664   IMG_AMOEBA_DROP,                      50,
665   IMG_AMOEBA_GROWING,                   6,
666   IMG_AMOEBA_WET,                       20,                     HA_NEXT,
667
668   IMG_AMOEBA_DEAD,                      -1,                     HA_NEXT,
669
670   IMG_AMOEBA_WET,                       -1,                     HA_NEXT,
671
672   IMG_AMOEBA_WET,                       100,
673   IMG_AMOEBA_GROWING,                   6,                      HA_NEXT,
674
675   IMG_AMOEBA_FULL,                      50,
676   IMG_AMOEBA_DEAD,                      50,
677   IMG_EXPLOSION,                        16,
678   IMG_DIAMOND,                          20,                     HA_NEXT,
679
680   IMG_GAME_OF_LIFE,                     -1,                     HA_NEXT,
681
682   IMG_BIOMAZE,                          -1,                     HA_NEXT,
683
684   IMG_MAGIC_WALL_ACTIVE,                -1,                     HA_NEXT,
685
686   IMG_BD_MAGIC_WALL_ACTIVE,             -1,                     HA_NEXT,
687
688   IMG_EXIT_CLOSED,                      200,
689   IMG_EXIT_OPENING,                     16,
690   IMG_EXIT_OPEN,                        100,                    HA_NEXT,
691
692   IMG_EXIT_OPEN,                        -1,                     HA_NEXT,
693
694   IMG_SOKOBAN_OBJECT,                   -1,                     HA_NEXT,
695
696   IMG_SOKOBAN_FIELD_EMPTY,              -1,                     HA_NEXT,
697
698   IMG_SOKOBAN_FIELD_FULL,               -1,                     HA_NEXT,
699
700   IMG_SPEED_PILL,                       -1,                     HA_NEXT,
701
702   HA_END
703 };
704 static char *helpscreen_eltext[][2] =
705 {
706  {"THE HERO:",                          "(Is _this_ guy good old Rockford?)"},
707  {"Normal sand:",                       "You can dig through it"},
708  {"Empty field:",                       "You can walk through it"},
709  {"Quicksand: You cannot pass it,",     "but rocks can fall through it"},
710  {"Massive Wall:",                      "Nothing can go through it"},
711  {"Normal Wall: You can't go through",  "it, but you can bomb it away"},
712  {"Growing Wall: Grows in several di-", "rections if there is an empty field"},
713  {"Invisible Wall: Behaves like normal","wall, but is invisible"},
714  {"Old Wall: Like normal wall, but",    "some things can fall down from it"},
715  {"Letter Wall: Looks like a letter,",  "behaves like a normal wall"},
716  {"Emerald: You must collect enough of","them to finish a level"},
717  {"Diamond: Counts as 3 emeralds, but", "can be destroyed by rocks"},
718  {"Diamond (BD style): Counts like one","emerald and behaves a bit different"},
719  {"Colorful Gems:",                     "Seem to behave like Emeralds"},
720  {"Rock: Smashes several things;",      "Can be moved by the player"},
721  {"Bomb: You can move it, but be",      "careful when dropping it"},
722  {"Nut: Throw a rock on it to open it;","Each nut contains an emerald"},
723  {"Wall with an emerald inside:",       "Bomb the wall away to get it"},
724  {"Wall with a diamond inside:",        "Bomb the wall away to get it"},
725  {"Wall with BD style diamond inside:", "Bomb the wall away to get it"},
726  {"Wall with colorful gem inside:",     "Bomb the wall away to get it"},
727  {"Acid: Things that fall in are gone", "forever (including our hero)"},
728  {"Key: Opens the door that has the",   "same color (red/yellow/green/blue)"},
729  {"Door: Can be opened by the key",     "with the same color"},
730  {"Door: You have to find out the",     "right color of the key for it"},
731  {"Dynamite: Collect it and use it to", "destroy walls or kill enemies"},
732  {"Dynamite: This one explodes after",  "a few seconds"},
733  {"Dyna Bomb: Explodes in 4 directions","with variable explosion size"},
734  {"Dyna Bomb: Increases the number of", "dyna bombs available at a time"},
735  {"Dyna Bomb: Increases the size of",   "explosion of dyna bombs"},
736  {"Dyna Bomb: Increases the power of",  "explosion of dyna bombs"},
737  {"Spaceship: Moves at the left side",  "of walls; don't touch it!"},
738  {"Bug: Moves at the right side",       "of walls; don't touch it!"},
739  {"Butterfly: Moves at the right side", "of walls; don't touch it!"},
740  {"Firefly: Moves at the left side",    "of walls; don't touch it!"},
741  {"Pacman: Eats the amoeba and you,",   "if you're not careful"},
742  {"Cruncher: Eats diamonds and you,",   "if you're not careful"},
743  {"Cruncher (BD style):",               "Eats almost everything"},
744  {"Robot: Tries to kill the player",    ""},
745  {"The mole: Eats the amoeba and turns","empty space into normal sand"},
746  {"The penguin: Guide him to the exit,","but keep him away from monsters!"},
747  {"The Pig: Harmless, but eats all",    "gems it can get"},
748  {"The Dragon: Breathes fire,",         "especially to some monsters"},
749  {"Sonde: Follows you everywhere;",     "harmless, but may block your way"},
750  {"Magic Wheel: Touch it to get rid of","the robots for some seconds"},
751  {"Light Bulb: All of them must be",    "switched on to finish a level"},
752  {"Extra Time Orb: Adds some seconds",  "to the time available for the level"},
753  {"Amoeba Drop: Grows to an amoeba on", "the ground - don't touch it"},
754  {"Dead Amoeba: Does not grow, but",    "can still kill bugs and spaceships"},
755  {"Normal Amoeba: Grows through empty", "fields, sand and quicksand"},
756  {"Dropping Amoeba: This one makes",    "drops that grow to a new amoeba"},
757  {"Living Amoeba (BD style): Contains", "other element, when surrounded"},
758  {"Game Of Life: Behaves like the well","known 'Game Of Life' (2333 style)"},
759  {"Biomaze: A bit like the 'Game Of",   "Life', but builds crazy mazes"},
760  {"Magic Wall: Changes rocks, emeralds","and diamonds when they pass it"},
761  {"Magic Wall (BD style):",             "Changes rocks and BD style diamonds"},
762  {"Exit door: Opens if you have enough","emeralds to finish the level"},
763  {"Open exit door: Enter here to leave","the level and exit the actual game"},
764  {"Sokoban element: Object which must", "be pushed to an empty field"},
765  {"Sokoban element: Empty field where", "a Sokoban object can be placed on"},
766  {"Sokoban element: Field with object", "which can be pushed away"},
767  {"Speed pill: Lets the player run",    "twice as fast as normally"},
768 };
769 static int num_helpscreen_els = sizeof(helpscreen_eltext) / (2*sizeof(char *));
770
771 static char *helpscreen_music[][3] =
772 {
773   { "Alchemy",                  "Ian Boddy",            "Drive" },
774   { "The Chase",                "Propaganda",           "A Secret Wish" },
775   { "Network 23",               "Tangerine Dream",      "Exit" },
776   { "Czardasz",                 "Robert Pieculewicz",   "Czardasz" },
777   { "21st Century Common Man",  "Tangerine Dream",      "Tyger" },
778   { "Voyager",                  "The Alan Parsons Project","Pyramid" },
779   { "Twilight Painter",         "Tangerine Dream",      "Heartbreakers" }
780 };
781 static int num_helpscreen_music = 7;
782 static int helpscreen_musicpos;
783
784 #if 0
785 void OLD_DrawHelpScreenElAction(int start)
786 {
787   int i = 0, j = 0;
788   int frame, graphic;
789   int xstart = SX+16, ystart = SY+64+2*32, ystep = TILEY+4;
790
791   while(helpscreen_action[j] != HA_END)
792   {
793     if (i>=start+MAX_HELPSCREEN_ELS || i>=num_helpscreen_els)
794       break;
795     else if (i<start || helpscreen_delay[i-start])
796     {
797       if (i>=start && helpscreen_delay[i-start])
798         helpscreen_delay[i-start]--;
799
800       while(helpscreen_action[j] != HA_NEXT)
801         j++;
802       j++;
803       i++;
804       continue;
805     }
806
807     j += 3*helpscreen_step[i-start];
808     graphic = helpscreen_action[j++];
809
810     if (helpscreen_frame[i-start])
811     {
812       frame = helpscreen_action[j++] - helpscreen_frame[i-start];
813       helpscreen_frame[i-start]--;
814     }
815     else
816     {
817       frame = 0;
818       helpscreen_frame[i-start] = helpscreen_action[j++]-1;
819     }
820
821     helpscreen_delay[i-start] = helpscreen_action[j++] - 1;
822
823     if (helpscreen_action[j] == HA_NEXT)
824     {
825       if (!helpscreen_frame[i-start])
826         helpscreen_step[i-start] = 0;
827     }
828     else
829     {
830       if (!helpscreen_frame[i-start])
831         helpscreen_step[i-start]++;
832       while(helpscreen_action[j] != HA_NEXT)
833         j++;
834     }
835     j++;
836
837     DrawOldGraphicExt(drawto, xstart, ystart+(i-start)*ystep, graphic+frame);
838     i++;
839   }
840
841   for(i=2;i<16;i++)
842   {
843     MarkTileDirty(0,i);
844     MarkTileDirty(1,i);
845   }
846 }
847 #endif
848
849 void DrawHelpScreenElAction(int start)
850 {
851   int i = 0, j = 0;
852   int xstart = mSX + 16;
853   int ystart = mSY + 64 + 2 * 32;
854   int ystep = TILEY + 4;
855   int graphic;
856   int frame_count;
857   int sync_frame;
858   int frame;
859
860   while (helpscreen_action[j] != HA_END)
861   {
862     if (i >= start + MAX_HELPSCREEN_ELS || i >= num_helpscreen_els)
863       break;
864     else if (i < start)
865     {
866       while (helpscreen_action[j] != HA_NEXT)
867         j++;
868
869       j++;
870       i++;
871
872       continue;
873     }
874
875     j += 2 * helpscreen_step[i-start];
876     graphic = helpscreen_action[j++];
877     frame_count = helpscreen_action[j++];
878     if (frame_count == -1)
879       frame_count = 1000000;
880
881     if (helpscreen_frame[i-start] == 0)
882     {
883       sync_frame = 0;
884       helpscreen_frame[i-start] = frame_count - 1;
885     }
886     else
887     {
888       sync_frame = frame_count - helpscreen_frame[i-start];
889       helpscreen_frame[i-start]--;
890     }
891
892     if (helpscreen_action[j] == HA_NEXT)
893     {
894       if (!helpscreen_frame[i-start])
895         helpscreen_step[i-start] = 0;
896     }
897     else
898     {
899       if (!helpscreen_frame[i-start])
900         helpscreen_step[i-start]++;
901       while(helpscreen_action[j] != HA_NEXT)
902         j++;
903     }
904     j++;
905
906     frame = getGraphicAnimationFrame(graphic, sync_frame);
907
908     DrawGraphicExt(drawto, xstart, ystart + (i-start) * ystep,
909                    graphic, frame);
910
911     i++;
912   }
913
914   for(i=2; i<16; i++)
915   {
916     MarkTileDirty(0, i);
917     MarkTileDirty(1, i);
918   }
919
920   FrameCounter++;
921 }
922
923 void DrawHelpScreenElText(int start)
924 {
925   int i;
926   int xstart = mSX + 56, ystart = mSY + 65 + 2 * 32, ystep = TILEY + 4;
927   int ybottom = SYSIZE - 20;
928
929   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
930   ClearWindow();
931   DrawHeadline();
932
933   DrawTextFCentered(100, FONT_TEXT_1, "The game elements:");
934
935   for(i=start; i < start + MAX_HELPSCREEN_ELS && i < num_helpscreen_els; i++)
936   {
937     DrawText(xstart,
938              ystart + (i - start) * ystep + (*helpscreen_eltext[i][1] ? 0 : 8),
939              helpscreen_eltext[i][0], FONT_TEXT_2);
940     DrawText(xstart, ystart + (i - start) * ystep + 16,
941              helpscreen_eltext[i][1], FONT_TEXT_2);
942   }
943
944   DrawTextFCentered(ybottom, FONT_TEXT_4,
945                     "Press any key or button for next page");
946 }
947
948 void DrawHelpScreenMusicText(int num)
949 {
950   int ystart = 150, ystep = 30;
951   int ybottom = SYSIZE - 20;
952
953   FadeSounds();
954   ClearWindow();
955   DrawHeadline();
956
957   DrawTextFCentered(100, FONT_TEXT_1, "The game background music loops:");
958
959   DrawTextFCentered(ystart + 0 * ystep, FONT_TEXT_2, "Excerpt from");
960   DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_3,
961                     "\"%s\"", helpscreen_music[num][0]);
962   DrawTextFCentered(ystart + 2 * ystep, FONT_TEXT_2, "by");
963   DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_3,
964                     "%s", helpscreen_music[num][1]);
965   DrawTextFCentered(ystart + 4 * ystep, FONT_TEXT_2, "from the album");
966   DrawTextFCentered(ystart + 5 * ystep, FONT_TEXT_3,
967                     "\"%s\"", helpscreen_music[num][2]);
968
969   DrawTextFCentered(ybottom, FONT_TEXT_4,
970                     "Press any key or button for next page");
971
972 #if 0
973   PlaySoundLoop(background_loop[num]);
974 #endif
975 }
976
977 void DrawHelpScreenCreditsText()
978 {
979   int ystart = 150, ystep = 30;
980   int ybottom = SYSIZE - 20;
981
982   FadeSounds();
983   ClearWindow();
984   DrawHeadline();
985
986   DrawTextFCentered(100, FONT_TEXT_1, "Credits:");
987   DrawTextFCentered(ystart + 0 * ystep, FONT_TEXT_2, "DOS port of the game:");
988   DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_3, "Guido Schulz");
989   DrawTextFCentered(ystart + 2 * ystep, FONT_TEXT_2, "Additional toons:");
990   DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_3, "Karl Hörnell");
991   DrawTextFCentered(ystart + 5 * ystep, FONT_TEXT_2,
992                     "...and many thanks to all contributors");
993   DrawTextFCentered(ystart + 6 * ystep, FONT_TEXT_2, "of new levels!");
994
995   DrawTextFCentered(ybottom, FONT_TEXT_4,
996                     "Press any key or button for next page");
997 }
998
999 void DrawHelpScreenContactText()
1000 {
1001   int ystart = 150, ystep = 30;
1002   int ybottom = SYSIZE - 20;
1003
1004   ClearWindow();
1005   DrawHeadline();
1006
1007   DrawTextFCentered(100, FONT_TEXT_1, "Program information:");
1008
1009   DrawTextFCentered(ystart + 0 * ystep, FONT_TEXT_2,
1010                     "This game is Freeware!");
1011   DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_2,
1012                     "If you like it, send e-mail to:");
1013   DrawTextFCentered(ystart + 2 * ystep, FONT_TEXT_3,
1014                     "info@artsoft.org");
1015   DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_2,
1016                     "or SnailMail to:");
1017   DrawTextFCentered(ystart + 4 * ystep + 0, FONT_TEXT_3,
1018                     "Holger Schemel");
1019   DrawTextFCentered(ystart + 4 * ystep + 20, FONT_TEXT_3,
1020                     "Detmolder Strasse 189");
1021   DrawTextFCentered(ystart + 4 * ystep + 40, FONT_TEXT_3,
1022                     "33604 Bielefeld");
1023   DrawTextFCentered(ystart + 4 * ystep + 60, FONT_TEXT_3,
1024                     "Germany");
1025
1026   DrawTextFCentered(ystart + 7 * ystep, FONT_TEXT_2,
1027                     "If you have created new levels,");
1028   DrawTextFCentered(ystart + 8 * ystep, FONT_TEXT_2,
1029                     "send them to me to include them!");
1030   DrawTextFCentered(ystart + 9 * ystep, FONT_TEXT_2,
1031                     ":-)");
1032
1033   DrawTextFCentered(ybottom, FONT_TEXT_4,
1034                     "Press any key or button for main menu");
1035 }
1036
1037 void DrawHelpScreen()
1038 {
1039   int i;
1040
1041   UnmapAllGadgets();
1042   CloseDoor(DOOR_CLOSE_2);
1043
1044   for(i=0;i<MAX_HELPSCREEN_ELS;i++)
1045     helpscreen_step[i] = helpscreen_frame[i] = 0;
1046   helpscreen_musicpos = 0;
1047   helpscreen_state = 0;
1048
1049   DrawHelpScreenElText(0);
1050   DrawHelpScreenElAction(0);
1051
1052   FadeToFront();
1053   InitAnimation();
1054
1055 #if 0
1056   PlaySoundLoop(SND_BACKGROUND_INFO);
1057 #else
1058   PlaySound_Menu_Start(SND_BACKGROUND_INFO);
1059 #endif
1060 }
1061
1062 void HandleHelpScreen(int button)
1063 {
1064   static unsigned long hs_delay = 0;
1065   int num_helpscreen_els_pages =
1066     (num_helpscreen_els + MAX_HELPSCREEN_ELS-1) / MAX_HELPSCREEN_ELS;
1067   int button_released = !button;
1068   int i;
1069
1070   if (button_released)
1071   {
1072     if (helpscreen_state < num_helpscreen_els_pages - 1)
1073     {
1074       for(i=0;i<MAX_HELPSCREEN_ELS;i++)
1075         helpscreen_step[i] = helpscreen_frame[i] = 0;
1076       helpscreen_state++;
1077
1078       FrameCounter = 0;
1079       DrawHelpScreenElText(helpscreen_state * MAX_HELPSCREEN_ELS);
1080       DrawHelpScreenElAction(helpscreen_state * MAX_HELPSCREEN_ELS);
1081     }
1082     else if (helpscreen_state <
1083              num_helpscreen_els_pages + num_helpscreen_music - 1)
1084     {
1085       helpscreen_state++;
1086       DrawHelpScreenMusicText(helpscreen_state - num_helpscreen_els_pages);
1087     }
1088     else if (helpscreen_state ==
1089              num_helpscreen_els_pages + num_helpscreen_music - 1)
1090     {
1091       helpscreen_state++;
1092       DrawHelpScreenCreditsText();
1093     }
1094     else if (helpscreen_state ==
1095              num_helpscreen_els_pages + num_helpscreen_music)
1096     {
1097       helpscreen_state++;
1098       DrawHelpScreenContactText();
1099     }
1100     else
1101     {
1102       FadeSounds();
1103
1104       game_status = GAME_MODE_MAIN;
1105       DrawMainMenu();
1106     }
1107   }
1108   else
1109   {
1110     if (DelayReached(&hs_delay, GAME_FRAME_DELAY))
1111     {
1112       if (helpscreen_state < num_helpscreen_els_pages)
1113         DrawHelpScreenElAction(helpscreen_state * MAX_HELPSCREEN_ELS);
1114     }
1115
1116     /* !!! workaround for playing "music" that is really a sound loop (and
1117        must therefore periodically be reactivated with the current sound
1118        engine !!! */
1119 #if 0
1120     PlaySoundLoop(SND_BACKGROUND_INFO);
1121 #else
1122     PlaySound_Menu_Continue(SND_BACKGROUND_INFO);
1123 #endif
1124
1125     DoAnimation();
1126   }
1127
1128   BackToFront();
1129 }
1130
1131 void HandleTypeName(int newxpos, Key key)
1132 {
1133   static int xpos = 0, ypos = 2;
1134   int font_width = getFontWidth(FONT_INPUT_1_ACTIVE);
1135   int name_width = getFontWidth(FONT_MENU_1) * strlen("Name:");
1136   int startx = mSX + 32 + name_width;
1137   int starty = mSY + ypos * 32;
1138
1139   if (newxpos)
1140   {
1141     xpos = newxpos;
1142
1143     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1144     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1145
1146     return;
1147   }
1148
1149   if (((key >= KSYM_A && key <= KSYM_Z) ||
1150        (key >= KSYM_a && key <= KSYM_z)) && 
1151       xpos < MAX_PLAYER_NAME_LEN)
1152   {
1153     char ascii;
1154
1155     if (key >= KSYM_A && key <= KSYM_Z)
1156       ascii = 'A' + (char)(key - KSYM_A);
1157     else
1158       ascii = 'a' + (char)(key - KSYM_a);
1159
1160     setup.player_name[xpos] = ascii;
1161     setup.player_name[xpos + 1] = 0;
1162     xpos++;
1163
1164     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1165     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1166   }
1167   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
1168   {
1169     xpos--;
1170     setup.player_name[xpos] = 0;
1171
1172     DrawText(startx + xpos * font_width, starty, "_ ", FONT_INPUT_1_ACTIVE);
1173   }
1174   else if (key == KSYM_Return && xpos > 0)
1175   {
1176     DrawText(startx, starty, setup.player_name, FONT_INPUT_1);
1177     DrawText(startx + xpos * font_width, starty, " ", FONT_INPUT_1_ACTIVE);
1178
1179     SaveSetup();
1180     game_status = GAME_MODE_MAIN;
1181   }
1182
1183   BackToFront();
1184 }
1185
1186 static void DrawChooseTree(TreeInfo **ti_ptr)
1187 {
1188   UnmapAllGadgets();
1189   CloseDoor(DOOR_CLOSE_2);
1190
1191   ClearWindow();
1192
1193   HandleChooseTree(0,0, 0,0, MB_MENU_INITIALIZE, ti_ptr);
1194   MapChooseTreeGadgets(*ti_ptr);
1195
1196   FadeToFront();
1197   InitAnimation();
1198 }
1199
1200 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1201 {
1202   struct GadgetInfo *gi = screen_gadget[id];
1203   int items_max, items_visible, item_position;
1204
1205   items_max = numTreeInfoInGroup(ti);
1206   items_visible = MAX_MENU_ENTRIES_ON_SCREEN - 1;
1207   item_position = first_entry;
1208
1209   if (item_position > items_max - items_visible)
1210     item_position = items_max - items_visible;
1211
1212   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1213                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1214 }
1215
1216 static void drawChooseTreeList(int first_entry, int num_page_entries,
1217                                TreeInfo *ti)
1218 {
1219   int i;
1220   char buffer[SCR_FIELDX * 2];
1221   int max_buffer_len = (SCR_FIELDX - 2) * 2;
1222   int num_entries = numTreeInfoInGroup(ti);
1223   char *title_string = NULL;
1224   int offset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : 16);
1225   int last_game_status = game_status;   /* save current game status */
1226
1227   DrawBackground(SX, SY, SXSIZE - 32, SYSIZE);
1228   redraw_mask |= REDRAW_FIELD;
1229
1230   title_string =
1231     (ti->type == TREE_TYPE_LEVEL_DIR ? "Level Directories" :
1232      ti->type == TREE_TYPE_GRAPHICS_DIR ? "Custom Graphics" :
1233      ti->type == TREE_TYPE_SOUNDS_DIR ? "Custom Sounds" :
1234      ti->type == TREE_TYPE_MUSIC_DIR ? "Custom Music" : "");
1235
1236   DrawText(SX + offset, SY + offset, title_string, FONT_TITLE_1);
1237
1238   /* force LEVELS font on artwork setup screen */
1239   game_status = GAME_MODE_LEVELS;
1240
1241   for(i=0; i<num_page_entries; i++)
1242   {
1243     TreeInfo *node, *node_first;
1244     int entry_pos = first_entry + i;
1245     int ypos = MENU_SCREEN_START_YPOS + i;
1246
1247     node_first = getTreeInfoFirstGroupEntry(ti);
1248     node = getTreeInfoFromPos(node_first, entry_pos);
1249
1250     strncpy(buffer, node->name , max_buffer_len);
1251     buffer[max_buffer_len] = '\0';
1252
1253     DrawText(mSX + 32, mSY + ypos * 32, buffer, FONT_TEXT_1 + node->color);
1254
1255     if (node->parent_link)
1256       initCursor(i, IMG_MENU_BUTTON_LEFT);
1257     else if (node->level_group)
1258       initCursor(i, IMG_MENU_BUTTON_RIGHT);
1259     else
1260       initCursor(i, IMG_MENU_BUTTON);
1261   }
1262
1263   if (first_entry > 0)
1264   {
1265     int ypos = 1;
1266
1267     DrawBackground(SX, SY + ypos * 32, TILEX, TILEY);
1268     DrawGraphicThruMask(0, ypos, IMG_MENU_BUTTON_UP, 0);
1269   }
1270
1271   if (first_entry + num_page_entries < num_entries)
1272   {
1273     int ypos = MAX_MENU_ENTRIES_ON_SCREEN + 1;
1274
1275     DrawBackground(SX, SY + ypos * 32, TILEX, TILEY);
1276     DrawGraphicThruMask(0, ypos, IMG_MENU_BUTTON_DOWN, 0);
1277   }
1278
1279   game_status = last_game_status;       /* restore current game status */
1280 }
1281
1282 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
1283 {
1284   TreeInfo *node, *node_first;
1285   int x, last_redraw_mask = redraw_mask;
1286
1287   if (ti->type != TREE_TYPE_LEVEL_DIR)
1288     return;
1289
1290   node_first = getTreeInfoFirstGroupEntry(ti);
1291   node = getTreeInfoFromPos(node_first, entry_pos);
1292
1293   DrawBackground(SX + 32, SY + 32, SXSIZE - 64, 32);
1294
1295   if (node->parent_link)
1296     DrawTextFCentered(40, FONT_TITLE_2, "leave group \"%s\"",
1297                       node->class_desc);
1298   else if (node->level_group)
1299     DrawTextFCentered(40, FONT_TITLE_2, "enter group \"%s\"",
1300                       node->class_desc);
1301   else if (ti->type == TREE_TYPE_LEVEL_DIR)
1302     DrawTextFCentered(40, FONT_TITLE_2, "%3d levels (%s)",
1303                       node->levels, node->class_desc);
1304
1305   /* let BackToFront() redraw only what is needed */
1306   redraw_mask = last_redraw_mask | REDRAW_TILES;
1307   for (x=0; x<SCR_FIELDX; x++)
1308     MarkTileDirty(x, 1);
1309 }
1310
1311 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
1312                              TreeInfo **ti_ptr)
1313 {
1314   static unsigned long choose_delay = 0;
1315   TreeInfo *ti = *ti_ptr;
1316   int x = 0;
1317   int y = ti->cl_cursor;
1318   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1319   int num_entries = numTreeInfoInGroup(ti);
1320   int num_page_entries;
1321
1322   if (num_entries <= MAX_MENU_ENTRIES_ON_SCREEN)
1323     num_page_entries = num_entries;
1324   else
1325     num_page_entries = MAX_MENU_ENTRIES_ON_SCREEN - 1;
1326
1327   if (button == MB_MENU_INITIALIZE)
1328   {
1329     int entry_pos = posTreeInfo(ti);
1330
1331     if (ti->cl_first == -1)
1332     {
1333       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1334       ti->cl_cursor =
1335         entry_pos - ti->cl_first;
1336     }
1337
1338     if (dx == 999)      /* first entry is set by scrollbar position */
1339       ti->cl_first = dy;
1340     else
1341       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1342                                 ti->cl_first, ti);
1343
1344     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1345     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1346     drawCursor(ti->cl_cursor, FC_RED);
1347     return;
1348   }
1349   else if (button == MB_MENU_LEAVE)
1350   {
1351     if (ti->node_parent)
1352     {
1353       *ti_ptr = ti->node_parent;
1354       DrawChooseTree(ti_ptr);
1355     }
1356     else if (game_status == GAME_MODE_SETUP)
1357     {
1358       execSetupArtwork();
1359     }
1360     else
1361     {
1362       game_status = GAME_MODE_MAIN;
1363       DrawMainMenu();
1364     }
1365
1366     return;
1367   }
1368
1369   if (mx || my)         /* mouse input */
1370   {
1371     x = (mx - SX) / 32;
1372     y = (my - SY) / 32 - MENU_SCREEN_START_YPOS;
1373   }
1374   else if (dx || dy)    /* keyboard input */
1375   {
1376     if (dy)
1377       y = ti->cl_cursor + dy;
1378
1379     if (ABS(dy) == SCR_FIELDY)  /* handle KSYM_Page_Up, KSYM_Page_Down */
1380     {
1381       dy = SIGN(dy);
1382       step = num_page_entries - 1;
1383       y = (dy < 0 ? -1 : num_page_entries);
1384     }
1385   }
1386
1387   if (x == 0 && y == -1)
1388   {
1389     if (ti->cl_first > 0 &&
1390         (dy || DelayReached(&choose_delay, GADGET_FRAME_DELAY)))
1391     {
1392       ti->cl_first -= step;
1393       if (ti->cl_first < 0)
1394         ti->cl_first = 0;
1395
1396       drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1397       drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1398       drawCursor(ti->cl_cursor, FC_RED);
1399       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1400                                 ti->cl_first, ti);
1401       return;
1402     }
1403   }
1404   else if (x == 0 && y > num_page_entries - 1)
1405   {
1406     if (ti->cl_first + num_page_entries < num_entries &&
1407         (dy || DelayReached(&choose_delay, GADGET_FRAME_DELAY)))
1408     {
1409       ti->cl_first += step;
1410       if (ti->cl_first + num_page_entries > num_entries)
1411         ti->cl_first = MAX(0, num_entries - num_page_entries);
1412
1413       drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1414       drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1415       drawCursor(ti->cl_cursor, FC_RED);
1416       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1417                                 ti->cl_first, ti);
1418       return;
1419     }
1420   }
1421
1422   if (dx == 1)
1423   {
1424     TreeInfo *node_first, *node_cursor;
1425     int entry_pos = ti->cl_first + y;
1426
1427     node_first = getTreeInfoFirstGroupEntry(ti);
1428     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1429
1430     if (node_cursor->node_group)
1431     {
1432       node_cursor->cl_first = ti->cl_first;
1433       node_cursor->cl_cursor = ti->cl_cursor;
1434       *ti_ptr = node_cursor->node_group;
1435       DrawChooseTree(ti_ptr);
1436       return;
1437     }
1438   }
1439   else if (dx == -1 && ti->node_parent)
1440   {
1441     *ti_ptr = ti->node_parent;
1442     DrawChooseTree(ti_ptr);
1443     return;
1444   }
1445
1446   if (x == 0 && y >= 0 && y < num_page_entries)
1447   {
1448     if (button)
1449     {
1450       if (y != ti->cl_cursor)
1451       {
1452         drawCursor(y, FC_RED);
1453         drawCursor(ti->cl_cursor, FC_BLUE);
1454         drawChooseTreeInfo(ti->cl_first + y, ti);
1455         ti->cl_cursor = y;
1456       }
1457     }
1458     else
1459     {
1460       TreeInfo *node_first, *node_cursor;
1461       int entry_pos = ti->cl_first + y;
1462
1463       node_first = getTreeInfoFirstGroupEntry(ti);
1464       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1465
1466       if (node_cursor->node_group)
1467       {
1468         node_cursor->cl_first = ti->cl_first;
1469         node_cursor->cl_cursor = ti->cl_cursor;
1470         *ti_ptr = node_cursor->node_group;
1471         DrawChooseTree(ti_ptr);
1472       }
1473       else if (node_cursor->parent_link)
1474       {
1475         *ti_ptr = node_cursor->node_parent;
1476         DrawChooseTree(ti_ptr);
1477       }
1478       else
1479       {
1480         node_cursor->cl_first = ti->cl_first;
1481         node_cursor->cl_cursor = ti->cl_cursor;
1482         *ti_ptr = node_cursor;
1483
1484         if (ti->type == TREE_TYPE_LEVEL_DIR)
1485         {
1486           LoadLevelSetup_SeriesInfo();
1487
1488           SaveLevelSetup_LastSeries();
1489           SaveLevelSetup_SeriesInfo();
1490           TapeErase();
1491         }
1492
1493         if (game_status == GAME_MODE_SETUP)
1494         {
1495           execSetupArtwork();
1496         }
1497         else
1498         {
1499           game_status = GAME_MODE_MAIN;
1500           DrawMainMenu();
1501         }
1502       }
1503     }
1504   }
1505
1506   BackToFront();
1507
1508   if (game_status == GAME_MODE_LEVELS || game_status == GAME_MODE_SETUP)
1509     DoAnimation();
1510 }
1511
1512 void DrawChooseLevel()
1513 {
1514   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
1515
1516   DrawChooseTree(&leveldir_current);
1517 }
1518
1519 void HandleChooseLevel(int mx, int my, int dx, int dy, int button)
1520 {
1521   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
1522 }
1523
1524 void DrawHallOfFame(int highlight_position)
1525 {
1526   UnmapAllGadgets();
1527   FadeSounds();
1528   CloseDoor(DOOR_CLOSE_2);
1529
1530   if (highlight_position < 0) 
1531     LoadScore(level_nr);
1532
1533   FadeToFront();
1534   InitAnimation();
1535
1536   HandleHallOfFame(highlight_position,0, 0,0, MB_MENU_INITIALIZE);
1537
1538 #if 0
1539   PlaySound(SND_BACKGROUND_SCORES);
1540 #else
1541   PlaySound_Menu_Start(SND_BACKGROUND_SCORES);
1542 #endif
1543 }
1544
1545 static void drawHallOfFameList(int first_entry, int highlight_position)
1546 {
1547   int i;
1548
1549   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
1550   ClearWindow();
1551
1552   DrawText(mSX + 80, mSY + 8, "Hall Of Fame", FONT_TITLE_1);
1553   DrawTextFCentered(46, FONT_TITLE_2, "HighScores of Level %d", level_nr);
1554
1555   for(i=0; i<MAX_MENU_ENTRIES_ON_SCREEN; i++)
1556   {
1557     int entry = first_entry + i;
1558     boolean active = (entry == highlight_position);
1559     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
1560     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
1561     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
1562     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
1563     int dx1 = 3 * getFontWidth(font_nr1);
1564     int dx2 = dx1 + getFontWidth(font_nr1);
1565     int dx3 = dx2 + 25 * getFontWidth(font_nr3);
1566     int sy = mSY + 64 + i * 32;
1567
1568     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
1569     DrawText(mSX + dx1, sy, ".", font_nr1);
1570     DrawText(mSX + dx2, sy, ".........................", font_nr3);
1571     if (strcmp(highscore[entry].Name, EMPTY_PLAYER_NAME) != 0)
1572       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
1573     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
1574   }
1575 }
1576
1577 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
1578 {
1579   static int first_entry = 0;
1580   static int highlight_position = 0;
1581   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1582   int button_released = !button;
1583
1584   if (button == MB_MENU_INITIALIZE)
1585   {
1586     first_entry = 0;
1587     highlight_position = mx;
1588     drawHallOfFameList(first_entry, highlight_position);
1589     return;
1590   }
1591
1592   if (ABS(dy) == SCR_FIELDY)    /* handle KSYM_Page_Up, KSYM_Page_Down */
1593     step = MAX_MENU_ENTRIES_ON_SCREEN - 1;
1594
1595   if (dy < 0)
1596   {
1597     if (first_entry > 0)
1598     {
1599       first_entry -= step;
1600       if (first_entry < 0)
1601         first_entry = 0;
1602
1603       drawHallOfFameList(first_entry, highlight_position);
1604       return;
1605     }
1606   }
1607   else if (dy > 0)
1608   {
1609     if (first_entry + MAX_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
1610     {
1611       first_entry += step;
1612       if (first_entry + MAX_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
1613         first_entry = MAX(0, MAX_SCORE_ENTRIES - MAX_MENU_ENTRIES_ON_SCREEN);
1614
1615       drawHallOfFameList(first_entry, highlight_position);
1616       return;
1617     }
1618   }
1619
1620   if (button_released)
1621   {
1622     FadeSound(SND_BACKGROUND_SCORES);
1623     game_status = GAME_MODE_MAIN;
1624     DrawMainMenu();
1625   }
1626
1627   BackToFront();
1628
1629   if (game_status == GAME_MODE_SCORES)
1630   {
1631     DoAnimation();
1632 #if 1
1633     PlaySound_Menu_Continue(SND_BACKGROUND_SCORES);
1634 #endif
1635   }
1636 }
1637
1638
1639 /* ========================================================================= */
1640 /* setup screen functions                                                    */
1641 /* ========================================================================= */
1642
1643 static struct TokenInfo *setup_info;
1644 static int num_setup_info;
1645
1646 static char *graphics_set_name;
1647 static char *sounds_set_name;
1648 static char *music_set_name;
1649
1650 static void execSetupMain()
1651 {
1652   setup_mode = SETUP_MODE_MAIN;
1653   DrawSetupScreen();
1654 }
1655
1656 static void execSetupGame()
1657 {
1658   setup_mode = SETUP_MODE_GAME;
1659   DrawSetupScreen();
1660 }
1661
1662 static void execSetupEditor()
1663 {
1664   setup_mode = SETUP_MODE_EDITOR;
1665   DrawSetupScreen();
1666 }
1667
1668 static void execSetupGraphics()
1669 {
1670   setup_mode = SETUP_MODE_GRAPHICS;
1671   DrawSetupScreen();
1672 }
1673
1674 static void execSetupSound()
1675 {
1676   setup_mode = SETUP_MODE_SOUND;
1677   DrawSetupScreen();
1678 }
1679
1680 static void execSetupArtwork()
1681 {
1682   /* needed if last screen (setup choice) changed graphics, sounds or music */
1683   ReloadCustomArtwork();
1684
1685   setup.graphics_set = artwork.gfx_current->identifier;
1686   setup.sounds_set = artwork.snd_current->identifier;
1687   setup.music_set = artwork.mus_current->identifier;
1688
1689   /* needed for displaying artwork name instead of artwork identifier */
1690   graphics_set_name = artwork.gfx_current->name;
1691   sounds_set_name = artwork.snd_current->name;
1692   music_set_name = artwork.mus_current->name;
1693
1694   setup_mode = SETUP_MODE_ARTWORK;
1695   DrawSetupScreen();
1696 }
1697
1698 static void execSetupChooseGraphics()
1699 {
1700   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
1701   DrawSetupScreen();
1702 }
1703
1704 static void execSetupChooseSounds()
1705 {
1706   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
1707   DrawSetupScreen();
1708 }
1709
1710 static void execSetupChooseMusic()
1711 {
1712   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
1713   DrawSetupScreen();
1714 }
1715
1716 static void execSetupInput()
1717 {
1718   setup_mode = SETUP_MODE_INPUT;
1719   DrawSetupScreen();
1720 }
1721
1722 static void execSetupShortcut()
1723 {
1724   setup_mode = SETUP_MODE_SHORTCUT;
1725   DrawSetupScreen();
1726 }
1727
1728 static void execExitSetup()
1729 {
1730   game_status = GAME_MODE_MAIN;
1731   DrawMainMenu();
1732 }
1733
1734 static void execSaveAndExitSetup()
1735 {
1736   SaveSetup();
1737   execExitSetup();
1738 }
1739
1740 static struct TokenInfo setup_info_main[] =
1741 {
1742   { TYPE_ENTER_MENU,    execSetupGame,          "Game Settings"         },
1743   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor Settings"       },
1744   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
1745   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
1746   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
1747   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
1748   { TYPE_ENTER_MENU,    execSetupShortcut,      "Key Shortcuts"         },
1749   { TYPE_EMPTY,         NULL,                   ""                      },
1750   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
1751   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
1752   { 0,                  NULL,                   NULL                    }
1753 };
1754
1755 static struct TokenInfo setup_info_game[] =
1756 {
1757   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode:"            },
1758   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
1759   { TYPE_SWITCH,        &setup.time_limit,      "Timelimit:"            },
1760   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record:"          },
1761   { TYPE_EMPTY,         NULL,                   ""                      },
1762   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1763   { 0,                  NULL,                   NULL                    }
1764 };
1765
1766 static struct TokenInfo setup_info_editor[] =
1767 {
1768   { TYPE_STRING,        NULL,                   "Offer Special Elements:"},
1769   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "BoulderDash:"  },
1770   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
1771   { TYPE_SWITCH,        &setup.editor.el_more,          "More:"         },
1772   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
1773   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
1774   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamd. Caves:" },
1775   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX Boulderd.:" },
1776   { TYPE_SWITCH,        &setup.editor.el_chars,         "Characters:"   },
1777   { TYPE_SWITCH,        &setup.editor.el_custom,        "Custom:"       },
1778   { TYPE_EMPTY,         NULL,                   ""                      },
1779   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1780   { 0,                  NULL,                   NULL                    }
1781 };
1782
1783 static struct TokenInfo setup_info_graphics[] =
1784 {
1785   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
1786   { TYPE_SWITCH,        &setup.scroll_delay,    "Scroll Delay:"         },
1787   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scroll.:"         },
1788 #if 0
1789   { TYPE_SWITCH,        &setup.double_buffering,"Buffered gfx:"         },
1790   { TYPE_SWITCH,        &setup.fading,          "Fading:"               },
1791 #endif
1792   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Doors:"          },
1793   { TYPE_SWITCH,        &setup.toons,           "Toons:"                },
1794   { TYPE_EMPTY,         NULL,                   ""                      },
1795   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1796   { 0,                  NULL,                   NULL                    }
1797 };
1798
1799 static struct TokenInfo setup_info_sound[] =
1800 {
1801   { TYPE_SWITCH,        &setup.sound,           "Sound:",               },
1802   { TYPE_EMPTY,         NULL,                   ""                      },
1803   { TYPE_SWITCH,        &setup.sound_simple,    "Simple Sound:"         },
1804   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Loops:"          },
1805   { TYPE_SWITCH,        &setup.sound_music,     "Game Music:"           },
1806   { TYPE_EMPTY,         NULL,                   ""                      },
1807   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1808   { 0,                  NULL,                   NULL                    }
1809 };
1810
1811 static struct TokenInfo setup_info_artwork[] =
1812 {
1813   { TYPE_ENTER_MENU,    execSetupChooseGraphics,"Custom Graphics"       },
1814   { TYPE_STRING,        &graphics_set_name,     ""                      },
1815   { TYPE_ENTER_MENU,    execSetupChooseSounds,  "Custom Sounds"         },
1816   { TYPE_STRING,        &sounds_set_name,       ""                      },
1817   { TYPE_ENTER_MENU,    execSetupChooseMusic,   "Custom Music"          },
1818   { TYPE_STRING,        &music_set_name,        ""                      },
1819   { TYPE_EMPTY,         NULL,                   ""                      },
1820   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
1821   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
1822   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
1823   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
1824   { TYPE_EMPTY,         NULL,                   ""                      },
1825   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1826   { 0,                  NULL,                   NULL                    }
1827 };
1828
1829 static struct TokenInfo setup_info_shortcut[] =
1830 {
1831   { TYPE_KEYTEXT,       NULL,                   "Quick Save Game:",     },
1832   { TYPE_KEY,           &setup.shortcut.save_game,      ""              },
1833   { TYPE_KEYTEXT,       NULL,                   "Quick Load Game:",     },
1834   { TYPE_KEY,           &setup.shortcut.load_game,      ""              },
1835   { TYPE_KEYTEXT,       NULL,                   "Toggle Pause:",        },
1836   { TYPE_KEY,           &setup.shortcut.toggle_pause,   ""              },
1837   { TYPE_EMPTY,         NULL,                   ""                      },
1838   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on Esc:"           },
1839   { TYPE_EMPTY,         NULL,                   ""                      },
1840   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1841   { 0,                  NULL,                   NULL                    }
1842 };
1843
1844 static Key getSetupKey()
1845 {
1846   Key key = KSYM_UNDEFINED;
1847   boolean got_key_event = FALSE;
1848
1849   while (!got_key_event)
1850   {
1851     if (PendingEvent())         /* got event */
1852     {
1853       Event event;
1854
1855       NextEvent(&event);
1856
1857       switch(event.type)
1858       {
1859         case EVENT_KEYPRESS:
1860           {
1861             key = GetEventKey((KeyEvent *)&event, TRUE);
1862
1863             /* press 'Escape' or 'Enter' to keep the existing key binding */
1864             if (key == KSYM_Escape || key == KSYM_Return)
1865               key = KSYM_UNDEFINED;     /* keep old value */
1866
1867             got_key_event = TRUE;
1868           }
1869           break;
1870
1871         case EVENT_KEYRELEASE:
1872           key_joystick_mapping = 0;
1873           break;
1874
1875         default:
1876           HandleOtherEvents(&event);
1877           break;
1878       }
1879     }
1880
1881     BackToFront();
1882     DoAnimation();
1883
1884     /* don't eat all CPU time */
1885     Delay(10);
1886   }
1887
1888   return key;
1889 }
1890
1891 static void drawSetupValue(int pos)
1892 {
1893   int xpos = MENU_SCREEN_VALUE_XPOS;
1894   int ypos = MENU_SCREEN_START_YPOS + pos;
1895   int font_nr = FONT_VALUE_1;
1896   char *value_string = getSetupValue(setup_info[pos].type & ~TYPE_GHOSTED,
1897                                      setup_info[pos].value);
1898
1899   if (value_string == NULL)
1900     return;
1901
1902   if (setup_info[pos].type & TYPE_KEY)
1903   {
1904     xpos = 3;
1905
1906     if (setup_info[pos].type & TYPE_QUERY)
1907     {
1908       value_string = "<press key>";
1909       font_nr = FONT_INPUT_1_ACTIVE;
1910     }
1911   }
1912   else if (setup_info[pos].type & TYPE_STRING)
1913   {
1914     int max_value_len = (SCR_FIELDX - 2) * 2;
1915
1916     xpos = 1;
1917     font_nr = FONT_VALUE_2;
1918
1919     if (strlen(value_string) > max_value_len)
1920       value_string[max_value_len] = '\0';
1921   }
1922   else if (setup_info[pos].type & TYPE_BOOLEAN_STYLE &&
1923            !*(boolean *)(setup_info[pos].value))
1924     font_nr = FONT_OPTION_OFF;
1925
1926   DrawText(mSX + xpos * 32, mSY + ypos * 32,
1927            (xpos == 3 ? "              " : "   "), font_nr);
1928   DrawText(mSX + xpos * 32, mSY + ypos * 32, value_string, font_nr);
1929 }
1930
1931 static void changeSetupValue(int pos)
1932 {
1933   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
1934   {
1935     *(boolean *)setup_info[pos].value ^= TRUE;
1936   }
1937   else if (setup_info[pos].type & TYPE_KEY)
1938   {
1939     Key key;
1940
1941     setup_info[pos].type |= TYPE_QUERY;
1942     drawSetupValue(pos);
1943     setup_info[pos].type &= ~TYPE_QUERY;
1944
1945     key = getSetupKey();
1946     if (key != KSYM_UNDEFINED)
1947       *(Key *)setup_info[pos].value = key;
1948   }
1949
1950   drawSetupValue(pos);
1951 }
1952
1953 static void DrawSetupScreen_Generic()
1954 {
1955   char *title_string = NULL;
1956   int i;
1957
1958   UnmapAllGadgets();
1959   CloseDoor(DOOR_CLOSE_2);
1960
1961   ClearWindow();
1962
1963   if (setup_mode == SETUP_MODE_MAIN)
1964   {
1965     setup_info = setup_info_main;
1966     title_string = "Setup";
1967   }
1968   else if (setup_mode == SETUP_MODE_GAME)
1969   {
1970     setup_info = setup_info_game;
1971     title_string = "Setup Game";
1972   }
1973   else if (setup_mode == SETUP_MODE_EDITOR)
1974   {
1975     setup_info = setup_info_editor;
1976     title_string = "Setup Editor";
1977   }
1978   else if (setup_mode == SETUP_MODE_GRAPHICS)
1979   {
1980     setup_info = setup_info_graphics;
1981     title_string = "Setup Graphics";
1982   }
1983   else if (setup_mode == SETUP_MODE_SOUND)
1984   {
1985     setup_info = setup_info_sound;
1986     title_string = "Setup Sound";
1987   }
1988   else if (setup_mode == SETUP_MODE_ARTWORK)
1989   {
1990     setup_info = setup_info_artwork;
1991     title_string = "Custom Artwork";
1992   }
1993   else if (setup_mode == SETUP_MODE_SHORTCUT)
1994   {
1995     setup_info = setup_info_shortcut;
1996     title_string = "Setup Shortcuts";
1997   }
1998
1999   DrawText(mSX + 16, mSY + 16, title_string, FONT_TITLE_1);
2000
2001   num_setup_info = 0;
2002   for(i=0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
2003   {
2004     void *value_ptr = setup_info[i].value;
2005     int ypos = MENU_SCREEN_START_YPOS + i;
2006     int font_nr = FONT_MENU_1;
2007
2008     /* set some entries to "unchangeable" according to other variables */
2009     if ((value_ptr == &setup.sound       && !audio.sound_available) ||
2010         (value_ptr == &setup.sound_loops && !audio.loops_available) ||
2011         (value_ptr == &setup.sound_music && !audio.music_available) ||
2012         (value_ptr == &setup.fullscreen  && !video.fullscreen_available))
2013       setup_info[i].type |= TYPE_GHOSTED;
2014
2015     if (setup_info[i].type & TYPE_STRING)
2016       font_nr = FONT_MENU_2;
2017
2018     DrawText(mSX + 32, mSY + ypos * 32, setup_info[i].text, font_nr);
2019
2020     if (setup_info[i].type & TYPE_ENTER_MENU)
2021       initCursor(i, IMG_MENU_BUTTON_RIGHT);
2022     else if (setup_info[i].type & TYPE_LEAVE_MENU)
2023       initCursor(i, IMG_MENU_BUTTON_LEFT);
2024     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
2025       initCursor(i, IMG_MENU_BUTTON);
2026
2027     if (setup_info[i].type & TYPE_VALUE)
2028       drawSetupValue(i);
2029
2030     num_setup_info++;
2031   }
2032
2033   FadeToFront();
2034   InitAnimation();
2035   HandleSetupScreen_Generic(0,0,0,0,MB_MENU_INITIALIZE);
2036 }
2037
2038 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
2039 {
2040   static int choice_store[MAX_SETUP_MODES];
2041   int choice = choice_store[setup_mode];        /* always starts with 0 */
2042   int x = 0;
2043   int y = choice;
2044
2045   if (button == MB_MENU_INITIALIZE)
2046   {
2047     /* advance to first valid menu entry */
2048     while (choice < num_setup_info &&
2049            (setup_info[choice].type & TYPE_SKIP_ENTRY))
2050       choice++;
2051     choice_store[setup_mode] = choice;
2052
2053     drawCursor(choice, FC_RED);
2054     return;
2055   }
2056   else if (button == MB_MENU_LEAVE)
2057   {
2058     for (y=0; y<num_setup_info; y++)
2059     {
2060       if (setup_info[y].type & TYPE_LEAVE_MENU)
2061       {
2062         void (*menu_callback_function)(void) = setup_info[y].value;
2063
2064         menu_callback_function();
2065         break;  /* absolutely needed because function changes 'setup_info'! */
2066       }
2067     }
2068
2069     return;
2070   }
2071
2072   if (mx || my)         /* mouse input */
2073   {
2074     x = (mx - SX) / 32;
2075     y = (my - SY) / 32 - MENU_SCREEN_START_YPOS;
2076   }
2077   else if (dx || dy)    /* keyboard input */
2078   {
2079     if (dx)
2080     {
2081       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE_MENU : TYPE_ENTER_MENU);
2082
2083       if ((setup_info[choice].type & menu_navigation_type) ||
2084           (setup_info[choice].type & TYPE_BOOLEAN_STYLE))
2085         button = MB_MENU_CHOICE;
2086     }
2087     else if (dy)
2088       y = choice + dy;
2089
2090     /* jump to next non-empty menu entry (up or down) */
2091     while (y > 0 && y < num_setup_info - 1 &&
2092            (setup_info[y].type & TYPE_SKIP_ENTRY))
2093       y += dy;
2094   }
2095
2096   if (x == 0 && y >= 0 && y < num_setup_info &&
2097       (setup_info[y].type & ~TYPE_SKIP_ENTRY))
2098   {
2099     if (button)
2100     {
2101       if (y != choice)
2102       {
2103         drawCursor(y, FC_RED);
2104         drawCursor(choice, FC_BLUE);
2105         choice = choice_store[setup_mode] = y;
2106       }
2107     }
2108     else if (!(setup_info[y].type & TYPE_GHOSTED))
2109     {
2110       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE_MENU)
2111       {
2112         void (*menu_callback_function)(void) = setup_info[choice].value;
2113
2114         menu_callback_function();
2115       }
2116       else
2117       {
2118         if ((setup_info[y].type & TYPE_KEYTEXT) &&
2119             (setup_info[y + 1].type & TYPE_KEY))
2120           y++;
2121
2122         if (setup_info[y].type & TYPE_VALUE)
2123           changeSetupValue(y);
2124       }
2125     }
2126   }
2127
2128   BackToFront();
2129
2130   if (game_status == GAME_MODE_SETUP)
2131     DoAnimation();
2132 }
2133
2134 void DrawSetupScreen_Input()
2135 {
2136   ClearWindow();
2137
2138   DrawText(mSX+16, mSY+16, "Setup Input", FONT_TITLE_1);
2139
2140   initCursor(0, IMG_MENU_BUTTON);
2141   initCursor(1, IMG_MENU_BUTTON);
2142   initCursor(2, IMG_MENU_BUTTON_RIGHT);
2143   initCursor(13, IMG_MENU_BUTTON_LEFT);
2144
2145   drawCursorXY(10, 0, IMG_MENU_BUTTON_LEFT);
2146   drawCursorXY(12, 0, IMG_MENU_BUTTON_RIGHT);
2147
2148   DrawText(mSX+32, mSY+2*32, "Player:", FONT_MENU_1);
2149   DrawText(mSX+32, mSY+3*32, "Device:", FONT_MENU_1);
2150   DrawText(mSX+32, mSY+15*32, "Back",   FONT_MENU_1);
2151
2152 #if 0
2153   DeactivateJoystickForCalibration();
2154   DrawTextFCentered(SYSIZE - 20, FONT_TEXT_4,
2155                     "Joysticks deactivated on this screen");
2156 #endif
2157
2158   HandleSetupScreen_Input(0,0, 0,0, MB_MENU_INITIALIZE);
2159   FadeToFront();
2160   InitAnimation();
2161 }
2162
2163 static void setJoystickDeviceToNr(char *device_name, int device_nr)
2164 {
2165   if (device_name == NULL)
2166     return;
2167
2168   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
2169     device_nr = 0;
2170
2171   if (strlen(device_name) > 1)
2172   {
2173     char c1 = device_name[strlen(device_name) - 1];
2174     char c2 = device_name[strlen(device_name) - 2];
2175
2176     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
2177       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
2178   }
2179   else
2180     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
2181             strlen(device_name));
2182 }
2183
2184 static void drawPlayerSetupInputInfo(int player_nr)
2185 {
2186   int i;
2187   static struct SetupKeyboardInfo custom_key;
2188   static struct
2189   {
2190     Key *key;
2191     char *text;
2192   } custom[] =
2193   {
2194     { &custom_key.left,  "Joystick Left"  },
2195     { &custom_key.right, "Joystick Right" },
2196     { &custom_key.up,    "Joystick Up"    },
2197     { &custom_key.down,  "Joystick Down"  },
2198     { &custom_key.snap,  "Button 1"       },
2199     { &custom_key.bomb,  "Button 2"       }
2200   };
2201   static char *joystick_name[MAX_PLAYERS] =
2202   {
2203     "Joystick1",
2204     "Joystick2",
2205     "Joystick3",
2206     "Joystick4"
2207   };
2208
2209   custom_key = setup.input[player_nr].key;
2210
2211   DrawText(mSX+11*32, mSY+2*32, int2str(player_nr +1, 1), FONT_INPUT_1_ACTIVE);
2212 #if 1
2213   DrawGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
2214                          PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
2215 #else
2216   DrawGraphicThruMask(8, 2, PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
2217 #endif
2218
2219   if (setup.input[player_nr].use_joystick)
2220   {
2221     char *device_name = setup.input[player_nr].joy.device_name;
2222
2223     DrawText(mSX+8*32, mSY+3*32,
2224              joystick_name[getJoystickNrFromDeviceName(device_name)],
2225              FONT_VALUE_1);
2226     DrawText(mSX+32, mSY+4*32, "Calibrate", FONT_MENU_1);
2227   }
2228   else
2229   {
2230     DrawText(mSX+8*32, mSY+3*32, "Keyboard ", FONT_VALUE_1);
2231     DrawText(mSX+32,   mSY+4*32, "Customize", FONT_MENU_1);
2232   }
2233
2234   DrawText(mSX+32, mSY+5*32, "Actual Settings:", FONT_MENU_1);
2235   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
2236   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
2237   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
2238   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
2239   DrawText(mSX+2*32, mSY+6*32, ":", FONT_VALUE_OLD);
2240   DrawText(mSX+2*32, mSY+7*32, ":", FONT_VALUE_OLD);
2241   DrawText(mSX+2*32, mSY+8*32, ":", FONT_VALUE_OLD);
2242   DrawText(mSX+2*32, mSY+9*32, ":", FONT_VALUE_OLD);
2243   DrawText(mSX+32, mSY+10*32, "Snap Field:", FONT_VALUE_OLD);
2244   DrawText(mSX+32, mSY+12*32, "Place Bomb:", FONT_VALUE_OLD);
2245
2246   for (i=0; i<6; i++)
2247   {
2248     int ypos = 6 + i + (i > 3 ? i-3 : 0);
2249
2250     DrawText(mSX + 3*32, mSY + ypos*32,
2251              "              ", FONT_VALUE_1);
2252     DrawText(mSX + 3*32, mSY + ypos*32,
2253              (setup.input[player_nr].use_joystick ?
2254               custom[i].text :
2255               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
2256   }
2257 }
2258
2259 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
2260 {
2261   static int choice = 0;
2262   static int player_nr = 0;
2263   int x = 0;
2264   int y = choice;
2265   int pos_start  = SETUPINPUT_SCREEN_POS_START;
2266   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
2267   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
2268   int pos_end    = SETUPINPUT_SCREEN_POS_END;
2269
2270   if (button == MB_MENU_INITIALIZE)
2271   {
2272     drawPlayerSetupInputInfo(player_nr);
2273     drawCursor(choice, FC_RED);
2274     return;
2275   }
2276   else if (button == MB_MENU_LEAVE)
2277   {
2278     setup_mode = SETUP_MODE_MAIN;
2279     DrawSetupScreen();
2280     InitJoysticks();
2281   }
2282
2283   if (mx || my)         /* mouse input */
2284   {
2285     x = (mx - SX) / 32;
2286     y = (my - SY) / 32 - MENU_SCREEN_START_YPOS;
2287   }
2288   else if (dx || dy)    /* keyboard input */
2289   {
2290     if (dx && choice == 0)
2291       x = (dx < 0 ? 10 : 12);
2292     else if ((dx && choice == 1) ||
2293              (dx == +1 && choice == 2) ||
2294              (dx == -1 && choice == pos_end))
2295       button = MB_MENU_CHOICE;
2296     else if (dy)
2297       y = choice + dy;
2298
2299     if (y >= pos_empty1 && y <= pos_empty2)
2300       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
2301   }
2302
2303   if (y == 0 && ((x == 0 && !button) || ((x == 10 || x == 12) && button)))
2304   {
2305     static unsigned long delay = 0;
2306
2307     if (!DelayReached(&delay, GADGET_FRAME_DELAY))
2308       goto out;
2309
2310     player_nr = (player_nr + (x == 10 ? -1 : +1) + MAX_PLAYERS) % MAX_PLAYERS;
2311
2312     drawPlayerSetupInputInfo(player_nr);
2313   }
2314   else if (x == 0 && y >= pos_start && y <= pos_end &&
2315            !(y >= pos_empty1 && y <= pos_empty2))
2316   {
2317     if (button)
2318     {
2319       if (y != choice)
2320       {
2321         drawCursor(y, FC_RED);
2322         drawCursor(choice, FC_BLUE);
2323         choice = y;
2324       }
2325     }
2326     else
2327     {
2328       if (y == 1)
2329       {
2330         char *device_name = setup.input[player_nr].joy.device_name;
2331
2332         if (!setup.input[player_nr].use_joystick)
2333         {
2334           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
2335
2336           setJoystickDeviceToNr(device_name, new_device_nr);
2337           setup.input[player_nr].use_joystick = TRUE;
2338         }
2339         else
2340         {
2341           int device_nr = getJoystickNrFromDeviceName(device_name);
2342           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
2343
2344           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
2345             setup.input[player_nr].use_joystick = FALSE;
2346           else
2347             setJoystickDeviceToNr(device_name, new_device_nr);
2348         }
2349
2350         drawPlayerSetupInputInfo(player_nr);
2351       }
2352       else if (y == 2)
2353       {
2354         if (setup.input[player_nr].use_joystick)
2355         {
2356           InitJoysticks();
2357           CalibrateJoystick(player_nr);
2358         }
2359         else
2360           CustomizeKeyboard(player_nr);
2361       }
2362       else if (y == pos_end)
2363       {
2364         InitJoysticks();
2365
2366         setup_mode = SETUP_MODE_MAIN;
2367         DrawSetupScreen();
2368       }
2369     }
2370   }
2371
2372   BackToFront();
2373
2374   out:
2375
2376   if (game_status == GAME_MODE_SETUP)
2377     DoAnimation();
2378 }
2379
2380 void CustomizeKeyboard(int player_nr)
2381 {
2382   int i;
2383   int step_nr;
2384   boolean finished = FALSE;
2385   static struct SetupKeyboardInfo custom_key;
2386   static struct
2387   {
2388     Key *key;
2389     char *text;
2390   } customize_step[] =
2391   {
2392     { &custom_key.left,  "Move Left"  },
2393     { &custom_key.right, "Move Right" },
2394     { &custom_key.up,    "Move Up"    },
2395     { &custom_key.down,  "Move Down"  },
2396     { &custom_key.snap,  "Snap Field" },
2397     { &custom_key.bomb,  "Place Bomb" }
2398   };
2399
2400   /* read existing key bindings from player setup */
2401   custom_key = setup.input[player_nr].key;
2402
2403   ClearWindow();
2404   DrawText(mSX + 16, mSY + 16, "Keyboard Input", FONT_TITLE_1);
2405
2406   BackToFront();
2407   InitAnimation();
2408
2409   step_nr = 0;
2410   DrawText(mSX, mSY + (2+2*step_nr)*32,
2411            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
2412   DrawText(mSX, mSY + (2+2*step_nr+1)*32,
2413            "Key:", FONT_INPUT_1_ACTIVE);
2414   DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2415            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
2416
2417   while(!finished)
2418   {
2419     if (PendingEvent())         /* got event */
2420     {
2421       Event event;
2422
2423       NextEvent(&event);
2424
2425       switch(event.type)
2426       {
2427         case EVENT_KEYPRESS:
2428           {
2429             Key key = GetEventKey((KeyEvent *)&event, FALSE);
2430
2431             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
2432             {
2433               finished = TRUE;
2434               break;
2435             }
2436
2437             /* all keys configured -- wait for "Escape" or "Return" key */
2438             if (step_nr == 6)
2439               break;
2440
2441             /* press 'Enter' to keep the existing key binding */
2442             if (key == KSYM_Return)
2443               key = *customize_step[step_nr].key;
2444
2445             /* check if key already used */
2446             for (i=0; i<step_nr; i++)
2447               if (*customize_step[i].key == key)
2448                 break;
2449             if (i < step_nr)
2450               break;
2451
2452             /* got new key binding */
2453             *customize_step[step_nr].key = key;
2454             DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2455                      "             ", FONT_VALUE_1);
2456             DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2457                      getKeyNameFromKey(key), FONT_VALUE_1);
2458             step_nr++;
2459
2460             /* un-highlight last query */
2461             DrawText(mSX, mSY+(2+2*(step_nr-1))*32,
2462                      customize_step[step_nr-1].text, FONT_MENU_1);
2463             DrawText(mSX, mSY+(2+2*(step_nr-1)+1)*32,
2464                      "Key:", FONT_MENU_1);
2465
2466             /* press 'Enter' to leave */
2467             if (step_nr == 6)
2468             {
2469               DrawText(mSX + 16, mSY + 15*32+16,
2470                        "Press Enter", FONT_TITLE_1);
2471               break;
2472             }
2473
2474             /* query next key binding */
2475             DrawText(mSX, mSY+(2+2*step_nr)*32,
2476                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
2477             DrawText(mSX, mSY+(2+2*step_nr+1)*32,
2478                      "Key:", FONT_INPUT_1_ACTIVE);
2479             DrawText(mSX + 4*32, mSY+(2+2*step_nr+1)*32,
2480                      getKeyNameFromKey(*customize_step[step_nr].key),
2481                      FONT_VALUE_OLD);
2482           }
2483           break;
2484
2485         case EVENT_KEYRELEASE:
2486           key_joystick_mapping = 0;
2487           break;
2488
2489         default:
2490           HandleOtherEvents(&event);
2491           break;
2492       }
2493     }
2494
2495     BackToFront();
2496     DoAnimation();
2497
2498     /* don't eat all CPU time */
2499     Delay(10);
2500   }
2501
2502   /* write new key bindings back to player setup */
2503   setup.input[player_nr].key = custom_key;
2504
2505   StopAnimation();
2506   DrawSetupScreen_Input();
2507 }
2508
2509 static boolean CalibrateJoystickMain(int player_nr)
2510 {
2511   int new_joystick_xleft = JOYSTICK_XMIDDLE;
2512   int new_joystick_xright = JOYSTICK_XMIDDLE;
2513   int new_joystick_yupper = JOYSTICK_YMIDDLE;
2514   int new_joystick_ylower = JOYSTICK_YMIDDLE;
2515   int new_joystick_xmiddle, new_joystick_ymiddle;
2516
2517   int joystick_fd = joystick.fd[player_nr];
2518   int x, y, last_x, last_y, xpos = 8, ypos = 3;
2519   boolean check[3][3];
2520   int check_remaining = 3 * 3;
2521   int joy_x, joy_y;
2522   int joy_value;
2523   int result = -1;
2524
2525   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
2526     return FALSE;
2527
2528   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
2529     return FALSE;
2530
2531   ClearWindow();
2532
2533   for(y=0; y < 3; y++)
2534   {
2535     for(x=0; x < 3; x++)
2536     {
2537       DrawGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
2538       check[x][y] = FALSE;
2539     }
2540   }
2541
2542   DrawText(mSX,      mSY +  6 * 32, " ROTATE JOYSTICK ", FONT_TITLE_1);
2543   DrawText(mSX,      mSY +  7 * 32, "IN ALL DIRECTIONS", FONT_TITLE_1);
2544   DrawText(mSX + 16, mSY +  9 * 32, "  IF ALL BALLS  ",  FONT_TITLE_1);
2545   DrawText(mSX,      mSY + 10 * 32, "   ARE YELLOW,   ", FONT_TITLE_1);
2546   DrawText(mSX,      mSY + 11 * 32, " CENTER JOYSTICK ", FONT_TITLE_1);
2547   DrawText(mSX,      mSY + 12 * 32, "       AND       ", FONT_TITLE_1);
2548   DrawText(mSX,      mSY + 13 * 32, "PRESS ANY BUTTON!", FONT_TITLE_1);
2549
2550   joy_value = Joystick(player_nr);
2551   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2552   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2553
2554   /* eventually uncalibrated center position (joystick could be uncentered) */
2555   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2556     return FALSE;
2557
2558   new_joystick_xmiddle = joy_x;
2559   new_joystick_ymiddle = joy_y;
2560
2561   DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
2562   BackToFront();
2563
2564   while(Joystick(player_nr) & JOY_BUTTON);      /* wait for released button */
2565   InitAnimation();
2566
2567   while(result < 0)
2568   {
2569     if (PendingEvent())         /* got event */
2570     {
2571       Event event;
2572
2573       NextEvent(&event);
2574
2575       switch(event.type)
2576       {
2577         case EVENT_KEYPRESS:
2578           switch(GetEventKey((KeyEvent *)&event, TRUE))
2579           {
2580             case KSYM_Return:
2581               if (check_remaining == 0)
2582                 result = 1;
2583               break;
2584
2585             case KSYM_Escape:
2586               result = 0;
2587               break;
2588
2589             default:
2590               break;
2591           }
2592           break;
2593
2594         case EVENT_KEYRELEASE:
2595           key_joystick_mapping = 0;
2596           break;
2597
2598         default:
2599           HandleOtherEvents(&event);
2600           break;
2601       }
2602     }
2603
2604     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2605       return FALSE;
2606
2607     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
2608     new_joystick_xright = MAX(new_joystick_xright, joy_x);
2609     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
2610     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
2611
2612     setup.input[player_nr].joy.xleft = new_joystick_xleft;
2613     setup.input[player_nr].joy.yupper = new_joystick_yupper;
2614     setup.input[player_nr].joy.xright = new_joystick_xright;
2615     setup.input[player_nr].joy.ylower = new_joystick_ylower;
2616     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
2617     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
2618
2619     CheckJoystickData();
2620
2621     joy_value = Joystick(player_nr);
2622
2623     if (joy_value & JOY_BUTTON && check_remaining == 0)
2624       result = 1;
2625
2626     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2627     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2628
2629     if (x != last_x || y != last_y)
2630     {
2631       DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_YELLOW, 0);
2632       DrawGraphic(xpos + x,      ypos + y,      IMG_MENU_CALIBRATE_RED,    0);
2633
2634       last_x = x;
2635       last_y = y;
2636
2637       if (check_remaining > 0 && !check[x+1][y+1])
2638       {
2639         check[x+1][y+1] = TRUE;
2640         check_remaining--;
2641       }
2642
2643 #if 0
2644 #ifdef DEBUG
2645       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
2646              setup.input[player_nr].joy.xleft,
2647              setup.input[player_nr].joy.xmiddle,
2648              setup.input[player_nr].joy.xright);
2649       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
2650              setup.input[player_nr].joy.yupper,
2651              setup.input[player_nr].joy.ymiddle,
2652              setup.input[player_nr].joy.ylower);
2653 #endif
2654 #endif
2655
2656     }
2657
2658     BackToFront();
2659     DoAnimation();
2660
2661     /* don't eat all CPU time */
2662     Delay(10);
2663   }
2664
2665   /* calibrated center position (joystick should now be centered) */
2666   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2667     return FALSE;
2668
2669   new_joystick_xmiddle = joy_x;
2670   new_joystick_ymiddle = joy_y;
2671
2672   StopAnimation();
2673
2674   DrawSetupScreen_Input();
2675
2676   /* wait until the last pressed button was released */
2677   while (Joystick(player_nr) & JOY_BUTTON)
2678   {
2679     if (PendingEvent())         /* got event */
2680     {
2681       Event event;
2682
2683       NextEvent(&event);
2684       HandleOtherEvents(&event);
2685
2686       Delay(10);
2687     }
2688   }
2689
2690   return TRUE;
2691 }
2692
2693 void CalibrateJoystick(int player_nr)
2694 {
2695   if (!CalibrateJoystickMain(player_nr))
2696   {
2697     ClearWindow();
2698
2699     DrawText(mSX + 16, mSY + 6*32, "  JOYSTICK NOT  ",  FONT_TITLE_1);
2700     DrawText(mSX,      mSY + 7*32, "    AVAILABLE    ", FONT_TITLE_1);
2701     BackToFront();
2702     Delay(2000);        /* show error message for two seconds */
2703   }
2704 }
2705
2706 void DrawSetupScreen()
2707 {
2708   DeactivateJoystick();
2709
2710   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
2711
2712   if (setup_mode == SETUP_MODE_INPUT)
2713     DrawSetupScreen_Input();
2714   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2715     DrawChooseTree(&artwork.gfx_current);
2716   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2717     DrawChooseTree(&artwork.snd_current);
2718   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2719     DrawChooseTree(&artwork.mus_current);
2720   else
2721     DrawSetupScreen_Generic();
2722 }
2723
2724 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
2725 {
2726   if (setup_mode == SETUP_MODE_INPUT)
2727     HandleSetupScreen_Input(mx, my, dx, dy, button);
2728   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2729     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
2730   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2731     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
2732   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2733     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
2734   else
2735     HandleSetupScreen_Generic(mx, my, dx, dy, button);
2736 }
2737
2738 void HandleGameActions()
2739 {
2740   if (game_status != GAME_MODE_PLAYING)
2741     return;
2742
2743   if (local_player->LevelSolved)
2744     GameWon();
2745
2746   if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
2747     TapeStop();
2748
2749   GameActions();
2750
2751   BackToFront();
2752 }
2753
2754 /* ---------- new screen button stuff -------------------------------------- */
2755
2756 /* graphic position and size values for buttons and scrollbars */
2757 #define SC_SCROLLBUTTON_XSIZE           32
2758 #define SC_SCROLLBUTTON_YSIZE           32
2759
2760 #define SC_SCROLL_UP_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
2761 #define SC_SCROLL_UP_YPOS               SC_SCROLLBUTTON_YSIZE
2762 #define SC_SCROLL_DOWN_XPOS             SC_SCROLL_UP_XPOS
2763 #define SC_SCROLL_DOWN_YPOS             (SYSIZE - SC_SCROLLBUTTON_YSIZE)
2764 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLL_UP_XPOS
2765 #define SC_SCROLL_VERTICAL_YPOS   (SC_SCROLL_UP_YPOS + SC_SCROLLBUTTON_YSIZE)
2766 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
2767 #define SC_SCROLL_VERTICAL_YSIZE        (SYSIZE - 3 * SC_SCROLLBUTTON_YSIZE)
2768
2769 #define SC_BORDER_SIZE                  14
2770
2771 static struct
2772 {
2773   int gfx_unpressed, gfx_pressed;
2774   int x, y;
2775   int gadget_id;
2776   char *infotext;
2777 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
2778 {
2779   {
2780     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
2781     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
2782     SCREEN_CTRL_ID_SCROLL_UP,
2783     "scroll up"
2784   },
2785   {
2786     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
2787     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
2788     SCREEN_CTRL_ID_SCROLL_DOWN,
2789     "scroll down"
2790   }
2791 };
2792
2793 static struct
2794 {
2795 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2796   Bitmap **gfx_unpressed, **gfx_pressed;
2797 #else
2798   int gfx_unpressed, gfx_pressed;
2799 #endif
2800   int x, y;
2801   int width, height;
2802   int type;
2803   int gadget_id;
2804   char *infotext;
2805 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
2806 {
2807   {
2808 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2809     &scrollbar_bitmap[0], &scrollbar_bitmap[1],
2810 #else
2811     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
2812 #endif
2813     SX + SC_SCROLL_VERTICAL_XPOS, SY + SC_SCROLL_VERTICAL_YPOS,
2814     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
2815     GD_TYPE_SCROLLBAR_VERTICAL,
2816     SCREEN_CTRL_ID_SCROLL_VERTICAL,
2817     "scroll level series vertically"
2818   }
2819 };
2820
2821 static void CreateScreenScrollbuttons()
2822 {
2823   struct GadgetInfo *gi;
2824   unsigned long event_mask;
2825   int i;
2826
2827   for (i=0; i<NUM_SCREEN_SCROLLBUTTONS; i++)
2828   {
2829     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
2830     int gfx_unpressed, gfx_pressed;
2831     int x, y, width, height;
2832     int gd_x1, gd_x2, gd_y1, gd_y2;
2833     int id = scrollbutton_info[i].gadget_id;
2834
2835     x = scrollbutton_info[i].x;
2836     y = scrollbutton_info[i].y;
2837
2838     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
2839
2840     x += SX;
2841     y += SY;
2842     width = SC_SCROLLBUTTON_XSIZE;
2843     height = SC_SCROLLBUTTON_YSIZE;
2844
2845     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
2846     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
2847     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
2848     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
2849     gd_x1 = graphic_info[gfx_unpressed].src_x;
2850     gd_y1 = graphic_info[gfx_unpressed].src_y;
2851     gd_x2 = graphic_info[gfx_pressed].src_x;
2852     gd_y2 = graphic_info[gfx_pressed].src_y;
2853
2854     gi = CreateGadget(GDI_CUSTOM_ID, id,
2855                       GDI_CUSTOM_TYPE_ID, i,
2856                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
2857                       GDI_X, x,
2858                       GDI_Y, y,
2859                       GDI_WIDTH, width,
2860                       GDI_HEIGHT, height,
2861                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
2862                       GDI_STATE, GD_BUTTON_UNPRESSED,
2863                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
2864                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
2865                       GDI_EVENT_MASK, event_mask,
2866                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
2867                       GDI_END);
2868
2869     if (gi == NULL)
2870       Error(ERR_EXIT, "cannot create gadget");
2871
2872     screen_gadget[id] = gi;
2873   }
2874 }
2875
2876 static void CreateScreenScrollbars()
2877 {
2878   int i;
2879
2880   for (i=0; i<NUM_SCREEN_SCROLLBARS; i++)
2881   {
2882     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
2883 #if !defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2884     int gfx_unpressed, gfx_pressed;
2885 #endif
2886     int gd_x1, gd_x2, gd_y1, gd_y2;
2887     struct GadgetInfo *gi;
2888     int items_max, items_visible, item_position;
2889     unsigned long event_mask;
2890     int num_page_entries = MAX_MENU_ENTRIES_ON_SCREEN - 1;
2891     int id = scrollbar_info[i].gadget_id;
2892
2893     items_max = num_page_entries;
2894     items_visible = num_page_entries;
2895     item_position = 0;
2896
2897     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
2898
2899 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2900     gd_bitmap_unpressed = *scrollbar_info[i].gfx_unpressed;
2901     gd_bitmap_pressed   = *scrollbar_info[i].gfx_pressed;
2902     gd_x1 = 0;
2903     gd_y1 = 0;
2904     gd_x2 = 0;
2905     gd_y2 = 0;
2906 #else
2907     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
2908     gfx_pressed   = scrollbar_info[i].gfx_pressed;
2909     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
2910     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
2911     gd_x1 = graphic_info[gfx_unpressed].src_x;
2912     gd_y1 = graphic_info[gfx_unpressed].src_y;
2913     gd_x2 = graphic_info[gfx_pressed].src_x;
2914     gd_y2 = graphic_info[gfx_pressed].src_y;
2915 #endif
2916
2917     gi = CreateGadget(GDI_CUSTOM_ID, id,
2918                       GDI_CUSTOM_TYPE_ID, i,
2919                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
2920                       GDI_X, scrollbar_info[i].x,
2921                       GDI_Y, scrollbar_info[i].y,
2922                       GDI_WIDTH, scrollbar_info[i].width,
2923                       GDI_HEIGHT, scrollbar_info[i].height,
2924                       GDI_TYPE, scrollbar_info[i].type,
2925                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
2926                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
2927                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
2928                       GDI_STATE, GD_BUTTON_UNPRESSED,
2929                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
2930                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
2931                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
2932                       GDI_EVENT_MASK, event_mask,
2933                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
2934                       GDI_END);
2935
2936     if (gi == NULL)
2937       Error(ERR_EXIT, "cannot create gadget");
2938
2939     screen_gadget[id] = gi;
2940   }
2941 }
2942
2943 void CreateScreenGadgets()
2944 {
2945 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2946   int i;
2947
2948   for (i=0; i < NUM_SCROLLBAR_BITMAPS; i++)
2949   {
2950     scrollbar_bitmap[i] = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
2951
2952     /* copy pointers to clip mask and GC */
2953     scrollbar_bitmap[i]->clip_mask =
2954       graphic_info[IMG_MENU_SCROLLBAR + i].clip_mask;
2955     scrollbar_bitmap[i]->stored_clip_gc =
2956       graphic_info[IMG_MENU_SCROLLBAR + i].clip_gc;
2957
2958     BlitBitmap(graphic_info[IMG_MENU_SCROLLBAR + i].bitmap,
2959                scrollbar_bitmap[i],
2960                graphic_info[IMG_MENU_SCROLLBAR + i].src_x,
2961                graphic_info[IMG_MENU_SCROLLBAR + i].src_y,
2962                TILEX, TILEY, 0, 0);
2963   }
2964 #endif
2965
2966   CreateScreenScrollbuttons();
2967   CreateScreenScrollbars();
2968 }
2969
2970 void FreeScreenGadgets()
2971 {
2972   int i;
2973
2974 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2975   for (i=0; i < NUM_SCROLLBAR_BITMAPS; i++)
2976   {
2977     /* prevent freeing clip mask and GC twice */
2978     scrollbar_bitmap[i]->clip_mask = None;
2979     scrollbar_bitmap[i]->stored_clip_gc = None;
2980
2981     FreeBitmap(scrollbar_bitmap[i]);
2982   }
2983 #endif
2984
2985   for (i=0; i<NUM_SCREEN_GADGETS; i++)
2986     FreeGadget(screen_gadget[i]);
2987 }
2988
2989 void MapChooseTreeGadgets(TreeInfo *ti)
2990 {
2991   int num_entries = numTreeInfoInGroup(ti);
2992   int i;
2993
2994   if (num_entries <= MAX_MENU_ENTRIES_ON_SCREEN)
2995     return;
2996
2997   for (i=0; i<NUM_SCREEN_GADGETS; i++)
2998     MapGadget(screen_gadget[i]);
2999 }
3000
3001 void UnmapChooseTreeGadgets()
3002 {
3003   int i;
3004
3005   for (i=0; i<NUM_SCREEN_GADGETS; i++)
3006     UnmapGadget(screen_gadget[i]);
3007 }
3008
3009 static void HandleScreenGadgets(struct GadgetInfo *gi)
3010 {
3011   int id = gi->custom_id;
3012
3013   if (game_status != GAME_MODE_LEVELS && game_status != GAME_MODE_SETUP)
3014     return;
3015
3016   switch (id)
3017   {
3018     case SCREEN_CTRL_ID_SCROLL_UP:
3019       if (game_status == GAME_MODE_LEVELS)
3020         HandleChooseLevel(SX,SY + 32, 0,0, MB_MENU_MARK);
3021       else if (game_status == GAME_MODE_SETUP)
3022         HandleSetupScreen(SX,SY + 32, 0,0, MB_MENU_MARK);
3023       break;
3024
3025     case SCREEN_CTRL_ID_SCROLL_DOWN:
3026       if (game_status == GAME_MODE_LEVELS)
3027         HandleChooseLevel(SX,SY + SYSIZE - 32, 0,0, MB_MENU_MARK);
3028       else if (game_status == GAME_MODE_SETUP)
3029         HandleSetupScreen(SX,SY + SYSIZE - 32, 0,0, MB_MENU_MARK);
3030       break;
3031
3032     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
3033       if (game_status == GAME_MODE_LEVELS)
3034         HandleChooseLevel(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3035       else if (game_status == GAME_MODE_SETUP)
3036         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3037       break;
3038
3039     default:
3040       break;
3041   }
3042 }