1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
12 #include "libgame/libgame.h"
24 #define DEBUG_TAPE_WHEN_PLAYING FALSE
26 // tape button identifiers
27 #define TAPE_CTRL_ID_EJECT 0
28 #define TAPE_CTRL_ID_EXTRA 1
29 #define TAPE_CTRL_ID_STOP 2
30 #define TAPE_CTRL_ID_PAUSE 3
31 #define TAPE_CTRL_ID_RECORD 4
32 #define TAPE_CTRL_ID_PLAY 5
33 #define TAPE_CTRL_ID_INSERT_SOLUTION 6
34 #define TAPE_CTRL_ID_PLAY_SOLUTION 7
36 #define NUM_TAPE_BUTTONS 8
38 // values for tape handling
39 #define TAPE_PAUSE_SECONDS_BEFORE_DEATH 5
40 #define TAPE_MIN_SECONDS_FOR_UNDO_BUFFER 20
42 // forward declaration for internal use
43 static void HandleTapeButtons(struct GadgetInfo *);
44 static void TapeStopWarpForward(void);
45 static float GetTapeLengthSecondsFloat(void);
46 static void CopyTapeToUndoBuffer(void);
48 static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
49 static struct TapeInfo tape_undo_buffer;
52 // ============================================================================
53 // video tape definitions
54 // ============================================================================
56 #define VIDEO_INFO_OFF (VIDEO_STATE_DATE_OFF | \
57 VIDEO_STATE_TIME_OFF | \
58 VIDEO_STATE_FRAME_OFF)
59 #define VIDEO_STATE_OFF (VIDEO_STATE_PLAY_OFF | \
60 VIDEO_STATE_REC_OFF | \
61 VIDEO_STATE_PAUSE_OFF | \
62 VIDEO_STATE_FFWD_OFF | \
63 VIDEO_STATE_WARP_OFF | \
64 VIDEO_STATE_WARP2_OFF | \
65 VIDEO_STATE_PBEND_OFF | \
66 VIDEO_STATE_1STEP_OFF)
67 #define VIDEO_PRESS_OFF (VIDEO_PRESS_PLAY_OFF | \
68 VIDEO_PRESS_REC_OFF | \
69 VIDEO_PRESS_PAUSE_OFF | \
70 VIDEO_PRESS_STOP_OFF | \
71 VIDEO_PRESS_EJECT_OFF)
72 #define VIDEO_ALL_OFF (VIDEO_INFO_OFF | \
76 #define VIDEO_INFO_ON (VIDEO_STATE_DATE_ON | \
77 VIDEO_STATE_TIME_ON | \
79 #define VIDEO_STATE_ON (VIDEO_STATE_PLAY_ON | \
80 VIDEO_STATE_REC_ON | \
81 VIDEO_STATE_PAUSE_ON | \
82 VIDEO_STATE_FFWD_ON | \
83 VIDEO_STATE_WARP_ON | \
84 VIDEO_STATE_WARP2_ON | \
85 VIDEO_STATE_PBEND_ON | \
87 #define VIDEO_PRESS_ON (VIDEO_PRESS_PLAY_ON | \
88 VIDEO_PRESS_REC_ON | \
89 VIDEO_PRESS_PAUSE_ON | \
90 VIDEO_PRESS_STOP_ON | \
92 #define VIDEO_ALL_ON (VIDEO_INFO_ON | \
96 #define VIDEO_INFO (VIDEO_INFO_ON | VIDEO_INFO_OFF)
97 #define VIDEO_STATE (VIDEO_STATE_ON | VIDEO_STATE_OFF)
98 #define VIDEO_PRESS (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
99 #define VIDEO_ALL (VIDEO_ALL_ON | VIDEO_ALL_OFF)
101 #define NUM_TAPE_FUNCTIONS 11
102 #define NUM_TAPE_FUNCTION_PARTS 2
103 #define NUM_TAPE_FUNCTION_STATES 2
106 // ============================================================================
107 // video display functions
108 // ============================================================================
110 static void DrawVideoDisplay_Graphics(unsigned int state, unsigned int value)
119 video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
122 { IMG_GFX_TAPE_LABEL_PLAY, &tape.label.play },
123 { IMG_GFX_TAPE_SYMBOL_PLAY, &tape.symbol.play },
126 { IMG_GFX_TAPE_LABEL_RECORD, &tape.label.record },
127 { IMG_GFX_TAPE_SYMBOL_RECORD, &tape.symbol.record },
130 { IMG_GFX_TAPE_LABEL_PAUSE, &tape.label.pause },
131 { IMG_GFX_TAPE_SYMBOL_PAUSE, &tape.symbol.pause },
134 { IMG_GFX_TAPE_LABEL_DATE, &tape.label.date },
138 { IMG_GFX_TAPE_LABEL_TIME, &tape.label.time },
142 // (no label for displaying optional frame)
147 { IMG_GFX_TAPE_LABEL_FAST_FORWARD, &tape.label.fast_forward },
148 { IMG_GFX_TAPE_SYMBOL_FAST_FORWARD, &tape.symbol.fast_forward },
151 { IMG_GFX_TAPE_LABEL_WARP_FORWARD, &tape.label.warp_forward },
152 { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD, &tape.symbol.warp_forward },
155 { IMG_GFX_TAPE_LABEL_WARP_FORWARD_BLIND, &tape.label.warp_forward_blind},
156 { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD_BLIND, &tape.symbol.warp_forward_blind},
159 { IMG_GFX_TAPE_LABEL_PAUSE_BEFORE_END, &tape.label.pause_before_end },
160 { IMG_GFX_TAPE_SYMBOL_PAUSE_BEFORE_END, &tape.symbol.pause_before_end },
163 { IMG_GFX_TAPE_LABEL_SINGLE_STEP, &tape.label.single_step },
164 { IMG_GFX_TAPE_SYMBOL_SINGLE_STEP, &tape.symbol.single_step },
168 for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++) // on or off states
170 for (i = 0; i < NUM_TAPE_FUNCTIONS; i++) // record, play, ...
172 for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++) // label or symbol
174 int graphic = video_pos[i][j].graphic;
175 struct XY *pos = video_pos[i][j].pos;
182 if (state & (1 << (i * 2 + k)))
184 struct GraphicInfo *gfx_bg = &graphic_info[IMG_BACKGROUND_TAPE];
185 struct GraphicInfo *gfx = &graphic_info[graphic];
189 (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
191 if (value == skip_value)
196 gd_bitmap = gfx->bitmap;
202 gd_bitmap = gfx_bg->bitmap;
203 gd_x = gfx_bg->src_x + pos->x;
204 gd_y = gfx_bg->src_y + pos->y;
207 // some tape graphics may be undefined -- only draw if defined
208 if (gd_bitmap != NULL)
209 BlitBitmap(gd_bitmap, drawto, gd_x, gd_y, gfx->width, gfx->height,
210 VX + pos->x, VY + pos->y);
212 redraw_mask |= REDRAW_DOOR_2;
220 #define DATETIME_NONE (0)
222 #define DATETIME_DATE_YYYY (1 << 0)
223 #define DATETIME_DATE_YY (1 << 1)
224 #define DATETIME_DATE_MON (1 << 2)
225 #define DATETIME_DATE_MM (1 << 3)
226 #define DATETIME_DATE_DD (1 << 4)
228 #define DATETIME_TIME_HH (1 << 5)
229 #define DATETIME_TIME_MIN (1 << 6)
230 #define DATETIME_TIME_MM (1 << 7)
231 #define DATETIME_TIME_SS (1 << 8)
233 #define DATETIME_FRAME (1 << 9)
235 #define DATETIME_XOFFSET_1 (1 << 10)
236 #define DATETIME_XOFFSET_2 (1 << 11)
238 #define DATETIME_DATE (DATETIME_DATE_YYYY | \
240 DATETIME_DATE_MON | \
244 #define DATETIME_TIME (DATETIME_TIME_HH | \
245 DATETIME_TIME_MIN | \
249 #define MAX_DATETIME_STRING_SIZE 32
251 static void DrawVideoDisplay_DateTime(unsigned int state, unsigned int value)
255 static char *month_shortnames[] =
273 struct TextPosInfo *pos;
278 { &tape.text.date, DATETIME_DATE_DD },
279 { &tape.text.date, DATETIME_DATE_MON | DATETIME_XOFFSET_1 },
280 { &tape.text.date, DATETIME_DATE_YY | DATETIME_XOFFSET_2 },
281 { &tape.text.date_yyyy, DATETIME_DATE_YYYY },
282 { &tape.text.date_yy, DATETIME_DATE_YY },
283 { &tape.text.date_mon, DATETIME_DATE_MON },
284 { &tape.text.date_mm, DATETIME_DATE_MM },
285 { &tape.text.date_dd, DATETIME_DATE_DD },
287 { &tape.text.time, DATETIME_TIME_MIN },
288 { &tape.text.time, DATETIME_TIME_SS | DATETIME_XOFFSET_1 },
289 { &tape.text.time_hh, DATETIME_TIME_HH },
290 { &tape.text.time_mm, DATETIME_TIME_MM },
291 { &tape.text.time_ss, DATETIME_TIME_SS },
293 { &tape.text.frame, DATETIME_FRAME },
295 { NULL, DATETIME_NONE },
298 for (i = 0; datetime_info[i].pos != NULL; i++)
300 struct TextPosInfo *pos = datetime_info[i].pos;
301 int type = datetime_info[i].type;
308 xpos = VX + pos->x + (type & DATETIME_XOFFSET_1 ? pos->xoffset :
309 type & DATETIME_XOFFSET_2 ? pos->xoffset2 : 0);
312 if ((type & DATETIME_DATE) && (state & VIDEO_STATE_DATE_ON))
314 char s[MAX_DATETIME_STRING_SIZE];
315 int year2 = value / 10000;
316 int year4 = (year2 < 70 ? 2000 + year2 : 1900 + year2);
317 int month_index_raw = (value / 100) % 100;
318 int month_index = month_index_raw % 12; // prevent invalid index
319 int month = month_index + 1;
320 int day = value % 100;
322 strcpy(s, (type & DATETIME_DATE_YYYY ? int2str(year4, 4) :
323 type & DATETIME_DATE_YY ? int2str(year2, 2) :
324 type & DATETIME_DATE_MON ? month_shortnames[month_index] :
325 type & DATETIME_DATE_MM ? int2str(month, 2) :
326 type & DATETIME_DATE_DD ? int2str(day, 2) : ""));
328 DrawText(xpos, ypos, s, pos->font);
330 else if ((type & DATETIME_TIME) && (state & VIDEO_STATE_TIME_ON))
332 char s[MAX_DATETIME_STRING_SIZE];
333 int hh = (value / 3600) % 100;
334 int min = value / 60;
335 int mm = (value / 60) % 60;
338 strcpy(s, (type & DATETIME_TIME_HH ? int2str(hh, 2) :
339 type & DATETIME_TIME_MIN ? int2str(min, 2) :
340 type & DATETIME_TIME_MM ? int2str(mm, 2) :
341 type & DATETIME_TIME_SS ? int2str(ss, 2) : ""));
343 DrawText(xpos, ypos, s, pos->font);
345 else if ((type & DATETIME_FRAME) && (state & VIDEO_STATE_FRAME_ON))
347 DrawText(xpos, ypos, int2str(value, pos->size), pos->font);
352 void DrawVideoDisplay(unsigned int state, unsigned int value)
354 DrawVideoDisplay_Graphics(state, value);
355 DrawVideoDisplay_DateTime(state, value);
358 static void DrawVideoDisplayLabel(unsigned int state)
360 DrawVideoDisplay(state, VIDEO_DISPLAY_LABEL_ONLY);
363 static void DrawVideoDisplaySymbol(unsigned int state)
365 DrawVideoDisplay(state, VIDEO_DISPLAY_SYMBOL_ONLY);
368 static void DrawVideoDisplayCurrentState(void)
372 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
375 state |= VIDEO_STATE_PAUSE_ON;
379 state |= VIDEO_STATE_REC_ON;
381 if (tape.single_step)
382 state |= VIDEO_STATE_1STEP_ON;
384 else if (tape.playing)
386 state |= VIDEO_STATE_PLAY_ON;
390 if (tape.deactivate_display)
391 state |= VIDEO_STATE_WARP2_ON;
392 else if (tape.warp_forward)
393 state |= VIDEO_STATE_WARP_ON;
394 else if (tape.fast_forward)
395 state |= VIDEO_STATE_FFWD_ON;
397 if (tape.pause_before_end)
398 state |= VIDEO_STATE_PBEND_ON;
402 // draw labels and symbols separately to prevent labels overlapping symbols
403 DrawVideoDisplayLabel(state);
404 DrawVideoDisplaySymbol(state);
407 void DrawCompleteVideoDisplay(void)
409 struct GraphicInfo *g_tape = &graphic_info[IMG_BACKGROUND_TAPE];
411 // draw tape background
412 BlitBitmap(g_tape->bitmap, drawto, g_tape->src_x, g_tape->src_y,
413 gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
415 // draw tape buttons (forced)
416 RedrawOrRemapTapeButtons();
418 DrawVideoDisplay(VIDEO_ALL_OFF, 0);
422 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
423 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
424 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
425 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
428 DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
430 else if (tape.playing)
432 DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
433 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
434 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
435 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
438 DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
440 else if (tape.date && tape.length)
442 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
443 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
444 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
447 BlitBitmap(drawto, bitmap_db_door_2, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
451 void TapeDeactivateDisplayOn(void)
453 SetDrawDeactivationMask(REDRAW_FIELD);
454 audio.sound_deactivated = TRUE;
457 void TapeDeactivateDisplayOff(boolean redraw_display)
459 SetDrawDeactivationMask(REDRAW_NONE);
460 audio.sound_deactivated = FALSE;
466 UpdateGameDoorValues();
467 DrawGameDoorValues();
472 // ============================================================================
473 // tape logging functions
474 // ============================================================================
478 LevelDirTree *leveldir;
479 boolean all_levelsets;
482 int num_levels_played;
483 int num_levels_solved;
484 int num_tapes_patched;
485 int num_tape_missing;
486 boolean level_failed[MAX_TAPES_PER_SET];
490 static char tape_patch_info[MAX_OUTPUT_LINESIZE];
492 static void PrintTapeReplayHeader(struct AutoPlayInfo *autoplay)
496 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
497 Print("Automatically fixing level tapes\n");
498 else if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
499 Print("Automatically uploading level tapes\n");
501 Print("Automatically playing level tapes\n");
504 Print("Level series identifier: '%s'\n", autoplay->leveldir->identifier);
505 Print("Level series name: '%s'\n", autoplay->leveldir->name);
506 Print("Level series author: '%s'\n", autoplay->leveldir->author);
507 Print("Number of levels: %d\n", autoplay->leveldir->levels);
511 DrawInitTextItem(autoplay->leveldir->name);
514 static void PrintTapeReplayProgress(boolean replay_finished)
516 static unsigned int counter_last = -1;
517 unsigned int counter = Counter();
518 unsigned int counter_seconds = counter / 1000;
520 if (!replay_finished)
522 unsigned int counter_delay = 50;
524 if (counter > counter_last + counter_delay)
527 PrintNoLog("Tape %03d %s[%02d:%02d]: [%02d:%02d] - playing tape ... ",
528 level_nr, tape_patch_info,
529 tape.length_seconds / 60, tape.length_seconds % 60,
530 TapeTime / 60, TapeTime % 60);
532 counter_last = counter;
537 float tape_length_seconds = GetTapeLengthSecondsFloat();
540 Print("Tape %03d %s[%02d:%02d]: (%02d:%02d.%03d / %.2f %%) - %s.\n",
541 level_nr, tape_patch_info,
542 tape.length_seconds / 60, tape.length_seconds % 60,
543 counter_seconds / 60, counter_seconds % 60, counter % 1000,
544 (float)counter / tape_length_seconds / 10,
545 tape.auto_play_level_fixed ? "solved and fixed" :
546 tape.auto_play_level_solved ? "solved" :
547 tape.auto_play_level_not_fixable ? "NOT SOLVED, NOT FIXED" :
554 static void PrintTapeReplaySummary(struct AutoPlayInfo *autoplay)
556 char *autoplay_status =
557 (autoplay->num_levels_played == autoplay->num_levels_solved &&
558 autoplay->num_levels_played > 0 ? " OK " : "WARN");
559 int autoplay_percent =
560 (autoplay->num_levels_played ?
561 autoplay->num_levels_solved * 100 / autoplay->num_levels_played : 0);
566 Print("Number of levels played: %d\n", autoplay->num_levels_played);
567 Print("Number of levels solved: %d (%d%%)\n", autoplay->num_levels_solved,
568 (autoplay->num_levels_played ?
569 autoplay->num_levels_solved * 100 / autoplay->num_levels_played : 0));
570 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
571 Print("Number of tapes fixed: %d\n", autoplay->num_tapes_patched);
573 Print("Summary (for automatic parsing by scripts):\n");
575 if (autoplay->tape_filename)
577 Print("TAPEFILE [%s] '%s', %d, %d, %d",
579 autoplay->leveldir->identifier,
580 autoplay->last_level_nr,
582 game.score_time_final);
586 Print("LEVELDIR [%s] '%s', SOLVED %d/%d (%d%%)",
588 autoplay->leveldir->identifier,
589 autoplay->num_levels_solved,
590 autoplay->num_levels_played,
593 if (autoplay->num_levels_played != autoplay->num_levels_solved)
596 for (i = 0; i < MAX_TAPES_PER_SET; i++)
597 if (autoplay->level_failed[i])
606 static FILE *tape_log_file;
608 static void OpenTapeLogfile(void)
610 if (!(tape_log_file = fopen(options.tape_log_filename, MODE_WRITE)))
611 Warn("cannot write tape logfile '%s'", options.tape_log_filename);
614 static void WriteTapeLogfile(byte action[MAX_TAPE_ACTIONS])
618 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
619 putFile8Bit(tape_log_file, action[i]);
622 static void CloseTapeLogfile(void)
624 fclose(tape_log_file);
628 // ============================================================================
629 // tape control functions
630 // ============================================================================
632 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
634 struct tm *lt = localtime(&epoch_seconds);
636 tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
639 void TapeSetDateFromNow(void)
641 TapeSetDateFromEpochSeconds(time(NULL));
648 CopyTapeToUndoBuffer();
652 tape.length_frames = 0;
653 tape.length_seconds = 0;
655 tape.score_tape_basename[0] = '\0';
657 if (leveldir_current)
659 strncpy(tape.level_identifier, leveldir_current->identifier,
661 tape.level_identifier[MAX_FILENAME_LEN] = '\0';
664 tape.level_nr = level_nr;
665 tape.pos[tape.counter].delay = 0;
669 tape.random_seed = InitRND(level.random_seed);
671 tape.file_version = FILE_VERSION_ACTUAL;
672 tape.game_version = GAME_VERSION_ACTUAL;
673 tape.engine_version = level.game_version;
675 tape.property_bits = TAPE_PROPERTY_NONE;
677 TapeSetDateFromNow();
679 for (i = 0; i < MAX_PLAYERS; i++)
680 tape.player_participates[i] = FALSE;
682 tape.centered_player_nr_next = -1;
683 tape.set_centered_player = FALSE;
685 // set flags for game actions to default values (may be overwritten later)
686 tape.use_key_actions = TRUE;
687 tape.use_mouse_actions = FALSE;
690 static void TapeRewind(void)
693 tape.delay_played = 0;
694 tape.pause_before_end = FALSE;
695 tape.recording = FALSE;
696 tape.playing = FALSE;
697 tape.fast_forward = FALSE;
698 tape.warp_forward = FALSE;
699 tape.deactivate_display = FALSE;
700 tape.auto_play = (global.autoplay_leveldir != NULL);
701 tape.auto_play_level_solved = FALSE;
702 tape.auto_play_level_fixed = FALSE;
703 tape.auto_play_level_not_fixable = FALSE;
704 tape.quick_resume = FALSE;
705 tape.single_step = FALSE;
707 tape.centered_player_nr_next = -1;
708 tape.set_centered_player = FALSE;
710 InitRND(tape.random_seed);
713 static void TapeSetRandomSeed(int random_seed)
715 tape.random_seed = InitRND(random_seed);
718 void TapeStartRecording(int random_seed)
720 if (!TAPE_IS_STOPPED(tape))
725 TapeSetRandomSeed(random_seed);
727 tape.recording = TRUE;
729 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
730 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
731 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
732 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
736 SetDrawDeactivationMask(REDRAW_NONE);
737 audio.sound_deactivated = FALSE;
739 // required here to update video display if tape door is closed
740 if (GetDoorState() & DOOR_CLOSE_2)
741 OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
744 static void TapeStartGameRecording(void)
746 StartGameActions(network.enabled, TRUE, level.random_seed);
749 static void TapeAppendRecording(void)
751 if (!tape.playing || !tape.pausing)
755 tape.playing = FALSE;
756 tape.fast_forward = FALSE;
757 tape.warp_forward = FALSE;
758 tape.pause_before_end = FALSE;
759 tape.deactivate_display = FALSE;
762 tape.recording = TRUE;
766 // set current delay (for last played move)
767 tape.pos[tape.counter].delay = tape.delay_played;
769 tape.property_bits |= TAPE_PROPERTY_REPLAYED;
772 TapeSetDateFromNow();
774 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
775 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
777 UpdateAndDisplayGameControlValues();
780 void TapeHaltRecording(void)
784 // initialize delay for next tape entry (to be able to continue recording)
785 if (tape.counter < MAX_TAPE_LEN)
786 tape.pos[tape.counter].delay = 0;
788 tape.length = tape.counter;
789 tape.length_frames = GetTapeLengthFrames();
790 tape.length_seconds = GetTapeLengthSeconds();
793 void TapeStopRecording(void)
798 tape.recording = FALSE;
799 tape.pausing = FALSE;
801 DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
802 MapTapeEjectButton();
805 boolean TapeAddAction(byte action[MAX_TAPE_ACTIONS])
809 if (tape.pos[tape.counter].delay > 0) // already stored action
811 boolean changed_events = FALSE;
813 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
814 if (tape.pos[tape.counter].action[i] != action[i])
815 changed_events = TRUE;
817 if (changed_events || tape.pos[tape.counter].delay >= 255)
819 if (tape.counter >= MAX_TAPE_LEN - 1)
823 tape.pos[tape.counter].delay = 0;
826 tape.pos[tape.counter].delay++;
829 if (tape.pos[tape.counter].delay == 0) // store new action
831 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
832 tape.pos[tape.counter].action[i] = action[i];
834 tape.pos[tape.counter].delay++;
840 void TapeRecordAction(byte action_raw[MAX_TAPE_ACTIONS])
842 byte action[MAX_TAPE_ACTIONS];
845 if (!tape.recording) // (record action even when tape is paused)
848 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
849 action[i] = action_raw[i];
851 if (tape.use_key_actions && tape.set_centered_player)
853 for (i = 0; i < MAX_PLAYERS; i++)
854 if (tape.centered_player_nr_next == i ||
855 tape.centered_player_nr_next == -1)
856 action[i] |= KEY_SET_FOCUS;
858 tape.set_centered_player = FALSE;
861 if (GameFrameDelay != GAME_FRAME_DELAY)
862 tape.property_bits |= TAPE_PROPERTY_GAME_SPEED;
864 if (setup.small_game_graphics || SCR_FIELDX >= 2 * SCR_FIELDX_DEFAULT)
865 tape.property_bits |= TAPE_PROPERTY_SMALL_GRAPHICS;
867 if (!TapeAddAction(action))
871 void TapeTogglePause(boolean toggle_mode)
873 if (tape.playing && tape.pausing && (toggle_mode & TAPE_TOGGLE_PLAY_PAUSE))
875 // continue playing in normal mode
876 tape.fast_forward = FALSE;
877 tape.warp_forward = FALSE;
878 tape.deactivate_display = FALSE;
880 tape.pause_before_end = FALSE;
883 tape.pausing = !tape.pausing;
885 if (tape.single_step && (toggle_mode & TAPE_TOGGLE_MANUAL))
886 tape.single_step = FALSE;
888 if (tape.single_step)
889 tape.property_bits |= TAPE_PROPERTY_SINGLE_STEP;
892 tape.property_bits |= TAPE_PROPERTY_PAUSE_MODE;
894 DrawVideoDisplayCurrentState();
896 if (tape.deactivate_display)
899 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
901 TapeDeactivateDisplayOn();
904 if (tape.quick_resume)
906 tape.quick_resume = FALSE;
908 TapeStopWarpForward();
909 TapeAppendRecording();
911 if (!CheckEngineSnapshotSingle())
912 SaveEngineSnapshotSingle();
914 // restart step/move snapshots after quick loading tape
915 SaveEngineSnapshotToListInitial();
917 // do not map undo/redo buttons after quick loading tape
921 if (game_status == GAME_MODE_PLAYING)
923 if (setup.show_load_save_buttons &&
924 setup.show_undo_redo_buttons &&
925 CheckEngineSnapshotList())
928 MapUndoRedoButtons();
929 else if (!tape.single_step)
930 MapLoadSaveButtons();
933 ModifyPauseButtons();
937 void TapeStartPlaying(void)
939 if (TAPE_IS_EMPTY(tape))
942 if (!TAPE_IS_STOPPED(tape))
949 DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
950 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
951 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
952 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
956 SetDrawDeactivationMask(REDRAW_NONE);
957 audio.sound_deactivated = FALSE;
960 static void TapeStartGamePlaying(void)
967 void TapeStopPlaying(void)
969 tape.playing = FALSE;
970 tape.pausing = FALSE;
972 if (tape.warp_forward)
973 TapeStopWarpForward();
975 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
976 MapTapeEjectButton();
979 byte *TapePlayAction(void)
981 int update_delay = FRAMES_PER_SECOND / 2;
982 boolean update_video_display = (FrameCounter % update_delay == 0);
983 boolean update_draw_label_on = ((FrameCounter / update_delay) % 2 == 1);
984 static byte action[MAX_TAPE_ACTIONS];
987 if (!tape.playing || tape.pausing)
990 if (tape.pause_before_end) // stop some seconds before end of tape
992 if (TapeTime > (int)tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
994 TapeStopWarpForward();
995 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1001 if (tape.counter >= tape.length) // end of tape reached
1003 if (tape.warp_forward && !tape.auto_play)
1005 TapeStopWarpForward();
1006 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1016 if (update_video_display && !tape.deactivate_display)
1020 if (tape.warp_forward)
1021 state |= VIDEO_STATE_WARP(update_draw_label_on);
1022 else if (tape.fast_forward)
1023 state |= VIDEO_STATE_FFWD(update_draw_label_on);
1025 if (tape.pause_before_end)
1026 state |= VIDEO_STATE_PBEND(update_draw_label_on);
1028 // draw labels and symbols separately to prevent labels overlapping symbols
1029 DrawVideoDisplayLabel(state);
1030 DrawVideoDisplaySymbol(state);
1033 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
1034 action[i] = tape.pos[tape.counter].action[i];
1036 #if DEBUG_TAPE_WHEN_PLAYING
1037 DebugContinued("", "%05d", FrameCounter);
1038 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
1039 DebugContinued("", " %08x", action[i]);
1040 DebugContinued("tape:play", "\n");
1043 tape.set_centered_player = FALSE;
1044 tape.centered_player_nr_next = -999;
1046 if (tape.use_key_actions)
1048 for (i = 0; i < MAX_PLAYERS; i++)
1050 if (action[i] & KEY_SET_FOCUS)
1052 tape.set_centered_player = TRUE;
1053 tape.centered_player_nr_next =
1054 (tape.centered_player_nr_next == -999 ? i : -1);
1057 action[i] &= ~KEY_SET_FOCUS;
1061 tape.delay_played++;
1062 if (tape.delay_played >= tape.pos[tape.counter].delay)
1065 tape.delay_played = 0;
1069 PrintTapeReplayProgress(FALSE);
1071 if (options.tape_log_filename != NULL)
1072 WriteTapeLogfile(action);
1080 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1082 TapeStopRecording();
1085 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
1087 if (tape.date && tape.length)
1089 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
1090 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
1091 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
1095 static void TapeStopGameOrTape(boolean stop_game)
1097 if (score_info_tape_play || (!tape.playing && stop_game))
1098 RequestQuitGame(FALSE);
1103 void TapeStopGame(void)
1105 if (game_status == GAME_MODE_MAIN)
1108 TapeStopGameOrTape(TRUE);
1111 void TapeStopTape(void)
1113 TapeStopGameOrTape(FALSE);
1116 unsigned int GetTapeLengthFrames(void)
1118 unsigned int tape_length_frames = 0;
1121 if (TAPE_IS_EMPTY(tape))
1124 for (i = 0; i < tape.length; i++)
1125 tape_length_frames += tape.pos[i].delay;
1127 return tape_length_frames;
1130 unsigned int GetTapeLengthSeconds(void)
1132 return (GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
1135 static float GetTapeLengthSecondsFloat(void)
1137 return ((float)GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
1140 static void TapeStartWarpForward(int mode)
1142 tape.fast_forward = (mode & AUTOPLAY_FFWD);
1143 tape.warp_forward = (mode & AUTOPLAY_WARP);
1144 tape.deactivate_display = (mode & AUTOPLAY_WARP_NO_DISPLAY);
1146 tape.pausing = FALSE;
1148 if (tape.deactivate_display)
1149 TapeDeactivateDisplayOn();
1151 DrawVideoDisplayCurrentState();
1154 static void TapeStopWarpForward(void)
1156 tape.fast_forward = FALSE;
1157 tape.warp_forward = FALSE;
1158 tape.deactivate_display = FALSE;
1160 tape.pause_before_end = FALSE;
1162 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
1164 DrawVideoDisplayCurrentState();
1167 static void TapeSingleStep(void)
1169 if (network.enabled)
1173 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1175 tape.single_step = !tape.single_step;
1177 DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
1180 void TapeQuickSave(void)
1182 if (game_status == GAME_MODE_MAIN)
1184 Request("No game that can be saved!", REQ_CONFIRM);
1189 if (game_status != GAME_MODE_PLAYING)
1193 TapeHaltRecording(); // prepare tape for saving on-the-fly
1195 if (TAPE_IS_EMPTY(tape))
1197 Request("No tape that can be saved!", REQ_CONFIRM);
1202 tape.property_bits |= TAPE_PROPERTY_SNAPSHOT;
1204 if (SaveTapeChecked(tape.level_nr))
1205 SaveEngineSnapshotSingle();
1208 void TapeQuickLoad(void)
1210 char *filename = getTapeFilename(level_nr);
1212 if (!fileExists(filename))
1214 Request("No tape for this level!", REQ_CONFIRM);
1219 if (tape.recording && !Request("Stop recording and load tape?",
1220 REQ_ASK | REQ_STAY_CLOSED))
1222 OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1227 if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
1230 if (CheckEngineSnapshotSingle())
1232 TapeStartGamePlaying();
1234 LoadEngineSnapshotSingle();
1236 DrawCompleteVideoDisplay();
1238 tape.playing = TRUE;
1239 tape.pausing = TRUE;
1241 TapeStopWarpForward();
1242 TapeAppendRecording();
1244 // restart step/move snapshots after quick loading tape
1245 SaveEngineSnapshotToListInitial();
1247 if (FrameCounter > 0)
1256 if (!TAPE_IS_EMPTY(tape))
1258 TapeStartGamePlaying();
1259 TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
1261 tape.quick_resume = TRUE;
1262 tape.property_bits |= TAPE_PROPERTY_SNAPSHOT;
1264 else // this should not happen (basically checked above)
1266 int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1268 Request("No tape for this level!", REQ_CONFIRM | reopen_door);
1272 static boolean checkRestartGame(char *message)
1274 if (game_status == GAME_MODE_MAIN)
1277 if (!hasStartedNetworkGame())
1280 if (level_editor_test_game)
1283 if (game.all_players_gone)
1286 if (!setup.ask_on_quit_game)
1289 if (Request(message, REQ_ASK | REQ_STAY_CLOSED))
1292 OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1297 void TapeRestartGame(void)
1299 if (score_info_tape_play)
1301 TapeStartGamePlaying();
1306 if (!checkRestartGame("Restart game?"))
1309 StartGameActions(network.enabled, setup.autorecord, level.random_seed);
1312 void TapeReplayAndPauseBeforeEnd(void)
1314 if (score_info_tape_play)
1317 if (TAPE_IS_EMPTY(tape) && !tape.recording)
1319 Request("No tape for this level!", REQ_CONFIRM);
1324 if (!checkRestartGame("Replay game and pause before end?"))
1328 TapeStartGamePlaying();
1329 TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
1331 tape.pause_before_end = TRUE;
1332 tape.quick_resume = TRUE;
1335 boolean hasSolutionTape(void)
1337 boolean tape_file_exists = fileExists(getSolutionTapeFilename(level_nr));
1338 boolean level_has_tape = (level.game_engine_type == GAME_ENGINE_TYPE_SP &&
1339 level.native_sp_level->demo.is_available);
1341 return (tape_file_exists || level_has_tape);
1344 boolean InsertSolutionTape(void)
1346 if (!hasSolutionTape())
1348 Request("No solution tape for this level!", REQ_CONFIRM);
1353 if (!TAPE_IS_STOPPED(tape))
1356 // if tape recorder already contains a tape, remove it without asking
1359 LoadSolutionTape(level_nr);
1361 DrawCompleteVideoDisplay();
1363 if (TAPE_IS_EMPTY(tape))
1365 Request("Loading solution tape for this level failed!", REQ_CONFIRM);
1373 boolean PlaySolutionTape(void)
1375 if (!InsertSolutionTape())
1378 TapeStartGamePlaying();
1383 static boolean PlayScoreTape_WaitForDownload(void)
1385 DelayCounter download_delay = { 10000 };
1387 ResetDelayCounter(&download_delay);
1389 // wait for score tape to be successfully downloaded (and fail on timeout)
1390 while (!server_scores.tape_downloaded)
1392 if (DelayReached(&download_delay))
1395 UPDATE_BUSY_STATE_NOT_LOADING();
1403 boolean PlayScoreTape(int entry_nr)
1405 struct ScoreEntry *entry = &scores.entry[entry_nr];
1406 char *tape_filename =
1408 getScoreTapeFilename(entry->tape_basename, level_nr) :
1409 getScoreCacheTapeFilename(entry->tape_basename, level_nr));
1410 boolean download_tape = (!fileExists(tape_filename));
1412 if (download_tape && entry->id == -1)
1414 FadeSkipNextFadeIn();
1416 Request("Cannot find score tape!", REQ_CONFIRM);
1421 server_scores.tape_downloaded = FALSE;
1424 ApiGetScoreTapeAsThread(level_nr, entry->id, entry->tape_basename);
1426 SetGameStatus(GAME_MODE_PLAYING);
1428 FadeOut(REDRAW_FIELD);
1430 if (download_tape && !PlayScoreTape_WaitForDownload())
1432 SetGameStatus(GAME_MODE_SCOREINFO);
1435 Request("Cannot download score tape from score server!", REQ_CONFIRM);
1440 if (!TAPE_IS_STOPPED(tape))
1443 // if tape recorder already contains a tape, remove it without asking
1446 if (entry->id == -1)
1447 LoadScoreTape(entry->tape_basename, level_nr);
1449 LoadScoreCacheTape(entry->tape_basename, level_nr);
1451 if (TAPE_IS_EMPTY(tape))
1453 SetGameStatus(GAME_MODE_SCOREINFO);
1456 Request("Cannot load score tape for this level!", REQ_CONFIRM);
1461 FadeSkipNextFadeOut();
1463 TapeStartGamePlaying();
1465 score_info_tape_play = TRUE;
1470 static boolean checkTapesFromSameLevel(struct TapeInfo *t1, struct TapeInfo *t2)
1472 return (strEqual(t1->level_identifier, t2->level_identifier) &&
1473 t1->level_nr == t2->level_nr);
1476 static void CopyTape(struct TapeInfo *tape_from, struct TapeInfo *tape_to)
1478 *tape_to = *tape_from;
1481 static void SwapTapes(struct TapeInfo *t1, struct TapeInfo *t2)
1483 struct TapeInfo tmp = *t1;
1489 static void CopyTapeToUndoBuffer(void)
1491 // copy tapes to undo buffer if large enough (or larger than last undo tape)
1492 // or if the last undo tape is from a different level set or level number
1493 if (tape.length_seconds >= TAPE_MIN_SECONDS_FOR_UNDO_BUFFER ||
1494 tape.length_seconds >= tape_undo_buffer.length_seconds ||
1495 !checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1497 CopyTape(&tape, &tape_undo_buffer);
1503 // only undo tapes from same level set and with same level number
1504 if (!checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1507 if (!TAPE_IS_STOPPED(tape))
1510 // swap last recorded tape with undo buffer, so undo can be reversed
1511 SwapTapes(&tape, &tape_undo_buffer);
1513 DrawCompleteVideoDisplay();
1516 void FixTape_ForceSinglePlayer(void)
1520 /* fix single-player tapes that contain player input for more than one
1521 player (due to a bug in 3.3.1.2 and earlier versions), which results
1522 in playing levels with more than one player in multi-player mode,
1523 even though the tape was originally recorded in single-player mode */
1525 // remove player input actions for all players but the first one
1526 for (i = 1; i < MAX_PLAYERS; i++)
1527 tape.player_participates[i] = FALSE;
1529 tape.changed = TRUE;
1533 // ----------------------------------------------------------------------------
1534 // tape autoplay functions
1535 // ----------------------------------------------------------------------------
1537 static TreeInfo *getNextValidAutoPlayEntry(TreeInfo *node)
1539 node = getNextValidTreeInfoEntry(node);
1541 while (node && node->is_copy)
1542 node = getNextValidTreeInfoEntry(node);
1547 static TreeInfo *getFirstValidAutoPlayEntry(TreeInfo *node)
1549 node = getFirstValidTreeInfoEntry(node);
1551 if (node && node->is_copy)
1552 return getNextValidAutoPlayEntry(node);
1557 static void AutoPlayTapes_SetScoreEntry(int score, int time)
1559 char *name = (options.mytapes ? setup.player_name : options.player_name);
1561 // set unique basename for score tape (for uploading to score server)
1562 strcpy(tape.score_tape_basename, getScoreTapeBasename(name));
1564 // store score in first score entry
1565 scores.last_added = 0;
1567 struct ScoreEntry *entry = &scores.entry[scores.last_added];
1569 strncpy(entry->tape_basename, tape.score_tape_basename, MAX_FILENAME_LEN);
1570 strncpy(entry->name, setup.player_name, MAX_PLAYER_NAME_LEN);
1572 entry->score = score;
1575 PrintNoLog("- uploading score tape to score server ... ");
1577 server_scores.uploaded = FALSE;
1580 static boolean AutoPlayTapes_WaitForUpload(void)
1582 DelayCounter upload_delay = { 10000 };
1584 ResetDelayCounter(&upload_delay);
1586 // wait for score tape to be successfully uploaded (and fail on timeout)
1587 while (!server_scores.uploaded)
1589 if (DelayReached(&upload_delay))
1592 Print("- uploading score tape to score server - TIMEOUT.\n");
1594 if (program.headless)
1595 Fail("cannot upload score tape to score server");
1600 UPDATE_BUSY_STATE();
1606 Print("- uploading score tape to score server - uploaded.\n");
1611 static int AutoPlayTapesExt(boolean initialize)
1613 static struct AutoPlayInfo autoplay;
1614 static int num_tapes = 0;
1615 static int patch_nr = 0;
1616 static char *patch_name[] =
1624 static int patch_version_first[] =
1626 VERSION_IDENT(0,0,0,0),
1627 VERSION_IDENT(3,3,1,0),
1628 VERSION_IDENT(0,0,0,0),
1632 static int patch_version_last[] =
1634 VERSION_IDENT(9,9,9,9),
1635 VERSION_IDENT(4,0,1,1),
1636 VERSION_IDENT(4,2,2,0),
1640 static byte patch_property_bit[] =
1643 TAPE_PROPERTY_EM_RANDOM_BUG,
1648 LevelDirTree *leveldir_current_last = leveldir_current;
1649 boolean init_level_set = FALSE;
1650 int level_nr_last = level_nr;
1655 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1657 if (tape.auto_play_level_solved)
1661 // level solved by patched tape -- save fixed tape
1662 char *filename = getTapeFilename(level_nr);
1663 char *filename_orig = getStringCat2(filename, ".orig");
1665 // create backup from old tape, if not yet existing
1666 if (!fileExists(filename_orig))
1667 rename(filename, filename_orig);
1669 SaveTapeToFilename(filename);
1671 tape.auto_play_level_fixed = TRUE;
1672 autoplay.num_tapes_patched++;
1675 // continue with next tape
1678 else if (patch_name[patch_nr + 1] != NULL)
1680 // level not solved by patched tape -- continue with next patch
1685 // level not solved by any patched tape -- continue with next tape
1686 tape.auto_play_level_not_fixable = TRUE;
1691 // just finished auto-playing tape
1692 PrintTapeReplayProgress(TRUE);
1694 if (options.tape_log_filename != NULL)
1697 if (global.autoplay_mode == AUTOPLAY_MODE_SAVE &&
1698 tape.auto_play_level_solved)
1700 AutoPlayTapes_SetScoreEntry(game.score_final, game.score_time_final);
1702 if (leveldir_current)
1704 // the tape's level set identifier may differ from current level set
1705 strncpy(tape.level_identifier, leveldir_current->identifier,
1707 tape.level_identifier[MAX_FILENAME_LEN] = '\0';
1709 // the tape's level number may differ from current level number
1710 tape.level_nr = level_nr;
1713 // save score tape to upload to server; may be required for some reasons:
1714 // * level set identifier in solution tapes may differ from level set
1715 // * level set identifier is missing (old-style tape without INFO chunk)
1716 // * solution tape may have native format (like Supaplex solution files)
1718 SaveScoreTape(level_nr);
1719 SaveServerScore(level_nr, TRUE);
1721 AutoPlayTapes_WaitForUpload();
1725 autoplay.num_levels_played++;
1727 if (tape.auto_play_level_solved)
1728 autoplay.num_levels_solved++;
1730 if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1731 autoplay.level_failed[level_nr] = !tape.auto_play_level_solved;
1735 if (strEqual(global.autoplay_leveldir, "ALL"))
1737 autoplay.all_levelsets = TRUE;
1739 // tape mass-uploading only allowed for private tapes
1740 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
1741 options.mytapes = TRUE;
1744 if ((global.autoplay_mode == AUTOPLAY_MODE_SAVE ||
1745 global.autoplay_mode == AUTOPLAY_MODE_UPLOAD) &&
1747 options.player_name == NULL)
1749 Fail("specify player name when uploading solution tapes");
1752 if (global.autoplay_mode != AUTOPLAY_MODE_UPLOAD)
1753 DrawCompleteVideoDisplay();
1755 if (program.headless)
1757 audio.sound_enabled = FALSE;
1758 setup.engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_OFF);
1761 if (strSuffix(global.autoplay_leveldir, ".tape"))
1763 autoplay.tape_filename = global.autoplay_leveldir;
1765 if (!fileExists(autoplay.tape_filename))
1766 Fail("tape file '%s' does not exist", autoplay.tape_filename);
1768 LoadTapeFromFilename(autoplay.tape_filename);
1770 if (tape.no_valid_file)
1771 Fail("cannot load tape file '%s'", autoplay.tape_filename);
1773 if (tape.no_info_chunk && !options.identifier)
1774 Fail("cannot get levelset from tape file '%s'", autoplay.tape_filename);
1776 if (tape.no_info_chunk && !options.level_nr)
1777 Fail("cannot get level nr from tape file '%s'", autoplay.tape_filename);
1779 global.autoplay_leveldir = tape.level_identifier;
1781 if (options.identifier != NULL)
1782 global.autoplay_leveldir = options.identifier;
1784 if (options.level_nr != NULL)
1785 tape.level_nr = atoi(options.level_nr);
1787 if (tape.level_nr >= 0 && tape.level_nr < MAX_TAPES_PER_SET)
1788 global.autoplay_level[tape.level_nr] = TRUE;
1790 global.autoplay_all = FALSE;
1791 options.mytapes = FALSE;
1794 if (autoplay.all_levelsets)
1796 // start auto-playing first level set
1797 autoplay.leveldir = getFirstValidAutoPlayEntry(leveldir_first);
1801 // auto-play selected level set
1802 autoplay.leveldir = getTreeInfoFromIdentifier(leveldir_first,
1803 global.autoplay_leveldir);
1806 if (autoplay.leveldir == NULL)
1807 Fail("no such level identifier: '%s'", global.autoplay_leveldir);
1809 // only private tapes may be modified
1810 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1811 options.mytapes = TRUE;
1813 // set timestamp for batch tape upload
1814 global.autoplay_time = time(NULL);
1818 init_level_set = TRUE;
1825 leveldir_current = autoplay.leveldir;
1827 if (autoplay.leveldir->first_level < 0)
1828 autoplay.leveldir->first_level = 0;
1829 if (autoplay.leveldir->last_level >= MAX_TAPES_PER_SET)
1830 autoplay.leveldir->last_level = MAX_TAPES_PER_SET - 1;
1832 autoplay.level_nr = autoplay.leveldir->first_level;
1834 autoplay.num_levels_played = 0;
1835 autoplay.num_levels_solved = 0;
1836 autoplay.num_tapes_patched = 0;
1837 autoplay.num_tape_missing = 0;
1839 for (i = 0; i < MAX_TAPES_PER_SET; i++)
1840 autoplay.level_failed[i] = FALSE;
1842 PrintTapeReplayHeader(&autoplay);
1844 init_level_set = FALSE;
1847 if (autoplay.all_levelsets && global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
1849 boolean skip_levelset = FALSE;
1851 if (!directoryExists(getTapeDir(autoplay.leveldir->subdir)))
1853 Print("No tape directory for this level set found -- skipping.\n");
1855 skip_levelset = TRUE;
1858 if (CheckTapeDirectoryUploadsComplete(autoplay.leveldir->subdir))
1860 Print("All tapes for this level set already uploaded -- skipping.\n");
1862 skip_levelset = TRUE;
1867 PrintTapeReplaySummary(&autoplay);
1869 // continue with next level set
1870 autoplay.leveldir = getNextValidAutoPlayEntry(autoplay.leveldir);
1872 // all level sets processed
1873 if (autoplay.leveldir == NULL)
1876 init_level_set = TRUE;
1882 if (global.autoplay_mode != AUTOPLAY_MODE_FIX || patch_nr == 0)
1883 level_nr = autoplay.level_nr++;
1885 UPDATE_BUSY_STATE();
1887 // check if all tapes for this level set have been processed
1888 if (level_nr > autoplay.leveldir->last_level)
1890 PrintTapeReplaySummary(&autoplay);
1892 if (!autoplay.all_levelsets)
1895 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
1896 MarkTapeDirectoryUploadsAsComplete(autoplay.leveldir->subdir);
1898 // continue with next level set
1899 autoplay.leveldir = getNextValidAutoPlayEntry(autoplay.leveldir);
1901 // all level sets processed
1902 if (autoplay.leveldir == NULL)
1905 init_level_set = TRUE;
1910 // set patch info (required for progress output)
1911 strcpy(tape_patch_info, "");
1912 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1913 snprintf(tape_patch_info, MAX_OUTPUT_LINESIZE, "[%-13s] ",
1914 patch_name[patch_nr]);
1916 if (!global.autoplay_all && !global.autoplay_level[level_nr])
1919 // speed things up in case of missing private tapes (skip loading level)
1920 if (options.mytapes && !fileExists(getTapeFilename(level_nr)))
1922 autoplay.num_tape_missing++;
1924 Print("Tape %03d: (no tape found)\n", level_nr);
1930 TapeRewind(); // needed to reset "tape.auto_play_level_solved"
1932 LoadLevel(level_nr);
1934 if (level.no_level_file || level.no_valid_file)
1936 Print("Tape %03d: (no level found)\n", level_nr);
1942 // ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY
1943 Print("Tape %03d: (only testing level)\n", level_nr);
1947 if (autoplay.tape_filename)
1948 LoadTapeFromFilename(autoplay.tape_filename);
1949 else if (options.mytapes)
1952 LoadSolutionTape(level_nr);
1954 if (tape.no_valid_file)
1956 autoplay.num_tape_missing++;
1958 Print("Tape %03d: (no tape found)\n", level_nr);
1963 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1965 boolean skip_patch = FALSE;
1967 if (tape.engine_version < patch_version_first[patch_nr] ||
1968 tape.engine_version > patch_version_last[patch_nr])
1970 Print("Tape %03d %s[%02d:%02d]: (%s %d.%d.%d.%d) - skipped.\n",
1971 level_nr, tape_patch_info,
1972 tape.length_seconds / 60, tape.length_seconds % 60,
1973 "not suitable for version",
1974 (tape.engine_version / 1000000) % 100,
1975 (tape.engine_version / 10000 ) % 100,
1976 (tape.engine_version / 100 ) % 100,
1977 (tape.engine_version ) % 100);
1982 if (strEqual(patch_name[patch_nr], "screen_34x34") &&
1983 tape.num_participating_players == 1)
1985 Print("Tape %03d %s[%02d:%02d]: (%s) - skipped.\n",
1986 level_nr, tape_patch_info,
1987 tape.length_seconds / 60, tape.length_seconds % 60,
1988 "not suitable for single player tapes");
1995 if (patch_name[patch_nr + 1] != NULL)
1997 // continue with next patch
2002 // continue with next tape
2009 if (strEqual(patch_name[patch_nr], "screen_34x34"))
2011 tape.scr_fieldx = SCR_FIELDX_DEFAULT * 2;
2012 tape.scr_fieldy = SCR_FIELDY_DEFAULT * 2;
2016 tape.property_bits |= patch_property_bit[patch_nr];
2022 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
2024 boolean use_temporary_tape_file = FALSE;
2026 Print("Tape %03d:\n", level_nr);
2028 AutoPlayTapes_SetScoreEntry(0, 0);
2030 if (autoplay.tape_filename == NULL)
2032 autoplay.tape_filename = (options.mytapes ? getTapeFilename(level_nr) :
2033 getDefaultSolutionTapeFilename(level_nr));
2035 if (!fileExists(autoplay.tape_filename))
2037 // non-standard or incorrect solution tape -- save to temporary file
2038 autoplay.tape_filename = getTemporaryTapeFilename();
2040 SaveTapeToFilename(autoplay.tape_filename);
2042 use_temporary_tape_file = TRUE;
2046 SaveServerScoreFromFile(level_nr, TRUE, autoplay.tape_filename);
2048 boolean success = AutoPlayTapes_WaitForUpload();
2050 if (use_temporary_tape_file)
2051 unlink(autoplay.tape_filename);
2053 // required for uploading multiple tapes
2054 autoplay.tape_filename = NULL;
2058 num_tapes = -num_tapes;
2068 if (options.tape_log_filename != NULL)
2071 TapeStartGamePlaying();
2072 TapeStartWarpForward(global.autoplay_mode);
2074 autoplay.last_level_nr = level_nr;
2079 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
2085 Print("SUMMARY: %d tapes uploaded.\n", num_tapes);
2087 Print("SUMMARY: Uploading tapes failed.\n");
2092 // clear timestamp for batch tape upload (required after interactive upload)
2093 global.autoplay_time = 0;
2095 // exit if running headless or if visually auto-playing tapes
2096 if (program.headless || global.autoplay_mode != AUTOPLAY_MODE_UPLOAD)
2099 // when running interactively, restore last selected level set and number
2100 leveldir_current = leveldir_current_last;
2101 level_nr = level_nr_last;
2106 int AutoPlayTapes(void)
2108 return AutoPlayTapesExt(TRUE);
2111 int AutoPlayTapesContinue(void)
2113 return AutoPlayTapesExt(FALSE);
2117 // ----------------------------------------------------------------------------
2118 // tape patch functions
2119 // ----------------------------------------------------------------------------
2121 static boolean PatchTape(struct TapeInfo *tape, char *mode)
2123 Print("[%d.%d.%d.%d]: ",
2124 (tape->engine_version / 1000000) % 100,
2125 (tape->engine_version / 10000 ) % 100,
2126 (tape->engine_version / 100 ) % 100,
2127 (tape->engine_version ) % 100);
2129 if (strEqual(mode, "info"))
2131 Print("property bits == 0x%02x\n", tape->property_bits);
2136 boolean unpatch_tape = FALSE;
2137 boolean use_property_bit = FALSE;
2138 byte property_bitmask = 0;
2140 if (strSuffix(mode, ":0") ||
2141 strSuffix(mode, ":off") ||
2142 strSuffix(mode, ":clear"))
2143 unpatch_tape = TRUE;
2145 if (strEqual(mode, "em_random_bug") || strPrefix(mode, "em_random_bug:"))
2147 // this bug was introduced in version 3.3.1.0 and fixed in version 4.0.1.2
2148 if (tape->engine_version < VERSION_IDENT(3,3,1,0) ||
2149 tape->engine_version >= VERSION_IDENT(4,0,1,2))
2151 Print("This tape version cannot be patched against EM random bug!\n");
2156 property_bitmask = TAPE_PROPERTY_EM_RANDOM_BUG;
2158 use_property_bit = TRUE;
2160 else if (strEqual(mode, "screen_34x34") || strPrefix(mode, "screen_34x34:"))
2162 // this bug only affects team mode tapes
2163 if (tape->num_participating_players == 1)
2165 Print("Only team mode tapes can be patched against screen size bug!\n");
2170 // this bug (that always existed before) was fixed in version 4.2.2.1
2171 if (tape->engine_version >= VERSION_IDENT(4,2,2,1))
2173 Print("This tape version cannot be patched against screen size bug!\n");
2178 int factor = (unpatch_tape ? 1 : 2);
2179 int scr_fieldx_new = SCR_FIELDX_DEFAULT * factor;
2180 int scr_fieldy_new = SCR_FIELDY_DEFAULT * factor;
2182 if (scr_fieldx_new == tape->scr_fieldx &&
2183 scr_fieldy_new == tape->scr_fieldy)
2185 Print("Tape already patched for '%s'!\n", mode);
2190 tape->scr_fieldx = scr_fieldx_new;
2191 tape->scr_fieldy = scr_fieldy_new;
2195 Print("Unknown patch mode '%s'!\n", mode);
2200 // patching tapes using property bits may be used for several patch modes
2201 if (use_property_bit)
2203 byte property_bits = tape->property_bits;
2204 boolean set_property_bit = (unpatch_tape ? FALSE : TRUE);
2206 if (set_property_bit)
2207 property_bits |= property_bitmask;
2209 property_bits &= ~property_bitmask;
2211 if (property_bits == tape->property_bits)
2213 Print("Tape already patched for '%s'!\n", mode);
2218 tape->property_bits = property_bits;
2221 Print("Patching for '%s' ... ", mode);
2226 void PatchTapes(void)
2228 static LevelDirTree *patchtapes_leveldir = NULL;
2229 static int num_tapes_found = 0;
2230 static int num_tapes_patched = 0;
2231 char *mode = global.patchtapes_mode;
2234 if (strEqual(mode, "help"))
2237 Print("Supported patch modes:\n");
2238 Print("- \"em_random_bug\" - use 64-bit random value bug for EM engine\n");
2239 Print("- \"screen_34x34\" - force visible playfield size of 34 x 34\n");
2241 Print("Supported modifiers:\n");
2242 Print("- add \":0\", \":off\" or \":clear\" to patch mode to un-patch tape file\n");
2248 patchtapes_leveldir = getTreeInfoFromIdentifier(leveldir_first,
2249 global.patchtapes_leveldir);
2251 if (patchtapes_leveldir == NULL)
2252 Fail("no such level identifier: '%s'", global.patchtapes_leveldir);
2254 leveldir_current = patchtapes_leveldir;
2256 if (patchtapes_leveldir->first_level < 0)
2257 patchtapes_leveldir->first_level = 0;
2258 if (patchtapes_leveldir->last_level >= MAX_TAPES_PER_SET)
2259 patchtapes_leveldir->last_level = MAX_TAPES_PER_SET - 1;
2262 Print("Patching level tapes for patch mode '%s'\n", mode);
2264 Print("Level series identifier: '%s'\n", patchtapes_leveldir->identifier);
2265 Print("Level series name: '%s'\n", patchtapes_leveldir->name);
2266 Print("Level series author: '%s'\n", patchtapes_leveldir->author);
2267 Print("Number of levels: %d\n", patchtapes_leveldir->levels);
2271 int first_level = patchtapes_leveldir->first_level;
2272 int last_level = patchtapes_leveldir->last_level;
2274 for (i = first_level; i <= last_level; i++)
2276 if (!global.patchtapes_all && !global.patchtapes_level[i])
2279 Print("Tape %03d: ", i);
2284 if (tape.no_valid_file)
2286 Print("(no tape found)\n");
2293 if (PatchTape(&tape, mode))
2295 char *filename = getTapeFilename(i);
2296 char *filename_orig = getStringCat2(filename, ".orig");
2298 if (!fileExists(filename_orig))
2299 rename(filename, filename_orig);
2301 SaveTapeToFilename(filename);
2303 Print("patched tape saved.\n");
2305 num_tapes_patched++;
2311 Print("Number of tapes found: %d\n", num_tapes_found);
2312 Print("Number of tapes patched: %d\n", num_tapes_patched);
2319 // ---------- new tape button stuff -------------------------------------------
2327 } tapebutton_info[NUM_TAPE_BUTTONS] =
2330 IMG_GFX_TAPE_BUTTON_EJECT, &tape.button.eject,
2331 TAPE_CTRL_ID_EJECT, "eject tape"
2334 // (same position as "eject" button)
2335 IMG_GFX_TAPE_BUTTON_EXTRA, &tape.button.eject,
2336 TAPE_CTRL_ID_EXTRA, "extra functions"
2339 IMG_GFX_TAPE_BUTTON_STOP, &tape.button.stop,
2340 TAPE_CTRL_ID_STOP, "stop tape"
2343 IMG_GFX_TAPE_BUTTON_PAUSE, &tape.button.pause,
2344 TAPE_CTRL_ID_PAUSE, "pause tape"
2347 IMG_GFX_TAPE_BUTTON_RECORD, &tape.button.record,
2348 TAPE_CTRL_ID_RECORD, "record tape"
2351 IMG_GFX_TAPE_BUTTON_PLAY, &tape.button.play,
2352 TAPE_CTRL_ID_PLAY, "play tape"
2355 IMG_GFX_TAPE_BUTTON_INSERT_SOLUTION,&tape.button.insert_solution,
2356 TAPE_CTRL_ID_INSERT_SOLUTION, "insert solution tape"
2359 IMG_GFX_TAPE_BUTTON_PLAY_SOLUTION, &tape.button.play_solution,
2360 TAPE_CTRL_ID_PLAY_SOLUTION, "play solution tape"
2364 void CreateTapeButtons(void)
2368 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2370 int graphic = tapebutton_info[i].graphic;
2371 struct GraphicInfo *gfx = &graphic_info[graphic];
2372 struct XY *pos = tapebutton_info[i].pos;
2373 struct GadgetInfo *gi;
2374 int gd_x = gfx->src_x;
2375 int gd_y = gfx->src_y;
2376 int gd_xp = gfx->src_x + gfx->pressed_xoffset;
2377 int gd_yp = gfx->src_y + gfx->pressed_yoffset;
2380 gi = CreateGadget(GDI_CUSTOM_ID, id,
2381 GDI_IMAGE_ID, graphic,
2382 GDI_INFO_TEXT, tapebutton_info[i].infotext,
2385 GDI_WIDTH, gfx->width,
2386 GDI_HEIGHT, gfx->height,
2387 GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
2388 GDI_STATE, GD_BUTTON_UNPRESSED,
2389 GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
2390 GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
2391 GDI_DIRECT_DRAW, FALSE,
2392 GDI_EVENT_MASK, GD_EVENT_RELEASED,
2393 GDI_CALLBACK_ACTION, HandleTapeButtons,
2397 Fail("cannot create gadget");
2399 tape_gadget[id] = gi;
2403 void FreeTapeButtons(void)
2407 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2408 FreeGadget(tape_gadget[i]);
2411 void MapTapeEjectButton(void)
2413 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
2414 MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
2417 void MapTapeWarpButton(void)
2419 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
2420 MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
2423 void MapTapeButtons(void)
2427 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2428 if (i != TAPE_CTRL_ID_EXTRA)
2429 MapGadget(tape_gadget[i]);
2431 if (tape.recording || tape.playing)
2432 MapTapeWarpButton();
2434 if (tape.show_game_buttons)
2435 MapGameButtonsOnTape();
2438 void UnmapTapeButtons(void)
2442 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2443 UnmapGadget(tape_gadget[i]);
2445 if (tape.show_game_buttons)
2446 UnmapGameButtonsOnTape();
2449 void RedrawTapeButtons(void)
2453 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2454 RedrawGadget(tape_gadget[i]);
2456 if (tape.show_game_buttons)
2457 RedrawGameButtonsOnTape();
2460 void RedrawOrRemapTapeButtons(void)
2462 if (tape_gadget[TAPE_CTRL_ID_PLAY]->mapped)
2464 // tape buttons already mapped
2465 RedrawTapeButtons();
2474 static void HandleTapeButtonsExt(int id)
2476 if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
2481 case TAPE_CTRL_ID_EJECT:
2484 if (TAPE_IS_EMPTY(tape))
2488 if (TAPE_IS_EMPTY(tape))
2489 Request("No tape for this level!", REQ_CONFIRM);
2494 SaveTapeChecked(level_nr);
2499 DrawCompleteVideoDisplay();
2502 case TAPE_CTRL_ID_EXTRA:
2505 tape.pause_before_end = !tape.pause_before_end;
2507 DrawVideoDisplayCurrentState();
2509 else if (tape.recording)
2516 case TAPE_CTRL_ID_STOP:
2521 case TAPE_CTRL_ID_PAUSE:
2522 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2526 case TAPE_CTRL_ID_RECORD:
2527 if (TAPE_IS_STOPPED(tape))
2529 TapeStartGameRecording();
2531 else if (tape.pausing)
2533 if (tape.playing) // PLAY -> PAUSE -> RECORD
2534 TapeAppendRecording();
2536 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2541 case TAPE_CTRL_ID_PLAY:
2542 if (tape.recording && tape.pausing) // PAUSE -> RECORD
2544 // ("TAPE_IS_EMPTY(tape)" is TRUE here -- probably fix this)
2546 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2549 if (TAPE_IS_EMPTY(tape))
2552 if (TAPE_IS_STOPPED(tape))
2554 TapeStartGamePlaying();
2556 else if (tape.playing)
2558 if (tape.pausing) // PAUSE -> PLAY
2560 TapeTogglePause(TAPE_TOGGLE_MANUAL | TAPE_TOGGLE_PLAY_PAUSE);
2562 else if (!tape.fast_forward) // PLAY -> FFWD
2564 tape.fast_forward = TRUE;
2566 else if (!tape.warp_forward) // FFWD -> WARP
2568 tape.warp_forward = TRUE;
2570 else if (!tape.deactivate_display) // WARP -> WARP BLIND
2572 tape.deactivate_display = TRUE;
2574 TapeDeactivateDisplayOn();
2576 else // WARP BLIND -> PLAY
2578 tape.fast_forward = FALSE;
2579 tape.warp_forward = FALSE;
2580 tape.deactivate_display = FALSE;
2582 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
2585 DrawVideoDisplayCurrentState();
2590 case TAPE_CTRL_ID_INSERT_SOLUTION:
2591 InsertSolutionTape();
2595 case TAPE_CTRL_ID_PLAY_SOLUTION:
2605 static void HandleTapeButtons(struct GadgetInfo *gi)
2607 HandleTapeButtonsExt(gi->custom_id);
2610 void HandleTapeButtonKeys(Key key)
2612 boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
2613 boolean extra_button_is_active = !eject_button_is_active;
2615 if (key == setup.shortcut.tape_eject && eject_button_is_active)
2616 HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
2617 else if (key == setup.shortcut.tape_extra && extra_button_is_active)
2618 HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
2619 else if (key == setup.shortcut.tape_stop)
2620 HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
2621 else if (key == setup.shortcut.tape_pause)
2622 HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
2623 else if (key == setup.shortcut.tape_record)
2624 HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
2625 else if (key == setup.shortcut.tape_play)
2626 HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);