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