From 524dfed08a13a3e59e315e34ffa4156c2b08e11c Mon Sep 17 00:00:00 2001 From: Michael J Harvey Date: Tue, 24 Aug 2021 14:13:49 -0600 Subject: [PATCH 1/3] Base game swap timer on realtime --- shuffler-src/setupform.lua | 3 ++- shuffler.lua | 46 ++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/shuffler-src/setupform.lua b/shuffler-src/setupform.lua index 71e253e..e22ee4d 100644 --- a/shuffler-src/setupform.lua +++ b/shuffler-src/setupform.lua @@ -297,7 +297,8 @@ function module.initial_setup(callback) -- internal information for output config.frame_count = 0 config.total_swaps = 0 - config.game_frame_count = {} + config.total_time = 0 + config.game_second_count = {} config.game_swaps = {} end diff --git a/shuffler.lua b/shuffler.lua index 5c3b78a..f9b94c9 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -6,7 +6,7 @@ --]] config = {} -next_swap_time = 0 +total_time_limit = 0 running = true plugins = {} @@ -24,6 +24,8 @@ MIN_BIZHAWK_VERSION = "2.6.1" RECOMMENDED_LUA_CORE = "LuaInterface" MAX_INTEGER = 99999999 +local gettime = require("socket.core").gettime + -- check if folder exists function path_exists(p) local ok, err, code = os.rename(p, p) @@ -247,6 +249,9 @@ end function swap_game(next_game) -- if a swap has already happened, don't call again if not running then return false end + + -- set the time for the next game to swap + update_next_swap_time() -- if no game provided, call get_next_game() next_game = next_game or get_next_game() @@ -255,7 +260,6 @@ function swap_game(next_game) -- (you might think we should just disable the timer at this point, but this -- allows new games to be added mid-run without the timer being disabled) if next_game == config.current_game then - update_next_swap_time() return false end @@ -295,14 +299,15 @@ function swap_game(next_game) return load_game(config.current_game) end -function swap_game_delay(f) - next_swap_time = config.frame_count + f +function swap_game_delay(seconds) + config.total_time_limit = config.total_time_limit + seconds end function update_next_swap_time() - next_swap_time = math.huge -- infinity if config.auto_shuffle then - swap_game_delay(math.random(config.min_swap * 60, config.max_swap * 60)) + swap_game_delay(math.random(config.min_swap * 100, config.max_swap * 100) / 100) + else + config.total_time_limit = math.huge -- infinity end end @@ -348,11 +353,7 @@ local function check_lua_core() end end --- this is going to be an APPROXIMATION and is not a substitute for an actual --- timer. games do not run at a consistent or exact 60 fps, so this method is --- provided purely for entertainment purposes -function frames_to_time(f) - local sec = math.floor(f / 60) +function seconds_to_time(sec) local min = math.floor(sec / 60) local hrs = math.floor(min / 60) return string.format('%02d:%02d:%02d', hrs, min%60, sec%60) @@ -432,6 +433,9 @@ function complete_setup() return end + config.total_time_limit = 0 + config.initial_time = gettime() + save_config(config, 'shuffler-src/config.lua') math.randomseed(config.nseed or config.seed) @@ -493,7 +497,6 @@ if emu.getsystemid() ~= "NULL" then end math.randomseed(config.nseed or config.seed) - update_next_swap_time() for _,plugin in ipairs(plugins) do if plugin.on_game_load ~= nil then @@ -516,6 +519,7 @@ end prev_input = input.get() frames_since_restart = 0 +last_time = gettime() while true do if emu.getsystemid() ~= "NULL" and running then -- wait for a frame to pass before turning sound back on @@ -525,13 +529,21 @@ while true do config.frame_count = frame_count frames_since_restart = frames_since_restart + 1 + local time = gettime() + local time_difference = time - last_time + last_time = time + + local total_time_since_start = time - config.initial_time + local total_time = config.total_time + time_difference + config.total_time = total_time + -- update the frame count specifically for the active game as well - local cgf = (config.game_frame_count[config.current_game] or 0) + 1 - config.game_frame_count[config.current_game] = cgf + local current_game_seconds = (config.game_second_count[config.current_game] or 0) + time_difference + config.game_second_count[config.current_game] = current_game_seconds -- save time info to files for OBS display - write_data('output-info/total-time.txt', frames_to_time(frame_count)) - write_data('output-info/current-time.txt', frames_to_time(cgf)) + write_data('output-info/total-time.txt', seconds_to_time(total_time)) + write_data('output-info/current-time.txt', seconds_to_time(current_game_seconds)) -- let plugins do operations each frame for _,plugin in ipairs(plugins) do @@ -552,7 +564,7 @@ while true do if input_rise[config.hk_complete] and frames_since_restart > math.min(3, config.min_swap/2) * 60 then mark_complete() end -- time to swap! - if frame_count >= next_swap_time then swap_game() end + if total_time_since_start >= config.total_time_limit then swap_game() end end emu.frameadvance() From 247ff6b2bc54f1d3388b78b1aef95d2a34997e5e Mon Sep 17 00:00:00 2001 From: Michael J Harvey Date: Tue, 24 Aug 2021 14:13:49 -0600 Subject: [PATCH 2/3] Base game swap timer on realtime --- shuffler-src/setupform.lua | 3 ++- shuffler.lua | 49 ++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/shuffler-src/setupform.lua b/shuffler-src/setupform.lua index 722ca3a..4485c7a 100644 --- a/shuffler-src/setupform.lua +++ b/shuffler-src/setupform.lua @@ -298,7 +298,8 @@ function module.initial_setup(callback) -- internal information for output config.frame_count = 0 config.total_swaps = 0 - config.game_frame_count = {} + config.total_time = 0 + config.game_second_count = {} config.game_swaps = {} end diff --git a/shuffler.lua b/shuffler.lua index e2001c1..88ba0d1 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -6,7 +6,7 @@ --]] config = {} -next_swap_time = 0 +total_time_limit = 0 running = true plugins = {} @@ -24,6 +24,8 @@ MIN_BIZHAWK_VERSION = "2.6.1" RECOMMENDED_LUA_CORE = "LuaInterface" MAX_INTEGER = 99999999 +local gettime = require("socket.core").gettime + function log_message(msg, quiet) if not quiet then print(msg) end @@ -270,6 +272,9 @@ end function swap_game(next_game) -- if a swap has already happened, don't call again if not running then return false end + + -- set the time for the next game to swap + update_next_swap_time() -- if no game provided, call get_next_game() next_game = next_game or get_next_game() @@ -278,7 +283,6 @@ function swap_game(next_game) -- (you might think we should just disable the timer at this point, but this -- allows new games to be added mid-run without the timer being disabled) if next_game == config.current_game then - update_next_swap_time() return false end @@ -318,14 +322,15 @@ function swap_game(next_game) return load_game(config.current_game) end -function swap_game_delay(f) - next_swap_time = config.frame_count + f +function swap_game_delay(seconds) + config.total_time_limit = config.total_time_limit + seconds end function update_next_swap_time() - next_swap_time = math.huge -- infinity if config.auto_shuffle then - swap_game_delay(math.random(config.min_swap * 60, config.max_swap * 60)) + swap_game_delay(math.random(config.min_swap * 100, config.max_swap * 100) / 100) + else + config.total_time_limit = math.huge -- infinity end end @@ -371,11 +376,7 @@ local function check_lua_core() end end --- this is going to be an APPROXIMATION and is not a substitute for an actual --- timer. games do not run at a consistent or exact 60 fps, so this method is --- provided purely for entertainment purposes -function frames_to_time(f) - local sec = math.floor(f / 60) +function seconds_to_time(sec) local min = math.floor(sec / 60) local hrs = math.floor(min / 60) return string.format('%02d:%02d:%02d', hrs, min%60, sec%60) @@ -463,6 +464,9 @@ function complete_setup() for _,game in ipairs(games) do log_message('GAME FOUND: ' .. game, true) end + + config.total_time_limit = 0 + config.initial_time = gettime() save_config(config, 'shuffler-src/config.lua') math.randomseed(config.nseed or config.seed) @@ -536,7 +540,6 @@ if emu.getsystemid() ~= "NULL" then end math.randomseed(config.nseed or config.seed) - update_next_swap_time() for _,plugin in ipairs(plugins) do if plugin.on_game_load ~= nil then @@ -562,6 +565,9 @@ frames_since_restart = 0 local ptime_total = nil local ptime_game = nil + +last_time = gettime() + while true do if emu.getsystemid() ~= "NULL" and running then -- wait for a frame to pass before turning sound back on @@ -571,25 +577,32 @@ while true do config.frame_count = frame_count frames_since_restart = frames_since_restart + 1 + local time = gettime() + local time_difference = time - last_time + last_time = time + + local total_time_since_start = time - config.initial_time + local total_time = config.total_time + time_difference + config.total_time = total_time + -- update the frame count specifically for the active game as well - local cgf = (config.game_frame_count[config.current_game] or 0) + 1 - config.game_frame_count[config.current_game] = cgf + local current_game_seconds = (config.game_second_count[config.current_game] or 0) + time_difference + config.game_second_count[config.current_game] = current_game_seconds -- save time info to files for OBS display if config.output_timers then - local time_total = frames_to_time(frame_count) + local time_total = seconds_to_time(total_time) if time_total ~= ptime_total then write_data('output-info/total-time.txt', time_total) ptime_total = time_total end - local time_game = frames_to_time(cgf) + local time_game = seconds_to_time(current_game_seconds) if time_game ~= ptime_game then write_data('output-info/current-time.txt', time_game) ptime_game = time_game end end - -- let plugins do operations each frame for _,plugin in ipairs(plugins) do if plugin.on_frame ~= nil then @@ -609,7 +622,7 @@ while true do if input_rise[config.hk_complete] and frames_since_restart > math.min(3, config.min_swap/2) * 60 then mark_complete() end -- time to swap! - if frame_count >= next_swap_time then swap_game() end + if total_time_since_start >= config.total_time_limit then swap_game() end end emu.frameadvance() From fd657c05ebf335563ae1416e25349734ee7fa424 Mon Sep 17 00:00:00 2001 From: Michael J Harvey Date: Sat, 4 Sep 2021 14:40:37 -0600 Subject: [PATCH 3/3] Fixed seed desync issue --- shuffler.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shuffler.lua b/shuffler.lua index 88ba0d1..667c617 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -315,7 +315,7 @@ function swap_game(next_game) end -- save an updated randomizer seed - config.nseed = math.random(MAX_INTEGER) + config.frame_count + config.nseed = math.random(MAX_INTEGER) save_config(config, 'shuffler-src/config.lua') -- load the new game WHICH IS JUST GOING TO RESTART THE WHOLE SCRIPT f***