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