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