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