// Populates <varname> with the current seek target time, if seeking. If not
// seeking, populate it with the current playback time.
alias seekdemo_getseektime "_seekdemo_getseektime_get \"$1\" _seekdemo_getseektime_rpn_$_seekdemo_state"
-alias _seekdemo_getseektime_get "cl_cmd rpn \"/$1\" ${$2} ="
+alias _seekdemo_getseektime_get "cl_cmd rpn \"/$1\" ${$2} def"
set _seekdemo_getseektime_rpn_idle time
set _seekdemo_getseektime_rpn_starting _seekdemo_target
set _seekdemo_getseektime_rpn_seeking _seekdemo_target
alias _demoseeking_restore_startseek ""
// starting - unexpected defer clear before seek started. Restore state by
// running check for forward/backward seek.
-alias _demoseeking_restore_starting "_seekdemo_check_increasing"
+alias _demoseeking_restore_starting "_seekdemo_start_seek"
// seeking - unexpected defer clear while seeking. Restart the loop.
alias _demoseeking_restore_seeking "_seekdemo_check_time"
// When starting a seek backwards, the demo is restarted before forwarding to
// the earlier time. Does nothing when no demo is playing (_demo_is_playing).
// ---
-alias seekdemo "set _seekdemo_demo_is_playing 0; cl_cmd rpn /_seekdemo_demo_is_playing _demo_is_playing 0 != def; _seekdemo_checkstart ${* q}"
// check if seek should start or if target time should be adjusted instead
-alias _seekdemo_checkstart "_seekdemo_checkstart_$_seekdemo_demo_is_playing ${* q}"
-alias _seekdemo_checkstart_0 "echo \"no demo currently playing\""
-alias _seekdemo_checkstart_1 "_seekdemo_checkstate_$_seekdemo_state ${* q}"
-// starting new seek. "dup" refers to the time value in the rpn command below
-alias _seekdemo_checkstate_idle "_seekdemo_calc_target dup \"$1\"; set _demoseeking_is_seeking 1; set _seekdemo_state starting; _seekdemo_save_options; _seekdemo_check_increasing"
+alias seekdemo "_seekdemo_checkstate_$_seekdemo_state ${* q}"
+// before starting seek, verify demo is playing first
+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}"
// when already seeking, only update the variable holding the target time
alias _seekdemo_checkstate_starting "_seekdemo_checkstate_seeking ${* q}"
-alias _seekdemo_checkstate_seeking "_seekdemo_calc_target _seekdemo_target \"$1\""
+alias _seekdemo_checkstate_seeking "rpn /_seekdemo_target _seekdemo_target \"$1\" add def"
-// calculate target time and set _seekdemo_time_increasing according to the direction. args: <rpn-ref-time> <seconds>
-alias _seekdemo_calc_target "cl_cmd rpn time $1 \"$2\" add dup /_seekdemo_target exch def le /_seekdemo_time_increasing exch def"
+alias _seekdemo_checkstart "_seekdemo_checkstart_$_seekdemo_demo_is_playing ${* q}"
+alias _seekdemo_checkstart_0 "echo \"no demo currently playing\""
+// start new seek
+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"
// check if seeking forwards or backwards
+alias _seekdemo_start_seek "set _seekdemo_time_increasing err; cl_cmd rpn /_seekdemo_time_increasing _seekdemo_target time ge def; _seekdemo_check_increasing"
alias _seekdemo_check_increasing "_seekdemo_check_increasing_$_seekdemo_time_increasing"
// when seeking backwards, restart the demo and start the reload wait loop
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"
alias _seekdemo_check_increasing_1 "_seekdemo_check_time"
+alias _seekdemo_check_increasing_err "_seekdemo_failed \"(demo not loaded?)\""
alias _seekdemo_check_reloading "_seekdemo_check_reloading_$_seekdemo_reload_success"
alias _seekdemo_check_reloading_0 "_seekdemo_failed (playdemo)"
// launch the "defer 10" command as a fallback to stop waiting in case demo reload fails