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