| 1 |
// |
| 2 |
// Copyright(C) 2022 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 Playback |
| 16 |
// |
| 17 |
|
| 18 |
#include "doomstat.h" |
| 19 |
#include "g_game.h" |
| 20 |
#include "i_system.h" |
| 21 |
#include "lprintf.h" |
| 22 |
#include "p_saveg.h" |
| 23 |
#include "w_wad.h" |
| 24 |
|
| 25 |
#include "dsda/args.h" |
| 26 |
#include "dsda/demo.h" |
| 27 |
#include "dsda/exdemo.h" |
| 28 |
#include "dsda/input.h" |
| 29 |
#include "dsda/key_frame.h" |
| 30 |
#include "dsda/skip.h" |
| 31 |
|
| 32 |
#include "playback.h" |
| 33 |
|
| 34 |
static const byte* playback_origin_p; |
| 35 |
static const byte* playback_p; |
| 36 |
static int playback_length; |
| 37 |
static int playback_behaviour; |
| 38 |
|
| 39 |
static dsda_arg_t* playdemo_arg; |
| 40 |
static dsda_arg_t* playlump_arg; |
| 41 |
static dsda_arg_t* fastdemo_arg; |
| 42 |
static dsda_arg_t* timedemo_arg; |
| 43 |
static dsda_arg_t* recordfromto_arg; |
| 44 |
static char* playback_name; |
| 45 |
static char* playback_filename; |
| 46 |
|
| 47 |
extern int demo_tics; |
| 48 |
|
| 49 |
dboolean demoplayback; |
| 50 |
dboolean userdemo; |
| 51 |
|
| 52 |
void dsda_RestartPlayback(void) { |
| 53 |
G_StartDemoPlayback(playback_origin_p, playback_length, playback_behaviour); |
| 54 |
} |
| 55 |
|
| 56 |
dboolean dsda_JumpToLogicTic(int tic) { |
| 57 |
if (tic < 0) |
| 58 |
return false; |
| 59 |
|
| 60 |
if (!dsda_RestoreClosestKeyFrame(tic)) |
| 61 |
return false; |
| 62 |
|
| 63 |
if (tic != true_logictic) |
| 64 |
dsda_SkipToLogicTic(tic); |
| 65 |
|
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
dboolean dsda_JumpToLogicTicFrom(int tic, int from_tic) { |
| 70 |
if (tic < 0 || tic > true_logictic) |
| 71 |
return false; |
| 72 |
|
| 73 |
if (!dsda_RestoreClosestKeyFrame(from_tic)) |
| 74 |
return false; |
| 75 |
|
| 76 |
if (tic != true_logictic) |
| 77 |
dsda_SkipToLogicTic(tic); |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
const char* dsda_PlaybackName(void) { |
| 83 |
return playback_name; |
| 84 |
} |
| 85 |
|
| 86 |
void dsda_ExecutePlaybackOptions(void) { |
| 87 |
if (playlump_arg) { |
| 88 |
if (W_CheckNumForName(playback_name) == LUMP_NOT_FOUND) |
| 89 |
I_Error("Unable to find required internal demo lump \"%s\"", playback_name); |
| 90 |
} |
| 91 |
|
| 92 |
if (playdemo_arg) |
| 93 |
{ |
| 94 |
G_DeferedPlayDemo(playback_name); |
| 95 |
userdemo = true; |
| 96 |
} |
| 97 |
else if (fastdemo_arg) { |
| 98 |
G_DeferedPlayDemo(playback_name); |
| 99 |
fastdemo = true; |
| 100 |
timingdemo = true; |
| 101 |
userdemo = true; |
| 102 |
} |
| 103 |
else if (timedemo_arg) |
| 104 |
{ |
| 105 |
G_DeferedPlayDemo(playback_name); |
| 106 |
singletics = true; |
| 107 |
timingdemo = true; |
| 108 |
userdemo = true; |
| 109 |
} |
| 110 |
else if (recordfromto_arg) { |
| 111 |
userdemo = true; |
| 112 |
G_ContinueDemo(playback_name); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
static void dsda_UpdatePlaybackName(const char* name, dboolean require_file) { |
| 117 |
if (playback_name) |
| 118 |
Z_Free(playback_name); |
| 119 |
|
| 120 |
if (playback_filename) |
| 121 |
Z_Free(playback_filename); |
| 122 |
|
| 123 |
playback_name = Z_Strdup(name); |
| 124 |
|
| 125 |
if (require_file) |
| 126 |
playback_filename = I_FindFile(playback_name, ".lmp"); |
| 127 |
else |
| 128 |
playback_filename = NULL; |
| 129 |
} |
| 130 |
|
| 131 |
const char* dsda_ParsePlaybackOptions(void) { |
| 132 |
dsda_arg_t* arg; |
| 133 |
|
| 134 |
arg = dsda_Arg(dsda_arg_playdemo); |
| 135 |
if (arg->found) { |
| 136 |
playdemo_arg = arg; |
| 137 |
dsda_UpdatePlaybackName(arg->value.v_string, true); |
| 138 |
// fall back to lump if file not found |
| 139 |
if (!playback_filename) |
| 140 |
playlump_arg = arg; |
| 141 |
return playback_filename; |
| 142 |
} |
| 143 |
|
| 144 |
arg = dsda_Arg(dsda_arg_playlump); |
| 145 |
if (arg->found) { |
| 146 |
playlump_arg = arg; |
| 147 |
dsda_UpdatePlaybackName(arg->value.v_string, false); |
| 148 |
playdemo_arg = arg; |
| 149 |
return playback_filename; |
| 150 |
} |
| 151 |
|
| 152 |
arg = dsda_Arg(dsda_arg_fastdemo); |
| 153 |
if (arg->found) { |
| 154 |
fastdemo_arg = arg; |
| 155 |
fastdemo = true; |
| 156 |
dsda_UpdatePlaybackName(arg->value.v_string, true); |
| 157 |
// fall back to lump if file not found |
| 158 |
if (!playback_filename) |
| 159 |
playlump_arg = arg; |
| 160 |
return playback_filename; |
| 161 |
} |
| 162 |
|
| 163 |
arg = dsda_Arg(dsda_arg_timedemo); |
| 164 |
if (arg->found) { |
| 165 |
timedemo_arg = arg; |
| 166 |
dsda_UpdatePlaybackName(arg->value.v_string, true); |
| 167 |
// fall back to lump if file not found |
| 168 |
if (!playback_filename) |
| 169 |
playlump_arg = arg; |
| 170 |
return playback_filename; |
| 171 |
} |
| 172 |
|
| 173 |
arg = dsda_Arg(dsda_arg_recordfromto); |
| 174 |
if (arg->found) { |
| 175 |
recordfromto_arg = arg; |
| 176 |
dsda_SetDemoBaseName(arg->value.v_string_array[1]); |
| 177 |
dsda_UpdatePlaybackName(arg->value.v_string_array[0], true); |
| 178 |
// require a file |
| 179 |
if (!playback_filename) |
| 180 |
playback_filename = I_RequireFile(arg->value.v_string_array[0], ".lmp"); |
| 181 |
return playback_filename; |
| 182 |
} |
| 183 |
|
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
void dsda_InitDemoPlayback(void) { |
| 188 |
demoplayback = true; |
| 189 |
} |
| 190 |
|
| 191 |
void dsda_AttachPlaybackStream(const byte* demo_p, int length, int behaviour) { |
| 192 |
playback_origin_p = demo_p; |
| 193 |
playback_p = demo_p; |
| 194 |
playback_length = length; |
| 195 |
playback_behaviour = behaviour; |
| 196 |
demo_tics = 0; |
| 197 |
} |
| 198 |
|
| 199 |
void dsda_StorePlaybackPosition(void) { |
| 200 |
P_SAVE_X(demo_tics); |
| 201 |
P_SAVE_X(playback_p); |
| 202 |
} |
| 203 |
|
| 204 |
void dsda_RestorePlaybackPosition(void) { |
| 205 |
P_LOAD_X(demo_tics); |
| 206 |
P_LOAD_X(playback_p); |
| 207 |
} |
| 208 |
|
| 209 |
void dsda_ClearPlaybackStream(void) { |
| 210 |
playback_origin_p = NULL; |
| 211 |
playback_p = NULL; |
| 212 |
playback_length = 0; |
| 213 |
playback_behaviour = 0; |
| 214 |
demo_tics = 0; |
| 215 |
|
| 216 |
demoplayback = false; |
| 217 |
userdemo = false; |
| 218 |
} |
| 219 |
|
| 220 |
static dboolean dsda_EndOfPlaybackStream(void) { |
| 221 |
return *playback_p == DEMOMARKER || |
| 222 |
playback_p + dsda_BytesPerTic() > playback_origin_p + playback_length; |
| 223 |
} |
| 224 |
|
| 225 |
void dsda_JoinDemo(ticcmd_t* cmd) { |
| 226 |
if (!demoplayback) |
| 227 |
return; |
| 228 |
|
| 229 |
if (dsda_SkipMode()) |
| 230 |
dsda_ExitSkipMode(); |
| 231 |
|
| 232 |
if (demorecording) |
| 233 |
dsda_WriteQueueToDemo(playback_p, playback_length - (playback_p - playback_origin_p)); |
| 234 |
|
| 235 |
dsda_ClearPlaybackStream(); |
| 236 |
|
| 237 |
if (cmd) |
| 238 |
dsda_JoinDemoCmd(cmd); |
| 239 |
else |
| 240 |
dsda_QueueJoin(); |
| 241 |
|
| 242 |
dsda_MergeExDemoFeatures(); |
| 243 |
} |
| 244 |
|
| 245 |
void dsda_TryPlaybackOneTick(ticcmd_t* cmd) { |
| 246 |
dboolean ended = false; |
| 247 |
|
| 248 |
if (!playback_p) |
| 249 |
return; |
| 250 |
|
| 251 |
if (dsda_EndOfPlaybackStream()) |
| 252 |
ended = true; |
| 253 |
else { |
| 254 |
G_ReadOneTick(cmd, &playback_p); |
| 255 |
|
| 256 |
++demo_tics; |
| 257 |
} |
| 258 |
|
| 259 |
if (ended) { |
| 260 |
if (playback_behaviour & PLAYBACK_JOIN_ON_END) |
| 261 |
dsda_JoinDemo(cmd); |
| 262 |
else |
| 263 |
G_CheckDemoStatus(); |
| 264 |
} |
| 265 |
else if (dsda_InputActive(dsda_input_join_demo) || dsda_InputJoyBActive(dsda_input_use)) |
| 266 |
dsda_JoinDemo(cmd); |
| 267 |
} |