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