1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://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
39 // forward declaration for internal use
40 static void HandleTapeButtons(struct GadgetInfo *);
41 static void TapeStopWarpForward(void);
42 static float GetTapeLengthSecondsFloat(void);
44 static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
47 // ============================================================================
48 // video tape definitions
49 // ============================================================================
51 #define VIDEO_INFO_OFF (VIDEO_STATE_DATE_OFF | \
52 VIDEO_STATE_TIME_OFF | \
53 VIDEO_STATE_FRAME_OFF)
54 #define VIDEO_STATE_OFF (VIDEO_STATE_PLAY_OFF | \
55 VIDEO_STATE_REC_OFF | \
56 VIDEO_STATE_PAUSE_OFF | \
57 VIDEO_STATE_FFWD_OFF | \
58 VIDEO_STATE_WARP_OFF | \
59 VIDEO_STATE_WARP2_OFF | \
60 VIDEO_STATE_PBEND_OFF | \
61 VIDEO_STATE_1STEP_OFF)
62 #define VIDEO_PRESS_OFF (VIDEO_PRESS_PLAY_OFF | \
63 VIDEO_PRESS_REC_OFF | \
64 VIDEO_PRESS_PAUSE_OFF | \
65 VIDEO_PRESS_STOP_OFF | \
66 VIDEO_PRESS_EJECT_OFF)
67 #define VIDEO_ALL_OFF (VIDEO_INFO_OFF | \
71 #define VIDEO_INFO_ON (VIDEO_STATE_DATE_ON | \
72 VIDEO_STATE_TIME_ON | \
74 #define VIDEO_STATE_ON (VIDEO_STATE_PLAY_ON | \
75 VIDEO_STATE_REC_ON | \
76 VIDEO_STATE_PAUSE_ON | \
77 VIDEO_STATE_FFWD_ON | \
78 VIDEO_STATE_WARP_ON | \
79 VIDEO_STATE_WARP2_ON | \
80 VIDEO_STATE_PBEND_ON | \
82 #define VIDEO_PRESS_ON (VIDEO_PRESS_PLAY_ON | \
83 VIDEO_PRESS_REC_ON | \
84 VIDEO_PRESS_PAUSE_ON | \
85 VIDEO_PRESS_STOP_ON | \
87 #define VIDEO_ALL_ON (VIDEO_INFO_ON | \
91 #define VIDEO_INFO (VIDEO_INFO_ON | VIDEO_INFO_OFF)
92 #define VIDEO_STATE (VIDEO_STATE_ON | VIDEO_STATE_OFF)
93 #define VIDEO_PRESS (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
94 #define VIDEO_ALL (VIDEO_ALL_ON | VIDEO_ALL_OFF)
96 #define NUM_TAPE_FUNCTIONS 11
97 #define NUM_TAPE_FUNCTION_PARTS 2
98 #define NUM_TAPE_FUNCTION_STATES 2
101 // ============================================================================
102 // video display functions
103 // ============================================================================
105 static void DrawVideoDisplay_Graphics(unsigned int state, unsigned int value)
114 video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
117 { IMG_GFX_TAPE_LABEL_PLAY, &tape.label.play },
118 { IMG_GFX_TAPE_SYMBOL_PLAY, &tape.symbol.play },
121 { IMG_GFX_TAPE_LABEL_RECORD, &tape.label.record },
122 { IMG_GFX_TAPE_SYMBOL_RECORD, &tape.symbol.record },
125 { IMG_GFX_TAPE_LABEL_PAUSE, &tape.label.pause },
126 { IMG_GFX_TAPE_SYMBOL_PAUSE, &tape.symbol.pause },
129 { IMG_GFX_TAPE_LABEL_DATE, &tape.label.date },
133 { IMG_GFX_TAPE_LABEL_TIME, &tape.label.time },
137 // (no label for displaying optional frame)
142 { IMG_GFX_TAPE_LABEL_FAST_FORWARD, &tape.label.fast_forward },
143 { IMG_GFX_TAPE_SYMBOL_FAST_FORWARD, &tape.symbol.fast_forward },
146 { IMG_GFX_TAPE_LABEL_WARP_FORWARD, &tape.label.warp_forward },
147 { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD, &tape.symbol.warp_forward },
150 { IMG_GFX_TAPE_LABEL_WARP_FORWARD_BLIND, &tape.label.warp_forward_blind},
151 { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD_BLIND, &tape.symbol.warp_forward_blind},
154 { IMG_GFX_TAPE_LABEL_PAUSE_BEFORE_END, &tape.label.pause_before_end },
155 { IMG_GFX_TAPE_SYMBOL_PAUSE_BEFORE_END, &tape.symbol.pause_before_end },
158 { IMG_GFX_TAPE_LABEL_SINGLE_STEP, &tape.label.single_step },
159 { IMG_GFX_TAPE_SYMBOL_SINGLE_STEP, &tape.symbol.single_step },
163 for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++) // on or off states
165 for (i = 0; i < NUM_TAPE_FUNCTIONS; i++) // record, play, ...
167 for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++) // label or symbol
169 int graphic = video_pos[i][j].graphic;
170 struct XY *pos = video_pos[i][j].pos;
177 if (state & (1 << (i * 2 + k)))
179 struct GraphicInfo *gfx_bg = &graphic_info[IMG_BACKGROUND_TAPE];
180 struct GraphicInfo *gfx = &graphic_info[graphic];
184 (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
186 if (value == skip_value)
191 gd_bitmap = gfx->bitmap;
197 gd_bitmap = gfx_bg->bitmap;
198 gd_x = gfx_bg->src_x + pos->x;
199 gd_y = gfx_bg->src_y + pos->y;
202 // some tape graphics may be undefined -- only draw if defined
203 if (gd_bitmap != NULL)
204 BlitBitmap(gd_bitmap, drawto, gd_x, gd_y, gfx->width, gfx->height,
205 VX + pos->x, VY + pos->y);
207 redraw_mask |= REDRAW_DOOR_2;
215 #define DATETIME_NONE (0)
217 #define DATETIME_DATE_YYYY (1 << 0)
218 #define DATETIME_DATE_YY (1 << 1)
219 #define DATETIME_DATE_MON (1 << 2)
220 #define DATETIME_DATE_MM (1 << 3)
221 #define DATETIME_DATE_DD (1 << 4)
223 #define DATETIME_TIME_HH (1 << 5)
224 #define DATETIME_TIME_MIN (1 << 6)
225 #define DATETIME_TIME_MM (1 << 7)
226 #define DATETIME_TIME_SS (1 << 8)
228 #define DATETIME_FRAME (1 << 9)
230 #define DATETIME_XOFFSET_1 (1 << 10)
231 #define DATETIME_XOFFSET_2 (1 << 11)
233 #define DATETIME_DATE (DATETIME_DATE_YYYY | \
235 DATETIME_DATE_MON | \
239 #define DATETIME_TIME (DATETIME_TIME_HH | \
240 DATETIME_TIME_MIN | \
244 #define MAX_DATETIME_STRING_SIZE 32
246 static void DrawVideoDisplay_DateTime(unsigned int state, unsigned int value)
250 static char *month_shortnames[] =
268 struct TextPosInfo *pos;
273 { &tape.text.date, DATETIME_DATE_DD },
274 { &tape.text.date, DATETIME_DATE_MON | DATETIME_XOFFSET_1 },
275 { &tape.text.date, DATETIME_DATE_YY | DATETIME_XOFFSET_2 },
276 { &tape.text.date_yyyy, DATETIME_DATE_YYYY },
277 { &tape.text.date_yy, DATETIME_DATE_YY },
278 { &tape.text.date_mon, DATETIME_DATE_MON },
279 { &tape.text.date_mm, DATETIME_DATE_MM },
280 { &tape.text.date_dd, DATETIME_DATE_DD },
282 { &tape.text.time, DATETIME_TIME_MIN },
283 { &tape.text.time, DATETIME_TIME_SS | DATETIME_XOFFSET_1 },
284 { &tape.text.time_hh, DATETIME_TIME_HH },
285 { &tape.text.time_mm, DATETIME_TIME_MM },
286 { &tape.text.time_ss, DATETIME_TIME_SS },
288 { &tape.text.frame, DATETIME_FRAME },
290 { NULL, DATETIME_NONE },
293 for (i = 0; datetime_info[i].pos != NULL; i++)
295 struct TextPosInfo *pos = datetime_info[i].pos;
296 int type = datetime_info[i].type;
303 xpos = VX + pos->x + (type & DATETIME_XOFFSET_1 ? pos->xoffset :
304 type & DATETIME_XOFFSET_2 ? pos->xoffset2 : 0);
307 if ((type & DATETIME_DATE) && (state & VIDEO_STATE_DATE_ON))
309 char s[MAX_DATETIME_STRING_SIZE];
310 int year2 = value / 10000;
311 int year4 = (year2 < 70 ? 2000 + year2 : 1900 + year2);
312 int month_index = (value / 100) % 100;
313 int month = month_index + 1;
314 int day = value % 100;
316 strcpy(s, (type & DATETIME_DATE_YYYY ? int2str(year4, 4) :
317 type & DATETIME_DATE_YY ? int2str(year2, 2) :
318 type & DATETIME_DATE_MON ? month_shortnames[month_index] :
319 type & DATETIME_DATE_MM ? int2str(month, 2) :
320 type & DATETIME_DATE_DD ? int2str(day, 2) : ""));
322 DrawText(xpos, ypos, s, pos->font);
324 else if ((type & DATETIME_TIME) && (state & VIDEO_STATE_TIME_ON))
326 char s[MAX_DATETIME_STRING_SIZE];
327 int hh = (value / 3600) % 100;
328 int min = value / 60;
329 int mm = (value / 60) % 60;
332 strcpy(s, (type & DATETIME_TIME_HH ? int2str(hh, 2) :
333 type & DATETIME_TIME_MIN ? int2str(min, 2) :
334 type & DATETIME_TIME_MM ? int2str(mm, 2) :
335 type & DATETIME_TIME_SS ? int2str(ss, 2) : ""));
337 DrawText(xpos, ypos, s, pos->font);
339 else if ((type & DATETIME_FRAME) && (state & VIDEO_STATE_FRAME_ON))
341 DrawText(xpos, ypos, int2str(value, pos->size), pos->font);
346 void DrawVideoDisplay(unsigned int state, unsigned int value)
348 DrawVideoDisplay_Graphics(state, value);
349 DrawVideoDisplay_DateTime(state, value);
352 static void DrawVideoDisplayLabel(unsigned int state)
354 DrawVideoDisplay(state, VIDEO_DISPLAY_LABEL_ONLY);
357 static void DrawVideoDisplaySymbol(unsigned int state)
359 DrawVideoDisplay(state, VIDEO_DISPLAY_SYMBOL_ONLY);
362 static void DrawVideoDisplayCurrentState(void)
366 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
369 state |= VIDEO_STATE_PAUSE_ON;
373 state |= VIDEO_STATE_REC_ON;
375 if (tape.single_step)
376 state |= VIDEO_STATE_1STEP_ON;
378 else if (tape.playing)
380 state |= VIDEO_STATE_PLAY_ON;
384 if (tape.deactivate_display)
385 state |= VIDEO_STATE_WARP2_ON;
386 else if (tape.warp_forward)
387 state |= VIDEO_STATE_WARP_ON;
388 else if (tape.fast_forward)
389 state |= VIDEO_STATE_FFWD_ON;
391 if (tape.pause_before_end)
392 state |= VIDEO_STATE_PBEND_ON;
396 // draw labels and symbols separately to prevent labels overlapping symbols
397 DrawVideoDisplayLabel(state);
398 DrawVideoDisplaySymbol(state);
401 void DrawCompleteVideoDisplay(void)
403 struct GraphicInfo *g_tape = &graphic_info[IMG_BACKGROUND_TAPE];
405 // draw tape background
406 BlitBitmap(g_tape->bitmap, drawto, g_tape->src_x, g_tape->src_y,
407 gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
409 // draw tape buttons (forced)
410 RedrawOrRemapTapeButtons();
412 DrawVideoDisplay(VIDEO_ALL_OFF, 0);
416 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
417 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
418 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
419 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
422 DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
424 else if (tape.playing)
426 DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
427 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
428 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
429 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
432 DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
434 else if (tape.date && tape.length)
436 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
437 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
438 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
441 BlitBitmap(drawto, bitmap_db_door_2, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
445 void TapeDeactivateDisplayOn(void)
447 SetDrawDeactivationMask(REDRAW_FIELD);
448 audio.sound_deactivated = TRUE;
451 void TapeDeactivateDisplayOff(boolean redraw_display)
453 SetDrawDeactivationMask(REDRAW_NONE);
454 audio.sound_deactivated = FALSE;
459 DrawGameDoorValues();
464 // ============================================================================
465 // tape logging functions
466 // ============================================================================
468 static void PrintTapeReplayProgress(boolean replay_finished)
470 static unsigned int counter_last = -1;
471 unsigned int counter = Counter();
472 unsigned int counter_seconds = counter / 1000;
474 if (!replay_finished)
476 unsigned int counter_delay = 50;
478 if (counter > counter_last + counter_delay)
481 PrintNoLog("Level %03d [%02d:%02d]: [%02d:%02d] - playing tape ... ",
482 level_nr, tape.length_seconds / 60, tape.length_seconds % 60,
483 TapeTime / 60, TapeTime % 60);
485 counter_last = counter;
490 float tape_length_seconds = GetTapeLengthSecondsFloat();
493 Print("Level %03d [%02d:%02d]: (%02d:%02d.%03d / %.2f %%) - %s.\n",
494 level_nr, tape.length_seconds / 60, tape.length_seconds % 60,
495 counter_seconds / 60, counter_seconds % 60, counter % 1000,
496 (float)counter / tape_length_seconds / 10,
497 tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
504 // ============================================================================
505 // tape control functions
506 // ============================================================================
508 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
510 struct tm *lt = localtime(&epoch_seconds);
512 tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
515 void TapeSetDateFromNow(void)
517 TapeSetDateFromEpochSeconds(time(NULL));
526 tape.length_frames = 0;
527 tape.length_seconds = 0;
529 if (leveldir_current)
530 setString(&tape.level_identifier, leveldir_current->identifier);
532 tape.level_nr = level_nr;
533 tape.pos[tape.counter].delay = 0;
536 tape.random_seed = InitRND(level.random_seed);
538 tape.file_version = FILE_VERSION_ACTUAL;
539 tape.game_version = GAME_VERSION_ACTUAL;
540 tape.engine_version = level.game_version;
542 TapeSetDateFromNow();
544 for (i = 0; i < MAX_PLAYERS; i++)
545 tape.player_participates[i] = FALSE;
547 tape.centered_player_nr_next = -1;
548 tape.set_centered_player = FALSE;
550 tape.event_mask = GAME_EVENTS_DEFAULT;
553 static void TapeRewind(void)
556 tape.delay_played = 0;
557 tape.pause_before_end = FALSE;
558 tape.recording = FALSE;
559 tape.playing = FALSE;
560 tape.fast_forward = FALSE;
561 tape.warp_forward = FALSE;
562 tape.deactivate_display = FALSE;
563 tape.auto_play = (global.autoplay_leveldir != NULL);
564 tape.auto_play_level_solved = FALSE;
565 tape.quick_resume = FALSE;
566 tape.single_step = FALSE;
568 tape.centered_player_nr_next = -1;
569 tape.set_centered_player = FALSE;
571 InitRND(tape.random_seed);
574 static void TapeSetRandomSeed(int random_seed)
576 tape.random_seed = InitRND(random_seed);
579 void TapeStartRecording(int random_seed)
581 if (!TAPE_IS_STOPPED(tape))
586 TapeSetRandomSeed(random_seed);
588 tape.recording = TRUE;
590 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
591 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
592 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
593 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
597 SetDrawDeactivationMask(REDRAW_NONE);
598 audio.sound_deactivated = FALSE;
600 // required here to update video display if tape door is closed
601 if (GetDoorState() & DOOR_CLOSE_2)
602 OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
605 static void TapeStartGameRecording(void)
607 StartGameActions(network.enabled, TRUE, level.random_seed);
610 static void TapeAppendRecording(void)
612 if (!tape.playing || !tape.pausing)
616 tape.playing = FALSE;
617 tape.fast_forward = FALSE;
618 tape.warp_forward = FALSE;
619 tape.pause_before_end = FALSE;
620 tape.deactivate_display = FALSE;
623 tape.recording = TRUE;
626 // set current delay (for last played move)
627 tape.pos[tape.counter].delay = tape.delay_played;
630 TapeSetDateFromNow();
632 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
633 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
635 UpdateAndDisplayGameControlValues();
638 void TapeHaltRecording(void)
642 // initialize delay for next tape entry (to be able to continue recording)
643 if (tape.counter < MAX_TAPE_LEN)
644 tape.pos[tape.counter].delay = 0;
646 tape.length = tape.counter;
647 tape.length_frames = GetTapeLengthFrames();
648 tape.length_seconds = GetTapeLengthSeconds();
651 void TapeStopRecording(void)
656 tape.recording = FALSE;
657 tape.pausing = FALSE;
659 DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
660 MapTapeEjectButton();
663 boolean TapeAddAction(byte action[MAX_TAPE_ACTIONS])
667 if (tape.pos[tape.counter].delay > 0) // already stored action
669 boolean changed_events = FALSE;
671 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
672 if (tape.pos[tape.counter].action[i] != action[i])
673 changed_events = TRUE;
675 if (changed_events || tape.pos[tape.counter].delay >= 255)
677 if (tape.counter >= MAX_TAPE_LEN - 1)
681 tape.pos[tape.counter].delay = 0;
684 tape.pos[tape.counter].delay++;
687 if (tape.pos[tape.counter].delay == 0) // store new action
689 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
690 tape.pos[tape.counter].action[i] = action[i];
692 tape.pos[tape.counter].delay++;
698 void TapeRecordAction(byte action_raw[MAX_TAPE_ACTIONS])
700 byte action[MAX_TAPE_ACTIONS];
703 if (!tape.recording) // (record action even when tape is paused)
706 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
707 action[i] = action_raw[i];
709 if (tape.event_mask == GAME_EVENTS_KEYS && tape.set_centered_player)
711 for (i = 0; i < MAX_PLAYERS; i++)
712 if (tape.centered_player_nr_next == i ||
713 tape.centered_player_nr_next == -1)
714 action[i] |= KEY_SET_FOCUS;
716 tape.set_centered_player = FALSE;
719 if (!TapeAddAction(action))
723 void TapeTogglePause(boolean toggle_mode)
725 if (tape.playing && tape.pausing && (toggle_mode & TAPE_TOGGLE_PLAY_PAUSE))
727 // continue playing in normal mode
728 tape.fast_forward = FALSE;
729 tape.warp_forward = FALSE;
730 tape.deactivate_display = FALSE;
732 tape.pause_before_end = FALSE;
735 tape.pausing = !tape.pausing;
737 if (tape.single_step && (toggle_mode & TAPE_TOGGLE_MANUAL))
738 tape.single_step = FALSE;
740 DrawVideoDisplayCurrentState();
742 if (tape.deactivate_display)
745 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
747 TapeDeactivateDisplayOn();
750 if (tape.quick_resume)
752 tape.quick_resume = FALSE;
754 TapeStopWarpForward();
755 TapeAppendRecording();
757 if (!CheckEngineSnapshotSingle())
758 SaveEngineSnapshotSingle();
760 // restart step/move snapshots after quick loading tape
761 SaveEngineSnapshotToListInitial();
763 // do not map undo/redo buttons after quick loading tape
767 if (game_status == GAME_MODE_PLAYING)
769 if (setup.show_snapshot_buttons && CheckEngineSnapshotList())
772 MapUndoRedoButtons();
773 else if (!tape.single_step)
774 UnmapUndoRedoButtons();
777 ModifyPauseButtons();
781 void TapeStartPlaying(void)
783 if (TAPE_IS_EMPTY(tape))
786 if (!TAPE_IS_STOPPED(tape))
793 DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
794 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
795 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
796 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
800 SetDrawDeactivationMask(REDRAW_NONE);
801 audio.sound_deactivated = FALSE;
804 static void TapeStartGamePlaying(void)
811 void TapeStopPlaying(void)
813 tape.playing = FALSE;
814 tape.pausing = FALSE;
816 if (tape.warp_forward)
817 TapeStopWarpForward();
819 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
820 MapTapeEjectButton();
823 byte *TapePlayAction(void)
825 int update_delay = FRAMES_PER_SECOND / 2;
826 boolean update_video_display = (FrameCounter % update_delay == 0);
827 boolean update_draw_label_on = ((FrameCounter / update_delay) % 2 == 1);
828 static byte action[MAX_TAPE_ACTIONS];
831 if (!tape.playing || tape.pausing)
834 if (tape.pause_before_end) // stop some seconds before end of tape
836 if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
838 TapeStopWarpForward();
839 TapeTogglePause(TAPE_TOGGLE_MANUAL);
845 if (tape.counter >= tape.length) // end of tape reached
847 if (tape.warp_forward && !tape.auto_play)
849 TapeStopWarpForward();
850 TapeTogglePause(TAPE_TOGGLE_MANUAL);
860 if (update_video_display && !tape.deactivate_display)
864 if (tape.warp_forward)
865 state |= VIDEO_STATE_WARP(update_draw_label_on);
866 else if (tape.fast_forward)
867 state |= VIDEO_STATE_FFWD(update_draw_label_on);
869 if (tape.pause_before_end)
870 state |= VIDEO_STATE_PBEND(update_draw_label_on);
872 // draw labels and symbols separately to prevent labels overlapping symbols
873 DrawVideoDisplayLabel(state);
874 DrawVideoDisplaySymbol(state);
877 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
878 action[i] = tape.pos[tape.counter].action[i];
880 #if DEBUG_TAPE_WHEN_PLAYING
881 printf("%05d", FrameCounter);
882 for (i = 0; i < MAX_TAPE_ACTIONS; i++)
883 printf(" %08x", action[i]);
887 tape.set_centered_player = FALSE;
888 tape.centered_player_nr_next = -999;
890 if (tape.event_mask == GAME_EVENTS_KEYS)
892 for (i = 0; i < MAX_PLAYERS; i++)
894 if (action[i] & KEY_SET_FOCUS)
896 tape.set_centered_player = TRUE;
897 tape.centered_player_nr_next =
898 (tape.centered_player_nr_next == -999 ? i : -1);
901 action[i] &= ~KEY_SET_FOCUS;
906 if (tape.delay_played >= tape.pos[tape.counter].delay)
909 tape.delay_played = 0;
913 PrintTapeReplayProgress(FALSE);
921 TapeTogglePause(TAPE_TOGGLE_MANUAL);
926 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
928 if (tape.date && tape.length)
930 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
931 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
932 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
936 unsigned int GetTapeLengthFrames(void)
938 unsigned int tape_length_frames = 0;
941 if (TAPE_IS_EMPTY(tape))
944 for (i = 0; i < tape.length; i++)
945 tape_length_frames += tape.pos[i].delay;
947 return tape_length_frames;
950 unsigned int GetTapeLengthSeconds(void)
952 return (GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
955 static float GetTapeLengthSecondsFloat(void)
957 return ((float)GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
960 static void TapeStartWarpForward(int mode)
962 tape.fast_forward = (mode & AUTOPLAY_FFWD);
963 tape.warp_forward = (mode & AUTOPLAY_WARP);
964 tape.deactivate_display = (mode & AUTOPLAY_WARP_NO_DISPLAY);
966 tape.pausing = FALSE;
968 if (tape.deactivate_display)
969 TapeDeactivateDisplayOn();
971 DrawVideoDisplayCurrentState();
974 static void TapeStopWarpForward(void)
976 tape.fast_forward = FALSE;
977 tape.warp_forward = FALSE;
978 tape.deactivate_display = FALSE;
980 tape.pause_before_end = FALSE;
982 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
984 DrawVideoDisplayCurrentState();
987 static void TapeSingleStep(void)
993 TapeTogglePause(TAPE_TOGGLE_MANUAL);
995 tape.single_step = !tape.single_step;
997 DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
1000 void TapeQuickSave(void)
1002 if (game_status == GAME_MODE_MAIN)
1004 Request("No game that can be saved!", REQ_CONFIRM);
1009 if (game_status != GAME_MODE_PLAYING)
1013 TapeHaltRecording(); // prepare tape for saving on-the-fly
1015 if (TAPE_IS_EMPTY(tape))
1017 Request("No tape that can be saved!", REQ_CONFIRM);
1022 if (SaveTapeChecked(tape.level_nr))
1023 SaveEngineSnapshotSingle();
1026 void TapeQuickLoad(void)
1028 char *filename = getTapeFilename(level_nr);
1030 if (!fileExists(filename))
1032 Request("No tape for this level!", REQ_CONFIRM);
1037 if (tape.recording && !Request("Stop recording and load tape?",
1038 REQ_ASK | REQ_STAY_CLOSED))
1040 OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1045 if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
1048 if (CheckEngineSnapshotSingle())
1050 TapeStartGamePlaying();
1052 LoadEngineSnapshotSingle();
1054 DrawCompleteVideoDisplay();
1056 tape.playing = TRUE;
1057 tape.pausing = TRUE;
1059 TapeStopWarpForward();
1060 TapeAppendRecording();
1062 // restart step/move snapshots after quick loading tape
1063 SaveEngineSnapshotToListInitial();
1065 if (FrameCounter > 0)
1074 if (!TAPE_IS_EMPTY(tape))
1076 TapeStartGamePlaying();
1077 TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
1079 tape.quick_resume = TRUE;
1081 else // this should not happen (basically checked above)
1083 int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1085 Request("No tape for this level!", REQ_CONFIRM | reopen_door);
1089 boolean hasSolutionTape(void)
1091 boolean tape_file_exists = fileExists(getSolutionTapeFilename(level_nr));
1092 boolean level_has_tape = (level.game_engine_type == GAME_ENGINE_TYPE_SP &&
1093 level.native_sp_level->demo.is_available);
1095 return (tape_file_exists || level_has_tape);
1098 boolean InsertSolutionTape(void)
1100 if (!hasSolutionTape())
1102 Request("No solution tape for this level!", REQ_CONFIRM);
1107 if (!TAPE_IS_STOPPED(tape))
1110 // if tape recorder already contains a tape, remove it without asking
1113 LoadSolutionTape(level_nr);
1115 DrawCompleteVideoDisplay();
1117 if (TAPE_IS_EMPTY(tape))
1119 Request("Loading solution tape for this level failed!", REQ_CONFIRM);
1127 boolean PlaySolutionTape(void)
1129 if (!InsertSolutionTape())
1132 TapeStartGamePlaying();
1138 // ----------------------------------------------------------------------------
1139 // tape autoplay functions
1140 // ----------------------------------------------------------------------------
1142 void AutoPlayTape(void)
1144 static LevelDirTree *autoplay_leveldir = NULL;
1145 static boolean autoplay_initialized = FALSE;
1146 static int autoplay_level_nr = -1;
1147 static int num_levels_played = 0;
1148 static int num_levels_solved = 0;
1149 static int num_tape_missing = 0;
1150 static boolean level_failed[MAX_TAPES_PER_SET];
1153 if (autoplay_initialized)
1155 // just finished auto-playing tape
1156 PrintTapeReplayProgress(TRUE);
1158 num_levels_played++;
1160 if (tape.auto_play_level_solved)
1161 num_levels_solved++;
1162 else if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1163 level_failed[level_nr] = TRUE;
1167 DrawCompleteVideoDisplay();
1169 audio.sound_enabled = FALSE;
1170 setup.engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_OFF);
1172 autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1173 global.autoplay_leveldir);
1175 if (autoplay_leveldir == NULL)
1176 Error(ERR_EXIT, "no such level identifier: '%s'",
1177 global.autoplay_leveldir);
1179 leveldir_current = autoplay_leveldir;
1181 if (autoplay_leveldir->first_level < 0)
1182 autoplay_leveldir->first_level = 0;
1183 if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
1184 autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1186 autoplay_level_nr = autoplay_leveldir->first_level;
1189 Print("Automatically playing level tapes\n");
1191 Print("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
1192 Print("Level series name: '%s'\n", autoplay_leveldir->name);
1193 Print("Level series author: '%s'\n", autoplay_leveldir->author);
1194 Print("Number of levels: %d\n", autoplay_leveldir->levels);
1198 for (i = 0; i < MAX_TAPES_PER_SET; i++)
1199 level_failed[i] = FALSE;
1201 autoplay_initialized = TRUE;
1204 while (autoplay_level_nr <= autoplay_leveldir->last_level)
1206 level_nr = autoplay_level_nr++;
1208 if (!global.autoplay_all && !global.autoplay_level[level_nr])
1213 LoadLevel(level_nr);
1215 if (level.no_level_file || level.no_valid_file)
1217 Print("Level %03d: (no level)\n", level_nr);
1223 // ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY
1224 Print("Level %03d: (only testing level)\n", level_nr);
1228 if (options.mytapes)
1231 LoadSolutionTape(level_nr);
1233 if (tape.no_valid_file)
1237 Print("Level %03d: (no tape)\n", level_nr);
1244 TapeStartGamePlaying();
1245 TapeStartWarpForward(global.autoplay_mode);
1252 Print("Number of levels played: %d\n", num_levels_played);
1253 Print("Number of levels solved: %d (%d%%)\n", num_levels_solved,
1254 (num_levels_played ? num_levels_solved * 100 / num_levels_played : 0));
1256 Print("Summary (for automatic parsing by scripts):\n");
1257 Print("LEVELDIR [%s] '%s', SOLVED %d/%d (%d%%)",
1258 (num_levels_played == num_levels_solved ? " OK " : "WARN"),
1259 autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
1260 (num_levels_played ? num_levels_solved * 100 / num_levels_played : 0));
1262 if (num_levels_played != num_levels_solved)
1265 for (i = 0; i < MAX_TAPES_PER_SET; i++)
1266 if (level_failed[i])
1277 // ---------- new tape button stuff -------------------------------------------
1285 } tapebutton_info[NUM_TAPE_BUTTONS] =
1288 IMG_GFX_TAPE_BUTTON_EJECT, &tape.button.eject,
1289 TAPE_CTRL_ID_EJECT, "eject tape"
1292 // (same position as "eject" button)
1293 IMG_GFX_TAPE_BUTTON_EXTRA, &tape.button.eject,
1294 TAPE_CTRL_ID_EXTRA, "extra functions"
1297 IMG_GFX_TAPE_BUTTON_STOP, &tape.button.stop,
1298 TAPE_CTRL_ID_STOP, "stop tape"
1301 IMG_GFX_TAPE_BUTTON_PAUSE, &tape.button.pause,
1302 TAPE_CTRL_ID_PAUSE, "pause tape"
1305 IMG_GFX_TAPE_BUTTON_RECORD, &tape.button.record,
1306 TAPE_CTRL_ID_RECORD, "record tape"
1309 IMG_GFX_TAPE_BUTTON_PLAY, &tape.button.play,
1310 TAPE_CTRL_ID_PLAY, "play tape"
1313 IMG_GFX_TAPE_BUTTON_INSERT_SOLUTION,&tape.button.insert_solution,
1314 TAPE_CTRL_ID_INSERT_SOLUTION, "insert solution tape"
1317 IMG_GFX_TAPE_BUTTON_PLAY_SOLUTION, &tape.button.play_solution,
1318 TAPE_CTRL_ID_PLAY_SOLUTION, "play solution tape"
1322 void CreateTapeButtons(void)
1326 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1328 int graphic = tapebutton_info[i].graphic;
1329 struct GraphicInfo *gfx = &graphic_info[graphic];
1330 struct XY *pos = tapebutton_info[i].pos;
1331 struct GadgetInfo *gi;
1332 int gd_x = gfx->src_x;
1333 int gd_y = gfx->src_y;
1334 int gd_xp = gfx->src_x + gfx->pressed_xoffset;
1335 int gd_yp = gfx->src_y + gfx->pressed_yoffset;
1338 gi = CreateGadget(GDI_CUSTOM_ID, id,
1339 GDI_IMAGE_ID, graphic,
1340 GDI_INFO_TEXT, tapebutton_info[i].infotext,
1343 GDI_WIDTH, gfx->width,
1344 GDI_HEIGHT, gfx->height,
1345 GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
1346 GDI_STATE, GD_BUTTON_UNPRESSED,
1347 GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
1348 GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
1349 GDI_DIRECT_DRAW, FALSE,
1350 GDI_EVENT_MASK, GD_EVENT_RELEASED,
1351 GDI_CALLBACK_ACTION, HandleTapeButtons,
1355 Error(ERR_EXIT, "cannot create gadget");
1357 tape_gadget[id] = gi;
1361 void FreeTapeButtons(void)
1365 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1366 FreeGadget(tape_gadget[i]);
1369 void MapTapeEjectButton(void)
1371 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1372 MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1375 void MapTapeWarpButton(void)
1377 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1378 MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1381 void MapTapeButtons(void)
1385 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1386 if (i != TAPE_CTRL_ID_EXTRA)
1387 MapGadget(tape_gadget[i]);
1389 if (tape.recording || tape.playing)
1390 MapTapeWarpButton();
1392 if (tape.show_game_buttons)
1393 MapGameButtonsOnTape();
1396 void UnmapTapeButtons(void)
1400 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1401 UnmapGadget(tape_gadget[i]);
1403 if (tape.show_game_buttons)
1404 UnmapGameButtonsOnTape();
1407 void RedrawTapeButtons(void)
1411 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1412 RedrawGadget(tape_gadget[i]);
1414 if (tape.show_game_buttons)
1415 RedrawGameButtonsOnTape();
1418 void RedrawOrRemapTapeButtons(void)
1420 if (tape_gadget[TAPE_CTRL_ID_PLAY]->mapped)
1422 // tape buttons already mapped
1423 RedrawTapeButtons();
1432 static void HandleTapeButtonsExt(int id)
1434 if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
1439 case TAPE_CTRL_ID_EJECT:
1442 if (TAPE_IS_EMPTY(tape))
1446 if (TAPE_IS_EMPTY(tape))
1447 Request("No tape for this level!", REQ_CONFIRM);
1452 SaveTapeChecked(level_nr);
1457 DrawCompleteVideoDisplay();
1460 case TAPE_CTRL_ID_EXTRA:
1463 tape.pause_before_end = !tape.pause_before_end;
1465 DrawVideoDisplayCurrentState();
1467 else if (tape.recording)
1474 case TAPE_CTRL_ID_STOP:
1479 case TAPE_CTRL_ID_PAUSE:
1480 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1484 case TAPE_CTRL_ID_RECORD:
1485 if (TAPE_IS_STOPPED(tape))
1487 TapeStartGameRecording();
1489 else if (tape.pausing)
1491 if (tape.playing) // PLAY -> PAUSE -> RECORD
1492 TapeAppendRecording();
1494 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1499 case TAPE_CTRL_ID_PLAY:
1500 if (tape.recording && tape.pausing) // PAUSE -> RECORD
1502 // ("TAPE_IS_EMPTY(tape)" is TRUE here -- probably fix this)
1504 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1507 if (TAPE_IS_EMPTY(tape))
1510 if (TAPE_IS_STOPPED(tape))
1512 TapeStartGamePlaying();
1514 else if (tape.playing)
1516 if (tape.pausing) // PAUSE -> PLAY
1518 TapeTogglePause(TAPE_TOGGLE_MANUAL | TAPE_TOGGLE_PLAY_PAUSE);
1520 else if (!tape.fast_forward) // PLAY -> FFWD
1522 tape.fast_forward = TRUE;
1524 else if (!tape.warp_forward) // FFWD -> WARP
1526 tape.warp_forward = TRUE;
1528 else if (!tape.deactivate_display) // WARP -> WARP BLIND
1530 tape.deactivate_display = TRUE;
1532 TapeDeactivateDisplayOn();
1534 else // WARP BLIND -> PLAY
1536 tape.fast_forward = FALSE;
1537 tape.warp_forward = FALSE;
1538 tape.deactivate_display = FALSE;
1540 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
1543 DrawVideoDisplayCurrentState();
1548 case TAPE_CTRL_ID_INSERT_SOLUTION:
1549 InsertSolutionTape();
1553 case TAPE_CTRL_ID_PLAY_SOLUTION:
1563 static void HandleTapeButtons(struct GadgetInfo *gi)
1565 HandleTapeButtonsExt(gi->custom_id);
1568 void HandleTapeButtonKeys(Key key)
1570 boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
1571 boolean extra_button_is_active = !eject_button_is_active;
1573 if (key == setup.shortcut.tape_eject && eject_button_is_active)
1574 HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
1575 else if (key == setup.shortcut.tape_extra && extra_button_is_active)
1576 HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
1577 else if (key == setup.shortcut.tape_stop)
1578 HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
1579 else if (key == setup.shortcut.tape_pause)
1580 HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
1581 else if (key == setup.shortcut.tape_record)
1582 HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
1583 else if (key == setup.shortcut.tape_play)
1584 HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);