Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base game swap timer on realtime #31

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion shuffler-src/setupform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 32 additions & 19 deletions shuffler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
--]]

config = {}
next_swap_time = 0
total_time_limit = 0
running = true
plugins = {}

Expand All @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand Down Expand Up @@ -311,21 +315,22 @@ 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***
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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down