From c40e1f929d16cfa88e4a1476c87823f5652f8251 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Fri, 8 Jun 2018 13:48:59 +0200 Subject: [PATCH] fix skipped inputs on lag frames --- _NOTICE | 1 - main.lua | 25 ++++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/_NOTICE b/_NOTICE index d038164..eada196 100644 --- a/_NOTICE +++ b/_NOTICE @@ -5,7 +5,6 @@ however, feel free to copy any snippets of code you find useful. TODOs: (that i can remember right now) - normalize and/or embed sprite type inputs - settle on a network architecture -- fix lag-frames skipped-inputs bug - detect frames when Mario is in a controllable state - compute how many input neurons the network needs instead of hardcoding - rewrite positions to be relative to Mario diff --git a/main.lua b/main.lua index a64a837..04f739e 100644 --- a/main.lua +++ b/main.lua @@ -20,6 +20,7 @@ local mom2max -- running element-wise maximum of mom2. local trial_frames = 0 local total_frames = 0 +local lagless_count = 0 local force_start = false local force_start_old = false @@ -563,6 +564,13 @@ local function prepare_reset() reset = true end +local function draw_framecount(x, y, frames) + local tf0 = frames % 1000 + local tf1 = (frames % 1000000 - tf0) / 1000 + local tf2 = (frames - tf0 - tf1) / 1000000 + gui.text(x, y, ("%03i,%03i,%03i"):format(tf2,tf1,tf0), '#FFFFFF', '#0000003F') +end + local function doit(dummy) local ingame_paused = game.get_state() == "paused" @@ -581,10 +589,7 @@ local function doit(dummy) end end - local tf0 = total_frames % 1000 - local tf1 = (total_frames % 1000000 - tf0) / 1000 - local tf2 = (total_frames - tf0 - tf1) / 1000000 - gui.text(12, 212, ("%03i,%03i,%03i"):format(tf2,tf1,tf0), '#FFFFFF', '#0000003F') + draw_framecount(12, 215, total_frames) screen_scroll_delta = screen_scroll_delta + game.R(0x775) @@ -723,7 +728,10 @@ while true do state_old = game.get_state() end - if reset then do_reset() end + if reset then + do_reset() + lagless_count = 0 + end if not cfg.enable_network then -- infinite time cheat. super handy for testing. @@ -739,13 +747,12 @@ while true do game.W(0x75A, 1) end - -- FIXME: if the game lags then we might miss our frame to change inputs! - -- don't rely on emu.framecount. - local doot = jp == nil or emu.framecount() % cfg.frameskip == 0 + local doot = jp == nil or lagless_count % cfg.frameskip == 0 doit(not doot) -- jp might still be nil if we're not ingame or we're not playing. if jp ~= nil then joypad.write(1, jp) end - game.advance() + game.advance() -- remember, this skips lag frames. + lagless_count = lagless_count + 1 end