Applied the renames for "minimum buttons pressed", "minimum buttons inputted".

This commit is contained in:
brandonevans 2012-04-25 04:35:27 +00:00
parent 82999aa2a9
commit 32a7970a99
1 changed files with 122 additions and 122 deletions

View File

@ -6,151 +6,151 @@ local controllers = 2
local x = 0 local x = 0
local y = 8 local y = 8
local holds = 0
local players = {} local players = {}
local presses = 0
local states = {} local states = {}
local pressed = 0
local inputted = 0
function table.copy(t) function table.copy(t)
local t2 = {} local t2 = {}
for k, v in pairs(t) do for k, v in pairs(t) do
t2[k] = v t2[k] = v
end end
return t2 return t2
end end
function counts() function counts()
--Display the counts of the holds and the presses. --Display the counts of the buttons pressed and inputted.
gui.text(x, y, 'Holds: ' .. holds) gui.text(x, y, 'Pressed: ' .. pressed)
gui.text(x, y + 8, 'Presses: ' .. presses) gui.text(x, y + 8, 'Inputted: ' .. inputted)
end end
function load(slot) function load(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
if not states[slot] or not states[slot].holds then if not states[slot] or states[slot].inputted == nil then
gui.text(x, y + 16, 'No data loaded from slot ' .. tostring(slot - 1)) gui.text(x, y + 16, 'No data loaded from slot ' .. tostring(slot - 1))
counts() counts()
return return
end end
--Load the data if there is any available for this slot. --Load the data if there is any available for this slot.
holds = states[slot].holds players = table.copy(states[slot].players)
players = table.copy(states[slot].players) pressed = states[slot].pressed
presses = states[slot].presses inputted = states[slot].inputted
gui.text(x, y + 16, 'Data loaded from slot ' .. tostring(slot - 1)) gui.text(x, y + 16, 'Data loaded from slot ' .. tostring(slot - 1))
counts() counts()
end end
function parse() function parse()
--If there is an open, read-only FM2 file, parse it for the initial data. --If there is an open, read-only FM2 file, parse it for the initial data.
if not movie.active() then if not movie.active() then
return false return false
end end
local fh = io.open(movie.name()) local fh = io.open(movie.name())
if not fh or movie.mode() == 'record' or movie.name():match( if not fh or movie.mode() == 'record' or movie.name():match(
'.%.(%w+)$' '.%.(%w+)$'
) ~= 'fm2' then ) ~= 'fm2' then
return false return false
end end
local frame = -1 local frame = -1
local last = {} local last = {}
local match = { local match = {
'right', 'left', 'down', 'up', 'start', 'select', 'B', 'A' 'right', 'left', 'down', 'up', '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() line = fh:read()
if not line then if not line then
break break
end end
--This is only a frame if it starts with a vertical bar. --This is only a frame if it starts with a vertical bar.
if string.sub(line, 0, 1) == '|' then if string.sub(line, 0, 1) == '|' then
frame = frame + 1 frame = frame + 1
players = {} players = {}
local player = -1 local player = -1
--Split up the sections by a vertical bar. --Split up the sections by a vertical bar.
for section in string.gmatch(line, '[^|]+') do for section in string.gmatch(line, '[^|]+') do
player = player + 1 player = player + 1
--Only deal with actual players. --Only deal with actual players.
if player ~= 0 then if player ~= 0 then
local button = 0 local button = 0
--Run through all the buttons. --Run through all the buttons.
for text in string.gmatch(section, '.') do for text in string.gmatch(section, '.') do
button = button + 1 button = button + 1
--Check if this button is pressed. --Check if this button is pressed.
if text ~= ' ' and text ~= '.' then if text ~= ' ' and text ~= '.' then
holds = holds + 1 inputted = inputted + 1
--If the button was not previously pressed, --If the button was not previously pressed,
--increment. --increment.
if table.maxn(last) < player or not last[player][ if table.maxn(last) < player or not last[player][
match[button] match[button]
] then ] then
presses = presses + 1 pressed = pressed + 1
end end
if table.maxn(players) < player then if table.maxn(players) < player then
table.insert(players, {}) table.insert(players, {})
end end
--Mark this button as pressed. --Mark this button as pressed.
players[player][match[button]] = true players[player][match[button]] = true
end end
end end
end end
end end
--Save the players to compare with the next frame in the file. --Save the players to compare with the next frame in the file.
last = players last = players
end end
end end
return true return true
end 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
while table.maxn(states) < slot do while table.maxn(states) < slot do
table.insert(states, {}) table.insert(states, {})
end end
--Mark the current data as the data for this slot. --Mark the current data as the data for this slot.
states[slot].holds = holds states[slot].players = table.copy(players)
states[slot].players = table.copy(players) states[slot].pressed = pressed
states[slot].presses = presses states[slot].inputted = inputted
gui.text(x, y + 16, 'Data saved to slot ' .. tostring(slot - 1)) gui.text(x, y + 16, 'Data saved to slot ' .. tostring(slot - 1))
counts() counts()
end end
if parse() then if parse() then
gui.text(x, y + 16, 'Movie parsed for data') gui.text(x, y + 16, 'Movie parsed for data')
else else
gui.text(x, y + 16, 'No movie parsed for data') gui.text(x, y + 16, 'No movie parsed for data')
end end
if savestate.registerload then if savestate.registerload then
savestate.registerload(load) savestate.registerload(load)
savestate.registersave(save) savestate.registersave(save)
end end
while true do while true do
--If this is the first frame, reset the data. --If this is the first frame, reset the data.
if emu.framecount() == 0 then if emu.framecount() == 0 then
holds = 0 players = {}
players = {} pressed = 0
presses = 0 inputted = 0
end end
--Check players one and two. --Check players one and two.
for player = 1, controllers do for player = 1, controllers do
local buttons = joypad.getdown(player) local buttons = joypad.getdown(player)
--Run through all of the pressed buttons. --Run through all of the pressed buttons.
for i, v in pairs(buttons) do for i, v in pairs(buttons) do
holds = holds + 1 inputted = inputted + 1
--If in the previous frame the button was not pressed, increment. --If in the previous frame the button was not pressed, increment.
if table.maxn(players) >= player and not players[player][i] then if table.maxn(players) >= player and not players[player][i] then
presses = presses + 1 pressed = pressed + 1
end end
end end
if table.maxn(players) < player then if table.maxn(players) < player then
table.insert(players, true) table.insert(players, true)
end end
--Mark these buttons as pressed. --Mark these buttons as pressed.
players[player] = buttons players[player] = buttons
end end
counts() counts()
emu.frameadvance() emu.frameadvance()
end end