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