5cc2e514e2e3b82ccf3c35344b4b159fb7a2f3b1
[rocksndiamonds.git] / src / screens.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  ©1995 Artsoft Development                               *
5 *        Holger Schemel                                    *
6 *        33659 Bielefeld-Senne                             *
7 *        Telefon: (0521) 493245                            *
8 *        eMail: aeglos@valinor.owl.de                      *
9 *               aeglos@uni-paderborn.de                    *
10 *               q99492@pbhrzx.uni-paderborn.de             *
11 *----------------------------------------------------------*
12 *  screens.c                                               *
13 ***********************************************************/
14
15 #include "screens.h"
16 #include "events.h"
17 #include "sound.h"
18 #include "game.h"
19 #include "tools.h"
20 #include "editor.h"
21 #include "misc.h"
22 #include "files.h"
23 #include "buttons.h"
24 #include "tape.h"
25 #include "joystick.h"
26 #include "cartoons.h"
27
28 #ifdef MSDOS
29 extern unsigned char get_ascii(KeySym);
30 #endif
31
32 void DrawHeadline()
33 {
34   int x1 = SX+(SXSIZE - strlen(GAMETITLE_STRING) * FONT1_XSIZE) / 2;
35   int y1 = SY+8;
36   int x2 = SX+(SXSIZE - strlen(COPYRIGHT_STRING) * FONT2_XSIZE) / 2;
37   int y2 = SY+46;
38
39   DrawText(x1,y1, GAMETITLE_STRING, FS_BIG,FC_YELLOW);
40   DrawText(x2,y2, COPYRIGHT_STRING, FS_SMALL,FC_RED);
41 }
42
43 void DrawMainMenu()
44 {
45   int i;
46
47   FadeSounds();
48   GetPlayerConfig();
49   LoadLevel(level_nr);
50
51   ClearWindow();
52   DrawHeadline();
53   DrawText(SX+32, SY+64, "Name:",FS_BIG,FC_GREEN);
54   DrawText(SX+192,SY+64, local_player->alias_name,FS_BIG,FC_RED);
55   DrawText(SX+32, SY+96, "Level:",FS_BIG,FC_GREEN);
56   DrawText(SX+352,SY+96, int2str(level_nr,3),FS_BIG,
57            (level_nr<leveldir[leveldir_nr].levels ? FC_RED : FC_YELLOW));
58   DrawText(SX+32, SY+128,"Hall Of Fame",FS_BIG,FC_GREEN);
59   DrawText(SX+32, SY+160,"Level Creator",FS_BIG,FC_GREEN);
60   DrawText(SY+32, SY+192,"Info Screen",FS_BIG,FC_GREEN);
61   DrawText(SX+32, SY+224,"Start Game",FS_BIG,FC_GREEN);
62   DrawText(SX+32, SY+256,"Setup",FS_BIG,FC_GREEN);
63   DrawText(SX+32, SY+288,"Quit",FS_BIG,FC_GREEN);
64
65   DrawMicroLevel(MICROLEV_XPOS,MICROLEV_YPOS);
66
67   for(i=2;i<10;i++)
68     DrawGraphic(0,i,GFX_KUGEL_BLAU);
69   DrawGraphic(10,3,GFX_PFEIL_L);
70   DrawGraphic(14,3,GFX_PFEIL_R);
71
72   DrawText(SX+40+16,SY+326,"A Game by Artsoft Entertainment",FS_SMALL,FC_BLUE);
73   DrawText(SX+40+16,SY+344,"Graphics: Deluxe Paint IV Amiga",
74            FS_SMALL,FC_BLUE);
75   DrawText(SX+60+16,SY+362,"Sounds: AudioMaster IV Amiga",
76            FS_SMALL,FC_BLUE);
77
78   FadeToFront();
79   InitAnimation();
80   HandleMainMenu(0,0,0,0,MB_MENU_INITIALIZE);
81
82   TapeStop();
83   if (TAPE_IS_EMPTY(tape))
84     LoadLevelTape(level_nr);
85   DrawCompleteVideoDisplay();
86
87   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
88
89   ClearEventQueue();
90   XAutoRepeatOn(display);
91 }
92
93 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
94 {
95   static int choice = 3;
96   static int redraw = TRUE;
97   int x = (mx+32-SX)/32, y = (my+32-SY)/32;
98
99   if (redraw || button == MB_MENU_INITIALIZE)
100   {
101     DrawGraphic(0,choice-1,GFX_KUGEL_ROT);
102     redraw = FALSE;
103   }
104
105   if (button == MB_MENU_INITIALIZE)
106     return;
107
108   if (dx || dy)
109   {
110     if (dx && choice==4)
111     {
112       x = (dx<0 ? 11 : 15);
113       y = 4;
114     }
115     else if (dy)
116     {
117       x = 1;
118       y = choice+dy;
119     }
120     else
121       x = y = 0;
122
123     if (y<3)
124       y = 3;
125     else if (y>10)
126       y = 10;
127   }
128
129   if (!mx && !my && !dx && !dy)
130   {
131     x = 1;
132     y = choice;
133   }
134
135   if (y==4 && ((x==11 && level_nr>0) ||
136                (x==15 && level_nr<leveldir[leveldir_nr].levels)) &&
137       button)
138   {
139     static long level_delay = 0;
140     int step = (button==1 ? 1 : button==2 ? 5 : 10);
141     int new_level_nr, old_level_nr = level_nr;
142
143     new_level_nr = level_nr + (x==11 ? -step : +step);
144     if (new_level_nr<0)
145       new_level_nr = 0;
146     if (new_level_nr>leveldir[leveldir_nr].levels-1)
147       new_level_nr = leveldir[leveldir_nr].levels-1;
148
149     if (old_level_nr==new_level_nr || !DelayReached(&level_delay,150))
150       goto out;
151
152     level_nr = new_level_nr;
153
154     if (level_nr > local_player->handicap)
155       level_nr = local_player->handicap;
156
157     DrawTextExt(drawto,gc,SX+352,SY+96, int2str(level_nr,3), FS_BIG,FC_RED);
158     DrawTextExt(window,gc,SX+352,SY+96, int2str(level_nr,3), FS_BIG,FC_RED);
159
160     LoadLevel(level_nr);
161     DrawMicroLevel(MICROLEV_XPOS,MICROLEV_YPOS);
162
163     TapeErase();
164     LoadLevelTape(level_nr);
165     DrawCompleteVideoDisplay();
166
167     /* needed because DrawMicroLevel() takes some time */
168     BackToFront();
169     XSync(display,FALSE);
170     DelayReached(&level_delay,0);       /* reset delay counter */
171   }
172   else if (x==1 && y>=3 && y<=10)
173   {
174     if (button)
175     {
176       if (y!=choice)
177       {
178         DrawGraphic(0,y-1,GFX_KUGEL_ROT);
179         DrawGraphic(0,choice-1,GFX_KUGEL_BLAU);
180       }
181       choice = y;
182     }
183     else
184     {
185       if (y==3)
186       {
187         game_status = TYPENAME;
188         HandleTypeName(strlen(local_player->alias_name),0);
189       }
190       else if (y==4)
191       {
192         if (num_leveldirs)
193         {
194           game_status = CHOOSELEVEL;
195           SavePlayerInfo(PLAYER_LEVEL);
196           DrawChooseLevel();
197         }
198       }
199       else if (y==5)
200       {
201         game_status = HALLOFFAME;
202         DrawHallOfFame(-1);
203       }
204       else if (y==6)
205       {
206         game_status = LEVELED;
207         if (leveldir[leveldir_nr].readonly)
208           AreYouSure("This level is read only !",AYS_CONFIRM);
209         DrawLevelEd();
210       }
211       else if (y==7)
212       {
213         game_status = HELPSCREEN;
214         DrawHelpScreen();
215       }
216       else if (y==8)
217       {
218         if (autorecord_on)
219           TapeStartRecording();
220
221         game_status = PLAYING;
222         InitGame();
223       }
224       else if (y==9)
225       {
226         game_status = SETUP;
227         DrawSetupScreen();
228       }
229       else if (y==10)
230       {
231         SavePlayerInfo(PLAYER_LEVEL);
232         if (AreYouSure("Do you really want to quit ?",AYS_ASK|AYS_STAY_CLOSED))
233           game_status = EXITGAME;
234       }
235
236       if (!button)
237         redraw = TRUE;
238     }
239   }
240   BackToFront();
241
242   out:
243
244   if (game_status==MAINMENU)
245     DoAnimation();
246 }
247
248 #define MAX_HELPSCREEN_ELS      10
249 #define HA_NEXT                 -999
250 #define HA_END                  -1000
251
252 static long helpscreen_state;
253 static int helpscreen_step[MAX_HELPSCREEN_ELS];
254 static int helpscreen_frame[MAX_HELPSCREEN_ELS];
255 static int helpscreen_delay[MAX_HELPSCREEN_ELS];
256 static int helpscreen_action[] =
257 {
258   GFX_SPIELER1_DOWN,4,2,
259   GFX_SPIELER1_UP,4,2,
260   GFX_SPIELER1_LEFT,4,2,
261   GFX_SPIELER1_RIGHT,4,2,
262   GFX_SPIELER1_PUSH_LEFT,4,2,
263   GFX_SPIELER1_PUSH_RIGHT,4,2,                                  HA_NEXT,
264   GFX_ERDREICH,1,100,                                           HA_NEXT,
265   GFX_LEERRAUM,1,100,                                           HA_NEXT,
266   GFX_MORAST_LEER,1,100,                                        HA_NEXT,
267   GFX_BETON,1,100,                                              HA_NEXT,
268   GFX_MAUERWERK,1,100,                                          HA_NEXT,
269   GFX_MAUER_R1,3,4, GFX_MAUERWERK,1,20, GFX_LEERRAUM,1,10,
270   GFX_MAUER_L1,3,4, GFX_MAUERWERK,1,20, GFX_LEERRAUM,1,10,      HA_NEXT,
271   GFX_UNSICHTBAR,1,100,                                         HA_NEXT,
272   GFX_FELSBODEN,1,100,                                          HA_NEXT,
273   GFX_CHAR_A,30,4, GFX_CHAR_AUSRUF,32,4,                        HA_NEXT,
274   GFX_EDELSTEIN,2,5,                                            HA_NEXT,
275   GFX_DIAMANT,2,5,                                              HA_NEXT,
276   GFX_EDELSTEIN_BD,2,5,                                         HA_NEXT,
277   GFX_EDELSTEIN_GELB,2,5, GFX_EDELSTEIN_ROT,2,5,
278   GFX_EDELSTEIN_LILA,2,5,                                       HA_NEXT,
279   GFX_FELSBROCKEN,4,5,                                          HA_NEXT,
280   GFX_BOMBE,1,50, GFX_EXPLOSION,8,1, GFX_LEERRAUM,1,10,         HA_NEXT,
281   GFX_KOKOSNUSS,1,50, GFX_CRACKINGNUT,3,1, GFX_EDELSTEIN,1,10,  HA_NEXT,
282   GFX_ERZ_EDEL,1,50, GFX_EXPLOSION,8,1, GFX_EDELSTEIN,1,10,     HA_NEXT,
283   GFX_ERZ_DIAM,1,50, GFX_EXPLOSION,8,1, GFX_DIAMANT,1,10,       HA_NEXT,
284   GFX_ERZ_EDEL_BD,1,50, GFX_EXPLOSION,8,1,GFX_EDELSTEIN_BD,1,10,HA_NEXT,
285   GFX_ERZ_EDEL_GELB,1,50, GFX_EXPLOSION,8,1,
286   GFX_EDELSTEIN_GELB,1,10, GFX_ERZ_EDEL_ROT,1,50,
287   GFX_EXPLOSION,8,1, GFX_EDELSTEIN_ROT,1,10,
288   GFX_ERZ_EDEL_LILA,1,50, GFX_EXPLOSION,8,1,
289   GFX_EDELSTEIN_LILA,1,10,                                      HA_NEXT,
290   GFX_GEBLUBBER,4,4,                                            HA_NEXT,
291   GFX_SCHLUESSEL1,4,25,                                         HA_NEXT,
292   GFX_PFORTE1,4,25,                                             HA_NEXT,
293   GFX_PFORTE1X,4,25,                                            HA_NEXT,
294   GFX_DYNAMIT_AUS,1,100,                                        HA_NEXT,
295   GFX_DYNAMIT,7,6, GFX_EXPLOSION,8,1, GFX_LEERRAUM,1,10,        HA_NEXT,
296   GFX_DYNABOMB+0,4,3, GFX_DYNABOMB+3,1,3, GFX_DYNABOMB+2,1,3,
297   GFX_DYNABOMB+1,1,3, GFX_DYNABOMB+0,1,3, GFX_EXPLOSION,8,1,
298   GFX_LEERRAUM,1,10,                                            HA_NEXT,
299   GFX_DYNABOMB_NR,1,100,                                        HA_NEXT,
300   GFX_DYNABOMB_SZ,1,100,                                        HA_NEXT,
301   GFX_FLIEGER+4,1,3, GFX_FLIEGER+0,1,3, GFX_FLIEGER+4,1,3,
302   GFX_FLIEGER+5,1,3, GFX_FLIEGER+1,1,3, GFX_FLIEGER+5,1,3,
303   GFX_FLIEGER+6,1,3, GFX_FLIEGER+2,1,3, GFX_FLIEGER+6,1,3,
304   GFX_FLIEGER+7,1,3, GFX_FLIEGER+3,1,3, GFX_FLIEGER+7,1,3,      HA_NEXT,
305   GFX_KAEFER+4,1,1, GFX_KAEFER+0,1,1, GFX_KAEFER+4,1,1,
306   GFX_KAEFER+5,1,1, GFX_KAEFER+1,1,1, GFX_KAEFER+5,1,1,
307   GFX_KAEFER+6,1,1, GFX_KAEFER+2,1,1, GFX_KAEFER+6,1,1,
308   GFX_KAEFER+7,1,1, GFX_KAEFER+3,1,1, GFX_KAEFER+7,1,1,         HA_NEXT,
309   GFX_BUTTERFLY,2,2,                                            HA_NEXT,
310   GFX_FIREFLY,2,2,                                              HA_NEXT,
311   GFX_PACMAN+0,1,3, GFX_PACMAN+4,1,2, GFX_PACMAN+0,1,3,
312   GFX_PACMAN+1,1,3, GFX_PACMAN+5,1,2, GFX_PACMAN+1,1,3,
313   GFX_PACMAN+2,1,3, GFX_PACMAN+6,1,2, GFX_PACMAN+2,1,3,
314   GFX_PACMAN+3,1,3, GFX_PACMAN+7,1,2, GFX_PACMAN+3,1,3,         HA_NEXT,
315   GFX_MAMPFER+0,4,1, GFX_MAMPFER+3,1,1, GFX_MAMPFER+2,1,1,
316   GFX_MAMPFER+1,1,1, GFX_MAMPFER+0,1,1,                         HA_NEXT,
317   GFX_MAMPFER2+0,4,1, GFX_MAMPFER2+3,1,1, GFX_MAMPFER2+2,1,1,
318   GFX_MAMPFER2+1,1,1, GFX_MAMPFER2+0,1,1,                       HA_NEXT,
319   GFX_ROBOT+0,4,1, GFX_ROBOT+3,1,1, GFX_ROBOT+2,1,1,
320   GFX_ROBOT+1,1,1, GFX_ROBOT+0,1,1,                             HA_NEXT,
321   GFX_MAULWURF_DOWN,4,2,
322   GFX_MAULWURF_UP,4,2,
323   GFX_MAULWURF_LEFT,4,2,
324   GFX_MAULWURF_RIGHT,4,2,                                       HA_NEXT,
325   GFX_PINGUIN_DOWN,4,2,
326   GFX_PINGUIN_UP,4,2,
327   GFX_PINGUIN_LEFT,4,2,
328   GFX_PINGUIN_RIGHT,4,2,                                        HA_NEXT,
329   GFX_SCHWEIN_DOWN,4,2,
330   GFX_SCHWEIN_UP,4,2,
331   GFX_SCHWEIN_LEFT,4,2,
332   GFX_SCHWEIN_RIGHT,4,2,                                        HA_NEXT,
333   GFX_DRACHE_DOWN,4,2,
334   GFX_DRACHE_UP,4,2,
335   GFX_DRACHE_LEFT,4,2,
336   GFX_DRACHE_RIGHT,4,2,                                         HA_NEXT,
337   GFX_SONDE_START,8,1,                                          HA_NEXT,
338   GFX_ABLENK,4,1,                                               HA_NEXT,
339   GFX_BIRNE_AUS,1,25, GFX_BIRNE_EIN,1,25,                       HA_NEXT,
340   GFX_ZEIT_VOLL,1,25, GFX_ZEIT_LEER,1,25,                       HA_NEXT,
341   GFX_TROPFEN,1,25, GFX_AMOEBING,4,1, GFX_AMOEBE_LEBT,1,10,     HA_NEXT,
342   GFX_AMOEBE_TOT+2,2,50, GFX_AMOEBE_TOT,2,50,                   HA_NEXT,
343   GFX_AMOEBE_LEBT,4,40,                                         HA_NEXT,
344   GFX_AMOEBE_LEBT,1,10, GFX_AMOEBING,4,2,                       HA_NEXT,
345   GFX_AMOEBE_LEBT,1,25, GFX_AMOEBE_TOT,1,25, GFX_EXPLOSION,8,1,
346   GFX_DIAMANT,1,10,                                             HA_NEXT,
347   GFX_LIFE,1,100,                                               HA_NEXT,
348   GFX_LIFE_ASYNC,1,100,                                         HA_NEXT,
349   GFX_SIEB_LEER,4,2,                                            HA_NEXT,
350   GFX_SIEB2_LEER,4,2,                                           HA_NEXT,
351   GFX_AUSGANG_ZU,1,100, GFX_AUSGANG_ACT,4,2,
352   GFX_AUSGANG_AUF+0,4,2, GFX_AUSGANG_AUF+3,1,2,
353   GFX_AUSGANG_AUF+2,1,2, GFX_AUSGANG_AUF+1,1,2,                 HA_NEXT,
354   GFX_AUSGANG_AUF+0,4,2, GFX_AUSGANG_AUF+3,1,2,
355   GFX_AUSGANG_AUF+2,1,2, GFX_AUSGANG_AUF+1,1,2,                 HA_NEXT,
356   HA_END
357 };
358 static char *helpscreen_eltext[][2] =
359 {
360  {"THE HERO:",                          "(Is _this_ guy good old Rockford?)"},
361  {"Normal sand:",                       "You can dig through it"},
362  {"Empty field:",                       "You can walk through it"},
363  {"Quicksand: You cannot pass it,",     "but rocks can fall though it"},
364  {"Massive Wall:",                      "Nothing can go through it"},
365  {"Normal Wall: You can't go through",  "it, but you can bomb it away"},
366  {"Growing Wall: Grows to the left or", "right if there is an empty field"},
367  {"Invisible Wall: Behaves like normal","wall, but is invisible"},
368  {"Old Wall: Like normal wall, but",    "some things can fall down from it"},
369  {"Letter Wall: Looks like a letter,",  "behaves like a normal wall"},
370  {"Emerald: You must collect enough of","them to finish a level"},
371  {"Diamond: Counts as 3 emeralds, but", "can be destroyed by rocks"},
372  {"Diamond (BD style): Counts like one","emerald and behaves a bit different"},
373  {"Colorful Gems:",                     "Seem to behave like Emeralds"},
374  {"Rock: Smashes several things;",      "Can be moved by the player"},
375  {"Bomb: You can move it, but be",      "careful when dropping it"},
376  {"Nut: Throw a rock on it to open it;","Each nut contains an emerald"},
377  {"Wall with an emerald inside:",       "Bomb the wall away to get it"},
378  {"Wall with a diamond inside:",        "Bomb the wall away to get it"},
379  {"Wall with BD style diamond inside:", "Bomb the wall away to get it"},
380  {"Wall with colorful gem inside:",     "Bomb the wall away to get it"},
381  {"Acid: Things that fall in are gone", "forever (including our hero)"},
382  {"Key: Opens the door that has the",   "same color (red/yellow/green/blue)"},
383  {"Door: Can be opened by the key",     "with the same color"},
384  {"Door: You have to find out the",     "right color of the key for it"},
385  {"Dynamite: Collect it and use it to", "destroy walls or kill enemies"},
386  {"Dynamite: This one explodes after",  "a few seconds"},
387  {"Dyna Bomb: Explodes in 4 directions","with variable explosion size"},
388  {"Dyna Bomb: Increases the number of", "dyna bombs available at a time"},
389  {"Dyna Bomb: Increases the size of",   "explosion of dyna bombs"},
390  {"Spaceship: Moves at the left side",  "of walls; don't touch it!"},
391  {"Bug: Moves at the right side",       "of walls; don't touch it!"},
392  {"Butterfly: Moves at the right side", "of walls; don't touch it!"},
393  {"Firefly: Moves at the left side",    "of walls; don't touch it!"},
394  {"Pacman: Eats the amoeba and you,",   "if you're not careful"},
395  {"Cruncher: Eats diamonds and you,",   "if you're not careful"},
396  {"Cruncher (BD style):",               "Eats almost everything"},
397  {"Robot: Tries to kill the player",    ""},
398  {"The mole: You must guide him savely","to the exit; he will follow you"},
399  {"The penguin: Guide him to the exit,","but keep him away from monsters!"},
400  {"The Pig: Harmless, but eats all",    "gems it can get"},
401  {"The Dragon: Breathes fire,",         "especially to some monsters"},
402  {"Sonde: Follows you everywhere;",     "harmless, but may block your way"},
403  {"Magic Wheel: Touch it to get rid of","the robots for some seconds"},
404  {"Light Bulb: All of them must be",    "switched on to finish a level"},
405  {"Extra Time Orb: Adds some seconds",  "to the time available for the level"},
406  {"Amoeba Drop: Grows to an amoeba on", "the ground - don't touch it"},
407  {"Dead Amoeba: Does not grow, but",    "can still kill bugs and spaceships"},
408  {"Normal Amoeba: Grows through empty", "fields, sand and quicksand"},
409  {"Dropping Amoeba: This one makes",    "drops that grow to a new amoeba"},
410  {"Living Amoeba (BD style): Contains", "other element, when surrounded"},
411  {"Game Of Life: Behaves like the well","known 'Game Of Life' (2333 style)"},
412  {"Biomaze: A bit like the 'Game Of",   "Life', but builds crazy mazes"},
413  {"Magic Wall: Changes rocks, emeralds","and diamonds when they pass it"},
414  {"Magic Wall (BD style):",             "Changes rocks and BD style diamonds"},
415  {"Exit door: Opens if you have enough","emeralds to finish the level"},
416  {"Open exit door: Enter here to leave","the level and exit the actual game"},
417 };
418 static int num_helpscreen_els = sizeof(helpscreen_eltext)/(2*sizeof(char *));
419
420 static char *helpscreen_music[][3] =
421 {
422   { "Alchemy",                  "Ian Boddy",            "Drive" },
423   { "The Chase",                "Propaganda",           "A Secret Wish" },
424   { "Network 23",               "Tangerine Dream",      "Exit" },
425   { "Czardasz",                 "Robert Pieculewicz",   "Czardasz" },
426   { "21st Century Common Man",  "Tangerine Dream",      "Tyger" },
427   { "Voyager",                  "The Alan Parsons Project","Pyramid" },
428   { "Twilight Painter",         "Tangerine Dream",      "Heartbreakers" }
429 };
430 static int helpscreen_musicpos;
431
432 void DrawHelpScreenElAction(int start)
433 {
434   int i = 0, j = 0;
435   int frame, graphic;
436   int xstart = SX+16, ystart = SY+64+2*32, ystep = TILEY+4;
437
438   while(helpscreen_action[j] != HA_END)
439   {
440     if (i>=start+MAX_HELPSCREEN_ELS || i>=num_helpscreen_els)
441       break;
442     else if (i<start || helpscreen_delay[i-start])
443     {
444       if (i>=start && helpscreen_delay[i-start])
445         helpscreen_delay[i-start]--;
446
447       while(helpscreen_action[j] != HA_NEXT)
448         j++;
449       j++;
450       i++;
451       continue;
452     }
453
454     j += 3*helpscreen_step[i-start];
455     graphic = helpscreen_action[j++];
456
457     if (helpscreen_frame[i-start])
458     {
459       frame = helpscreen_action[j++] - helpscreen_frame[i-start];
460       helpscreen_frame[i-start]--;
461     }
462     else
463     {
464       frame = 0;
465       helpscreen_frame[i-start] = helpscreen_action[j++]-1;
466     }
467
468     helpscreen_delay[i-start] = helpscreen_action[j++] - 1;
469
470     if (helpscreen_action[j] == HA_NEXT)
471     {
472       if (!helpscreen_frame[i-start])
473         helpscreen_step[i-start] = 0;
474     }
475     else
476     {
477       if (!helpscreen_frame[i-start])
478         helpscreen_step[i-start]++;
479       while(helpscreen_action[j] != HA_NEXT)
480         j++;
481     }
482     j++;
483
484     DrawGraphicExt(drawto, gc, xstart, ystart+(i-start)*ystep, graphic+frame);
485     i++;
486   }
487
488   for(i=2;i<16;i++)
489   {
490     MarkTileDirty(0,i);
491     MarkTileDirty(1,i);
492   }
493 }
494
495 void DrawHelpScreenElText(int start)
496 {
497   int i;
498   int xstart = SX+56, ystart = SY+65+2*32, ystep = TILEY+4;
499   char text[FULL_SXSIZE/FONT2_XSIZE+10];
500
501   ClearWindow();
502   DrawHeadline();
503
504   sprintf(text,"The game elements:");
505   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+100,
506            text,FS_SMALL,FC_GREEN);
507
508   for(i=start;i<start+MAX_HELPSCREEN_ELS && i<num_helpscreen_els;i++)
509   {
510     DrawText(xstart,ystart+(i-start)*ystep+(*helpscreen_eltext[i][1] ? 0 : 8),
511              helpscreen_eltext[i][0],FS_SMALL,FC_YELLOW);
512     DrawText(xstart,ystart+(i-start)*ystep+16,
513              helpscreen_eltext[i][1],FS_SMALL,FC_YELLOW);
514   }
515
516   sprintf(text,"Press any key or button for next page");
517   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+SYSIZE-20,
518            text,FS_SMALL,FC_BLUE);
519 }
520
521 void DrawHelpScreenMusicText(int num)
522 {
523   int ystart = 150, ystep = 30;
524   char text[FULL_SXSIZE/FONT2_XSIZE+10];
525
526   FadeSounds();
527   ClearWindow();
528   DrawHeadline();
529
530   sprintf(text,"The game background music loops:");
531   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+100,
532            text,FS_SMALL,FC_GREEN);
533
534   sprintf(text,"Excerpt from");
535   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+0*ystep,
536            text,FS_SMALL,FC_YELLOW);
537   sprintf(text,"%c%s%c",'\"',helpscreen_music[num][0],'\"');
538   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+1*ystep,
539            text,FS_SMALL,FC_RED);
540   sprintf(text,"by");
541   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+2*ystep,
542            text,FS_SMALL,FC_YELLOW);
543   sprintf(text,"%s",helpscreen_music[num][1]);
544   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+3*ystep,
545            text,FS_SMALL,FC_RED);
546   sprintf(text,"from the album");
547   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+4*ystep,
548            text,FS_SMALL,FC_YELLOW);
549   sprintf(text,"%c%s%c",'\"',helpscreen_music[num][2],'\"');
550   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+5*ystep,
551            text,FS_SMALL,FC_RED);
552
553   sprintf(text,"Press any key or button for next page");
554   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+SYSIZE-20,
555            text,FS_SMALL,FC_BLUE);
556
557   PlaySoundLoop(background_loop[num]);
558 }
559
560 void DrawHelpScreenCreditsText()
561 {
562   int ystart = 150, ystep = 30;
563   char text[FULL_SXSIZE/FONT2_XSIZE+10];
564
565   FadeSounds();
566   ClearWindow();
567   DrawHeadline();
568
569   sprintf(text,"Program information:");
570   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+100,
571            text,FS_SMALL,FC_GREEN);
572
573   sprintf(text,"This game is Freeware!");
574   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+0*ystep,
575            text,FS_SMALL,FC_YELLOW);
576   sprintf(text,"If you like it, send e-mail to:");
577   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+1*ystep,
578            text,FS_SMALL,FC_YELLOW);
579   sprintf(text,"aeglos@valinor.owl.de");
580   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+2*ystep,
581            text,FS_SMALL,FC_RED);
582   sprintf(text,"or SnailMail to:");
583   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+3*ystep,
584            text,FS_SMALL,FC_YELLOW);
585   sprintf(text,"Holger Schemel");
586   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+4*ystep,
587            text,FS_SMALL,FC_RED);
588   sprintf(text,"Sennehof 28");
589   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+4*ystep+20,
590            text,FS_SMALL,FC_RED);
591   sprintf(text,"33659 Bielefeld");
592   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+4*ystep+40,
593            text,FS_SMALL,FC_RED);
594   sprintf(text,"Germany");
595   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+4*ystep+60,
596            text,FS_SMALL,FC_RED);
597
598   sprintf(text,"If you have created new levels,");
599   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+7*ystep,
600            text,FS_SMALL,FC_YELLOW);
601   sprintf(text,"send them to me to include them!");
602   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+8*ystep,
603            text,FS_SMALL,FC_YELLOW);
604   sprintf(text,":-)");
605   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+ystart+9*ystep,
606            text,FS_SMALL,FC_YELLOW);
607
608   sprintf(text,"Press any key or button for main menu");
609   DrawText(SX+(SXSIZE-strlen(text)*FONT2_XSIZE)/2,SY+SYSIZE-20,
610            text,FS_SMALL,FC_BLUE);
611 }
612
613 void DrawHelpScreen()
614 {
615   int i;
616
617   CloseDoor(DOOR_CLOSE_2);
618
619   for(i=0;i<MAX_HELPSCREEN_ELS;i++)
620     helpscreen_step[i] = helpscreen_frame[i] = helpscreen_delay[i] = 0;
621   helpscreen_musicpos = 0;
622   helpscreen_state = 0;
623   DrawHelpScreenElText(0);
624   DrawHelpScreenElAction(0);
625
626   FadeToFront();
627   InitAnimation();
628   PlaySoundLoop(SND_RHYTHMLOOP);
629 }
630
631 void HandleHelpScreen(int button)
632 {
633   static long hs_delay = 0;
634   int num_helpscreen_els_pages =
635     (num_helpscreen_els + MAX_HELPSCREEN_ELS-1) / MAX_HELPSCREEN_ELS;
636   int button_released = !button;
637   int i;
638
639   if (button_released)
640   {
641     if (helpscreen_state<num_helpscreen_els_pages-1)
642     {
643       for(i=0;i<MAX_HELPSCREEN_ELS;i++)
644         helpscreen_step[i] = helpscreen_frame[i] = helpscreen_delay[i] = 0;
645       helpscreen_state++;
646       DrawHelpScreenElText(helpscreen_state*MAX_HELPSCREEN_ELS);
647       DrawHelpScreenElAction(helpscreen_state*MAX_HELPSCREEN_ELS);
648     }
649     else if (helpscreen_state<num_helpscreen_els_pages+num_bg_loops-1)
650     {
651       helpscreen_state++;
652       DrawHelpScreenMusicText(helpscreen_state-num_helpscreen_els_pages);
653     }
654     else if (helpscreen_state==num_helpscreen_els_pages+num_bg_loops-1)
655     {
656       helpscreen_state++;
657       DrawHelpScreenCreditsText();
658     }
659     else
660     {
661       FadeSounds();
662       DrawMainMenu();
663       game_status = MAINMENU;
664     }
665   }
666   else
667   {
668     if (DelayReached(&hs_delay,GAME_FRAME_DELAY * 2))
669     {
670       if (helpscreen_state<num_helpscreen_els_pages)
671         DrawHelpScreenElAction(helpscreen_state*MAX_HELPSCREEN_ELS);
672     }
673     DoAnimation();
674   }
675
676   BackToFront();
677 }
678
679 void CheckCheat()
680 {
681   int old_handicap = local_player->handicap;
682
683   if (!strcmp(local_player->alias_name,"Artsoft"))
684     local_player->handicap = leveldir[leveldir_nr].levels-1;
685
686   if (local_player->handicap != old_handicap)
687   {
688     SavePlayerInfo(PLAYER_LEVEL);
689     level_nr = local_player->handicap;
690   }
691 }
692
693 void HandleTypeName(int newxpos, KeySym key)
694 {
695   static int xpos = 0, ypos = 2;
696   unsigned char ascii;
697
698   if (newxpos)
699   {
700     xpos = newxpos;
701     DrawText(SX+6*32, SY+ypos*32, local_player->alias_name, FS_BIG, FC_YELLOW);
702     DrawGraphic(xpos+6,ypos,GFX_KUGEL_ROT);
703     return;
704   }
705
706 #ifndef MSDOS
707   if ((key>=XK_A && key<=XK_Z) || (key>=XK_a && key<=XK_z && 
708       xpos<MAX_NAMELEN-1))
709   {
710     if (key>=XK_A && key<=XK_Z)
711       ascii = 'A'+(char)(key-XK_A);
712     if (key>=XK_a && key<=XK_z)
713       ascii = 'a'+(char)(key-XK_a);
714 #else
715   if((ascii = get_ascii(key)) && xpos<MAX_NAMELEN-1)
716   {
717 #endif
718     local_player->alias_name[xpos] = ascii;
719     local_player->alias_name[xpos+1] = 0;
720     xpos++;
721     DrawTextExt(drawto,gc,SX+6*32,SY+ypos*32,
722                 local_player->alias_name,FS_BIG,FC_YELLOW);
723     DrawTextExt(window,gc,SX+6*32,SY+ypos*32,
724                 local_player->alias_name,FS_BIG,FC_YELLOW);
725     DrawGraphic(xpos+6,ypos,GFX_KUGEL_ROT);
726   }
727   else if ((key==XK_Delete || key==XK_BackSpace) && xpos>0)
728   {
729     xpos--;
730     local_player->alias_name[xpos] = 0;
731     DrawGraphic(xpos+6,ypos,GFX_KUGEL_ROT);
732     DrawGraphic(xpos+7,ypos,GFX_LEERRAUM);
733   }
734   else if (key==XK_Return && xpos>0)
735   {
736     DrawText(SX+6*32,SY+ypos*32,local_player->alias_name,FS_BIG,FC_RED);
737     DrawGraphic(xpos+6,ypos,GFX_LEERRAUM);
738     SavePlayerInfo(PLAYER_SETUP);
739     CheckCheat();
740
741     game_status = MAINMENU;
742 /*
743     DrawMainMenu();
744 */
745
746   }
747   BackToFront();
748 }
749
750 void DrawChooseLevel()
751 {
752   int i;
753
754   CloseDoor(DOOR_CLOSE_2);
755
756   ClearWindow();
757   DrawText(SX,SY,"Level Directories",FS_BIG,FC_GREEN);
758   for(i=0;i<num_leveldirs;i++)
759   {
760     DrawText(SX+32,SY+(i+2)*32,leveldir[i].name,FS_BIG,FC_YELLOW);
761     DrawGraphic(0,i+2,GFX_KUGEL_BLAU);
762   }
763
764   FadeToFront();
765   InitAnimation();
766   HandleChooseLevel(0,0,0,0,MB_MENU_INITIALIZE);
767 }
768
769 void HandleChooseLevel(int mx, int my, int dx, int dy, int button)
770 {
771   static int choice = 3;
772   static int redraw = TRUE;
773   int x = (mx+32-SX)/32, y = (my+32-SY)/32;
774
775   if (button == MB_MENU_INITIALIZE)
776   {
777     redraw = TRUE;
778     choice = leveldir_nr + 3;
779   }
780
781   if (redraw)
782   {
783     DrawGraphic(0,choice-1,GFX_KUGEL_ROT);
784     redraw = FALSE;
785   }
786
787   if (button == MB_MENU_INITIALIZE)
788     return;
789
790   if (dx || dy)
791   {
792     if (dy)
793     {
794       x = 1;
795       y = choice+dy;
796     }
797     else
798       x = y = 0;
799
800     if (y<3)
801       y = 3;
802     else if (y>num_leveldirs+2)
803       y = num_leveldirs+2;
804   }
805
806   if (!mx && !my && !dx && !dy)
807   {
808     x = 1;
809     y = choice;
810   }
811
812   if (x==1 && y>=3 && y<=num_leveldirs+2)
813   {
814     if (button)
815     {
816       if (y!=choice)
817       {
818         DrawGraphic(0,y-1,GFX_KUGEL_ROT);
819         DrawGraphic(0,choice-1,GFX_KUGEL_BLAU);
820       }
821       choice = y;
822     }
823     else
824     {
825       local_player->leveldir_nr = leveldir_nr = y-3;
826       LoadPlayerInfo(PLAYER_LEVEL);
827       SavePlayerInfo(PLAYER_SETUP);
828       CheckCheat();
829
830       TapeErase();
831       LoadLevelTape(level_nr);
832
833       game_status = MAINMENU;
834       DrawMainMenu();
835       redraw = TRUE;
836     }
837   }
838   BackToFront();
839
840   if (game_status==CHOOSELEVEL)
841     DoAnimation();
842 }
843
844 void DrawHallOfFame(int pos)
845 {
846   int y;
847   char txt[40];
848
849   CloseDoor(DOOR_CLOSE_2);
850
851   if (pos<0) 
852     LoadScore(level_nr);
853   ClearWindow();
854   DrawText(SX+64,SY+10,"Hall Of Fame",FS_BIG,FC_YELLOW);
855   sprintf(txt,"HighScores of Level %d",level_nr);
856   DrawText(SX+256-strlen(txt)*7,SY+48,txt,FS_SMALL,FC_RED);
857   for(y=0;y<MAX_SCORE_ENTRIES;y++)
858   {
859     DrawText(SX,SY+64+y*32,".................",FS_BIG,
860              (y==pos ? FC_RED : FC_GREEN));
861     DrawText(SX,SY+64+y*32,highscore[y].Name,FS_BIG,
862              (y==pos ? FC_RED : FC_GREEN));
863     DrawText(SX+12*32,SY+64+y*32,
864              int2str(highscore[y].Score,5),FS_BIG,
865              (y==pos ? FC_RED : FC_GREEN));
866   }
867
868   FadeToFront();
869   InitAnimation();
870   PlaySound(SND_HALLOFFAME);
871 }
872
873 void HandleHallOfFame(int button)
874 {
875   int button_released = !button;
876
877   if (button_released)
878   {
879     FadeSound(SND_HALLOFFAME);
880     game_status = MAINMENU;
881     DrawMainMenu();
882     BackToFront();
883   }
884   else
885     DoAnimation();
886 }
887
888 void DrawSetupScreen()
889 {
890   int i;
891   static struct setup
892   {
893     unsigned int bit;
894     char *text, *mode[2];
895     int color[2];
896   } setup[] =
897   {
898     {SETUP_SOUND,       "Sound:",       {"on", "off"},  {FC_YELLOW,FC_BLUE}},
899     {SETUP_SOUND_LOOPS, " Sound Loops:",{"on", "off"},  {FC_YELLOW,FC_BLUE}},
900     {SETUP_SOUND_MUSIC, " Game Music:", {"on", "off"},  {FC_YELLOW,FC_BLUE}},
901     {SETUP_TOONS,       "Toons:",       {"on", "off"},  {FC_YELLOW,FC_BLUE}},
902     {SETUP_DIRECT_DRAW, "Buffered gfx:",{"off","on" },  {FC_BLUE,FC_YELLOW}},
903     {SETUP_SCROLL_DELAY,"Scroll Delay:",{"on", "off"},  {FC_YELLOW,FC_BLUE}},
904     {SETUP_SOFT_SCROLL, "Soft Scroll.:",{"on", "off"},  {FC_YELLOW,FC_BLUE}},
905     {SETUP_FADING,      "Fading:",      {"on", "off"},  {FC_YELLOW,FC_BLUE}},
906     {SETUP_QUICK_DOORS, "Quick Doors:", {"on", "off"},  {FC_YELLOW,FC_BLUE}},
907     {SETUP_AUTO_RECORD, "Auto-Record:", {"on", "off"},  {FC_YELLOW,FC_BLUE}},
908     {SETUP_2ND_JOYSTICK,"Joystick:",    {"2nd","1st"},  {FC_YELLOW,FC_YELLOW}},
909     {0,                 "Cal. Joystick",{"",   ""},     {0,0}},
910     {0,                 "",             {"",   ""},     {0,0}},
911     {0,                 "Exit",         {"",   ""},     {0,0}},
912     {0,                 "Save and exit",{"",   ""},     {0,0}}
913   };
914
915   CloseDoor(DOOR_CLOSE_2);
916   ClearWindow();
917   DrawText(SX+16, SY+16,  "SETUP",FS_BIG,FC_YELLOW);
918
919   for(i=SETUP_SCREEN_POS_START;i<=SETUP_SCREEN_POS_END;i++)
920   {
921     int base = i - SETUP_SCREEN_POS_START;
922
923     if (i != SETUP_SCREEN_POS_EMPTY)
924     {
925       DrawGraphic(0,i,GFX_KUGEL_BLAU);
926       DrawText(SX+32,SY+i*32, setup[base].text, FS_BIG,FC_GREEN);
927     }
928
929     if (i < SETUP_SCREEN_POS_EMPTY)
930     {
931       int setting_bit = setup[base].bit;
932       int setting_pos = ((local_player->setup & setting_bit) != 0 ? 0 : 1);
933       DrawText(SX+14*32, SY+i*32,setup[base].mode[setting_pos],
934                FS_BIG,setup[base].color[setting_pos]);
935     }
936   }
937
938   FadeToFront();
939   InitAnimation();
940   HandleSetupScreen(0,0,0,0,MB_MENU_INITIALIZE);
941 }
942
943 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
944 {
945   static int choice = 3;
946   static int redraw = TRUE;
947   int x = (mx+32-SX)/32, y = (my+32-SY)/32;
948   int pos_start = SETUP_SCREEN_POS_START + 1;
949   int pos_empty = SETUP_SCREEN_POS_EMPTY + 1;
950   int pos_end   = SETUP_SCREEN_POS_END   + 1;
951
952   if (button == MB_MENU_INITIALIZE)
953     redraw = TRUE;
954
955   if (redraw)
956   {
957     DrawGraphic(0,choice-1,GFX_KUGEL_ROT);
958     redraw = FALSE;
959   }
960
961   if (button == MB_MENU_INITIALIZE)
962     return;
963
964   if (dx || dy)
965   {
966     if (dy)
967     {
968       x = 1;
969       y = choice+dy;
970     }
971     else
972       x = y = 0;
973
974     if (y == pos_empty)
975       y = (dy>0 ? pos_empty+1 : pos_empty-1);
976
977     if (y < pos_start)
978       y = pos_start;
979     else if (y > pos_end)
980       y = pos_end;
981   }
982
983   if (!mx && !my && !dx && !dy)
984   {
985     x = 1;
986     y = choice;
987   }
988
989   if (x==1 && y>=pos_start && y<=pos_end && y!=pos_empty)
990   {
991     if (button)
992     {
993       if (y!=choice)
994       {
995         DrawGraphic(0,y-1,GFX_KUGEL_ROT);
996         DrawGraphic(0,choice-1,GFX_KUGEL_BLAU);
997       }
998       choice = y;
999     }
1000     else
1001     {
1002       int yy = y-1;
1003
1004       if (y==3 && sound_status==SOUND_AVAILABLE)
1005       {
1006         if (SETUP_SOUND_ON(local_player->setup))
1007         {
1008           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1009           DrawText(SX+14*32, SY+(yy+1)*32,"off",FS_BIG,FC_BLUE);
1010           DrawText(SX+14*32, SY+(yy+2)*32,"off",FS_BIG,FC_BLUE);
1011           local_player->setup &= ~SETUP_SOUND_LOOPS;
1012           local_player->setup &= ~SETUP_SOUND_MUSIC;
1013         }
1014         else
1015           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1016         local_player->setup ^= SETUP_SOUND;
1017       }
1018       else if (y==4 && sound_loops_allowed)
1019       {
1020         if (SETUP_SOUND_LOOPS_ON(local_player->setup))
1021           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1022         else
1023         {
1024           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1025           DrawText(SX+14*32, SY+(yy-1)*32,"on ",FS_BIG,FC_YELLOW);
1026           local_player->setup |= SETUP_SOUND;
1027         }
1028         local_player->setup ^= SETUP_SOUND_LOOPS;
1029       }
1030       else if (y==5 && sound_loops_allowed)
1031       {
1032         if (SETUP_SOUND_MUSIC_ON(local_player->setup))
1033           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1034         else
1035         {
1036           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1037           DrawText(SX+14*32, SY+(yy-2)*32,"on ",FS_BIG,FC_YELLOW);
1038           local_player->setup |= SETUP_SOUND;
1039         }
1040         local_player->setup ^= SETUP_SOUND_MUSIC;
1041       }
1042       else if (y==6)
1043       {
1044         if (SETUP_TOONS_ON(local_player->setup))
1045           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1046         else
1047           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1048         local_player->setup ^= SETUP_TOONS;
1049       }
1050       else if (y==7)
1051       {
1052         if (!SETUP_DIRECT_DRAW_ON(local_player->setup))
1053           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1054         else
1055           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1056         local_player->setup ^= SETUP_DIRECT_DRAW;
1057       }
1058       else if (y==8)
1059       {
1060         if (SETUP_SCROLL_DELAY_ON(local_player->setup))
1061           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1062         else
1063           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1064         local_player->setup ^= SETUP_SCROLL_DELAY;
1065       }
1066       else if (y==9)
1067       {
1068         if (SETUP_SOFT_SCROLL_ON(local_player->setup))
1069           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1070         else
1071           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1072         local_player->setup ^= SETUP_SOFT_SCROLL;
1073       }
1074       else if (y==10)
1075       {
1076         if (SETUP_FADING_ON(local_player->setup))
1077           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1078         else
1079           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1080         local_player->setup ^= SETUP_FADING;
1081       }
1082       else if (y==11)
1083       {
1084         if (SETUP_QUICK_DOORS_ON(local_player->setup))
1085           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1086         else
1087           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1088         local_player->setup ^= SETUP_QUICK_DOORS;
1089       }
1090       else if (y==12)
1091       {
1092         if (SETUP_AUTO_RECORD_ON(local_player->setup))
1093           DrawText(SX+14*32, SY+yy*32,"off",FS_BIG,FC_BLUE);
1094         else
1095           DrawText(SX+14*32, SY+yy*32,"on ",FS_BIG,FC_YELLOW);
1096         local_player->setup ^= SETUP_AUTO_RECORD;
1097       }
1098       else if (y==13)
1099       {
1100         if (SETUP_2ND_JOYSTICK_ON(local_player->setup))
1101           DrawText(SX+14*32, SY+yy*32,"1st",FS_BIG,FC_YELLOW);
1102         else
1103           DrawText(SX+14*32, SY+yy*32,"2nd",FS_BIG,FC_YELLOW);
1104         local_player->setup ^= SETUP_2ND_JOYSTICK;
1105       }
1106       else if (y==14)
1107       {
1108         CalibrateJoystick();
1109         redraw = TRUE;
1110       }
1111       else if (y==pos_end-1 || y==pos_end)
1112       {
1113         if (y==pos_end)
1114         {
1115           SavePlayerInfo(PLAYER_SETUP);
1116           SaveJoystickData();
1117         }
1118
1119         game_status = MAINMENU;
1120         DrawMainMenu();
1121         redraw = TRUE;
1122       }
1123     }
1124   }
1125   BackToFront();
1126
1127   if (game_status==SETUP)
1128     DoAnimation();
1129 }
1130
1131 void CalibrateJoystick()
1132 {
1133 #ifdef __FreeBSD__
1134   struct joystick joy_ctrl;
1135 #else
1136   struct joystick_control
1137   {
1138     int buttons;
1139     int x;
1140     int y;
1141   } joy_ctrl;
1142 #endif
1143
1144 #ifdef MSDOS
1145   char joy_nr[4];
1146 #endif
1147
1148   int new_joystick_xleft, new_joystick_xright, new_joystick_xmiddle;
1149   int new_joystick_yupper, new_joystick_ylower, new_joystick_ymiddle;
1150
1151   if (joystick_status==JOYSTICK_OFF)
1152     goto error_out;
1153
1154 #ifndef MSDOS
1155   ClearWindow();
1156   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1157   DrawText(SX+16, SY+8*32, " THE UPPER LEFT ",FS_BIG,FC_YELLOW);
1158   DrawText(SX+16, SY+9*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1159   BackToFront();
1160
1161 #ifdef __FreeBSD__
1162   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1163 #else
1164   joy_ctrl.buttons = 0;
1165 #endif
1166   while(Joystick() & JOY_BUTTON);
1167 #ifdef __FreeBSD__
1168   while(!(joy_ctrl.b1||joy_ctrl.b2))
1169 #else
1170   while(!joy_ctrl.buttons)
1171 #endif
1172   {
1173     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1174     {
1175       joystick_status=JOYSTICK_OFF;
1176       goto error_out;
1177     }
1178     Delay(10);
1179   }
1180
1181   new_joystick_xleft = joy_ctrl.x;
1182   new_joystick_yupper = joy_ctrl.y;
1183
1184   ClearWindow();
1185   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1186   DrawText(SX+32, SY+8*32, "THE LOWER RIGHT",FS_BIG,FC_YELLOW);
1187   DrawText(SX+16, SY+9*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1188   BackToFront();
1189
1190 #ifdef __FreeBSD__
1191   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1192 #else
1193   joy_ctrl.buttons = 0;
1194 #endif
1195   while(Joystick() & JOY_BUTTON);
1196 #ifdef __FreeBSD__
1197   while(!(joy_ctrl.b1||joy_ctrl.b2))
1198 #else
1199   while(!joy_ctrl.buttons)
1200 #endif
1201   {
1202     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1203     {
1204       joystick_status=JOYSTICK_OFF;
1205       goto error_out;
1206     }
1207     Delay(10);
1208   }
1209
1210   new_joystick_xright = joy_ctrl.x;
1211   new_joystick_ylower = joy_ctrl.y;
1212
1213   ClearWindow();
1214   DrawText(SX+32, SY+16+7*32, "CENTER JOYSTICK",FS_BIG,FC_YELLOW);
1215   DrawText(SX+16, SY+16+8*32, "AND PRESS BUTTON",FS_BIG,FC_YELLOW);
1216   BackToFront();
1217
1218 #ifdef __FreeBSD__
1219   joy_ctrl.b1 = joy_ctrl.b2 = 0;
1220 #else
1221   joy_ctrl.buttons = 0;
1222 #endif
1223   while(Joystick() & JOY_BUTTON);
1224 #ifdef __FreeBSD__
1225   while(!(joy_ctrl.b1||joy_ctrl.b2))
1226 #else
1227   while(!joy_ctrl.buttons)
1228 #endif
1229   {
1230     if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
1231     {
1232       joystick_status=JOYSTICK_OFF;
1233       goto error_out;
1234     }
1235     Delay(10);
1236   }
1237
1238   new_joystick_xmiddle = joy_ctrl.x;
1239   new_joystick_ymiddle = joy_ctrl.y;
1240
1241   joystick[joystick_nr].xleft = new_joystick_xleft;
1242   joystick[joystick_nr].yupper = new_joystick_yupper;
1243   joystick[joystick_nr].xright = new_joystick_xright;
1244   joystick[joystick_nr].ylower = new_joystick_ylower;
1245   joystick[joystick_nr].xmiddle = new_joystick_xmiddle;
1246   joystick[joystick_nr].ymiddle = new_joystick_ymiddle;
1247
1248   CheckJoystickData();
1249
1250   DrawSetupScreen();
1251   while(Joystick() & JOY_BUTTON);
1252   return;
1253
1254 #endif
1255   error_out:
1256
1257 #ifdef MSDOS
1258   joy_nr[0] = '#';
1259   joy_nr[1] = SETUP_2ND_JOYSTICK_ON(local_player->setup)+49;
1260   joy_nr[2] = '\0';
1261
1262   remove_joystick();
1263   ClearWindow();
1264   DrawText(SX+32, SY+7*32, "CENTER JOYSTICK",FS_BIG,FC_YELLOW);
1265   DrawText(SX+16+7*32, SY+8*32, joy_nr, FS_BIG,FC_YELLOW);
1266   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
1267   BackToFront();
1268
1269   for(clear_keybuf();!keypressed(););
1270   install_joystick(JOY_TYPE_2PADS);
1271
1272   ClearWindow();
1273   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1274   DrawText(SX+16, SY+8*32, " THE UPPER LEFT ",FS_BIG,FC_YELLOW);
1275   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
1276   BackToFront();
1277
1278   for(clear_keybuf();!keypressed(););
1279   calibrate_joystick(SETUP_2ND_JOYSTICK_ON(local_player->setup));
1280
1281   ClearWindow();
1282   DrawText(SX+16, SY+7*32, "MOVE JOYSTICK TO",FS_BIG,FC_YELLOW);
1283   DrawText(SX+32, SY+8*32, "THE LOWER RIGHT",FS_BIG,FC_YELLOW);
1284   DrawText(SX+32, SY+9*32, "AND PRESS A KEY",FS_BIG,FC_YELLOW);
1285   BackToFront();
1286
1287   for(clear_keybuf();!keypressed(););
1288   calibrate_joystick(SETUP_2ND_JOYSTICK_ON(local_player->setup));
1289
1290   DrawSetupScreen();
1291   return;
1292 #endif
1293
1294   ClearWindow();
1295   DrawText(SX+16, SY+16, "NO JOYSTICK",FS_BIG,FC_YELLOW);
1296   DrawText(SX+16, SY+48, " AVAILABLE ",FS_BIG,FC_YELLOW);
1297   BackToFront();
1298   Delay(3000);
1299   DrawSetupScreen();
1300 }
1301
1302 void HandleGameActions(int player_action)
1303 {
1304   if (game_status != PLAYING)
1305     return;
1306
1307   if (local_player->LevelSolved)
1308     GameWon();
1309
1310   if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
1311     TapeStop();
1312
1313   GameActions(player_action);
1314
1315   BackToFront();
1316 }
1317
1318 void HandleVideoButtons(int mx, int my, int button)
1319 {
1320   if (game_status != MAINMENU && game_status != PLAYING)
1321     return;
1322
1323   switch(CheckVideoButtons(mx,my,button))
1324   {
1325     case BUTTON_VIDEO_EJECT:
1326       TapeStop();
1327       if (TAPE_IS_EMPTY(tape))
1328       {
1329         LoadLevelTape(level_nr);
1330         if (TAPE_IS_EMPTY(tape))
1331           AreYouSure("No tape for this level !",AYS_CONFIRM);
1332       }
1333       else
1334       {
1335         if (tape.changed)
1336           SaveLevelTape(tape.level_nr);
1337         TapeErase();
1338       }
1339       DrawCompleteVideoDisplay();
1340       break;
1341     case BUTTON_VIDEO_STOP:
1342       TapeStop();
1343       break;
1344     case BUTTON_VIDEO_PAUSE:
1345       TapeTogglePause();
1346       break;
1347     case BUTTON_VIDEO_REC:
1348       if (TAPE_IS_STOPPED(tape))
1349       {
1350         TapeStartRecording();
1351         game_status = PLAYING;
1352         InitGame();
1353       }
1354       else if (tape.pausing)
1355       {
1356         if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
1357         {
1358           tape.pos[tape.counter].delay = tape.delay_played;
1359           tape.playing = FALSE;
1360           tape.recording = TRUE;
1361           tape.changed = TRUE;
1362
1363           DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON,0);
1364         }
1365         else
1366           TapeTogglePause();
1367       }
1368       break;
1369     case BUTTON_VIDEO_PLAY:
1370       if (TAPE_IS_EMPTY(tape))
1371         break;
1372
1373       if (TAPE_IS_STOPPED(tape))
1374       {
1375         TapeStartPlaying();
1376         game_status = PLAYING;
1377         InitGame();
1378       }
1379       else if (tape.playing)
1380       {
1381         if (tape.pausing)                       /* PAUSE -> PLAY */
1382           TapeTogglePause();
1383         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
1384         {
1385           tape.fast_forward = TRUE;
1386           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
1387         }
1388         else if (!tape.pause_before_death)      /* FFWD PLAY -> + AUTO PAUSE */
1389         {
1390           tape.pause_before_death = TRUE;
1391           DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, VIDEO_DISPLAY_LABEL_ONLY);
1392         }
1393         else                                    /* -> NORMAL PLAY */
1394         {
1395           tape.fast_forward = FALSE;
1396           tape.pause_before_death = FALSE;
1397           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PAUSE_OFF, 0);
1398         }
1399       }
1400       break;
1401     default:
1402       break;
1403   }
1404
1405   BackToFront();
1406 }
1407
1408 void HandleSoundButtons(int mx, int my, int button)
1409 {
1410   if (game_status != PLAYING)
1411     return;
1412
1413   switch(CheckSoundButtons(mx,my,button))
1414   {
1415     case BUTTON_SOUND_MUSIC:
1416       if (sound_music_on)
1417       { 
1418         sound_music_on = FALSE;
1419         local_player->setup &= ~SETUP_SOUND_MUSIC;
1420         FadeSound(background_loop[level_nr % num_bg_loops]);
1421         DrawSoundDisplay(BUTTON_SOUND_MUSIC_OFF);
1422       }
1423       else if (sound_loops_allowed)
1424       { 
1425         sound_on = sound_music_on = TRUE;
1426         local_player->setup |= (SETUP_SOUND | SETUP_SOUND_MUSIC);
1427         PlaySoundLoop(background_loop[level_nr % num_bg_loops]);
1428         DrawSoundDisplay(BUTTON_SOUND_MUSIC_ON);
1429       }
1430       else
1431         DrawSoundDisplay(BUTTON_SOUND_MUSIC_OFF);
1432       break;
1433     case BUTTON_SOUND_LOOPS:
1434       if (sound_loops_on)
1435       { 
1436         sound_loops_on = FALSE;
1437         local_player->setup &= ~SETUP_SOUND_LOOPS;
1438         DrawSoundDisplay(BUTTON_SOUND_LOOPS_OFF);
1439       }
1440       else if (sound_loops_allowed)
1441       { 
1442         sound_on = sound_loops_on = TRUE;
1443         local_player->setup |= (SETUP_SOUND | SETUP_SOUND_LOOPS);
1444         DrawSoundDisplay(BUTTON_SOUND_LOOPS_ON);
1445       }
1446       else
1447         DrawSoundDisplay(BUTTON_SOUND_LOOPS_OFF);
1448       break;
1449     case BUTTON_SOUND_SIMPLE:
1450       if (sound_simple_on)
1451       { 
1452         sound_simple_on = FALSE;
1453         local_player->setup &= ~SETUP_SOUND;
1454         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_OFF);
1455       }
1456       else if (sound_status==SOUND_AVAILABLE)
1457       { 
1458         sound_on = sound_simple_on = TRUE;
1459         local_player->setup |= SETUP_SOUND;
1460         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_ON);
1461       }
1462       else
1463         DrawSoundDisplay(BUTTON_SOUND_SIMPLE_OFF);
1464       break;
1465     default:
1466       break;
1467   }
1468
1469   BackToFront();
1470 }
1471
1472 void HandleGameButtons(int mx, int my, int button)
1473 {
1474   if (game_status != PLAYING)
1475     return;
1476
1477   switch(CheckGameButtons(mx,my,button))
1478   {
1479     case BUTTON_GAME_STOP:
1480       if (AllPlayersGone)
1481       {
1482         CloseDoor(DOOR_CLOSE_1);
1483         game_status = MAINMENU;
1484         DrawMainMenu();
1485         break;
1486       }
1487
1488       if (AreYouSure("Do you really want to quit the game ?",
1489                       AYS_ASK | AYS_STAY_CLOSED))
1490       { 
1491         game_status = MAINMENU;
1492         DrawMainMenu();
1493       }
1494       else
1495         OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1496       break;
1497     case BUTTON_GAME_PAUSE:
1498       if (tape.pausing)
1499       {
1500         tape.pausing = FALSE;
1501         DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
1502       }
1503       else
1504       {
1505         tape.pausing = TRUE;
1506         DrawVideoDisplay(VIDEO_STATE_PAUSE_ON,0);
1507       }
1508       break;
1509     case BUTTON_GAME_PLAY:
1510       if (tape.pausing)
1511       {
1512         tape.pausing = FALSE;
1513         DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
1514       }
1515       break;
1516     default:
1517       break;
1518   }
1519
1520   BackToFront();
1521 }