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