-Used the new movie.getinput() function to simplify parse(), now frames().

--TODO: Disallow Reset, Lag, and other non-buttons. Pause?
--If this works like adelikat says, my missing load state idea will be possible.
-Made the loading information show up on the console instead of the gui.

Note: Still need savestate.registersave/load!
This commit is contained in:
brandman211 2012-03-27 21:11:34 +00:00
parent 0330e1f742
commit be807c3def
1 changed files with 35 additions and 67 deletions

View File

@ -4,9 +4,9 @@
local x = 0 local x = 0
local y = 36 local y = 36
local holds = 0 local holds
local pressed = {} local pressed
local presses = 0 local presses
local states = {} local states = {}
function table.copy(t) function table.copy(t)
@ -35,67 +35,46 @@ function load(slot)
holds = states[slot].holds holds = states[slot].holds
pressed = table.copy(states[slot].pressed) pressed = table.copy(states[slot].pressed)
presses = states[slot].presses presses = states[slot].presses
gui.text(x, y + 28, 'Data loaded from slot ' .. tostring(slot - 1)) console.output('Data loaded from slot ' .. tostring(slot - 1))
counts() counts()
end end
function parse() function record(buttons)
--Run through all of the pressed buttons.
for button, value in pairs(buttons) do
if value then
holds = holds + 1
--If in the previous frame the button was not pressed, increment.
if not pressed[button] then
presses = presses + 1
end
end
--Mark this button as pressed or not pressed.
pressed[button] = value
end
end
function frames()
--If there is an open, read-only TAS file, parse it for the initial data. --If there is an open, read-only TAS file, parse it for the initial data.
if not movie.isloaded() then if not movie.isloaded() then
return false return false
end end
local fh = io.open(movie.filename())
if not fh or movie.mode() == 'record' or movie.filename():match(
'.%.(%w+)$'
) ~= 'tas' then
return false
end
local frame = -1 local frame = -1
local last = {} reset()
local match = {
'Up', 'Down', 'Left', 'Right', 'Start', 'Select', 'B', 'A'
}
--Parse up until two frames before the current one. --Parse up until two frames before the current one.
while frame ~= emu.framecount() - 2 do while frame ~= emu.framecount() - 2 do
line = fh:read() record(movie.getinput(frame))
if not line then
break
end
--This is only a frame if it starts with a vertical bar.
if string.sub(line, 0, 1) == '|' then
frame = frame + 1 frame = frame + 1
local player = -1
--Split up the sections by a vertical bar.
for section in string.gmatch(line, '[^|]+') do
player = player + 1
--Only deal with actual players.
if player ~= 0 then
local button = 0
--Run through all the buttons.
for text in string.gmatch(section, '.') do
button = button + 1
local name = 'P' .. player .. ' ' .. match[button]
local pressed = false
--Check if this button is pressed.
if text ~= ' ' and text ~= '.' then
holds = holds + 1
--If the button was not previously pressed,
--increment.
if not last[name] then
presses = presses + 1
end
pressed = true
end
----Mark this button as pressed or not pressed.
last[name] = pressed
end
end
end
end
end end
return true return true
end end
function reset()
holds = 0
pressed = {}
presses = 0
end
function save(slot) function save(slot)
--As Lua starts counting from 1, and there may be a slot 0, increment. --As Lua starts counting from 1, and there may be a slot 0, increment.
slot = slot + 1 slot = slot + 1
@ -106,14 +85,15 @@ function save(slot)
states[slot].holds = holds states[slot].holds = holds
states[slot].buttons = table.copy(buttons) states[slot].buttons = table.copy(buttons)
states[slot].presses = presses states[slot].presses = presses
gui.text(x, y + 28, 'Data saved to slot ' .. tostring(slot - 1)) console.output('Data saved to slot ' .. tostring(slot - 1))
counts() counts()
end end
if parse() then reset()
gui.text(x, y + 28, 'Movie parsed for data') if frames() then
console.output('Data loaded from frames')
else else
gui.text(x, y + 28, 'No movie parsed for data') console.output('No data loaded from frames')
end end
if savestate.registerload then if savestate.registerload then
savestate.registerload(load) savestate.registerload(load)
@ -127,19 +107,7 @@ while true do
pressed = {} pressed = {}
presses = 0 presses = 0
end end
local buttons = joypad.get() record(joypad.get())
--Run through all of the pressed buttons.
for button, value in pairs(buttons) do
if value then
holds = holds + 1
--If in the previous frame the button was not pressed, increment.
if not pressed[button] then
presses = presses + 1
end
end
--Mark this button as pressed or not pressed.
pressed[button] = value
end
counts() counts()
emu.frameadvance() emu.frameadvance()
end end