rnd-20060810-1-src
[rocksndiamonds.git] / src / tape.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * tape.c                                                   *
12 ***********************************************************/
13
14 #include "libgame/libgame.h"
15
16 #include "tape.h"
17 #include "init.h"
18 #include "game.h"
19 #include "tools.h"
20 #include "files.h"
21 #include "network.h"
22 #include "cartoons.h"
23
24 /* tape button identifiers */
25 #define TAPE_CTRL_ID_EJECT                      0
26 #define TAPE_CTRL_ID_EXTRA                      1
27 #define TAPE_CTRL_ID_STOP                       2
28 #define TAPE_CTRL_ID_PAUSE                      3
29 #define TAPE_CTRL_ID_RECORD                     4
30 #define TAPE_CTRL_ID_PLAY                       5
31
32 #define NUM_TAPE_BUTTONS                        6
33
34 /* values for tape handling */
35 #define TAPE_PAUSE_SECONDS_BEFORE_DEATH         5
36
37 /* forward declaration for internal use */
38 static void HandleTapeButtons(struct GadgetInfo *);
39 static void TapeStopWarpForward();
40
41 static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
42
43
44 /* ========================================================================= */
45 /* video tape definitions                                                    */
46 /* ========================================================================= */
47
48 /* some positions in the video tape control window */
49 #define VIDEO_DATE_LABEL_XPOS           (VIDEO_DISPLAY1_XPOS)
50 #define VIDEO_DATE_LABEL_YPOS           (VIDEO_DISPLAY1_YPOS)
51 #define VIDEO_DATE_LABEL_XSIZE          (VIDEO_DISPLAY_XSIZE)
52 #define VIDEO_DATE_LABEL_YSIZE          (VIDEO_DISPLAY_YSIZE)
53 #define VIDEO_DATE_XPOS                 (VIDEO_DISPLAY1_XPOS + 2)
54 #define VIDEO_DATE_YPOS                 (VIDEO_DISPLAY1_YPOS + 14)
55 #define VIDEO_DATE_XSIZE                (VIDEO_DISPLAY_XSIZE)
56 #define VIDEO_DATE_YSIZE                16
57 #define VIDEO_REC_LABEL_XPOS            (VIDEO_DISPLAY2_XPOS)
58 #define VIDEO_REC_LABEL_YPOS            (VIDEO_DISPLAY2_YPOS)
59 #define VIDEO_REC_LABEL_XSIZE           20
60 #define VIDEO_REC_LABEL_YSIZE           12
61 #define VIDEO_REC_SYMBOL_XPOS           (VIDEO_DISPLAY2_XPOS + 20)
62 #define VIDEO_REC_SYMBOL_YPOS           (VIDEO_DISPLAY2_YPOS)
63 #define VIDEO_REC_SYMBOL_XSIZE          16
64 #define VIDEO_REC_SYMBOL_YSIZE          16
65 #define VIDEO_PLAY_LABEL_XPOS           (VIDEO_DISPLAY2_XPOS + 65)
66 #define VIDEO_PLAY_LABEL_YPOS           (VIDEO_DISPLAY2_YPOS)
67 #define VIDEO_PLAY_LABEL_XSIZE          22
68 #define VIDEO_PLAY_LABEL_YSIZE          12
69 #define VIDEO_PLAY_SYMBOL_XPOS          (VIDEO_DISPLAY2_XPOS + 52)
70 #define VIDEO_PLAY_SYMBOL_YPOS          (VIDEO_DISPLAY2_YPOS)
71 #define VIDEO_PLAY_SYMBOL_XSIZE         11
72 #define VIDEO_PLAY_SYMBOL_YSIZE         13
73 #define VIDEO_PAUSE_LABEL_XPOS          (VIDEO_DISPLAY2_XPOS)
74 #define VIDEO_PAUSE_LABEL_YPOS          (VIDEO_DISPLAY2_YPOS + 20)
75 #define VIDEO_PAUSE_LABEL_XSIZE         35
76 #define VIDEO_PAUSE_LABEL_YSIZE         8
77 #define VIDEO_PAUSE_SYMBOL_XPOS         (VIDEO_DISPLAY2_XPOS + 35)
78 #define VIDEO_PAUSE_SYMBOL_YPOS         (VIDEO_DISPLAY2_YPOS)
79 #define VIDEO_PAUSE_SYMBOL_XSIZE        17
80 #define VIDEO_PAUSE_SYMBOL_YSIZE        13
81 #define VIDEO_TIME_XPOS                 (VIDEO_DISPLAY2_XPOS + 39)
82 #define VIDEO_TIME_YPOS                 (VIDEO_DISPLAY2_YPOS + 14)
83 #define VIDEO_TIME_XSIZE                50
84 #define VIDEO_TIME_YSIZE                16
85
86 /* some default values for tape labels and symbols */
87 #define VIDEO_LABEL_XPOS                5
88 #define VIDEO_LABEL_YPOS                42
89 #define VIDEO_LABEL_XSIZE               40
90 #define VIDEO_LABEL_YSIZE               28
91 #define VIDEO_SYMBOL_XPOS               39
92 #define VIDEO_SYMBOL_YPOS               42
93 #define VIDEO_SYMBOL_XSIZE              56
94 #define VIDEO_SYMBOL_YSIZE              13
95
96 /* values for certain tape labels and symbols */
97 #define VIDEO_FFWD_LABEL_XPOS           VIDEO_LABEL_XPOS
98 #define VIDEO_FFWD_LABEL_YPOS           193
99 #define VIDEO_FFWD_LABEL_XSIZE          VIDEO_LABEL_XSIZE
100 #define VIDEO_FFWD_LABEL_YSIZE          VIDEO_LABEL_YSIZE
101 #define VIDEO_FFWD_SYMBOL_XPOS          VIDEO_SYMBOL_XPOS
102 #define VIDEO_FFWD_SYMBOL_YPOS          193
103 #define VIDEO_FFWD_SYMBOL_XSIZE         27
104 #define VIDEO_FFWD_SYMBOL_YSIZE         VIDEO_SYMBOL_YSIZE
105
106 #define VIDEO_PBEND_LABEL_XPOS          VIDEO_LABEL_XPOS
107 #define VIDEO_PBEND_LABEL_YPOS          221
108 #define VIDEO_PBEND_LABEL_XSIZE         VIDEO_LABEL_XSIZE
109 #define VIDEO_PBEND_LABEL_YSIZE         VIDEO_LABEL_YSIZE
110 #define VIDEO_PBEND_SYMBOL_XPOS         VIDEO_SYMBOL_XPOS
111 #define VIDEO_PBEND_SYMBOL_YPOS         221
112 #define VIDEO_PBEND_SYMBOL_XSIZE        27
113 #define VIDEO_PBEND_SYMBOL_YSIZE        VIDEO_SYMBOL_YSIZE
114
115 #define VIDEO_WARP_LABEL_XPOS           VIDEO_LABEL_XPOS
116 #define VIDEO_WARP_LABEL_YPOS           165
117 #define VIDEO_WARP_LABEL_XSIZE          VIDEO_LABEL_XSIZE
118 #define VIDEO_WARP_LABEL_YSIZE          VIDEO_LABEL_YSIZE
119 #define VIDEO_WARP_SYMBOL_XPOS          VIDEO_SYMBOL_XPOS
120 #define VIDEO_WARP_SYMBOL_YPOS          165
121 #define VIDEO_WARP_SYMBOL_XSIZE         27
122 #define VIDEO_WARP_SYMBOL_YSIZE         VIDEO_SYMBOL_YSIZE
123 #define VIDEO_WARP2_SYMBOL_XPOS         VIDEO_SYMBOL_XPOS
124 #define VIDEO_WARP2_SYMBOL_YPOS         152
125 #define VIDEO_WARP2_SYMBOL_XSIZE        27
126 #define VIDEO_WARP2_SYMBOL_YSIZE        VIDEO_SYMBOL_YSIZE
127
128 #define VIDEO_1STEP_SYMBOL_XPOS         (VIDEO_SYMBOL_XPOS + 18)
129 #define VIDEO_1STEP_SYMBOL_YPOS         139
130 #define VIDEO_1STEP_SYMBOL_XSIZE        (VIDEO_SYMBOL_XSIZE - 18)
131 #define VIDEO_1STEP_SYMBOL_YSIZE        VIDEO_SYMBOL_YSIZE
132
133 #define PG_X3(x)                        (DOOR_GFX_PAGEX3 + (x))
134 #define PG_X4(x)                        (DOOR_GFX_PAGEX4 + (x))
135 #define PG_X5(x)                        (DOOR_GFX_PAGEX5 + (x))
136 #define PG_X6(x)                        (DOOR_GFX_PAGEX6 + (x))
137
138 #define PG_Y1(y)                        (DOOR_GFX_PAGEY1 + (y))
139 #define PG_Y2(y)                        (DOOR_GFX_PAGEY2 + (y))
140
141 #define VIDEO_INFO_OFF                  (VIDEO_STATE_DATE_OFF   |       \
142                                          VIDEO_STATE_TIME_OFF)
143 #define VIDEO_STATE_OFF                 (VIDEO_STATE_PLAY_OFF   |       \
144                                          VIDEO_STATE_REC_OFF    |       \
145                                          VIDEO_STATE_PAUSE_OFF  |       \
146                                          VIDEO_STATE_FFWD_OFF   |       \
147                                          VIDEO_STATE_PBEND_OFF  |       \
148                                          VIDEO_STATE_1STEP_OFF)
149 #define VIDEO_PRESS_OFF                 (VIDEO_PRESS_PLAY_OFF   |       \
150                                          VIDEO_PRESS_REC_OFF    |       \
151                                          VIDEO_PRESS_PAUSE_OFF  |       \
152                                          VIDEO_PRESS_STOP_OFF   |       \
153                                          VIDEO_PRESS_EJECT_OFF)
154 #define VIDEO_ALL_OFF                   (VIDEO_INFO_OFF         |       \
155                                          VIDEO_STATE_OFF        |       \
156                                          VIDEO_PRESS_OFF)
157
158 #define VIDEO_INFO_ON                   (VIDEO_STATE_DATE_ON    |       \
159                                          VIDEO_STATE_TIME_ON)
160 #define VIDEO_STATE_ON                  (VIDEO_STATE_PLAY_ON    |       \
161                                          VIDEO_STATE_REC_ON     |       \
162                                          VIDEO_STATE_PAUSE_ON   |       \
163                                          VIDEO_STATE_FFWD_ON    |       \
164                                          VIDEO_STATE_PBEND_ON   |       \
165                                          VIDEO_STATE_1STEP_ON)
166 #define VIDEO_PRESS_ON                  (VIDEO_PRESS_PLAY_ON    |       \
167                                          VIDEO_PRESS_REC_ON     |       \
168                                          VIDEO_PRESS_PAUSE_ON   |       \
169                                          VIDEO_PRESS_STOP_ON    |       \
170                                          VIDEO_PRESS_EJECT_ON)
171 #define VIDEO_ALL_ON                    (VIDEO_INFO_ON          |       \
172                                          VIDEO_STATE_ON         |       \
173                                          VIDEO_PRESS_ON)
174
175 #define VIDEO_INFO                      (VIDEO_INFO_ON | VIDEO_INFO_OFF)
176 #define VIDEO_STATE                     (VIDEO_STATE_ON | VIDEO_STATE_OFF)
177 #define VIDEO_PRESS                     (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
178 #define VIDEO_ALL                       (VIDEO_ALL_ON | VIDEO_ALL_OFF)
179
180 #define NUM_TAPE_FUNCTIONS              10
181 #define NUM_TAPE_FUNCTION_PARTS         2
182 #define NUM_TAPE_FUNCTION_STATES        2
183
184
185 /* ========================================================================= */
186 /* video display functions                                                   */
187 /* ========================================================================= */
188
189 void DrawVideoDisplay(unsigned long state, unsigned long value)
190 {
191   int i, j, k;
192   static char *monatsname[12] =
193   {
194     "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
195     "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
196   };
197   static struct
198   {
199     int gd_x1, gd_y1;
200     int gd_x2, gd_y2;
201     int x, y;
202     int width, height;
203   }
204   video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
205   {
206     {
207       {
208         PG_X4(VIDEO_PLAY_LABEL_XPOS),   PG_Y2(VIDEO_PLAY_LABEL_YPOS),
209         PG_X3(VIDEO_PLAY_LABEL_XPOS),   PG_Y2(VIDEO_PLAY_LABEL_YPOS),
210         VIDEO_PLAY_LABEL_XPOS,          VIDEO_PLAY_LABEL_YPOS,
211         VIDEO_PLAY_LABEL_XSIZE,         VIDEO_PLAY_LABEL_YSIZE
212       },
213       {
214         PG_X4(VIDEO_PLAY_SYMBOL_XPOS),  PG_Y2(VIDEO_PLAY_SYMBOL_YPOS),
215         PG_X3(VIDEO_PLAY_SYMBOL_XPOS),  PG_Y2(VIDEO_PLAY_SYMBOL_YPOS),
216         VIDEO_PLAY_SYMBOL_XPOS,         VIDEO_PLAY_SYMBOL_YPOS,
217         VIDEO_PLAY_SYMBOL_XSIZE,        VIDEO_PLAY_SYMBOL_YSIZE
218       }
219     },
220     {
221       {
222         PG_X4(VIDEO_REC_LABEL_XPOS),    PG_Y2(VIDEO_REC_LABEL_YPOS),
223         PG_X3(VIDEO_REC_LABEL_XPOS),    PG_Y2(VIDEO_REC_LABEL_YPOS),
224         VIDEO_REC_LABEL_XPOS,           VIDEO_REC_LABEL_YPOS,
225         VIDEO_REC_LABEL_XSIZE,          VIDEO_REC_LABEL_YSIZE
226       },
227       {
228         PG_X4(VIDEO_REC_SYMBOL_XPOS),   PG_Y2(VIDEO_REC_SYMBOL_YPOS),
229         PG_X3(VIDEO_REC_SYMBOL_XPOS),   PG_Y2(VIDEO_REC_SYMBOL_YPOS),
230         VIDEO_REC_SYMBOL_XPOS,          VIDEO_REC_SYMBOL_YPOS,
231         VIDEO_REC_SYMBOL_XSIZE,         VIDEO_REC_SYMBOL_YSIZE
232       }
233     },
234     {
235       {
236         PG_X4(VIDEO_PAUSE_LABEL_XPOS),  PG_Y2(VIDEO_PAUSE_LABEL_YPOS),
237         PG_X3(VIDEO_PAUSE_LABEL_XPOS),  PG_Y2(VIDEO_PAUSE_LABEL_YPOS),
238         VIDEO_PAUSE_LABEL_XPOS,         VIDEO_PAUSE_LABEL_YPOS,
239         VIDEO_PAUSE_LABEL_XSIZE,        VIDEO_PAUSE_LABEL_YSIZE
240       },
241       {
242         PG_X4(VIDEO_PAUSE_SYMBOL_XPOS), PG_Y2(VIDEO_PAUSE_SYMBOL_YPOS),
243         PG_X3(VIDEO_PAUSE_SYMBOL_XPOS), PG_Y2(VIDEO_PAUSE_SYMBOL_YPOS),
244         VIDEO_PAUSE_SYMBOL_XPOS,        VIDEO_PAUSE_SYMBOL_YPOS,
245         VIDEO_PAUSE_SYMBOL_XSIZE,       VIDEO_PAUSE_SYMBOL_YSIZE
246       }
247     },
248     {
249       {
250         PG_X4(VIDEO_DATE_LABEL_XPOS),   PG_Y2(VIDEO_DATE_LABEL_YPOS),
251         PG_X3(VIDEO_DATE_LABEL_XPOS),   PG_Y2(VIDEO_DATE_LABEL_YPOS),
252         VIDEO_DATE_LABEL_XPOS,          VIDEO_DATE_LABEL_YPOS,
253         VIDEO_DATE_LABEL_XSIZE,         VIDEO_DATE_LABEL_YSIZE
254       },
255       {
256         PG_X4(VIDEO_DATE_XPOS),         PG_Y2(VIDEO_DATE_YPOS),
257         PG_X3(VIDEO_DATE_XPOS),         PG_Y2(VIDEO_DATE_YPOS),
258         VIDEO_DATE_XPOS,                VIDEO_DATE_YPOS,
259         VIDEO_DATE_XSIZE,               VIDEO_DATE_YSIZE
260       }
261     },
262     {
263       {
264         -1,                             -1,
265         -1,                             -1,
266         -1,                             -1,
267         -1,                             -1
268       },
269       {
270         PG_X4(VIDEO_TIME_XPOS),         PG_Y2(VIDEO_TIME_YPOS),
271         PG_X3(VIDEO_TIME_XPOS),         PG_Y2(VIDEO_TIME_YPOS),
272         VIDEO_TIME_XPOS,                VIDEO_TIME_YPOS,
273         VIDEO_TIME_XSIZE,               VIDEO_TIME_YSIZE
274       }
275     },
276     {
277       {
278         PG_X6(VIDEO_FFWD_LABEL_XPOS),   PG_Y1(VIDEO_FFWD_LABEL_YPOS),
279         PG_X3(VIDEO_LABEL_XPOS),        PG_Y2(VIDEO_LABEL_YPOS),
280         VIDEO_LABEL_XPOS,               VIDEO_LABEL_YPOS,
281         VIDEO_LABEL_XSIZE,              VIDEO_LABEL_YSIZE
282       },
283       {
284         PG_X6(VIDEO_FFWD_SYMBOL_XPOS),  PG_Y1(VIDEO_FFWD_SYMBOL_YPOS),
285         PG_X3(VIDEO_FFWD_SYMBOL_XPOS),  PG_Y2(VIDEO_SYMBOL_YPOS),
286         VIDEO_SYMBOL_XPOS,              VIDEO_SYMBOL_YPOS,
287         VIDEO_FFWD_SYMBOL_XSIZE,        VIDEO_FFWD_SYMBOL_YSIZE
288       }
289     },
290     {
291       {
292         PG_X6(VIDEO_PBEND_LABEL_XPOS),  PG_Y1(VIDEO_PBEND_LABEL_YPOS),
293         PG_X3(VIDEO_LABEL_XPOS),        PG_Y2(VIDEO_LABEL_YPOS),
294         VIDEO_LABEL_XPOS,               VIDEO_LABEL_YPOS,
295         VIDEO_LABEL_XSIZE,              VIDEO_LABEL_YSIZE
296       },
297       {
298         PG_X6(VIDEO_PBEND_SYMBOL_XPOS), PG_Y1(VIDEO_PBEND_SYMBOL_YPOS),
299         PG_X3(VIDEO_PBEND_SYMBOL_XPOS), PG_Y2(VIDEO_SYMBOL_YPOS),
300         VIDEO_SYMBOL_XPOS,              VIDEO_SYMBOL_YPOS,
301         VIDEO_PBEND_SYMBOL_XSIZE,       VIDEO_PBEND_SYMBOL_YSIZE
302       }
303     },
304     {
305       {
306         PG_X6(VIDEO_WARP_LABEL_XPOS),   PG_Y1(VIDEO_WARP_LABEL_YPOS),
307         PG_X3(VIDEO_LABEL_XPOS),        PG_Y2(VIDEO_LABEL_YPOS),
308         VIDEO_LABEL_XPOS,               VIDEO_LABEL_YPOS,
309         VIDEO_LABEL_XSIZE,              VIDEO_LABEL_YSIZE
310       },
311       {
312         PG_X6(VIDEO_WARP_SYMBOL_XPOS),  PG_Y1(VIDEO_WARP_SYMBOL_YPOS),
313         PG_X3(VIDEO_WARP_SYMBOL_XPOS),  PG_Y2(VIDEO_SYMBOL_YPOS),
314         VIDEO_SYMBOL_XPOS,              VIDEO_SYMBOL_YPOS,
315         VIDEO_WARP_SYMBOL_XSIZE,        VIDEO_WARP_SYMBOL_YSIZE
316       }
317     },
318     {
319       {
320         -1,                             -1,
321         -1,                             -1,
322         -1,                             -1,
323         -1,                             -1
324       },
325       {
326         PG_X6(VIDEO_WARP2_SYMBOL_XPOS), PG_Y1(VIDEO_WARP2_SYMBOL_YPOS),
327         PG_X3(VIDEO_WARP2_SYMBOL_XPOS), PG_Y2(VIDEO_SYMBOL_YPOS),
328         VIDEO_SYMBOL_XPOS,              VIDEO_SYMBOL_YPOS,
329         VIDEO_WARP2_SYMBOL_XSIZE,       VIDEO_WARP2_SYMBOL_YSIZE
330       }
331     },
332     {
333       {
334         -1,                             -1,
335         -1,                             -1,
336         -1,                             -1,
337         -1,                             -1
338       },
339       {
340         PG_X6(VIDEO_1STEP_SYMBOL_XPOS), PG_Y1(VIDEO_1STEP_SYMBOL_YPOS),
341         PG_X3(VIDEO_1STEP_SYMBOL_XPOS), PG_Y2(VIDEO_SYMBOL_YPOS),
342         VIDEO_1STEP_SYMBOL_XPOS,        VIDEO_SYMBOL_YPOS,
343         VIDEO_1STEP_SYMBOL_XSIZE,       VIDEO_1STEP_SYMBOL_YSIZE
344       }
345     },
346   };
347
348   for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++)        /* on or off states */
349   {
350     for (i = 0; i < NUM_TAPE_FUNCTIONS; i++)            /* record, play, ... */
351     {
352       for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++)     /* label or symbol */
353       {
354         if (state & (1 << (i * 2 + k)))
355         {
356           int gd_x, gd_y;
357           int skip_value =
358             (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
359
360           if (k == 1)           /* on */
361           {
362             gd_x = video_pos[i][j].gd_x1;
363             gd_y = video_pos[i][j].gd_y1;
364           }
365           else                  /* off */
366           {
367             gd_x = video_pos[i][j].gd_x2;
368             gd_y = video_pos[i][j].gd_y2;
369           }
370
371           if (video_pos[i][j].x != -1 && value != skip_value)
372             BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
373                        gd_x, gd_y,
374                        video_pos[i][j].width,
375                        video_pos[i][j].height,
376                        VX + video_pos[i][j].x,
377                        VY + video_pos[i][j].y);
378         }
379       }
380     }
381   }
382
383   if (state & VIDEO_STATE_DATE_ON)
384   {
385     int tag = value % 100;
386     int monat = (value/100) % 100;
387     int jahr = (value/10000);
388
389     DrawText(VX + VIDEO_DATE_XPOS, VY + VIDEO_DATE_YPOS,
390              int2str(tag, 2), FONT_TAPE_RECORDER);
391     DrawText(VX + VIDEO_DATE_XPOS + 27, VY + VIDEO_DATE_YPOS,
392              monatsname[monat], FONT_TAPE_RECORDER);
393     DrawText(VX + VIDEO_DATE_XPOS + 64, VY + VIDEO_DATE_YPOS,
394              int2str(jahr, 2), FONT_TAPE_RECORDER);
395   }
396
397   if (state & VIDEO_STATE_TIME_ON)
398   {
399     int min = value / 60;
400     int sec = value % 60;
401
402     DrawText(VX + VIDEO_TIME_XPOS, VY + VIDEO_TIME_YPOS,
403              int2str(min, 2), FONT_TAPE_RECORDER);
404     DrawText(VX + VIDEO_TIME_XPOS + 27, VY + VIDEO_TIME_YPOS,
405              int2str(sec, 2), FONT_TAPE_RECORDER);
406   }
407
408   redraw_mask |= REDRAW_DOOR_2;
409 }
410
411 void DrawCompleteVideoDisplay()
412 {
413   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
414              DOOR_GFX_PAGEX3, DOOR_GFX_PAGEY2,
415              gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
416   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
417              DOOR_GFX_PAGEX4 + VIDEO_CONTROL_XPOS,
418              DOOR_GFX_PAGEY2 + VIDEO_CONTROL_YPOS,
419              VIDEO_CONTROL_XSIZE, VIDEO_CONTROL_YSIZE,
420              gfx.vx + VIDEO_CONTROL_XPOS, gfx.vy + VIDEO_CONTROL_YPOS);
421
422   DrawVideoDisplay(VIDEO_ALL_OFF, 0);
423   if (tape.date && tape.length)
424   {
425     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
426     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
427   }
428
429   BlitBitmap(drawto, bitmap_db_door, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
430              DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
431 }
432
433 void TapeDeactivateDisplayOn()
434 {
435   SetDrawDeactivationMask(REDRAW_FIELD);
436   audio.sound_deactivated = TRUE;
437 }
438
439 void TapeDeactivateDisplayOff(boolean redraw_display)
440 {
441   SetDrawDeactivationMask(REDRAW_NONE);
442   audio.sound_deactivated = FALSE;
443
444   if (redraw_display)
445   {
446     RedrawPlayfield(TRUE, 0,0,0,0);
447     DrawGameDoorValues();
448   }
449 }
450
451
452 /* ========================================================================= */
453 /* tape control functions                                                    */
454 /* ========================================================================= */
455
456 static void TapeSetDate()
457 {
458   time_t epoch_seconds = time(NULL);
459   struct tm *now = localtime(&epoch_seconds);
460
461   tape.date = 10000 * (now->tm_year % 100) + 100 * now->tm_mon + now->tm_mday;
462 }
463
464 void TapeErase()
465 {
466   int i;
467
468   tape.length = 0;
469   tape.counter = 0;
470
471   if (leveldir_current)
472     setString(&tape.level_identifier, leveldir_current->identifier);
473
474   tape.level_nr = level_nr;
475   tape.pos[tape.counter].delay = 0;
476   tape.changed = TRUE;
477
478   tape.random_seed = InitRND(NEW_RANDOMIZE);
479
480   tape.file_version = FILE_VERSION_ACTUAL;
481   tape.game_version = GAME_VERSION_ACTUAL;
482   tape.engine_version = level.game_version;
483
484   TapeSetDate();
485
486   for (i = 0; i < MAX_PLAYERS; i++)
487     tape.player_participates[i] = FALSE;
488
489   tape.centered_player_nr_next = -1;
490   tape.set_centered_player = FALSE;
491 }
492
493 static void TapeRewind()
494 {
495   tape.counter = 0;
496   tape.delay_played = 0;
497   tape.pause_before_death = FALSE;
498   tape.recording = FALSE;
499   tape.playing = FALSE;
500   tape.fast_forward = FALSE;
501   tape.warp_forward = FALSE;
502   tape.deactivate_display = FALSE;
503   tape.auto_play = (global.autoplay_leveldir != NULL);
504   tape.auto_play_level_solved = FALSE;
505   tape.quick_resume = FALSE;
506   tape.single_step = FALSE;
507
508   tape.centered_player_nr_next = -1;
509   tape.set_centered_player = FALSE;
510
511   InitRND(tape.random_seed);
512 }
513
514 static void TapeSetRandomSeed(long random_seed)
515 {
516   tape.random_seed = InitRND(random_seed);
517 }
518
519 void TapeStartRecording(long random_seed)
520 {
521   if (!TAPE_IS_STOPPED(tape))
522     TapeStop();
523
524   TapeErase();
525   TapeRewind();
526   TapeSetRandomSeed(random_seed);
527
528   tape.recording = TRUE;
529
530   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
531   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
532   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
533   MapTapeWarpButton();
534
535   SetDrawDeactivationMask(REDRAW_NONE);
536   audio.sound_deactivated = FALSE;
537 }
538
539 static void TapeStartGameRecording()
540 {
541   TapeStartRecording(NEW_RANDOMIZE);
542
543 #if defined(NETWORK_AVALIABLE)
544   if (options.network)
545     SendToServer_StartPlaying();
546   else
547 #endif
548   {
549     game_status = GAME_MODE_PLAYING;
550     StopAnimation();
551     InitGame();
552   }
553 }
554
555 static void TapeAppendRecording()
556 {
557   if (!tape.playing || !tape.pausing)
558     return;
559
560   tape.pos[tape.counter].delay = tape.delay_played;
561   tape.playing = FALSE;
562   tape.recording = TRUE;
563   tape.changed = TRUE;
564
565   TapeSetDate();
566
567   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
568   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
569 }
570
571 void TapeHaltRecording()
572 {
573   if (!tape.recording)
574     return;
575
576   tape.counter++;
577   tape.pos[tape.counter].delay = 0;
578
579   tape.length = tape.counter;
580   tape.length_seconds = GetTapeLength();
581 }
582
583 void TapeStopRecording()
584 {
585   TapeHaltRecording();
586
587   tape.recording = FALSE;
588   tape.pausing = FALSE;
589
590   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
591   MapTapeEjectButton();
592 }
593
594 void TapeRecordAction(byte action_raw[MAX_PLAYERS])
595 {
596   byte action[MAX_PLAYERS];
597   int i;
598
599   if (!tape.recording)          /* (record action even when tape is paused) */
600     return;
601
602   if (tape.counter >= MAX_TAPE_LEN - 1)
603   {
604     TapeStopRecording();
605     return;
606   }
607
608   for (i = 0; i < MAX_PLAYERS; i++)
609     action[i] = action_raw[i];
610
611   if (tape.set_centered_player)
612   {
613     for (i = 0; i < MAX_PLAYERS; i++)
614       if (tape.centered_player_nr_next == i ||
615           tape.centered_player_nr_next == -1)
616         action[i] |= KEY_SET_FOCUS;
617
618     tape.set_centered_player = FALSE;
619   }
620
621   if (tape.pos[tape.counter].delay > 0)         /* already stored action */
622   {
623     boolean changed_events = FALSE;
624
625     for (i = 0; i < MAX_PLAYERS; i++)
626       if (tape.pos[tape.counter].action[i] != action[i])
627         changed_events = TRUE;
628
629     if (changed_events || tape.pos[tape.counter].delay >= 255)
630     {
631       tape.counter++;
632       tape.pos[tape.counter].delay = 0;
633     }
634     else
635       tape.pos[tape.counter].delay++;
636   }
637
638   if (tape.pos[tape.counter].delay == 0)        /* store new action */
639   {
640     for (i = 0; i < MAX_PLAYERS; i++)
641       tape.pos[tape.counter].action[i] = action[i];
642
643     tape.pos[tape.counter].delay++;
644   }
645 }
646
647 void TapeTogglePause(boolean toggle_manual)
648 {
649   int state = 0;
650
651   if (tape.pause_before_death)
652     state |= VIDEO_STATE_PBEND_OFF;
653   else if (tape.fast_forward)
654     state |= VIDEO_STATE_FFWD_OFF;
655
656   tape.pausing = !tape.pausing;
657   tape.fast_forward = FALSE;
658   tape.pause_before_death = FALSE;
659
660   if (tape.single_step && toggle_manual)
661     tape.single_step = FALSE;
662
663   state |= (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
664   if (tape.playing)
665     state |= VIDEO_STATE_PLAY_ON;
666   else
667     state |= (tape.single_step ? VIDEO_STATE_1STEP_ON : VIDEO_STATE_1STEP_OFF);
668
669   DrawVideoDisplay(state, 0);
670
671   if (tape.warp_forward)
672   {
673     TapeStopWarpForward();
674
675     if (tape.quick_resume)
676     {
677       tape.quick_resume = FALSE;
678
679       TapeAppendRecording();
680     }
681   }
682 }
683
684 void TapeStartPlaying()
685 {
686   if (TAPE_IS_EMPTY(tape))
687     return;
688
689   if (!TAPE_IS_STOPPED(tape))
690     TapeStop();
691
692   TapeRewind();
693
694   tape.playing = TRUE;
695
696   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
697   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
698   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
699   MapTapeWarpButton();
700
701   SetDrawDeactivationMask(REDRAW_NONE);
702   audio.sound_deactivated = FALSE;
703 }
704
705 static void TapeStartGamePlaying()
706 {
707   TapeStartPlaying();
708
709   game_status = GAME_MODE_PLAYING;
710   StopAnimation();
711   InitGame();
712 }
713
714 void TapeStopPlaying()
715 {
716   tape.playing = FALSE;
717   tape.pausing = FALSE;
718
719   if (tape.warp_forward)
720     TapeStopWarpForward();
721
722   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
723   MapTapeEjectButton();
724 }
725
726 byte *TapePlayAction()
727 {
728   static byte action[MAX_PLAYERS];
729   int i;
730
731   if (!tape.playing || tape.pausing)
732     return NULL;
733
734   if (tape.pause_before_death)  /* stop 10 seconds before player gets killed */
735   {
736     if (!(FrameCounter % 20))
737     {
738       if ((FrameCounter / 20) % 2)
739         DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
740       else
741         DrawVideoDisplay(VIDEO_STATE_PBEND_OFF, VIDEO_DISPLAY_LABEL_ONLY);
742     }
743
744     if (tape.warp_forward)
745     {
746       if (tape.deactivate_display)
747         DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
748       else
749         DrawVideoDisplay(VIDEO_STATE_WARP2_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
750     }
751
752     if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
753     {
754       TapeTogglePause(TAPE_TOGGLE_MANUAL);
755
756       return NULL;
757     }
758   }
759   else if (tape.fast_forward)
760   {
761     if ((FrameCounter / 20) % 2)
762       DrawVideoDisplay(VIDEO_STATE_FFWD_ON, VIDEO_DISPLAY_LABEL_ONLY);
763     else
764       DrawVideoDisplay(VIDEO_STATE_FFWD_OFF, VIDEO_DISPLAY_LABEL_ONLY);
765
766     if (tape.warp_forward)
767     {
768       if (tape.deactivate_display)
769         DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
770       else
771         DrawVideoDisplay(VIDEO_STATE_WARP2_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
772     }
773   }
774
775 #if 0
776   /* !!! this makes things much slower !!! */
777   else if (tape.warp_forward)
778   {
779     if ((FrameCounter / 20) % 2)
780       DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_LABEL_ONLY);
781     else
782       DrawVideoDisplay(VIDEO_STATE_WARP_OFF, VIDEO_DISPLAY_LABEL_ONLY);
783
784     DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
785   }
786 #endif
787
788   if (tape.counter >= tape.length)      /* end of tape reached */
789   {
790     if (tape.warp_forward && !tape.auto_play)
791       TapeTogglePause(TAPE_TOGGLE_MANUAL);
792     else
793       TapeStop();
794
795     return NULL;
796   }
797
798   for (i = 0; i < MAX_PLAYERS; i++)
799     action[i] = tape.pos[tape.counter].action[i];
800
801   tape.set_centered_player = FALSE;
802   tape.centered_player_nr_next = -999;
803
804   for (i = 0; i < MAX_PLAYERS; i++)
805   {
806     if (action[i] & KEY_SET_FOCUS)
807     {
808       tape.set_centered_player = TRUE;
809       tape.centered_player_nr_next =
810         (tape.centered_player_nr_next == -999 ? i : -1);
811     }
812
813     action[i] &= ~KEY_SET_FOCUS;
814   }
815
816   tape.delay_played++;
817   if (tape.delay_played >= tape.pos[tape.counter].delay)
818   {
819     tape.counter++;
820     tape.delay_played = 0;
821   }
822
823   return action;
824 }
825
826 void TapeStop()
827 {
828   TapeStopRecording();
829   TapeStopPlaying();
830
831   DrawVideoDisplay(VIDEO_STATE_OFF, 0);
832
833   if (tape.date && tape.length)
834   {
835     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
836     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
837   }
838
839 #if 0
840   if (tape.auto_play)
841     AutoPlayTape();     /* continue automatically playing next tape */
842 #endif
843 }
844
845 unsigned int GetTapeLength()
846 {
847   unsigned int tape_length = 0;
848   int i;
849
850   if (TAPE_IS_EMPTY(tape))
851     return(0);
852
853   for (i = 0; i < tape.length; i++)
854     tape_length += tape.pos[i].delay;
855
856   return(tape_length * GAME_FRAME_DELAY / 1000);
857 }
858
859 static void TapeStartWarpForward()
860 {
861   tape.warp_forward = TRUE;
862
863   if (!tape.fast_forward && !tape.pause_before_death)
864   {
865     tape.pausing = FALSE;
866     tape.deactivate_display = TRUE;
867
868     TapeDeactivateDisplayOn();
869   }
870
871   if (tape.fast_forward || tape.pause_before_death)
872     DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
873   else
874     DrawVideoDisplay(VIDEO_STATE_WARP_ON, 0);
875 }
876
877 static void TapeStopWarpForward()
878 {
879   int state = (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
880
881   tape.warp_forward = FALSE;
882   tape.deactivate_display = FALSE;
883
884   TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
885
886   if (tape.pause_before_death)
887     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_PBEND_ON;
888   else if (tape.fast_forward)
889     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_FFWD_ON;
890   else
891     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_PLAY_ON;
892
893   DrawVideoDisplay(state, 0);
894 }
895
896 static void TapeSingleStep()
897 {
898   if (options.network)
899     return;
900
901   if (!tape.pausing)
902     TapeTogglePause(TAPE_TOGGLE_MANUAL);
903
904   tape.single_step = !tape.single_step;
905
906   DrawVideoDisplay((tape.single_step ? VIDEO_STATE_1STEP_ON :
907                     VIDEO_STATE_1STEP_OFF), 0);
908 }
909
910 void TapeQuickSave()
911 {
912   if (game_status == GAME_MODE_PLAYING)
913   {
914     if (tape.recording)
915       TapeHaltRecording();      /* prepare tape for saving on-the-fly */
916
917     if (TAPE_IS_EMPTY(tape))
918       Request("No tape that can be saved !", REQ_CONFIRM);
919     else
920       SaveTape(tape.level_nr);
921   }
922   else if (game_status == GAME_MODE_MAIN)
923     Request("No game that can be saved !", REQ_CONFIRM);
924 }
925
926 void TapeQuickLoad()
927 {
928   char *filename = getTapeFilename(level_nr);
929
930   if (!fileExists(filename))
931   {
932     Request("No tape for this level !", REQ_CONFIRM);
933
934     return;
935   }
936
937   if (tape.recording && !Request("Stop recording and load tape ?",
938                                  REQ_ASK | REQ_STAY_CLOSED))
939   {
940     OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
941
942     return;
943   }
944
945   if (game_status == GAME_MODE_PLAYING || game_status == GAME_MODE_MAIN)
946   {
947     TapeStop();
948     TapeErase();
949
950     LoadTape(level_nr);
951     if (!TAPE_IS_EMPTY(tape))
952     {
953       TapeStartGamePlaying();
954       TapeStartWarpForward();
955
956       tape.quick_resume = TRUE;
957     }
958     else        /* this should not happen (basically checked above) */
959     {
960       int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
961
962       Request("No tape for this level !", REQ_CONFIRM | reopen_door);
963     }
964   }
965 }
966
967 void InsertSolutionTape()
968 {
969   if (!TAPE_IS_EMPTY(tape))
970     return;
971
972   LoadSolutionTape(level_nr);
973
974   if (TAPE_IS_EMPTY(tape))
975     Request("No solution tape for this level !", REQ_CONFIRM);
976
977   DrawCompleteVideoDisplay();
978 }
979
980
981 /* ------------------------------------------------------------------------- *
982  * tape autoplay functions
983  * ------------------------------------------------------------------------- */
984
985 void AutoPlayTape()
986 {
987   static LevelDirTree *autoplay_leveldir = NULL;
988   static boolean autoplay_initialized = FALSE;
989   static int autoplay_level_nr = -1;
990   static int num_levels_played = 0;
991   static int num_levels_solved = 0;
992   static int num_tape_missing = 0;
993   static boolean level_failed[MAX_TAPES_PER_SET];
994   static boolean tape_missing[MAX_TAPES_PER_SET];
995   int i;
996
997   if (autoplay_initialized)
998   {
999     /* just finished auto-playing tape */
1000     printf("%s.\n", tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
1001
1002     num_levels_played++;
1003
1004     if (tape.auto_play_level_solved)
1005       num_levels_solved++;
1006     else if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1007       level_failed[level_nr] = TRUE;
1008   }
1009   else
1010   {
1011     DrawCompleteVideoDisplay();
1012     audio.sound_enabled = FALSE;
1013
1014     autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1015                                                   global.autoplay_leveldir);
1016
1017     if (autoplay_leveldir == NULL)
1018       Error(ERR_EXIT, "no such level identifier: '%s'",
1019             global.autoplay_leveldir);
1020
1021     leveldir_current = autoplay_leveldir;
1022
1023     if (autoplay_leveldir->first_level < 0)
1024       autoplay_leveldir->first_level = 0;
1025     if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
1026       autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1027
1028     autoplay_level_nr = autoplay_leveldir->first_level;
1029
1030     printf_line("=", 79);
1031     printf("Automatically playing level tapes\n");
1032     printf_line("-", 79);
1033     printf("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
1034     printf("Level series name:       '%s'\n", autoplay_leveldir->name);
1035     printf("Level series author:     '%s'\n", autoplay_leveldir->author);
1036     printf("Number of levels:        %d\n",   autoplay_leveldir->levels);
1037     printf_line("=", 79);
1038     printf("\n");
1039
1040     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1041     {
1042       level_failed[i] = FALSE;
1043       tape_missing[i] = FALSE;
1044     }
1045
1046     autoplay_initialized = TRUE;
1047   }
1048
1049   while (autoplay_level_nr <= autoplay_leveldir->last_level)
1050   {
1051     level_nr = autoplay_level_nr++;
1052
1053     if (!global.autoplay_all && !global.autoplay_level[level_nr])
1054       continue;
1055
1056     TapeErase();
1057
1058     printf("Level %03d: ", level_nr);
1059
1060     LoadLevel(level_nr);
1061     if (level.no_valid_file)
1062     {
1063       printf("(no level)\n");
1064       continue;
1065     }
1066
1067 #if 0
1068     /* ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY */
1069     printf("(only testing level)\n");
1070     continue;
1071 #endif
1072
1073     LoadSolutionTape(level_nr);
1074
1075     if (tape.no_valid_file)
1076     {
1077       num_tape_missing++;
1078       if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1079         tape_missing[level_nr] = TRUE;
1080
1081       printf("(no tape)\n");
1082       continue;
1083     }
1084
1085     printf("playing tape ... ");
1086
1087     TapeStartGamePlaying();
1088     TapeStartWarpForward();
1089
1090     return;
1091   }
1092
1093   printf("\n");
1094   printf_line("=", 79);
1095   printf("Number of levels played: %d\n", num_levels_played);
1096   printf("Number of levels solved: %d (%d%%)\n", num_levels_solved,
1097          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1098   printf_line("-", 79);
1099   printf("Summary (for automatic parsing by scripts):\n");
1100   printf("LEVELDIR '%s', SOLVED %d/%d (%d%%)",
1101          autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
1102          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1103
1104   if (num_levels_played != num_levels_solved)
1105   {
1106     printf(", FAILED:");
1107     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1108       if (level_failed[i])
1109         printf(" %03d", i);
1110   }
1111
1112 #if 0
1113   if (num_tape_missing > 0)
1114   {
1115     printf(", NO TAPE:");
1116     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1117       if (tape_missing[i])
1118         printf(" %03d", i);
1119   }
1120 #endif
1121
1122   printf("\n");
1123   printf_line("=", 79);
1124
1125   CloseAllAndExit(0);
1126 }
1127
1128
1129 /* ---------- new tape button stuff ---------------------------------------- */
1130
1131 /* graphic position values for tape buttons */
1132 #define TAPE_BUTTON_XSIZE       18
1133 #define TAPE_BUTTON_YSIZE       18
1134 #define TAPE_BUTTON_XPOS        5
1135 #define TAPE_BUTTON_YPOS        77
1136
1137 #define TAPE_BUTTON_EJECT_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
1138 #define TAPE_BUTTON_EXTRA_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
1139 #define TAPE_BUTTON_STOP_XPOS   (TAPE_BUTTON_XPOS + 1 * TAPE_BUTTON_XSIZE)
1140 #define TAPE_BUTTON_PAUSE_XPOS  (TAPE_BUTTON_XPOS + 2 * TAPE_BUTTON_XSIZE)
1141 #define TAPE_BUTTON_RECORD_XPOS (TAPE_BUTTON_XPOS + 3 * TAPE_BUTTON_XSIZE)
1142 #define TAPE_BUTTON_PLAY_XPOS   (TAPE_BUTTON_XPOS + 4 * TAPE_BUTTON_XSIZE)
1143
1144 static struct
1145 {
1146   int x, y;
1147   int gadget_id;
1148   char *infotext;
1149 } tapebutton_info[NUM_TAPE_BUTTONS] =
1150 {
1151   {
1152     TAPE_BUTTON_EJECT_XPOS,     TAPE_BUTTON_YPOS,
1153     TAPE_CTRL_ID_EJECT,
1154     "eject tape"
1155   },
1156   {
1157     TAPE_BUTTON_EXTRA_XPOS,     TAPE_BUTTON_YPOS,
1158     TAPE_CTRL_ID_EXTRA,
1159     "extra functions"
1160   },
1161   {
1162     TAPE_BUTTON_STOP_XPOS,      TAPE_BUTTON_YPOS,
1163     TAPE_CTRL_ID_STOP,
1164     "stop tape"
1165   },
1166   {
1167     TAPE_BUTTON_PAUSE_XPOS,     TAPE_BUTTON_YPOS,
1168     TAPE_CTRL_ID_PAUSE,
1169     "pause tape"
1170   },
1171   {
1172     TAPE_BUTTON_RECORD_XPOS,    TAPE_BUTTON_YPOS,
1173     TAPE_CTRL_ID_RECORD,
1174     "record tape"
1175   },
1176   {
1177     TAPE_BUTTON_PLAY_XPOS,      TAPE_BUTTON_YPOS,
1178     TAPE_CTRL_ID_PLAY,
1179     "play tape"
1180   }
1181 };
1182
1183 void CreateTapeButtons()
1184 {
1185   int i;
1186
1187   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1188   {
1189     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
1190     struct GadgetInfo *gi;
1191     int gd_xoffset, gd_yoffset;
1192     int gd_x1, gd_x2, gd_y;
1193     int id = i;
1194
1195     gd_xoffset = tapebutton_info[i].x;
1196     gd_yoffset = tapebutton_info[i].y;
1197     gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
1198     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
1199     gd_y  = DOOR_GFX_PAGEY2 + gd_yoffset;
1200
1201     if (i == TAPE_CTRL_ID_EXTRA)
1202     {
1203       gd_x1 = DOOR_GFX_PAGEX6 + gd_xoffset;
1204       gd_x2 = DOOR_GFX_PAGEX5 + gd_xoffset;
1205     }
1206
1207     gi = CreateGadget(GDI_CUSTOM_ID, id,
1208                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
1209                       GDI_X, VX + gd_xoffset,
1210                       GDI_Y, VY + gd_yoffset,
1211                       GDI_WIDTH, TAPE_BUTTON_XSIZE,
1212                       GDI_HEIGHT, TAPE_BUTTON_YSIZE,
1213                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
1214                       GDI_STATE, GD_BUTTON_UNPRESSED,
1215                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
1216                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
1217                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
1218                       GDI_CALLBACK_ACTION, HandleTapeButtons,
1219                       GDI_END);
1220
1221     if (gi == NULL)
1222       Error(ERR_EXIT, "cannot create gadget");
1223
1224     tape_gadget[id] = gi;
1225   }
1226 }
1227
1228 void FreeTapeButtons()
1229 {
1230   int i;
1231
1232   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1233     FreeGadget(tape_gadget[i]);
1234 }
1235
1236 void MapTapeEjectButton()
1237 {
1238   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1239   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1240 }
1241
1242 void MapTapeWarpButton()
1243 {
1244   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1245   MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1246 }
1247
1248 void MapTapeButtons()
1249 {
1250   int i;
1251
1252   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1253     if (i != TAPE_CTRL_ID_EXTRA)
1254       MapGadget(tape_gadget[i]);
1255
1256   if (tape.recording || tape.playing)
1257     MapTapeWarpButton();
1258 }
1259
1260 void UnmapTapeButtons()
1261 {
1262   int i;
1263
1264   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1265     UnmapGadget(tape_gadget[i]);
1266 }
1267
1268 static void HandleTapeButtons(struct GadgetInfo *gi)
1269 {
1270   int id = gi->custom_id;
1271
1272   if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
1273     return;
1274
1275   switch (id)
1276   {
1277     case TAPE_CTRL_ID_EJECT:
1278       TapeStop();
1279       if (TAPE_IS_EMPTY(tape))
1280       {
1281         LoadTape(level_nr);
1282         if (TAPE_IS_EMPTY(tape))
1283           Request("No tape for this level !", REQ_CONFIRM);
1284       }
1285       else
1286       {
1287         if (tape.changed)
1288           SaveTape(tape.level_nr);
1289         TapeErase();
1290       }
1291       DrawCompleteVideoDisplay();
1292       break;
1293
1294     case TAPE_CTRL_ID_EXTRA:
1295       if (tape.playing)
1296       {
1297         if (!tape.warp_forward)                 /* PLAY -> WARP FORWARD PLAY */
1298         {
1299           TapeStartWarpForward();
1300         }
1301         else                                    /* WARP FORWARD PLAY -> PLAY */
1302         {
1303           TapeStopWarpForward();
1304         }
1305       }
1306       else if (tape.recording)
1307         TapeSingleStep();
1308       break;
1309
1310     case TAPE_CTRL_ID_STOP:
1311       TapeStop();
1312       break;
1313
1314     case TAPE_CTRL_ID_PAUSE:
1315       TapeTogglePause(TAPE_TOGGLE_MANUAL);
1316       break;
1317
1318     case TAPE_CTRL_ID_RECORD:
1319       if (TAPE_IS_STOPPED(tape))
1320         TapeStartGameRecording();
1321       else if (tape.pausing)
1322       {
1323         if (tape.playing)                       /* PLAY -> PAUSE -> RECORD */
1324           TapeAppendRecording();
1325         else
1326           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1327       }
1328       break;
1329
1330     case TAPE_CTRL_ID_PLAY:
1331       if (TAPE_IS_EMPTY(tape))
1332         break;
1333
1334       if (TAPE_IS_STOPPED(tape))
1335       {
1336         TapeStartGamePlaying();
1337       }
1338       else if (tape.playing)
1339       {
1340         if (tape.pausing)                       /* PAUSE -> PLAY */
1341         {
1342           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1343         }
1344         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
1345         {
1346           tape.fast_forward = TRUE;
1347           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
1348         }
1349         else if (!tape.pause_before_death)      /* FFWD PLAY -> AUTO PAUSE */
1350         {
1351           tape.pause_before_death = TRUE;
1352           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_ON, 0);
1353         }
1354         else                                    /* AUTO PAUSE -> NORMAL PLAY */
1355         {
1356           if (tape.warp_forward)
1357             TapeStopWarpForward();
1358
1359           tape.fast_forward = FALSE;
1360           tape.pause_before_death = FALSE;
1361
1362           DrawVideoDisplay(VIDEO_STATE_PBEND_OFF | VIDEO_STATE_PLAY_ON, 0);
1363         }
1364       }
1365       break;
1366
1367     default:
1368       break;
1369   }
1370 }