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 // ============================================================================
474 static char tape_patch_info[MAX_OUTPUT_LINESIZE];
476 static void PrintTapeReplayProgress(boolean replay_finished)
478 static unsigned int counter_last = -1;
479 unsigned int counter = Counter();
480 unsigned int counter_seconds = counter / 1000;
482 if (!replay_finished)
484 unsigned int counter_delay = 50;
486 if (counter > counter_last + counter_delay)
489 PrintNoLog("Tape %03d %s[%02d:%02d]: [%02d:%02d] - playing tape ... ",
490 level_nr, tape_patch_info,
491 tape.length_seconds / 60, tape.length_seconds % 60,
492 TapeTime / 60, TapeTime % 60);
494 counter_last = counter;
499 float tape_length_seconds = GetTapeLengthSecondsFloat();
502 Print("Tape %03d %s[%02d:%02d]: (%02d:%02d.%03d / %.2f %%) - %s.\n",
503 level_nr, tape_patch_info,
504 tape.length_seconds / 60, tape.length_seconds % 60,
505 counter_seconds / 60, counter_seconds % 60, counter % 1000,
506 (float)counter / tape_length_seconds / 10,
507 tape.auto_play_level_fixed ? "solved and fixed" :
508 tape.auto_play_level_solved ? "solved" :
509 tape.auto_play_level_not_fixable ? "NOT SOLVED, NOT FIXED" :
516 static FILE *tape_log_file;
518 static void OpenTapeLogfile(void)
520 if (!(tape_log_file = fopen(options.tape_log_filename, MODE_WRITE)))
521 Warn("cannot write tape logfile '%s'", options.tape_log_filename);
524 static void WriteTapeLogfile(byte action[MAX_TAPE_ACTIONS])
528 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
529 putFile8Bit(tape_log_file, action[i]);
532 static void CloseTapeLogfile(void)
534 fclose(tape_log_file);
538 // ============================================================================
539 // tape control functions
540 // ============================================================================
542 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
544 struct tm *lt = localtime(&epoch_seconds);
546 tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
549 void TapeSetDateFromNow(void)
551 TapeSetDateFromEpochSeconds(time(NULL));
558 CopyTapeToUndoBuffer();
562 tape.length_frames = 0;
563 tape.length_seconds = 0;
565 tape.score_tape_basename[0] = '\0';
567 if (leveldir_current)
569 strncpy(tape.level_identifier, leveldir_current->identifier,
571 tape.level_identifier[MAX_FILENAME_LEN] = '\0';
574 tape.level_nr = level_nr;
575 tape.pos[tape.counter].delay = 0;
578 tape.random_seed = InitRND(level.random_seed);
580 tape.file_version = FILE_VERSION_ACTUAL;
581 tape.game_version = GAME_VERSION_ACTUAL;
582 tape.engine_version = level.game_version;
584 TapeSetDateFromNow();
586 for (i = 0; i < MAX_PLAYERS; i++)
587 tape.player_participates[i] = FALSE;
589 tape.centered_player_nr_next = -1;
590 tape.set_centered_player = FALSE;
592 // set flags for game actions to default values (may be overwritten later)
593 tape.use_key_actions = TRUE;
594 tape.use_mouse_actions = FALSE;
597 static void TapeRewind(void)
600 tape.delay_played = 0;
601 tape.pause_before_end = FALSE;
602 tape.recording = FALSE;
603 tape.playing = FALSE;
604 tape.fast_forward = FALSE;
605 tape.warp_forward = FALSE;
606 tape.deactivate_display = FALSE;
607 tape.auto_play = (global.autoplay_leveldir != NULL);
608 tape.auto_play_level_solved = FALSE;
609 tape.auto_play_level_fixed = FALSE;
610 tape.auto_play_level_not_fixable = FALSE;
611 tape.quick_resume = FALSE;
612 tape.single_step = FALSE;
614 tape.centered_player_nr_next = -1;
615 tape.set_centered_player = FALSE;
617 InitRND(tape.random_seed);
620 static void TapeSetRandomSeed(int random_seed)
622 tape.random_seed = InitRND(random_seed);
625 void TapeStartRecording(int random_seed)
627 if (!TAPE_IS_STOPPED(tape))
632 TapeSetRandomSeed(random_seed);
634 tape.recording = TRUE;
636 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
637 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
638 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
639 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
643 SetDrawDeactivationMask(REDRAW_NONE);
644 audio.sound_deactivated = FALSE;
646 // required here to update video display if tape door is closed
647 if (GetDoorState() & DOOR_CLOSE_2)
648 OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
651 static void TapeStartGameRecording(void)
653 StartGameActions(network.enabled, TRUE, level.random_seed);
656 static void TapeAppendRecording(void)
658 if (!tape.playing || !tape.pausing)
662 tape.playing = FALSE;
663 tape.fast_forward = FALSE;
664 tape.warp_forward = FALSE;
665 tape.pause_before_end = FALSE;
666 tape.deactivate_display = FALSE;
669 tape.recording = TRUE;
672 // set current delay (for last played move)
673 tape.pos[tape.counter].delay = tape.delay_played;
676 TapeSetDateFromNow();
678 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
679 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
681 UpdateAndDisplayGameControlValues();
684 void TapeHaltRecording(void)
688 // initialize delay for next tape entry (to be able to continue recording)
689 if (tape.counter < MAX_TAPE_LEN)
690 tape.pos[tape.counter].delay = 0;
692 tape.length = tape.counter;
693 tape.length_frames = GetTapeLengthFrames();
694 tape.length_seconds = GetTapeLengthSeconds();
697 void TapeStopRecording(void)
702 tape.recording = FALSE;
703 tape.pausing = FALSE;
705 DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
706 MapTapeEjectButton();
709 boolean TapeAddAction(byte action[MAX_TAPE_ACTIONS])
713 if (tape.pos[tape.counter].delay > 0) // already stored action
715 boolean changed_events = FALSE;
717 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
718 if (tape.pos[tape.counter].action[i] != action[i])
719 changed_events = TRUE;
721 if (changed_events || tape.pos[tape.counter].delay >= 255)
723 if (tape.counter >= MAX_TAPE_LEN - 1)
727 tape.pos[tape.counter].delay = 0;
730 tape.pos[tape.counter].delay++;
733 if (tape.pos[tape.counter].delay == 0) // store new action
735 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
736 tape.pos[tape.counter].action[i] = action[i];
738 tape.pos[tape.counter].delay++;
744 void TapeRecordAction(byte action_raw[MAX_TAPE_ACTIONS])
746 byte action[MAX_TAPE_ACTIONS];
749 if (!tape.recording) // (record action even when tape is paused)
752 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
753 action[i] = action_raw[i];
755 if (tape.use_key_actions && tape.set_centered_player)
757 for (i = 0; i < MAX_PLAYERS; i++)
758 if (tape.centered_player_nr_next == i ||
759 tape.centered_player_nr_next == -1)
760 action[i] |= KEY_SET_FOCUS;
762 tape.set_centered_player = FALSE;
765 if (!TapeAddAction(action))
769 void TapeTogglePause(boolean toggle_mode)
771 if (tape.playing && tape.pausing && (toggle_mode & TAPE_TOGGLE_PLAY_PAUSE))
773 // continue playing in normal mode
774 tape.fast_forward = FALSE;
775 tape.warp_forward = FALSE;
776 tape.deactivate_display = FALSE;
778 tape.pause_before_end = FALSE;
781 tape.pausing = !tape.pausing;
783 if (tape.single_step && (toggle_mode & TAPE_TOGGLE_MANUAL))
784 tape.single_step = FALSE;
786 DrawVideoDisplayCurrentState();
788 if (tape.deactivate_display)
791 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
793 TapeDeactivateDisplayOn();
796 if (tape.quick_resume)
798 tape.quick_resume = FALSE;
800 TapeStopWarpForward();
801 TapeAppendRecording();
803 if (!CheckEngineSnapshotSingle())
804 SaveEngineSnapshotSingle();
806 // restart step/move snapshots after quick loading tape
807 SaveEngineSnapshotToListInitial();
809 // do not map undo/redo buttons after quick loading tape
813 if (game_status == GAME_MODE_PLAYING)
815 if (setup.show_snapshot_buttons && CheckEngineSnapshotList())
818 MapUndoRedoButtons();
819 else if (!tape.single_step)
820 UnmapUndoRedoButtons();
823 ModifyPauseButtons();
827 void TapeStartPlaying(void)
829 if (TAPE_IS_EMPTY(tape))
832 if (!TAPE_IS_STOPPED(tape))
839 DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
840 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
841 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
842 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
846 SetDrawDeactivationMask(REDRAW_NONE);
847 audio.sound_deactivated = FALSE;
850 static void TapeStartGamePlaying(void)
857 void TapeStopPlaying(void)
859 tape.playing = FALSE;
860 tape.pausing = FALSE;
862 if (tape.warp_forward)
863 TapeStopWarpForward();
865 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
866 MapTapeEjectButton();
869 byte *TapePlayAction(void)
871 int update_delay = FRAMES_PER_SECOND / 2;
872 boolean update_video_display = (FrameCounter % update_delay == 0);
873 boolean update_draw_label_on = ((FrameCounter / update_delay) % 2 == 1);
874 static byte action[MAX_TAPE_ACTIONS];
877 if (!tape.playing || tape.pausing)
880 if (tape.pause_before_end) // stop some seconds before end of tape
882 if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
884 TapeStopWarpForward();
885 TapeTogglePause(TAPE_TOGGLE_MANUAL);
891 if (tape.counter >= tape.length) // end of tape reached
893 if (tape.warp_forward && !tape.auto_play)
895 TapeStopWarpForward();
896 TapeTogglePause(TAPE_TOGGLE_MANUAL);
906 if (update_video_display && !tape.deactivate_display)
910 if (tape.warp_forward)
911 state |= VIDEO_STATE_WARP(update_draw_label_on);
912 else if (tape.fast_forward)
913 state |= VIDEO_STATE_FFWD(update_draw_label_on);
915 if (tape.pause_before_end)
916 state |= VIDEO_STATE_PBEND(update_draw_label_on);
918 // draw labels and symbols separately to prevent labels overlapping symbols
919 DrawVideoDisplayLabel(state);
920 DrawVideoDisplaySymbol(state);
923 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
924 action[i] = tape.pos[tape.counter].action[i];
926 #if DEBUG_TAPE_WHEN_PLAYING
927 DebugContinued("", "%05d", FrameCounter);
928 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
929 DebugContinued("", " %08x", action[i]);
930 DebugContinued("tape:play", "\n");
933 tape.set_centered_player = FALSE;
934 tape.centered_player_nr_next = -999;
936 if (tape.use_key_actions)
938 for (i = 0; i < MAX_PLAYERS; i++)
940 if (action[i] & KEY_SET_FOCUS)
942 tape.set_centered_player = TRUE;
943 tape.centered_player_nr_next =
944 (tape.centered_player_nr_next == -999 ? i : -1);
947 action[i] &= ~KEY_SET_FOCUS;
952 if (tape.delay_played >= tape.pos[tape.counter].delay)
955 tape.delay_played = 0;
959 PrintTapeReplayProgress(FALSE);
961 if (options.tape_log_filename != NULL)
962 WriteTapeLogfile(action);
970 TapeTogglePause(TAPE_TOGGLE_MANUAL);
975 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
977 if (tape.date && tape.length)
979 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
980 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
981 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
985 unsigned int GetTapeLengthFrames(void)
987 unsigned int tape_length_frames = 0;
990 if (TAPE_IS_EMPTY(tape))
993 for (i = 0; i < tape.length; i++)
994 tape_length_frames += tape.pos[i].delay;
996 return tape_length_frames;
999 unsigned int GetTapeLengthSeconds(void)
1001 return (GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
1004 static float GetTapeLengthSecondsFloat(void)
1006 return ((float)GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
1009 static void TapeStartWarpForward(int mode)
1011 tape.fast_forward = (mode & AUTOPLAY_FFWD);
1012 tape.warp_forward = (mode & AUTOPLAY_WARP);
1013 tape.deactivate_display = (mode & AUTOPLAY_WARP_NO_DISPLAY);
1015 tape.pausing = FALSE;
1017 if (tape.deactivate_display)
1018 TapeDeactivateDisplayOn();
1020 DrawVideoDisplayCurrentState();
1023 static void TapeStopWarpForward(void)
1025 tape.fast_forward = FALSE;
1026 tape.warp_forward = FALSE;
1027 tape.deactivate_display = FALSE;
1029 tape.pause_before_end = FALSE;
1031 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
1033 DrawVideoDisplayCurrentState();
1036 static void TapeSingleStep(void)
1038 if (network.enabled)
1042 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1044 tape.single_step = !tape.single_step;
1046 DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
1049 void TapeQuickSave(void)
1051 if (game_status == GAME_MODE_MAIN)
1053 Request("No game that can be saved!", REQ_CONFIRM);
1058 if (game_status != GAME_MODE_PLAYING)
1062 TapeHaltRecording(); // prepare tape for saving on-the-fly
1064 if (TAPE_IS_EMPTY(tape))
1066 Request("No tape that can be saved!", REQ_CONFIRM);
1071 if (SaveTapeChecked(tape.level_nr))
1072 SaveEngineSnapshotSingle();
1075 void TapeQuickLoad(void)
1077 char *filename = getTapeFilename(level_nr);
1079 if (!fileExists(filename))
1081 Request("No tape for this level!", REQ_CONFIRM);
1086 if (tape.recording && !Request("Stop recording and load tape?",
1087 REQ_ASK | REQ_STAY_CLOSED))
1089 OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1094 if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
1097 if (CheckEngineSnapshotSingle())
1099 TapeStartGamePlaying();
1101 LoadEngineSnapshotSingle();
1103 DrawCompleteVideoDisplay();
1105 tape.playing = TRUE;
1106 tape.pausing = TRUE;
1108 TapeStopWarpForward();
1109 TapeAppendRecording();
1111 // restart step/move snapshots after quick loading tape
1112 SaveEngineSnapshotToListInitial();
1114 if (FrameCounter > 0)
1123 if (!TAPE_IS_EMPTY(tape))
1125 TapeStartGamePlaying();
1126 TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
1128 tape.quick_resume = TRUE;
1130 else // this should not happen (basically checked above)
1132 int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1134 Request("No tape for this level!", REQ_CONFIRM | reopen_door);
1138 boolean hasSolutionTape(void)
1140 boolean tape_file_exists = fileExists(getSolutionTapeFilename(level_nr));
1141 boolean level_has_tape = (level.game_engine_type == GAME_ENGINE_TYPE_SP &&
1142 level.native_sp_level->demo.is_available);
1144 return (tape_file_exists || level_has_tape);
1147 boolean InsertSolutionTape(void)
1149 if (!hasSolutionTape())
1151 Request("No solution tape for this level!", REQ_CONFIRM);
1156 if (!TAPE_IS_STOPPED(tape))
1159 // if tape recorder already contains a tape, remove it without asking
1162 LoadSolutionTape(level_nr);
1164 DrawCompleteVideoDisplay();
1166 if (TAPE_IS_EMPTY(tape))
1168 Request("Loading solution tape for this level failed!", REQ_CONFIRM);
1176 boolean PlaySolutionTape(void)
1178 if (!InsertSolutionTape())
1181 TapeStartGamePlaying();
1186 static boolean checkTapesFromSameLevel(struct TapeInfo *t1, struct TapeInfo *t2)
1188 return (strEqual(t1->level_identifier, t2->level_identifier) &&
1189 t1->level_nr == t2->level_nr);
1192 static void CopyTape(struct TapeInfo *tape_from, struct TapeInfo *tape_to)
1194 *tape_to = *tape_from;
1197 static void SwapTapes(struct TapeInfo *t1, struct TapeInfo *t2)
1199 struct TapeInfo tmp = *t1;
1205 static void CopyTapeToUndoBuffer(void)
1207 // copy tapes to undo buffer if large enough (or larger than last undo tape)
1208 // or if the last undo tape is from a different level set or level number
1209 if (tape.length_seconds >= TAPE_MIN_SECONDS_FOR_UNDO_BUFFER ||
1210 tape.length_seconds >= tape_undo_buffer.length_seconds ||
1211 !checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1213 CopyTape(&tape, &tape_undo_buffer);
1219 // only undo tapes from same level set and with same level number
1220 if (!checkTapesFromSameLevel(&tape, &tape_undo_buffer))
1223 if (!TAPE_IS_STOPPED(tape))
1226 // swap last recorded tape with undo buffer, so undo can be reversed
1227 SwapTapes(&tape, &tape_undo_buffer);
1229 DrawCompleteVideoDisplay();
1232 void FixTape_ForceSinglePlayer(void)
1236 /* fix single-player tapes that contain player input for more than one
1237 player (due to a bug in 3.3.1.2 and earlier versions), which results
1238 in playing levels with more than one player in multi-player mode,
1239 even though the tape was originally recorded in single-player mode */
1241 // remove player input actions for all players but the first one
1242 for (i = 1; i < MAX_PLAYERS; i++)
1243 tape.player_participates[i] = FALSE;
1245 tape.changed = TRUE;
1249 // ----------------------------------------------------------------------------
1250 // tape autoplay functions
1251 // ----------------------------------------------------------------------------
1253 void AutoPlayTapes(void)
1255 static LevelDirTree *autoplay_leveldir = NULL;
1256 static boolean autoplay_initialized = FALSE;
1257 static int autoplay_last_level_nr = -1;
1258 static int autoplay_level_nr = -1;
1259 static int num_levels_played = 0;
1260 static int num_levels_solved = 0;
1261 static int num_tapes_patched = 0;
1262 static int num_tape_missing = 0;
1263 static boolean level_failed[MAX_TAPES_PER_SET];
1264 static char *tape_filename = NULL;
1265 static int patch_nr = 0;
1266 static char *patch_name[] =
1274 static int patch_version_first[] =
1276 VERSION_IDENT(0,0,0,0),
1277 VERSION_IDENT(3,3,1,0),
1278 VERSION_IDENT(0,0,0,0),
1282 static int patch_version_last[] =
1284 VERSION_IDENT(9,9,9,9),
1285 VERSION_IDENT(4,0,1,1),
1286 VERSION_IDENT(4,2,2,0),
1290 static byte patch_property_bit[] =
1293 TAPE_PROPERTY_EM_RANDOM_BUG,
1300 if (autoplay_initialized)
1302 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1304 if (tape.auto_play_level_solved)
1308 // level solved by patched tape -- save fixed tape
1309 char *filename = getTapeFilename(level_nr);
1310 char *filename_orig = getStringCat2(filename, ".orig");
1312 // create backup from old tape, if not yet existing
1313 if (!fileExists(filename_orig))
1314 rename(filename, filename_orig);
1316 SaveTapeToFilename(filename);
1318 tape.auto_play_level_fixed = TRUE;
1319 num_tapes_patched++;
1322 // continue with next tape
1325 else if (patch_name[patch_nr + 1] != NULL)
1327 // level not solved by patched tape -- continue with next patch
1332 // level not solved by any patched tape -- continue with next tape
1333 tape.auto_play_level_not_fixable = TRUE;
1338 // just finished auto-playing tape
1339 PrintTapeReplayProgress(TRUE);
1341 if (options.tape_log_filename != NULL)
1344 if (global.autoplay_mode == AUTOPLAY_MODE_SAVE &&
1345 tape.auto_play_level_solved)
1347 // set unique basename for score tape (for uploading to score server)
1348 strcpy(tape.score_tape_basename, getScoreTapeBasename(setup.player_name));
1350 // store score in first score entry
1351 scores.last_added = 0;
1353 struct ScoreEntry *entry = &scores.entry[scores.last_added];
1355 strncpy(entry->tape_basename, tape.score_tape_basename, MAX_FILENAME_LEN);
1356 strncpy(entry->name, setup.player_name, MAX_PLAYER_NAME_LEN);
1358 entry->score = game.score_final;
1359 entry->time = game.score_time_final;
1361 if (leveldir_current)
1363 // the tape's level set identifier may differ from current level set
1364 strncpy(tape.level_identifier, leveldir_current->identifier,
1366 tape.level_identifier[MAX_FILENAME_LEN] = '\0';
1368 // the tape's level number may differ from current level number
1369 tape.level_nr = level_nr;
1372 PrintNoLog("- uploading score tape to score server ... ");
1374 server_scores.uploaded = FALSE;
1376 // save score tape to upload to server; may be required for some reasons:
1377 // * level set identifier in solution tapes may differ from level set
1378 // * solution tape may have native format (like Supaplex solution files)
1380 SaveScoreTape(level_nr);
1381 SaveServerScore(level_nr);
1383 unsigned int upload_delay = 0;
1384 unsigned int upload_delay_value = 10000;
1386 ResetDelayCounter(&upload_delay);
1388 // wait for score tape to be successfully uploaded (and fail on timeout)
1389 while (!server_scores.uploaded)
1391 if (DelayReached(&upload_delay, upload_delay_value))
1394 Print("- uploading score tape to score server - TIMEOUT.\n");
1396 Fail("cannot upload score tape to score server");
1403 Print("- uploading score tape to score server - uploaded.\n");
1407 num_levels_played++;
1409 if (tape.auto_play_level_solved)
1410 num_levels_solved++;
1412 if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1413 level_failed[level_nr] = !tape.auto_play_level_solved;
1417 DrawCompleteVideoDisplay();
1419 audio.sound_enabled = FALSE;
1420 setup.engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_OFF);
1422 if (strSuffix(global.autoplay_leveldir, ".tape"))
1424 tape_filename = global.autoplay_leveldir;
1426 LoadTapeFromFilename(tape_filename);
1428 if (tape.no_valid_file)
1430 if (!fileExists(tape_filename))
1431 Fail("tape file '%s' does not exist", tape_filename);
1433 Fail("cannot load tape file '%s'", tape_filename);
1436 global.autoplay_leveldir = tape.level_identifier;
1438 if (tape.level_nr >= 0 && tape.level_nr < MAX_TAPES_PER_SET)
1439 global.autoplay_level[tape.level_nr] = TRUE;
1441 global.autoplay_all = FALSE;
1444 autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1445 global.autoplay_leveldir);
1447 if (autoplay_leveldir == NULL)
1448 Fail("no such level identifier: '%s'", global.autoplay_leveldir);
1450 leveldir_current = autoplay_leveldir;
1452 if (autoplay_leveldir->first_level < 0)
1453 autoplay_leveldir->first_level = 0;
1454 if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
1455 autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1457 autoplay_level_nr = autoplay_leveldir->first_level;
1460 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1461 Print("Automatically fixing level tapes\n");
1463 Print("Automatically playing level tapes\n");
1465 Print("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
1466 Print("Level series name: '%s'\n", autoplay_leveldir->name);
1467 Print("Level series author: '%s'\n", autoplay_leveldir->author);
1468 Print("Number of levels: %d\n", autoplay_leveldir->levels);
1472 for (i = 0; i < MAX_TAPES_PER_SET; i++)
1473 level_failed[i] = FALSE;
1475 // only private tapes may be modified
1476 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1477 options.mytapes = TRUE;
1479 autoplay_initialized = TRUE;
1484 if (global.autoplay_mode != AUTOPLAY_MODE_FIX || patch_nr == 0)
1485 level_nr = autoplay_level_nr++;
1487 if (level_nr > autoplay_leveldir->last_level)
1490 // set patch info (required for progress output)
1491 strcpy(tape_patch_info, "");
1492 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1493 snprintf(tape_patch_info, MAX_OUTPUT_LINESIZE, "[%-13s] ",
1494 patch_name[patch_nr]);
1496 if (!global.autoplay_all && !global.autoplay_level[level_nr])
1501 LoadLevel(level_nr);
1503 if (level.no_level_file || level.no_valid_file)
1505 Print("Tape %03d: (no level found)\n", level_nr);
1511 // ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY
1512 Print("Tape %03d: (only testing level)\n", level_nr);
1517 LoadTapeFromFilename(tape_filename);
1518 else if (options.mytapes)
1521 LoadSolutionTape(level_nr);
1523 if (tape.no_valid_file)
1527 Print("Tape %03d: (no tape found)\n", level_nr);
1532 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1534 boolean skip_patch = FALSE;
1536 if (tape.engine_version < patch_version_first[patch_nr] ||
1537 tape.engine_version > patch_version_last[patch_nr])
1539 Print("Tape %03d %s[%02d:%02d]: (%s %d.%d.%d.%d) - skipped.\n",
1540 level_nr, tape_patch_info,
1541 tape.length_seconds / 60, tape.length_seconds % 60,
1542 "not suitable for version",
1543 (tape.engine_version / 1000000) % 100,
1544 (tape.engine_version / 10000 ) % 100,
1545 (tape.engine_version / 100 ) % 100,
1546 (tape.engine_version ) % 100);
1551 if (strEqual(patch_name[patch_nr], "screen_34x34") &&
1552 tape.num_participating_players == 1)
1554 Print("Tape %03d %s[%02d:%02d]: (%s) - skipped.\n",
1555 level_nr, tape_patch_info,
1556 tape.length_seconds / 60, tape.length_seconds % 60,
1557 "not suitable for single player tapes");
1564 if (patch_name[patch_nr + 1] != NULL)
1566 // continue with next patch
1571 // continue with next tape
1578 if (strEqual(patch_name[patch_nr], "screen_34x34"))
1580 tape.scr_fieldx = SCR_FIELDX_DEFAULT * 2;
1581 tape.scr_fieldy = SCR_FIELDY_DEFAULT * 2;
1585 tape.property_bits |= patch_property_bit[patch_nr];
1591 if (options.tape_log_filename != NULL)
1594 TapeStartGamePlaying();
1595 TapeStartWarpForward(global.autoplay_mode);
1597 autoplay_last_level_nr = level_nr;
1602 char *autoplay_status = (num_levels_played == num_levels_solved &&
1603 num_levels_played > 0 ? " OK " : "WARN");
1604 int autoplay_percent = (num_levels_played ?
1605 num_levels_solved * 100 / num_levels_played : 0);
1609 Print("Number of levels played: %d\n", num_levels_played);
1610 Print("Number of levels solved: %d (%d%%)\n", num_levels_solved,
1611 (num_levels_played ? num_levels_solved * 100 / num_levels_played : 0));
1612 if (global.autoplay_mode == AUTOPLAY_MODE_FIX)
1613 Print("Number of tapes fixed: %d\n", num_tapes_patched);
1615 Print("Summary (for automatic parsing by scripts):\n");
1619 Print("TAPEFILE [%s] '%s', %d, %d, %d",
1621 autoplay_leveldir->identifier,
1622 autoplay_last_level_nr,
1624 game.score_time_final);
1628 Print("LEVELDIR [%s] '%s', SOLVED %d/%d (%d%%)",
1630 autoplay_leveldir->identifier,
1635 if (num_levels_played != num_levels_solved)
1638 for (i = 0; i < MAX_TAPES_PER_SET; i++)
1639 if (level_failed[i])
1651 // ----------------------------------------------------------------------------
1652 // tape patch functions
1653 // ----------------------------------------------------------------------------
1655 static boolean PatchTape(struct TapeInfo *tape, char *mode)
1657 Print("[%d.%d.%d.%d]: ",
1658 (tape->engine_version / 1000000) % 100,
1659 (tape->engine_version / 10000 ) % 100,
1660 (tape->engine_version / 100 ) % 100,
1661 (tape->engine_version ) % 100);
1663 if (strEqual(mode, "info"))
1665 Print("property bits == 0x%02x\n", tape->property_bits);
1670 boolean unpatch_tape = FALSE;
1671 boolean use_property_bit = FALSE;
1672 byte property_bitmask = 0;
1674 if (strSuffix(mode, ":0") ||
1675 strSuffix(mode, ":off") ||
1676 strSuffix(mode, ":clear"))
1677 unpatch_tape = TRUE;
1679 if (strEqual(mode, "em_random_bug") || strPrefix(mode, "em_random_bug:"))
1681 // this bug was introduced in version 3.3.1.0 and fixed in version 4.0.1.2
1682 if (tape->engine_version < VERSION_IDENT(3,3,1,0) ||
1683 tape->engine_version >= VERSION_IDENT(4,0,1,2))
1685 Print("This tape version cannot be patched against EM random bug!\n");
1690 property_bitmask = TAPE_PROPERTY_EM_RANDOM_BUG;
1692 use_property_bit = TRUE;
1694 else if (strEqual(mode, "screen_34x34") || strPrefix(mode, "screen_34x34:"))
1696 // this bug only affects team mode tapes
1697 if (tape->num_participating_players == 1)
1699 Print("Only team mode tapes can be patched against screen size bug!\n");
1704 // this bug (that always existed before) was fixed in version 4.2.2.1
1705 if (tape->engine_version >= VERSION_IDENT(4,2,2,1))
1707 Print("This tape version cannot be patched against screen size bug!\n");
1712 int factor = (unpatch_tape ? 1 : 2);
1713 int scr_fieldx_new = SCR_FIELDX_DEFAULT * factor;
1714 int scr_fieldy_new = SCR_FIELDY_DEFAULT * factor;
1716 if (scr_fieldx_new == tape->scr_fieldx &&
1717 scr_fieldy_new == tape->scr_fieldy)
1719 Print("Tape already patched for '%s'!\n", mode);
1724 tape->scr_fieldx = scr_fieldx_new;
1725 tape->scr_fieldy = scr_fieldy_new;
1729 Print("Unknown patch mode '%s'!\n", mode);
1734 // patching tapes using property bits may be used for several patch modes
1735 if (use_property_bit)
1737 byte property_bits = tape->property_bits;
1738 boolean set_property_bit = (unpatch_tape ? FALSE : TRUE);
1740 if (set_property_bit)
1741 property_bits |= property_bitmask;
1743 property_bits &= ~property_bitmask;
1745 if (property_bits == tape->property_bits)
1747 Print("Tape already patched for '%s'!\n", mode);
1752 tape->property_bits = property_bits;
1755 Print("Patching for '%s' ... ", mode);
1760 void PatchTapes(void)
1762 static LevelDirTree *patchtapes_leveldir = NULL;
1763 static int num_tapes_found = 0;
1764 static int num_tapes_patched = 0;
1765 char *mode = global.patchtapes_mode;
1768 if (strEqual(mode, "help"))
1771 Print("Supported patch modes:\n");
1772 Print("- \"em_random_bug\" - use 64-bit random value bug for EM engine\n");
1773 Print("- \"screen_34x34\" - force visible playfield size of 34 x 34\n");
1775 Print("Supported modifiers:\n");
1776 Print("- add \":0\", \":off\" or \":clear\" to patch mode to un-patch tape file\n");
1782 patchtapes_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1783 global.patchtapes_leveldir);
1785 if (patchtapes_leveldir == NULL)
1786 Fail("no such level identifier: '%s'", global.patchtapes_leveldir);
1788 leveldir_current = patchtapes_leveldir;
1790 if (patchtapes_leveldir->first_level < 0)
1791 patchtapes_leveldir->first_level = 0;
1792 if (patchtapes_leveldir->last_level >= MAX_TAPES_PER_SET)
1793 patchtapes_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1796 Print("Patching level tapes for patch mode '%s'\n", mode);
1798 Print("Level series identifier: '%s'\n", patchtapes_leveldir->identifier);
1799 Print("Level series name: '%s'\n", patchtapes_leveldir->name);
1800 Print("Level series author: '%s'\n", patchtapes_leveldir->author);
1801 Print("Number of levels: %d\n", patchtapes_leveldir->levels);
1805 int first_level = patchtapes_leveldir->first_level;
1806 int last_level = patchtapes_leveldir->last_level;
1808 for (i = first_level; i <= last_level; i++)
1810 if (!global.patchtapes_all && !global.patchtapes_level[i])
1813 Print("Tape %03d: ", i);
1818 if (tape.no_valid_file)
1820 Print("(no tape found)\n");
1827 if (PatchTape(&tape, mode))
1829 char *filename = getTapeFilename(i);
1830 char *filename_orig = getStringCat2(filename, ".orig");
1832 if (!fileExists(filename_orig))
1833 rename(filename, filename_orig);
1835 SaveTapeToFilename(filename);
1837 Print("patched tape saved.\n");
1839 num_tapes_patched++;
1845 Print("Number of tapes found: %d\n", num_tapes_found);
1846 Print("Number of tapes patched: %d\n", num_tapes_patched);
1853 // ---------- new tape button stuff -------------------------------------------
1861 } tapebutton_info[NUM_TAPE_BUTTONS] =
1864 IMG_GFX_TAPE_BUTTON_EJECT, &tape.button.eject,
1865 TAPE_CTRL_ID_EJECT, "eject tape"
1868 // (same position as "eject" button)
1869 IMG_GFX_TAPE_BUTTON_EXTRA, &tape.button.eject,
1870 TAPE_CTRL_ID_EXTRA, "extra functions"
1873 IMG_GFX_TAPE_BUTTON_STOP, &tape.button.stop,
1874 TAPE_CTRL_ID_STOP, "stop tape"
1877 IMG_GFX_TAPE_BUTTON_PAUSE, &tape.button.pause,
1878 TAPE_CTRL_ID_PAUSE, "pause tape"
1881 IMG_GFX_TAPE_BUTTON_RECORD, &tape.button.record,
1882 TAPE_CTRL_ID_RECORD, "record tape"
1885 IMG_GFX_TAPE_BUTTON_PLAY, &tape.button.play,
1886 TAPE_CTRL_ID_PLAY, "play tape"
1889 IMG_GFX_TAPE_BUTTON_INSERT_SOLUTION,&tape.button.insert_solution,
1890 TAPE_CTRL_ID_INSERT_SOLUTION, "insert solution tape"
1893 IMG_GFX_TAPE_BUTTON_PLAY_SOLUTION, &tape.button.play_solution,
1894 TAPE_CTRL_ID_PLAY_SOLUTION, "play solution tape"
1898 void CreateTapeButtons(void)
1902 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1904 int graphic = tapebutton_info[i].graphic;
1905 struct GraphicInfo *gfx = &graphic_info[graphic];
1906 struct XY *pos = tapebutton_info[i].pos;
1907 struct GadgetInfo *gi;
1908 int gd_x = gfx->src_x;
1909 int gd_y = gfx->src_y;
1910 int gd_xp = gfx->src_x + gfx->pressed_xoffset;
1911 int gd_yp = gfx->src_y + gfx->pressed_yoffset;
1914 gi = CreateGadget(GDI_CUSTOM_ID, id,
1915 GDI_IMAGE_ID, graphic,
1916 GDI_INFO_TEXT, tapebutton_info[i].infotext,
1919 GDI_WIDTH, gfx->width,
1920 GDI_HEIGHT, gfx->height,
1921 GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
1922 GDI_STATE, GD_BUTTON_UNPRESSED,
1923 GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
1924 GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
1925 GDI_DIRECT_DRAW, FALSE,
1926 GDI_EVENT_MASK, GD_EVENT_RELEASED,
1927 GDI_CALLBACK_ACTION, HandleTapeButtons,
1931 Fail("cannot create gadget");
1933 tape_gadget[id] = gi;
1937 void FreeTapeButtons(void)
1941 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1942 FreeGadget(tape_gadget[i]);
1945 void MapTapeEjectButton(void)
1947 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1948 MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1951 void MapTapeWarpButton(void)
1953 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1954 MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1957 void MapTapeButtons(void)
1961 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1962 if (i != TAPE_CTRL_ID_EXTRA)
1963 MapGadget(tape_gadget[i]);
1965 if (tape.recording || tape.playing)
1966 MapTapeWarpButton();
1968 if (tape.show_game_buttons)
1969 MapGameButtonsOnTape();
1972 void UnmapTapeButtons(void)
1976 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1977 UnmapGadget(tape_gadget[i]);
1979 if (tape.show_game_buttons)
1980 UnmapGameButtonsOnTape();
1983 void RedrawTapeButtons(void)
1987 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1988 RedrawGadget(tape_gadget[i]);
1990 if (tape.show_game_buttons)
1991 RedrawGameButtonsOnTape();
1994 void RedrawOrRemapTapeButtons(void)
1996 if (tape_gadget[TAPE_CTRL_ID_PLAY]->mapped)
1998 // tape buttons already mapped
1999 RedrawTapeButtons();
2008 static void HandleTapeButtonsExt(int id)
2010 if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
2015 case TAPE_CTRL_ID_EJECT:
2018 if (TAPE_IS_EMPTY(tape))
2022 if (TAPE_IS_EMPTY(tape))
2023 Request("No tape for this level!", REQ_CONFIRM);
2028 SaveTapeChecked(level_nr);
2033 DrawCompleteVideoDisplay();
2036 case TAPE_CTRL_ID_EXTRA:
2039 tape.pause_before_end = !tape.pause_before_end;
2041 DrawVideoDisplayCurrentState();
2043 else if (tape.recording)
2050 case TAPE_CTRL_ID_STOP:
2055 case TAPE_CTRL_ID_PAUSE:
2056 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2060 case TAPE_CTRL_ID_RECORD:
2061 if (TAPE_IS_STOPPED(tape))
2063 TapeStartGameRecording();
2065 else if (tape.pausing)
2067 if (tape.playing) // PLAY -> PAUSE -> RECORD
2068 TapeAppendRecording();
2070 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2075 case TAPE_CTRL_ID_PLAY:
2076 if (tape.recording && tape.pausing) // PAUSE -> RECORD
2078 // ("TAPE_IS_EMPTY(tape)" is TRUE here -- probably fix this)
2080 TapeTogglePause(TAPE_TOGGLE_MANUAL);
2083 if (TAPE_IS_EMPTY(tape))
2086 if (TAPE_IS_STOPPED(tape))
2088 TapeStartGamePlaying();
2090 else if (tape.playing)
2092 if (tape.pausing) // PAUSE -> PLAY
2094 TapeTogglePause(TAPE_TOGGLE_MANUAL | TAPE_TOGGLE_PLAY_PAUSE);
2096 else if (!tape.fast_forward) // PLAY -> FFWD
2098 tape.fast_forward = TRUE;
2100 else if (!tape.warp_forward) // FFWD -> WARP
2102 tape.warp_forward = TRUE;
2104 else if (!tape.deactivate_display) // WARP -> WARP BLIND
2106 tape.deactivate_display = TRUE;
2108 TapeDeactivateDisplayOn();
2110 else // WARP BLIND -> PLAY
2112 tape.fast_forward = FALSE;
2113 tape.warp_forward = FALSE;
2114 tape.deactivate_display = FALSE;
2116 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
2119 DrawVideoDisplayCurrentState();
2124 case TAPE_CTRL_ID_INSERT_SOLUTION:
2125 InsertSolutionTape();
2129 case TAPE_CTRL_ID_PLAY_SOLUTION:
2139 static void HandleTapeButtons(struct GadgetInfo *gi)
2141 HandleTapeButtonsExt(gi->custom_id);
2144 void HandleTapeButtonKeys(Key key)
2146 boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
2147 boolean extra_button_is_active = !eject_button_is_active;
2149 if (key == setup.shortcut.tape_eject && eject_button_is_active)
2150 HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
2151 else if (key == setup.shortcut.tape_extra && extra_button_is_active)
2152 HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
2153 else if (key == setup.shortcut.tape_stop)
2154 HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
2155 else if (key == setup.shortcut.tape_pause)
2156 HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
2157 else if (key == setup.shortcut.tape_record)
2158 HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
2159 else if (key == setup.shortcut.tape_play)
2160 HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);