rnd-20100617-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
198 #if 0
199   static struct
200   {
201     int gd_x1, gd_y1;
202     int gd_x2, gd_y2;
203     int x, y;
204     int width, height;
205   }
206   video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
207   {
208     {
209       {
210         PG_X4(VIDEO_PLAY_LABEL_XPOS),   PG_Y2(VIDEO_PLAY_LABEL_YPOS),
211         PG_X3(VIDEO_PLAY_LABEL_XPOS),   PG_Y2(VIDEO_PLAY_LABEL_YPOS),
212         VIDEO_PLAY_LABEL_XPOS,          VIDEO_PLAY_LABEL_YPOS,
213         VIDEO_PLAY_LABEL_XSIZE,         VIDEO_PLAY_LABEL_YSIZE
214       },
215       {
216         PG_X4(VIDEO_PLAY_SYMBOL_XPOS),  PG_Y2(VIDEO_PLAY_SYMBOL_YPOS),
217         PG_X3(VIDEO_PLAY_SYMBOL_XPOS),  PG_Y2(VIDEO_PLAY_SYMBOL_YPOS),
218         VIDEO_PLAY_SYMBOL_XPOS,         VIDEO_PLAY_SYMBOL_YPOS,
219         VIDEO_PLAY_SYMBOL_XSIZE,        VIDEO_PLAY_SYMBOL_YSIZE
220       }
221     },
222     {
223       {
224         PG_X4(VIDEO_REC_LABEL_XPOS),    PG_Y2(VIDEO_REC_LABEL_YPOS),
225         PG_X3(VIDEO_REC_LABEL_XPOS),    PG_Y2(VIDEO_REC_LABEL_YPOS),
226         VIDEO_REC_LABEL_XPOS,           VIDEO_REC_LABEL_YPOS,
227         VIDEO_REC_LABEL_XSIZE,          VIDEO_REC_LABEL_YSIZE
228       },
229       {
230         PG_X4(VIDEO_REC_SYMBOL_XPOS),   PG_Y2(VIDEO_REC_SYMBOL_YPOS),
231         PG_X3(VIDEO_REC_SYMBOL_XPOS),   PG_Y2(VIDEO_REC_SYMBOL_YPOS),
232         VIDEO_REC_SYMBOL_XPOS,          VIDEO_REC_SYMBOL_YPOS,
233         VIDEO_REC_SYMBOL_XSIZE,         VIDEO_REC_SYMBOL_YSIZE
234       }
235     },
236     {
237       {
238         PG_X4(VIDEO_PAUSE_LABEL_XPOS),  PG_Y2(VIDEO_PAUSE_LABEL_YPOS),
239         PG_X3(VIDEO_PAUSE_LABEL_XPOS),  PG_Y2(VIDEO_PAUSE_LABEL_YPOS),
240         VIDEO_PAUSE_LABEL_XPOS,         VIDEO_PAUSE_LABEL_YPOS,
241         VIDEO_PAUSE_LABEL_XSIZE,        VIDEO_PAUSE_LABEL_YSIZE
242       },
243       {
244         PG_X4(VIDEO_PAUSE_SYMBOL_XPOS), PG_Y2(VIDEO_PAUSE_SYMBOL_YPOS),
245         PG_X3(VIDEO_PAUSE_SYMBOL_XPOS), PG_Y2(VIDEO_PAUSE_SYMBOL_YPOS),
246         VIDEO_PAUSE_SYMBOL_XPOS,        VIDEO_PAUSE_SYMBOL_YPOS,
247         VIDEO_PAUSE_SYMBOL_XSIZE,       VIDEO_PAUSE_SYMBOL_YSIZE
248       }
249     },
250     {
251       {
252         PG_X4(VIDEO_DATE_LABEL_XPOS),   PG_Y2(VIDEO_DATE_LABEL_YPOS),
253         PG_X3(VIDEO_DATE_LABEL_XPOS),   PG_Y2(VIDEO_DATE_LABEL_YPOS),
254         VIDEO_DATE_LABEL_XPOS,          VIDEO_DATE_LABEL_YPOS,
255         VIDEO_DATE_LABEL_XSIZE,         VIDEO_DATE_LABEL_YSIZE
256       },
257       {
258         PG_X4(VIDEO_DATE_XPOS),         PG_Y2(VIDEO_DATE_YPOS),
259         PG_X3(VIDEO_DATE_XPOS),         PG_Y2(VIDEO_DATE_YPOS),
260         VIDEO_DATE_XPOS,                VIDEO_DATE_YPOS,
261         VIDEO_DATE_XSIZE,               VIDEO_DATE_YSIZE
262       }
263     },
264     {
265       {
266         -1,                             -1,
267         -1,                             -1,
268         -1,                             -1,
269         -1,                             -1
270       },
271       {
272         PG_X4(VIDEO_TIME_XPOS),         PG_Y2(VIDEO_TIME_YPOS),
273         PG_X3(VIDEO_TIME_XPOS),         PG_Y2(VIDEO_TIME_YPOS),
274         VIDEO_TIME_XPOS,                VIDEO_TIME_YPOS,
275         VIDEO_TIME_XSIZE,               VIDEO_TIME_YSIZE
276       }
277     },
278     {
279       {
280         PG_X6(VIDEO_FFWD_LABEL_XPOS),   PG_Y1(VIDEO_FFWD_LABEL_YPOS),
281         PG_X3(VIDEO_LABEL_XPOS),        PG_Y2(VIDEO_LABEL_YPOS),
282         VIDEO_LABEL_XPOS,               VIDEO_LABEL_YPOS,
283         VIDEO_LABEL_XSIZE,              VIDEO_LABEL_YSIZE
284       },
285       {
286         PG_X6(VIDEO_FFWD_SYMBOL_XPOS),  PG_Y1(VIDEO_FFWD_SYMBOL_YPOS),
287         PG_X3(VIDEO_FFWD_SYMBOL_XPOS),  PG_Y2(VIDEO_SYMBOL_YPOS),
288         VIDEO_SYMBOL_XPOS,              VIDEO_SYMBOL_YPOS,
289         VIDEO_FFWD_SYMBOL_XSIZE,        VIDEO_FFWD_SYMBOL_YSIZE
290       }
291     },
292     {
293       {
294         PG_X6(VIDEO_PBEND_LABEL_XPOS),  PG_Y1(VIDEO_PBEND_LABEL_YPOS),
295         PG_X3(VIDEO_LABEL_XPOS),        PG_Y2(VIDEO_LABEL_YPOS),
296         VIDEO_LABEL_XPOS,               VIDEO_LABEL_YPOS,
297         VIDEO_LABEL_XSIZE,              VIDEO_LABEL_YSIZE
298       },
299       {
300         PG_X6(VIDEO_PBEND_SYMBOL_XPOS), PG_Y1(VIDEO_PBEND_SYMBOL_YPOS),
301         PG_X3(VIDEO_PBEND_SYMBOL_XPOS), PG_Y2(VIDEO_SYMBOL_YPOS),
302         VIDEO_SYMBOL_XPOS,              VIDEO_SYMBOL_YPOS,
303         VIDEO_PBEND_SYMBOL_XSIZE,       VIDEO_PBEND_SYMBOL_YSIZE
304       }
305     },
306     {
307       {
308         PG_X6(VIDEO_WARP_LABEL_XPOS),   PG_Y1(VIDEO_WARP_LABEL_YPOS),
309         PG_X3(VIDEO_LABEL_XPOS),        PG_Y2(VIDEO_LABEL_YPOS),
310         VIDEO_LABEL_XPOS,               VIDEO_LABEL_YPOS,
311         VIDEO_LABEL_XSIZE,              VIDEO_LABEL_YSIZE
312       },
313       {
314         PG_X6(VIDEO_WARP_SYMBOL_XPOS),  PG_Y1(VIDEO_WARP_SYMBOL_YPOS),
315         PG_X3(VIDEO_WARP_SYMBOL_XPOS),  PG_Y2(VIDEO_SYMBOL_YPOS),
316         VIDEO_SYMBOL_XPOS,              VIDEO_SYMBOL_YPOS,
317         VIDEO_WARP_SYMBOL_XSIZE,        VIDEO_WARP_SYMBOL_YSIZE
318       }
319     },
320     {
321       {
322         -1,                             -1,
323         -1,                             -1,
324         -1,                             -1,
325         -1,                             -1
326       },
327       {
328         PG_X6(VIDEO_WARP2_SYMBOL_XPOS), PG_Y1(VIDEO_WARP2_SYMBOL_YPOS),
329         PG_X3(VIDEO_WARP2_SYMBOL_XPOS), PG_Y2(VIDEO_SYMBOL_YPOS),
330         VIDEO_SYMBOL_XPOS,              VIDEO_SYMBOL_YPOS,
331         VIDEO_WARP2_SYMBOL_XSIZE,       VIDEO_WARP2_SYMBOL_YSIZE
332       }
333     },
334     {
335       {
336         -1,                             -1,
337         -1,                             -1,
338         -1,                             -1,
339         -1,                             -1
340       },
341       {
342         PG_X6(VIDEO_1STEP_SYMBOL_XPOS), PG_Y1(VIDEO_1STEP_SYMBOL_YPOS),
343         PG_X3(VIDEO_1STEP_SYMBOL_XPOS), PG_Y2(VIDEO_SYMBOL_YPOS),
344         VIDEO_1STEP_SYMBOL_XPOS,        VIDEO_SYMBOL_YPOS,
345         VIDEO_1STEP_SYMBOL_XSIZE,       VIDEO_1STEP_SYMBOL_YSIZE
346       }
347     },
348   };
349 #endif
350
351 #if 1
352   static struct
353   {
354     int graphic;
355     struct Rect *pos;
356   }
357   video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
358   {
359     {
360       { IMG_TAPE_LABEL_GFX_PLAY,                &tape.label.play        },
361       { IMG_TAPE_SYMBOL_GFX_PLAY,               &tape.symbol.play       },
362     },
363     {
364       { IMG_TAPE_LABEL_GFX_RECORD,              &tape.label.record      },
365       { IMG_TAPE_SYMBOL_GFX_RECORD,             &tape.symbol.record     },
366     },
367     {
368       { IMG_TAPE_LABEL_GFX_PAUSE,               &tape.label.pause       },
369       { IMG_TAPE_SYMBOL_GFX_PAUSE,              &tape.symbol.pause      },
370     },
371     {
372       { IMG_TAPE_LABEL_GFX_DATE,                &tape.label.date        },
373       { -1,                                     NULL                    },
374     },
375     {
376       { IMG_TAPE_LABEL_GFX_TIME,                &tape.label.time        },
377       { -1,                                     NULL                    },
378     },
379     {
380       { IMG_TAPE_LABEL_GFX_FAST_FORWARD,        &tape.label.fast_forward  },
381       { IMG_TAPE_SYMBOL_GFX_FAST_FORWARD,       &tape.symbol.fast_forward },
382     },
383     {
384       { IMG_TAPE_LABEL_GFX_PAUSE_BEFORE_END,    &tape.label.pause_before_end  },
385       { IMG_TAPE_SYMBOL_GFX_PAUSE_BEFORE_END,   &tape.symbol.pause_before_end },
386     },
387     {
388       { IMG_TAPE_LABEL_GFX_WARP_FORWARD_BLIND,  &tape.label.warp_forward_blind},
389       { IMG_TAPE_SYMBOL_GFX_WARP_FORWARD_BLIND, &tape.symbol.warp_forward_blind},
390     },
391     {
392       { IMG_TAPE_LABEL_GFX_WARP_FORWARD,        &tape.label.warp_forward  },
393       { IMG_TAPE_SYMBOL_GFX_WARP_FORWARD,       &tape.symbol.warp_forward },
394     },
395     {
396       { IMG_TAPE_LABEL_GFX_SINGLE_STEP,         &tape.label.single_step  },
397       { IMG_TAPE_SYMBOL_GFX_SINGLE_STEP,        &tape.symbol.single_step },
398     },
399   };
400
401   for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++)        /* on or off states */
402   {
403     for (i = 0; i < NUM_TAPE_FUNCTIONS; i++)            /* record, play, ... */
404     {
405       for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++)     /* label or symbol */
406       {
407         if (video_pos[i][j].graphic == -1 ||
408             video_pos[i][j].pos->x < 0 ||
409             video_pos[i][j].pos->y < 0)
410           continue;
411
412 #if 0
413         printf("::: %d: %d, %ld\n",
414                i,
415                video_pos[i][j].graphic,
416                graphic_info[video_pos[i][j].graphic].bitmap);
417 #endif
418
419 #if 0
420         if (i < 9)
421           continue;
422 #endif
423
424         if (state & (1 << (i * 2 + k)))
425         {
426           struct GraphicInfo *gfx_bg = &graphic_info[IMG_BACKGROUND_TAPE];
427           struct GraphicInfo *gfx = &graphic_info[video_pos[i][j].graphic];
428           struct Rect *pos = video_pos[i][j].pos;
429           Bitmap *gd_bitmap;
430           int gd_x, gd_y;
431           int skip_value =
432             (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
433
434           if (value == skip_value)
435             continue;
436
437           if (k == 1)           /* on */
438           {
439             gd_bitmap = gfx->bitmap;
440             gd_x = gfx->src_x;
441             gd_y = gfx->src_y;
442           }
443           else                  /* off */
444           {
445             gd_bitmap = gfx_bg->bitmap;
446             gd_x = gfx_bg->src_x + pos->x;
447             gd_y = gfx_bg->src_y + pos->y;
448           }
449
450 #if 0
451           printf("::: %d (%d): %d, %ld - %d [%d] [%d] [%d]\n",
452                  i, k,
453                  video_pos[i][j].graphic,
454                  graphic_info[video_pos[i][j].graphic].bitmap,
455                  gfx->bitmap,
456                  gd_bitmap,
457                  (gd_bitmap == NULL),
458                  0);
459
460           if (gd_bitmap == NULL)
461             printf("::: gfx %d skipped\n", video_pos[i][j].graphic);
462 #endif
463
464           /* some tape graphics may be undefined -- only draw if defined */
465           if (gd_bitmap != NULL)
466             BlitBitmap(gd_bitmap, drawto, gd_x, gd_y, gfx->width, gfx->height,
467                        VX + pos->x, VY + pos->y);
468         }
469       }
470     }
471   }
472
473 #else
474
475   for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++)        /* on or off states */
476   {
477     for (i = 0; i < NUM_TAPE_FUNCTIONS; i++)            /* record, play, ... */
478     {
479       for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++)     /* label or symbol */
480       {
481         if (state & (1 << (i * 2 + k)))
482         {
483           int gd_x, gd_y;
484           int skip_value =
485             (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
486
487           if (k == 1)           /* on */
488           {
489             gd_x = video_pos[i][j].gd_x1;
490             gd_y = video_pos[i][j].gd_y1;
491           }
492           else                  /* off */
493           {
494             gd_x = video_pos[i][j].gd_x2;
495             gd_y = video_pos[i][j].gd_y2;
496           }
497
498           if (video_pos[i][j].x != -1 && value != skip_value)
499             BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
500                        gd_x, gd_y,
501                        video_pos[i][j].width,
502                        video_pos[i][j].height,
503                        VX + video_pos[i][j].x,
504                        VY + video_pos[i][j].y);
505         }
506       }
507     }
508   }
509 #endif
510
511 #if 1
512   if (state & VIDEO_STATE_DATE_ON)
513   {
514     struct TextPosInfo *pos = &tape.text.date;
515     int tag = value % 100;
516     int monat = (value/100) % 100;
517     int jahr = (value/10000);
518
519     DrawText(VX + pos->x,      VY + pos->y, int2str(tag, 2),   pos->font);
520     DrawText(VX + pos->x + 27, VY + pos->y, monatsname[monat], pos->font);
521     DrawText(VX + pos->x + 64, VY + pos->y, int2str(jahr, 2),  pos->font);
522   }
523
524   if (state & VIDEO_STATE_TIME_ON)
525   {
526     struct TextPosInfo *pos = &tape.text.time;
527     int min = value / 60;
528     int sec = value % 60;
529
530     DrawText(VX + pos->x,      VY + pos->y, int2str(min, 2), pos->font);
531     DrawText(VX + pos->x + 27, VY + pos->y, int2str(sec, 2), pos->font);
532   }
533
534 #else
535
536   if (state & VIDEO_STATE_DATE_ON)
537   {
538     int tag = value % 100;
539     int monat = (value/100) % 100;
540     int jahr = (value/10000);
541
542     DrawText(VX + VIDEO_DATE_XPOS, VY + VIDEO_DATE_YPOS,
543              int2str(tag, 2), FONT_TAPE_RECORDER);
544     DrawText(VX + VIDEO_DATE_XPOS + 27, VY + VIDEO_DATE_YPOS,
545              monatsname[monat], FONT_TAPE_RECORDER);
546     DrawText(VX + VIDEO_DATE_XPOS + 64, VY + VIDEO_DATE_YPOS,
547              int2str(jahr, 2), FONT_TAPE_RECORDER);
548   }
549
550   if (state & VIDEO_STATE_TIME_ON)
551   {
552     int min = value / 60;
553     int sec = value % 60;
554
555     DrawText(VX + VIDEO_TIME_XPOS, VY + VIDEO_TIME_YPOS,
556              int2str(min, 2), FONT_TAPE_RECORDER);
557     DrawText(VX + VIDEO_TIME_XPOS + 27, VY + VIDEO_TIME_YPOS,
558              int2str(sec, 2), FONT_TAPE_RECORDER);
559   }
560 #endif
561
562   redraw_mask |= REDRAW_DOOR_2;
563 }
564
565 void DrawCompleteVideoDisplay()
566 {
567 #if 0
568   printf("::: %d, %d  /  %d, %d [%d] [%d, %d] [%d/%d]\n",
569          VX, VY, EX, EY, game_status, gfx.vx, gfx.vy,
570          tape.date, tape.length);
571 #endif
572
573   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
574              DOOR_GFX_PAGEX3, DOOR_GFX_PAGEY2,
575              gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
576   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
577              DOOR_GFX_PAGEX4 + VIDEO_CONTROL_XPOS,
578              DOOR_GFX_PAGEY2 + VIDEO_CONTROL_YPOS,
579              VIDEO_CONTROL_XSIZE, VIDEO_CONTROL_YSIZE,
580              gfx.vx + VIDEO_CONTROL_XPOS, gfx.vy + VIDEO_CONTROL_YPOS);
581
582   DrawVideoDisplay(VIDEO_ALL_OFF, 0);
583
584 #if 1
585   if (tape.recording)
586   {
587     DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
588     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
589     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
590
591     if (tape.pausing)
592       DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
593   }
594   else if (tape.playing)
595   {
596     DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
597     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
598     DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
599
600     if (tape.pausing)
601       DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
602   }
603   else if (tape.date && tape.length)
604   {
605     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
606     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
607   }
608 #else
609   if (tape.date && tape.length)
610   {
611     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
612     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
613   }
614 #endif
615
616   BlitBitmap(drawto, bitmap_db_door, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
617              DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
618 }
619
620 void TapeDeactivateDisplayOn()
621 {
622   SetDrawDeactivationMask(REDRAW_FIELD);
623   audio.sound_deactivated = TRUE;
624 }
625
626 void TapeDeactivateDisplayOff(boolean redraw_display)
627 {
628   SetDrawDeactivationMask(REDRAW_NONE);
629   audio.sound_deactivated = FALSE;
630
631   if (redraw_display)
632   {
633     RedrawPlayfield(TRUE, 0,0,0,0);
634     DrawGameDoorValues();
635   }
636 }
637
638
639 /* ========================================================================= */
640 /* tape control functions                                                    */
641 /* ========================================================================= */
642
643 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
644 {
645   struct tm *lt = localtime(&epoch_seconds);
646
647   tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
648 }
649
650 void TapeSetDateFromNow()
651 {
652   TapeSetDateFromEpochSeconds(time(NULL));
653 }
654
655 void TapeErase()
656 {
657   int i;
658
659   tape.counter = 0;
660   tape.length = 0;
661   tape.length_seconds = 0;
662
663   if (leveldir_current)
664     setString(&tape.level_identifier, leveldir_current->identifier);
665
666   tape.level_nr = level_nr;
667   tape.pos[tape.counter].delay = 0;
668   tape.changed = TRUE;
669
670   tape.random_seed = InitRND(level.random_seed);
671
672   tape.file_version = FILE_VERSION_ACTUAL;
673   tape.game_version = GAME_VERSION_ACTUAL;
674   tape.engine_version = level.game_version;
675
676   TapeSetDateFromNow();
677
678   for (i = 0; i < MAX_PLAYERS; i++)
679     tape.player_participates[i] = FALSE;
680
681   tape.centered_player_nr_next = -1;
682   tape.set_centered_player = FALSE;
683 }
684
685 static void TapeRewind()
686 {
687   tape.counter = 0;
688   tape.delay_played = 0;
689   tape.pause_before_death = FALSE;
690   tape.recording = FALSE;
691   tape.playing = FALSE;
692   tape.fast_forward = FALSE;
693   tape.warp_forward = FALSE;
694   tape.deactivate_display = FALSE;
695   tape.auto_play = (global.autoplay_leveldir != NULL);
696   tape.auto_play_level_solved = FALSE;
697   tape.quick_resume = FALSE;
698   tape.single_step = FALSE;
699
700   tape.centered_player_nr_next = -1;
701   tape.set_centered_player = FALSE;
702
703   InitRND(tape.random_seed);
704 }
705
706 static void TapeSetRandomSeed(long random_seed)
707 {
708   tape.random_seed = InitRND(random_seed);
709 }
710
711 void TapeStartRecording(long random_seed)
712 {
713   if (!TAPE_IS_STOPPED(tape))
714     TapeStop();
715
716   TapeErase();
717   TapeRewind();
718   TapeSetRandomSeed(random_seed);
719
720   tape.recording = TRUE;
721
722   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
723   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
724   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
725
726   MapTapeWarpButton();
727
728   SetDrawDeactivationMask(REDRAW_NONE);
729   audio.sound_deactivated = FALSE;
730 }
731
732 static void TapeStartGameRecording()
733 {
734   TapeStartRecording(level.random_seed);
735
736 #if defined(NETWORK_AVALIABLE)
737   if (options.network)
738   {
739     SendToServer_StartPlaying();
740
741     return;
742   }
743 #endif
744
745   InitGame();
746 }
747
748 static void TapeAppendRecording()
749 {
750   if (!tape.playing || !tape.pausing)
751     return;
752
753   tape.pos[tape.counter].delay = tape.delay_played;
754   tape.playing = FALSE;
755   tape.recording = TRUE;
756   tape.changed = TRUE;
757
758   TapeSetDateFromNow();
759
760   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
761   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
762
763 #if 1
764   UpdateAndDisplayGameControlValues();
765 #endif
766 }
767
768 void TapeHaltRecording()
769 {
770   if (!tape.recording)
771     return;
772
773   tape.counter++;
774   tape.pos[tape.counter].delay = 0;
775
776   tape.length = tape.counter;
777   tape.length_seconds = GetTapeLength();
778 }
779
780 void TapeStopRecording()
781 {
782   TapeHaltRecording();
783
784   tape.recording = FALSE;
785   tape.pausing = FALSE;
786
787   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
788   MapTapeEjectButton();
789 }
790
791 void TapeRecordAction(byte action_raw[MAX_PLAYERS])
792 {
793   byte action[MAX_PLAYERS];
794   int i;
795
796   if (!tape.recording)          /* (record action even when tape is paused) */
797     return;
798
799   if (tape.counter >= MAX_TAPE_LEN - 1)
800   {
801     TapeStopRecording();
802     return;
803   }
804
805   for (i = 0; i < MAX_PLAYERS; i++)
806     action[i] = action_raw[i];
807
808   if (tape.set_centered_player)
809   {
810     for (i = 0; i < MAX_PLAYERS; i++)
811       if (tape.centered_player_nr_next == i ||
812           tape.centered_player_nr_next == -1)
813         action[i] |= KEY_SET_FOCUS;
814
815     tape.set_centered_player = FALSE;
816   }
817
818   if (tape.pos[tape.counter].delay > 0)         /* already stored action */
819   {
820     boolean changed_events = FALSE;
821
822     for (i = 0; i < MAX_PLAYERS; i++)
823       if (tape.pos[tape.counter].action[i] != action[i])
824         changed_events = TRUE;
825
826     if (changed_events || tape.pos[tape.counter].delay >= 255)
827     {
828       tape.counter++;
829       tape.pos[tape.counter].delay = 0;
830     }
831     else
832       tape.pos[tape.counter].delay++;
833   }
834
835   if (tape.pos[tape.counter].delay == 0)        /* store new action */
836   {
837     for (i = 0; i < MAX_PLAYERS; i++)
838       tape.pos[tape.counter].action[i] = action[i];
839
840     tape.pos[tape.counter].delay++;
841   }
842 }
843
844 void TapeTogglePause(boolean toggle_manual)
845 {
846   int state = 0;
847
848   if (tape.pause_before_death)
849     state |= VIDEO_STATE_PBEND_OFF;
850   else if (tape.fast_forward)
851     state |= VIDEO_STATE_FFWD_OFF;
852
853   tape.pausing = !tape.pausing;
854   tape.fast_forward = FALSE;
855   tape.pause_before_death = FALSE;
856
857   if (tape.single_step && toggle_manual)
858     tape.single_step = FALSE;
859
860   state |= (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
861   if (tape.playing)
862     state |= VIDEO_STATE_PLAY_ON;
863   else
864     state |= (tape.single_step ? VIDEO_STATE_1STEP_ON : VIDEO_STATE_1STEP_OFF);
865
866   DrawVideoDisplay(state, 0);
867
868   if (tape.warp_forward)
869   {
870     TapeStopWarpForward();
871
872     if (tape.quick_resume)
873     {
874       tape.quick_resume = FALSE;
875
876       TapeAppendRecording();
877
878       if (!CheckEngineSnapshot())
879         SaveEngineSnapshot();
880     }
881   }
882 }
883
884 void TapeStartPlaying()
885 {
886   if (TAPE_IS_EMPTY(tape))
887     return;
888
889   if (!TAPE_IS_STOPPED(tape))
890     TapeStop();
891
892   TapeRewind();
893
894   tape.playing = TRUE;
895
896   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
897   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
898   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
899
900   MapTapeWarpButton();
901
902   SetDrawDeactivationMask(REDRAW_NONE);
903   audio.sound_deactivated = FALSE;
904 }
905
906 static void TapeStartGamePlaying()
907 {
908   TapeStartPlaying();
909
910   InitGame();
911 }
912
913 void TapeStopPlaying()
914 {
915   tape.playing = FALSE;
916   tape.pausing = FALSE;
917
918   if (tape.warp_forward)
919     TapeStopWarpForward();
920
921   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
922   MapTapeEjectButton();
923 }
924
925 byte *TapePlayAction()
926 {
927   static byte action[MAX_PLAYERS];
928   int i;
929
930   if (!tape.playing || tape.pausing)
931     return NULL;
932
933   if (tape.pause_before_death)  /* stop 10 seconds before player gets killed */
934   {
935     if (!(FrameCounter % 20))
936     {
937       if ((FrameCounter / 20) % 2)
938         DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
939       else
940         DrawVideoDisplay(VIDEO_STATE_PBEND_OFF, VIDEO_DISPLAY_LABEL_ONLY);
941     }
942
943     if (tape.warp_forward)
944     {
945       if (tape.deactivate_display)
946         DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
947       else
948         DrawVideoDisplay(VIDEO_STATE_WARP2_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
949     }
950
951     if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
952     {
953       TapeTogglePause(TAPE_TOGGLE_MANUAL);
954
955       return NULL;
956     }
957   }
958   else if (tape.fast_forward)
959   {
960     if ((FrameCounter / 20) % 2)
961       DrawVideoDisplay(VIDEO_STATE_FFWD_ON, VIDEO_DISPLAY_LABEL_ONLY);
962     else
963       DrawVideoDisplay(VIDEO_STATE_FFWD_OFF, VIDEO_DISPLAY_LABEL_ONLY);
964
965     if (tape.warp_forward)
966     {
967       if (tape.deactivate_display)
968         DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
969       else
970         DrawVideoDisplay(VIDEO_STATE_WARP2_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
971     }
972   }
973
974 #if 0
975   /* !!! this makes things much slower !!! */
976   else if (tape.warp_forward)
977   {
978     if ((FrameCounter / 20) % 2)
979       DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_LABEL_ONLY);
980     else
981       DrawVideoDisplay(VIDEO_STATE_WARP_OFF, VIDEO_DISPLAY_LABEL_ONLY);
982
983     DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
984   }
985 #endif
986
987   if (tape.counter >= tape.length)      /* end of tape reached */
988   {
989     if (tape.warp_forward && !tape.auto_play)
990       TapeTogglePause(TAPE_TOGGLE_MANUAL);
991     else
992       TapeStop();
993
994     return NULL;
995   }
996
997   for (i = 0; i < MAX_PLAYERS; i++)
998     action[i] = tape.pos[tape.counter].action[i];
999
1000   tape.set_centered_player = FALSE;
1001   tape.centered_player_nr_next = -999;
1002
1003   for (i = 0; i < MAX_PLAYERS; i++)
1004   {
1005     if (action[i] & KEY_SET_FOCUS)
1006     {
1007       tape.set_centered_player = TRUE;
1008       tape.centered_player_nr_next =
1009         (tape.centered_player_nr_next == -999 ? i : -1);
1010     }
1011
1012     action[i] &= ~KEY_SET_FOCUS;
1013   }
1014
1015   tape.delay_played++;
1016   if (tape.delay_played >= tape.pos[tape.counter].delay)
1017   {
1018     tape.counter++;
1019     tape.delay_played = 0;
1020   }
1021
1022   return action;
1023 }
1024
1025 void TapeStop()
1026 {
1027   TapeStopRecording();
1028   TapeStopPlaying();
1029
1030   DrawVideoDisplay(VIDEO_STATE_OFF, 0);
1031
1032   if (tape.date && tape.length)
1033   {
1034     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
1035     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
1036   }
1037 }
1038
1039 unsigned int GetTapeLength()
1040 {
1041   unsigned int tape_length = 0;
1042   int i;
1043
1044   if (TAPE_IS_EMPTY(tape))
1045     return(0);
1046
1047   for (i = 0; i < tape.length; i++)
1048     tape_length += tape.pos[i].delay;
1049
1050   return(tape_length * GAME_FRAME_DELAY / 1000);
1051 }
1052
1053 static void TapeStartWarpForward()
1054 {
1055   tape.warp_forward = TRUE;
1056
1057   if (!tape.fast_forward && !tape.pause_before_death)
1058   {
1059     tape.pausing = FALSE;
1060     tape.deactivate_display = TRUE;
1061
1062     TapeDeactivateDisplayOn();
1063   }
1064
1065   if (tape.fast_forward || tape.pause_before_death)
1066     DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
1067   else
1068     DrawVideoDisplay(VIDEO_STATE_WARP_ON, 0);
1069 }
1070
1071 static void TapeStopWarpForward()
1072 {
1073   int state = (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
1074
1075   tape.warp_forward = FALSE;
1076   tape.deactivate_display = FALSE;
1077
1078   TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
1079
1080   if (tape.pause_before_death)
1081     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_PBEND_ON;
1082   else if (tape.fast_forward)
1083     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_FFWD_ON;
1084   else
1085     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_PLAY_ON;
1086
1087   DrawVideoDisplay(state, 0);
1088 }
1089
1090 static void TapeSingleStep()
1091 {
1092   if (options.network)
1093     return;
1094
1095   if (!tape.pausing)
1096     TapeTogglePause(TAPE_TOGGLE_MANUAL);
1097
1098   tape.single_step = !tape.single_step;
1099
1100   DrawVideoDisplay((tape.single_step ? VIDEO_STATE_1STEP_ON :
1101                     VIDEO_STATE_1STEP_OFF), 0);
1102 }
1103
1104 void TapeQuickSave()
1105 {
1106   if (game_status == GAME_MODE_MAIN)
1107   {
1108     Request("No game that can be saved !", REQ_CONFIRM);
1109
1110     return;
1111   }
1112
1113   if (game_status != GAME_MODE_PLAYING)
1114     return;
1115
1116   if (tape.recording)
1117     TapeHaltRecording();        /* prepare tape for saving on-the-fly */
1118
1119   if (TAPE_IS_EMPTY(tape))
1120   {
1121     Request("No tape that can be saved !", REQ_CONFIRM);
1122
1123     return;
1124   }
1125
1126   if (SaveTapeChecked(tape.level_nr))
1127     SaveEngineSnapshot();
1128 }
1129
1130 void TapeQuickLoad()
1131 {
1132   char *filename = getTapeFilename(level_nr);
1133
1134   if (!fileExists(filename))
1135   {
1136     Request("No tape for this level !", REQ_CONFIRM);
1137
1138     return;
1139   }
1140
1141   if (tape.recording && !Request("Stop recording and load tape ?",
1142                                  REQ_ASK | REQ_STAY_CLOSED))
1143   {
1144     OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1145
1146     return;
1147   }
1148
1149   if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
1150     return;
1151
1152   if (CheckEngineSnapshot())
1153   {
1154     TapeStartGamePlaying();
1155
1156     LoadEngineSnapshot();
1157
1158     DrawCompleteVideoDisplay();
1159
1160     tape.playing = TRUE;
1161     tape.pausing = TRUE;
1162
1163     TapeStopWarpForward();
1164     TapeAppendRecording();
1165
1166     if (FrameCounter > 0)
1167       return;
1168   }
1169
1170   TapeStop();
1171   TapeErase();
1172
1173   LoadTape(level_nr);
1174
1175   if (!TAPE_IS_EMPTY(tape))
1176   {
1177     TapeStartGamePlaying();
1178     TapeStartWarpForward();
1179
1180     tape.quick_resume = TRUE;
1181   }
1182   else  /* this should not happen (basically checked above) */
1183   {
1184     int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1185
1186     Request("No tape for this level !", REQ_CONFIRM | reopen_door);
1187   }
1188 }
1189
1190 void InsertSolutionTape()
1191 {
1192   if (!TAPE_IS_EMPTY(tape))
1193     return;
1194
1195   LoadSolutionTape(level_nr);
1196
1197   if (TAPE_IS_EMPTY(tape))
1198     Request("No solution tape for this level !", REQ_CONFIRM);
1199
1200   DrawCompleteVideoDisplay();
1201 }
1202
1203
1204 /* ------------------------------------------------------------------------- *
1205  * tape autoplay functions
1206  * ------------------------------------------------------------------------- */
1207
1208 void AutoPlayTape()
1209 {
1210   static LevelDirTree *autoplay_leveldir = NULL;
1211   static boolean autoplay_initialized = FALSE;
1212   static int autoplay_level_nr = -1;
1213   static int num_levels_played = 0;
1214   static int num_levels_solved = 0;
1215   static int num_tape_missing = 0;
1216   static boolean level_failed[MAX_TAPES_PER_SET];
1217   static boolean tape_missing[MAX_TAPES_PER_SET];
1218   int i;
1219
1220   if (autoplay_initialized)
1221   {
1222     /* just finished auto-playing tape */
1223     printf("%s.\n", tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
1224
1225     num_levels_played++;
1226
1227     if (tape.auto_play_level_solved)
1228       num_levels_solved++;
1229     else if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1230       level_failed[level_nr] = TRUE;
1231   }
1232   else
1233   {
1234     DrawCompleteVideoDisplay();
1235     audio.sound_enabled = FALSE;
1236
1237     autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1238                                                   global.autoplay_leveldir);
1239
1240     if (autoplay_leveldir == NULL)
1241       Error(ERR_EXIT, "no such level identifier: '%s'",
1242             global.autoplay_leveldir);
1243
1244     leveldir_current = autoplay_leveldir;
1245
1246     if (autoplay_leveldir->first_level < 0)
1247       autoplay_leveldir->first_level = 0;
1248     if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
1249       autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1250
1251     autoplay_level_nr = autoplay_leveldir->first_level;
1252
1253     printf_line("=", 79);
1254     printf("Automatically playing level tapes\n");
1255     printf_line("-", 79);
1256     printf("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
1257     printf("Level series name:       '%s'\n", autoplay_leveldir->name);
1258     printf("Level series author:     '%s'\n", autoplay_leveldir->author);
1259     printf("Number of levels:        %d\n",   autoplay_leveldir->levels);
1260     printf_line("=", 79);
1261     printf("\n");
1262
1263     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1264     {
1265       level_failed[i] = FALSE;
1266       tape_missing[i] = FALSE;
1267     }
1268
1269     autoplay_initialized = TRUE;
1270   }
1271
1272   while (autoplay_level_nr <= autoplay_leveldir->last_level)
1273   {
1274     level_nr = autoplay_level_nr++;
1275
1276     if (!global.autoplay_all && !global.autoplay_level[level_nr])
1277       continue;
1278
1279     TapeErase();
1280
1281     printf("Level %03d: ", level_nr);
1282
1283     LoadLevel(level_nr);
1284     if (level.no_valid_file)
1285     {
1286       printf("(no level)\n");
1287       continue;
1288     }
1289
1290 #if 0
1291     /* ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY */
1292     printf("(only testing level)\n");
1293     continue;
1294 #endif
1295
1296     LoadSolutionTape(level_nr);
1297
1298     if (tape.no_valid_file)
1299     {
1300       num_tape_missing++;
1301       if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1302         tape_missing[level_nr] = TRUE;
1303
1304       printf("(no tape)\n");
1305       continue;
1306     }
1307
1308     printf("playing tape ... ");
1309
1310     TapeStartGamePlaying();
1311     TapeStartWarpForward();
1312
1313     return;
1314   }
1315
1316   printf("\n");
1317   printf_line("=", 79);
1318   printf("Number of levels played: %d\n", num_levels_played);
1319   printf("Number of levels solved: %d (%d%%)\n", num_levels_solved,
1320          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1321   printf_line("-", 79);
1322   printf("Summary (for automatic parsing by scripts):\n");
1323   printf("LEVELDIR '%s', SOLVED %d/%d (%d%%)",
1324          autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
1325          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1326
1327   if (num_levels_played != num_levels_solved)
1328   {
1329     printf(", FAILED:");
1330     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1331       if (level_failed[i])
1332         printf(" %03d", i);
1333   }
1334
1335 #if 0
1336   if (num_tape_missing > 0)
1337   {
1338     printf(", NO TAPE:");
1339     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1340       if (tape_missing[i])
1341         printf(" %03d", i);
1342   }
1343 #endif
1344
1345   printf("\n");
1346   printf_line("=", 79);
1347
1348   CloseAllAndExit(0);
1349 }
1350
1351
1352 /* ---------- new tape button stuff ---------------------------------------- */
1353
1354 static struct
1355 {
1356   int graphic;
1357   struct Rect *pos;
1358   int gadget_id;
1359   char *infotext;
1360 } tapebutton_info[NUM_TAPE_BUTTONS] =
1361 {
1362   {
1363     IMG_TAPE_BUTTON_GFX_EJECT,          &tape.button.eject,
1364     TAPE_CTRL_ID_EJECT,                 "eject tape"
1365   },
1366   {
1367     /* (same position as "eject" button) */
1368     IMG_TAPE_BUTTON_GFX_EXTRA,          &tape.button.eject,
1369     TAPE_CTRL_ID_EXTRA,                 "extra functions"
1370   },
1371   {
1372     IMG_TAPE_BUTTON_GFX_STOP,           &tape.button.stop,
1373     TAPE_CTRL_ID_STOP,                  "stop tape"
1374   },
1375   {
1376     IMG_TAPE_BUTTON_GFX_PAUSE,          &tape.button.pause,
1377     TAPE_CTRL_ID_PAUSE,                 "pause tape"
1378   },
1379   {
1380     IMG_TAPE_BUTTON_GFX_RECORD,         &tape.button.record,
1381     TAPE_CTRL_ID_RECORD,                "record tape"
1382   },
1383   {
1384     IMG_TAPE_BUTTON_GFX_PLAY,           &tape.button.play,
1385     TAPE_CTRL_ID_PLAY,                  "play tape"
1386   }
1387 };
1388
1389 void CreateTapeButtons()
1390 {
1391   int i;
1392
1393   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1394   {
1395     struct GraphicInfo *gfx = &graphic_info[tapebutton_info[i].graphic];
1396     struct Rect *pos = tapebutton_info[i].pos;
1397     struct GadgetInfo *gi;
1398     int gd_x = gfx->src_x;
1399     int gd_y = gfx->src_y;
1400     int gd_xp = gfx->src_x + gfx->pressed_xoffset;
1401     int gd_yp = gfx->src_y + gfx->pressed_yoffset;
1402     int id = i;
1403
1404     gi = CreateGadget(GDI_CUSTOM_ID, id,
1405                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
1406                       GDI_X, VX + pos->x,
1407                       GDI_Y, VY + pos->y,
1408                       GDI_WIDTH, gfx->width,
1409                       GDI_HEIGHT, gfx->height,
1410                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
1411                       GDI_STATE, GD_BUTTON_UNPRESSED,
1412                       GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
1413                       GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
1414                       GDI_DIRECT_DRAW, FALSE,
1415                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
1416                       GDI_CALLBACK_ACTION, HandleTapeButtons,
1417                       GDI_END);
1418
1419     if (gi == NULL)
1420       Error(ERR_EXIT, "cannot create gadget");
1421
1422     tape_gadget[id] = gi;
1423   }
1424 }
1425
1426 void FreeTapeButtons()
1427 {
1428   int i;
1429
1430   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1431     FreeGadget(tape_gadget[i]);
1432 }
1433
1434 void MapTapeEjectButton()
1435 {
1436   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1437   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1438 }
1439
1440 void MapTapeWarpButton()
1441 {
1442   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1443   MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1444 }
1445
1446 void MapTapeButtons()
1447 {
1448   int i;
1449
1450   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1451     if (i != TAPE_CTRL_ID_EXTRA)
1452       MapGadget(tape_gadget[i]);
1453
1454   if (tape.recording || tape.playing)
1455     MapTapeWarpButton();
1456 }
1457
1458 void UnmapTapeButtons()
1459 {
1460   int i;
1461
1462   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1463     UnmapGadget(tape_gadget[i]);
1464 }
1465
1466 static void HandleTapeButtonsExt(int id)
1467 {
1468   if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
1469     return;
1470
1471   switch (id)
1472   {
1473     case TAPE_CTRL_ID_EJECT:
1474       TapeStop();
1475
1476       if (TAPE_IS_EMPTY(tape))
1477       {
1478         LoadTape(level_nr);
1479
1480         if (TAPE_IS_EMPTY(tape))
1481           Request("No tape for this level !", REQ_CONFIRM);
1482       }
1483       else
1484       {
1485         if (tape.changed)
1486           SaveTapeChecked(tape.level_nr);
1487
1488         TapeErase();
1489       }
1490
1491       DrawCompleteVideoDisplay();
1492       break;
1493
1494     case TAPE_CTRL_ID_EXTRA:
1495       if (tape.playing)
1496       {
1497         if (!tape.warp_forward)                 /* PLAY -> WARP FORWARD PLAY */
1498         {
1499           TapeStartWarpForward();
1500         }
1501         else                                    /* WARP FORWARD PLAY -> PLAY */
1502         {
1503           TapeStopWarpForward();
1504         }
1505       }
1506       else if (tape.recording)
1507         TapeSingleStep();
1508
1509       break;
1510
1511     case TAPE_CTRL_ID_STOP:
1512       TapeStop();
1513       break;
1514
1515     case TAPE_CTRL_ID_PAUSE:
1516       TapeTogglePause(TAPE_TOGGLE_MANUAL);
1517       break;
1518
1519     case TAPE_CTRL_ID_RECORD:
1520       if (TAPE_IS_STOPPED(tape))
1521         TapeStartGameRecording();
1522       else if (tape.pausing)
1523       {
1524         if (tape.playing)                       /* PLAY -> PAUSE -> RECORD */
1525           TapeAppendRecording();
1526         else
1527           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1528       }
1529       break;
1530
1531     case TAPE_CTRL_ID_PLAY:
1532       if (TAPE_IS_EMPTY(tape))
1533         break;
1534
1535       if (TAPE_IS_STOPPED(tape))
1536       {
1537         TapeStartGamePlaying();
1538       }
1539       else if (tape.playing)
1540       {
1541         if (tape.pausing)                       /* PAUSE -> PLAY */
1542         {
1543           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1544         }
1545         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
1546         {
1547           tape.fast_forward = TRUE;
1548           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
1549         }
1550         else if (!tape.pause_before_death)      /* FFWD PLAY -> AUTO PAUSE */
1551         {
1552           tape.pause_before_death = TRUE;
1553           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_ON, 0);
1554         }
1555         else                                    /* AUTO PAUSE -> NORMAL PLAY */
1556         {
1557           if (tape.warp_forward)
1558             TapeStopWarpForward();
1559
1560           tape.fast_forward = FALSE;
1561           tape.pause_before_death = FALSE;
1562
1563           DrawVideoDisplay(VIDEO_STATE_PBEND_OFF | VIDEO_STATE_PLAY_ON, 0);
1564         }
1565       }
1566       break;
1567
1568     default:
1569       break;
1570   }
1571 }
1572
1573 static void HandleTapeButtons(struct GadgetInfo *gi)
1574 {
1575   HandleTapeButtonsExt(gi->custom_id);
1576 }
1577
1578 void HandleTapeButtonKeys(Key key)
1579 {
1580   boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
1581   boolean extra_button_is_active = !eject_button_is_active;
1582
1583   if (key == setup.shortcut.tape_eject && eject_button_is_active)
1584     HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
1585   else if (key == setup.shortcut.tape_extra && extra_button_is_active)
1586     HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
1587   else if (key == setup.shortcut.tape_stop)
1588     HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
1589   else if (key == setup.shortcut.tape_pause)
1590     HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
1591   else if (key == setup.shortcut.tape_record)
1592     HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
1593   else if (key == setup.shortcut.tape_play)
1594     HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);
1595 }