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