fix skipped inputs on lag frames

This commit is contained in:
Connor Olding 2018-06-08 13:48:59 +02:00
parent 912e114efe
commit c40e1f929d
2 changed files with 16 additions and 10 deletions

View file

@ -5,7 +5,6 @@ however, feel free to copy any snippets of code you find useful.
TODOs: (that i can remember right now) TODOs: (that i can remember right now)
- normalize and/or embed sprite type inputs - normalize and/or embed sprite type inputs
- settle on a network architecture - settle on a network architecture
- fix lag-frames skipped-inputs bug
- detect frames when Mario is in a controllable state - detect frames when Mario is in a controllable state
- compute how many input neurons the network needs instead of hardcoding - compute how many input neurons the network needs instead of hardcoding
- rewrite positions to be relative to Mario - rewrite positions to be relative to Mario

View file

@ -20,6 +20,7 @@ local mom2max -- running element-wise maximum of mom2.
local trial_frames = 0 local trial_frames = 0
local total_frames = 0 local total_frames = 0
local lagless_count = 0
local force_start = false local force_start = false
local force_start_old = false local force_start_old = false
@ -563,6 +564,13 @@ local function prepare_reset()
reset = true reset = true
end 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 function doit(dummy)
local ingame_paused = game.get_state() == "paused" local ingame_paused = game.get_state() == "paused"
@ -581,10 +589,7 @@ local function doit(dummy)
end end
end end
local tf0 = total_frames % 1000 draw_framecount(12, 215, total_frames)
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')
screen_scroll_delta = screen_scroll_delta + game.R(0x775) screen_scroll_delta = screen_scroll_delta + game.R(0x775)
@ -723,7 +728,10 @@ while true do
state_old = game.get_state() state_old = game.get_state()
end end
if reset then do_reset() end if reset then
do_reset()
lagless_count = 0
end
if not cfg.enable_network then if not cfg.enable_network then
-- infinite time cheat. super handy for testing. -- infinite time cheat. super handy for testing.
@ -739,13 +747,12 @@ while true do
game.W(0x75A, 1) game.W(0x75A, 1)
end end
-- FIXME: if the game lags then we might miss our frame to change inputs! local doot = jp == nil or lagless_count % cfg.frameskip == 0
-- don't rely on emu.framecount.
local doot = jp == nil or emu.framecount() % cfg.frameskip == 0
doit(not doot) doit(not doot)
-- jp might still be nil if we're not ingame or we're not playing. -- jp might still be nil if we're not ingame or we're not playing.
if jp ~= nil then joypad.write(1, jp) end if jp ~= nil then joypad.write(1, jp) end
game.advance() game.advance() -- remember, this skips lag frames.
lagless_count = lagless_count + 1
end end