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