1 // Provides a seekdemo alias that allows fast-forwarding demo playback by a
6 // Add "exec input-demoseeking.cfg" and the following cl_hook_gamestart_all
7 // hook to autoexec.cfg. If you already have a cl_hook_gamestart_all hook, add
8 // the quoted command to your hook, separated with a semicolon:
10 // exec demoseeking.cfg
11 // alias cl_hook_gamestart_all "demoseeking_game_started"
14 // 1. start a demo (ply/playdemo command or menu), and
15 // 2. while in a demo, use the "seekdemo <seconds>" command
17 // Alternatively, use input-demoseeking.cfg instead to restore the default
20 // * use ,. (comma and dot) keys to seek -5/+5 seconds
21 // * use m/ (m and slash) keys to seek -30/+30 seconds
22 // * - (minus) also works instead of slash
24 // Options (persisted):
26 // _demoseeking_min_speed
27 // Minimum seek speed. Default 1.5.
28 // _demoseeking_max_speed
29 // Maximum seek speed. Default 200. Reduce this if the client freezes too
30 // much while seeking. If the framerate is still high while seeking, you
31 // may increase this, but increasing this with low framerate is unlikely to
32 // improve seek performance.
37 // Indicates whether a demo is currently playing. Set to 1 when playdemo
39 // Note: the setup above is needed to reset this reliably.
41 // The name of the latest played demo. Same as the argument passed to the
42 // last playdemo command. If playdemo fails to start the demo (e.g. when
43 // the file does not exist) this variable is not updated.
44 // Note: the setup above is needed to reset this reliably.
45 // _demoseeking_is_seeking
46 // 1 while a seek is in progress, 0 otherwise.
50 // - The alias playdemo_hook is run when demo playback is started.
51 // - The alias seekdemo_hook_seek_end is run after a seek completed successfully.
52 // - The alias seekdemo_getseektime can be used by scripts to get the current
53 // playback time or seek target time. See its usage below.
55 // state initialization
56 alias _demoseeking_init_vars "set _demoseeking_vars_loaded 1; set _demo_is_playing 0; set _current_demo_name \"\"; set _demoseeking_is_seeking 0; set _seekdemo_state idle; set _seekdemo_target 0; alias playdemo_hook \"\"; alias seekdemo_hook_seek_end"
57 alias _demoseeking_init_vars1 ""
58 _demoseeking_init_vars${_demoseeking_vars_loaded ?}
60 // option initialization (persistent)
61 alias _demoseeking_init_options "seta _demoseeking_options_loaded 1; seta _demoseeking_min_speed 1.5; seta _demoseeking_max_speed 200"
62 alias _demoseeking_init_options1 ""
63 _demoseeking_init_options${_demoseeking_options_loaded ?}
65 // Hook into game start to reset seekdemo state. This prevents reloading the
66 // last demo when seekdemo is accidentally called during an actual game.
67 alias demoseeking_game_started "set _demo_is_playing 0; set _current_demo_name \"\""
69 // usage: seekdemo_getseektime <varname>
70 // Populates <varname> with the current seek target time, if seeking. If not
71 // seeking, populate it with the current playback time.
72 alias seekdemo_getseektime "_seekdemo_getseektime_get \"$1\" _seekdemo_getseektime_rpn_$_seekdemo_state"
73 alias _seekdemo_getseektime_get "cl_cmd rpn \"/$1\" ${$2} def"
74 set _seekdemo_getseektime_rpn_idle time
75 set _seekdemo_getseektime_rpn_starting _seekdemo_target
76 set _seekdemo_getseektime_rpn_seeking _seekdemo_target
78 // Hook into "playdemo" command. This alias runs immediately after the actual
79 // "playdemo" command runs. Needed for seekdemo to restart the demo when
80 // seeking backwards. Runs the playdemo_hook alias when a demo file is being
83 // "cl_cmd rpn" fails while a map is loading, leaving the value of
84 // _demoseeking_playdemo_success untouched, which means the playdemo command
86 alias playdemo "set _demoseeking_playdemo_success 1; cl_cmd rpn /_demoseeking_playdemo_success 0 def; _demoseeking_playdemo_check \"$1\""
87 alias _demoseeking_playdemo_check "_demoseeking_playdemo_check_$_demoseeking_playdemo_success \"$1\""
88 alias _demoseeking_playdemo_check_0 "echo \"playdemo failed\""
89 alias _demoseeking_playdemo_check_1 "set _demo_is_playing 1; set _current_demo_name \"$1\"; playdemo_hook"
91 // Hook into the "defer" command to restore state when "defer clear" is run
92 // while seeking. "defer clear" seems to be run by some csprogs.
94 // compare the first argument. when it's "clear", run _demoseeking_restore
95 alias defer "alias _demoseeking_deftmp_$1 \"\"; alias _demoseeking_deftmp_clear _demoseeking_restore; _demoseeking_deftmp_$1; unalias _demoseeking_deftmp_$1"
96 alias _demoseeking_restore "_demoseeking_restore_$_seekdemo_state"
97 // idle - no seek, no state to restore
98 alias _demoseeking_restore_idle ""
99 // startseek - special state where this script calls defer clear before
100 // starting the seek loop
101 alias _demoseeking_restore_startseek ""
102 // starting - unexpected defer clear before seek started. Restore state by
103 // running check for forward/backward seek.
104 alias _demoseeking_restore_starting "_seekdemo_start_seek"
105 // seeking - unexpected defer clear while seeking. Restart the loop.
106 alias _demoseeking_restore_seeking "_seekdemo_check_time"
108 // usage: seekdemo <seconds>
109 // Seek the playing demo by the number of seconds. May be a floating point
110 // number. The seek is asynchronous. When a seek is already in action, the
111 // target time is adjusted by the specified amount.
112 // When starting a seek backwards, the demo is restarted before forwarding to
113 // the earlier time. Does nothing when no demo is playing (_demo_is_playing).
115 // check if seek should start or if target time should be adjusted instead
116 alias seekdemo "_seekdemo_checkstate_$_seekdemo_state ${* q}"
117 // before starting seek, verify demo is playing first
118 alias _seekdemo_checkstate_idle "set _seekdemo_demo_is_playing 0; cl_cmd rpn /_seekdemo_demo_is_playing _demo_is_playing 0 != def; _seekdemo_checkstart ${* q}"
119 // when already seeking, only update the variable holding the target time
120 alias _seekdemo_checkstate_starting "_seekdemo_checkstate_seeking ${* q}"
121 alias _seekdemo_checkstate_seeking "rpn /_seekdemo_target _seekdemo_target \"$1\" add def"
123 alias _seekdemo_checkstart "_seekdemo_checkstart_$_seekdemo_demo_is_playing ${* q}"
124 alias _seekdemo_checkstart_0 "echo \"no demo currently playing\""
126 alias _seekdemo_checkstart_1 "cl_cmd rpn /_seekdemo_target time \"$1\" add def; set _demoseeking_is_seeking 1; set _seekdemo_state starting; _seekdemo_save_options; _seekdemo_start_seek"
128 // check if seeking forwards or backwards
129 alias _seekdemo_start_seek "set _seekdemo_time_increasing err; cl_cmd rpn /_seekdemo_time_increasing _seekdemo_target time ge def; _seekdemo_check_increasing"
130 alias _seekdemo_check_increasing "_seekdemo_check_increasing_$_seekdemo_time_increasing"
131 // when seeking backwards, restart the demo and start the reload wait loop
132 alias _seekdemo_check_increasing_0 "playdemo \"$_current_demo_name\"; set _seekdemo_reload_success 1; cl_cmd rpn _seekdemo_reload_success 0 def; _seekdemo_check_reloading"
133 alias _seekdemo_check_increasing_1 "_seekdemo_check_time"
134 alias _seekdemo_check_increasing_err "_seekdemo_failed \"(demo not loaded?)\""
135 alias _seekdemo_check_reloading "_seekdemo_check_reloading_$_seekdemo_reload_success"
136 alias _seekdemo_check_reloading_0 "_seekdemo_failed (playdemo)"
137 // launch the "defer 10" command as a fallback to stop waiting in case demo reload fails
138 alias _seekdemo_check_reloading_1 "defer 10 \"_seekdemo_failed (reload)\"; _seekdemo_wait_reload"
140 // demo reload wait loop. "cl_cmd rpn" fails to execute until demo finished loading, leaving _demo_loaded 0
141 alias _seekdemo_wait_reload "set _demo_loaded 0; cl_cmd rpn /_demo_loaded 1 def; _seekdemo_check_loaded"
142 alias _seekdemo_check_loaded "_seekdemo_check_loaded_$_demo_loaded"
143 alias _seekdemo_check_loaded_0 "defer 0.02 _seekdemo_wait_reload"
144 // defer clear kills the fallback "defer 10" above
145 alias _seekdemo_check_loaded_1 "set _seekdemo_state startseek; defer clear; set _seekdemo_state seeking; _seekdemo_check_time"
147 // start of main seek loop, check if target time is reached
148 alias _seekdemo_check_time "set _seekdemo_seek_state failed; cl_cmd rpn _seekdemo_target time dup /_seekdemo_current_time exch def gt /_seekdemo_seek_state exch def; _seekdemo_check_continue"
149 alias _seekdemo_check_continue "_seekdemo_check_continue_$_seekdemo_seek_state"
150 alias _seekdemo_check_continue_0 "_seekdemo_leave_seek; seekdemo_hook_seek_end"
151 // Update options while seeking.
152 // - Adjust the playback speed by setting the slowmo cvar. The further the seek, the faster the playback speed.
153 // - Compare the speed to different speed levels to maximize performance for long seeks while reducing screen flashes for short ones.
154 // Then continue the loop with "defer 0", so the check is run on every rendered frame.
155 alias _seekdemo_check_continue_1 "set _seekdemo_speed_level failed; cl_cmd rpn _seekdemo_target time sub 10 mul _demoseeking_min_speed max _demoseeking_max_speed min dup /slowmo exch def dup 80 gt exch 2 gt add /_seekdemo_speed_level exch def; _seekdemo_set_options; defer 0 _seekdemo_check_time"
156 alias _seekdemo_check_continue_failed "_seekdemo_failed \"(demo ended?)\""
158 alias _seekdemo_failed "_seekdemo_leave_seek; echo SEEK FAILED ${1 ?}"
160 // Update options while and after seeking and for different levels of seek speed.
161 // - Disable r_render only at high seek speed to maximize performance, but keep
162 // it enabled otherwise to avoid jarring black screens for short seeks.
163 // - snd_startnonloopingsounds disables all sounds that can safely be disabled,
164 // avoiding lots of sounds playing at the same time.
165 // - Other disabled options prevent side effects from fast seeking at low
166 // framerate, where particles and other effects accumulate and appear at the
167 // same time when the target has been reached.
168 alias _seekdemo_leave_seek "_seekdemo_restore_options; set _demoseeking_is_seeking 0; set _seekdemo_state idle; r_render 1; snd_startnonloopingsounds 1; slowmo \"$_seekdemo_sav_slowmo\""
169 alias _seekdemo_set_options "_seekdemo_set_options_$_seekdemo_speed_level"
170 // speed level 0: below 2x playback speed, target almost reached, all options restored
171 alias _seekdemo_set_options_0 "r_render 1; snd_startnonloopingsounds 1; _seekdemo_restore_options"
172 // speed level 1: between 2x and 80x playback speed, sounds disabled, game rendered with reduced graphics
173 alias _seekdemo_set_options_1 "r_render 1; snd_startnonloopingsounds 0; _seekdemo_settemp_options"
174 // speed level 2: above 80x playback speed, sounds disabled, game not rendered to maximize performance
175 alias _seekdemo_set_options_2 "r_render 0; snd_startnonloopingsounds 0; _seekdemo_settemp_options"
176 // save/restore options for reduced settings
177 alias _seekdemo_save_options "set _seekdemo_sav_cl_decals \"$cl_decals\"; set _seekdemo_sav_cl_damagetext \"$cl_damagetext\"; set _seekdemo_sav_cl_particles \"$cl_particles\"; set _seekdemo_sav_cl_casings \"$cl_casings\"; set _seekdemo_sav_slowmo \"$slowmo\""
178 alias _seekdemo_settemp_options "settemp cl_decals 0; settemp cl_damagetext 0; settemp cl_particles 0; settemp cl_casings 0"
179 alias _seekdemo_restore_options "settemp cl_decals \"$_seekdemo_sav_cl_decals\"; settemp cl_damagetext \"$_seekdemo_sav_cl_damagetext\"; settemp cl_particles \"$_seekdemo_sav_cl_particles\"; settemp cl_casings \"$_seekdemo_sav_cl_casings\""