Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/Assets/Lua/ButtonCount.lua
2 views
1
--Written by Brandon Evans
2
3
--You can change the position of the text here.
4
local x = 0
5
local y = 36
6
7
--You can blacklist buttons from being recorded here.
8
local blacklist = {'Lag', 'Pause', 'Reset'}
9
10
local data = {}
11
local registered = false
12
local state = {}
13
local states = {}
14
15
function deepcopy(object)
16
local lookup_table = {}
17
local function _copy(object)
18
if type(object) ~= 'table' then
19
return object
20
elseif lookup_table[object] then
21
return lookup_table[object]
22
end
23
local new_table = {}
24
lookup_table[object] = new_table
25
for index, value in pairs(object) do
26
new_table[_copy(index)] = _copy(value)
27
end
28
return setmetatable(new_table, getmetatable(object))
29
end
30
return _copy(object)
31
end
32
33
function counts(obj)
34
gui.text(x, y, 'Pressed: ' .. obj.pressed)
35
gui.text(x, y + 14, 'Inputted: ' .. obj.inputted)
36
end
37
38
function frames()
39
if not movie.isloaded() then
40
console.log('No data loaded from frames')
41
return
42
end
43
reset()
44
--Get data from every frame but this one. This frame's data will be decided
45
--in real-time.
46
for frame = 0, emu.framecount() - 1 do
47
record(movie.getinput(frame))
48
end
49
console.log('Data loaded from frames')
50
counts(data)
51
end
52
53
function load(name)
54
registered = true
55
state = {}
56
if not states[name] then
57
frames()
58
save(name)
59
return
60
end
61
data = deepcopy(states[name])
62
console.log('Data loaded from ' .. name)
63
--Show the data from before the state's frame.
64
local previous = states[name].previous
65
counts(previous)
66
end
67
68
function record(buttons)
69
for button, value in pairs(buttons) do
70
local blacklisted = false
71
for index, name in pairs(blacklist) do
72
if name == button then
73
blacklisted = true
74
end
75
end
76
if value and not blacklisted then
77
data.inputted = data.inputted + 1
78
if not data.buttons[button] then
79
data.pressed = data.pressed + 1
80
end
81
end
82
data.buttons[button] = value
83
end
84
end
85
86
function reset()
87
data.buttons = {}
88
data.pressed = 0
89
data.inputted = 0
90
end
91
92
function save(name)
93
registered = true
94
if next(state) == nil then
95
data.previous = deepcopy(data)
96
--Include the state's frame in the data.
97
record(joypad.get())
98
state = deepcopy(data)
99
end
100
states[name] = deepcopy(state)
101
console.log('Data saved to ' .. name)
102
end
103
104
reset()
105
frames()
106
107
if event.onloadstate then
108
event.onloadstate(load)
109
event.onsavestate(save)
110
end
111
112
while true do
113
--If this is the first frame, reset the data.
114
if emu.framecount() == 0 then
115
reset()
116
end
117
if not registered then
118
record(joypad.get())
119
end
120
registered = false
121
state = {}
122
counts(data)
123
emu.frameadvance()
124
end
125