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