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 TapeSetDateFromIsoDateString(char *date)
636 // check ISO date string for correct length
637 if (strlen(date) != 10)
640 // check ISO date string for correct format
641 for (i = 0; i < strlen(date); i++)
642 if (((i != 4 && i != 7) && (date[i] < '0' || date[i] > '9')) ||
643 ((i == 4 || i == 7) && (date[i] != '-')))
646 int yy = (date[2] - '0') * 10 + (date[3] - '0');
647 int mm = (date[5] - '0') * 10 + (date[6] - '0');
648 int dd = (date[8] - '0') * 10 + (date[9] - '0');
650 if (mm < 1 || mm > 12 || dd < 1 || dd > 31)
653 tape.date = 10000 * yy + 100 * (mm - 1) + dd;
656 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
658 struct tm *lt = localtime(&epoch_seconds);
660 tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
663 void TapeSetDateFromNow(void)
665 TapeSetDateFromEpochSeconds(time(NULL));
672 CopyTapeToUndoBuffer();
676 tape.length_frames = 0;
677 tape.length_seconds = 0;
679 tape.score_tape_basename[0] = '\0';
681 if (leveldir_current)
683 strncpy(tape.level_identifier, leveldir_current->identifier,
685 tape.level_identifier[MAX_FILENAME_LEN] = '\0';
688 tape.level_nr = level_nr;
689 tape.pos[tape.counter].delay = 0;
693 tape.random_seed = InitRND(level.random_seed);
695 tape.file_version = FILE_VERSION_ACTUAL;
696 tape.game_version = GAME_VERSION_ACTUAL;
697 tape.engine_version = level.game_version;
699 tape.property_bits = TAPE_PROPERTY_NONE;
701 TapeSetDateFromNow();
703 for (i = 0; i < MAX_PLAYERS; i++)
704 tape.player_participates[i] = FALSE;
706 tape.centered_player_nr_next = -1;
707 tape.set_centered_player = FALSE;
709 // set flags for game actions to default values (may be overwritten later)
710 tape.use_key_actions = TRUE;
711 tape.use_mouse_actions = FALSE;
714 static void TapeRewind(void)
717 tape.delay_played = 0;
718 tape.pause_before_end = FALSE;
719 tape.recording = FALSE;
720 tape.playing = FALSE;
721 tape.fast_forward = FALSE;
722 tape.warp_forward = FALSE;
723 tape.deactivate_display = FALSE;
724 tape.auto_play = (global.autoplay_leveldir != NULL);
725 tape.auto_play_level_solved = FALSE;
726 tape.auto_play_level_fixed = FALSE;
727 tape.auto_play_level_not_fixable = FALSE;
728 tape.quick_resume = FALSE;
729 tape.single_step = FALSE;
731 tape.centered_player_nr_next = -1;
732 tape.set_centered_player = FALSE;
734 InitRND(tape.random_seed);
737 static void TapeSetRandomSeed(int random_seed)
739 tape.random_seed = InitRND(random_seed);
742 void TapeStartRecording(int random_seed)
744 if (!TAPE_IS_STOPPED(tape))
749 TapeSetRandomSeed(random_seed);
751 tape.recording = TRUE;
753 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
754 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
755 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
756 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
760 SetDrawDeactivationMask(REDRAW_NONE);
761 audio.sound_deactivated = FALSE;
763 // required here to update video display if tape door is closed
764 if (GetDoorState() & DOOR_CLOSE_2)
765 OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
768 static void TapeStartGameRecording(void)
770 StartGameActions(network.enabled, TRUE, level.random_seed);
773 static void TapeAppendRecording(void)
775 if (!tape.playing || !tape.pausing)
779 tape.playing = FALSE;
780 tape.fast_forward = FALSE;
781 tape.warp_forward = FALSE;
782 tape.pause_before_end = FALSE;
783 tape.deactivate_display = FALSE;
786 tape.recording = TRUE;
790 // set current delay (for last played move)
791 tape.pos[tape.counter].delay = tape.delay_played;
793 tape.property_bits |= TAPE_PROPERTY_REPLAYED;
796 TapeSetDateFromNow();
798 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
799 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
801 UpdateAndDisplayGameControlValues();
804 void TapeHaltRecording(void)
806 // only advance tape counter if any input events have been recorded
807 if (tape.pos[tape.counter].delay > 0)
810 // initialize delay for next tape entry (to be able to continue recording)
811 if (tape.counter < MAX_TAPE_LEN)
812 tape.pos[tape.counter].delay = 0;
814 tape.length = tape.counter;
815 tape.length_frames = GetTapeLengthFrames();
816 tape.length_seconds = GetTapeLengthSeconds();
819 void TapeStopRecording(void)
824 tape.recording = FALSE;
825 tape.pausing = FALSE;
827 DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
828 MapTapeEjectButton();
831 boolean TapeAddAction(byte action[MAX_TAPE_ACTIONS])
835 if (tape.pos[tape.counter].delay > 0) // already stored action
837 boolean changed_events = FALSE;
839 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
840 if (tape.pos[tape.counter].action[i] != action[i])
841 changed_events = TRUE;
843 if (changed_events || tape.pos[tape.counter].delay >= 255)
845 if (tape.counter >= MAX_TAPE_LEN - 1)
849 tape.pos[tape.counter].delay = 0;
852 tape.pos[tape.counter].delay++;
855 if (tape.pos[tape.counter].delay == 0) // store new action
857 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
858 tape.pos[tape.counter].action[i] = action[i];
860 tape.pos[tape.counter].delay++;
868 void TapeRecordAction(byte action_raw[MAX_TAPE_ACTIONS])
870 byte action[MAX_TAPE_ACTIONS];
873 if (!tape.recording) // (record action even when tape is paused)
876 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
877 action[i] = action_raw[i];
879 if (tape.use_key_actions && tape.set_centered_player)
881 for (i = 0; i < MAX_PLAYERS; i++)
882 if (tape.centered_player_nr_next == i ||
883 tape.centered_player_nr_next == -1)
884 action[i] |= KEY_SET_FOCUS;
886 tape.set_centered_player = FALSE;
889 if (GameFrameDelay != GAME_FRAME_DELAY)
890 tape.property_bits |= TAPE_PROPERTY_GAME_SPEED;
892 if (setup.small_game_graphics || SCR_FIELDX >= 2 * SCR_FIELDX_DEFAULT)
893 tape.property_bits |= TAPE_PROPERTY_SMALL_GRAPHICS;
895 if (!TapeAddAction(action))
899 void TapeTogglePause(boolean toggle_mode)
901 if (tape.playing && tape.pausing && (toggle_mode & TAPE_TOGGLE_PLAY_PAUSE))
903 // continue playing in normal mode
904 tape.fast_forward = FALSE;
905 tape.warp_forward = FALSE;
906 tape.deactivate_display = FALSE;
908 tape.pause_before_end = FALSE;
911 tape.pausing = !tape.pausing;
913 if (tape.single_step && (toggle_mode & TAPE_TOGGLE_MANUAL))
914 tape.single_step = FALSE;
916 if (tape.single_step)
917 tape.property_bits |= TAPE_PROPERTY_SINGLE_STEP;
920 tape.property_bits |= TAPE_PROPERTY_PAUSE_MODE;
922 DrawVideoDisplayCurrentState();
924 if (tape.deactivate_display)
927 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
929 TapeDeactivateDisplayOn();
932 if (tape.quick_resume)
934 tape.quick_resume = FALSE;
936 TapeStopWarpForward();
937 TapeAppendRecording();
939 if (!CheckEngineSnapshotSingle())
940 SaveEngineSnapshotSingle();
942 // restart step/move snapshots after quick loading tape
943 SaveEngineSnapshotToListInitial();
945 // do not map undo/redo buttons after quick loading tape
949 if (game_status == GAME_MODE_PLAYING)
951 if (setup.show_load_save_buttons &&
952 setup.show_undo_redo_buttons &&
953 CheckEngineSnapshotList())
956 MapUndoRedoButtons();
957 else if (!tape.single_step)
958 MapLoadSaveButtons();
961 ModifyPauseButtons();
964 // stop tape when leaving auto-pause after completely replaying tape
965 if (tape.playing && !tape.pausing && tape.counter >= tape.length)
969 void TapeStartPlaying(void)
971 if (TAPE_IS_EMPTY(tape))
974 if (!TAPE_IS_STOPPED(tape))
981 DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
982 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
983 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
984 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
988 SetDrawDeactivationMask(REDRAW_NONE);
989 audio.sound_deactivated = FALSE;
992 static void TapeStartGamePlaying(void)
999 void TapeStopPlaying(void)
1001 tape.playing = FALSE;
1002 tape.pausing = FALSE;
1004 if (tape.warp_forward)
1005 TapeStopWarpForward();
1007 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
1008 MapTapeEjectButton();
1011 byte *TapePlayAction(void)
1013 int update_delay = FRAMES_PER_SECOND / 2;
1014 boolean update_video_display = (FrameCounter % update_delay == 0);
1015 boolean update_draw_label_on = ((FrameCounter / update_delay) % 2 == 1);
1016 static byte action[MAX_TAPE_ACTIONS];
1019 if (!tape.playing || tape.pausing)
1022 if (tape.pause_before_end) // stop some seconds before end of tape
1024 if (TapeTime > (int)tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
1026 TapeStopWarpForward();
1027 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1029 if (setup.autorecord_after_replay)
1030 TapeAppendRecording();
1036 if (tape.counter >= tape.length) // end of tape reached
1038 if (!tape.auto_play)
1040 TapeStopWarpForward();
1041 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1043 if (setup.autorecord_after_replay)
1044 TapeAppendRecording();
1054 if (update_video_display && !tape.deactivate_display)
1058 if (tape.warp_forward)
1059 state |= VIDEO_STATE_WARP(update_draw_label_on);
1060 else if (tape.fast_forward)
1061 state |= VIDEO_STATE_FFWD(update_draw_label_on);
1063 if (tape.pause_before_end)
1064 state |= VIDEO_STATE_PBEND(update_draw_label_on);
1066 // draw labels and symbols separately to prevent labels overlapping symbols
1067 DrawVideoDisplayLabel(state);
1068 DrawVideoDisplaySymbol(state);
1071 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
1072 action[i] = tape.pos[tape.counter].action[i];
1074 #if DEBUG_TAPE_WHEN_PLAYING
1075 DebugContinued("", "%05d", FrameCounter);
1076 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
1077 DebugContinued("", " %08x", action[i]);
1078 DebugContinued("tape:play", "\n");
1081 tape.set_centered_player = FALSE;
1082 tape.centered_player_nr_next = -999;
1084 if (tape.use_key_actions)
1086 for (i = 0; i < MAX_PLAYERS; i++)
1088 if (action[i] & KEY_SET_FOCUS)
1090 tape.set_centered_player = TRUE;
1091 tape.centered_player_nr_next =
1092 (tape.centered_player_nr_next == -999 ? i : -1);
1095 action[i] &= ~KEY_SET_FOCUS;
1099 tape.delay_played++;
1100 if (tape.delay_played >= tape.pos[tape.counter].delay)
1103 tape.delay_played = 0;
1107 PrintTapeReplayProgress(FALSE);
1109 if (options.tape_log_filename != NULL)
1110 WriteTapeLogfile(action);
1118 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1120 TapeStopRecording();
1123 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
1125 if (tape.date && tape.length)
1127 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
1128 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
1129 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
1133 static void TapeStopGameOrTape(boolean stop_game)
1135 if (score_info_tape_play || (!tape.playing && stop_game))
1136 RequestQuitGame(FALSE);
1141 void TapeStopGame(void)
1143 if (game_status == GAME_MODE_MAIN)
1146 TapeStopGameOrTape(TRUE);
1149 void TapeStopTape(void)
1151 TapeStopGameOrTape(FALSE);
1154 unsigned int GetTapeLengthFrames(void)
1156 unsigned int tape_length_frames = 0;
1159 if (TAPE_IS_EMPTY(tape))
1162 for (i = 0; i < tape.length; i++)
1163 tape_length_frames += tape.pos[i].delay;
1165 return tape_length_frames;
1168 unsigned int GetTapeLengthSeconds(void)
1170 return (GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
1173 static float GetTapeLengthSecondsFloat(void)
1175 return ((float)GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
1178 static void TapeStartWarpForward(int mode)
1180 tape.fast_forward = (mode & AUTOPLAY_FFWD);
1181 tape.warp_forward = (mode & AUTOPLAY_WARP);
1182 tape.deactivate_display = (mode & AUTOPLAY_WARP_NO_DISPLAY);
1184 tape.pausing = FALSE;
1186 if (tape.deactivate_display)
1187 TapeDeactivateDisplayOn();
1189 DrawVideoDisplayCurrentState();
1192 static void TapeStopWarpForward(void)
1194 tape.fast_forward = FALSE;
1195 tape.warp_forward = FALSE;
1196 tape.deactivate_display = FALSE;
1198 tape.pause_before_end = FALSE;
1200 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
1202 DrawVideoDisplayCurrentState();
1205 static void TapeSingleStep(void)
1207 if (network.enabled)
1211 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1213 tape.single_step = !tape.single_step;
1215 DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
1218 void TapeQuickSave(void)
1220 if (game_status != GAME_MODE_PLAYING)
1222 Request("No game that could be saved!", REQ_CONFIRM);
1227 if (!tape.recording)
1229 Request("No recording that could be saved!", REQ_CONFIRM);
1234 TapeHaltRecording(); // prepare tape for saving on-the-fly
1236 if (TAPE_IS_EMPTY(tape))
1238 Request("No tape that could be saved!", REQ_CONFIRM);
1243 tape.property_bits |= TAPE_PROPERTY_SNAPSHOT;
1245 if (SaveTapeChecked(tape.level_nr))
1246 SaveEngineSnapshotSingle();
1249 void TapeQuickLoad(void)
1251 char *filename = getTapeFilename(level_nr);
1253 if (!fileExists(filename))
1255 Request("No tape for this level!", REQ_CONFIRM);
1260 if (tape.recording && !Request("Stop recording and load tape?",
1261 REQ_ASK | REQ_STAY_CLOSED))
1263 OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1268 if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
1271 if (CheckEngineSnapshotSingle())
1273 TapeStartGamePlaying();
1275 LoadEngineSnapshotSingle();
1277 DrawCompleteVideoDisplay();
1279 tape.playing = TRUE;
1280 tape.pausing = TRUE;
1282 TapeStopWarpForward();
1283 TapeAppendRecording();
1285 // restart step/move snapshots after quick loading tape
1286 SaveEngineSnapshotToListInitial();
1288 if (FrameCounter > 0)
1297 if (!TAPE_IS_EMPTY(tape))
1299 TapeStartGamePlaying();
1300 TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
1302 tape.quick_resume = TRUE;
1303 tape.property_bits |= TAPE_PROPERTY_SNAPSHOT;
1305 else // this should not happen (basically checked above)
1307 int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1309 Request("No tape for this level!", REQ_CONFIRM | reopen_door);
1313 static boolean checkRestartGame(char *message)
1315 if (game_status == GAME_MODE_MAIN)
1318 if (!hasStartedNetworkGame())
1321 if (level_editor_test_game)
1324 if (game.all_players_gone)
1327 if (!setup.ask_on_quit_game)
1330 if (Request(message, REQ_ASK | REQ_STAY_CLOSED))
1333 OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1338 void TapeRestartGame(void)
1340 if (score_info_tape_play)
1342 TapeStartGamePlaying();
1347 if (!checkRestartGame("Restart game?"))
1350 StartGameActions(network.enabled, setup.autorecord, level.random_seed);
1353 void TapeReplayAndPauseBeforeEnd(void)
1355 if (score_info_tape_play)
1358 if (TAPE_IS_EMPTY(tape) && !tape.recording)
1360 Request("No tape for this level!", REQ_CONFIRM);
1365 if (!checkRestartGame("Replay game and pause before end?"))
1369 TapeStartGamePlaying();
1370 TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
1372 tape.pause_before_end = TRUE;
1373 tape.quick_resume = TRUE;
1376 boolean hasSolutionTape(void)
1378 boolean tape_file_exists = fileExists(getSolutionTapeFilename(level_nr));
1379 boolean level_has_tape = (level.game_engine_type == GAME_ENGINE_TYPE_SP &&
1380 level.native_sp_level->demo.is_available);
1382 return (tape_file_exists || level_has_tape);
1385 boolean InsertSolutionTape(void)
1387 if (!hasSolutionTape())
1389 Request("No solution tape for this level!", REQ_CONFIRM);
1394 if (!TAPE_IS_STOPPED(tape))
1397 // if tape recorder already contains a tape, remove it without asking
1400 LoadSolutionTape(level_nr);
1402 DrawCompleteVideoDisplay();
1404 if (TAPE_IS_EMPTY(tape))
1406 Request("Loading solution tape for this level failed!", REQ_CONFIRM);
1414 boolean PlaySolutionTape(void)
1416 if (!InsertSolutionTape())
1419 TapeStartGamePlaying();
1424 static boolean PlayScoreTape_WaitForDownload(void)
1426 DelayCounter download_delay = { 10000 };
1428 ResetDelayCounter(&download_delay);
1430 // wait for score tape to be successfully downloaded (and fail on timeout)
1431 while (!server_scores.tape_downloaded)
1433 if (DelayReached(&download_delay))
1436 UPDATE_BUSY_STATE_NOT_LOADING();
1444 boolean PlayScoreTape(int entry_nr)
1446 struct ScoreEntry *entry = &scores.entry[entry_nr];
1447 char *tape_filename =
1449 getScoreTapeFilename(entry->tape_basename, level_nr) :
1450 getScoreCacheTapeFilename(entry->tape_basename, level_nr));
1451 boolean download_tape = (!fileExists(tape_filename));
1453 if (download_tape && entry->id == -1)
1455 FadeSkipNextFadeIn();
1457 Request("Cannot find score tape!", REQ_CONFIRM);
1462 server_scores.tape_downloaded = FALSE;
1465 ApiGetScoreTapeAsThread(level_nr, entry->id, entry->tape_basename);
1467 SetGameStatus(GAME_MODE_PLAYING);
1469 FadeOut(REDRAW_FIELD);
1471 if (download_tape && !PlayScoreTape_WaitForDownload())
1473 SetGameStatus(GAME_MODE_SCOREINFO);
1476 Request("Cannot download score tape from score server!", REQ_CONFIRM);
1481 if (!TAPE_IS_STOPPED(tape))
1484 // if tape recorder already contains a tape, remove it without asking
1487 if (entry->id == -1)
1488 LoadScoreTape(entry->tape_basename, level_nr);
1490 LoadScoreCacheTape(entry->tape_basename, level_nr);
1492 if (TAPE_IS_EMPTY(tape))
1494 SetGameStatus(GAME_MODE_SCOREINFO);
1497 Request("Cannot load score tape for this level!", REQ_CONFIRM);
1502 FadeSkipNextFadeOut();
1504 TapeStartGamePlaying();
1506 score_info_tape_play = TRUE;
1511 static boolean checkTapesFromSameLevel(struct TapeInfo *t1, struct TapeInfo *t2)
1513 return (strEqual(t1->level_identifier, t2->level_identifier) &&
1514 t1->level_nr == t2->level_nr);
1517 static void CopyTape(struct TapeInfo *tape_from, struct TapeInfo *tape_to)
1519 *tape_to = *tape_from;
1522 static void SwapTapes(struct TapeInfo *t1, struct TapeInfo *t2)
1524 struct TapeInfo tmp = *t1;
1530 static void CopyTapeToUndoBuffer(void)
1532 // copy tapes to undo buffer if large enough (or larger than last undo tape)
1533 // or if the last undo tape is from a different level set or level number
1534 if (tape.length_seconds >= TAPE_MIN_SECONDS_FOR_UNDO_BUFFER ||
1535 tape.length_seconds >= tape_undo_buffer.length_seconds ||
1536 !checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1538 CopyTape(&tape, &tape_undo_buffer);
1544 // only undo tapes from same level set and with same level number
1545 if (!checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1548 if (!TAPE_IS_STOPPED(tape))
1551 // swap last recorded tape with undo buffer, so undo can be reversed
1552 SwapTapes(&tape, &tape_undo_buffer);
1554 DrawCompleteVideoDisplay();
1557 void FixTape_ForceSinglePlayer(void)
1561 /* fix single-player tapes that contain player input for more than one
1562 player (due to a bug in 3.3.1.2 and earlier versions), which results
1563 in playing levels with more than one player in multi-player mode,
1564 even though the tape was originally recorded in single-player mode */
1566 // remove player input actions for all players but the first one
1567 for (i = 1; i < MAX_PLAYERS; i++)
1568 tape.player_participates[i] = FALSE;
1570 tape.changed = TRUE;
1574 // ----------------------------------------------------------------------------
1575 // tape autoplay functions
1576 // ----------------------------------------------------------------------------
1578 static TreeInfo *getNextValidAutoPlayEntry(TreeInfo *node)
1580 node = getNextValidTreeInfoEntry(node);
1582 while (node && node->is_copy)
1583 node = getNextValidTreeInfoEntry(node);
1588 static TreeInfo *getFirstValidAutoPlayEntry(TreeInfo *node)
1590 node = getFirstValidTreeInfoEntry(node);
1592 if (node && node->is_copy)
1593 return getNextValidAutoPlayEntry(node);
1598 static void AutoPlayTapes_SetScoreEntry(int score, int time)
1600 char *name = (options.mytapes ? setup.player_name : options.player_name);
1602 // set unique basename for score tape (for uploading to score server)
1603 strcpy(tape.score_tape_basename, getScoreTapeBasename(name));
1605 // store score in first score entry
1606 scores.last_added = 0;
1608 struct ScoreEntry *entry = &scores.entry[scores.last_added];
1610 strncpy(entry->tape_basename, tape.score_tape_basename, MAX_FILENAME_LEN);
1611 strncpy(entry->name, setup.player_name, MAX_PLAYER_NAME_LEN);
1613 entry->score = score;
1616 PrintNoLog("- uploading score tape to score server ... ");
1618 server_scores.uploaded = FALSE;
1621 static boolean AutoPlayTapes_WaitForUpload(void)
1623 DelayCounter upload_delay = { 10000 };
1625 ResetDelayCounter(&upload_delay);
1627 // wait for score tape to be successfully uploaded (and fail on timeout)
1628 while (!server_scores.uploaded)
1630 if (DelayReached(&upload_delay))
1633 Print("- uploading score tape to score server - TIMEOUT.\n");
1635 if (program.headless)
1636 Fail("cannot upload score tape to score server");
1641 UPDATE_BUSY_STATE();
1647 Print("- uploading score tape to score server - uploaded.\n");
1652 static int AutoPlayTapesExt(boolean initialize)
1654 static struct AutoPlayInfo autoplay;
1655 static int num_tapes = 0;
1656 static int patch_nr = 0;
1657 static char *patch_name[] =
1665 static int patch_version_first[] =
1667 VERSION_IDENT(0,0,0,0),
1668 VERSION_IDENT(3,3,1,0),
1669 VERSION_IDENT(0,0,0,0),
1673 static int patch_version_last[] =
1675 VERSION_IDENT(9,9,9,9),
1676 VERSION_IDENT(4,0,1,1),
1677 VERSION_IDENT(4,2,2,0),
1681 static byte patch_property_bit[] =
1684 TAPE_PROPERTY_EM_RANDOM_BUG,
1689 LevelDirTree *leveldir_current_last = leveldir_current;
1690 boolean init_level_set = FALSE;
1691 int level_nr_last = level_nr;
1696 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1698 if (tape.auto_play_level_solved)
1702 // level solved by patched tape -- save fixed tape
1703 char *filename = getTapeFilename(level_nr);
1704 char *filename_orig = getStringCat2(filename, ".orig");
1706 // create backup from old tape, if not yet existing
1707 if (!fileExists(filename_orig))
1708 rename(filename, filename_orig);
1710 SaveTapeToFilename(filename);
1712 tape.auto_play_level_fixed = TRUE;
1713 autoplay.num_tapes_patched++;
1716 // continue with next tape
1719 else if (patch_name[patch_nr + 1] != NULL)
1721 // level not solved by patched tape -- continue with next patch
1726 // level not solved by any patched tape -- continue with next tape
1727 tape.auto_play_level_not_fixable = TRUE;
1732 // just finished auto-playing tape
1733 PrintTapeReplayProgress(TRUE);
1735 if (options.tape_log_filename != NULL)
1738 if (global.autoplay_mode == AUTOPLAY_MODE_SAVE &&
1739 tape.auto_play_level_solved)
1741 AutoPlayTapes_SetScoreEntry(game.score_final, game.score_time_final);
1743 if (leveldir_current)
1745 // the tape's level set identifier may differ from current level set
1746 strncpy(tape.level_identifier, leveldir_current->identifier,
1748 tape.level_identifier[MAX_FILENAME_LEN] = '\0';
1750 // the tape's level number may differ from current level number
1751 tape.level_nr = level_nr;
1754 // save score tape to upload to server; may be required for some reasons:
1755 // * level set identifier in solution tapes may differ from level set
1756 // * level set identifier is missing (old-style tape without INFO chunk)
1757 // * solution tape may have native format (like Supaplex solution files)
1759 SaveScoreTape(level_nr);
1760 SaveServerScore(level_nr, TRUE);
1762 AutoPlayTapes_WaitForUpload();
1766 autoplay.num_levels_played++;
1768 if (tape.auto_play_level_solved)
1769 autoplay.num_levels_solved++;
1771 if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1772 autoplay.level_failed[level_nr] = !tape.auto_play_level_solved;
1776 if (strEqual(global.autoplay_leveldir, "ALL"))
1778 autoplay.all_levelsets = TRUE;
1780 // tape mass-uploading only allowed for private tapes
1781 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
1782 options.mytapes = TRUE;
1785 if ((global.autoplay_mode == AUTOPLAY_MODE_SAVE ||
1786 global.autoplay_mode == AUTOPLAY_MODE_UPLOAD) &&
1788 options.player_name == NULL)
1790 Fail("specify player name when uploading solution tapes");
1793 if (global.autoplay_mode != AUTOPLAY_MODE_UPLOAD)
1794 DrawCompleteVideoDisplay();
1796 if (program.headless)
1798 audio.sound_enabled = FALSE;
1799 setup.engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_OFF);
1802 if (strSuffix(global.autoplay_leveldir, ".tape"))
1804 autoplay.tape_filename = global.autoplay_leveldir;
1806 if (!fileExists(autoplay.tape_filename))
1807 Fail("tape file '%s' does not exist", autoplay.tape_filename);
1809 LoadTapeFromFilename(autoplay.tape_filename);
1811 if (tape.no_valid_file)
1812 Fail("cannot load tape file '%s'", autoplay.tape_filename);
1814 if (tape.no_info_chunk && !options.identifier)
1815 Fail("cannot get levelset from tape file '%s'", autoplay.tape_filename);
1817 if (tape.no_info_chunk && !options.level_nr)
1818 Fail("cannot get level nr from tape file '%s'", autoplay.tape_filename);
1820 global.autoplay_leveldir = tape.level_identifier;
1822 if (options.identifier != NULL)
1823 global.autoplay_leveldir = options.identifier;
1825 if (options.level_nr != NULL)
1826 tape.level_nr = atoi(options.level_nr);
1828 if (tape.level_nr >= 0 && tape.level_nr < MAX_TAPES_PER_SET)
1829 global.autoplay_level[tape.level_nr] = TRUE;
1831 global.autoplay_all = FALSE;
1832 options.mytapes = FALSE;
1835 if (autoplay.all_levelsets)
1837 // start auto-playing first level set
1838 autoplay.leveldir = getFirstValidAutoPlayEntry(leveldir_first);
1842 // auto-play selected level set
1843 autoplay.leveldir = getTreeInfoFromIdentifier(leveldir_first,
1844 global.autoplay_leveldir);
1847 if (autoplay.leveldir == NULL)
1848 Fail("no such level identifier: '%s'", global.autoplay_leveldir);
1850 // only private tapes may be modified
1851 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1852 options.mytapes = TRUE;
1854 // set timestamp for batch tape upload
1855 global.autoplay_time = time(NULL);
1859 init_level_set = TRUE;
1866 leveldir_current = autoplay.leveldir;
1868 if (autoplay.leveldir->first_level < 0)
1869 autoplay.leveldir->first_level = 0;
1870 if (autoplay.leveldir->last_level >= MAX_TAPES_PER_SET)
1871 autoplay.leveldir->last_level = MAX_TAPES_PER_SET - 1;
1873 autoplay.level_nr = autoplay.leveldir->first_level;
1875 autoplay.num_levels_played = 0;
1876 autoplay.num_levels_solved = 0;
1877 autoplay.num_tapes_patched = 0;
1878 autoplay.num_tape_missing = 0;
1880 for (i = 0; i < MAX_TAPES_PER_SET; i++)
1881 autoplay.level_failed[i] = FALSE;
1883 PrintTapeReplayHeader(&autoplay);
1885 init_level_set = FALSE;
1888 if (autoplay.all_levelsets && global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
1890 boolean skip_levelset = FALSE;
1892 if (!directoryExists(getTapeDir(autoplay.leveldir->subdir)))
1894 Print("No tape directory for this level set found -- skipping.\n");
1896 skip_levelset = TRUE;
1899 if (CheckTapeDirectoryUploadsComplete(autoplay.leveldir->subdir))
1901 Print("All tapes for this level set already uploaded -- skipping.\n");
1903 skip_levelset = TRUE;
1908 PrintTapeReplaySummary(&autoplay);
1910 // continue with next level set
1911 autoplay.leveldir = getNextValidAutoPlayEntry(autoplay.leveldir);
1913 // all level sets processed
1914 if (autoplay.leveldir == NULL)
1917 init_level_set = TRUE;
1923 if (global.autoplay_mode != AUTOPLAY_MODE_FIX || patch_nr == 0)
1924 level_nr = autoplay.level_nr++;
1926 UPDATE_BUSY_STATE();
1928 // check if all tapes for this level set have been processed
1929 if (level_nr > autoplay.leveldir->last_level)
1931 PrintTapeReplaySummary(&autoplay);
1933 if (!autoplay.all_levelsets)
1936 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
1937 MarkTapeDirectoryUploadsAsComplete(autoplay.leveldir->subdir);
1939 // continue with next level set
1940 autoplay.leveldir = getNextValidAutoPlayEntry(autoplay.leveldir);
1942 // all level sets processed
1943 if (autoplay.leveldir == NULL)
1946 init_level_set = TRUE;
1951 // set patch info (required for progress output)
1952 strcpy(tape_patch_info, "");
1953 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1954 snprintf(tape_patch_info, MAX_OUTPUT_LINESIZE, "[%-13s] ",
1955 patch_name[patch_nr]);
1957 if (!global.autoplay_all && !global.autoplay_level[level_nr])
1960 // speed things up in case of missing private tapes (skip loading level)
1961 if (options.mytapes && !fileExists(getTapeFilename(level_nr)))
1963 autoplay.num_tape_missing++;
1965 Print("Tape %03d: (no tape found)\n", level_nr);
1971 TapeRewind(); // needed to reset "tape.auto_play_level_solved"
1973 LoadLevel(level_nr);
1975 if (level.no_level_file || level.no_valid_file)
1977 Print("Tape %03d: (no level found)\n", level_nr);
1983 // ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY
1984 Print("Tape %03d: (only testing level)\n", level_nr);
1988 if (autoplay.tape_filename)
1989 LoadTapeFromFilename(autoplay.tape_filename);
1990 else if (options.mytapes)
1993 LoadSolutionTape(level_nr);
1995 if (tape.no_valid_file)
1997 autoplay.num_tape_missing++;
1999 Print("Tape %03d: (no tape found)\n", level_nr);
2004 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
2006 boolean skip_patch = FALSE;
2008 if (tape.engine_version < patch_version_first[patch_nr] ||
2009 tape.engine_version > patch_version_last[patch_nr])
2011 Print("Tape %03d %s[%02d:%02d]: (%s %d.%d.%d.%d) - skipped.\n",
2012 level_nr, tape_patch_info,
2013 tape.length_seconds / 60, tape.length_seconds % 60,
2014 "not suitable for version",
2015 (tape.engine_version / 1000000) % 100,
2016 (tape.engine_version / 10000 ) % 100,
2017 (tape.engine_version / 100 ) % 100,
2018 (tape.engine_version ) % 100);
2023 if (strEqual(patch_name[patch_nr], "screen_34x34") &&
2024 tape.num_participating_players == 1)
2026 Print("Tape %03d %s[%02d:%02d]: (%s) - skipped.\n",
2027 level_nr, tape_patch_info,
2028 tape.length_seconds / 60, tape.length_seconds % 60,
2029 "not suitable for single player tapes");
2036 if (patch_name[patch_nr + 1] != NULL)
2038 // continue with next patch
2043 // continue with next tape
2050 if (strEqual(patch_name[patch_nr], "screen_34x34"))
2052 tape.scr_fieldx = SCR_FIELDX_DEFAULT * 2;
2053 tape.scr_fieldy = SCR_FIELDY_DEFAULT * 2;
2057 tape.property_bits |= patch_property_bit[patch_nr];
2063 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
2065 boolean use_temporary_tape_file = FALSE;
2067 Print("Tape %03d:\n", level_nr);
2069 AutoPlayTapes_SetScoreEntry(0, 0);
2071 if (autoplay.tape_filename == NULL)
2073 autoplay.tape_filename = (options.mytapes ? getTapeFilename(level_nr) :
2074 getDefaultSolutionTapeFilename(level_nr));
2076 if (!fileExists(autoplay.tape_filename))
2078 // non-standard or incorrect solution tape -- save to temporary file
2079 autoplay.tape_filename = getTemporaryTapeFilename();
2081 SaveTapeToFilename(autoplay.tape_filename);
2083 use_temporary_tape_file = TRUE;
2087 SaveServerScoreFromFile(level_nr, TRUE, autoplay.tape_filename);
2089 boolean success = AutoPlayTapes_WaitForUpload();
2091 if (use_temporary_tape_file)
2092 unlink(autoplay.tape_filename);
2094 // required for uploading multiple tapes
2095 autoplay.tape_filename = NULL;
2099 num_tapes = -num_tapes;
2109 if (options.tape_log_filename != NULL)
2112 TapeStartGamePlaying();
2113 TapeStartWarpForward(global.autoplay_mode);
2115 autoplay.last_level_nr = level_nr;
2120 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
2126 Print("SUMMARY: %d tapes uploaded.\n", num_tapes);
2128 Print("SUMMARY: Uploading tapes failed.\n");
2133 // clear timestamp for batch tape upload (required after interactive upload)
2134 global.autoplay_time = 0;
2136 // exit if running headless or if visually auto-playing tapes
2137 if (program.headless || global.autoplay_mode != AUTOPLAY_MODE_UPLOAD)
2140 // when running interactively, restore last selected level set and number
2141 leveldir_current = leveldir_current_last;
2142 level_nr = level_nr_last;
2147 int AutoPlayTapes(void)
2149 return AutoPlayTapesExt(TRUE);
2152 int AutoPlayTapesContinue(void)
2154 return AutoPlayTapesExt(FALSE);
2158 // ----------------------------------------------------------------------------
2159 // tape patch functions
2160 // ----------------------------------------------------------------------------
2162 static boolean PatchTape(struct TapeInfo *tape, char *mode)
2164 Print("[%d.%d.%d.%d]: ",
2165 (tape->engine_version / 1000000) % 100,
2166 (tape->engine_version / 10000 ) % 100,
2167 (tape->engine_version / 100 ) % 100,
2168 (tape->engine_version ) % 100);
2170 if (strEqual(mode, "info"))
2172 Print("property bits == 0x%02x\n", tape->property_bits);
2177 boolean unpatch_tape = FALSE;
2178 boolean use_property_bit = FALSE;
2179 byte property_bitmask = 0;
2181 if (strSuffix(mode, ":0") ||
2182 strSuffix(mode, ":off") ||
2183 strSuffix(mode, ":clear"))
2184 unpatch_tape = TRUE;
2186 if (strEqual(mode, "em_random_bug") || strPrefix(mode, "em_random_bug:"))
2188 // this bug was introduced in version 3.3.1.0 and fixed in version 4.0.1.2
2189 if (tape->engine_version < VERSION_IDENT(3,3,1,0) ||
2190 tape->engine_version >= VERSION_IDENT(4,0,1,2))
2192 Print("This tape version cannot be patched against EM random bug!\n");
2197 property_bitmask = TAPE_PROPERTY_EM_RANDOM_BUG;
2199 use_property_bit = TRUE;
2201 else if (strEqual(mode, "screen_34x34") || strPrefix(mode, "screen_34x34:"))
2203 // this bug only affects team mode tapes
2204 if (tape->num_participating_players == 1)
2206 Print("Only team mode tapes can be patched against screen size bug!\n");
2211 // this bug (that always existed before) was fixed in version 4.2.2.1
2212 if (tape->engine_version >= VERSION_IDENT(4,2,2,1))
2214 Print("This tape version cannot be patched against screen size bug!\n");
2219 int factor = (unpatch_tape ? 1 : 2);
2220 int scr_fieldx_new = SCR_FIELDX_DEFAULT * factor;
2221 int scr_fieldy_new = SCR_FIELDY_DEFAULT * factor;
2223 if (scr_fieldx_new == tape->scr_fieldx &&
2224 scr_fieldy_new == tape->scr_fieldy)
2226 Print("Tape already patched for '%s'!\n", mode);
2231 tape->scr_fieldx = scr_fieldx_new;
2232 tape->scr_fieldy = scr_fieldy_new;
2236 Print("Unknown patch mode '%s'!\n", mode);
2241 // patching tapes using property bits may be used for several patch modes
2242 if (use_property_bit)
2244 byte property_bits = tape->property_bits;
2245 boolean set_property_bit = (unpatch_tape ? FALSE : TRUE);
2247 if (set_property_bit)
2248 property_bits |= property_bitmask;
2250 property_bits &= ~property_bitmask;
2252 if (property_bits == tape->property_bits)
2254 Print("Tape already patched for '%s'!\n", mode);
2259 tape->property_bits = property_bits;
2262 Print("Patching for '%s' ... ", mode);
2267 void PatchTapes(void)
2269 static LevelDirTree *patchtapes_leveldir = NULL;
2270 static int num_tapes_found = 0;
2271 static int num_tapes_patched = 0;
2272 char *mode = global.patchtapes_mode;
2275 if (strEqual(mode, "help"))
2278 Print("Supported patch modes:\n");
2279 Print("- \"em_random_bug\" - use 64-bit random value bug for EM engine\n");
2280 Print("- \"screen_34x34\" - force visible playfield size of 34 x 34\n");
2282 Print("Supported modifiers:\n");
2283 Print("- add \":0\", \":off\" or \":clear\" to patch mode to un-patch tape file\n");
2289 patchtapes_leveldir = getTreeInfoFromIdentifier(leveldir_first,
2290 global.patchtapes_leveldir);
2292 if (patchtapes_leveldir == NULL)
2293 Fail("no such level identifier: '%s'", global.patchtapes_leveldir);
2295 leveldir_current = patchtapes_leveldir;
2297 if (patchtapes_leveldir->first_level < 0)
2298 patchtapes_leveldir->first_level = 0;
2299 if (patchtapes_leveldir->last_level >= MAX_TAPES_PER_SET)
2300 patchtapes_leveldir->last_level = MAX_TAPES_PER_SET - 1;
2303 Print("Patching level tapes for patch mode '%s'\n", mode);
2305 Print("Level series identifier: '%s'\n", patchtapes_leveldir->identifier);
2306 Print("Level series name: '%s'\n", patchtapes_leveldir->name);
2307 Print("Level series author: '%s'\n", patchtapes_leveldir->author);
2308 Print("Number of levels: %d\n", patchtapes_leveldir->levels);
2312 int first_level = patchtapes_leveldir->first_level;
2313 int last_level = patchtapes_leveldir->last_level;
2315 for (i = first_level; i <= last_level; i++)
2317 if (!global.patchtapes_all && !global.patchtapes_level[i])
2320 Print("Tape %03d: ", i);
2325 if (tape.no_valid_file)
2327 Print("(no tape found)\n");
2334 if (PatchTape(&tape, mode))
2336 char *filename = getTapeFilename(i);
2337 char *filename_orig = getStringCat2(filename, ".orig");
2339 if (!fileExists(filename_orig))
2340 rename(filename, filename_orig);
2342 SaveTapeToFilename(filename);
2344 Print("patched tape saved.\n");
2346 num_tapes_patched++;
2352 Print("Number of tapes found: %d\n", num_tapes_found);
2353 Print("Number of tapes patched: %d\n", num_tapes_patched);
2360 // ---------- new tape button stuff -------------------------------------------
2368 } tapebutton_info[NUM_TAPE_BUTTONS] =
2371 IMG_GFX_TAPE_BUTTON_EJECT, &tape.button.eject,
2372 TAPE_CTRL_ID_EJECT, "eject tape"
2375 // (same position as "eject" button)
2376 IMG_GFX_TAPE_BUTTON_EXTRA, &tape.button.eject,
2377 TAPE_CTRL_ID_EXTRA, "extra functions"
2380 IMG_GFX_TAPE_BUTTON_STOP, &tape.button.stop,
2381 TAPE_CTRL_ID_STOP, "stop tape"
2384 IMG_GFX_TAPE_BUTTON_PAUSE, &tape.button.pause,
2385 TAPE_CTRL_ID_PAUSE, "pause tape"
2388 IMG_GFX_TAPE_BUTTON_RECORD, &tape.button.record,
2389 TAPE_CTRL_ID_RECORD, "record tape"
2392 IMG_GFX_TAPE_BUTTON_PLAY, &tape.button.play,
2393 TAPE_CTRL_ID_PLAY, "play tape"
2396 IMG_GFX_TAPE_BUTTON_INSERT_SOLUTION,&tape.button.insert_solution,
2397 TAPE_CTRL_ID_INSERT_SOLUTION, "insert solution tape"
2400 IMG_GFX_TAPE_BUTTON_PLAY_SOLUTION, &tape.button.play_solution,
2401 TAPE_CTRL_ID_PLAY_SOLUTION, "play solution tape"
2405 void CreateTapeButtons(void)
2409 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2411 int graphic = tapebutton_info[i].graphic;
2412 struct GraphicInfo *gfx = &graphic_info[graphic];
2413 struct XY *pos = tapebutton_info[i].pos;
2414 struct GadgetInfo *gi;
2415 int gd_x = gfx->src_x;
2416 int gd_y = gfx->src_y;
2417 int gd_xp = gfx->src_x + gfx->pressed_xoffset;
2418 int gd_yp = gfx->src_y + gfx->pressed_yoffset;
2421 gi = CreateGadget(GDI_CUSTOM_ID, id,
2422 GDI_IMAGE_ID, graphic,
2423 GDI_INFO_TEXT, tapebutton_info[i].infotext,
2426 GDI_WIDTH, gfx->width,
2427 GDI_HEIGHT, gfx->height,
2428 GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
2429 GDI_STATE, GD_BUTTON_UNPRESSED,
2430 GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
2431 GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
2432 GDI_DIRECT_DRAW, FALSE,
2433 GDI_EVENT_MASK, GD_EVENT_RELEASED,
2434 GDI_CALLBACK_ACTION, HandleTapeButtons,
2438 Fail("cannot create gadget");
2440 tape_gadget[id] = gi;
2444 void FreeTapeButtons(void)
2448 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2449 FreeGadget(tape_gadget[i]);
2452 void MapTapeEjectButton(void)
2454 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
2455 MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
2458 void MapTapeWarpButton(void)
2460 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
2461 MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
2464 void MapTapeButtons(void)
2468 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2469 if (i != TAPE_CTRL_ID_EXTRA)
2470 MapGadget(tape_gadget[i]);
2472 if (tape.recording || tape.playing)
2473 MapTapeWarpButton();
2475 if (tape.show_game_buttons)
2476 MapGameButtonsOnTape();
2479 void UnmapTapeButtons(void)
2483 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2484 UnmapGadget(tape_gadget[i]);
2486 if (tape.show_game_buttons)
2487 UnmapGameButtonsOnTape();
2490 void RedrawTapeButtons(void)
2494 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2495 RedrawGadget(tape_gadget[i]);
2497 if (tape.show_game_buttons)
2498 RedrawGameButtonsOnTape();
2501 void RedrawOrRemapTapeButtons(void)
2503 if (tape_gadget[TAPE_CTRL_ID_PLAY]->mapped)
2505 // tape buttons already mapped
2506 RedrawTapeButtons();
2515 static void HandleTapeButtonsExt(int id)
2517 if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
2522 case TAPE_CTRL_ID_EJECT:
2525 if (TAPE_IS_EMPTY(tape))
2529 if (TAPE_IS_EMPTY(tape))
2530 Request("No tape for this level!", REQ_CONFIRM);
2535 SaveTapeChecked(level_nr);
2540 DrawCompleteVideoDisplay();
2543 case TAPE_CTRL_ID_EXTRA:
2546 tape.pause_before_end = !tape.pause_before_end;
2548 DrawVideoDisplayCurrentState();
2550 else if (tape.recording)
2557 case TAPE_CTRL_ID_STOP:
2562 case TAPE_CTRL_ID_PAUSE:
2563 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2567 case TAPE_CTRL_ID_RECORD:
2568 if (TAPE_IS_STOPPED(tape))
2570 TapeStartGameRecording();
2572 else if (tape.pausing)
2574 if (tape.playing) // PLAY -> PAUSE -> RECORD
2575 TapeAppendRecording();
2577 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2582 case TAPE_CTRL_ID_PLAY:
2583 if (tape.recording && tape.pausing) // PAUSE -> RECORD
2585 // ("TAPE_IS_EMPTY(tape)" is TRUE here -- probably fix this)
2587 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2590 if (TAPE_IS_EMPTY(tape))
2593 if (TAPE_IS_STOPPED(tape))
2595 TapeStartGamePlaying();
2597 else if (tape.playing)
2599 if (tape.pausing) // PAUSE -> PLAY
2601 TapeTogglePause(TAPE_TOGGLE_MANUAL | TAPE_TOGGLE_PLAY_PAUSE);
2603 else if (!tape.fast_forward) // PLAY -> FFWD
2605 tape.fast_forward = TRUE;
2607 else if (!tape.warp_forward) // FFWD -> WARP
2609 tape.warp_forward = TRUE;
2611 else if (!tape.deactivate_display) // WARP -> WARP BLIND
2613 tape.deactivate_display = TRUE;
2615 TapeDeactivateDisplayOn();
2617 else // WARP BLIND -> PLAY
2619 tape.fast_forward = FALSE;
2620 tape.warp_forward = FALSE;
2621 tape.deactivate_display = FALSE;
2623 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
2626 DrawVideoDisplayCurrentState();
2631 case TAPE_CTRL_ID_INSERT_SOLUTION:
2632 InsertSolutionTape();
2636 case TAPE_CTRL_ID_PLAY_SOLUTION:
2646 static void HandleTapeButtons(struct GadgetInfo *gi)
2648 HandleTapeButtonsExt(gi->custom_id);
2651 void HandleTapeButtonKeys(Key key)
2653 boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
2654 boolean extra_button_is_active = !eject_button_is_active;
2656 if (key == setup.shortcut.tape_eject && eject_button_is_active)
2657 HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
2658 else if (key == setup.shortcut.tape_extra && extra_button_is_active)
2659 HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
2660 else if (key == setup.shortcut.tape_stop)
2661 HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
2662 else if (key == setup.shortcut.tape_pause)
2663 HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
2664 else if (key == setup.shortcut.tape_record)
2665 HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
2666 else if (key == setup.shortcut.tape_play)
2667 HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);