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