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