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