+ if (tape.pos[tape.counter].delay > 0) /* already stored action */
+ {
+ boolean changed_events = FALSE;
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ if (tape.pos[tape.counter].action[i] != action[i])
+ changed_events = TRUE;
+
+ if (changed_events || tape.pos[tape.counter].delay >= 255)
+ {
+ if (tape.counter >= MAX_TAPE_LEN - 1)
+ return FALSE;
+
+ tape.counter++;
+ tape.pos[tape.counter].delay = 0;
+ }
+ else
+ tape.pos[tape.counter].delay++;
+ }
+
+ if (tape.pos[tape.counter].delay == 0) /* store new action */
+ {
+ for (i = 0; i < MAX_PLAYERS; i++)
+ tape.pos[tape.counter].action[i] = action[i];
+
+ tape.pos[tape.counter].delay++;
+ }
+
+ return TRUE;
+}
+
+void TapeRecordAction(byte action_raw[MAX_PLAYERS])
+{
+ byte action[MAX_PLAYERS];
+ int i;
+
+ if (!tape.recording) /* (record action even when tape is paused) */
+ return;
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ action[i] = action_raw[i];
+
+ if (!tape.use_mouse && tape.set_centered_player)
+ {
+ for (i = 0; i < MAX_PLAYERS; i++)
+ if (tape.centered_player_nr_next == i ||
+ tape.centered_player_nr_next == -1)
+ action[i] |= KEY_SET_FOCUS;
+
+ tape.set_centered_player = FALSE;
+ }
+
+ if (!TapeAddAction(action))
+ TapeStopRecording();
+}
+
+void TapeTogglePause(boolean toggle_mode)
+{
+ if (tape.playing && tape.pausing && (toggle_mode & TAPE_TOGGLE_PLAY_PAUSE))
+ {
+ // continue playing in normal mode
+ tape.fast_forward = FALSE;
+ tape.warp_forward = FALSE;
+ tape.deactivate_display = FALSE;
+
+ tape.pause_before_end = FALSE;
+ }
+
+ tape.pausing = !tape.pausing;
+
+ if (tape.single_step && (toggle_mode & TAPE_TOGGLE_MANUAL))
+ tape.single_step = FALSE;
+
+ DrawVideoDisplayCurrentState();
+
+ if (tape.deactivate_display)
+ {
+ if (tape.pausing)
+ TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
+ else
+ TapeDeactivateDisplayOn();
+ }
+
+ if (tape.quick_resume)
+ {
+ tape.quick_resume = FALSE;
+
+ TapeStopWarpForward();
+ TapeAppendRecording();
+
+ if (!CheckEngineSnapshotSingle())
+ SaveEngineSnapshotSingle();
+
+ // restart step/move snapshots after quick loading tape
+ SaveEngineSnapshotToListInitial();
+
+ // do not map undo/redo buttons after quick loading tape
+ return;
+ }
+
+ if (setup.show_snapshot_buttons &&
+ game_status == GAME_MODE_PLAYING &&
+ CheckEngineSnapshotList())
+ {
+ if (tape.pausing)
+ MapUndoRedoButtons();
+ else if (!tape.single_step)
+ UnmapUndoRedoButtons();
+ }
+}
+
+void TapeStartPlaying(void)
+{
+ if (TAPE_IS_EMPTY(tape))
+ return;
+
+ if (!TAPE_IS_STOPPED(tape))
+ TapeStop();
+
+ TapeRewind();
+
+ tape.playing = TRUE;
+
+ DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
+ DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
+ DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
+ DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
+
+ MapTapeWarpButton();
+
+ SetDrawDeactivationMask(REDRAW_NONE);
+ audio.sound_deactivated = FALSE;
+}
+
+static void TapeStartGamePlaying(void)
+{
+ TapeStartPlaying();
+
+ InitGame();
+}
+
+void TapeStopPlaying(void)
+{
+ tape.playing = FALSE;
+ tape.pausing = FALSE;
+
+ if (tape.warp_forward)
+ TapeStopWarpForward();
+
+ DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
+ MapTapeEjectButton();
+}
+
+byte *TapePlayAction(void)
+{
+ int update_delay = FRAMES_PER_SECOND / 2;
+ boolean update_video_display = (FrameCounter % update_delay == 0);
+ boolean update_draw_label_on = ((FrameCounter / update_delay) % 2 == 1);
+ static byte action[MAX_PLAYERS];
+ int i;
+
+ if (!tape.playing || tape.pausing)
+ return NULL;
+
+ if (tape.pause_before_end) // stop some seconds before end of tape
+ {
+ if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
+ {
+ TapeStopWarpForward();
+ TapeTogglePause(TAPE_TOGGLE_MANUAL);
+
+ return NULL;
+ }
+ }
+
+ if (tape.counter >= tape.length) /* end of tape reached */
+ {
+ if (tape.warp_forward && !tape.auto_play)
+ {
+ TapeStopWarpForward();
+ TapeTogglePause(TAPE_TOGGLE_MANUAL);
+ }
+ else
+ {
+ TapeStop();
+ }
+
+ return NULL;
+ }
+
+ if (update_video_display && !tape.deactivate_display)
+ {
+ int state = 0;
+
+ if (tape.warp_forward)
+ state |= VIDEO_STATE_WARP(update_draw_label_on);
+ else if (tape.fast_forward)
+ state |= VIDEO_STATE_FFWD(update_draw_label_on);
+
+ if (tape.pause_before_end)
+ state |= VIDEO_STATE_PBEND(update_draw_label_on);
+
+ // draw labels and symbols separately to prevent labels overlapping symbols
+ DrawVideoDisplayLabel(state);
+ DrawVideoDisplaySymbol(state);
+ }
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ action[i] = tape.pos[tape.counter].action[i];
+
+#if DEBUG_TAPE_WHEN_PLAYING
+ printf("%05d", FrameCounter);
+ for (i = 0; i < MAX_PLAYERS; i++)
+ printf(" %08x", action[i]);
+ printf("\n");
+#endif
+
+ tape.set_centered_player = FALSE;
+ tape.centered_player_nr_next = -999;
+
+ if (!tape.use_mouse)
+ {
+ for (i = 0; i < MAX_PLAYERS; i++)
+ {
+ if (action[i] & KEY_SET_FOCUS)
+ {
+ tape.set_centered_player = TRUE;
+ tape.centered_player_nr_next =
+ (tape.centered_player_nr_next == -999 ? i : -1);
+ }
+
+ action[i] &= ~KEY_SET_FOCUS;
+ }
+ }
+
+ tape.delay_played++;
+ if (tape.delay_played >= tape.pos[tape.counter].delay)
+ {
+ tape.counter++;
+ tape.delay_played = 0;
+ }
+
+ if (tape.auto_play)
+ PrintTapeReplayProgress(FALSE);
+
+ return action;
+}
+
+void TapeStop(void)
+{
+ if (tape.pausing)
+ TapeTogglePause(TAPE_TOGGLE_MANUAL);
+
+ TapeStopRecording();
+ TapeStopPlaying();
+
+ DrawVideoDisplay(VIDEO_STATE_OFF, 0);
+
+ if (tape.date && tape.length)
+ {
+ DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
+ DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
+ DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
+ }
+}
+
+unsigned int GetTapeLengthFrames(void)
+{
+ unsigned int tape_length_frames = 0;
+ int i;
+
+ if (TAPE_IS_EMPTY(tape))
+ return(0);
+
+ for (i = 0; i < tape.length; i++)
+ tape_length_frames += tape.pos[i].delay;
+
+ return tape_length_frames;
+}
+
+unsigned int GetTapeLengthSeconds(void)
+{
+ return (GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
+}
+
+static float GetTapeLengthSecondsFloat(void)
+{
+ return ((float)GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
+}
+
+static void TapeStartWarpForward(int mode)
+{
+ tape.fast_forward = (mode & AUTOPLAY_FFWD);
+ tape.warp_forward = (mode & AUTOPLAY_WARP);
+ tape.deactivate_display = (mode & AUTOPLAY_WARP_NO_DISPLAY);
+
+ tape.pausing = FALSE;
+
+ if (tape.deactivate_display)
+ TapeDeactivateDisplayOn();
+
+ DrawVideoDisplayCurrentState();
+}
+
+static void TapeStopWarpForward(void)
+{
+ tape.fast_forward = FALSE;
+ tape.warp_forward = FALSE;
+ tape.deactivate_display = FALSE;
+
+ tape.pause_before_end = FALSE;
+
+ TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
+
+ DrawVideoDisplayCurrentState();
+}
+
+static void TapeSingleStep(void)
+{
+ if (network.enabled)
+ return;
+
+ if (!tape.pausing)
+ TapeTogglePause(TAPE_TOGGLE_MANUAL);
+
+ tape.single_step = !tape.single_step;
+
+ DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
+}
+
+void TapeQuickSave(void)
+{
+ if (game_status == GAME_MODE_MAIN)
+ {
+ Request("No game that can be saved!", REQ_CONFIRM);
+