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
32 #define NUM_TAPE_BUTTONS 6
34 /* values for tape handling */
35 #define TAPE_PAUSE_SECONDS_BEFORE_DEATH 5
37 /* forward declaration for internal use */
38 static void HandleTapeButtons(struct GadgetInfo *);
39 static void TapeStopWarpForward();
40 static float GetTapeLengthSecondsFloat();
42 static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
45 /* ========================================================================= */
46 /* video tape definitions */
47 /* ========================================================================= */
49 #define VIDEO_INFO_OFF (VIDEO_STATE_DATE_OFF | \
50 VIDEO_STATE_TIME_OFF | \
51 VIDEO_STATE_FRAME_OFF)
52 #define VIDEO_STATE_OFF (VIDEO_STATE_PLAY_OFF | \
53 VIDEO_STATE_REC_OFF | \
54 VIDEO_STATE_PAUSE_OFF | \
55 VIDEO_STATE_FFWD_OFF | \
56 VIDEO_STATE_WARP_OFF | \
57 VIDEO_STATE_WARP2_OFF | \
58 VIDEO_STATE_PBEND_OFF | \
59 VIDEO_STATE_1STEP_OFF)
60 #define VIDEO_PRESS_OFF (VIDEO_PRESS_PLAY_OFF | \
61 VIDEO_PRESS_REC_OFF | \
62 VIDEO_PRESS_PAUSE_OFF | \
63 VIDEO_PRESS_STOP_OFF | \
64 VIDEO_PRESS_EJECT_OFF)
65 #define VIDEO_ALL_OFF (VIDEO_INFO_OFF | \
69 #define VIDEO_INFO_ON (VIDEO_STATE_DATE_ON | \
70 VIDEO_STATE_TIME_ON | \
72 #define VIDEO_STATE_ON (VIDEO_STATE_PLAY_ON | \
73 VIDEO_STATE_REC_ON | \
74 VIDEO_STATE_PAUSE_ON | \
75 VIDEO_STATE_FFWD_ON | \
76 VIDEO_STATE_WARP_ON | \
77 VIDEO_STATE_WARP2_ON | \
78 VIDEO_STATE_PBEND_ON | \
80 #define VIDEO_PRESS_ON (VIDEO_PRESS_PLAY_ON | \
81 VIDEO_PRESS_REC_ON | \
82 VIDEO_PRESS_PAUSE_ON | \
83 VIDEO_PRESS_STOP_ON | \
85 #define VIDEO_ALL_ON (VIDEO_INFO_ON | \
89 #define VIDEO_INFO (VIDEO_INFO_ON | VIDEO_INFO_OFF)
90 #define VIDEO_STATE (VIDEO_STATE_ON | VIDEO_STATE_OFF)
91 #define VIDEO_PRESS (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
92 #define VIDEO_ALL (VIDEO_ALL_ON | VIDEO_ALL_OFF)
94 #define NUM_TAPE_FUNCTIONS 11
95 #define NUM_TAPE_FUNCTION_PARTS 2
96 #define NUM_TAPE_FUNCTION_STATES 2
99 /* ========================================================================= */
100 /* video display functions */
101 /* ========================================================================= */
103 static void DrawVideoDisplay_Graphics(unsigned int state, unsigned int value)
112 video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
115 { IMG_GFX_TAPE_LABEL_PLAY, &tape.label.play },
116 { IMG_GFX_TAPE_SYMBOL_PLAY, &tape.symbol.play },
119 { IMG_GFX_TAPE_LABEL_RECORD, &tape.label.record },
120 { IMG_GFX_TAPE_SYMBOL_RECORD, &tape.symbol.record },
123 { IMG_GFX_TAPE_LABEL_PAUSE, &tape.label.pause },
124 { IMG_GFX_TAPE_SYMBOL_PAUSE, &tape.symbol.pause },
127 { IMG_GFX_TAPE_LABEL_DATE, &tape.label.date },
131 { IMG_GFX_TAPE_LABEL_TIME, &tape.label.time },
135 /* (no label for displaying optional frame) */
140 { IMG_GFX_TAPE_LABEL_FAST_FORWARD, &tape.label.fast_forward },
141 { IMG_GFX_TAPE_SYMBOL_FAST_FORWARD, &tape.symbol.fast_forward },
144 { IMG_GFX_TAPE_LABEL_WARP_FORWARD, &tape.label.warp_forward },
145 { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD, &tape.symbol.warp_forward },
148 { IMG_GFX_TAPE_LABEL_WARP_FORWARD_BLIND, &tape.label.warp_forward_blind},
149 { IMG_GFX_TAPE_SYMBOL_WARP_FORWARD_BLIND, &tape.symbol.warp_forward_blind},
152 { IMG_GFX_TAPE_LABEL_PAUSE_BEFORE_END, &tape.label.pause_before_end },
153 { IMG_GFX_TAPE_SYMBOL_PAUSE_BEFORE_END, &tape.symbol.pause_before_end },
156 { IMG_GFX_TAPE_LABEL_SINGLE_STEP, &tape.label.single_step },
157 { IMG_GFX_TAPE_SYMBOL_SINGLE_STEP, &tape.symbol.single_step },
161 for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++) /* on or off states */
163 for (i = 0; i < NUM_TAPE_FUNCTIONS; i++) /* record, play, ... */
165 for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++) /* label or symbol */
167 int graphic = video_pos[i][j].graphic;
168 struct XY *pos = video_pos[i][j].pos;
175 if (state & (1 << (i * 2 + k)))
177 struct GraphicInfo *gfx_bg = &graphic_info[IMG_BACKGROUND_TAPE];
178 struct GraphicInfo *gfx = &graphic_info[graphic];
182 (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
184 if (value == skip_value)
189 gd_bitmap = gfx->bitmap;
195 gd_bitmap = gfx_bg->bitmap;
196 gd_x = gfx_bg->src_x + pos->x;
197 gd_y = gfx_bg->src_y + pos->y;
200 /* some tape graphics may be undefined -- only draw if defined */
201 if (gd_bitmap != NULL)
202 BlitBitmap(gd_bitmap, drawto, gd_x, gd_y, gfx->width, gfx->height,
203 VX + pos->x, VY + pos->y);
205 redraw_mask |= REDRAW_DOOR_2;
213 #define DATETIME_NONE (0)
215 #define DATETIME_DATE_YYYY (1 << 0)
216 #define DATETIME_DATE_YY (1 << 1)
217 #define DATETIME_DATE_MON (1 << 2)
218 #define DATETIME_DATE_MM (1 << 3)
219 #define DATETIME_DATE_DD (1 << 4)
221 #define DATETIME_TIME_HH (1 << 5)
222 #define DATETIME_TIME_MIN (1 << 6)
223 #define DATETIME_TIME_MM (1 << 7)
224 #define DATETIME_TIME_SS (1 << 8)
226 #define DATETIME_FRAME (1 << 9)
228 #define DATETIME_XOFFSET_1 (1 << 10)
229 #define DATETIME_XOFFSET_2 (1 << 11)
231 #define DATETIME_DATE (DATETIME_DATE_YYYY | \
233 DATETIME_DATE_MON | \
237 #define DATETIME_TIME (DATETIME_TIME_HH | \
238 DATETIME_TIME_MIN | \
242 #define MAX_DATETIME_STRING_SIZE 32
244 static void DrawVideoDisplay_DateTime(unsigned int state, unsigned int value)
248 static char *month_shortnames[] =
266 struct TextPosInfo *pos;
271 { &tape.text.date, DATETIME_DATE_DD },
272 { &tape.text.date, DATETIME_DATE_MON | DATETIME_XOFFSET_1 },
273 { &tape.text.date, DATETIME_DATE_YY | DATETIME_XOFFSET_2 },
274 { &tape.text.date_yyyy, DATETIME_DATE_YYYY },
275 { &tape.text.date_yy, DATETIME_DATE_YY },
276 { &tape.text.date_mon, DATETIME_DATE_MON },
277 { &tape.text.date_mm, DATETIME_DATE_MM },
278 { &tape.text.date_dd, DATETIME_DATE_DD },
280 { &tape.text.time, DATETIME_TIME_MIN },
281 { &tape.text.time, DATETIME_TIME_SS | DATETIME_XOFFSET_1 },
282 { &tape.text.time_hh, DATETIME_TIME_HH },
283 { &tape.text.time_mm, DATETIME_TIME_MM },
284 { &tape.text.time_ss, DATETIME_TIME_SS },
286 { &tape.text.frame, DATETIME_FRAME },
288 { NULL, DATETIME_NONE },
291 for (i = 0; datetime_info[i].pos != NULL; i++)
293 struct TextPosInfo *pos = datetime_info[i].pos;
294 int type = datetime_info[i].type;
301 xpos = VX + pos->x + (type & DATETIME_XOFFSET_1 ? pos->xoffset :
302 type & DATETIME_XOFFSET_2 ? pos->xoffset2 : 0);
305 if ((type & DATETIME_DATE) && (state & VIDEO_STATE_DATE_ON))
307 char s[MAX_DATETIME_STRING_SIZE];
308 int year2 = value / 10000;
309 int year4 = (year2 < 70 ? 2000 + year2 : 1900 + year2);
310 int month_index = (value / 100) % 100;
311 int month = month_index + 1;
312 int day = value % 100;
314 strcpy(s, (type & DATETIME_DATE_YYYY ? int2str(year4, 4) :
315 type & DATETIME_DATE_YY ? int2str(year2, 2) :
316 type & DATETIME_DATE_MON ? month_shortnames[month_index] :
317 type & DATETIME_DATE_MM ? int2str(month, 2) :
318 type & DATETIME_DATE_DD ? int2str(day, 2) : ""));
320 DrawText(xpos, ypos, s, pos->font);
322 else if ((type & DATETIME_TIME) && (state & VIDEO_STATE_TIME_ON))
324 char s[MAX_DATETIME_STRING_SIZE];
325 int hh = (value / 3600) % 100;
326 int min = value / 60;
327 int mm = (value / 60) % 60;
330 strcpy(s, (type & DATETIME_TIME_HH ? int2str(hh, 2) :
331 type & DATETIME_TIME_MIN ? int2str(min, 2) :
332 type & DATETIME_TIME_MM ? int2str(mm, 2) :
333 type & DATETIME_TIME_SS ? int2str(ss, 2) : ""));
335 DrawText(xpos, ypos, s, pos->font);
337 else if ((type & DATETIME_FRAME) && (state & VIDEO_STATE_FRAME_ON))
339 DrawText(xpos, ypos, int2str(value, pos->size), pos->font);
344 void DrawVideoDisplay(unsigned int state, unsigned int value)
346 DrawVideoDisplay_Graphics(state, value);
347 DrawVideoDisplay_DateTime(state, value);
350 void DrawVideoDisplayLabel(unsigned int state)
352 DrawVideoDisplay(state, VIDEO_DISPLAY_LABEL_ONLY);
355 void DrawVideoDisplaySymbol(unsigned int state)
357 DrawVideoDisplay(state, VIDEO_DISPLAY_SYMBOL_ONLY);
360 void DrawVideoDisplayCurrentState()
364 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
367 state |= VIDEO_STATE_PAUSE_ON;
371 state |= VIDEO_STATE_REC_ON;
373 if (tape.single_step)
374 state |= VIDEO_STATE_1STEP_ON;
376 else if (tape.playing)
378 state |= VIDEO_STATE_PLAY_ON;
382 if (tape.deactivate_display)
383 state |= VIDEO_STATE_WARP2_ON;
384 else if (tape.warp_forward)
385 state |= VIDEO_STATE_WARP_ON;
386 else if (tape.fast_forward)
387 state |= VIDEO_STATE_FFWD_ON;
389 if (tape.pause_before_end)
390 state |= VIDEO_STATE_PBEND_ON;
394 // draw labels and symbols separately to prevent labels overlapping symbols
395 DrawVideoDisplayLabel(state);
396 DrawVideoDisplaySymbol(state);
399 void DrawCompleteVideoDisplay()
401 struct GraphicInfo *g_tape = &graphic_info[IMG_BACKGROUND_TAPE];
403 /* draw tape background */
404 BlitBitmap(g_tape->bitmap, drawto, g_tape->src_x, g_tape->src_y,
405 gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
407 /* draw tape buttons (forced) */
408 RedrawOrRemapTapeButtons();
410 DrawVideoDisplay(VIDEO_ALL_OFF, 0);
414 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
415 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
416 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
417 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
420 DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
422 else if (tape.playing)
424 DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
425 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
426 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
427 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
430 DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
432 else if (tape.date && tape.length)
434 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
435 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
436 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
439 BlitBitmap(drawto, bitmap_db_door_2, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
443 void TapeDeactivateDisplayOn()
445 SetDrawDeactivationMask(REDRAW_FIELD);
446 audio.sound_deactivated = TRUE;
449 void TapeDeactivateDisplayOff(boolean redraw_display)
451 SetDrawDeactivationMask(REDRAW_NONE);
452 audio.sound_deactivated = FALSE;
457 DrawGameDoorValues();
462 /* ========================================================================= */
463 /* tape logging functions */
464 /* ========================================================================= */
466 void PrintTapeReplayProgress(boolean replay_finished)
468 static unsigned int counter_last = -1;
469 unsigned int counter = Counter();
470 unsigned int counter_seconds = counter / 1000;
472 if (!replay_finished)
474 unsigned int counter_delay = 50;
476 if (counter > counter_last + counter_delay)
479 PrintNoLog("Level %03d [%02d:%02d]: [%02d:%02d] - playing tape ... ",
480 level_nr, tape.length_seconds / 60, tape.length_seconds % 60,
481 TapeTime / 60, TapeTime % 60);
483 counter_last = counter;
488 float tape_length_seconds = GetTapeLengthSecondsFloat();
491 Print("Level %03d [%02d:%02d]: (%02d:%02d.%03d / %.2f %%) - %s.\n",
492 level_nr, tape.length_seconds / 60, tape.length_seconds % 60,
493 counter_seconds / 60, counter_seconds % 60, counter % 1000,
494 (float)counter / tape_length_seconds / 10,
495 tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
502 /* ========================================================================= */
503 /* tape control functions */
504 /* ========================================================================= */
506 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
508 struct tm *lt = localtime(&epoch_seconds);
510 tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
513 void TapeSetDateFromNow()
515 TapeSetDateFromEpochSeconds(time(NULL));
524 tape.length_frames = 0;
525 tape.length_seconds = 0;
527 if (leveldir_current)
528 setString(&tape.level_identifier, leveldir_current->identifier);
530 tape.level_nr = level_nr;
531 tape.pos[tape.counter].delay = 0;
534 tape.random_seed = InitRND(level.random_seed);
536 tape.file_version = FILE_VERSION_ACTUAL;
537 tape.game_version = GAME_VERSION_ACTUAL;
538 tape.engine_version = level.game_version;
540 TapeSetDateFromNow();
542 for (i = 0; i < MAX_PLAYERS; i++)
543 tape.player_participates[i] = FALSE;
545 tape.centered_player_nr_next = -1;
546 tape.set_centered_player = FALSE;
549 static void TapeRewind()
552 tape.delay_played = 0;
553 tape.pause_before_end = FALSE;
554 tape.recording = FALSE;
555 tape.playing = FALSE;
556 tape.fast_forward = FALSE;
557 tape.warp_forward = FALSE;
558 tape.deactivate_display = FALSE;
559 tape.auto_play = (global.autoplay_leveldir != NULL);
560 tape.auto_play_level_solved = FALSE;
561 tape.quick_resume = FALSE;
562 tape.single_step = FALSE;
564 tape.centered_player_nr_next = -1;
565 tape.set_centered_player = FALSE;
567 InitRND(tape.random_seed);
570 static void TapeSetRandomSeed(int random_seed)
572 tape.random_seed = InitRND(random_seed);
575 void TapeStartRecording(int random_seed)
577 if (!TAPE_IS_STOPPED(tape))
582 TapeSetRandomSeed(random_seed);
584 tape.recording = TRUE;
586 DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
587 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
588 DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
589 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, 0);
593 SetDrawDeactivationMask(REDRAW_NONE);
594 audio.sound_deactivated = FALSE;
597 static void TapeStartGameRecording()
599 TapeStartRecording(level.random_seed);
601 #if defined(NETWORK_AVALIABLE)
604 SendToServer_StartPlaying();
613 static void TapeAppendRecording()
615 if (!tape.playing || !tape.pausing)
619 tape.playing = FALSE;
620 tape.fast_forward = FALSE;
621 tape.warp_forward = FALSE;
622 tape.pause_before_end = FALSE;
623 tape.deactivate_display = FALSE;
626 tape.recording = TRUE;
629 // set current delay (for last played move)
630 tape.pos[tape.counter].delay = tape.delay_played;
633 TapeSetDateFromNow();
635 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
636 DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
638 UpdateAndDisplayGameControlValues();
641 void TapeHaltRecording()
645 // initialize delay for next tape entry (to be able to continue recording)
646 if (tape.counter < MAX_TAPE_LEN)
647 tape.pos[tape.counter].delay = 0;
649 tape.length = tape.counter;
650 tape.length_frames = GetTapeLengthFrames();
651 tape.length_seconds = GetTapeLengthSeconds();
654 void TapeStopRecording()
659 tape.recording = FALSE;
660 tape.pausing = FALSE;
662 DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
663 MapTapeEjectButton();
666 boolean TapeAddAction(byte action[MAX_PLAYERS])
670 if (tape.pos[tape.counter].delay > 0) /* already stored action */
672 boolean changed_events = FALSE;
674 for (i = 0; i < MAX_PLAYERS; i++)
675 if (tape.pos[tape.counter].action[i] != action[i])
676 changed_events = TRUE;
678 if (changed_events || tape.pos[tape.counter].delay >= 255)
680 if (tape.counter >= MAX_TAPE_LEN - 1)
684 tape.pos[tape.counter].delay = 0;
687 tape.pos[tape.counter].delay++;
690 if (tape.pos[tape.counter].delay == 0) /* store new action */
692 for (i = 0; i < MAX_PLAYERS; i++)
693 tape.pos[tape.counter].action[i] = action[i];
695 tape.pos[tape.counter].delay++;
701 void TapeRecordAction(byte action_raw[MAX_PLAYERS])
703 byte action[MAX_PLAYERS];
706 if (!tape.recording) /* (record action even when tape is paused) */
709 for (i = 0; i < MAX_PLAYERS; i++)
710 action[i] = action_raw[i];
712 if (tape.set_centered_player)
714 for (i = 0; i < MAX_PLAYERS; i++)
715 if (tape.centered_player_nr_next == i ||
716 tape.centered_player_nr_next == -1)
717 action[i] |= KEY_SET_FOCUS;
719 tape.set_centered_player = FALSE;
722 if (!TapeAddAction(action))
726 void TapeTogglePause(boolean toggle_mode)
728 if (tape.playing && tape.pausing && (toggle_mode & TAPE_TOGGLE_PLAY_PAUSE))
730 // continue playing in normal mode
731 tape.fast_forward = FALSE;
732 tape.warp_forward = FALSE;
733 tape.deactivate_display = FALSE;
735 tape.pause_before_end = FALSE;
738 tape.pausing = !tape.pausing;
740 if (tape.single_step && (toggle_mode & TAPE_TOGGLE_MANUAL))
741 tape.single_step = FALSE;
743 DrawVideoDisplayCurrentState();
745 if (tape.deactivate_display)
748 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
750 TapeDeactivateDisplayOn();
753 if (tape.quick_resume)
755 tape.quick_resume = FALSE;
757 TapeStopWarpForward();
758 TapeAppendRecording();
760 if (!CheckEngineSnapshotSingle())
761 SaveEngineSnapshotSingle();
763 // restart step/move snapshots after quick loading tape
764 SaveEngineSnapshotToListInitial();
766 // do not map undo/redo buttons after quick loading tape
770 if (setup.show_snapshot_buttons &&
771 game_status == GAME_MODE_PLAYING &&
772 CheckEngineSnapshotList())
775 MapUndoRedoButtons();
776 else if (!tape.single_step)
777 UnmapUndoRedoButtons();
781 void TapeStartPlaying()
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()
811 void TapeStopPlaying()
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()
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_PLAYERS];
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_PLAYERS; 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_PLAYERS; i++)
883 printf(" %08x", action[i]);
887 tape.set_centered_player = FALSE;
888 tape.centered_player_nr_next = -999;
890 for (i = 0; i < MAX_PLAYERS; i++)
892 if (action[i] & KEY_SET_FOCUS)
894 tape.set_centered_player = TRUE;
895 tape.centered_player_nr_next =
896 (tape.centered_player_nr_next == -999 ? i : -1);
899 action[i] &= ~KEY_SET_FOCUS;
903 if (tape.delay_played >= tape.pos[tape.counter].delay)
906 tape.delay_played = 0;
910 PrintTapeReplayProgress(FALSE);
920 DrawVideoDisplay(VIDEO_STATE_OFF, 0);
922 if (tape.date && tape.length)
924 DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
925 DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
926 DrawVideoDisplay(VIDEO_STATE_FRAME_ON, tape.length_frames);
930 unsigned int GetTapeLengthFrames()
932 unsigned int tape_length_frames = 0;
935 if (TAPE_IS_EMPTY(tape))
938 for (i = 0; i < tape.length; i++)
939 tape_length_frames += tape.pos[i].delay;
941 return tape_length_frames;
944 unsigned int GetTapeLengthSeconds()
946 return (GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
949 static float GetTapeLengthSecondsFloat()
951 return ((float)GetTapeLengthFrames() * GAME_FRAME_DELAY / 1000);
954 static void TapeStartWarpForward(int mode)
956 tape.fast_forward = (mode & AUTOPLAY_FFWD);
957 tape.warp_forward = (mode & AUTOPLAY_WARP);
958 tape.deactivate_display = (mode & AUTOPLAY_WARP_NO_DISPLAY);
960 tape.pausing = FALSE;
962 if (tape.deactivate_display)
963 TapeDeactivateDisplayOn();
965 DrawVideoDisplayCurrentState();
968 static void TapeStopWarpForward()
970 tape.fast_forward = FALSE;
971 tape.warp_forward = FALSE;
972 tape.deactivate_display = FALSE;
974 tape.pause_before_end = FALSE;
976 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
978 DrawVideoDisplayCurrentState();
981 static void TapeSingleStep()
987 TapeTogglePause(TAPE_TOGGLE_MANUAL);
989 tape.single_step = !tape.single_step;
991 DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
996 if (game_status == GAME_MODE_MAIN)
998 Request("No game that can be saved!", REQ_CONFIRM);
1003 if (game_status != GAME_MODE_PLAYING)
1007 TapeHaltRecording(); /* prepare tape for saving on-the-fly */
1009 if (TAPE_IS_EMPTY(tape))
1011 Request("No tape that can be saved!", REQ_CONFIRM);
1016 if (SaveTapeChecked(tape.level_nr))
1017 SaveEngineSnapshotSingle();
1020 void TapeQuickLoad()
1022 char *filename = getTapeFilename(level_nr);
1024 if (!fileExists(filename))
1026 Request("No tape for this level!", REQ_CONFIRM);
1031 if (tape.recording && !Request("Stop recording and load tape?",
1032 REQ_ASK | REQ_STAY_CLOSED))
1034 OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
1039 if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
1042 if (CheckEngineSnapshotSingle())
1044 TapeStartGamePlaying();
1046 LoadEngineSnapshotSingle();
1048 DrawCompleteVideoDisplay();
1050 tape.playing = TRUE;
1051 tape.pausing = TRUE;
1053 TapeStopWarpForward();
1054 TapeAppendRecording();
1056 // restart step/move snapshots after quick loading tape
1057 SaveEngineSnapshotToListInitial();
1059 if (FrameCounter > 0)
1068 if (!TAPE_IS_EMPTY(tape))
1070 TapeStartGamePlaying();
1071 TapeStartWarpForward(AUTOPLAY_MODE_WARP_NO_DISPLAY);
1073 tape.quick_resume = TRUE;
1075 else /* this should not happen (basically checked above) */
1077 int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1079 Request("No tape for this level!", REQ_CONFIRM | reopen_door);
1083 void InsertSolutionTape()
1085 boolean level_has_tape = (level.game_engine_type == GAME_ENGINE_TYPE_SP &&
1086 level.native_sp_level->demo.is_available);
1088 if (!fileExists(getSolutionTapeFilename(level_nr)) && !level_has_tape)
1090 Request("No solution tape for this level!", REQ_CONFIRM);
1095 // if tape recorder already contains a tape, remove it without asking
1098 LoadSolutionTape(level_nr);
1100 if (TAPE_IS_EMPTY(tape))
1101 Request("Loading solution tape for this level failed!", REQ_CONFIRM);
1103 DrawCompleteVideoDisplay();
1107 /* ------------------------------------------------------------------------- *
1108 * tape autoplay functions
1109 * ------------------------------------------------------------------------- */
1113 static LevelDirTree *autoplay_leveldir = NULL;
1114 static boolean autoplay_initialized = FALSE;
1115 static int autoplay_level_nr = -1;
1116 static int num_levels_played = 0;
1117 static int num_levels_solved = 0;
1118 static int num_tape_missing = 0;
1119 static boolean level_failed[MAX_TAPES_PER_SET];
1122 if (autoplay_initialized)
1124 /* just finished auto-playing tape */
1125 PrintTapeReplayProgress(TRUE);
1127 num_levels_played++;
1129 if (tape.auto_play_level_solved)
1130 num_levels_solved++;
1131 else if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1132 level_failed[level_nr] = TRUE;
1136 DrawCompleteVideoDisplay();
1138 audio.sound_enabled = FALSE;
1139 setup.engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_OFF);
1141 autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1142 global.autoplay_leveldir);
1144 if (autoplay_leveldir == NULL)
1145 Error(ERR_EXIT, "no such level identifier: '%s'",
1146 global.autoplay_leveldir);
1148 leveldir_current = autoplay_leveldir;
1150 if (autoplay_leveldir->first_level < 0)
1151 autoplay_leveldir->first_level = 0;
1152 if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
1153 autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1155 autoplay_level_nr = autoplay_leveldir->first_level;
1158 Print("Automatically playing level tapes\n");
1160 Print("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
1161 Print("Level series name: '%s'\n", autoplay_leveldir->name);
1162 Print("Level series author: '%s'\n", autoplay_leveldir->author);
1163 Print("Number of levels: %d\n", autoplay_leveldir->levels);
1167 for (i = 0; i < MAX_TAPES_PER_SET; i++)
1168 level_failed[i] = FALSE;
1170 autoplay_initialized = TRUE;
1173 while (autoplay_level_nr <= autoplay_leveldir->last_level)
1175 level_nr = autoplay_level_nr++;
1177 if (!global.autoplay_all && !global.autoplay_level[level_nr])
1182 LoadLevel(level_nr);
1184 if (level.no_level_file || level.no_valid_file)
1186 Print("Level %03d: (no level)\n", level_nr);
1192 /* ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY */
1193 Print("Level %03d: (only testing level)\n", level_nr);
1197 if (options.mytapes)
1200 LoadSolutionTape(level_nr);
1202 if (tape.no_valid_file)
1206 Print("Level %03d: (no tape)\n", level_nr);
1213 TapeStartGamePlaying();
1214 TapeStartWarpForward(global.autoplay_mode);
1221 Print("Number of levels played: %d\n", num_levels_played);
1222 Print("Number of levels solved: %d (%d%%)\n", num_levels_solved,
1223 (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1225 Print("Summary (for automatic parsing by scripts):\n");
1226 Print("LEVELDIR '%s', SOLVED %d/%d (%d%%)",
1227 autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
1228 (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1230 if (num_levels_played != num_levels_solved)
1233 for (i = 0; i < MAX_TAPES_PER_SET; i++)
1234 if (level_failed[i])
1245 /* ---------- new tape button stuff ---------------------------------------- */
1253 } tapebutton_info[NUM_TAPE_BUTTONS] =
1256 IMG_GFX_TAPE_BUTTON_EJECT, &tape.button.eject,
1257 TAPE_CTRL_ID_EJECT, "eject tape"
1260 /* (same position as "eject" button) */
1261 IMG_GFX_TAPE_BUTTON_EXTRA, &tape.button.eject,
1262 TAPE_CTRL_ID_EXTRA, "extra functions"
1265 IMG_GFX_TAPE_BUTTON_STOP, &tape.button.stop,
1266 TAPE_CTRL_ID_STOP, "stop tape"
1269 IMG_GFX_TAPE_BUTTON_PAUSE, &tape.button.pause,
1270 TAPE_CTRL_ID_PAUSE, "pause tape"
1273 IMG_GFX_TAPE_BUTTON_RECORD, &tape.button.record,
1274 TAPE_CTRL_ID_RECORD, "record tape"
1277 IMG_GFX_TAPE_BUTTON_PLAY, &tape.button.play,
1278 TAPE_CTRL_ID_PLAY, "play tape"
1282 void CreateTapeButtons()
1286 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1288 struct GraphicInfo *gfx = &graphic_info[tapebutton_info[i].graphic];
1289 struct XY *pos = tapebutton_info[i].pos;
1290 struct GadgetInfo *gi;
1291 int gd_x = gfx->src_x;
1292 int gd_y = gfx->src_y;
1293 int gd_xp = gfx->src_x + gfx->pressed_xoffset;
1294 int gd_yp = gfx->src_y + gfx->pressed_yoffset;
1297 gi = CreateGadget(GDI_CUSTOM_ID, id,
1298 GDI_INFO_TEXT, tapebutton_info[i].infotext,
1301 GDI_WIDTH, gfx->width,
1302 GDI_HEIGHT, gfx->height,
1303 GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
1304 GDI_STATE, GD_BUTTON_UNPRESSED,
1305 GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
1306 GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
1307 GDI_DIRECT_DRAW, FALSE,
1308 GDI_EVENT_MASK, GD_EVENT_RELEASED,
1309 GDI_CALLBACK_ACTION, HandleTapeButtons,
1313 Error(ERR_EXIT, "cannot create gadget");
1315 tape_gadget[id] = gi;
1319 void FreeTapeButtons()
1323 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1324 FreeGadget(tape_gadget[i]);
1327 void MapTapeEjectButton()
1329 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1330 MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1333 void MapTapeWarpButton()
1335 UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1336 MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1339 void MapTapeButtons()
1343 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1344 if (i != TAPE_CTRL_ID_EXTRA)
1345 MapGadget(tape_gadget[i]);
1347 if (tape.recording || tape.playing)
1348 MapTapeWarpButton();
1350 if (tape.show_game_buttons)
1354 void UnmapTapeButtons()
1358 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1359 UnmapGadget(tape_gadget[i]);
1361 if (tape.show_game_buttons)
1365 void RedrawTapeButtons()
1369 for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1370 RedrawGadget(tape_gadget[i]);
1372 if (tape.show_game_buttons)
1373 RedrawGameButtons();
1375 // RedrawGadget() may have set REDRAW_ALL if buttons are defined off-area
1376 redraw_mask &= ~REDRAW_ALL;
1379 void RedrawOrRemapTapeButtons()
1381 if (tape_gadget[TAPE_CTRL_ID_PLAY]->mapped)
1383 // tape buttons already mapped
1384 RedrawTapeButtons();
1393 static void HandleTapeButtonsExt(int id)
1395 if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
1400 case TAPE_CTRL_ID_EJECT:
1403 if (TAPE_IS_EMPTY(tape))
1407 if (TAPE_IS_EMPTY(tape))
1408 Request("No tape for this level!", REQ_CONFIRM);
1413 SaveTapeChecked(level_nr);
1418 DrawCompleteVideoDisplay();
1421 case TAPE_CTRL_ID_EXTRA:
1424 tape.pause_before_end = !tape.pause_before_end;
1426 DrawVideoDisplayCurrentState();
1428 else if (tape.recording)
1435 case TAPE_CTRL_ID_STOP:
1440 case TAPE_CTRL_ID_PAUSE:
1441 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1445 case TAPE_CTRL_ID_RECORD:
1446 if (TAPE_IS_STOPPED(tape))
1448 TapeStartGameRecording();
1450 else if (tape.pausing)
1452 if (tape.playing) /* PLAY -> PAUSE -> RECORD */
1453 TapeAppendRecording();
1455 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1460 case TAPE_CTRL_ID_PLAY:
1461 if (tape.recording && tape.pausing) /* PAUSE -> RECORD */
1463 // ("TAPE_IS_EMPTY(tape)" is TRUE here -- probably fix this)
1465 TapeTogglePause(TAPE_TOGGLE_MANUAL);
1468 if (TAPE_IS_EMPTY(tape))
1471 if (TAPE_IS_STOPPED(tape))
1473 TapeStartGamePlaying();
1475 else if (tape.playing)
1477 if (tape.pausing) /* PAUSE -> PLAY */
1479 TapeTogglePause(TAPE_TOGGLE_MANUAL | TAPE_TOGGLE_PLAY_PAUSE);
1481 else if (!tape.fast_forward) /* PLAY -> FFWD */
1483 tape.fast_forward = TRUE;
1485 else if (!tape.warp_forward) /* FFWD -> WARP */
1487 tape.warp_forward = TRUE;
1489 else if (!tape.deactivate_display) /* WARP -> WARP BLIND */
1491 tape.deactivate_display = TRUE;
1493 TapeDeactivateDisplayOn();
1495 else /* WARP BLIND -> PLAY */
1497 tape.fast_forward = FALSE;
1498 tape.warp_forward = FALSE;
1499 tape.deactivate_display = FALSE;
1501 TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
1504 DrawVideoDisplayCurrentState();
1514 static void HandleTapeButtons(struct GadgetInfo *gi)
1516 HandleTapeButtonsExt(gi->custom_id);
1519 void HandleTapeButtonKeys(Key key)
1521 boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
1522 boolean extra_button_is_active = !eject_button_is_active;
1524 if (key == setup.shortcut.tape_eject && eject_button_is_active)
1525 HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
1526 else if (key == setup.shortcut.tape_extra && extra_button_is_active)
1527 HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
1528 else if (key == setup.shortcut.tape_stop)
1529 HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
1530 else if (key == setup.shortcut.tape_pause)
1531 HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
1532 else if (key == setup.shortcut.tape_record)
1533 HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
1534 else if (key == setup.shortcut.tape_play)
1535 HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);