| 1 |
// |
| 2 |
// Copyright(C) 2020 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Key Frame |
| 16 |
// |
| 17 |
|
| 18 |
#include <time.h> |
| 19 |
|
| 20 |
#include "doomstat.h" |
| 21 |
#include "s_advsound.h" |
| 22 |
#include "s_sound.h" |
| 23 |
#include "st_stuff.h" |
| 24 |
#include "p_saveg.h" |
| 25 |
#include "p_map.h" |
| 26 |
#include "r_draw.h" |
| 27 |
#include "r_fps.h" |
| 28 |
#include "r_main.h" |
| 29 |
#include "g_game.h" |
| 30 |
#include "m_file.h" |
| 31 |
#include "i_system.h" |
| 32 |
#include "lprintf.h" |
| 33 |
#include "e6y.h" |
| 34 |
|
| 35 |
#include "heretic/sb_bar.h" |
| 36 |
|
| 37 |
#include "dsda.h" |
| 38 |
#include "dsda/args.h" |
| 39 |
#include "dsda/build.h" |
| 40 |
#include "dsda/configuration.h" |
| 41 |
#include "dsda/demo.h" |
| 42 |
#include "dsda/features.h" |
| 43 |
#include "dsda/mapinfo.h" |
| 44 |
#include "dsda/options.h" |
| 45 |
#include "dsda/pause.h" |
| 46 |
#include "dsda/playback.h" |
| 47 |
#include "dsda/save.h" |
| 48 |
#include "dsda/settings.h" |
| 49 |
#include "dsda/time.h" |
| 50 |
|
| 51 |
#include "key_frame.h" |
| 52 |
|
| 53 |
static dboolean auto_kf_timed_out; |
| 54 |
static int auto_kf_timeout_count; |
| 55 |
|
| 56 |
#define TIMEOUT_LIMIT 1 |
| 57 |
|
| 58 |
static dsda_key_frame_t first_kf; |
| 59 |
static dsda_key_frame_t temp_kf; |
| 60 |
dsda_key_frame_t quick_kf; |
| 61 |
auto_kf_t* auto_key_frames; |
| 62 |
int auto_kf_size = 0; |
| 63 |
static auto_kf_t* last_auto_kf; |
| 64 |
dsda_key_frame_t* playback_key_frames; |
| 65 |
int playback_kf_size = 0; |
| 66 |
static int restore_key_frame_index = -1; |
| 67 |
|
| 68 |
static int dsda_auto_key_frame_interval; |
| 69 |
static int dsda_auto_key_frame_depth; |
| 70 |
static int dsda_auto_key_frame_timeout; |
| 71 |
|
| 72 |
static int autoKeyFrameTimeout(void) { |
| 73 |
return dsda_StartInBuildMode() ? 0 : dsda_auto_key_frame_timeout; |
| 74 |
} |
| 75 |
|
| 76 |
static int autoKeyFrameDepth(void) { |
| 77 |
if (dsda_StartInBuildMode() && dsda_auto_key_frame_depth < 60) |
| 78 |
return 60; |
| 79 |
|
| 80 |
return dsda_auto_key_frame_depth; |
| 81 |
} |
| 82 |
|
| 83 |
static int autoKeyFrameInterval(void) { |
| 84 |
if (dsda_StartInBuildMode()) |
| 85 |
return 1; |
| 86 |
|
| 87 |
return dsda_auto_key_frame_interval; |
| 88 |
} |
| 89 |
|
| 90 |
static dboolean autoKFExists(auto_kf_t* auto_kf) { |
| 91 |
return auto_kf && auto_kf->auto_index && auto_kf->kf.buffer; |
| 92 |
} |
| 93 |
|
| 94 |
void dsda_ForgetAutoKeyFrames(void) { |
| 95 |
if (last_auto_kf) |
| 96 |
last_auto_kf->auto_index = 0; |
| 97 |
} |
| 98 |
|
| 99 |
static void dsda_ResetParentKF(dsda_key_frame_t* kf) { |
| 100 |
kf->parent.auto_kf = NULL; |
| 101 |
kf->parent.buffer = NULL; |
| 102 |
} |
| 103 |
|
| 104 |
static void dsda_AttachAutoKF(dsda_key_frame_t* kf) { |
| 105 |
if (autoKFExists(last_auto_kf)) { |
| 106 |
kf->parent.auto_kf = last_auto_kf; |
| 107 |
kf->parent.buffer = last_auto_kf->kf.buffer; |
| 108 |
} |
| 109 |
else |
| 110 |
dsda_ResetParentKF(kf); |
| 111 |
} |
| 112 |
|
| 113 |
static void dsda_ResolveParentKF(dsda_key_frame_t* kf) { |
| 114 |
if (autoKFExists(kf->parent.auto_kf) && kf->parent.auto_kf->kf.buffer == kf->parent.buffer) |
| 115 |
last_auto_kf = kf->parent.auto_kf; |
| 116 |
else { |
| 117 |
dsda_ResetParentKF(kf); |
| 118 |
dsda_ForgetAutoKeyFrames(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static void dsda_RewindKF(auto_kf_t** current) { |
| 123 |
auto_kf_t* auto_kf; |
| 124 |
|
| 125 |
auto_kf = *current; |
| 126 |
|
| 127 |
if (auto_kf && |
| 128 |
auto_kf->auto_index && auto_kf->prev->auto_index && |
| 129 |
auto_kf->auto_index == auto_kf->prev->auto_index + 1) |
| 130 |
*current = auto_kf->prev; |
| 131 |
else |
| 132 |
*current = NULL; |
| 133 |
} |
| 134 |
|
| 135 |
static dsda_key_frame_t* dsda_ClosestKeyFrame(int target_tic_count) { |
| 136 |
dsda_key_frame_t* closest = NULL; |
| 137 |
|
| 138 |
if (auto_key_frames) |
| 139 |
for (int i = 0; i < auto_kf_size; i++) |
| 140 |
if (auto_key_frames[i].kf.game_tic_count <= target_tic_count) |
| 141 |
if (!closest || auto_key_frames[i].kf.game_tic_count > closest->game_tic_count) |
| 142 |
closest = &auto_key_frames[i].kf; |
| 143 |
|
| 144 |
if (playback_key_frames) |
| 145 |
for (int i = 0; i < playback_kf_size; i++) |
| 146 |
if (playback_key_frames[i].game_tic_count <= target_tic_count) |
| 147 |
if (!closest || playback_key_frames[i].game_tic_count > closest->game_tic_count) |
| 148 |
closest = &playback_key_frames[i]; |
| 149 |
|
| 150 |
if (!demorecording && temp_kf.buffer) |
| 151 |
if (temp_kf.game_tic_count <= target_tic_count) |
| 152 |
if (!closest || temp_kf.game_tic_count > closest->game_tic_count) |
| 153 |
closest = &temp_kf; |
| 154 |
|
| 155 |
if (!demorecording && quick_kf.buffer) |
| 156 |
if (quick_kf.game_tic_count <= target_tic_count) |
| 157 |
if (!closest || quick_kf.game_tic_count > closest->game_tic_count) |
| 158 |
closest = &quick_kf; |
| 159 |
|
| 160 |
if (first_kf.buffer) |
| 161 |
if (first_kf.game_tic_count <= target_tic_count) |
| 162 |
if (!closest || first_kf.game_tic_count > closest->game_tic_count) |
| 163 |
closest = &first_kf; |
| 164 |
|
| 165 |
return closest; |
| 166 |
} |
| 167 |
|
| 168 |
void dsda_CopyKeyFrame(dsda_key_frame_t* dest, dsda_key_frame_t* source) { |
| 169 |
*dest = *source; |
| 170 |
dest->buffer = Z_Malloc(dest->buffer_length); |
| 171 |
memcpy(dest->buffer, source->buffer, dest->buffer_length); |
| 172 |
} |
| 173 |
|
| 174 |
void dsda_InitAutoKeyFrames(void) { |
| 175 |
int i; |
| 176 |
|
| 177 |
dsda_auto_key_frame_interval = dsda_IntConfig(dsda_config_auto_key_frame_interval); |
| 178 |
dsda_auto_key_frame_depth = dsda_IntConfig(dsda_config_auto_key_frame_depth); |
| 179 |
dsda_auto_key_frame_timeout = dsda_IntConfig(dsda_config_auto_key_frame_timeout); |
| 180 |
|
| 181 |
auto_kf_size = autoKeyFrameDepth(); |
| 182 |
|
| 183 |
if (!auto_kf_size) { |
| 184 |
last_auto_kf = NULL; |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
++auto_kf_size; // chain includes a terminator |
| 189 |
|
| 190 |
if (auto_key_frames != NULL) |
| 191 |
Z_Free(auto_key_frames); |
| 192 |
|
| 193 |
auto_key_frames = Z_Calloc(auto_kf_size, sizeof(auto_kf_t)); |
| 194 |
|
| 195 |
auto_key_frames[0].prev = &auto_key_frames[auto_kf_size - 1]; |
| 196 |
auto_key_frames[auto_kf_size - 1].next = &auto_key_frames[0]; |
| 197 |
|
| 198 |
for (i = 0; i < auto_kf_size - 1; ++i) |
| 199 |
auto_key_frames[i].next = &auto_key_frames[i + 1]; |
| 200 |
|
| 201 |
for (i = 1; i < auto_kf_size; ++i) |
| 202 |
auto_key_frames[i].prev = &auto_key_frames[i - 1]; |
| 203 |
|
| 204 |
last_auto_kf = &auto_key_frames[auto_kf_size - 1]; |
| 205 |
} |
| 206 |
|
| 207 |
void dsda_InitPlaybackKeyFrames() { |
| 208 |
// Max of 60 keyframes are saved, and they need to have 1 minute in between each |
| 209 |
playback_kf_size = demo_tics_count * demo_playerscount / TICRATE / 60; |
| 210 |
if (playback_kf_size > 60) |
| 211 |
playback_kf_size = 60; |
| 212 |
|
| 213 |
if (playback_key_frames != NULL) |
| 214 |
Z_Free(playback_key_frames); |
| 215 |
|
| 216 |
playback_key_frames = Z_Calloc(playback_kf_size, sizeof(dsda_key_frame_t)); |
| 217 |
} |
| 218 |
|
| 219 |
void dsda_ExportKeyFrame(byte* buffer, int length) { |
| 220 |
char name[40]; |
| 221 |
int timestamp; |
| 222 |
|
| 223 |
timestamp = totalleveltimes + leveltime; |
| 224 |
|
| 225 |
snprintf(name, sizeof(name), "backup-%010d.kf", timestamp); |
| 226 |
|
| 227 |
if (M_FileExists(name)) |
| 228 |
snprintf(name, sizeof(name), "backup-%010d-%lld.kf", timestamp, (long long) time(NULL)); |
| 229 |
|
| 230 |
if (!M_WriteFile(name, buffer, length)) |
| 231 |
I_Error("dsda_ExportKeyFrame: Failed to write key frame."); |
| 232 |
} |
| 233 |
|
| 234 |
// Stripped down version of G_DoSaveGame |
| 235 |
void dsda_StoreKeyFrame(dsda_key_frame_t* key_frame, byte complete, byte export) { |
| 236 |
key_frame->game_tic_count = true_logictic; |
| 237 |
|
| 238 |
P_InitSaveBuffer(); |
| 239 |
|
| 240 |
P_SAVE_BYTE(complete); |
| 241 |
P_SAVE_X(key_frame->game_tic_count); |
| 242 |
|
| 243 |
// Store state of demo playback buffer |
| 244 |
dsda_StorePlaybackPosition(); |
| 245 |
|
| 246 |
// Store state of demo recording buffer |
| 247 |
dsda_StoreDemoData(complete); |
| 248 |
|
| 249 |
dsda_ArchiveAll(); |
| 250 |
|
| 251 |
if (key_frame->buffer != NULL) Z_Free(key_frame->buffer); |
| 252 |
|
| 253 |
key_frame->buffer = savebuffer; |
| 254 |
key_frame->buffer_length = save_p - savebuffer; |
| 255 |
|
| 256 |
P_ForgetSaveBuffer(); |
| 257 |
|
| 258 |
dsda_AttachAutoKF(key_frame); |
| 259 |
|
| 260 |
if (complete) { |
| 261 |
if (demorecording && export) |
| 262 |
dsda_ExportKeyFrame(key_frame->buffer, key_frame->buffer_length); |
| 263 |
|
| 264 |
doom_printf("Stored key frame"); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
// Stripped down version of G_DoLoadGame |
| 269 |
void dsda_RestoreKeyFrame(dsda_key_frame_t* key_frame, dboolean skip_wipe) { |
| 270 |
void G_AfterLoad(void); |
| 271 |
|
| 272 |
byte complete; |
| 273 |
|
| 274 |
if (key_frame->buffer == NULL) { |
| 275 |
doom_printf("No key frame found"); |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
dsda_TrackFeature(uf_keyframe); |
| 280 |
|
| 281 |
if (skip_wipe || dsda_BuildMode()) |
| 282 |
dsda_SkipNextWipe(); |
| 283 |
|
| 284 |
save_p = key_frame->buffer; |
| 285 |
|
| 286 |
P_LOAD_BYTE(complete); |
| 287 |
P_LOAD_X(key_frame->game_tic_count); |
| 288 |
|
| 289 |
// Restore state of demo playback buffer |
| 290 |
dsda_RestorePlaybackPosition(); |
| 291 |
|
| 292 |
// Restore state of demo recording buffer |
| 293 |
dsda_RestoreDemoData(complete); |
| 294 |
|
| 295 |
dsda_UnArchiveAll(); |
| 296 |
|
| 297 |
dsda_RestoreCommandHistory(); |
| 298 |
|
| 299 |
restore_key_frame_index = (totalleveltimes + leveltime) / (35 * autoKeyFrameInterval()); |
| 300 |
|
| 301 |
G_AfterLoad(); |
| 302 |
|
| 303 |
dsda_QueueJoin(); |
| 304 |
|
| 305 |
dsda_ResolveParentKF(key_frame); |
| 306 |
|
| 307 |
doom_printf("Restored key frame"); |
| 308 |
} |
| 309 |
|
| 310 |
void dsda_StoreTempKeyFrame(void) { |
| 311 |
dsda_StoreKeyFrame(&temp_kf, true, true); |
| 312 |
} |
| 313 |
|
| 314 |
void dsda_StoreQuickKeyFrame(void) { |
| 315 |
dsda_StoreKeyFrame(&quick_kf, true, true); |
| 316 |
} |
| 317 |
|
| 318 |
void dsda_RestoreQuickKeyFrame(void) { |
| 319 |
dsda_RestoreKeyFrame(&quick_kf, false); |
| 320 |
} |
| 321 |
|
| 322 |
dboolean dsda_RestoreClosestKeyFrame(int tic) { |
| 323 |
dsda_key_frame_t* key_frame; |
| 324 |
|
| 325 |
key_frame = dsda_ClosestKeyFrame(tic); |
| 326 |
|
| 327 |
if (!key_frame) |
| 328 |
return false; |
| 329 |
|
| 330 |
dsda_RestoreKeyFrame(key_frame, true); |
| 331 |
|
| 332 |
return true; |
| 333 |
} |
| 334 |
|
| 335 |
void dsda_RestoreKeyFrameFile(const char* name) { |
| 336 |
char *filename; |
| 337 |
dsda_key_frame_t key_frame = { 0 }; |
| 338 |
|
| 339 |
filename = I_RequireFile(name, ".kf"); |
| 340 |
M_ReadFile(filename, &key_frame.buffer); |
| 341 |
Z_Free(filename); |
| 342 |
|
| 343 |
dsda_RestoreKeyFrame(&key_frame, false); |
| 344 |
Z_Free(key_frame.buffer); |
| 345 |
} |
| 346 |
|
| 347 |
void dsda_ContinueKeyFrame(void) { |
| 348 |
dsda_arg_t* arg; |
| 349 |
|
| 350 |
arg = dsda_Arg(dsda_arg_from_key_frame); |
| 351 |
if (arg->found) { |
| 352 |
dsda_RestoreKeyFrameFile(arg->value.v_string); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
void dsda_RewindAutoKeyFrame(void) { |
| 357 |
auto_kf_t* load_kf; |
| 358 |
|
| 359 |
load_kf = last_auto_kf; |
| 360 |
dsda_RewindKF(&load_kf); |
| 361 |
|
| 362 |
if (load_kf) |
| 363 |
dsda_RestoreKeyFrame(&load_kf->kf, true); |
| 364 |
else |
| 365 |
doom_printf("No key frame found"); // rewind past the depth limit |
| 366 |
} |
| 367 |
|
| 368 |
void dsda_ResetAutoKeyFrameTimeout(void) { |
| 369 |
auto_kf_timed_out = false; |
| 370 |
auto_kf_timeout_count = 0; |
| 371 |
} |
| 372 |
|
| 373 |
void dsda_UpdateAutoKeyFrames(void) { |
| 374 |
int key_frame_index; |
| 375 |
int current_time; |
| 376 |
int interval_tics; |
| 377 |
dsda_key_frame_t* current_key_frame; |
| 378 |
|
| 379 |
if ( |
| 380 |
auto_kf_timed_out || |
| 381 |
auto_kf_size == 0 || |
| 382 |
gamestate != GS_LEVEL || |
| 383 |
gameaction != ga_nothing |
| 384 |
) return; |
| 385 |
|
| 386 |
current_time = totalleveltimes + leveltime; |
| 387 |
interval_tics = 35 * autoKeyFrameInterval(); |
| 388 |
|
| 389 |
// Automatically save a key frame each interval |
| 390 |
if (current_time % interval_tics == 0) { |
| 391 |
key_frame_index = current_time / interval_tics; |
| 392 |
|
| 393 |
// Don't duplicate on rewind |
| 394 |
if (key_frame_index == restore_key_frame_index) { |
| 395 |
restore_key_frame_index = -1; |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
last_auto_kf = last_auto_kf->next; |
| 400 |
last_auto_kf->next->auto_index = 0; |
| 401 |
last_auto_kf->auto_index = last_auto_kf->prev->auto_index + 1; |
| 402 |
|
| 403 |
current_key_frame = &last_auto_kf->kf; |
| 404 |
|
| 405 |
{ |
| 406 |
unsigned long long elapsed_time; |
| 407 |
|
| 408 |
dsda_StartTimer(dsda_timer_key_frame); |
| 409 |
dsda_StoreKeyFrame(current_key_frame, false, false); |
| 410 |
elapsed_time = dsda_ElapsedTimeMS(dsda_timer_key_frame); |
| 411 |
|
| 412 |
if (autoKeyFrameTimeout()) { |
| 413 |
if (elapsed_time > autoKeyFrameTimeout()) { |
| 414 |
++auto_kf_timeout_count; |
| 415 |
|
| 416 |
if (auto_kf_timeout_count > TIMEOUT_LIMIT) { |
| 417 |
auto_kf_timed_out = true; |
| 418 |
doom_printf("Slow key framing: rewind disabled"); |
| 419 |
} |
| 420 |
} |
| 421 |
else |
| 422 |
auto_kf_timeout_count = 0; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
if (!first_kf.buffer) |
| 427 |
dsda_CopyKeyFrame(&first_kf, current_key_frame); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
void dsda_UpdatePlaybackKeyFrames(void) { |
| 432 |
int current_time; |
| 433 |
int interval_tics; |
| 434 |
|
| 435 |
if (gameaction != ga_nothing || playback_kf_size == 0) return; |
| 436 |
|
| 437 |
current_time = totalleveltimes + leveltime; |
| 438 |
interval_tics = (demo_tics_count * demo_playerscount) / playback_kf_size; |
| 439 |
|
| 440 |
// Automatically save a key frame each interval |
| 441 |
if (current_time % interval_tics == 0 && |
| 442 |
current_time / interval_tics < playback_kf_size) { |
| 443 |
|
| 444 |
dsda_key_frame_t* current_key_frame = &playback_key_frames[current_time / interval_tics]; |
| 445 |
|
| 446 |
if (!current_key_frame->buffer) |
| 447 |
dsda_StoreKeyFrame(current_key_frame, false, false); |
| 448 |
} |
| 449 |
} |