rnd-20060102-2-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
490 static void TapeRewind()
491 {
492   tape.counter = 0;
493   tape.delay_played = 0;
494   tape.pause_before_death = FALSE;
495   tape.recording = FALSE;
496   tape.playing = FALSE;
497   tape.fast_forward = FALSE;
498   tape.warp_forward = FALSE;
499   tape.deactivate_display = FALSE;
500   tape.auto_play = (global.autoplay_leveldir != NULL);
501   tape.auto_play_level_solved = FALSE;
502   tape.quick_resume = FALSE;
503   tape.single_step = FALSE;
504
505   InitRND(tape.random_seed);
506 }
507
508 static void TapeSetRandomSeed(long random_seed)
509 {
510   tape.random_seed = InitRND(random_seed);
511 }
512
513 void TapeStartRecording(long random_seed)
514 {
515   if (!TAPE_IS_STOPPED(tape))
516     TapeStop();
517
518   TapeErase();
519   TapeRewind();
520   TapeSetRandomSeed(random_seed);
521
522   tape.recording = TRUE;
523
524   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
525   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
526   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
527   MapTapeWarpButton();
528
529   SetDrawDeactivationMask(REDRAW_NONE);
530   audio.sound_deactivated = FALSE;
531 }
532
533 static void TapeStartGameRecording()
534 {
535   TapeStartRecording(NEW_RANDOMIZE);
536
537 #if defined(NETWORK_AVALIABLE)
538   if (options.network)
539     SendToServer_StartPlaying();
540   else
541 #endif
542   {
543     game_status = GAME_MODE_PLAYING;
544     StopAnimation();
545     InitGame();
546   }
547 }
548
549 static void TapeAppendRecording()
550 {
551   if (!tape.playing || !tape.pausing)
552     return;
553
554   tape.pos[tape.counter].delay = tape.delay_played;
555   tape.playing = FALSE;
556   tape.recording = TRUE;
557   tape.changed = TRUE;
558
559   TapeSetDate();
560
561   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
562   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
563 }
564
565 void TapeHaltRecording()
566 {
567   if (!tape.recording)
568     return;
569
570   tape.counter++;
571   tape.pos[tape.counter].delay = 0;
572
573   tape.length = tape.counter;
574   tape.length_seconds = GetTapeLength();
575 }
576
577 void TapeStopRecording()
578 {
579   TapeHaltRecording();
580
581   tape.recording = FALSE;
582   tape.pausing = FALSE;
583
584   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
585   MapTapeEjectButton();
586 }
587
588 void TapeRecordAction(byte action[MAX_PLAYERS])
589 {
590   int i;
591
592   if (!tape.recording)          /* (record action even when tape is paused) */
593     return;
594
595   if (tape.counter >= MAX_TAPE_LEN - 1)
596   {
597     TapeStopRecording();
598     return;
599   }
600
601   if (tape.pos[tape.counter].delay > 0)         /* already stored action */
602   {
603     boolean changed_events = FALSE;
604
605     for (i = 0; i < MAX_PLAYERS; i++)
606       if (tape.pos[tape.counter].action[i] != action[i])
607         changed_events = TRUE;
608
609     if (changed_events || tape.pos[tape.counter].delay >= 255)
610     {
611       tape.counter++;
612       tape.pos[tape.counter].delay = 0;
613     }
614     else
615       tape.pos[tape.counter].delay++;
616   }
617
618   if (tape.pos[tape.counter].delay == 0)        /* store new action */
619   {
620     for (i = 0; i < MAX_PLAYERS; i++)
621       tape.pos[tape.counter].action[i] = action[i];
622
623     tape.pos[tape.counter].delay++;
624   }
625 }
626
627 void TapeTogglePause(boolean toggle_manual)
628 {
629   int state = 0;
630
631   if (tape.pause_before_death)
632     state |= VIDEO_STATE_PBEND_OFF;
633   else if (tape.fast_forward)
634     state |= VIDEO_STATE_FFWD_OFF;
635
636   tape.pausing = !tape.pausing;
637   tape.fast_forward = FALSE;
638   tape.pause_before_death = FALSE;
639
640   if (tape.single_step && toggle_manual)
641     tape.single_step = FALSE;
642
643   state |= (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
644   if (tape.playing)
645     state |= VIDEO_STATE_PLAY_ON;
646   else
647     state |= (tape.single_step ? VIDEO_STATE_1STEP_ON : VIDEO_STATE_1STEP_OFF);
648
649   DrawVideoDisplay(state, 0);
650
651   if (tape.warp_forward)
652   {
653     TapeStopWarpForward();
654
655     if (tape.quick_resume)
656     {
657       tape.quick_resume = FALSE;
658
659       TapeAppendRecording();
660     }
661   }
662 }
663
664 void TapeStartPlaying()
665 {
666   if (TAPE_IS_EMPTY(tape))
667     return;
668
669   if (!TAPE_IS_STOPPED(tape))
670     TapeStop();
671
672   TapeRewind();
673
674   tape.playing = TRUE;
675
676   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
677   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
678   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
679   MapTapeWarpButton();
680
681   SetDrawDeactivationMask(REDRAW_NONE);
682   audio.sound_deactivated = FALSE;
683 }
684
685 static void TapeStartGamePlaying()
686 {
687   TapeStartPlaying();
688
689   game_status = GAME_MODE_PLAYING;
690   StopAnimation();
691   InitGame();
692 }
693
694 void TapeStopPlaying()
695 {
696   tape.playing = FALSE;
697   tape.pausing = FALSE;
698
699   if (tape.warp_forward)
700     TapeStopWarpForward();
701
702   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
703   MapTapeEjectButton();
704 }
705
706 byte *TapePlayAction()
707 {
708   static byte action[MAX_PLAYERS];
709   int i;
710
711   if (!tape.playing || tape.pausing)
712     return NULL;
713
714   if (tape.pause_before_death)  /* stop 10 seconds before player gets killed */
715   {
716     if (!(FrameCounter % 20))
717     {
718       if ((FrameCounter / 20) % 2)
719         DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
720       else
721         DrawVideoDisplay(VIDEO_STATE_PBEND_OFF, VIDEO_DISPLAY_LABEL_ONLY);
722     }
723
724     if (tape.warp_forward)
725     {
726       if (tape.deactivate_display)
727         DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
728       else
729         DrawVideoDisplay(VIDEO_STATE_WARP2_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
730     }
731
732     if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
733     {
734       TapeTogglePause(TAPE_TOGGLE_MANUAL);
735
736       return NULL;
737     }
738   }
739   else if (tape.fast_forward)
740   {
741     if ((FrameCounter / 20) % 2)
742       DrawVideoDisplay(VIDEO_STATE_FFWD_ON, VIDEO_DISPLAY_LABEL_ONLY);
743     else
744       DrawVideoDisplay(VIDEO_STATE_FFWD_OFF, VIDEO_DISPLAY_LABEL_ONLY);
745
746     if (tape.warp_forward)
747     {
748       if (tape.deactivate_display)
749         DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
750       else
751         DrawVideoDisplay(VIDEO_STATE_WARP2_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
752     }
753   }
754
755 #if 0
756   /* !!! this makes things much slower !!! */
757   else if (tape.warp_forward)
758   {
759     if ((FrameCounter / 20) % 2)
760       DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_LABEL_ONLY);
761     else
762       DrawVideoDisplay(VIDEO_STATE_WARP_OFF, VIDEO_DISPLAY_LABEL_ONLY);
763
764     DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
765   }
766 #endif
767
768   if (tape.counter >= tape.length)      /* end of tape reached */
769   {
770     if (tape.warp_forward && !tape.auto_play)
771       TapeTogglePause(TAPE_TOGGLE_MANUAL);
772     else
773       TapeStop();
774
775     return NULL;
776   }
777
778   for (i = 0; i < MAX_PLAYERS; i++)
779     action[i] = tape.pos[tape.counter].action[i];
780
781   tape.delay_played++;
782   if (tape.delay_played >= tape.pos[tape.counter].delay)
783   {
784     tape.counter++;
785     tape.delay_played = 0;
786   }
787
788   return action;
789 }
790
791 void TapeStop()
792 {
793   TapeStopRecording();
794   TapeStopPlaying();
795
796   DrawVideoDisplay(VIDEO_STATE_OFF, 0);
797
798   if (tape.date && tape.length)
799   {
800     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
801     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
802   }
803
804 #if 0
805   if (tape.auto_play)
806     AutoPlayTape();     /* continue automatically playing next tape */
807 #endif
808 }
809
810 unsigned int GetTapeLength()
811 {
812   unsigned int tape_length = 0;
813   int i;
814
815   if (TAPE_IS_EMPTY(tape))
816     return(0);
817
818   for (i = 0; i < tape.length; i++)
819     tape_length += tape.pos[i].delay;
820
821   return(tape_length * GAME_FRAME_DELAY / 1000);
822 }
823
824 static void TapeStartWarpForward()
825 {
826   tape.warp_forward = TRUE;
827
828   if (!tape.fast_forward && !tape.pause_before_death)
829   {
830     tape.pausing = FALSE;
831     tape.deactivate_display = TRUE;
832
833     TapeDeactivateDisplayOn();
834   }
835
836   if (tape.fast_forward || tape.pause_before_death)
837     DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
838   else
839     DrawVideoDisplay(VIDEO_STATE_WARP_ON, 0);
840 }
841
842 static void TapeStopWarpForward()
843 {
844   int state = (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
845
846   tape.warp_forward = FALSE;
847   tape.deactivate_display = FALSE;
848
849   TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
850
851   if (tape.pause_before_death)
852     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_PBEND_ON;
853   else if (tape.fast_forward)
854     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_FFWD_ON;
855   else
856     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_PLAY_ON;
857
858   DrawVideoDisplay(state, 0);
859 }
860
861 static void TapeSingleStep()
862 {
863   if (options.network)
864     return;
865
866   if (!tape.pausing)
867     TapeTogglePause(TAPE_TOGGLE_MANUAL);
868
869   tape.single_step = !tape.single_step;
870
871   DrawVideoDisplay((tape.single_step ? VIDEO_STATE_1STEP_ON :
872                     VIDEO_STATE_1STEP_OFF), 0);
873 }
874
875 void TapeQuickSave()
876 {
877   if (game_status == GAME_MODE_PLAYING)
878   {
879     if (tape.recording)
880       TapeHaltRecording();      /* prepare tape for saving on-the-fly */
881
882     if (TAPE_IS_EMPTY(tape))
883       Request("No tape that can be saved !", REQ_CONFIRM);
884     else
885       SaveTape(tape.level_nr);
886   }
887   else if (game_status == GAME_MODE_MAIN)
888     Request("No game that can be saved !", REQ_CONFIRM);
889 }
890
891 void TapeQuickLoad()
892 {
893   char *filename = getTapeFilename(level_nr);
894
895   if (!fileExists(filename))
896   {
897     Request("No tape for this level !", REQ_CONFIRM);
898
899     return;
900   }
901
902   if (tape.recording && !Request("Stop recording and load tape ?",
903                                  REQ_ASK | REQ_STAY_CLOSED))
904   {
905     OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
906
907     return;
908   }
909
910   if (game_status == GAME_MODE_PLAYING || game_status == GAME_MODE_MAIN)
911   {
912     TapeStop();
913     TapeErase();
914
915     LoadTape(level_nr);
916     if (!TAPE_IS_EMPTY(tape))
917     {
918       TapeStartGamePlaying();
919       TapeStartWarpForward();
920
921       tape.quick_resume = TRUE;
922     }
923     else        /* this should not happen (basically checked above) */
924     {
925       int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
926
927       Request("No tape for this level !", REQ_CONFIRM | reopen_door);
928     }
929   }
930 }
931
932 void InsertSolutionTape()
933 {
934   if (!TAPE_IS_EMPTY(tape))
935     return;
936
937   LoadSolutionTape(level_nr);
938
939   if (TAPE_IS_EMPTY(tape))
940     Request("No solution tape for this level !", REQ_CONFIRM);
941
942   DrawCompleteVideoDisplay();
943 }
944
945
946 /* ------------------------------------------------------------------------- *
947  * tape autoplay functions
948  * ------------------------------------------------------------------------- */
949
950 void AutoPlayTape()
951 {
952   static LevelDirTree *autoplay_leveldir = NULL;
953   static boolean autoplay_initialized = FALSE;
954   static int autoplay_level_nr = -1;
955   static int num_levels_played = 0;
956   static int num_levels_solved = 0;
957   static int num_tape_missing = 0;
958   static boolean level_failed[MAX_TAPES_PER_SET];
959   static boolean tape_missing[MAX_TAPES_PER_SET];
960   int i;
961
962   if (autoplay_initialized)
963   {
964     /* just finished auto-playing tape */
965     printf("%s.\n", tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
966
967     num_levels_played++;
968
969     if (tape.auto_play_level_solved)
970       num_levels_solved++;
971     else if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
972       level_failed[level_nr] = TRUE;
973   }
974   else
975   {
976     DrawCompleteVideoDisplay();
977     audio.sound_enabled = FALSE;
978
979     autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
980                                                   global.autoplay_leveldir);
981
982     if (autoplay_leveldir == NULL)
983       Error(ERR_EXIT, "no such level identifier: '%s'",
984             global.autoplay_leveldir);
985
986     leveldir_current = autoplay_leveldir;
987
988     if (autoplay_leveldir->first_level < 0)
989       autoplay_leveldir->first_level = 0;
990     if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
991       autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
992
993     autoplay_level_nr = autoplay_leveldir->first_level;
994
995     printf_line("=", 79);
996     printf("Automatically playing level tapes\n");
997     printf_line("-", 79);
998     printf("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
999     printf("Level series name:       '%s'\n", autoplay_leveldir->name);
1000     printf("Level series author:     '%s'\n", autoplay_leveldir->author);
1001     printf("Number of levels:        %d\n",   autoplay_leveldir->levels);
1002     printf_line("=", 79);
1003     printf("\n");
1004
1005     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1006     {
1007       level_failed[i] = FALSE;
1008       tape_missing[i] = FALSE;
1009     }
1010
1011     autoplay_initialized = TRUE;
1012   }
1013
1014   while (autoplay_level_nr <= autoplay_leveldir->last_level)
1015   {
1016     level_nr = autoplay_level_nr++;
1017
1018     if (!global.autoplay_all && !global.autoplay_level[level_nr])
1019       continue;
1020
1021     TapeErase();
1022
1023     printf("Level %03d: ", level_nr);
1024
1025     LoadLevel(level_nr);
1026     if (level.no_valid_file)
1027     {
1028       printf("(no level)\n");
1029       continue;
1030     }
1031
1032 #if 0
1033     /* ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY */
1034     printf("(only testing level)\n");
1035     continue;
1036 #endif
1037
1038     LoadSolutionTape(level_nr);
1039
1040     if (tape.no_valid_file)
1041     {
1042       num_tape_missing++;
1043       if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1044         tape_missing[level_nr] = TRUE;
1045
1046       printf("(no tape)\n");
1047       continue;
1048     }
1049
1050     printf("playing tape ... ");
1051
1052     TapeStartGamePlaying();
1053     TapeStartWarpForward();
1054
1055     return;
1056   }
1057
1058   printf("\n");
1059   printf_line("=", 79);
1060   printf("Number of levels played: %d\n", num_levels_played);
1061   printf("Number of levels solved: %d (%d%%)\n", num_levels_solved,
1062          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1063   printf_line("-", 79);
1064   printf("Summary (for automatic parsing by scripts):\n");
1065   printf("LEVELDIR '%s', SOLVED %d/%d (%d%%)",
1066          autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
1067          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1068
1069   if (num_levels_played != num_levels_solved)
1070   {
1071     printf(", FAILED:");
1072     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1073       if (level_failed[i])
1074         printf(" %03d", i);
1075   }
1076
1077 #if 0
1078   if (num_tape_missing > 0)
1079   {
1080     printf(", NO TAPE:");
1081     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1082       if (tape_missing[i])
1083         printf(" %03d", i);
1084   }
1085 #endif
1086
1087   printf("\n");
1088   printf_line("=", 79);
1089
1090   CloseAllAndExit(0);
1091 }
1092
1093
1094 /* ---------- new tape button stuff ---------------------------------------- */
1095
1096 /* graphic position values for tape buttons */
1097 #define TAPE_BUTTON_XSIZE       18
1098 #define TAPE_BUTTON_YSIZE       18
1099 #define TAPE_BUTTON_XPOS        5
1100 #define TAPE_BUTTON_YPOS        77
1101
1102 #define TAPE_BUTTON_EJECT_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
1103 #define TAPE_BUTTON_EXTRA_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
1104 #define TAPE_BUTTON_STOP_XPOS   (TAPE_BUTTON_XPOS + 1 * TAPE_BUTTON_XSIZE)
1105 #define TAPE_BUTTON_PAUSE_XPOS  (TAPE_BUTTON_XPOS + 2 * TAPE_BUTTON_XSIZE)
1106 #define TAPE_BUTTON_RECORD_XPOS (TAPE_BUTTON_XPOS + 3 * TAPE_BUTTON_XSIZE)
1107 #define TAPE_BUTTON_PLAY_XPOS   (TAPE_BUTTON_XPOS + 4 * TAPE_BUTTON_XSIZE)
1108
1109 static struct
1110 {
1111   int x, y;
1112   int gadget_id;
1113   char *infotext;
1114 } tapebutton_info[NUM_TAPE_BUTTONS] =
1115 {
1116   {
1117     TAPE_BUTTON_EJECT_XPOS,     TAPE_BUTTON_YPOS,
1118     TAPE_CTRL_ID_EJECT,
1119     "eject tape"
1120   },
1121   {
1122     TAPE_BUTTON_EXTRA_XPOS,     TAPE_BUTTON_YPOS,
1123     TAPE_CTRL_ID_EXTRA,
1124     "extra functions"
1125   },
1126   {
1127     TAPE_BUTTON_STOP_XPOS,      TAPE_BUTTON_YPOS,
1128     TAPE_CTRL_ID_STOP,
1129     "stop tape"
1130   },
1131   {
1132     TAPE_BUTTON_PAUSE_XPOS,     TAPE_BUTTON_YPOS,
1133     TAPE_CTRL_ID_PAUSE,
1134     "pause tape"
1135   },
1136   {
1137     TAPE_BUTTON_RECORD_XPOS,    TAPE_BUTTON_YPOS,
1138     TAPE_CTRL_ID_RECORD,
1139     "record tape"
1140   },
1141   {
1142     TAPE_BUTTON_PLAY_XPOS,      TAPE_BUTTON_YPOS,
1143     TAPE_CTRL_ID_PLAY,
1144     "play tape"
1145   }
1146 };
1147
1148 void CreateTapeButtons()
1149 {
1150   int i;
1151
1152   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1153   {
1154     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
1155     struct GadgetInfo *gi;
1156     int gd_xoffset, gd_yoffset;
1157     int gd_x1, gd_x2, gd_y;
1158     int id = i;
1159
1160     gd_xoffset = tapebutton_info[i].x;
1161     gd_yoffset = tapebutton_info[i].y;
1162     gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
1163     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
1164     gd_y  = DOOR_GFX_PAGEY2 + gd_yoffset;
1165
1166     if (i == TAPE_CTRL_ID_EXTRA)
1167     {
1168       gd_x1 = DOOR_GFX_PAGEX6 + gd_xoffset;
1169       gd_x2 = DOOR_GFX_PAGEX5 + gd_xoffset;
1170     }
1171
1172     gi = CreateGadget(GDI_CUSTOM_ID, id,
1173                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
1174                       GDI_X, VX + gd_xoffset,
1175                       GDI_Y, VY + gd_yoffset,
1176                       GDI_WIDTH, TAPE_BUTTON_XSIZE,
1177                       GDI_HEIGHT, TAPE_BUTTON_YSIZE,
1178                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
1179                       GDI_STATE, GD_BUTTON_UNPRESSED,
1180                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
1181                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
1182                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
1183                       GDI_CALLBACK_ACTION, HandleTapeButtons,
1184                       GDI_END);
1185
1186     if (gi == NULL)
1187       Error(ERR_EXIT, "cannot create gadget");
1188
1189     tape_gadget[id] = gi;
1190   }
1191 }
1192
1193 void FreeTapeButtons()
1194 {
1195   int i;
1196
1197   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1198     FreeGadget(tape_gadget[i]);
1199 }
1200
1201 void MapTapeEjectButton()
1202 {
1203   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1204   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1205 }
1206
1207 void MapTapeWarpButton()
1208 {
1209   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1210   MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1211 }
1212
1213 void MapTapeButtons()
1214 {
1215   int i;
1216
1217   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1218     if (i != TAPE_CTRL_ID_EXTRA)
1219       MapGadget(tape_gadget[i]);
1220
1221   if (tape.recording || tape.playing)
1222     MapTapeWarpButton();
1223 }
1224
1225 void UnmapTapeButtons()
1226 {
1227   int i;
1228
1229   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1230     UnmapGadget(tape_gadget[i]);
1231 }
1232
1233 static void HandleTapeButtons(struct GadgetInfo *gi)
1234 {
1235   int id = gi->custom_id;
1236
1237   if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
1238     return;
1239
1240   switch (id)
1241   {
1242     case TAPE_CTRL_ID_EJECT:
1243       TapeStop();
1244       if (TAPE_IS_EMPTY(tape))
1245       {
1246         LoadTape(level_nr);
1247         if (TAPE_IS_EMPTY(tape))
1248           Request("No tape for this level !", REQ_CONFIRM);
1249       }
1250       else
1251       {
1252         if (tape.changed)
1253           SaveTape(tape.level_nr);
1254         TapeErase();
1255       }
1256       DrawCompleteVideoDisplay();
1257       break;
1258
1259     case TAPE_CTRL_ID_EXTRA:
1260       if (tape.playing)
1261       {
1262         if (!tape.warp_forward)                 /* PLAY -> WARP FORWARD PLAY */
1263         {
1264           TapeStartWarpForward();
1265         }
1266         else                                    /* WARP FORWARD PLAY -> PLAY */
1267         {
1268           TapeStopWarpForward();
1269         }
1270       }
1271       else if (tape.recording)
1272         TapeSingleStep();
1273       break;
1274
1275     case TAPE_CTRL_ID_STOP:
1276       TapeStop();
1277       break;
1278
1279     case TAPE_CTRL_ID_PAUSE:
1280       TapeTogglePause(TAPE_TOGGLE_MANUAL);
1281       break;
1282
1283     case TAPE_CTRL_ID_RECORD:
1284       if (TAPE_IS_STOPPED(tape))
1285         TapeStartGameRecording();
1286       else if (tape.pausing)
1287       {
1288         if (tape.playing)                       /* PLAY -> PAUSE -> RECORD */
1289           TapeAppendRecording();
1290         else
1291           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1292       }
1293       break;
1294
1295     case TAPE_CTRL_ID_PLAY:
1296       if (TAPE_IS_EMPTY(tape))
1297         break;
1298
1299       if (TAPE_IS_STOPPED(tape))
1300       {
1301         TapeStartGamePlaying();
1302       }
1303       else if (tape.playing)
1304       {
1305         if (tape.pausing)                       /* PAUSE -> PLAY */
1306         {
1307           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1308         }
1309         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
1310         {
1311           tape.fast_forward = TRUE;
1312           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
1313         }
1314         else if (!tape.pause_before_death)      /* FFWD PLAY -> AUTO PAUSE */
1315         {
1316           tape.pause_before_death = TRUE;
1317           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_ON, 0);
1318         }
1319         else                                    /* AUTO PAUSE -> NORMAL PLAY */
1320         {
1321           if (tape.warp_forward)
1322             TapeStopWarpForward();
1323
1324           tape.fast_forward = FALSE;
1325           tape.pause_before_death = FALSE;
1326
1327           DrawVideoDisplay(VIDEO_STATE_PBEND_OFF | VIDEO_STATE_PLAY_ON, 0);
1328         }
1329       }
1330       break;
1331
1332     default:
1333       break;
1334   }
1335 }