+ for (i = 0; i < tape.length; i++)
+ tape_length_frames += tape.pos[i].delay;
+
+ return tape_length_frames;
+}
+
+unsigned int GetTapeLengthSeconds()
+{
+ return (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()
+{
+ 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()
+{
+ if (options.network)
+ return;
+
+ if (!tape.pausing)
+ TapeTogglePause(TAPE_TOGGLE_MANUAL);
+
+ tape.single_step = !tape.single_step;
+
+ DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
+}
+
+void TapeQuickSave()
+{
+ if (game_status == GAME_MODE_MAIN)
+ {
+ Request("No game that can be saved!", REQ_CONFIRM);
+
+ return;
+ }
+
+ if (game_status != GAME_MODE_PLAYING)
+ return;
+
+ if (tape.recording)
+ TapeHaltRecording(); /* prepare tape for saving on-the-fly */
+
+ if (TAPE_IS_EMPTY(tape))
+ {
+ Request("No tape that can be saved!", REQ_CONFIRM);
+
+ return;
+ }
+
+ if (SaveTapeChecked(tape.level_nr))
+ SaveEngineSnapshotSingle();
+}
+
+void TapeQuickLoad()
+{
+ char *filename = getTapeFilename(level_nr);
+
+ if (!fileExists(filename))
+ {
+ Request("No tape for this level!", REQ_CONFIRM);
+
+ return;
+ }
+
+ if (tape.recording && !Request("Stop recording and load tape?",
+ REQ_ASK | REQ_STAY_CLOSED))
+ {
+ OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
+
+ return;
+ }
+
+ if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
+ return;
+
+ if (CheckEngineSnapshotSingle())
+ {
+ TapeStartGamePlaying();
+
+ LoadEngineSnapshotSingle();
+
+ DrawCompleteVideoDisplay();
+
+ tape.playing = TRUE;
+ tape.pausing = TRUE;
+
+ TapeStopWarpForward();
+ TapeAppendRecording();
+
+ // restart step/move snapshots after quick loading tape
+ SaveEngineSnapshotToListInitial();
+
+ if (FrameCounter > 0)
+ return;
+ }
+
+ TapeStop();
+ TapeErase();
+
+ LoadTape(level_nr);
+
+ if (!TAPE_IS_EMPTY(tape))
+ {
+ TapeStartGamePlaying();
+ TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
+
+ tape.quick_resume = TRUE;
+ }
+ else /* this should not happen (basically checked above) */
+ {
+ int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
+
+ Request("No tape for this level!", REQ_CONFIRM | reopen_door);
+ }
+}
+
+void InsertSolutionTape()
+{
+ if (!TAPE_IS_EMPTY(tape))
+ return;
+
+ LoadSolutionTape(level_nr);
+
+ if (TAPE_IS_EMPTY(tape))
+ Request("No solution tape for this level!", REQ_CONFIRM);
+
+ DrawCompleteVideoDisplay();
+}
+
+
+/* ------------------------------------------------------------------------- *
+ * tape autoplay functions
+ * ------------------------------------------------------------------------- */
+
+void AutoPlayTape()
+{
+ static LevelDirTree *autoplay_leveldir = NULL;
+ static boolean autoplay_initialized = FALSE;
+ static int autoplay_level_nr = -1;
+ static int num_levels_played = 0;
+ static int num_levels_solved = 0;
+ static int num_tape_missing = 0;
+ static boolean level_failed[MAX_TAPES_PER_SET];
+ int i;
+
+ if (autoplay_initialized)
+ {
+ /* just finished auto-playing tape */
+ Print("%s.\n", tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
+
+ num_levels_played++;
+
+ if (tape.auto_play_level_solved)
+ num_levels_solved++;
+ else if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
+ level_failed[level_nr] = TRUE;
+ }
+ else
+ {
+ DrawCompleteVideoDisplay();
+
+ audio.sound_enabled = FALSE;
+ setup.engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_OFF);
+
+ autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
+ global.autoplay_leveldir);
+
+ if (autoplay_leveldir == NULL)
+ Error(ERR_EXIT, "no such level identifier: '%s'",
+ global.autoplay_leveldir);
+
+ leveldir_current = autoplay_leveldir;
+
+ if (autoplay_leveldir->first_level < 0)
+ autoplay_leveldir->first_level = 0;
+ if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
+ autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
+
+ autoplay_level_nr = autoplay_leveldir->first_level;
+
+ PrintLine("=", 79);
+ Print("Automatically playing level tapes\n");
+ PrintLine("-", 79);
+ Print("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
+ Print("Level series name: '%s'\n", autoplay_leveldir->name);
+ Print("Level series author: '%s'\n", autoplay_leveldir->author);
+ Print("Number of levels: %d\n", autoplay_leveldir->levels);
+ PrintLine("=", 79);
+ Print("\n");
+
+ for (i = 0; i < MAX_TAPES_PER_SET; i++)
+ level_failed[i] = FALSE;
+
+ autoplay_initialized = TRUE;
+ }
+
+ while (autoplay_level_nr <= autoplay_leveldir->last_level)
+ {
+ level_nr = autoplay_level_nr++;
+
+ if (!global.autoplay_all && !global.autoplay_level[level_nr])
+ continue;
+
+ TapeErase();
+
+ Print("Level %03d: ", level_nr);
+
+ LoadLevel(level_nr);
+ if (level.no_level_file || level.no_valid_file)
+ {
+ Print("(no level)\n");
+ continue;
+ }
+
+#if 0
+ /* ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY */
+ Print("(only testing level)\n");
+ continue;
+#endif
+
+ if (options.mytapes)
+ LoadTape(level_nr);
+ else
+ LoadSolutionTape(level_nr);
+
+ if (tape.no_valid_file)
+ {
+ num_tape_missing++;
+
+ Print("(no tape)\n");
+
+ continue;
+ }
+
+ Print("playing tape ... ");
+
+ TapeStartGamePlaying();
+ TapeStartWarpForward(global.autoplay_mode);
+
+ return;
+ }
+
+ Print("\n");
+ PrintLine("=", 79);
+ Print("Number of levels played: %d\n", num_levels_played);
+ Print("Number of levels solved: %d (%d%%)\n", num_levels_solved,
+ (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
+ PrintLine("-", 79);
+ Print("Summary (for automatic parsing by scripts):\n");
+ Print("LEVELDIR '%s', SOLVED %d/%d (%d%%)",
+ autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
+ (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
+
+ if (num_levels_played != num_levels_solved)
+ {
+ Print(", FAILED:");
+ for (i = 0; i < MAX_TAPES_PER_SET; i++)
+ if (level_failed[i])
+ Print(" %03d", i);
+ }
+
+ Print("\n");
+ PrintLine("=", 79);
+
+ CloseAllAndExit(0);
+}
+
+
+/* ---------- new tape button stuff ---------------------------------------- */
+
+static struct
+{
+ int graphic;
+ struct XY *pos;
+ int gadget_id;
+ char *infotext;
+} tapebutton_info[NUM_TAPE_BUTTONS] =
+{
+ {
+ IMG_GFX_TAPE_BUTTON_EJECT, &tape.button.eject,
+ TAPE_CTRL_ID_EJECT, "eject tape"
+ },
+ {
+ /* (same position as "eject" button) */
+ IMG_GFX_TAPE_BUTTON_EXTRA, &tape.button.eject,
+ TAPE_CTRL_ID_EXTRA, "extra functions"
+ },
+ {
+ IMG_GFX_TAPE_BUTTON_STOP, &tape.button.stop,
+ TAPE_CTRL_ID_STOP, "stop tape"
+ },
+ {
+ IMG_GFX_TAPE_BUTTON_PAUSE, &tape.button.pause,
+ TAPE_CTRL_ID_PAUSE, "pause tape"
+ },
+ {
+ IMG_GFX_TAPE_BUTTON_RECORD, &tape.button.record,
+ TAPE_CTRL_ID_RECORD, "record tape"
+ },
+ {
+ IMG_GFX_TAPE_BUTTON_PLAY, &tape.button.play,
+ TAPE_CTRL_ID_PLAY, "play tape"
+ }
+};
+
+void CreateTapeButtons()
+{
+ int i;
+
+ for (i = 0; i < NUM_TAPE_BUTTONS; i++)
+ {
+ struct GraphicInfo *gfx = &graphic_info[tapebutton_info[i].graphic];
+ struct XY *pos = tapebutton_info[i].pos;
+ struct GadgetInfo *gi;
+ int gd_x = gfx->src_x;
+ int gd_y = gfx->src_y;
+ int gd_xp = gfx->src_x + gfx->pressed_xoffset;
+ int gd_yp = gfx->src_y + gfx->pressed_yoffset;
+ int id = i;
+
+ gi = CreateGadget(GDI_CUSTOM_ID, id,
+ GDI_INFO_TEXT, tapebutton_info[i].infotext,
+ GDI_X, VX + pos->x,
+ GDI_Y, VY + pos->y,
+ GDI_WIDTH, gfx->width,
+ GDI_HEIGHT, gfx->height,
+ GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
+ GDI_STATE, GD_BUTTON_UNPRESSED,
+ GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
+ GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
+ GDI_DIRECT_DRAW, FALSE,
+ GDI_EVENT_MASK, GD_EVENT_RELEASED,
+ GDI_CALLBACK_ACTION, HandleTapeButtons,
+ GDI_END);
+
+ if (gi == NULL)
+ Error(ERR_EXIT, "cannot create gadget");
+
+ tape_gadget[id] = gi;
+ }
+}
+
+void FreeTapeButtons()
+{
+ int i;
+
+ for (i = 0; i < NUM_TAPE_BUTTONS; i++)
+ FreeGadget(tape_gadget[i]);
+}
+
+void MapTapeEjectButton()
+{
+ UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
+ MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
+}
+
+void MapTapeWarpButton()
+{
+ UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
+ MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
+}
+
+void MapTapeButtons()
+{
+ int i;
+
+ for (i = 0; i < NUM_TAPE_BUTTONS; i++)
+ if (i != TAPE_CTRL_ID_EXTRA)
+ MapGadget(tape_gadget[i]);
+
+ if (tape.recording || tape.playing)
+ MapTapeWarpButton();
+
+ if (tape.show_game_buttons)
+ MapGameButtons();
+}
+
+void UnmapTapeButtons()
+{
+ int i;
+
+ for (i = 0; i < NUM_TAPE_BUTTONS; i++)
+ UnmapGadget(tape_gadget[i]);
+
+ if (tape.show_game_buttons)
+ UnmapGameButtons();
+}
+
+void RedrawTapeButtons()
+{
+ int i;
+
+ for (i = 0; i < NUM_TAPE_BUTTONS; i++)
+ RedrawGadget(tape_gadget[i]);
+
+ if (tape.show_game_buttons)
+ RedrawGameButtons();
+
+ // RedrawGadget() may have set REDRAW_ALL if buttons are defined off-area
+ redraw_mask &= ~REDRAW_ALL;
+}
+
+void RedrawOrRemapTapeButtons()
+{
+ if (tape_gadget[TAPE_CTRL_ID_PLAY]->mapped)
+ {
+ // tape buttons already mapped
+ RedrawTapeButtons();
+ }
+ else
+ {
+ UnmapTapeButtons();
+ MapTapeButtons();
+ }
+}
+
+static void HandleTapeButtonsExt(int id)
+{
+ if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
+ return;
+
+ switch (id)
+ {
+ case TAPE_CTRL_ID_EJECT:
+ TapeStop();
+
+ if (TAPE_IS_EMPTY(tape))
+ {
+ LoadTape(level_nr);
+
+ if (TAPE_IS_EMPTY(tape))
+ Request("No tape for this level!", REQ_CONFIRM);
+ }
+ else
+ {
+ if (tape.changed)
+ SaveTapeChecked(tape.level_nr);
+
+ TapeErase();
+ }
+
+ DrawCompleteVideoDisplay();
+ break;
+
+ case TAPE_CTRL_ID_EXTRA:
+ if (tape.playing)
+ {
+ tape.pause_before_end = !tape.pause_before_end;
+
+ DrawVideoDisplayCurrentState();
+ }
+ else if (tape.recording)
+ {
+ TapeSingleStep();
+ }
+
+ break;
+
+ case TAPE_CTRL_ID_STOP:
+ TapeStop();
+
+ break;
+
+ case TAPE_CTRL_ID_PAUSE:
+ TapeTogglePause(TAPE_TOGGLE_MANUAL);
+
+ break;
+
+ case TAPE_CTRL_ID_RECORD:
+ if (TAPE_IS_STOPPED(tape))
+ {
+ TapeStartGameRecording();
+ }
+ else if (tape.pausing)
+ {
+ if (tape.playing) /* PLAY -> PAUSE -> RECORD */
+ TapeAppendRecording();
+ else
+ TapeTogglePause(TAPE_TOGGLE_MANUAL);
+ }
+
+ break;
+
+ case TAPE_CTRL_ID_PLAY:
+ if (tape.recording && tape.pausing) /* PAUSE -> RECORD */
+ {
+ // ("TAPE_IS_EMPTY(tape)" is TRUE here -- probably fix this)
+
+ TapeTogglePause(TAPE_TOGGLE_MANUAL);
+ }
+
+ if (TAPE_IS_EMPTY(tape))
+ break;
+
+ if (TAPE_IS_STOPPED(tape))
+ {
+ TapeStartGamePlaying();
+ }
+ else if (tape.playing)
+ {
+ if (tape.pausing) /* PAUSE -> PLAY */
+ {
+ TapeTogglePause(TAPE_TOGGLE_MANUAL | TAPE_TOGGLE_PLAY_PAUSE);
+ }
+ else if (!tape.fast_forward) /* PLAY -> FFWD */
+ {
+ tape.fast_forward = TRUE;
+ }
+ else if (!tape.warp_forward) /* FFWD -> WARP */
+ {
+ tape.warp_forward = TRUE;
+ }
+ else if (!tape.deactivate_display) /* WARP -> WARP BLIND */
+ {
+ tape.deactivate_display = TRUE;
+
+ TapeDeactivateDisplayOn();
+ }
+ else /* WARP BLIND -> PLAY */
+ {
+ tape.fast_forward = FALSE;
+ tape.warp_forward = FALSE;
+ tape.deactivate_display = FALSE;
+
+ TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
+ }
+
+ DrawVideoDisplayCurrentState();
+ }
+
+ break;
+
+ default:
+ break;
+ }
+}
+
+static void HandleTapeButtons(struct GadgetInfo *gi)
+{
+ HandleTapeButtonsExt(gi->custom_id);
+}
+
+void HandleTapeButtonKeys(Key key)
+{
+ boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
+ boolean extra_button_is_active = !eject_button_is_active;