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"
22 #define DEBUG_TAPE_WHEN_PLAYING FALSE
24 // tape button identifiers
25 #define TAPE_CTRL_ID_EJECT 0
26 #define TAPE_CTRL_ID_EXTRA 1
27 #define TAPE_CTRL_ID_STOP 2
28 #define TAPE_CTRL_ID_PAUSE 3
29 #define TAPE_CTRL_ID_RECORD 4
30 #define TAPE_CTRL_ID_PLAY 5
31 #define TAPE_CTRL_ID_INSERT_SOLUTION 6
32 #define TAPE_CTRL_ID_PLAY_SOLUTION 7
34 #define NUM_TAPE_BUTTONS 8
36 // values for tape handling
37 #define TAPE_PAUSE_SECONDS_BEFORE_DEATH 5
38 #define TAPE_MIN_SECONDS_FOR_UNDO_BUFFER 20
40 // forward declaration for internal use
41 static void HandleTapeButtons(struct GadgetInfo *);
42 static void TapeStopWarpForward(void);
43 static float GetTapeLengthSecondsFloat(void);
44 static void CopyTapeToUndoBuffer(void);
46 static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
47 static struct TapeInfo tape_undo_buffer;
50 // ============================================================================
51 // video tape definitions
52 // ============================================================================
54 #define VIDEO_INFO_OFF (VIDEO_STATE_DATE_OFF | \
55 VIDEO_STATE_TIME_OFF | \
56 VIDEO_STATE_FRAME_OFF)
57 #define VIDEO_STATE_OFF (VIDEO_STATE_PLAY_OFF | \
58 VIDEO_STATE_REC_OFF | \
59 VIDEO_STATE_PAUSE_OFF | \
60 VIDEO_STATE_FFWD_OFF | \
61 VIDEO_STATE_WARP_OFF | \
62 VIDEO_STATE_WARP2_OFF | \
63 VIDEO_STATE_PBEND_OFF | \
64 VIDEO_STATE_1STEP_OFF)
65 #define VIDEO_PRESS_OFF (VIDEO_PRESS_PLAY_OFF | \
66 VIDEO_PRESS_REC_OFF | \
67 VIDEO_PRESS_PAUSE_OFF | \
68 VIDEO_PRESS_STOP_OFF | \
69 VIDEO_PRESS_EJECT_OFF)
70 #define VIDEO_ALL_OFF (VIDEO_INFO_OFF | \
74 #define VIDEO_INFO_ON (VIDEO_STATE_DATE_ON | \
75 VIDEO_STATE_TIME_ON | \
77 #define VIDEO_STATE_ON (VIDEO_STATE_PLAY_ON | \
78 VIDEO_STATE_REC_ON | \
79 VIDEO_STATE_PAUSE_ON | \
80 VIDEO_STATE_FFWD_ON | \
81 VIDEO_STATE_WARP_ON | \
82 VIDEO_STATE_WARP2_ON | \
83 VIDEO_STATE_PBEND_ON | \
85 #define VIDEO_PRESS_ON (VIDEO_PRESS_PLAY_ON | \
86 VIDEO_PRESS_REC_ON | \
87 VIDEO_PRESS_PAUSE_ON | \
88 VIDEO_PRESS_STOP_ON | \
90 #define VIDEO_ALL_ON (VIDEO_INFO_ON | \
94 #define VIDEO_INFO (VIDEO_INFO_ON | VIDEO_INFO_OFF)
95 #define VIDEO_STATE (VIDEO_STATE_ON | VIDEO_STATE_OFF)
96 #define VIDEO_PRESS (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
97 #define VIDEO_ALL (VIDEO_ALL_ON | VIDEO_ALL_OFF)
99 #define NUM_TAPE_FUNCTIONS 11
100 #define NUM_TAPE_FUNCTION_PARTS 2
101 #define NUM_TAPE_FUNCTION_STATES 2
104 // ============================================================================
105 // video display functions
106 // ============================================================================
108 static void DrawVideoDisplay_Graphics(unsigned int state, unsigned int value)
117 video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
120 { IMG_GFX_TAPE_LABEL_PLAY, &tape.label.play },
121 { IMG_GFX_TAPE_SYMBOL_PLAY, &tape.symbol.play },
124 { IMG_GFX_TAPE_LABEL_RECORD, &tape.label.record },
125 { IMG_GFX_TAPE_SYMBOL_RECORD, &tape.symbol.record },
128 { IMG_GFX_TAPE_LABEL_PAUSE, &tape.label.pause },
129 { IMG_GFX_TAPE_SYMBOL_PAUSE, &tape.symbol.pause },
132 { IMG_GFX_TAPE_LABEL_DATE, &tape.label.date },
136 { IMG_GFX_TAPE_LABEL_TIME, &tape.label.time },
140 // (no label for displaying optional frame)
145 { IMG_GFX_TAPE_LABEL_FAST_FORWARD, &tape.label.fast_forward },
146 { IMG_GFX_TAPE_SYMBOL_FAST_FORWARD, &tape.symbol.fast_forward },
149 { IMG_GFX_TAPE_LABEL_WARP_FORWARD, &tape.label.warp_forward },
150 { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD, &tape.symbol.warp_forward },
153 { IMG_GFX_TAPE_LABEL_WARP_FORWARD_BLIND, &tape.label.warp_forward_blind},
154 { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD_BLIND, &tape.symbol.warp_forward_blind},
157 { IMG_GFX_TAPE_LABEL_PAUSE_BEFORE_END, &tape.label.pause_before_end },
158 { IMG_GFX_TAPE_SYMBOL_PAUSE_BEFORE_END, &tape.symbol.pause_before_end },
161 { IMG_GFX_TAPE_LABEL_SINGLE_STEP, &tape.label.single_step },
162 { IMG_GFX_TAPE_SYMBOL_SINGLE_STEP, &tape.symbol.single_step },
166 for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++) // on or off states
168 for (i = 0; i < NUM_TAPE_FUNCTIONS; i++) // record, play, ...
170 for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++) // label or symbol
172 int graphic = video_pos[i][j].graphic;
173 struct XY *pos = video_pos[i][j].pos;
180 if (state & (1 << (i * 2 + k)))
182 struct GraphicInfo *gfx_bg = &graphic_info[IMG_BACKGROUND_TAPE];
183 struct GraphicInfo *gfx = &graphic_info[graphic];
187 (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
189 if (value == skip_value)
194 gd_bitmap = gfx->bitmap;
200 gd_bitmap = gfx_bg->bitmap;
201 gd_x = gfx_bg->src_x + pos->x;
202 gd_y = gfx_bg->src_y + pos->y;
205 // some tape graphics may be undefined -- only draw if defined
206 if (gd_bitmap != NULL)
207 BlitBitmap(gd_bitmap, drawto, gd_x, gd_y, gfx->width, gfx->height,
208 VX + pos->x, VY + pos->y);
210 redraw_mask |= REDRAW_DOOR_2;
218 #define DATETIME_NONE (0)
220 #define DATETIME_DATE_YYYY (1 << 0)
221 #define DATETIME_DATE_YY (1 << 1)
222 #define DATETIME_DATE_MON (1 << 2)
223 #define DATETIME_DATE_MM (1 << 3)
224 #define DATETIME_DATE_DD (1 << 4)
226 #define DATETIME_TIME_HH (1 << 5)
227 #define DATETIME_TIME_MIN (1 << 6)
228 #define DATETIME_TIME_MM (1 << 7)
229 #define DATETIME_TIME_SS (1 << 8)
231 #define DATETIME_FRAME (1 << 9)
233 #define DATETIME_XOFFSET_1 (1 << 10)
234 #define DATETIME_XOFFSET_2 (1 << 11)
236 #define DATETIME_DATE (DATETIME_DATE_YYYY | \
238 DATETIME_DATE_MON | \
242 #define DATETIME_TIME (DATETIME_TIME_HH | \
243 DATETIME_TIME_MIN | \
247 #define MAX_DATETIME_STRING_SIZE 32
249 static void DrawVideoDisplay_DateTime(unsigned int state, unsigned int value)
253 static char *month_shortnames[] =
271 struct TextPosInfo *pos;
276 { &tape.text.date, DATETIME_DATE_DD },
277 { &tape.text.date, DATETIME_DATE_MON | DATETIME_XOFFSET_1 },
278 { &tape.text.date, DATETIME_DATE_YY | DATETIME_XOFFSET_2 },
279 { &tape.text.date_yyyy, DATETIME_DATE_YYYY },
280 { &tape.text.date_yy, DATETIME_DATE_YY },
281 { &tape.text.date_mon, DATETIME_DATE_MON },
282 { &tape.text.date_mm, DATETIME_DATE_MM },
283 { &tape.text.date_dd, DATETIME_DATE_DD },
285 { &tape.text.time, DATETIME_TIME_MIN },
286 { &tape.text.time, DATETIME_TIME_SS | DATETIME_XOFFSET_1 },
287 { &tape.text.time_hh, DATETIME_TIME_HH },
288 { &tape.text.time_mm, DATETIME_TIME_MM },
289 { &tape.text.time_ss, DATETIME_TIME_SS },
291 { &tape.text.frame, DATETIME_FRAME },
293 { NULL, DATETIME_NONE },
296 for (i = 0; datetime_info[i].pos != NULL; i++)
298 struct TextPosInfo *pos = datetime_info[i].pos;
299 int type = datetime_info[i].type;
306 xpos = VX + pos->x + (type & DATETIME_XOFFSET_1 ? pos->xoffset :
307 type & DATETIME_XOFFSET_2 ? pos->xoffset2 : 0);
310 if ((type & DATETIME_DATE) && (state & VIDEO_STATE_DATE_ON))
312 char s[MAX_DATETIME_STRING_SIZE];
313 int year2 = value / 10000;
314 int year4 = (year2 < 70 ? 2000 + year2 : 1900 + year2);
315 int month_index_raw = (value / 100) % 100;
316 int month_index = month_index_raw % 12; // prevent invalid index
317 int month = month_index + 1;
318 int day = value % 100;
320 strcpy(s, (type & DATETIME_DATE_YYYY ? int2str(year4, 4) :
321 type & DATETIME_DATE_YY ? int2str(year2, 2) :
322 type & DATETIME_DATE_MON ? month_shortnames[month_index] :
323 type & DATETIME_DATE_MM ? int2str(month, 2) :
324 type & DATETIME_DATE_DD ? int2str(day, 2) : ""));
326 DrawText(xpos, ypos, s, pos->font);
328 else if ((type & DATETIME_TIME) && (state & VIDEO_STATE_TIME_ON))
330 char s[MAX_DATETIME_STRING_SIZE];
331 int hh = (value / 3600) % 100;
332 int min = value / 60;
333 int mm = (value / 60) % 60;
336 strcpy(s, (type & DATETIME_TIME_HH ? int2str(hh, 2) :
337 type & DATETIME_TIME_MIN ? int2str(min, 2) :
338 type & DATETIME_TIME_MM ? int2str(mm, 2) :
339 type & DATETIME_TIME_SS ? int2str(ss, 2) : ""));
341 DrawText(xpos, ypos, s, pos->font);
343 else if ((type & DATETIME_FRAME) && (state & VIDEO_STATE_FRAME_ON))
345 DrawText(xpos, ypos, int2str(value, pos->size), pos->font);
350 void DrawVideoDisplay(unsigned int state, unsigned int value)
352 DrawVideoDisplay_Graphics(state, value);
353 DrawVideoDisplay_DateTime(state, value);
356 static void DrawVideoDisplayLabel(unsigned int state)
358 DrawVideoDisplay(state, VIDEO_DISPLAY_LABEL_ONLY);
361 static void DrawVideoDisplaySymbol(unsigned int state)
363 DrawVideoDisplay(state, VIDEO_DISPLAY_SYMBOL_ONLY);
366 static void DrawVideoDisplayCurrentState(void)
370 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
373 state |= VIDEO_STATE_PAUSE_ON;
377 state |= VIDEO_STATE_REC_ON;
379 if (tape.single_step)
380 state |= VIDEO_STATE_1STEP_ON;
382 else if (tape.playing)
384 state |= VIDEO_STATE_PLAY_ON;
388 if (tape.deactivate_display)
389 state |= VIDEO_STATE_WARP2_ON;
390 else if (tape.warp_forward)
391 state |= VIDEO_STATE_WARP_ON;
392 else if (tape.fast_forward)
393 state |= VIDEO_STATE_FFWD_ON;
395 if (tape.pause_before_end)
396 state |= VIDEO_STATE_PBEND_ON;
400 // draw labels and symbols separately to prevent labels overlapping symbols
401 DrawVideoDisplayLabel(state);
402 DrawVideoDisplaySymbol(state);
405 void DrawCompleteVideoDisplay(void)
407 struct GraphicInfo *g_tape = &graphic_info[IMG_BACKGROUND_TAPE];
409 // draw tape background
410 BlitBitmap(g_tape->bitmap, drawto, g_tape->src_x, g_tape->src_y,
411 gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
413 // draw tape buttons (forced)
414 RedrawOrRemapTapeButtons();
416 DrawVideoDisplay(VIDEO_ALL_OFF, 0);
420 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
421 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
422 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
423 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
426 DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
428 else if (tape.playing)
430 DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
431 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
432 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
433 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
436 DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
438 else if (tape.date && tape.length)
440 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
441 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
442 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
445 BlitBitmap(drawto, bitmap_db_door_2, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
449 void TapeDeactivateDisplayOn(void)
451 SetDrawDeactivationMask(REDRAW_FIELD);
452 audio.sound_deactivated = TRUE;
455 void TapeDeactivateDisplayOff(boolean redraw_display)
457 SetDrawDeactivationMask(REDRAW_NONE);
458 audio.sound_deactivated = FALSE;
464 UpdateGameDoorValues();
465 DrawGameDoorValues();
470 // ============================================================================
471 // tape logging functions
472 // ============================================================================
476 LevelDirTree *leveldir;
477 boolean all_levelsets;
480 int num_levels_played;
481 int num_levels_solved;
482 int num_tapes_patched;
483 int num_tape_missing;
484 boolean level_failed[MAX_TAPES_PER_SET];
488 static char tape_patch_info[MAX_OUTPUT_LINESIZE];
490 static void PrintTapeReplayHeader(struct AutoPlayInfo *autoplay)
494 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
495 Print("Automatically fixing level tapes\n");
496 else if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
497 Print("Automatically uploading level tapes\n");
499 Print("Automatically playing level tapes\n");
502 Print("Level series identifier: '%s'\n", autoplay->leveldir->identifier);
503 Print("Level series name: '%s'\n", autoplay->leveldir->name);
504 Print("Level series author: '%s'\n", autoplay->leveldir->author);
505 Print("Number of levels: %d\n", autoplay->leveldir->levels);
509 DrawInitTextItem(autoplay->leveldir->name);
512 static void PrintTapeReplayProgress(boolean replay_finished)
514 static unsigned int counter_last = -1;
515 unsigned int counter = Counter();
516 unsigned int counter_seconds = counter / 1000;
518 if (!replay_finished)
520 unsigned int counter_delay = 50;
522 if (counter > counter_last + counter_delay)
525 PrintNoLog("Tape %03d %s[%02d:%02d]: [%02d:%02d] - playing tape ... ",
526 level_nr, tape_patch_info,
527 tape.length_seconds / 60, tape.length_seconds % 60,
528 TapeTime / 60, TapeTime % 60);
530 counter_last = counter;
535 float tape_length_seconds = GetTapeLengthSecondsFloat();
538 Print("Tape %03d %s[%02d:%02d]: (%02d:%02d.%03d / %.2f %%) - %s.\n",
539 level_nr, tape_patch_info,
540 tape.length_seconds / 60, tape.length_seconds % 60,
541 counter_seconds / 60, counter_seconds % 60, counter % 1000,
542 (float)counter / tape_length_seconds / 10,
543 tape.auto_play_level_fixed ? "solved and fixed" :
544 tape.auto_play_level_solved ? "solved" :
545 tape.auto_play_level_not_fixable ? "NOT SOLVED, NOT FIXED" :
552 static void PrintTapeReplaySummary(struct AutoPlayInfo *autoplay)
554 char *autoplay_status =
555 (autoplay->num_levels_played == autoplay->num_levels_solved &&
556 autoplay->num_levels_played > 0 ? " OK " : "WARN");
557 int autoplay_percent =
558 (autoplay->num_levels_played ?
559 autoplay->num_levels_solved * 100 / autoplay->num_levels_played : 0);
564 Print("Number of levels played: %d\n", autoplay->num_levels_played);
565 Print("Number of levels solved: %d (%d%%)\n", autoplay->num_levels_solved,
566 (autoplay->num_levels_played ?
567 autoplay->num_levels_solved * 100 / autoplay->num_levels_played : 0));
568 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
569 Print("Number of tapes fixed: %d\n", autoplay->num_tapes_patched);
571 Print("Summary (for automatic parsing by scripts):\n");
573 if (autoplay->tape_filename)
575 Print("TAPEFILE [%s] '%s', %d, %d, %d",
577 autoplay->leveldir->identifier,
578 autoplay->last_level_nr,
580 game.score_time_final);
584 Print("LEVELDIR [%s] '%s', SOLVED %d/%d (%d%%)",
586 autoplay->leveldir->identifier,
587 autoplay->num_levels_solved,
588 autoplay->num_levels_played,
591 if (autoplay->num_levels_played != autoplay->num_levels_solved)
594 for (i = 0; i < MAX_TAPES_PER_SET; i++)
595 if (autoplay->level_failed[i])
604 static FILE *tape_log_file;
606 static void OpenTapeLogfile(void)
608 if (!(tape_log_file = fopen(options.tape_log_filename, MODE_WRITE)))
609 Warn("cannot write tape logfile '%s'", options.tape_log_filename);
612 static void WriteTapeLogfile(byte action[MAX_TAPE_ACTIONS])
616 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
617 putFile8Bit(tape_log_file, action[i]);
620 static void CloseTapeLogfile(void)
622 fclose(tape_log_file);
626 // ============================================================================
627 // tape control functions
628 // ============================================================================
630 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
632 struct tm *lt = localtime(&epoch_seconds);
634 tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
637 void TapeSetDateFromNow(void)
639 TapeSetDateFromEpochSeconds(time(NULL));
646 CopyTapeToUndoBuffer();
650 tape.length_frames = 0;
651 tape.length_seconds = 0;
653 tape.score_tape_basename[0] = '\0';
655 if (leveldir_current)
657 strncpy(tape.level_identifier, leveldir_current->identifier,
659 tape.level_identifier[MAX_FILENAME_LEN] = '\0';
662 tape.level_nr = level_nr;
663 tape.pos[tape.counter].delay = 0;
666 tape.random_seed = InitRND(level.random_seed);
668 tape.file_version = FILE_VERSION_ACTUAL;
669 tape.game_version = GAME_VERSION_ACTUAL;
670 tape.engine_version = level.game_version;
672 tape.property_bits = TAPE_PROPERTY_NONE;
674 TapeSetDateFromNow();
676 for (i = 0; i < MAX_PLAYERS; i++)
677 tape.player_participates[i] = FALSE;
679 tape.centered_player_nr_next = -1;
680 tape.set_centered_player = FALSE;
682 // set flags for game actions to default values (may be overwritten later)
683 tape.use_key_actions = TRUE;
684 tape.use_mouse_actions = FALSE;
687 static void TapeRewind(void)
690 tape.delay_played = 0;
691 tape.pause_before_end = FALSE;
692 tape.recording = FALSE;
693 tape.playing = FALSE;
694 tape.fast_forward = FALSE;
695 tape.warp_forward = FALSE;
696 tape.deactivate_display = FALSE;
697 tape.auto_play = (global.autoplay_leveldir != NULL);
698 tape.auto_play_level_solved = FALSE;
699 tape.auto_play_level_fixed = FALSE;
700 tape.auto_play_level_not_fixable = FALSE;
701 tape.quick_resume = FALSE;
702 tape.single_step = FALSE;
704 tape.centered_player_nr_next = -1;
705 tape.set_centered_player = FALSE;
707 InitRND(tape.random_seed);
710 static void TapeSetRandomSeed(int random_seed)
712 tape.random_seed = InitRND(random_seed);
715 void TapeStartRecording(int random_seed)
717 if (!TAPE_IS_STOPPED(tape))
722 TapeSetRandomSeed(random_seed);
724 tape.recording = TRUE;
726 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
727 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
728 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
729 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
733 SetDrawDeactivationMask(REDRAW_NONE);
734 audio.sound_deactivated = FALSE;
736 // required here to update video display if tape door is closed
737 if (GetDoorState() & DOOR_CLOSE_2)
738 OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
741 static void TapeStartGameRecording(void)
743 StartGameActions(network.enabled, TRUE, level.random_seed);
746 static void TapeAppendRecording(void)
748 if (!tape.playing || !tape.pausing)
752 tape.playing = FALSE;
753 tape.fast_forward = FALSE;
754 tape.warp_forward = FALSE;
755 tape.pause_before_end = FALSE;
756 tape.deactivate_display = FALSE;
759 tape.recording = TRUE;
762 // set current delay (for last played move)
763 tape.pos[tape.counter].delay = tape.delay_played;
765 tape.property_bits |= TAPE_PROPERTY_REPLAYED;
768 TapeSetDateFromNow();
770 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
771 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
773 UpdateAndDisplayGameControlValues();
776 void TapeHaltRecording(void)
780 // initialize delay for next tape entry (to be able to continue recording)
781 if (tape.counter < MAX_TAPE_LEN)
782 tape.pos[tape.counter].delay = 0;
784 tape.length = tape.counter;
785 tape.length_frames = GetTapeLengthFrames();
786 tape.length_seconds = GetTapeLengthSeconds();
789 void TapeStopRecording(void)
794 tape.recording = FALSE;
795 tape.pausing = FALSE;
797 DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
798 MapTapeEjectButton();
801 boolean TapeAddAction(byte action[MAX_TAPE_ACTIONS])
805 if (tape.pos[tape.counter].delay > 0) // already stored action
807 boolean changed_events = FALSE;
809 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
810 if (tape.pos[tape.counter].action[i] != action[i])
811 changed_events = TRUE;
813 if (changed_events || tape.pos[tape.counter].delay >= 255)
815 if (tape.counter >= MAX_TAPE_LEN - 1)
819 tape.pos[tape.counter].delay = 0;
822 tape.pos[tape.counter].delay++;
825 if (tape.pos[tape.counter].delay == 0) // store new action
827 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
828 tape.pos[tape.counter].action[i] = action[i];
830 tape.pos[tape.counter].delay++;
836 void TapeRecordAction(byte action_raw[MAX_TAPE_ACTIONS])
838 byte action[MAX_TAPE_ACTIONS];
841 if (!tape.recording) // (record action even when tape is paused)
844 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
845 action[i] = action_raw[i];
847 if (tape.use_key_actions && tape.set_centered_player)
849 for (i = 0; i < MAX_PLAYERS; i++)
850 if (tape.centered_player_nr_next == i ||
851 tape.centered_player_nr_next == -1)
852 action[i] |= KEY_SET_FOCUS;
854 tape.set_centered_player = FALSE;
857 if (GameFrameDelay != GAME_FRAME_DELAY)
858 tape.property_bits |= TAPE_PROPERTY_GAME_SPEED;
860 if (setup.small_game_graphics || SCR_FIELDX >= 2 * SCR_FIELDX_DEFAULT)
861 tape.property_bits |= TAPE_PROPERTY_SMALL_GRAPHICS;
863 if (!TapeAddAction(action))
867 void TapeTogglePause(boolean toggle_mode)
869 if (tape.playing && tape.pausing && (toggle_mode & TAPE_TOGGLE_PLAY_PAUSE))
871 // continue playing in normal mode
872 tape.fast_forward = FALSE;
873 tape.warp_forward = FALSE;
874 tape.deactivate_display = FALSE;
876 tape.pause_before_end = FALSE;
879 tape.pausing = !tape.pausing;
881 if (tape.single_step && (toggle_mode & TAPE_TOGGLE_MANUAL))
882 tape.single_step = FALSE;
884 if (tape.single_step)
885 tape.property_bits |= TAPE_PROPERTY_SINGLE_STEP;
888 tape.property_bits |= TAPE_PROPERTY_PAUSE_MODE;
890 DrawVideoDisplayCurrentState();
892 if (tape.deactivate_display)
895 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
897 TapeDeactivateDisplayOn();
900 if (tape.quick_resume)
902 tape.quick_resume = FALSE;
904 TapeStopWarpForward();
905 TapeAppendRecording();
907 if (!CheckEngineSnapshotSingle())
908 SaveEngineSnapshotSingle();
910 // restart step/move snapshots after quick loading tape
911 SaveEngineSnapshotToListInitial();
913 // do not map undo/redo buttons after quick loading tape
917 if (game_status == GAME_MODE_PLAYING)
919 if (setup.show_load_save_buttons &&
920 setup.show_undo_redo_buttons &&
921 CheckEngineSnapshotList())
924 MapUndoRedoButtons();
925 else if (!tape.single_step)
926 MapLoadSaveButtons();
929 ModifyPauseButtons();
933 void TapeStartPlaying(void)
935 if (TAPE_IS_EMPTY(tape))
938 if (!TAPE_IS_STOPPED(tape))
945 DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
946 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
947 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
948 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
952 SetDrawDeactivationMask(REDRAW_NONE);
953 audio.sound_deactivated = FALSE;
956 static void TapeStartGamePlaying(void)
963 void TapeStopPlaying(void)
965 tape.playing = FALSE;
966 tape.pausing = FALSE;
968 if (tape.warp_forward)
969 TapeStopWarpForward();
971 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
972 MapTapeEjectButton();
975 byte *TapePlayAction(void)
977 int update_delay = FRAMES_PER_SECOND / 2;
978 boolean update_video_display = (FrameCounter % update_delay == 0);
979 boolean update_draw_label_on = ((FrameCounter / update_delay) % 2 == 1);
980 static byte action[MAX_TAPE_ACTIONS];
983 if (!tape.playing || tape.pausing)
986 if (tape.pause_before_end) // stop some seconds before end of tape
988 if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
990 TapeStopWarpForward();
991 TapeTogglePause(TAPE_TOGGLE_MANUAL);
997 if (tape.counter >= tape.length) // end of tape reached
999 if (tape.warp_forward && !tape.auto_play)
1001 TapeStopWarpForward();
1002 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1012 if (update_video_display && !tape.deactivate_display)
1016 if (tape.warp_forward)
1017 state |= VIDEO_STATE_WARP(update_draw_label_on);
1018 else if (tape.fast_forward)
1019 state |= VIDEO_STATE_FFWD(update_draw_label_on);
1021 if (tape.pause_before_end)
1022 state |= VIDEO_STATE_PBEND(update_draw_label_on);
1024 // draw labels and symbols separately to prevent labels overlapping symbols
1025 DrawVideoDisplayLabel(state);
1026 DrawVideoDisplaySymbol(state);
1029 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
1030 action[i] = tape.pos[tape.counter].action[i];
1032 #if DEBUG_TAPE_WHEN_PLAYING
1033 DebugContinued("", "%05d", FrameCounter);
1034 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
1035 DebugContinued("", " %08x", action[i]);
1036 DebugContinued("tape:play", "\n");
1039 tape.set_centered_player = FALSE;
1040 tape.centered_player_nr_next = -999;
1042 if (tape.use_key_actions)
1044 for (i = 0; i < MAX_PLAYERS; i++)
1046 if (action[i] & KEY_SET_FOCUS)
1048 tape.set_centered_player = TRUE;
1049 tape.centered_player_nr_next =
1050 (tape.centered_player_nr_next == -999 ? i : -1);
1053 action[i] &= ~KEY_SET_FOCUS;
1057 tape.delay_played++;
1058 if (tape.delay_played >= tape.pos[tape.counter].delay)
1061 tape.delay_played = 0;
1065 PrintTapeReplayProgress(FALSE);
1067 if (options.tape_log_filename != NULL)
1068 WriteTapeLogfile(action);
1076 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1078 TapeStopRecording();
1081 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
1083 if (tape.date && tape.length)
1085 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
1086 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
1087 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
1091 unsigned int GetTapeLengthFrames(void)
1093 unsigned int tape_length_frames = 0;
1096 if (TAPE_IS_EMPTY(tape))
1099 for (i = 0; i < tape.length; i++)
1100 tape_length_frames += tape.pos[i].delay;
1102 return tape_length_frames;
1105 unsigned int GetTapeLengthSeconds(void)
1107 return (GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
1110 static float GetTapeLengthSecondsFloat(void)
1112 return ((float)GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
1115 static void TapeStartWarpForward(int mode)
1117 tape.fast_forward = (mode & AUTOPLAY_FFWD);
1118 tape.warp_forward = (mode & AUTOPLAY_WARP);
1119 tape.deactivate_display = (mode & AUTOPLAY_WARP_NO_DISPLAY);
1121 tape.pausing = FALSE;
1123 if (tape.deactivate_display)
1124 TapeDeactivateDisplayOn();
1126 DrawVideoDisplayCurrentState();
1129 static void TapeStopWarpForward(void)
1131 tape.fast_forward = FALSE;
1132 tape.warp_forward = FALSE;
1133 tape.deactivate_display = FALSE;
1135 tape.pause_before_end = FALSE;
1137 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
1139 DrawVideoDisplayCurrentState();
1142 static void TapeSingleStep(void)
1144 if (network.enabled)
1148 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1150 tape.single_step = !tape.single_step;
1152 DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
1155 void TapeQuickSave(void)
1157 if (game_status == GAME_MODE_MAIN)
1159 Request("No game that can be saved!", REQ_CONFIRM);
1164 if (game_status != GAME_MODE_PLAYING)
1168 TapeHaltRecording(); // prepare tape for saving on-the-fly
1170 if (TAPE_IS_EMPTY(tape))
1172 Request("No tape that can be saved!", REQ_CONFIRM);
1177 tape.property_bits |= TAPE_PROPERTY_SNAPSHOT;
1179 if (SaveTapeChecked(tape.level_nr))
1180 SaveEngineSnapshotSingle();
1183 void TapeQuickLoad(void)
1185 char *filename = getTapeFilename(level_nr);
1187 if (!fileExists(filename))
1189 Request("No tape for this level!", REQ_CONFIRM);
1194 if (tape.recording && !Request("Stop recording and load tape?",
1195 REQ_ASK | REQ_STAY_CLOSED))
1197 OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1202 if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
1205 if (CheckEngineSnapshotSingle())
1207 TapeStartGamePlaying();
1209 LoadEngineSnapshotSingle();
1211 DrawCompleteVideoDisplay();
1213 tape.playing = TRUE;
1214 tape.pausing = TRUE;
1216 TapeStopWarpForward();
1217 TapeAppendRecording();
1219 // restart step/move snapshots after quick loading tape
1220 SaveEngineSnapshotToListInitial();
1222 if (FrameCounter > 0)
1231 if (!TAPE_IS_EMPTY(tape))
1233 TapeStartGamePlaying();
1234 TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
1236 tape.quick_resume = TRUE;
1237 tape.property_bits |= TAPE_PROPERTY_SNAPSHOT;
1239 else // this should not happen (basically checked above)
1241 int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1243 Request("No tape for this level!", REQ_CONFIRM | reopen_door);
1247 boolean hasSolutionTape(void)
1249 boolean tape_file_exists = fileExists(getSolutionTapeFilename(level_nr));
1250 boolean level_has_tape = (level.game_engine_type == GAME_ENGINE_TYPE_SP &&
1251 level.native_sp_level->demo.is_available);
1253 return (tape_file_exists || level_has_tape);
1256 boolean InsertSolutionTape(void)
1258 if (!hasSolutionTape())
1260 Request("No solution tape for this level!", REQ_CONFIRM);
1265 if (!TAPE_IS_STOPPED(tape))
1268 // if tape recorder already contains a tape, remove it without asking
1271 LoadSolutionTape(level_nr);
1273 DrawCompleteVideoDisplay();
1275 if (TAPE_IS_EMPTY(tape))
1277 Request("Loading solution tape for this level failed!", REQ_CONFIRM);
1285 boolean PlaySolutionTape(void)
1287 if (!InsertSolutionTape())
1290 TapeStartGamePlaying();
1295 static boolean checkTapesFromSameLevel(struct TapeInfo *t1, struct TapeInfo *t2)
1297 return (strEqual(t1->level_identifier, t2->level_identifier) &&
1298 t1->level_nr == t2->level_nr);
1301 static void CopyTape(struct TapeInfo *tape_from, struct TapeInfo *tape_to)
1303 *tape_to = *tape_from;
1306 static void SwapTapes(struct TapeInfo *t1, struct TapeInfo *t2)
1308 struct TapeInfo tmp = *t1;
1314 static void CopyTapeToUndoBuffer(void)
1316 // copy tapes to undo buffer if large enough (or larger than last undo tape)
1317 // or if the last undo tape is from a different level set or level number
1318 if (tape.length_seconds >= TAPE_MIN_SECONDS_FOR_UNDO_BUFFER ||
1319 tape.length_seconds >= tape_undo_buffer.length_seconds ||
1320 !checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1322 CopyTape(&tape, &tape_undo_buffer);
1328 // only undo tapes from same level set and with same level number
1329 if (!checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1332 if (!TAPE_IS_STOPPED(tape))
1335 // swap last recorded tape with undo buffer, so undo can be reversed
1336 SwapTapes(&tape, &tape_undo_buffer);
1338 DrawCompleteVideoDisplay();
1341 void FixTape_ForceSinglePlayer(void)
1345 /* fix single-player tapes that contain player input for more than one
1346 player (due to a bug in 3.3.1.2 and earlier versions), which results
1347 in playing levels with more than one player in multi-player mode,
1348 even though the tape was originally recorded in single-player mode */
1350 // remove player input actions for all players but the first one
1351 for (i = 1; i < MAX_PLAYERS; i++)
1352 tape.player_participates[i] = FALSE;
1354 tape.changed = TRUE;
1358 // ----------------------------------------------------------------------------
1359 // tape autoplay functions
1360 // ----------------------------------------------------------------------------
1362 static TreeInfo *getNextValidAutoPlayEntry(TreeInfo *node)
1364 node = getNextValidTreeInfoEntry(node);
1366 while (node && node->is_copy)
1367 node = getNextValidTreeInfoEntry(node);
1372 static TreeInfo *getFirstValidAutoPlayEntry(TreeInfo *node)
1374 node = getFirstValidTreeInfoEntry(node);
1376 if (node && node->is_copy)
1377 return getNextValidAutoPlayEntry(node);
1382 static void AutoPlayTapes_SetScoreEntry(int score, int time)
1384 // set unique basename for score tape (for uploading to score server)
1385 strcpy(tape.score_tape_basename, getScoreTapeBasename(setup.player_name));
1387 // store score in first score entry
1388 scores.last_added = 0;
1390 struct ScoreEntry *entry = &scores.entry[scores.last_added];
1392 strncpy(entry->tape_basename, tape.score_tape_basename, MAX_FILENAME_LEN);
1393 strncpy(entry->name, setup.player_name, MAX_PLAYER_NAME_LEN);
1395 entry->score = score;
1398 PrintNoLog("- uploading score tape to score server ... ");
1400 server_scores.uploaded = FALSE;
1403 static void AutoPlayTapes_WaitForUpload(void)
1405 unsigned int upload_delay = 0;
1406 unsigned int upload_delay_value = 10000;
1408 ResetDelayCounter(&upload_delay);
1410 // wait for score tape to be successfully uploaded (and fail on timeout)
1411 while (!server_scores.uploaded)
1413 if (DelayReached(&upload_delay, upload_delay_value))
1416 Print("- uploading score tape to score server - TIMEOUT.\n");
1418 Fail("cannot upload score tape to score server");
1421 UPDATE_BUSY_STATE();
1427 Print("- uploading score tape to score server - uploaded.\n");
1430 static int AutoPlayTapesExt(boolean initialize)
1432 static struct AutoPlayInfo autoplay;
1433 static int num_tapes = 0;
1434 static int patch_nr = 0;
1435 static char *patch_name[] =
1443 static int patch_version_first[] =
1445 VERSION_IDENT(0,0,0,0),
1446 VERSION_IDENT(3,3,1,0),
1447 VERSION_IDENT(0,0,0,0),
1451 static int patch_version_last[] =
1453 VERSION_IDENT(9,9,9,9),
1454 VERSION_IDENT(4,0,1,1),
1455 VERSION_IDENT(4,2,2,0),
1459 static byte patch_property_bit[] =
1462 TAPE_PROPERTY_EM_RANDOM_BUG,
1467 boolean init_level_set = FALSE;
1472 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1474 if (tape.auto_play_level_solved)
1478 // level solved by patched tape -- save fixed tape
1479 char *filename = getTapeFilename(level_nr);
1480 char *filename_orig = getStringCat2(filename, ".orig");
1482 // create backup from old tape, if not yet existing
1483 if (!fileExists(filename_orig))
1484 rename(filename, filename_orig);
1486 SaveTapeToFilename(filename);
1488 tape.auto_play_level_fixed = TRUE;
1489 autoplay.num_tapes_patched++;
1492 // continue with next tape
1495 else if (patch_name[patch_nr + 1] != NULL)
1497 // level not solved by patched tape -- continue with next patch
1502 // level not solved by any patched tape -- continue with next tape
1503 tape.auto_play_level_not_fixable = TRUE;
1508 // just finished auto-playing tape
1509 PrintTapeReplayProgress(TRUE);
1511 if (options.tape_log_filename != NULL)
1514 if (global.autoplay_mode == AUTOPLAY_MODE_SAVE &&
1515 tape.auto_play_level_solved)
1517 AutoPlayTapes_SetScoreEntry(game.score_final, game.score_time_final);
1519 if (leveldir_current)
1521 // the tape's level set identifier may differ from current level set
1522 strncpy(tape.level_identifier, leveldir_current->identifier,
1524 tape.level_identifier[MAX_FILENAME_LEN] = '\0';
1526 // the tape's level number may differ from current level number
1527 tape.level_nr = level_nr;
1530 // save score tape to upload to server; may be required for some reasons:
1531 // * level set identifier in solution tapes may differ from level set
1532 // * level set identifier is missing (old-style tape without INFO chunk)
1533 // * solution tape may have native format (like Supaplex solution files)
1535 SaveScoreTape(level_nr);
1536 SaveServerScore(level_nr);
1538 AutoPlayTapes_WaitForUpload();
1542 autoplay.num_levels_played++;
1544 if (tape.auto_play_level_solved)
1545 autoplay.num_levels_solved++;
1547 if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1548 autoplay.level_failed[level_nr] = !tape.auto_play_level_solved;
1552 if (strEqual(global.autoplay_leveldir, "ALL"))
1554 autoplay.all_levelsets = TRUE;
1556 // tape mass-uploading only allowed for private tapes
1557 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
1558 options.mytapes = TRUE;
1561 if ((global.autoplay_mode == AUTOPLAY_MODE_SAVE ||
1562 global.autoplay_mode == AUTOPLAY_MODE_UPLOAD) &&
1564 options.player_name == NULL)
1566 Fail("specify player name when uploading solution tapes");
1569 if (global.autoplay_mode != AUTOPLAY_MODE_UPLOAD)
1570 DrawCompleteVideoDisplay();
1572 if (program.headless)
1574 audio.sound_enabled = FALSE;
1575 setup.engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_OFF);
1578 if (strSuffix(global.autoplay_leveldir, ".tape"))
1580 autoplay.tape_filename = global.autoplay_leveldir;
1582 LoadTapeFromFilename(autoplay.tape_filename);
1584 if (tape.no_valid_file)
1586 if (!fileExists(autoplay.tape_filename))
1587 Fail("tape file '%s' does not exist", autoplay.tape_filename);
1589 Fail("cannot load tape file '%s'", autoplay.tape_filename);
1592 global.autoplay_leveldir = tape.level_identifier;
1594 if (tape.level_nr >= 0 && tape.level_nr < MAX_TAPES_PER_SET)
1595 global.autoplay_level[tape.level_nr] = TRUE;
1597 global.autoplay_all = FALSE;
1600 if (autoplay.all_levelsets)
1602 // start auto-playing first level set
1603 autoplay.leveldir = getFirstValidAutoPlayEntry(leveldir_first);
1607 // auto-play selected level set
1608 autoplay.leveldir = getTreeInfoFromIdentifier(leveldir_first,
1609 global.autoplay_leveldir);
1612 if (autoplay.leveldir == NULL)
1613 Fail("no such level identifier: '%s'", global.autoplay_leveldir);
1615 // only private tapes may be modified
1616 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1617 options.mytapes = TRUE;
1619 // set timestamp for batch tape upload
1620 global.autoplay_time = time(NULL);
1624 init_level_set = TRUE;
1631 leveldir_current = autoplay.leveldir;
1633 if (autoplay.leveldir->first_level < 0)
1634 autoplay.leveldir->first_level = 0;
1635 if (autoplay.leveldir->last_level >= MAX_TAPES_PER_SET)
1636 autoplay.leveldir->last_level = MAX_TAPES_PER_SET - 1;
1638 autoplay.level_nr = autoplay.leveldir->first_level;
1640 autoplay.num_levels_played = 0;
1641 autoplay.num_levels_solved = 0;
1642 autoplay.num_tapes_patched = 0;
1643 autoplay.num_tape_missing = 0;
1645 for (i = 0; i < MAX_TAPES_PER_SET; i++)
1646 autoplay.level_failed[i] = FALSE;
1648 PrintTapeReplayHeader(&autoplay);
1650 init_level_set = FALSE;
1653 if (global.autoplay_mode != AUTOPLAY_MODE_FIX || patch_nr == 0)
1654 level_nr = autoplay.level_nr++;
1656 UPDATE_BUSY_STATE();
1658 // check if all tapes for this level set have been processed
1659 if (level_nr > autoplay.leveldir->last_level)
1661 PrintTapeReplaySummary(&autoplay);
1663 if (!autoplay.all_levelsets)
1666 // continue with next level set
1667 autoplay.leveldir = getNextValidAutoPlayEntry(autoplay.leveldir);
1669 // all level sets processed
1670 if (autoplay.leveldir == NULL)
1673 init_level_set = TRUE;
1678 // set patch info (required for progress output)
1679 strcpy(tape_patch_info, "");
1680 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1681 snprintf(tape_patch_info, MAX_OUTPUT_LINESIZE, "[%-13s] ",
1682 patch_name[patch_nr]);
1684 if (!global.autoplay_all && !global.autoplay_level[level_nr])
1688 TapeRewind(); // needed to reset "tape.auto_play_level_solved"
1690 LoadLevel(level_nr);
1692 if (level.no_level_file || level.no_valid_file)
1694 Print("Tape %03d: (no level found)\n", level_nr);
1700 // ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY
1701 Print("Tape %03d: (only testing level)\n", level_nr);
1705 if (autoplay.tape_filename)
1706 LoadTapeFromFilename(autoplay.tape_filename);
1707 else if (options.mytapes)
1710 LoadSolutionTape(level_nr);
1712 if (tape.no_valid_file)
1714 autoplay.num_tape_missing++;
1716 Print("Tape %03d: (no tape found)\n", level_nr);
1721 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1723 boolean skip_patch = FALSE;
1725 if (tape.engine_version < patch_version_first[patch_nr] ||
1726 tape.engine_version > patch_version_last[patch_nr])
1728 Print("Tape %03d %s[%02d:%02d]: (%s %d.%d.%d.%d) - skipped.\n",
1729 level_nr, tape_patch_info,
1730 tape.length_seconds / 60, tape.length_seconds % 60,
1731 "not suitable for version",
1732 (tape.engine_version / 1000000) % 100,
1733 (tape.engine_version / 10000 ) % 100,
1734 (tape.engine_version / 100 ) % 100,
1735 (tape.engine_version ) % 100);
1740 if (strEqual(patch_name[patch_nr], "screen_34x34") &&
1741 tape.num_participating_players == 1)
1743 Print("Tape %03d %s[%02d:%02d]: (%s) - skipped.\n",
1744 level_nr, tape_patch_info,
1745 tape.length_seconds / 60, tape.length_seconds % 60,
1746 "not suitable for single player tapes");
1753 if (patch_name[patch_nr + 1] != NULL)
1755 // continue with next patch
1760 // continue with next tape
1767 if (strEqual(patch_name[patch_nr], "screen_34x34"))
1769 tape.scr_fieldx = SCR_FIELDX_DEFAULT * 2;
1770 tape.scr_fieldy = SCR_FIELDY_DEFAULT * 2;
1774 tape.property_bits |= patch_property_bit[patch_nr];
1780 if (global.autoplay_mode == AUTOPLAY_MODE_UPLOAD)
1782 boolean use_temporary_tape_file = FALSE;
1784 Print("Tape %03d:\n", level_nr);
1786 AutoPlayTapes_SetScoreEntry(0, 0);
1788 if (autoplay.tape_filename == NULL)
1790 autoplay.tape_filename = (options.mytapes ? getTapeFilename(level_nr) :
1791 getDefaultSolutionTapeFilename(level_nr));
1793 boolean correct_info_chunk =
1794 (strEqual(leveldir_current->identifier, tape.level_identifier) &&
1795 level_nr == tape.level_nr);
1797 if (!correct_info_chunk)
1799 strncpy(tape.level_identifier, leveldir_current->identifier,
1801 tape.level_identifier[MAX_FILENAME_LEN] = '\0';
1802 tape.level_nr = level_nr;
1805 if (!fileExists(autoplay.tape_filename) || !correct_info_chunk)
1807 // non-standard or incorrect solution tape -- save to temporary file
1808 autoplay.tape_filename = getTemporaryTapeFilename();
1810 SaveTapeToFilename(autoplay.tape_filename);
1812 use_temporary_tape_file = TRUE;
1816 SaveServerScoreFromFile(level_nr, autoplay.tape_filename);
1818 AutoPlayTapes_WaitForUpload();
1820 if (use_temporary_tape_file)
1821 unlink(autoplay.tape_filename);
1823 // required for uploading multiple tapes
1824 autoplay.tape_filename = NULL;
1831 if (options.tape_log_filename != NULL)
1834 TapeStartGamePlaying();
1835 TapeStartWarpForward(global.autoplay_mode);
1837 autoplay.last_level_nr = level_nr;
1842 // clear timestamp for batch tape upload (required after interactive upload)
1843 global.autoplay_time = 0;
1845 if (program.headless)
1851 int AutoPlayTapes(void)
1853 return AutoPlayTapesExt(TRUE);
1856 int AutoPlayTapesContinue(void)
1858 return AutoPlayTapesExt(FALSE);
1862 // ----------------------------------------------------------------------------
1863 // tape patch functions
1864 // ----------------------------------------------------------------------------
1866 static boolean PatchTape(struct TapeInfo *tape, char *mode)
1868 Print("[%d.%d.%d.%d]: ",
1869 (tape->engine_version / 1000000) % 100,
1870 (tape->engine_version / 10000 ) % 100,
1871 (tape->engine_version / 100 ) % 100,
1872 (tape->engine_version ) % 100);
1874 if (strEqual(mode, "info"))
1876 Print("property bits == 0x%02x\n", tape->property_bits);
1881 boolean unpatch_tape = FALSE;
1882 boolean use_property_bit = FALSE;
1883 byte property_bitmask = 0;
1885 if (strSuffix(mode, ":0") ||
1886 strSuffix(mode, ":off") ||
1887 strSuffix(mode, ":clear"))
1888 unpatch_tape = TRUE;
1890 if (strEqual(mode, "em_random_bug") || strPrefix(mode, "em_random_bug:"))
1892 // this bug was introduced in version 3.3.1.0 and fixed in version 4.0.1.2
1893 if (tape->engine_version < VERSION_IDENT(3,3,1,0) ||
1894 tape->engine_version >= VERSION_IDENT(4,0,1,2))
1896 Print("This tape version cannot be patched against EM random bug!\n");
1901 property_bitmask = TAPE_PROPERTY_EM_RANDOM_BUG;
1903 use_property_bit = TRUE;
1905 else if (strEqual(mode, "screen_34x34") || strPrefix(mode, "screen_34x34:"))
1907 // this bug only affects team mode tapes
1908 if (tape->num_participating_players == 1)
1910 Print("Only team mode tapes can be patched against screen size bug!\n");
1915 // this bug (that always existed before) was fixed in version 4.2.2.1
1916 if (tape->engine_version >= VERSION_IDENT(4,2,2,1))
1918 Print("This tape version cannot be patched against screen size bug!\n");
1923 int factor = (unpatch_tape ? 1 : 2);
1924 int scr_fieldx_new = SCR_FIELDX_DEFAULT * factor;
1925 int scr_fieldy_new = SCR_FIELDY_DEFAULT * factor;
1927 if (scr_fieldx_new == tape->scr_fieldx &&
1928 scr_fieldy_new == tape->scr_fieldy)
1930 Print("Tape already patched for '%s'!\n", mode);
1935 tape->scr_fieldx = scr_fieldx_new;
1936 tape->scr_fieldy = scr_fieldy_new;
1940 Print("Unknown patch mode '%s'!\n", mode);
1945 // patching tapes using property bits may be used for several patch modes
1946 if (use_property_bit)
1948 byte property_bits = tape->property_bits;
1949 boolean set_property_bit = (unpatch_tape ? FALSE : TRUE);
1951 if (set_property_bit)
1952 property_bits |= property_bitmask;
1954 property_bits &= ~property_bitmask;
1956 if (property_bits == tape->property_bits)
1958 Print("Tape already patched for '%s'!\n", mode);
1963 tape->property_bits = property_bits;
1966 Print("Patching for '%s' ... ", mode);
1971 void PatchTapes(void)
1973 static LevelDirTree *patchtapes_leveldir = NULL;
1974 static int num_tapes_found = 0;
1975 static int num_tapes_patched = 0;
1976 char *mode = global.patchtapes_mode;
1979 if (strEqual(mode, "help"))
1982 Print("Supported patch modes:\n");
1983 Print("- \"em_random_bug\" - use 64-bit random value bug for EM engine\n");
1984 Print("- \"screen_34x34\" - force visible playfield size of 34 x 34\n");
1986 Print("Supported modifiers:\n");
1987 Print("- add \":0\", \":off\" or \":clear\" to patch mode to un-patch tape file\n");
1993 patchtapes_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1994 global.patchtapes_leveldir);
1996 if (patchtapes_leveldir == NULL)
1997 Fail("no such level identifier: '%s'", global.patchtapes_leveldir);
1999 leveldir_current = patchtapes_leveldir;
2001 if (patchtapes_leveldir->first_level < 0)
2002 patchtapes_leveldir->first_level = 0;
2003 if (patchtapes_leveldir->last_level >= MAX_TAPES_PER_SET)
2004 patchtapes_leveldir->last_level = MAX_TAPES_PER_SET - 1;
2007 Print("Patching level tapes for patch mode '%s'\n", mode);
2009 Print("Level series identifier: '%s'\n", patchtapes_leveldir->identifier);
2010 Print("Level series name: '%s'\n", patchtapes_leveldir->name);
2011 Print("Level series author: '%s'\n", patchtapes_leveldir->author);
2012 Print("Number of levels: %d\n", patchtapes_leveldir->levels);
2016 int first_level = patchtapes_leveldir->first_level;
2017 int last_level = patchtapes_leveldir->last_level;
2019 for (i = first_level; i <= last_level; i++)
2021 if (!global.patchtapes_all && !global.patchtapes_level[i])
2024 Print("Tape %03d: ", i);
2029 if (tape.no_valid_file)
2031 Print("(no tape found)\n");
2038 if (PatchTape(&tape, mode))
2040 char *filename = getTapeFilename(i);
2041 char *filename_orig = getStringCat2(filename, ".orig");
2043 if (!fileExists(filename_orig))
2044 rename(filename, filename_orig);
2046 SaveTapeToFilename(filename);
2048 Print("patched tape saved.\n");
2050 num_tapes_patched++;
2056 Print("Number of tapes found: %d\n", num_tapes_found);
2057 Print("Number of tapes patched: %d\n", num_tapes_patched);
2064 // ---------- new tape button stuff -------------------------------------------
2072 } tapebutton_info[NUM_TAPE_BUTTONS] =
2075 IMG_GFX_TAPE_BUTTON_EJECT, &tape.button.eject,
2076 TAPE_CTRL_ID_EJECT, "eject tape"
2079 // (same position as "eject" button)
2080 IMG_GFX_TAPE_BUTTON_EXTRA, &tape.button.eject,
2081 TAPE_CTRL_ID_EXTRA, "extra functions"
2084 IMG_GFX_TAPE_BUTTON_STOP, &tape.button.stop,
2085 TAPE_CTRL_ID_STOP, "stop tape"
2088 IMG_GFX_TAPE_BUTTON_PAUSE, &tape.button.pause,
2089 TAPE_CTRL_ID_PAUSE, "pause tape"
2092 IMG_GFX_TAPE_BUTTON_RECORD, &tape.button.record,
2093 TAPE_CTRL_ID_RECORD, "record tape"
2096 IMG_GFX_TAPE_BUTTON_PLAY, &tape.button.play,
2097 TAPE_CTRL_ID_PLAY, "play tape"
2100 IMG_GFX_TAPE_BUTTON_INSERT_SOLUTION,&tape.button.insert_solution,
2101 TAPE_CTRL_ID_INSERT_SOLUTION, "insert solution tape"
2104 IMG_GFX_TAPE_BUTTON_PLAY_SOLUTION, &tape.button.play_solution,
2105 TAPE_CTRL_ID_PLAY_SOLUTION, "play solution tape"
2109 void CreateTapeButtons(void)
2113 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2115 int graphic = tapebutton_info[i].graphic;
2116 struct GraphicInfo *gfx = &graphic_info[graphic];
2117 struct XY *pos = tapebutton_info[i].pos;
2118 struct GadgetInfo *gi;
2119 int gd_x = gfx->src_x;
2120 int gd_y = gfx->src_y;
2121 int gd_xp = gfx->src_x + gfx->pressed_xoffset;
2122 int gd_yp = gfx->src_y + gfx->pressed_yoffset;
2125 gi = CreateGadget(GDI_CUSTOM_ID, id,
2126 GDI_IMAGE_ID, graphic,
2127 GDI_INFO_TEXT, tapebutton_info[i].infotext,
2130 GDI_WIDTH, gfx->width,
2131 GDI_HEIGHT, gfx->height,
2132 GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
2133 GDI_STATE, GD_BUTTON_UNPRESSED,
2134 GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
2135 GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
2136 GDI_DIRECT_DRAW, FALSE,
2137 GDI_EVENT_MASK, GD_EVENT_RELEASED,
2138 GDI_CALLBACK_ACTION, HandleTapeButtons,
2142 Fail("cannot create gadget");
2144 tape_gadget[id] = gi;
2148 void FreeTapeButtons(void)
2152 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2153 FreeGadget(tape_gadget[i]);
2156 void MapTapeEjectButton(void)
2158 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
2159 MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
2162 void MapTapeWarpButton(void)
2164 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
2165 MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
2168 void MapTapeButtons(void)
2172 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2173 if (i != TAPE_CTRL_ID_EXTRA)
2174 MapGadget(tape_gadget[i]);
2176 if (tape.recording || tape.playing)
2177 MapTapeWarpButton();
2179 if (tape.show_game_buttons)
2180 MapGameButtonsOnTape();
2183 void UnmapTapeButtons(void)
2187 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2188 UnmapGadget(tape_gadget[i]);
2190 if (tape.show_game_buttons)
2191 UnmapGameButtonsOnTape();
2194 void RedrawTapeButtons(void)
2198 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
2199 RedrawGadget(tape_gadget[i]);
2201 if (tape.show_game_buttons)
2202 RedrawGameButtonsOnTape();
2205 void RedrawOrRemapTapeButtons(void)
2207 if (tape_gadget[TAPE_CTRL_ID_PLAY]->mapped)
2209 // tape buttons already mapped
2210 RedrawTapeButtons();
2219 static void HandleTapeButtonsExt(int id)
2221 if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
2226 case TAPE_CTRL_ID_EJECT:
2229 if (TAPE_IS_EMPTY(tape))
2233 if (TAPE_IS_EMPTY(tape))
2234 Request("No tape for this level!", REQ_CONFIRM);
2239 SaveTapeChecked(level_nr);
2244 DrawCompleteVideoDisplay();
2247 case TAPE_CTRL_ID_EXTRA:
2250 tape.pause_before_end = !tape.pause_before_end;
2252 DrawVideoDisplayCurrentState();
2254 else if (tape.recording)
2261 case TAPE_CTRL_ID_STOP:
2266 case TAPE_CTRL_ID_PAUSE:
2267 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2271 case TAPE_CTRL_ID_RECORD:
2272 if (TAPE_IS_STOPPED(tape))
2274 TapeStartGameRecording();
2276 else if (tape.pausing)
2278 if (tape.playing) // PLAY -> PAUSE -> RECORD
2279 TapeAppendRecording();
2281 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2286 case TAPE_CTRL_ID_PLAY:
2287 if (tape.recording && tape.pausing) // PAUSE -> RECORD
2289 // ("TAPE_IS_EMPTY(tape)" is TRUE here -- probably fix this)
2291 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2294 if (TAPE_IS_EMPTY(tape))
2297 if (TAPE_IS_STOPPED(tape))
2299 TapeStartGamePlaying();
2301 else if (tape.playing)
2303 if (tape.pausing) // PAUSE -> PLAY
2305 TapeTogglePause(TAPE_TOGGLE_MANUAL | TAPE_TOGGLE_PLAY_PAUSE);
2307 else if (!tape.fast_forward) // PLAY -> FFWD
2309 tape.fast_forward = TRUE;
2311 else if (!tape.warp_forward) // FFWD -> WARP
2313 tape.warp_forward = TRUE;
2315 else if (!tape.deactivate_display) // WARP -> WARP BLIND
2317 tape.deactivate_display = TRUE;
2319 TapeDeactivateDisplayOn();
2321 else // WARP BLIND -> PLAY
2323 tape.fast_forward = FALSE;
2324 tape.warp_forward = FALSE;
2325 tape.deactivate_display = FALSE;
2327 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
2330 DrawVideoDisplayCurrentState();
2335 case TAPE_CTRL_ID_INSERT_SOLUTION:
2336 InsertSolutionTape();
2340 case TAPE_CTRL_ID_PLAY_SOLUTION:
2350 static void HandleTapeButtons(struct GadgetInfo *gi)
2352 HandleTapeButtonsExt(gi->custom_id);
2355 void HandleTapeButtonKeys(Key key)
2357 boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
2358 boolean extra_button_is_active = !eject_button_is_active;
2360 if (key == setup.shortcut.tape_eject && eject_button_is_active)
2361 HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
2362 else if (key == setup.shortcut.tape_extra && extra_button_is_active)
2363 HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
2364 else if (key == setup.shortcut.tape_stop)
2365 HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
2366 else if (key == setup.shortcut.tape_pause)
2367 HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
2368 else if (key == setup.shortcut.tape_record)
2369 HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
2370 else if (key == setup.shortcut.tape_play)
2371 HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);