renamed variable
[rocksndiamonds.git] / src / tape.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // tape.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "tape.h"
15 #include "init.h"
16 #include "game.h"
17 #include "tools.h"
18 #include "files.h"
19 #include "network.h"
20 #include "anim.h"
21
22 #define DEBUG_TAPE_WHEN_PLAYING                 FALSE
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                                          VIDEO_STATE_FRAME_OFF)
144 #define VIDEO_STATE_OFF                 (VIDEO_STATE_PLAY_OFF   |       \
145                                          VIDEO_STATE_REC_OFF    |       \
146                                          VIDEO_STATE_PAUSE_OFF  |       \
147                                          VIDEO_STATE_FFWD_OFF   |       \
148                                          VIDEO_STATE_PBEND_OFF  |       \
149                                          VIDEO_STATE_1STEP_OFF)
150 #define VIDEO_PRESS_OFF                 (VIDEO_PRESS_PLAY_OFF   |       \
151                                          VIDEO_PRESS_REC_OFF    |       \
152                                          VIDEO_PRESS_PAUSE_OFF  |       \
153                                          VIDEO_PRESS_STOP_OFF   |       \
154                                          VIDEO_PRESS_EJECT_OFF)
155 #define VIDEO_ALL_OFF                   (VIDEO_INFO_OFF         |       \
156                                          VIDEO_STATE_OFF        |       \
157                                          VIDEO_PRESS_OFF)
158
159 #define VIDEO_INFO_ON                   (VIDEO_STATE_DATE_ON    |       \
160                                          VIDEO_STATE_TIME_ON    |       \
161                                          VIDEO_STATE_FRAME_ON)
162 #define VIDEO_STATE_ON                  (VIDEO_STATE_PLAY_ON    |       \
163                                          VIDEO_STATE_REC_ON     |       \
164                                          VIDEO_STATE_PAUSE_ON   |       \
165                                          VIDEO_STATE_FFWD_ON    |       \
166                                          VIDEO_STATE_PBEND_ON   |       \
167                                          VIDEO_STATE_1STEP_ON)
168 #define VIDEO_PRESS_ON                  (VIDEO_PRESS_PLAY_ON    |       \
169                                          VIDEO_PRESS_REC_ON     |       \
170                                          VIDEO_PRESS_PAUSE_ON   |       \
171                                          VIDEO_PRESS_STOP_ON    |       \
172                                          VIDEO_PRESS_EJECT_ON)
173 #define VIDEO_ALL_ON                    (VIDEO_INFO_ON          |       \
174                                          VIDEO_STATE_ON         |       \
175                                          VIDEO_PRESS_ON)
176
177 #define VIDEO_INFO                      (VIDEO_INFO_ON | VIDEO_INFO_OFF)
178 #define VIDEO_STATE                     (VIDEO_STATE_ON | VIDEO_STATE_OFF)
179 #define VIDEO_PRESS                     (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
180 #define VIDEO_ALL                       (VIDEO_ALL_ON | VIDEO_ALL_OFF)
181
182 #define NUM_TAPE_FUNCTIONS              11
183 #define NUM_TAPE_FUNCTION_PARTS         2
184 #define NUM_TAPE_FUNCTION_STATES        2
185
186
187 /* ========================================================================= */
188 /* video display functions                                                   */
189 /* ========================================================================= */
190
191 static void DrawVideoDisplay_Graphics(unsigned int state, unsigned int value)
192 {
193   int i, j, k;
194
195   static struct
196   {
197     int graphic;
198     struct XY *pos;
199   }
200   video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
201   {
202     {
203       { IMG_GFX_TAPE_LABEL_PLAY,                &tape.label.play        },
204       { IMG_GFX_TAPE_SYMBOL_PLAY,               &tape.symbol.play       },
205     },
206     {
207       { IMG_GFX_TAPE_LABEL_RECORD,              &tape.label.record      },
208       { IMG_GFX_TAPE_SYMBOL_RECORD,             &tape.symbol.record     },
209     },
210     {
211       { IMG_GFX_TAPE_LABEL_PAUSE,               &tape.label.pause       },
212       { IMG_GFX_TAPE_SYMBOL_PAUSE,              &tape.symbol.pause      },
213     },
214     {
215       { IMG_GFX_TAPE_LABEL_DATE,                &tape.label.date        },
216       { -1,                                     NULL                    },
217     },
218     {
219       { IMG_GFX_TAPE_LABEL_TIME,                &tape.label.time        },
220       { -1,                                     NULL                    },
221     },
222     {
223       /* (no label for displaying optional frame) */
224       { -1,                                     NULL                    },
225       { -1,                                     NULL                    },
226     },
227     {
228       { IMG_GFX_TAPE_LABEL_FAST_FORWARD,        &tape.label.fast_forward  },
229       { IMG_GFX_TAPE_SYMBOL_FAST_FORWARD,       &tape.symbol.fast_forward },
230     },
231     {
232       { IMG_GFX_TAPE_LABEL_PAUSE_BEFORE_END,    &tape.label.pause_before_end  },
233       { IMG_GFX_TAPE_SYMBOL_PAUSE_BEFORE_END,   &tape.symbol.pause_before_end },
234     },
235     {
236       { IMG_GFX_TAPE_LABEL_WARP_FORWARD_BLIND,  &tape.label.warp_forward_blind},
237       { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD_BLIND, &tape.symbol.warp_forward_blind},
238     },
239     {
240       { IMG_GFX_TAPE_LABEL_WARP_FORWARD,        &tape.label.warp_forward  },
241       { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD,       &tape.symbol.warp_forward },
242     },
243     {
244       { IMG_GFX_TAPE_LABEL_SINGLE_STEP,         &tape.label.single_step  },
245       { IMG_GFX_TAPE_SYMBOL_SINGLE_STEP,        &tape.symbol.single_step },
246     },
247   };
248
249   for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++)        /* on or off states */
250   {
251     for (i = 0; i < NUM_TAPE_FUNCTIONS; i++)            /* record, play, ... */
252     {
253       for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++)     /* label or symbol */
254       {
255         int graphic = video_pos[i][j].graphic;
256         struct XY *pos = video_pos[i][j].pos;
257
258         if (graphic == -1 ||
259             pos->x == -1 ||
260             pos->y == -1)
261           continue;
262
263         if (state & (1 << (i * 2 + k)))
264         {
265           struct GraphicInfo *gfx_bg = &graphic_info[IMG_BACKGROUND_TAPE];
266           struct GraphicInfo *gfx = &graphic_info[graphic];
267           Bitmap *gd_bitmap;
268           int gd_x, gd_y;
269           int skip_value =
270             (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
271
272           if (value == skip_value)
273             continue;
274
275           if (k == 1)           /* on */
276           {
277             gd_bitmap = gfx->bitmap;
278             gd_x = gfx->src_x;
279             gd_y = gfx->src_y;
280           }
281           else                  /* off */
282           {
283             gd_bitmap = gfx_bg->bitmap;
284             gd_x = gfx_bg->src_x + pos->x;
285             gd_y = gfx_bg->src_y + pos->y;
286           }
287
288           /* some tape graphics may be undefined -- only draw if defined */
289           if (gd_bitmap != NULL)
290             BlitBitmap(gd_bitmap, drawto, gd_x, gd_y, gfx->width, gfx->height,
291                        VX + pos->x, VY + pos->y);
292
293           redraw_mask |= REDRAW_DOOR_2;
294         }
295       }
296     }
297   }
298 }
299
300
301 #define DATETIME_NONE                   (0)
302
303 #define DATETIME_DATE_YYYY              (1 << 0)
304 #define DATETIME_DATE_YY                (1 << 1)
305 #define DATETIME_DATE_MON               (1 << 2)
306 #define DATETIME_DATE_MM                (1 << 3)
307 #define DATETIME_DATE_DD                (1 << 4)
308
309 #define DATETIME_TIME_HH                (1 << 5)
310 #define DATETIME_TIME_MIN               (1 << 6)
311 #define DATETIME_TIME_MM                (1 << 7)
312 #define DATETIME_TIME_SS                (1 << 8)
313
314 #define DATETIME_FRAME                  (1 << 9)
315
316 #define DATETIME_XOFFSET_1              (1 << 10)
317 #define DATETIME_XOFFSET_2              (1 << 11)
318
319 #define DATETIME_DATE                   (DATETIME_DATE_YYYY     |       \
320                                          DATETIME_DATE_YY       |       \
321                                          DATETIME_DATE_MON      |       \
322                                          DATETIME_DATE_MM       |       \
323                                          DATETIME_DATE_DD)
324
325 #define DATETIME_TIME                   (DATETIME_TIME_HH       |       \
326                                          DATETIME_TIME_MIN      |       \
327                                          DATETIME_TIME_MM       |       \
328                                          DATETIME_TIME_SS)
329
330 #define MAX_DATETIME_STRING_SIZE        32
331
332 static void DrawVideoDisplay_DateTime(unsigned int state, unsigned int value)
333 {
334   int i;
335
336   static char *month_shortnames[] =
337   {
338     "JAN",
339     "FEB",
340     "MAR",
341     "APR",
342     "MAY",
343     "JUN",
344     "JUL",
345     "AUG",
346     "SEP",
347     "OCT",
348     "NOV",
349     "DEC"
350   };
351
352   static struct
353   {
354     struct TextPosInfo *pos;
355     int type;
356   }
357   datetime_info[] =
358   {
359     { &tape.text.date,          DATETIME_DATE_DD                        },
360     { &tape.text.date,          DATETIME_DATE_MON | DATETIME_XOFFSET_1  },
361     { &tape.text.date,          DATETIME_DATE_YY  | DATETIME_XOFFSET_2  },
362     { &tape.text.date_yyyy,     DATETIME_DATE_YYYY                      },
363     { &tape.text.date_yy,       DATETIME_DATE_YY                        },
364     { &tape.text.date_mon,      DATETIME_DATE_MON                       },
365     { &tape.text.date_mm,       DATETIME_DATE_MM                        },
366     { &tape.text.date_dd,       DATETIME_DATE_DD                        },
367
368     { &tape.text.time,          DATETIME_TIME_MIN                       },
369     { &tape.text.time,          DATETIME_TIME_SS  | DATETIME_XOFFSET_1  },
370     { &tape.text.time_hh,       DATETIME_TIME_HH                        },
371     { &tape.text.time_mm,       DATETIME_TIME_MM                        },
372     { &tape.text.time_ss,       DATETIME_TIME_SS                        },
373
374     { &tape.text.frame,         DATETIME_FRAME                          },
375
376     { NULL,                     DATETIME_NONE                           },
377   };
378
379   for (i = 0; datetime_info[i].pos != NULL; i++)
380   {
381     struct TextPosInfo *pos = datetime_info[i].pos;
382     int type = datetime_info[i].type;
383     int xpos, ypos;
384
385     if (pos->x == -1 &&
386         pos->y == -1)
387       continue;
388
389     xpos = VX + pos->x + (type & DATETIME_XOFFSET_1 ? pos->xoffset  :
390                           type & DATETIME_XOFFSET_2 ? pos->xoffset2 : 0);
391     ypos = VY + pos->y;
392
393     if ((type & DATETIME_DATE) && (state & VIDEO_STATE_DATE_ON))
394     {
395       char s[MAX_DATETIME_STRING_SIZE];
396       int year2 = value / 10000;
397       int year4 = (year2 < 70 ? 2000 + year2 : 1900 + year2);
398       int month_index = (value / 100) % 100;
399       int month = month_index + 1;
400       int day = value % 100;
401
402       strcpy(s, (type & DATETIME_DATE_YYYY ? int2str(year4, 4) :
403                  type & DATETIME_DATE_YY   ? int2str(year2, 2) :
404                  type & DATETIME_DATE_MON  ? month_shortnames[month_index] :
405                  type & DATETIME_DATE_MM   ? int2str(month, 2) :
406                  type & DATETIME_DATE_DD   ? int2str(day, 2) : ""));
407
408       DrawText(xpos, ypos, s, pos->font);
409     }
410     else if ((type & DATETIME_TIME) && (state & VIDEO_STATE_TIME_ON))
411     {
412       char s[MAX_DATETIME_STRING_SIZE];
413       int hh = (value / 3600) % 100;
414       int min = value / 60;
415       int mm = (value / 60) % 60;
416       int ss = value % 60;
417
418       strcpy(s, (type & DATETIME_TIME_HH  ? int2str(hh, 2) :
419                  type & DATETIME_TIME_MIN ? int2str(min, 2) :
420                  type & DATETIME_TIME_MM  ? int2str(mm, 2) :
421                  type & DATETIME_TIME_SS  ? int2str(ss, 2) : ""));
422
423       DrawText(xpos, ypos, s, pos->font);
424     }
425     else if ((type & DATETIME_FRAME) && (state & VIDEO_STATE_FRAME_ON))
426     {
427       DrawText(xpos, ypos, int2str(value, pos->size), pos->font);
428     }
429   }
430 }
431
432 void DrawVideoDisplay(unsigned int state, unsigned int value)
433 {
434   DrawVideoDisplay_Graphics(state, value);
435   DrawVideoDisplay_DateTime(state, value);
436 }
437
438 void DrawVideoDisplayLabel(unsigned int state)
439 {
440   DrawVideoDisplay(state, VIDEO_DISPLAY_LABEL_ONLY);
441 }
442
443 void DrawVideoDisplaySymbol(unsigned int state)
444 {
445   DrawVideoDisplay(state, VIDEO_DISPLAY_SYMBOL_ONLY);
446 }
447
448 void DrawCompleteVideoDisplay()
449 {
450   struct GraphicInfo *g_tape = &graphic_info[IMG_BACKGROUND_TAPE];
451
452   /* draw tape background */
453   BlitBitmap(g_tape->bitmap, drawto, g_tape->src_x, g_tape->src_y,
454              gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
455
456   /* draw tape buttons (forced) */
457   UnmapTapeButtons();
458   MapTapeButtons();
459
460   DrawVideoDisplay(VIDEO_ALL_OFF, 0);
461
462   if (tape.recording)
463   {
464     DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
465     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
466     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
467     DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
468
469     if (tape.pausing)
470       DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
471   }
472   else if (tape.playing)
473   {
474     DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
475     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
476     DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
477     DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
478
479     if (tape.pausing)
480       DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
481   }
482   else if (tape.date && tape.length)
483   {
484     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
485     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
486     DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
487   }
488
489   BlitBitmap(drawto, bitmap_db_door_2, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
490              0, 0);
491 }
492
493 void TapeDeactivateDisplayOn()
494 {
495   SetDrawDeactivationMask(REDRAW_FIELD);
496   audio.sound_deactivated = TRUE;
497 }
498
499 void TapeDeactivateDisplayOff(boolean redraw_display)
500 {
501   SetDrawDeactivationMask(REDRAW_NONE);
502   audio.sound_deactivated = FALSE;
503
504   if (redraw_display)
505   {
506     RedrawPlayfield();
507     DrawGameDoorValues();
508   }
509 }
510
511
512 /* ========================================================================= */
513 /* tape control functions                                                    */
514 /* ========================================================================= */
515
516 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
517 {
518   struct tm *lt = localtime(&epoch_seconds);
519
520   tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
521 }
522
523 void TapeSetDateFromNow()
524 {
525   TapeSetDateFromEpochSeconds(time(NULL));
526 }
527
528 void TapeErase()
529 {
530   int i;
531
532   tape.counter = 0;
533   tape.length = 0;
534   tape.length_frames = 0;
535   tape.length_seconds = 0;
536
537   if (leveldir_current)
538     setString(&tape.level_identifier, leveldir_current->identifier);
539
540   tape.level_nr = level_nr;
541   tape.pos[tape.counter].delay = 0;
542   tape.changed = TRUE;
543
544   tape.random_seed = InitRND(level.random_seed);
545
546   tape.file_version = FILE_VERSION_ACTUAL;
547   tape.game_version = GAME_VERSION_ACTUAL;
548   tape.engine_version = level.game_version;
549
550   TapeSetDateFromNow();
551
552   for (i = 0; i < MAX_PLAYERS; i++)
553     tape.player_participates[i] = FALSE;
554
555   tape.centered_player_nr_next = -1;
556   tape.set_centered_player = FALSE;
557 }
558
559 static void TapeRewind()
560 {
561   tape.counter = 0;
562   tape.delay_played = 0;
563   tape.pause_before_end = FALSE;
564   tape.recording = FALSE;
565   tape.playing = FALSE;
566   tape.fast_forward = FALSE;
567   tape.warp_forward = FALSE;
568   tape.deactivate_display = FALSE;
569   tape.auto_play = (global.autoplay_leveldir != NULL);
570   tape.auto_play_level_solved = FALSE;
571   tape.quick_resume = FALSE;
572   tape.single_step = FALSE;
573
574   tape.centered_player_nr_next = -1;
575   tape.set_centered_player = FALSE;
576
577   InitRND(tape.random_seed);
578 }
579
580 static void TapeSetRandomSeed(int random_seed)
581 {
582   tape.random_seed = InitRND(random_seed);
583 }
584
585 void TapeStartRecording(int random_seed)
586 {
587   if (!TAPE_IS_STOPPED(tape))
588     TapeStop();
589
590   TapeErase();
591   TapeRewind();
592   TapeSetRandomSeed(random_seed);
593
594   tape.recording = TRUE;
595
596   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
597   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
598   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
599   DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
600
601   MapTapeWarpButton();
602
603   SetDrawDeactivationMask(REDRAW_NONE);
604   audio.sound_deactivated = FALSE;
605 }
606
607 static void TapeStartGameRecording()
608 {
609   TapeStartRecording(level.random_seed);
610
611 #if defined(NETWORK_AVALIABLE)
612   if (options.network)
613   {
614     SendToServer_StartPlaying();
615
616     return;
617   }
618 #endif
619
620   InitGame();
621 }
622
623 static void TapeAppendRecording()
624 {
625   if (!tape.playing || !tape.pausing)
626     return;
627
628   // stop playing
629   tape.playing = FALSE;
630   tape.fast_forward = FALSE;
631   tape.warp_forward = FALSE;
632   tape.pause_before_end = FALSE;
633   tape.deactivate_display = FALSE;
634
635   // start recording
636   tape.recording = TRUE;
637   tape.changed = TRUE;
638
639   // set current delay (for last played move)
640   tape.pos[tape.counter].delay = tape.delay_played;
641
642   // set current date
643   TapeSetDateFromNow();
644
645   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
646   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
647
648   UpdateAndDisplayGameControlValues();
649 }
650
651 void TapeHaltRecording()
652 {
653   if (!tape.recording)
654     return;
655
656   tape.counter++;
657   tape.pos[tape.counter].delay = 0;
658
659   tape.length = tape.counter;
660   tape.length_frames = GetTapeLengthFrames();
661   tape.length_seconds = GetTapeLengthSeconds();
662 }
663
664 void TapeStopRecording()
665 {
666   TapeHaltRecording();
667
668   tape.recording = FALSE;
669   tape.pausing = FALSE;
670
671   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
672   MapTapeEjectButton();
673 }
674
675 void TapeRecordAction(byte action_raw[MAX_PLAYERS])
676 {
677   byte action[MAX_PLAYERS];
678   int i;
679
680   if (!tape.recording)          /* (record action even when tape is paused) */
681     return;
682
683   if (tape.counter >= MAX_TAPE_LEN - 1)
684   {
685     TapeStopRecording();
686     return;
687   }
688
689   for (i = 0; i < MAX_PLAYERS; i++)
690     action[i] = action_raw[i];
691
692   if (tape.set_centered_player)
693   {
694     for (i = 0; i < MAX_PLAYERS; i++)
695       if (tape.centered_player_nr_next == i ||
696           tape.centered_player_nr_next == -1)
697         action[i] |= KEY_SET_FOCUS;
698
699     tape.set_centered_player = FALSE;
700   }
701
702   if (tape.pos[tape.counter].delay > 0)         /* already stored action */
703   {
704     boolean changed_events = FALSE;
705
706     for (i = 0; i < MAX_PLAYERS; i++)
707       if (tape.pos[tape.counter].action[i] != action[i])
708         changed_events = TRUE;
709
710     if (changed_events || tape.pos[tape.counter].delay >= 255)
711     {
712       tape.counter++;
713       tape.pos[tape.counter].delay = 0;
714     }
715     else
716       tape.pos[tape.counter].delay++;
717   }
718
719   if (tape.pos[tape.counter].delay == 0)        /* store new action */
720   {
721     for (i = 0; i < MAX_PLAYERS; i++)
722       tape.pos[tape.counter].action[i] = action[i];
723
724     tape.pos[tape.counter].delay++;
725   }
726 }
727
728 void TapeTogglePause(boolean toggle_manual)
729 {
730   int state = 0;
731
732   tape.pausing = !tape.pausing;
733
734   if (tape.single_step && toggle_manual)
735     tape.single_step = FALSE;
736
737   state |= VIDEO_STATE_PAUSE(tape.pausing);
738
739   if (tape.pause_before_end)
740     state |= VIDEO_STATE_PBEND(!tape.pausing);
741   else if (tape.fast_forward)
742     state |= VIDEO_STATE_FFWD(!tape.pausing);
743
744   if (tape.playing)
745     state |= VIDEO_STATE_PLAY_ON;
746   else
747     state |= VIDEO_STATE_1STEP(tape.single_step);
748
749   DrawVideoDisplay(state, 0);
750
751   if (tape.warp_forward)
752   {
753     if (tape.pausing)
754     {
755       TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
756     }
757     else if (tape.deactivate_display)
758     {
759       TapeDeactivateDisplayOn();
760
761       DrawVideoDisplaySymbol(VIDEO_STATE_WARP_ON);
762     }
763     else
764     {
765       DrawVideoDisplaySymbol(VIDEO_STATE_WARP2_ON);
766     }
767
768     if (tape.quick_resume)
769     {
770       tape.quick_resume = FALSE;
771
772       TapeStopWarpForward();
773       TapeAppendRecording();
774
775       if (!CheckEngineSnapshotSingle())
776         SaveEngineSnapshotSingle();
777
778       // restart step/move snapshots after quick loading tape
779       SaveEngineSnapshotToListInitial();
780     }
781   }
782
783   if (setup.show_snapshot_buttons &&
784       game_status == GAME_MODE_PLAYING &&
785       CheckEngineSnapshotList())
786   {
787     if (tape.pausing)
788       MapUndoRedoButtons();
789     else if (!tape.single_step)
790       UnmapUndoRedoButtons();
791   }
792 }
793
794 void TapeStartPlaying()
795 {
796   if (TAPE_IS_EMPTY(tape))
797     return;
798
799   if (!TAPE_IS_STOPPED(tape))
800     TapeStop();
801
802   TapeRewind();
803
804   tape.playing = TRUE;
805
806   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
807   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
808   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
809   DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
810
811   MapTapeWarpButton();
812
813   SetDrawDeactivationMask(REDRAW_NONE);
814   audio.sound_deactivated = FALSE;
815 }
816
817 static void TapeStartGamePlaying()
818 {
819   TapeStartPlaying();
820
821   InitGame();
822 }
823
824 void TapeStopPlaying()
825 {
826   tape.playing = FALSE;
827   tape.pausing = FALSE;
828
829   if (tape.warp_forward)
830     TapeStopWarpForward();
831
832   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
833   MapTapeEjectButton();
834 }
835
836 byte *TapePlayAction()
837 {
838   int update_delay = FRAMES_PER_SECOND / 2;
839   boolean update_video_display = (FrameCounter % update_delay == 0);
840   boolean update_draw_label_on = ((FrameCounter / update_delay) % 2 == 1);
841   static byte action[MAX_PLAYERS];
842   int i;
843
844   if (!tape.playing || tape.pausing)
845     return NULL;
846
847   if (tape.pause_before_end)  // stop some seconds before end of tape
848   {
849     if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
850     {
851       tape.fast_forward = FALSE;
852       tape.pause_before_end = FALSE;
853
854       TapeStopWarpForward();
855       TapeTogglePause(TAPE_TOGGLE_MANUAL);
856
857       return NULL;
858     }
859   }
860
861   if (update_video_display && !tape.deactivate_display)
862   {
863     if (tape.pause_before_end)
864       DrawVideoDisplayLabel(VIDEO_STATE_PBEND(update_draw_label_on));
865     else if (tape.fast_forward)
866       DrawVideoDisplayLabel(VIDEO_STATE_FFWD(update_draw_label_on));
867
868     if (tape.warp_forward)
869       DrawVideoDisplaySymbol(VIDEO_STATE_WARP2_ON);
870   }
871
872   if (tape.counter >= tape.length)      /* end of tape reached */
873   {
874     if (tape.warp_forward && !tape.auto_play)
875       TapeTogglePause(TAPE_TOGGLE_MANUAL);
876     else
877       TapeStop();
878
879     return NULL;
880   }
881
882   for (i = 0; i < MAX_PLAYERS; i++)
883     action[i] = tape.pos[tape.counter].action[i];
884
885 #if DEBUG_TAPE_WHEN_PLAYING
886   printf("%05d", FrameCounter);
887   for (i = 0; i < MAX_PLAYERS; i++)
888     printf("   %08x", action[i]);
889   printf("\n");
890 #endif
891
892   tape.set_centered_player = FALSE;
893   tape.centered_player_nr_next = -999;
894
895   for (i = 0; i < MAX_PLAYERS; i++)
896   {
897     if (action[i] & KEY_SET_FOCUS)
898     {
899       tape.set_centered_player = TRUE;
900       tape.centered_player_nr_next =
901         (tape.centered_player_nr_next == -999 ? i : -1);
902     }
903
904     action[i] &= ~KEY_SET_FOCUS;
905   }
906
907   tape.delay_played++;
908   if (tape.delay_played >= tape.pos[tape.counter].delay)
909   {
910     tape.counter++;
911     tape.delay_played = 0;
912   }
913
914   return action;
915 }
916
917 void TapeStop()
918 {
919   TapeStopRecording();
920   TapeStopPlaying();
921
922   DrawVideoDisplay(VIDEO_STATE_OFF, 0);
923
924   if (tape.date && tape.length)
925   {
926     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
927     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
928     DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
929   }
930 }
931
932 unsigned int GetTapeLengthFrames()
933 {
934   unsigned int tape_length_frames = 0;
935   int i;
936
937   if (TAPE_IS_EMPTY(tape))
938     return(0);
939
940   for (i = 0; i < tape.length; i++)
941     tape_length_frames += tape.pos[i].delay;
942
943   return tape_length_frames;
944 }
945
946 unsigned int GetTapeLengthSeconds()
947 {
948   return (GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
949 }
950
951 static void TapeStartWarpForward()
952 {
953   tape.warp_forward = TRUE;
954
955   if (!tape.fast_forward && !tape.pause_before_end)
956   {
957     tape.pausing = FALSE;
958     tape.pause_before_end = TRUE;
959     tape.deactivate_display = TRUE;
960
961     TapeDeactivateDisplayOn();
962
963     DrawVideoDisplay(VIDEO_STATE_PBEND_ON, 0);
964     DrawVideoDisplaySymbol(VIDEO_STATE_WARP_ON);
965   }
966   else
967   {
968     DrawVideoDisplaySymbol(VIDEO_STATE_WARP2_ON);
969   }
970 }
971
972 static void TapeStopWarpForward()
973 {
974   int state = VIDEO_STATE_PAUSE(tape.pausing);
975
976   if (tape.deactivate_display)
977     tape.pause_before_end = FALSE;
978
979   tape.warp_forward = FALSE;
980   tape.deactivate_display = FALSE;
981
982   TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
983
984   state |= VIDEO_STATE_WARP_OFF;
985   state |= (tape.pause_before_end ? VIDEO_STATE_PBEND_ON :
986             tape.fast_forward     ? VIDEO_STATE_FFWD_ON :
987             VIDEO_STATE_PLAY_ON);
988
989   DrawVideoDisplay(state, 0);
990 }
991
992 static void TapeSingleStep()
993 {
994   if (options.network)
995     return;
996
997   if (!tape.pausing)
998     TapeTogglePause(TAPE_TOGGLE_MANUAL);
999
1000   tape.single_step = !tape.single_step;
1001
1002   DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
1003 }
1004
1005 void TapeQuickSave()
1006 {
1007   if (game_status == GAME_MODE_MAIN)
1008   {
1009     Request("No game that can be saved!", REQ_CONFIRM);
1010
1011     return;
1012   }
1013
1014   if (game_status != GAME_MODE_PLAYING)
1015     return;
1016
1017   if (tape.recording)
1018     TapeHaltRecording();        /* prepare tape for saving on-the-fly */
1019
1020   if (TAPE_IS_EMPTY(tape))
1021   {
1022     Request("No tape that can be saved!", REQ_CONFIRM);
1023
1024     return;
1025   }
1026
1027   if (SaveTapeChecked(tape.level_nr))
1028     SaveEngineSnapshotSingle();
1029 }
1030
1031 void TapeQuickLoad()
1032 {
1033   char *filename = getTapeFilename(level_nr);
1034
1035   if (!fileExists(filename))
1036   {
1037     Request("No tape for this level!", REQ_CONFIRM);
1038
1039     return;
1040   }
1041
1042   if (tape.recording && !Request("Stop recording and load tape?",
1043                                  REQ_ASK | REQ_STAY_CLOSED))
1044   {
1045     OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1046
1047     return;
1048   }
1049
1050   if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
1051     return;
1052
1053   if (CheckEngineSnapshotSingle())
1054   {
1055     TapeStartGamePlaying();
1056
1057     LoadEngineSnapshotSingle();
1058
1059     DrawCompleteVideoDisplay();
1060
1061     tape.playing = TRUE;
1062     tape.pausing = TRUE;
1063
1064     TapeStopWarpForward();
1065     TapeAppendRecording();
1066
1067     // restart step/move snapshots after quick loading tape
1068     SaveEngineSnapshotToListInitial();
1069
1070     if (FrameCounter > 0)
1071       return;
1072   }
1073
1074   TapeStop();
1075   TapeErase();
1076
1077   LoadTape(level_nr);
1078
1079   if (!TAPE_IS_EMPTY(tape))
1080   {
1081     TapeStartGamePlaying();
1082     TapeStartWarpForward();
1083
1084     tape.quick_resume = TRUE;
1085   }
1086   else  /* this should not happen (basically checked above) */
1087   {
1088     int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1089
1090     Request("No tape for this level!", REQ_CONFIRM | reopen_door);
1091   }
1092 }
1093
1094 void InsertSolutionTape()
1095 {
1096   if (!TAPE_IS_EMPTY(tape))
1097     return;
1098
1099   LoadSolutionTape(level_nr);
1100
1101   if (TAPE_IS_EMPTY(tape))
1102     Request("No solution tape for this level!", REQ_CONFIRM);
1103
1104   DrawCompleteVideoDisplay();
1105 }
1106
1107
1108 /* ------------------------------------------------------------------------- *
1109  * tape autoplay functions
1110  * ------------------------------------------------------------------------- */
1111
1112 void AutoPlayTape()
1113 {
1114   static LevelDirTree *autoplay_leveldir = NULL;
1115   static boolean autoplay_initialized = FALSE;
1116   static int autoplay_level_nr = -1;
1117   static int num_levels_played = 0;
1118   static int num_levels_solved = 0;
1119   static int num_tape_missing = 0;
1120   static boolean level_failed[MAX_TAPES_PER_SET];
1121   int i;
1122
1123   if (autoplay_initialized)
1124   {
1125     /* just finished auto-playing tape */
1126     printf("%s.\n", tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
1127
1128     num_levels_played++;
1129
1130     if (tape.auto_play_level_solved)
1131       num_levels_solved++;
1132     else if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1133       level_failed[level_nr] = TRUE;
1134   }
1135   else
1136   {
1137     DrawCompleteVideoDisplay();
1138
1139     audio.sound_enabled = FALSE;
1140     setup.engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_OFF);
1141
1142     autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1143                                                   global.autoplay_leveldir);
1144
1145     if (autoplay_leveldir == NULL)
1146       Error(ERR_EXIT, "no such level identifier: '%s'",
1147             global.autoplay_leveldir);
1148
1149     leveldir_current = autoplay_leveldir;
1150
1151     if (autoplay_leveldir->first_level < 0)
1152       autoplay_leveldir->first_level = 0;
1153     if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
1154       autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1155
1156     autoplay_level_nr = autoplay_leveldir->first_level;
1157
1158     printf_line("=", 79);
1159     printf("Automatically playing level tapes\n");
1160     printf_line("-", 79);
1161     printf("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
1162     printf("Level series name:       '%s'\n", autoplay_leveldir->name);
1163     printf("Level series author:     '%s'\n", autoplay_leveldir->author);
1164     printf("Number of levels:        %d\n",   autoplay_leveldir->levels);
1165     printf_line("=", 79);
1166     printf("\n");
1167
1168     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1169       level_failed[i] = FALSE;
1170
1171     autoplay_initialized = TRUE;
1172   }
1173
1174   while (autoplay_level_nr <= autoplay_leveldir->last_level)
1175   {
1176     level_nr = autoplay_level_nr++;
1177
1178     if (!global.autoplay_all && !global.autoplay_level[level_nr])
1179       continue;
1180
1181     TapeErase();
1182
1183     printf("Level %03d: ", level_nr);
1184
1185     LoadLevel(level_nr);
1186     if (level.no_valid_file)
1187     {
1188       printf("(no level)\n");
1189       continue;
1190     }
1191
1192 #if 0
1193     /* ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY */
1194     printf("(only testing level)\n");
1195     continue;
1196 #endif
1197
1198     LoadSolutionTape(level_nr);
1199
1200     if (tape.no_valid_file)
1201     {
1202       num_tape_missing++;
1203
1204       printf("(no tape)\n");
1205
1206       continue;
1207     }
1208
1209     printf("playing tape ... ");
1210
1211     TapeStartGamePlaying();
1212
1213     if (global.autoplay_mode == AUTOPLAY_FFWD)
1214       tape.fast_forward = TRUE;
1215
1216     if (global.autoplay_mode != AUTOPLAY_PLAY)
1217       TapeStartWarpForward();
1218
1219     return;
1220   }
1221
1222   printf("\n");
1223   printf_line("=", 79);
1224   printf("Number of levels played: %d\n", num_levels_played);
1225   printf("Number of levels solved: %d (%d%%)\n", num_levels_solved,
1226          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1227   printf_line("-", 79);
1228   printf("Summary (for automatic parsing by scripts):\n");
1229   printf("LEVELDIR '%s', SOLVED %d/%d (%d%%)",
1230          autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
1231          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1232
1233   if (num_levels_played != num_levels_solved)
1234   {
1235     printf(", FAILED:");
1236     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1237       if (level_failed[i])
1238         printf(" %03d", i);
1239   }
1240
1241   printf("\n");
1242   printf_line("=", 79);
1243
1244   CloseAllAndExit(0);
1245 }
1246
1247
1248 /* ---------- new tape button stuff ---------------------------------------- */
1249
1250 static struct
1251 {
1252   int graphic;
1253   struct XY *pos;
1254   int gadget_id;
1255   char *infotext;
1256 } tapebutton_info[NUM_TAPE_BUTTONS] =
1257 {
1258   {
1259     IMG_GFX_TAPE_BUTTON_EJECT,          &tape.button.eject,
1260     TAPE_CTRL_ID_EJECT,                 "eject tape"
1261   },
1262   {
1263     /* (same position as "eject" button) */
1264     IMG_GFX_TAPE_BUTTON_EXTRA,          &tape.button.eject,
1265     TAPE_CTRL_ID_EXTRA,                 "extra functions"
1266   },
1267   {
1268     IMG_GFX_TAPE_BUTTON_STOP,           &tape.button.stop,
1269     TAPE_CTRL_ID_STOP,                  "stop tape"
1270   },
1271   {
1272     IMG_GFX_TAPE_BUTTON_PAUSE,          &tape.button.pause,
1273     TAPE_CTRL_ID_PAUSE,                 "pause tape"
1274   },
1275   {
1276     IMG_GFX_TAPE_BUTTON_RECORD,         &tape.button.record,
1277     TAPE_CTRL_ID_RECORD,                "record tape"
1278   },
1279   {
1280     IMG_GFX_TAPE_BUTTON_PLAY,           &tape.button.play,
1281     TAPE_CTRL_ID_PLAY,                  "play tape"
1282   }
1283 };
1284
1285 void CreateTapeButtons()
1286 {
1287   int i;
1288
1289   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1290   {
1291     struct GraphicInfo *gfx = &graphic_info[tapebutton_info[i].graphic];
1292     struct XY *pos = tapebutton_info[i].pos;
1293     struct GadgetInfo *gi;
1294     int gd_x = gfx->src_x;
1295     int gd_y = gfx->src_y;
1296     int gd_xp = gfx->src_x + gfx->pressed_xoffset;
1297     int gd_yp = gfx->src_y + gfx->pressed_yoffset;
1298     int id = i;
1299
1300     gi = CreateGadget(GDI_CUSTOM_ID, id,
1301                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
1302                       GDI_X, VX + pos->x,
1303                       GDI_Y, VY + pos->y,
1304                       GDI_WIDTH, gfx->width,
1305                       GDI_HEIGHT, gfx->height,
1306                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
1307                       GDI_STATE, GD_BUTTON_UNPRESSED,
1308                       GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
1309                       GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
1310                       GDI_DIRECT_DRAW, FALSE,
1311                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
1312                       GDI_CALLBACK_ACTION, HandleTapeButtons,
1313                       GDI_END);
1314
1315     if (gi == NULL)
1316       Error(ERR_EXIT, "cannot create gadget");
1317
1318     tape_gadget[id] = gi;
1319   }
1320 }
1321
1322 void FreeTapeButtons()
1323 {
1324   int i;
1325
1326   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1327     FreeGadget(tape_gadget[i]);
1328 }
1329
1330 void MapTapeEjectButton()
1331 {
1332   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1333   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1334 }
1335
1336 void MapTapeWarpButton()
1337 {
1338   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1339   MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1340 }
1341
1342 void MapTapeButtons()
1343 {
1344   int i;
1345
1346   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1347     if (i != TAPE_CTRL_ID_EXTRA)
1348       MapGadget(tape_gadget[i]);
1349
1350   if (tape.recording || tape.playing)
1351     MapTapeWarpButton();
1352
1353   if (tape.show_game_buttons)
1354     MapGameButtons();
1355 }
1356
1357 void UnmapTapeButtons()
1358 {
1359   int i;
1360
1361   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1362     UnmapGadget(tape_gadget[i]);
1363
1364   if (tape.show_game_buttons)
1365     UnmapGameButtons();
1366 }
1367
1368 static void HandleTapeButtonsExt(int id)
1369 {
1370   if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
1371     return;
1372
1373   switch (id)
1374   {
1375     case TAPE_CTRL_ID_EJECT:
1376       TapeStop();
1377
1378       if (TAPE_IS_EMPTY(tape))
1379       {
1380         LoadTape(level_nr);
1381
1382         if (TAPE_IS_EMPTY(tape))
1383           Request("No tape for this level!", REQ_CONFIRM);
1384       }
1385       else
1386       {
1387         if (tape.changed)
1388           SaveTapeChecked(tape.level_nr);
1389
1390         TapeErase();
1391       }
1392
1393       DrawCompleteVideoDisplay();
1394       break;
1395
1396     case TAPE_CTRL_ID_EXTRA:
1397       if (tape.playing)
1398       {
1399         if (!tape.warp_forward)                 /* PLAY -> WARP FORWARD PLAY */
1400         {
1401           TapeStartWarpForward();
1402         }
1403         else if (tape.pausing)                  /* PAUSE -> WARP FORWARD PLAY */
1404         {
1405           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1406         }
1407         else                                    /* WARP FORWARD PLAY -> PLAY */
1408         {
1409           TapeStopWarpForward();
1410         }
1411       }
1412       else if (tape.recording)
1413         TapeSingleStep();
1414
1415       break;
1416
1417     case TAPE_CTRL_ID_STOP:
1418       TapeStop();
1419       break;
1420
1421     case TAPE_CTRL_ID_PAUSE:
1422       TapeTogglePause(TAPE_TOGGLE_MANUAL);
1423       break;
1424
1425     case TAPE_CTRL_ID_RECORD:
1426       if (TAPE_IS_STOPPED(tape))
1427         TapeStartGameRecording();
1428       else if (tape.pausing)
1429       {
1430         if (tape.playing)                       /* PLAY -> PAUSE -> RECORD */
1431           TapeAppendRecording();
1432         else
1433           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1434       }
1435       break;
1436
1437     case TAPE_CTRL_ID_PLAY:
1438       if (tape.recording && tape.pausing)       /* PAUSE -> RECORD */
1439       {
1440         // ("TAPE_IS_EMPTY(tape)" is TRUE here -- probably fix this)
1441
1442         TapeTogglePause(TAPE_TOGGLE_MANUAL);
1443       }
1444
1445       if (TAPE_IS_EMPTY(tape))
1446         break;
1447
1448       if (TAPE_IS_STOPPED(tape))
1449       {
1450         TapeStartGamePlaying();
1451       }
1452       else if (tape.playing)
1453       {
1454         if (tape.pausing)                       /* PAUSE -> PLAY */
1455         {
1456           // continue playing in normal mode
1457           tape.fast_forward = FALSE;
1458           tape.warp_forward = FALSE;
1459           tape.pause_before_end = FALSE;
1460           tape.deactivate_display = FALSE;
1461
1462           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1463         }
1464         else if (tape.warp_forward &&
1465                  !tape.fast_forward)            /* WARP FORWARD PLAY -> PLAY */
1466         {
1467           TapeStopWarpForward();
1468         }
1469         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
1470         {
1471           tape.fast_forward = TRUE;
1472
1473           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
1474         }
1475         else if (!tape.pause_before_end)        /* FFWD PLAY -> AUTO PAUSE */
1476         {
1477           tape.pause_before_end = TRUE;
1478
1479           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_ON, 0);
1480
1481           if (tape.warp_forward)
1482             DrawVideoDisplaySymbol(VIDEO_STATE_WARP2_ON);
1483         }
1484         else                                    /* AUTO PAUSE -> NORMAL PLAY */
1485         {
1486           if (tape.warp_forward)
1487             TapeStopWarpForward();
1488
1489           tape.fast_forward = FALSE;
1490           tape.pause_before_end = FALSE;
1491
1492           DrawVideoDisplay(VIDEO_STATE_PBEND_OFF | VIDEO_STATE_PLAY_ON, 0);
1493         }
1494       }
1495       break;
1496
1497     default:
1498       break;
1499   }
1500 }
1501
1502 static void HandleTapeButtons(struct GadgetInfo *gi)
1503 {
1504   HandleTapeButtonsExt(gi->custom_id);
1505 }
1506
1507 void HandleTapeButtonKeys(Key key)
1508 {
1509   boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
1510   boolean extra_button_is_active = !eject_button_is_active;
1511
1512   if (key == setup.shortcut.tape_eject && eject_button_is_active)
1513     HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
1514   else if (key == setup.shortcut.tape_extra && extra_button_is_active)
1515     HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
1516   else if (key == setup.shortcut.tape_stop)
1517     HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
1518   else if (key == setup.shortcut.tape_pause)
1519     HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
1520   else if (key == setup.shortcut.tape_record)
1521     HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
1522   else if (key == setup.shortcut.tape_play)
1523     HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);
1524 }