added command to play tape and upload resulting score to server
[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 //                  https://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 #define TAPE_CTRL_ID_INSERT_SOLUTION            6
32 #define TAPE_CTRL_ID_PLAY_SOLUTION              7
33
34 #define NUM_TAPE_BUTTONS                        8
35
36 // values for tape handling
37 #define TAPE_PAUSE_SECONDS_BEFORE_DEATH         5
38 #define TAPE_MIN_SECONDS_FOR_UNDO_BUFFER        20
39
40 // forward declaration for internal use
41 static void HandleTapeButtons(struct GadgetInfo *);
42 static void TapeStopWarpForward(void);
43 static float GetTapeLengthSecondsFloat(void);
44 static void CopyTapeToUndoBuffer(void);
45
46 static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
47 static struct TapeInfo tape_undo_buffer;
48
49
50 // ============================================================================
51 // video tape definitions
52 // ============================================================================
53
54 #define VIDEO_INFO_OFF                  (VIDEO_STATE_DATE_OFF   |       \
55                                          VIDEO_STATE_TIME_OFF   |       \
56                                          VIDEO_STATE_FRAME_OFF)
57 #define VIDEO_STATE_OFF                 (VIDEO_STATE_PLAY_OFF   |       \
58                                          VIDEO_STATE_REC_OFF    |       \
59                                          VIDEO_STATE_PAUSE_OFF  |       \
60                                          VIDEO_STATE_FFWD_OFF   |       \
61                                          VIDEO_STATE_WARP_OFF   |       \
62                                          VIDEO_STATE_WARP2_OFF  |       \
63                                          VIDEO_STATE_PBEND_OFF  |       \
64                                          VIDEO_STATE_1STEP_OFF)
65 #define VIDEO_PRESS_OFF                 (VIDEO_PRESS_PLAY_OFF   |       \
66                                          VIDEO_PRESS_REC_OFF    |       \
67                                          VIDEO_PRESS_PAUSE_OFF  |       \
68                                          VIDEO_PRESS_STOP_OFF   |       \
69                                          VIDEO_PRESS_EJECT_OFF)
70 #define VIDEO_ALL_OFF                   (VIDEO_INFO_OFF         |       \
71                                          VIDEO_STATE_OFF        |       \
72                                          VIDEO_PRESS_OFF)
73
74 #define VIDEO_INFO_ON                   (VIDEO_STATE_DATE_ON    |       \
75                                          VIDEO_STATE_TIME_ON    |       \
76                                          VIDEO_STATE_FRAME_ON)
77 #define VIDEO_STATE_ON                  (VIDEO_STATE_PLAY_ON    |       \
78                                          VIDEO_STATE_REC_ON     |       \
79                                          VIDEO_STATE_PAUSE_ON   |       \
80                                          VIDEO_STATE_FFWD_ON    |       \
81                                          VIDEO_STATE_WARP_ON    |       \
82                                          VIDEO_STATE_WARP2_ON   |       \
83                                          VIDEO_STATE_PBEND_ON   |       \
84                                          VIDEO_STATE_1STEP_ON)
85 #define VIDEO_PRESS_ON                  (VIDEO_PRESS_PLAY_ON    |       \
86                                          VIDEO_PRESS_REC_ON     |       \
87                                          VIDEO_PRESS_PAUSE_ON   |       \
88                                          VIDEO_PRESS_STOP_ON    |       \
89                                          VIDEO_PRESS_EJECT_ON)
90 #define VIDEO_ALL_ON                    (VIDEO_INFO_ON          |       \
91                                          VIDEO_STATE_ON         |       \
92                                          VIDEO_PRESS_ON)
93
94 #define VIDEO_INFO                      (VIDEO_INFO_ON | VIDEO_INFO_OFF)
95 #define VIDEO_STATE                     (VIDEO_STATE_ON | VIDEO_STATE_OFF)
96 #define VIDEO_PRESS                     (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
97 #define VIDEO_ALL                       (VIDEO_ALL_ON | VIDEO_ALL_OFF)
98
99 #define NUM_TAPE_FUNCTIONS              11
100 #define NUM_TAPE_FUNCTION_PARTS         2
101 #define NUM_TAPE_FUNCTION_STATES        2
102
103
104 // ============================================================================
105 // video display functions
106 // ============================================================================
107
108 static void DrawVideoDisplay_Graphics(unsigned int state, unsigned int value)
109 {
110   int i, j, k;
111
112   static struct
113   {
114     int graphic;
115     struct XY *pos;
116   }
117   video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
118   {
119     {
120       { IMG_GFX_TAPE_LABEL_PLAY,                &tape.label.play        },
121       { IMG_GFX_TAPE_SYMBOL_PLAY,               &tape.symbol.play       },
122     },
123     {
124       { IMG_GFX_TAPE_LABEL_RECORD,              &tape.label.record      },
125       { IMG_GFX_TAPE_SYMBOL_RECORD,             &tape.symbol.record     },
126     },
127     {
128       { IMG_GFX_TAPE_LABEL_PAUSE,               &tape.label.pause       },
129       { IMG_GFX_TAPE_SYMBOL_PAUSE,              &tape.symbol.pause      },
130     },
131     {
132       { IMG_GFX_TAPE_LABEL_DATE,                &tape.label.date        },
133       { -1,                                     NULL                    },
134     },
135     {
136       { IMG_GFX_TAPE_LABEL_TIME,                &tape.label.time        },
137       { -1,                                     NULL                    },
138     },
139     {
140       // (no label for displaying optional frame)
141       { -1,                                     NULL                    },
142       { -1,                                     NULL                    },
143     },
144     {
145       { IMG_GFX_TAPE_LABEL_FAST_FORWARD,        &tape.label.fast_forward  },
146       { IMG_GFX_TAPE_SYMBOL_FAST_FORWARD,       &tape.symbol.fast_forward },
147     },
148     {
149       { IMG_GFX_TAPE_LABEL_WARP_FORWARD,        &tape.label.warp_forward  },
150       { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD,       &tape.symbol.warp_forward },
151     },
152     {
153       { IMG_GFX_TAPE_LABEL_WARP_FORWARD_BLIND,  &tape.label.warp_forward_blind},
154       { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD_BLIND, &tape.symbol.warp_forward_blind},
155     },
156     {
157       { IMG_GFX_TAPE_LABEL_PAUSE_BEFORE_END,    &tape.label.pause_before_end  },
158       { IMG_GFX_TAPE_SYMBOL_PAUSE_BEFORE_END,   &tape.symbol.pause_before_end },
159     },
160     {
161       { IMG_GFX_TAPE_LABEL_SINGLE_STEP,         &tape.label.single_step  },
162       { IMG_GFX_TAPE_SYMBOL_SINGLE_STEP,        &tape.symbol.single_step },
163     },
164   };
165
166   for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++)        // on or off states
167   {
168     for (i = 0; i < NUM_TAPE_FUNCTIONS; i++)            // record, play, ...
169     {
170       for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++)     // label or symbol
171       {
172         int graphic = video_pos[i][j].graphic;
173         struct XY *pos = video_pos[i][j].pos;
174
175         if (graphic == -1 ||
176             pos->x == -1 ||
177             pos->y == -1)
178           continue;
179
180         if (state & (1 << (i * 2 + k)))
181         {
182           struct GraphicInfo *gfx_bg = &graphic_info[IMG_BACKGROUND_TAPE];
183           struct GraphicInfo *gfx = &graphic_info[graphic];
184           Bitmap *gd_bitmap;
185           int gd_x, gd_y;
186           int skip_value =
187             (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
188
189           if (value == skip_value)
190             continue;
191
192           if (k == 1)           // on
193           {
194             gd_bitmap = gfx->bitmap;
195             gd_x = gfx->src_x;
196             gd_y = gfx->src_y;
197           }
198           else                  // off
199           {
200             gd_bitmap = gfx_bg->bitmap;
201             gd_x = gfx_bg->src_x + pos->x;
202             gd_y = gfx_bg->src_y + pos->y;
203           }
204
205           // some tape graphics may be undefined -- only draw if defined
206           if (gd_bitmap != NULL)
207             BlitBitmap(gd_bitmap, drawto, gd_x, gd_y, gfx->width, gfx->height,
208                        VX + pos->x, VY + pos->y);
209
210           redraw_mask |= REDRAW_DOOR_2;
211         }
212       }
213     }
214   }
215 }
216
217
218 #define DATETIME_NONE                   (0)
219
220 #define DATETIME_DATE_YYYY              (1 << 0)
221 #define DATETIME_DATE_YY                (1 << 1)
222 #define DATETIME_DATE_MON               (1 << 2)
223 #define DATETIME_DATE_MM                (1 << 3)
224 #define DATETIME_DATE_DD                (1 << 4)
225
226 #define DATETIME_TIME_HH                (1 << 5)
227 #define DATETIME_TIME_MIN               (1 << 6)
228 #define DATETIME_TIME_MM                (1 << 7)
229 #define DATETIME_TIME_SS                (1 << 8)
230
231 #define DATETIME_FRAME                  (1 << 9)
232
233 #define DATETIME_XOFFSET_1              (1 << 10)
234 #define DATETIME_XOFFSET_2              (1 << 11)
235
236 #define DATETIME_DATE                   (DATETIME_DATE_YYYY     |       \
237                                          DATETIME_DATE_YY       |       \
238                                          DATETIME_DATE_MON      |       \
239                                          DATETIME_DATE_MM       |       \
240                                          DATETIME_DATE_DD)
241
242 #define DATETIME_TIME                   (DATETIME_TIME_HH       |       \
243                                          DATETIME_TIME_MIN      |       \
244                                          DATETIME_TIME_MM       |       \
245                                          DATETIME_TIME_SS)
246
247 #define MAX_DATETIME_STRING_SIZE        32
248
249 static void DrawVideoDisplay_DateTime(unsigned int state, unsigned int value)
250 {
251   int i;
252
253   static char *month_shortnames[] =
254   {
255     "JAN",
256     "FEB",
257     "MAR",
258     "APR",
259     "MAY",
260     "JUN",
261     "JUL",
262     "AUG",
263     "SEP",
264     "OCT",
265     "NOV",
266     "DEC"
267   };
268
269   static struct
270   {
271     struct TextPosInfo *pos;
272     int type;
273   }
274   datetime_info[] =
275   {
276     { &tape.text.date,          DATETIME_DATE_DD                        },
277     { &tape.text.date,          DATETIME_DATE_MON | DATETIME_XOFFSET_1  },
278     { &tape.text.date,          DATETIME_DATE_YY  | DATETIME_XOFFSET_2  },
279     { &tape.text.date_yyyy,     DATETIME_DATE_YYYY                      },
280     { &tape.text.date_yy,       DATETIME_DATE_YY                        },
281     { &tape.text.date_mon,      DATETIME_DATE_MON                       },
282     { &tape.text.date_mm,       DATETIME_DATE_MM                        },
283     { &tape.text.date_dd,       DATETIME_DATE_DD                        },
284
285     { &tape.text.time,          DATETIME_TIME_MIN                       },
286     { &tape.text.time,          DATETIME_TIME_SS  | DATETIME_XOFFSET_1  },
287     { &tape.text.time_hh,       DATETIME_TIME_HH                        },
288     { &tape.text.time_mm,       DATETIME_TIME_MM                        },
289     { &tape.text.time_ss,       DATETIME_TIME_SS                        },
290
291     { &tape.text.frame,         DATETIME_FRAME                          },
292
293     { NULL,                     DATETIME_NONE                           },
294   };
295
296   for (i = 0; datetime_info[i].pos != NULL; i++)
297   {
298     struct TextPosInfo *pos = datetime_info[i].pos;
299     int type = datetime_info[i].type;
300     int xpos, ypos;
301
302     if (pos->x == -1 &&
303         pos->y == -1)
304       continue;
305
306     xpos = VX + pos->x + (type & DATETIME_XOFFSET_1 ? pos->xoffset  :
307                           type & DATETIME_XOFFSET_2 ? pos->xoffset2 : 0);
308     ypos = VY + pos->y;
309
310     if ((type & DATETIME_DATE) && (state & VIDEO_STATE_DATE_ON))
311     {
312       char s[MAX_DATETIME_STRING_SIZE];
313       int year2 = value / 10000;
314       int year4 = (year2 < 70 ? 2000 + year2 : 1900 + year2);
315       int month_index = (value / 100) % 100;
316       int month = month_index + 1;
317       int day = value % 100;
318
319       strcpy(s, (type & DATETIME_DATE_YYYY ? int2str(year4, 4) :
320                  type & DATETIME_DATE_YY   ? int2str(year2, 2) :
321                  type & DATETIME_DATE_MON  ? month_shortnames[month_index] :
322                  type & DATETIME_DATE_MM   ? int2str(month, 2) :
323                  type & DATETIME_DATE_DD   ? int2str(day, 2) : ""));
324
325       DrawText(xpos, ypos, s, pos->font);
326     }
327     else if ((type & DATETIME_TIME) && (state & VIDEO_STATE_TIME_ON))
328     {
329       char s[MAX_DATETIME_STRING_SIZE];
330       int hh = (value / 3600) % 100;
331       int min = value / 60;
332       int mm = (value / 60) % 60;
333       int ss = value % 60;
334
335       strcpy(s, (type & DATETIME_TIME_HH  ? int2str(hh, 2) :
336                  type & DATETIME_TIME_MIN ? int2str(min, 2) :
337                  type & DATETIME_TIME_MM  ? int2str(mm, 2) :
338                  type & DATETIME_TIME_SS  ? int2str(ss, 2) : ""));
339
340       DrawText(xpos, ypos, s, pos->font);
341     }
342     else if ((type & DATETIME_FRAME) && (state & VIDEO_STATE_FRAME_ON))
343     {
344       DrawText(xpos, ypos, int2str(value, pos->size), pos->font);
345     }
346   }
347 }
348
349 void DrawVideoDisplay(unsigned int state, unsigned int value)
350 {
351   DrawVideoDisplay_Graphics(state, value);
352   DrawVideoDisplay_DateTime(state, value);
353 }
354
355 static void DrawVideoDisplayLabel(unsigned int state)
356 {
357   DrawVideoDisplay(state, VIDEO_DISPLAY_LABEL_ONLY);
358 }
359
360 static void DrawVideoDisplaySymbol(unsigned int state)
361 {
362   DrawVideoDisplay(state, VIDEO_DISPLAY_SYMBOL_ONLY);
363 }
364
365 static void DrawVideoDisplayCurrentState(void)
366 {
367   int state = 0;
368
369   DrawVideoDisplay(VIDEO_STATE_OFF, 0);
370
371   if (tape.pausing)
372     state |= VIDEO_STATE_PAUSE_ON;
373
374   if (tape.recording)
375   {
376     state |= VIDEO_STATE_REC_ON;
377
378     if (tape.single_step)
379       state |= VIDEO_STATE_1STEP_ON;
380   }
381   else if (tape.playing)
382   {
383     state |= VIDEO_STATE_PLAY_ON;
384
385     if (!tape.pausing)
386     {
387       if (tape.deactivate_display)
388         state |= VIDEO_STATE_WARP2_ON;
389       else if (tape.warp_forward)
390         state |= VIDEO_STATE_WARP_ON;
391       else if (tape.fast_forward)
392         state |= VIDEO_STATE_FFWD_ON;
393
394       if (tape.pause_before_end)
395         state |= VIDEO_STATE_PBEND_ON;
396     }
397   }
398
399   // draw labels and symbols separately to prevent labels overlapping symbols
400   DrawVideoDisplayLabel(state);
401   DrawVideoDisplaySymbol(state);
402 }
403
404 void DrawCompleteVideoDisplay(void)
405 {
406   struct GraphicInfo *g_tape = &graphic_info[IMG_BACKGROUND_TAPE];
407
408   // draw tape background
409   BlitBitmap(g_tape->bitmap, drawto, g_tape->src_x, g_tape->src_y,
410              gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
411
412   // draw tape buttons (forced)
413   RedrawOrRemapTapeButtons();
414
415   DrawVideoDisplay(VIDEO_ALL_OFF, 0);
416
417   if (tape.recording)
418   {
419     DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
420     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
421     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
422     DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
423
424     if (tape.pausing)
425       DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
426   }
427   else if (tape.playing)
428   {
429     DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
430     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
431     DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
432     DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
433
434     if (tape.pausing)
435       DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
436   }
437   else if (tape.date && tape.length)
438   {
439     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
440     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
441     DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
442   }
443
444   BlitBitmap(drawto, bitmap_db_door_2, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
445              0, 0);
446 }
447
448 void TapeDeactivateDisplayOn(void)
449 {
450   SetDrawDeactivationMask(REDRAW_FIELD);
451   audio.sound_deactivated = TRUE;
452 }
453
454 void TapeDeactivateDisplayOff(boolean redraw_display)
455 {
456   SetDrawDeactivationMask(REDRAW_NONE);
457   audio.sound_deactivated = FALSE;
458
459   if (redraw_display)
460   {
461     RedrawPlayfield();
462
463     UpdateGameDoorValues();
464     DrawGameDoorValues();
465   }
466 }
467
468
469 // ============================================================================
470 // tape logging functions
471 // ============================================================================
472
473 static char tape_patch_info[MAX_OUTPUT_LINESIZE];
474
475 static void PrintTapeReplayProgress(boolean replay_finished)
476 {
477   static unsigned int counter_last = -1;
478   unsigned int counter = Counter();
479   unsigned int counter_seconds  = counter / 1000;
480
481   if (!replay_finished)
482   {
483     unsigned int counter_delay = 50;
484
485     if (counter > counter_last + counter_delay)
486     {
487       PrintNoLog("\r");
488       PrintNoLog("Tape %03d %s[%02d:%02d]: [%02d:%02d] - playing tape ... ",
489                  level_nr,  tape_patch_info,
490                  tape.length_seconds / 60, tape.length_seconds % 60,
491                  TapeTime / 60, TapeTime % 60);
492
493       counter_last = counter;
494     }
495   }
496   else
497   {
498     float tape_length_seconds = GetTapeLengthSecondsFloat();
499
500     PrintNoLog("\r");
501     Print("Tape %03d %s[%02d:%02d]: (%02d:%02d.%03d / %.2f %%) - %s.\n",
502           level_nr, tape_patch_info,
503           tape.length_seconds / 60, tape.length_seconds % 60,
504           counter_seconds / 60, counter_seconds % 60, counter % 1000,
505           (float)counter / tape_length_seconds / 10,
506           tape.auto_play_level_fixed ? "solved and fixed" :
507           tape.auto_play_level_solved ? "solved" :
508           tape.auto_play_level_not_fixable ? "NOT SOLVED, NOT FIXED" :
509           "NOT SOLVED");
510
511     counter_last = -1;
512   }
513 }
514
515
516 // ============================================================================
517 // tape control functions
518 // ============================================================================
519
520 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
521 {
522   struct tm *lt = localtime(&epoch_seconds);
523
524   tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
525 }
526
527 void TapeSetDateFromNow(void)
528 {
529   TapeSetDateFromEpochSeconds(time(NULL));
530 }
531
532 void TapeErase(void)
533 {
534   int i;
535
536   CopyTapeToUndoBuffer();
537
538   tape.counter = 0;
539   tape.length = 0;
540   tape.length_frames = 0;
541   tape.length_seconds = 0;
542
543   tape.score_tape_basename[0] = '\0';
544
545   if (leveldir_current)
546   {
547     strncpy(tape.level_identifier, leveldir_current->identifier,
548             MAX_FILENAME_LEN);
549     tape.level_identifier[MAX_FILENAME_LEN] = '\0';
550   }
551
552   tape.level_nr = level_nr;
553   tape.pos[tape.counter].delay = 0;
554   tape.changed = TRUE;
555
556   tape.random_seed = InitRND(level.random_seed);
557
558   tape.file_version = FILE_VERSION_ACTUAL;
559   tape.game_version = GAME_VERSION_ACTUAL;
560   tape.engine_version = level.game_version;
561
562   TapeSetDateFromNow();
563
564   for (i = 0; i < MAX_PLAYERS; i++)
565     tape.player_participates[i] = FALSE;
566
567   tape.centered_player_nr_next = -1;
568   tape.set_centered_player = FALSE;
569
570   // set flags for game actions to default values (may be overwritten later)
571   tape.use_key_actions = TRUE;
572   tape.use_mouse_actions = FALSE;
573 }
574
575 static void TapeRewind(void)
576 {
577   tape.counter = 0;
578   tape.delay_played = 0;
579   tape.pause_before_end = FALSE;
580   tape.recording = FALSE;
581   tape.playing = FALSE;
582   tape.fast_forward = FALSE;
583   tape.warp_forward = FALSE;
584   tape.deactivate_display = FALSE;
585   tape.auto_play = (global.autoplay_leveldir != NULL);
586   tape.auto_play_level_solved = FALSE;
587   tape.auto_play_level_fixed = FALSE;
588   tape.auto_play_level_not_fixable = FALSE;
589   tape.quick_resume = FALSE;
590   tape.single_step = FALSE;
591
592   tape.centered_player_nr_next = -1;
593   tape.set_centered_player = FALSE;
594
595   InitRND(tape.random_seed);
596 }
597
598 static void TapeSetRandomSeed(int random_seed)
599 {
600   tape.random_seed = InitRND(random_seed);
601 }
602
603 void TapeStartRecording(int random_seed)
604 {
605   if (!TAPE_IS_STOPPED(tape))
606     TapeStop();
607
608   TapeErase();
609   TapeRewind();
610   TapeSetRandomSeed(random_seed);
611
612   tape.recording = TRUE;
613
614   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
615   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
616   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
617   DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
618
619   MapTapeWarpButton();
620
621   SetDrawDeactivationMask(REDRAW_NONE);
622   audio.sound_deactivated = FALSE;
623
624   // required here to update video display if tape door is closed
625   if (GetDoorState() & DOOR_CLOSE_2)
626     OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
627 }
628
629 static void TapeStartGameRecording(void)
630 {
631   StartGameActions(network.enabled, TRUE, level.random_seed);
632 }
633
634 static void TapeAppendRecording(void)
635 {
636   if (!tape.playing || !tape.pausing)
637     return;
638
639   // stop playing
640   tape.playing = FALSE;
641   tape.fast_forward = FALSE;
642   tape.warp_forward = FALSE;
643   tape.pause_before_end = FALSE;
644   tape.deactivate_display = FALSE;
645
646   // start recording
647   tape.recording = TRUE;
648   tape.changed = TRUE;
649
650   // set current delay (for last played move)
651   tape.pos[tape.counter].delay = tape.delay_played;
652
653   // set current date
654   TapeSetDateFromNow();
655
656   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
657   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
658
659   UpdateAndDisplayGameControlValues();
660 }
661
662 void TapeHaltRecording(void)
663 {
664   tape.counter++;
665
666   // initialize delay for next tape entry (to be able to continue recording)
667   if (tape.counter < MAX_TAPE_LEN)
668     tape.pos[tape.counter].delay = 0;
669
670   tape.length = tape.counter;
671   tape.length_frames = GetTapeLengthFrames();
672   tape.length_seconds = GetTapeLengthSeconds();
673 }
674
675 void TapeStopRecording(void)
676 {
677   if (tape.recording)
678     TapeHaltRecording();
679
680   tape.recording = FALSE;
681   tape.pausing = FALSE;
682
683   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
684   MapTapeEjectButton();
685 }
686
687 boolean TapeAddAction(byte action[MAX_TAPE_ACTIONS])
688 {
689   int i;
690
691   if (tape.pos[tape.counter].delay > 0)         // already stored action
692   {
693     boolean changed_events = FALSE;
694
695     for (i = 0; i < MAX_TAPE_ACTIONS; i++)
696       if (tape.pos[tape.counter].action[i] != action[i])
697         changed_events = TRUE;
698
699     if (changed_events || tape.pos[tape.counter].delay >= 255)
700     {
701       if (tape.counter >= MAX_TAPE_LEN - 1)
702         return FALSE;
703
704       tape.counter++;
705       tape.pos[tape.counter].delay = 0;
706     }
707     else
708       tape.pos[tape.counter].delay++;
709   }
710
711   if (tape.pos[tape.counter].delay == 0)        // store new action
712   {
713     for (i = 0; i < MAX_TAPE_ACTIONS; i++)
714       tape.pos[tape.counter].action[i] = action[i];
715
716     tape.pos[tape.counter].delay++;
717   }
718
719   return TRUE;
720 }
721
722 void TapeRecordAction(byte action_raw[MAX_TAPE_ACTIONS])
723 {
724   byte action[MAX_TAPE_ACTIONS];
725   int i;
726
727   if (!tape.recording)          // (record action even when tape is paused)
728     return;
729
730   for (i = 0; i < MAX_TAPE_ACTIONS; i++)
731     action[i] = action_raw[i];
732
733   if (tape.use_key_actions && tape.set_centered_player)
734   {
735     for (i = 0; i < MAX_PLAYERS; i++)
736       if (tape.centered_player_nr_next == i ||
737           tape.centered_player_nr_next == -1)
738         action[i] |= KEY_SET_FOCUS;
739
740     tape.set_centered_player = FALSE;
741   }
742
743   if (!TapeAddAction(action))
744     TapeStopRecording();
745 }
746
747 void TapeTogglePause(boolean toggle_mode)
748 {
749   if (tape.playing && tape.pausing && (toggle_mode & TAPE_TOGGLE_PLAY_PAUSE))
750   {
751     // continue playing in normal mode
752     tape.fast_forward = FALSE;
753     tape.warp_forward = FALSE;
754     tape.deactivate_display = FALSE;
755
756     tape.pause_before_end = FALSE;
757   }
758
759   tape.pausing = !tape.pausing;
760
761   if (tape.single_step && (toggle_mode & TAPE_TOGGLE_MANUAL))
762     tape.single_step = FALSE;
763
764   DrawVideoDisplayCurrentState();
765
766   if (tape.deactivate_display)
767   {
768     if (tape.pausing)
769       TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
770     else
771       TapeDeactivateDisplayOn();
772   }
773
774   if (tape.quick_resume)
775   {
776     tape.quick_resume = FALSE;
777
778     TapeStopWarpForward();
779     TapeAppendRecording();
780
781     if (!CheckEngineSnapshotSingle())
782       SaveEngineSnapshotSingle();
783
784     // restart step/move snapshots after quick loading tape
785     SaveEngineSnapshotToListInitial();
786
787     // do not map undo/redo buttons after quick loading tape
788     return;
789   }
790
791   if (game_status == GAME_MODE_PLAYING)
792   {
793     if (setup.show_snapshot_buttons && CheckEngineSnapshotList())
794     {
795       if (tape.pausing)
796         MapUndoRedoButtons();
797       else if (!tape.single_step)
798         UnmapUndoRedoButtons();
799     }
800
801     ModifyPauseButtons();
802   }
803 }
804
805 void TapeStartPlaying(void)
806 {
807   if (TAPE_IS_EMPTY(tape))
808     return;
809
810   if (!TAPE_IS_STOPPED(tape))
811     TapeStop();
812
813   TapeRewind();
814
815   tape.playing = TRUE;
816
817   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
818   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
819   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
820   DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
821
822   MapTapeWarpButton();
823
824   SetDrawDeactivationMask(REDRAW_NONE);
825   audio.sound_deactivated = FALSE;
826 }
827
828 static void TapeStartGamePlaying(void)
829 {
830   TapeStartPlaying();
831
832   InitGame();
833 }
834
835 void TapeStopPlaying(void)
836 {
837   tape.playing = FALSE;
838   tape.pausing = FALSE;
839
840   if (tape.warp_forward)
841     TapeStopWarpForward();
842
843   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
844   MapTapeEjectButton();
845 }
846
847 byte *TapePlayAction(void)
848 {
849   int update_delay = FRAMES_PER_SECOND / 2;
850   boolean update_video_display = (FrameCounter % update_delay == 0);
851   boolean update_draw_label_on = ((FrameCounter / update_delay) % 2 == 1);
852   static byte action[MAX_TAPE_ACTIONS];
853   int i;
854
855   if (!tape.playing || tape.pausing)
856     return NULL;
857
858   if (tape.pause_before_end)  // stop some seconds before end of tape
859   {
860     if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
861     {
862       TapeStopWarpForward();
863       TapeTogglePause(TAPE_TOGGLE_MANUAL);
864
865       return NULL;
866     }
867   }
868
869   if (tape.counter >= tape.length)      // end of tape reached
870   {
871     if (tape.warp_forward && !tape.auto_play)
872     {
873       TapeStopWarpForward();
874       TapeTogglePause(TAPE_TOGGLE_MANUAL);
875     }
876     else
877     {
878       TapeStop();
879     }
880
881     return NULL;
882   }
883
884   if (update_video_display && !tape.deactivate_display)
885   {
886     int state = 0;
887
888     if (tape.warp_forward)
889       state |= VIDEO_STATE_WARP(update_draw_label_on);
890     else if (tape.fast_forward)
891       state |= VIDEO_STATE_FFWD(update_draw_label_on);
892
893     if (tape.pause_before_end)
894       state |= VIDEO_STATE_PBEND(update_draw_label_on);
895
896     // draw labels and symbols separately to prevent labels overlapping symbols
897     DrawVideoDisplayLabel(state);
898     DrawVideoDisplaySymbol(state);
899   }
900
901   for (i = 0; i < MAX_TAPE_ACTIONS; i++)
902     action[i] = tape.pos[tape.counter].action[i];
903
904 #if DEBUG_TAPE_WHEN_PLAYING
905   DebugContinued("", "%05d", FrameCounter);
906   for (i = 0; i < MAX_TAPE_ACTIONS; i++)
907     DebugContinued("", "   %08x", action[i]);
908   DebugContinued("tape:play", "\n");
909 #endif
910
911   tape.set_centered_player = FALSE;
912   tape.centered_player_nr_next = -999;
913
914   if (tape.use_key_actions)
915   {
916     for (i = 0; i < MAX_PLAYERS; i++)
917     {
918       if (action[i] & KEY_SET_FOCUS)
919       {
920         tape.set_centered_player = TRUE;
921         tape.centered_player_nr_next =
922           (tape.centered_player_nr_next == -999 ? i : -1);
923       }
924
925       action[i] &= ~KEY_SET_FOCUS;
926     }
927   }
928
929   tape.delay_played++;
930   if (tape.delay_played >= tape.pos[tape.counter].delay)
931   {
932     tape.counter++;
933     tape.delay_played = 0;
934   }
935
936   if (tape.auto_play)
937     PrintTapeReplayProgress(FALSE);
938
939   return action;
940 }
941
942 void TapeStop(void)
943 {
944   if (tape.pausing)
945     TapeTogglePause(TAPE_TOGGLE_MANUAL);
946
947   TapeStopRecording();
948   TapeStopPlaying();
949
950   DrawVideoDisplay(VIDEO_STATE_OFF, 0);
951
952   if (tape.date && tape.length)
953   {
954     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
955     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
956     DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
957   }
958 }
959
960 unsigned int GetTapeLengthFrames(void)
961 {
962   unsigned int tape_length_frames = 0;
963   int i;
964
965   if (TAPE_IS_EMPTY(tape))
966     return 0;
967
968   for (i = 0; i < tape.length; i++)
969     tape_length_frames += tape.pos[i].delay;
970
971   return tape_length_frames;
972 }
973
974 unsigned int GetTapeLengthSeconds(void)
975 {
976   return (GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
977 }
978
979 static float GetTapeLengthSecondsFloat(void)
980 {
981   return ((float)GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
982 }
983
984 static void TapeStartWarpForward(int mode)
985 {
986   tape.fast_forward = (mode & AUTOPLAY_FFWD);
987   tape.warp_forward = (mode & AUTOPLAY_WARP);
988   tape.deactivate_display = (mode & AUTOPLAY_WARP_NO_DISPLAY);
989
990   tape.pausing = FALSE;
991
992   if (tape.deactivate_display)
993     TapeDeactivateDisplayOn();
994
995   DrawVideoDisplayCurrentState();
996 }
997
998 static void TapeStopWarpForward(void)
999 {
1000   tape.fast_forward = FALSE;
1001   tape.warp_forward = FALSE;
1002   tape.deactivate_display = FALSE;
1003
1004   tape.pause_before_end = FALSE;
1005
1006   TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
1007
1008   DrawVideoDisplayCurrentState();
1009 }
1010
1011 static void TapeSingleStep(void)
1012 {
1013   if (network.enabled)
1014     return;
1015
1016   if (!tape.pausing)
1017     TapeTogglePause(TAPE_TOGGLE_MANUAL);
1018
1019   tape.single_step = !tape.single_step;
1020
1021   DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
1022 }
1023
1024 void TapeQuickSave(void)
1025 {
1026   if (game_status == GAME_MODE_MAIN)
1027   {
1028     Request("No game that can be saved!", REQ_CONFIRM);
1029
1030     return;
1031   }
1032
1033   if (game_status != GAME_MODE_PLAYING)
1034     return;
1035
1036   if (tape.recording)
1037     TapeHaltRecording();        // prepare tape for saving on-the-fly
1038
1039   if (TAPE_IS_EMPTY(tape))
1040   {
1041     Request("No tape that can be saved!", REQ_CONFIRM);
1042
1043     return;
1044   }
1045
1046   if (SaveTapeChecked(tape.level_nr))
1047     SaveEngineSnapshotSingle();
1048 }
1049
1050 void TapeQuickLoad(void)
1051 {
1052   char *filename = getTapeFilename(level_nr);
1053
1054   if (!fileExists(filename))
1055   {
1056     Request("No tape for this level!", REQ_CONFIRM);
1057
1058     return;
1059   }
1060
1061   if (tape.recording && !Request("Stop recording and load tape?",
1062                                  REQ_ASK | REQ_STAY_CLOSED))
1063   {
1064     OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1065
1066     return;
1067   }
1068
1069   if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
1070     return;
1071
1072   if (CheckEngineSnapshotSingle())
1073   {
1074     TapeStartGamePlaying();
1075
1076     LoadEngineSnapshotSingle();
1077
1078     DrawCompleteVideoDisplay();
1079
1080     tape.playing = TRUE;
1081     tape.pausing = TRUE;
1082
1083     TapeStopWarpForward();
1084     TapeAppendRecording();
1085
1086     // restart step/move snapshots after quick loading tape
1087     SaveEngineSnapshotToListInitial();
1088
1089     if (FrameCounter > 0)
1090       return;
1091   }
1092
1093   TapeStop();
1094   TapeErase();
1095
1096   LoadTape(level_nr);
1097
1098   if (!TAPE_IS_EMPTY(tape))
1099   {
1100     TapeStartGamePlaying();
1101     TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
1102
1103     tape.quick_resume = TRUE;
1104   }
1105   else  // this should not happen (basically checked above)
1106   {
1107     int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1108
1109     Request("No tape for this level!", REQ_CONFIRM | reopen_door);
1110   }
1111 }
1112
1113 boolean hasSolutionTape(void)
1114 {
1115   boolean tape_file_exists = fileExists(getSolutionTapeFilename(level_nr));
1116   boolean level_has_tape = (level.game_engine_type == GAME_ENGINE_TYPE_SP &&
1117                             level.native_sp_level->demo.is_available);
1118
1119   return (tape_file_exists || level_has_tape);
1120 }
1121
1122 boolean InsertSolutionTape(void)
1123 {
1124   if (!hasSolutionTape())
1125   {
1126     Request("No solution tape for this level!", REQ_CONFIRM);
1127
1128     return FALSE;
1129   }
1130
1131   if (!TAPE_IS_STOPPED(tape))
1132     TapeStop();
1133
1134   // if tape recorder already contains a tape, remove it without asking
1135   TapeErase();
1136
1137   LoadSolutionTape(level_nr);
1138
1139   DrawCompleteVideoDisplay();
1140
1141   if (TAPE_IS_EMPTY(tape))
1142   {
1143     Request("Loading solution tape for this level failed!", REQ_CONFIRM);
1144
1145     return FALSE;
1146   }
1147
1148   return TRUE;
1149 }
1150
1151 boolean PlaySolutionTape(void)
1152 {
1153   if (!InsertSolutionTape())
1154     return FALSE;
1155
1156   TapeStartGamePlaying();
1157
1158   return TRUE;
1159 }
1160
1161 static boolean checkTapesFromSameLevel(struct TapeInfo *t1, struct TapeInfo *t2)
1162 {
1163   return (strEqual(t1->level_identifier, t2->level_identifier) &&
1164           t1->level_nr == t2->level_nr);
1165 }
1166
1167 static void CopyTape(struct TapeInfo *tape_from, struct TapeInfo *tape_to)
1168 {
1169   *tape_to = *tape_from;
1170 }
1171
1172 static void SwapTapes(struct TapeInfo *t1, struct TapeInfo *t2)
1173 {
1174   struct TapeInfo tmp = *t1;
1175
1176   *t1 = *t2;
1177   *t2 = tmp;
1178 }
1179
1180 static void CopyTapeToUndoBuffer(void)
1181 {
1182   // copy tapes to undo buffer if large enough (or larger than last undo tape)
1183   // or if the last undo tape is from a different level set or level number
1184   if (tape.length_seconds >= TAPE_MIN_SECONDS_FOR_UNDO_BUFFER ||
1185       tape.length_seconds >= tape_undo_buffer.length_seconds ||
1186       !checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1187   {
1188     CopyTape(&tape, &tape_undo_buffer);
1189   }
1190 }
1191
1192 void UndoTape(void)
1193 {
1194   // only undo tapes from same level set and with same level number
1195   if (!checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1196     return;
1197
1198   if (!TAPE_IS_STOPPED(tape))
1199     TapeStop();
1200
1201   // swap last recorded tape with undo buffer, so undo can be reversed
1202   SwapTapes(&tape, &tape_undo_buffer);
1203
1204   DrawCompleteVideoDisplay();
1205 }
1206
1207 void FixTape_ForceSinglePlayer(void)
1208 {
1209   int i;
1210
1211   /* fix single-player tapes that contain player input for more than one
1212      player (due to a bug in 3.3.1.2 and earlier versions), which results
1213      in playing levels with more than one player in multi-player mode,
1214      even though the tape was originally recorded in single-player mode */
1215
1216   // remove player input actions for all players but the first one
1217   for (i = 1; i < MAX_PLAYERS; i++)
1218     tape.player_participates[i] = FALSE;
1219
1220   tape.changed = TRUE;
1221 }
1222
1223
1224 // ----------------------------------------------------------------------------
1225 // tape autoplay functions
1226 // ----------------------------------------------------------------------------
1227
1228 void AutoPlayTapes(void)
1229 {
1230   static LevelDirTree *autoplay_leveldir = NULL;
1231   static boolean autoplay_initialized = FALSE;
1232   static int autoplay_last_level_nr = -1;
1233   static int autoplay_level_nr = -1;
1234   static int num_levels_played = 0;
1235   static int num_levels_solved = 0;
1236   static int num_tapes_patched = 0;
1237   static int num_tape_missing = 0;
1238   static boolean level_failed[MAX_TAPES_PER_SET];
1239   static char *tape_filename = NULL;
1240   static int patch_nr = 0;
1241   static char *patch_name[] =
1242   {
1243     "original tape",
1244     "em_random_bug",
1245     "screen_34x34",
1246
1247     NULL
1248   };
1249   static int patch_version_first[] =
1250   {
1251     VERSION_IDENT(0,0,0,0),
1252     VERSION_IDENT(3,3,1,0),
1253     VERSION_IDENT(0,0,0,0),
1254
1255     -1
1256   };
1257   static int patch_version_last[] =
1258   {
1259     VERSION_IDENT(9,9,9,9),
1260     VERSION_IDENT(4,0,1,1),
1261     VERSION_IDENT(4,2,2,0),
1262
1263     -1
1264   };
1265   static byte patch_property_bit[] =
1266   {
1267     TAPE_PROPERTY_NONE,
1268     TAPE_PROPERTY_EM_RANDOM_BUG,
1269     TAPE_PROPERTY_NONE,
1270
1271     -1
1272   };
1273   int i;
1274
1275   if (autoplay_initialized)
1276   {
1277     if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1278     {
1279       if (tape.auto_play_level_solved)
1280       {
1281         if (patch_nr > 0)
1282         {
1283           // level solved by patched tape -- save fixed tape
1284           char *filename = getTapeFilename(level_nr);
1285           char *filename_orig = getStringCat2(filename, ".orig");
1286
1287           // create backup from old tape, if not yet existing
1288           if (!fileExists(filename_orig))
1289             rename(filename, filename_orig);
1290
1291           SaveTapeToFilename(filename);
1292
1293           tape.auto_play_level_fixed = TRUE;
1294           num_tapes_patched++;
1295         }
1296
1297         // continue with next tape
1298         patch_nr = 0;
1299       }
1300       else if (patch_name[patch_nr + 1] != NULL)
1301       {
1302         // level not solved by patched tape -- continue with next patch
1303         patch_nr++;
1304       }
1305       else
1306       {
1307         // level not solved by any patched tape -- continue with next tape
1308         tape.auto_play_level_not_fixable = TRUE;
1309         patch_nr = 0;
1310       }
1311     }
1312
1313     // just finished auto-playing tape
1314     PrintTapeReplayProgress(TRUE);
1315
1316     if (global.autoplay_mode == AUTOPLAY_MODE_SAVE &&
1317         tape.auto_play_level_solved)
1318     {
1319       // set unique basename for score tape (for uploading to score server)
1320       strcpy(tape.score_tape_basename, getScoreTapeBasename(setup.player_name));
1321
1322       // store score in first score entry
1323       scores.last_added = 0;
1324
1325       struct ScoreEntry *entry = &scores.entry[scores.last_added];
1326
1327       strncpy(entry->tape_basename, tape.score_tape_basename, MAX_FILENAME_LEN);
1328       strncpy(entry->name, setup.player_name, MAX_PLAYER_NAME_LEN);
1329
1330       entry->score = game.score_final;
1331       entry->time = game.score_time_final;
1332
1333       if (leveldir_current)
1334       {
1335         // the tape's level set identifier may differ from current level set
1336         strncpy(tape.level_identifier, leveldir_current->identifier,
1337                 MAX_FILENAME_LEN);
1338         tape.level_identifier[MAX_FILENAME_LEN] = '\0';
1339
1340         // the tape's level number may differ from current level number
1341         tape.level_nr = level_nr;
1342       }
1343
1344       PrintNoLog("- uploading score tape to score server ... ");
1345
1346       server_scores.uploaded = FALSE;
1347
1348       // temporarily save score tape (as the tape filename is unknown here)
1349       SaveScoreTape(level_nr);
1350       SaveServerScore(level_nr);
1351
1352       unsigned int upload_delay = 0;
1353       unsigned int upload_delay_value = 10000;
1354
1355       ResetDelayCounter(&upload_delay);
1356
1357       // wait for score tape to be successfully uploaded (and fail on timeout)
1358       while (!server_scores.uploaded)
1359       {
1360         if (DelayReached(&upload_delay, upload_delay_value))
1361         {
1362           PrintNoLog("\r");
1363           Print("- uploading score tape to score server - TIMEOUT.\n");
1364
1365           Fail("cannot upload score tape to score server");
1366         }
1367
1368         Delay(20);
1369       }
1370
1371       PrintNoLog("\r");
1372       Print("- uploading score tape to score server - uploaded.\n");
1373     }
1374
1375     if (patch_nr == 0)
1376       num_levels_played++;
1377
1378     if (tape.auto_play_level_solved)
1379       num_levels_solved++;
1380
1381     if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1382       level_failed[level_nr] = !tape.auto_play_level_solved;
1383   }
1384   else
1385   {
1386     DrawCompleteVideoDisplay();
1387
1388     audio.sound_enabled = FALSE;
1389     setup.engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_OFF);
1390
1391     if (strSuffix(global.autoplay_leveldir, ".tape"))
1392     {
1393       tape_filename = global.autoplay_leveldir;
1394
1395       LoadTapeFromFilename(tape_filename);
1396
1397       global.autoplay_leveldir = tape.level_identifier;
1398
1399       if (tape.level_nr >= 0 && tape.level_nr < MAX_TAPES_PER_SET)
1400         global.autoplay_level[tape.level_nr] = TRUE;
1401
1402       global.autoplay_all = FALSE;
1403     }
1404
1405     autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1406                                                   global.autoplay_leveldir);
1407
1408     if (autoplay_leveldir == NULL)
1409       Fail("no such level identifier: '%s'", global.autoplay_leveldir);
1410
1411     leveldir_current = autoplay_leveldir;
1412
1413     if (autoplay_leveldir->first_level < 0)
1414       autoplay_leveldir->first_level = 0;
1415     if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
1416       autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1417
1418     autoplay_level_nr = autoplay_leveldir->first_level;
1419
1420     PrintLine("=", 79);
1421     if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1422       Print("Automatically fixing level tapes\n");
1423     else
1424       Print("Automatically playing level tapes\n");
1425     PrintLine("-", 79);
1426     Print("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
1427     Print("Level series name:       '%s'\n", autoplay_leveldir->name);
1428     Print("Level series author:     '%s'\n", autoplay_leveldir->author);
1429     Print("Number of levels:        %d\n",   autoplay_leveldir->levels);
1430     PrintLine("=", 79);
1431     Print("\n");
1432
1433     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1434       level_failed[i] = FALSE;
1435
1436     // only private tapes may be modified
1437     if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1438       options.mytapes = TRUE;
1439
1440     autoplay_initialized = TRUE;
1441   }
1442
1443   while (1)
1444   {
1445     if (global.autoplay_mode != AUTOPLAY_MODE_FIX || patch_nr == 0)
1446       level_nr = autoplay_level_nr++;
1447
1448     if (level_nr > autoplay_leveldir->last_level)
1449       break;
1450
1451     // set patch info (required for progress output)
1452     strcpy(tape_patch_info, "");
1453     if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1454       snprintf(tape_patch_info, MAX_OUTPUT_LINESIZE, "[%-13s] ",
1455                patch_name[patch_nr]);
1456
1457     if (!global.autoplay_all && !global.autoplay_level[level_nr])
1458       continue;
1459
1460     TapeErase();
1461
1462     LoadLevel(level_nr);
1463
1464     if (level.no_level_file || level.no_valid_file)
1465     {
1466       Print("Tape %03d: (no level found)\n", level_nr);
1467
1468       continue;
1469     }
1470
1471 #if 0
1472     // ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY
1473     Print("Tape %03d: (only testing level)\n", level_nr);
1474     continue;
1475 #endif
1476
1477     if (tape_filename)
1478       LoadTapeFromFilename(tape_filename);
1479     else if (options.mytapes)
1480       LoadTape(level_nr);
1481     else
1482       LoadSolutionTape(level_nr);
1483
1484     if (tape.no_valid_file)
1485     {
1486       num_tape_missing++;
1487
1488       Print("Tape %03d: (no tape found)\n", level_nr);
1489
1490       continue;
1491     }
1492
1493     if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1494     {
1495       boolean skip_patch = FALSE;
1496
1497       if (tape.engine_version < patch_version_first[patch_nr] ||
1498           tape.engine_version > patch_version_last[patch_nr])
1499       {
1500         Print("Tape %03d %s[%02d:%02d]: (%s %d.%d.%d.%d) - skipped.\n",
1501               level_nr,  tape_patch_info,
1502               tape.length_seconds / 60, tape.length_seconds % 60,
1503               "not suitable for version",
1504               (tape.engine_version / 1000000) % 100,
1505               (tape.engine_version / 10000  ) % 100,
1506               (tape.engine_version / 100    ) % 100,
1507               (tape.engine_version          ) % 100);
1508
1509         skip_patch = TRUE;
1510       }
1511
1512       if (strEqual(patch_name[patch_nr], "screen_34x34") &&
1513           tape.num_participating_players == 1)
1514       {
1515         Print("Tape %03d %s[%02d:%02d]: (%s) - skipped.\n",
1516               level_nr,  tape_patch_info,
1517               tape.length_seconds / 60, tape.length_seconds % 60,
1518               "not suitable for single player tapes");
1519
1520         skip_patch = TRUE;
1521       }
1522
1523       if (skip_patch)
1524       {
1525         if (patch_name[patch_nr + 1] != NULL)
1526         {
1527           // continue with next patch
1528           patch_nr++;
1529         }
1530         else
1531         {
1532           // continue with next tape
1533           patch_nr = 0;
1534         }
1535
1536         continue;
1537       }
1538
1539       if (strEqual(patch_name[patch_nr], "screen_34x34"))
1540       {
1541         tape.scr_fieldx = SCR_FIELDX_DEFAULT * 2;
1542         tape.scr_fieldy = SCR_FIELDY_DEFAULT * 2;
1543       }
1544       else
1545       {
1546         tape.property_bits |= patch_property_bit[patch_nr];
1547       }
1548     }
1549
1550     InitCounter();
1551
1552     TapeStartGamePlaying();
1553     TapeStartWarpForward(global.autoplay_mode);
1554
1555     autoplay_last_level_nr = level_nr;
1556
1557     return;
1558   }
1559
1560   char *autoplay_status = (num_levels_played == num_levels_solved &&
1561                            num_levels_played > 0 ? " OK " : "WARN");
1562   int autoplay_percent = (num_levels_played ?
1563                           num_levels_solved * 100 / num_levels_played : 0);
1564
1565   Print("\n");
1566   PrintLine("=", 79);
1567   Print("Number of levels played: %d\n", num_levels_played);
1568   Print("Number of levels solved: %d (%d%%)\n", num_levels_solved,
1569         (num_levels_played ? num_levels_solved * 100 / num_levels_played : 0));
1570   if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1571     Print("Number of tapes fixed: %d\n", num_tapes_patched);
1572   PrintLine("-", 79);
1573   Print("Summary (for automatic parsing by scripts):\n");
1574
1575   if (tape_filename)
1576   {
1577     Print("TAPEFILE [%s] '%s', %d, %d, %d",
1578           autoplay_status,
1579           autoplay_leveldir->identifier,
1580           autoplay_last_level_nr,
1581           game.score_final,
1582           game.score_time_final);
1583   }
1584   else
1585   {
1586     Print("LEVELDIR [%s] '%s', SOLVED %d/%d (%d%%)",
1587           autoplay_status,
1588           autoplay_leveldir->identifier,
1589           num_levels_solved,
1590           num_levels_played,
1591           autoplay_percent);
1592
1593     if (num_levels_played != num_levels_solved)
1594     {
1595       Print(", FAILED:");
1596       for (i = 0; i < MAX_TAPES_PER_SET; i++)
1597         if (level_failed[i])
1598           Print(" %03d", i);
1599     }
1600   }
1601
1602   Print("\n");
1603   PrintLine("=", 79);
1604
1605   CloseAllAndExit(0);
1606 }
1607
1608
1609 // ----------------------------------------------------------------------------
1610 // tape patch functions
1611 // ----------------------------------------------------------------------------
1612
1613 static boolean PatchTape(struct TapeInfo *tape, char *mode)
1614 {
1615   Print("[%d.%d.%d.%d]: ",
1616         (tape->engine_version / 1000000) % 100,
1617         (tape->engine_version / 10000  ) % 100,
1618         (tape->engine_version / 100    ) % 100,
1619         (tape->engine_version          ) % 100);
1620
1621   if (strEqual(mode, "info"))
1622   {
1623     Print("property bits == 0x%02x\n", tape->property_bits);
1624
1625     return FALSE;
1626   }
1627
1628   boolean unpatch_tape = FALSE;
1629   boolean use_property_bit = FALSE;
1630   byte property_bitmask = 0;
1631
1632   if (strSuffix(mode, ":0") ||
1633       strSuffix(mode, ":off") ||
1634       strSuffix(mode, ":clear"))
1635     unpatch_tape = TRUE;
1636
1637   if (strEqual(mode, "em_random_bug") || strPrefix(mode, "em_random_bug:"))
1638   {
1639     // this bug was introduced in version 3.3.1.0 and fixed in version 4.0.1.2
1640     if (tape->engine_version <  VERSION_IDENT(3,3,1,0) ||
1641         tape->engine_version >= VERSION_IDENT(4,0,1,2))
1642     {
1643       Print("This tape version cannot be patched against EM random bug!\n");
1644
1645       return FALSE;
1646     }
1647
1648     property_bitmask = TAPE_PROPERTY_EM_RANDOM_BUG;
1649
1650     use_property_bit = TRUE;
1651   }
1652   else if (strEqual(mode, "screen_34x34") || strPrefix(mode, "screen_34x34:"))
1653   {
1654     // this bug only affects team mode tapes
1655     if (tape->num_participating_players == 1)
1656     {
1657       Print("Only team mode tapes can be patched against screen size bug!\n");
1658
1659       return FALSE;
1660     }
1661
1662     // this bug (that always existed before) was fixed in version 4.2.2.1
1663     if (tape->engine_version >= VERSION_IDENT(4,2,2,1))
1664     {
1665       Print("This tape version cannot be patched against screen size bug!\n");
1666
1667       return FALSE;
1668     }
1669
1670     int factor = (unpatch_tape ? 1 : 2);
1671     int scr_fieldx_new = SCR_FIELDX_DEFAULT * factor;
1672     int scr_fieldy_new = SCR_FIELDY_DEFAULT * factor;
1673
1674     if (scr_fieldx_new == tape->scr_fieldx &&
1675         scr_fieldy_new == tape->scr_fieldy)
1676     {
1677       Print("Tape already patched for '%s'!\n", mode);
1678
1679       return FALSE;
1680     }
1681
1682     tape->scr_fieldx = scr_fieldx_new;
1683     tape->scr_fieldy = scr_fieldy_new;
1684   }
1685   else
1686   {
1687     Print("Unknown patch mode '%s'!\n", mode);
1688
1689     return FALSE;
1690   }
1691
1692   // patching tapes using property bits may be used for several patch modes
1693   if (use_property_bit)
1694   {
1695     byte property_bits = tape->property_bits;
1696     boolean set_property_bit = (unpatch_tape ? FALSE : TRUE);
1697
1698     if (set_property_bit)
1699       property_bits |= property_bitmask;
1700     else
1701       property_bits &= ~property_bitmask;
1702
1703     if (property_bits == tape->property_bits)
1704     {
1705       Print("Tape already patched for '%s'!\n", mode);
1706
1707       return FALSE;
1708     }
1709
1710     tape->property_bits = property_bits;
1711   }
1712
1713   Print("Patching for '%s' ... ", mode);
1714
1715   return TRUE;
1716 }
1717
1718 void PatchTapes(void)
1719 {
1720   static LevelDirTree *patchtapes_leveldir = NULL;
1721   static int num_tapes_found = 0;
1722   static int num_tapes_patched = 0;
1723   char *mode = global.patchtapes_mode;
1724   int i;
1725
1726   if (strEqual(mode, "help"))
1727   {
1728     PrintLine("=", 79);
1729     Print("Supported patch modes:\n");
1730     Print("- \"em_random_bug\"   - use 64-bit random value bug for EM engine\n");
1731     Print("- \"screen_34x34\"    - force visible playfield size of 34 x 34\n");
1732     PrintLine("-", 79);
1733     Print("Supported modifiers:\n");
1734     Print("- add \":0\", \":off\" or \":clear\" to patch mode to un-patch tape file\n");
1735     PrintLine("=", 79);
1736
1737     CloseAllAndExit(0);
1738   }
1739
1740   patchtapes_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1741                                                   global.patchtapes_leveldir);
1742
1743   if (patchtapes_leveldir == NULL)
1744     Fail("no such level identifier: '%s'", global.patchtapes_leveldir);
1745
1746   leveldir_current = patchtapes_leveldir;
1747
1748   if (patchtapes_leveldir->first_level < 0)
1749     patchtapes_leveldir->first_level = 0;
1750   if (patchtapes_leveldir->last_level >= MAX_TAPES_PER_SET)
1751     patchtapes_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1752
1753   PrintLine("=", 79);
1754   Print("Patching level tapes for patch mode '%s'\n", mode);
1755   PrintLine("-", 79);
1756   Print("Level series identifier: '%s'\n", patchtapes_leveldir->identifier);
1757   Print("Level series name:       '%s'\n", patchtapes_leveldir->name);
1758   Print("Level series author:     '%s'\n", patchtapes_leveldir->author);
1759   Print("Number of levels:        %d\n",   patchtapes_leveldir->levels);
1760   PrintLine("=", 79);
1761   Print("\n");
1762
1763   int first_level = patchtapes_leveldir->first_level;
1764   int last_level = patchtapes_leveldir->last_level;
1765
1766   for (i = first_level; i <= last_level; i++)
1767   {
1768     if (!global.patchtapes_all && !global.patchtapes_level[i])
1769       continue;
1770
1771     Print("Tape %03d: ", i);
1772
1773     TapeErase();
1774     LoadTape(i);
1775
1776     if (tape.no_valid_file)
1777     {
1778       Print("(no tape found)\n");
1779
1780       continue;
1781     }
1782
1783     num_tapes_found++;
1784
1785     if (PatchTape(&tape, mode))
1786     {
1787       char *filename = getTapeFilename(i);
1788       char *filename_orig = getStringCat2(filename, ".orig");
1789
1790       if (!fileExists(filename_orig))
1791         rename(filename, filename_orig);
1792
1793       SaveTapeToFilename(filename);
1794
1795       Print("patched tape saved.\n");
1796
1797       num_tapes_patched++;
1798     }
1799   }
1800
1801   Print("\n");
1802   PrintLine("=", 79);
1803   Print("Number of tapes found: %d\n", num_tapes_found);
1804   Print("Number of tapes patched: %d\n", num_tapes_patched);
1805   PrintLine("=", 79);
1806
1807   CloseAllAndExit(0);
1808 }
1809
1810
1811 // ---------- new tape button stuff -------------------------------------------
1812
1813 static struct
1814 {
1815   int graphic;
1816   struct XY *pos;
1817   int gadget_id;
1818   char *infotext;
1819 } tapebutton_info[NUM_TAPE_BUTTONS] =
1820 {
1821   {
1822     IMG_GFX_TAPE_BUTTON_EJECT,          &tape.button.eject,
1823     TAPE_CTRL_ID_EJECT,                 "eject tape"
1824   },
1825   {
1826     // (same position as "eject" button)
1827     IMG_GFX_TAPE_BUTTON_EXTRA,          &tape.button.eject,
1828     TAPE_CTRL_ID_EXTRA,                 "extra functions"
1829   },
1830   {
1831     IMG_GFX_TAPE_BUTTON_STOP,           &tape.button.stop,
1832     TAPE_CTRL_ID_STOP,                  "stop tape"
1833   },
1834   {
1835     IMG_GFX_TAPE_BUTTON_PAUSE,          &tape.button.pause,
1836     TAPE_CTRL_ID_PAUSE,                 "pause tape"
1837   },
1838   {
1839     IMG_GFX_TAPE_BUTTON_RECORD,         &tape.button.record,
1840     TAPE_CTRL_ID_RECORD,                "record tape"
1841   },
1842   {
1843     IMG_GFX_TAPE_BUTTON_PLAY,           &tape.button.play,
1844     TAPE_CTRL_ID_PLAY,                  "play tape"
1845   },
1846   {
1847     IMG_GFX_TAPE_BUTTON_INSERT_SOLUTION,&tape.button.insert_solution,
1848     TAPE_CTRL_ID_INSERT_SOLUTION,       "insert solution tape"
1849   },
1850   {
1851     IMG_GFX_TAPE_BUTTON_PLAY_SOLUTION,  &tape.button.play_solution,
1852     TAPE_CTRL_ID_PLAY_SOLUTION,         "play solution tape"
1853   }
1854 };
1855
1856 void CreateTapeButtons(void)
1857 {
1858   int i;
1859
1860   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1861   {
1862     int graphic = tapebutton_info[i].graphic;
1863     struct GraphicInfo *gfx = &graphic_info[graphic];
1864     struct XY *pos = tapebutton_info[i].pos;
1865     struct GadgetInfo *gi;
1866     int gd_x = gfx->src_x;
1867     int gd_y = gfx->src_y;
1868     int gd_xp = gfx->src_x + gfx->pressed_xoffset;
1869     int gd_yp = gfx->src_y + gfx->pressed_yoffset;
1870     int id = i;
1871
1872     gi = CreateGadget(GDI_CUSTOM_ID, id,
1873                       GDI_IMAGE_ID, graphic,
1874                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
1875                       GDI_X, VX + pos->x,
1876                       GDI_Y, VY + pos->y,
1877                       GDI_WIDTH, gfx->width,
1878                       GDI_HEIGHT, gfx->height,
1879                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
1880                       GDI_STATE, GD_BUTTON_UNPRESSED,
1881                       GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
1882                       GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
1883                       GDI_DIRECT_DRAW, FALSE,
1884                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
1885                       GDI_CALLBACK_ACTION, HandleTapeButtons,
1886                       GDI_END);
1887
1888     if (gi == NULL)
1889       Fail("cannot create gadget");
1890
1891     tape_gadget[id] = gi;
1892   }
1893 }
1894
1895 void FreeTapeButtons(void)
1896 {
1897   int i;
1898
1899   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1900     FreeGadget(tape_gadget[i]);
1901 }
1902
1903 void MapTapeEjectButton(void)
1904 {
1905   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1906   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1907 }
1908
1909 void MapTapeWarpButton(void)
1910 {
1911   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1912   MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1913 }
1914
1915 void MapTapeButtons(void)
1916 {
1917   int i;
1918
1919   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1920     if (i != TAPE_CTRL_ID_EXTRA)
1921       MapGadget(tape_gadget[i]);
1922
1923   if (tape.recording || tape.playing)
1924     MapTapeWarpButton();
1925
1926   if (tape.show_game_buttons)
1927     MapGameButtonsOnTape();
1928 }
1929
1930 void UnmapTapeButtons(void)
1931 {
1932   int i;
1933
1934   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1935     UnmapGadget(tape_gadget[i]);
1936
1937   if (tape.show_game_buttons)
1938     UnmapGameButtonsOnTape();
1939 }
1940
1941 void RedrawTapeButtons(void)
1942 {
1943   int i;
1944
1945   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1946     RedrawGadget(tape_gadget[i]);
1947
1948   if (tape.show_game_buttons)
1949     RedrawGameButtonsOnTape();
1950 }
1951
1952 void RedrawOrRemapTapeButtons(void)
1953 {
1954   if (tape_gadget[TAPE_CTRL_ID_PLAY]->mapped)
1955   {
1956     // tape buttons already mapped
1957     RedrawTapeButtons();
1958   }
1959   else
1960   {
1961     UnmapTapeButtons();
1962     MapTapeButtons();
1963   }
1964 }
1965
1966 static void HandleTapeButtonsExt(int id)
1967 {
1968   if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
1969     return;
1970
1971   switch (id)
1972   {
1973     case TAPE_CTRL_ID_EJECT:
1974       TapeStop();
1975
1976       if (TAPE_IS_EMPTY(tape))
1977       {
1978         LoadTape(level_nr);
1979
1980         if (TAPE_IS_EMPTY(tape))
1981           Request("No tape for this level!", REQ_CONFIRM);
1982       }
1983       else
1984       {
1985         if (tape.changed)
1986           SaveTapeChecked(level_nr);
1987
1988         TapeErase();
1989       }
1990
1991       DrawCompleteVideoDisplay();
1992       break;
1993
1994     case TAPE_CTRL_ID_EXTRA:
1995       if (tape.playing)
1996       {
1997         tape.pause_before_end = !tape.pause_before_end;
1998
1999         DrawVideoDisplayCurrentState();
2000       }
2001       else if (tape.recording)
2002       {
2003         TapeSingleStep();
2004       }
2005
2006       break;
2007
2008     case TAPE_CTRL_ID_STOP:
2009       TapeStop();
2010
2011       break;
2012
2013     case TAPE_CTRL_ID_PAUSE:
2014       TapeTogglePause(TAPE_TOGGLE_MANUAL);
2015
2016       break;
2017
2018     case TAPE_CTRL_ID_RECORD:
2019       if (TAPE_IS_STOPPED(tape))
2020       {
2021         TapeStartGameRecording();
2022       }
2023       else if (tape.pausing)
2024       {
2025         if (tape.playing)                       // PLAY -> PAUSE -> RECORD
2026           TapeAppendRecording();
2027         else
2028           TapeTogglePause(TAPE_TOGGLE_MANUAL);
2029       }
2030
2031       break;
2032
2033     case TAPE_CTRL_ID_PLAY:
2034       if (tape.recording && tape.pausing)       // PAUSE -> RECORD
2035       {
2036         // ("TAPE_IS_EMPTY(tape)" is TRUE here -- probably fix this)
2037
2038         TapeTogglePause(TAPE_TOGGLE_MANUAL);
2039       }
2040
2041       if (TAPE_IS_EMPTY(tape))
2042         break;
2043
2044       if (TAPE_IS_STOPPED(tape))
2045       {
2046         TapeStartGamePlaying();
2047       }
2048       else if (tape.playing)
2049       {
2050         if (tape.pausing)                       // PAUSE -> PLAY
2051         {
2052           TapeTogglePause(TAPE_TOGGLE_MANUAL | TAPE_TOGGLE_PLAY_PAUSE);
2053         }
2054         else if (!tape.fast_forward)            // PLAY -> FFWD
2055         {
2056           tape.fast_forward = TRUE;
2057         }
2058         else if (!tape.warp_forward)            // FFWD -> WARP
2059         {
2060           tape.warp_forward = TRUE;
2061         }
2062         else if (!tape.deactivate_display)      // WARP -> WARP BLIND
2063         {
2064           tape.deactivate_display = TRUE;
2065
2066           TapeDeactivateDisplayOn();
2067         }
2068         else                                    // WARP BLIND -> PLAY
2069         {
2070           tape.fast_forward = FALSE;
2071           tape.warp_forward = FALSE;
2072           tape.deactivate_display = FALSE;
2073
2074           TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
2075         }
2076
2077         DrawVideoDisplayCurrentState();
2078       }
2079
2080       break;
2081
2082     case TAPE_CTRL_ID_INSERT_SOLUTION:
2083       InsertSolutionTape();
2084
2085       break;
2086
2087     case TAPE_CTRL_ID_PLAY_SOLUTION:
2088       PlaySolutionTape();
2089
2090       break;
2091
2092     default:
2093       break;
2094   }
2095 }
2096
2097 static void HandleTapeButtons(struct GadgetInfo *gi)
2098 {
2099   HandleTapeButtonsExt(gi->custom_id);
2100 }
2101
2102 void HandleTapeButtonKeys(Key key)
2103 {
2104   boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
2105   boolean extra_button_is_active = !eject_button_is_active;
2106
2107   if (key == setup.shortcut.tape_eject && eject_button_is_active)
2108     HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
2109   else if (key == setup.shortcut.tape_extra && extra_button_is_active)
2110     HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
2111   else if (key == setup.shortcut.tape_stop)
2112     HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
2113   else if (key == setup.shortcut.tape_pause)
2114     HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
2115   else if (key == setup.shortcut.tape_record)
2116     HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
2117   else if (key == setup.shortcut.tape_play)
2118     HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);
2119 }