-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavicaptureTREE2.lua
More file actions
98 lines (79 loc) · 3.06 KB
/
avicaptureTREE2.lua
File metadata and controls
98 lines (79 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
-- avicaptureTREE.lua - Captures the screen by a set amount of frames
-- https://wade7wastaken.github.io/MupenLuaDoc/#aviStartcapture
-- require("lua.misc.Utils") Optimizing without libraries
---@diagnostic disable: undefined-global, lowercase-global
printf = function(s, ...) print(string.format(s, ...)) end
errorf = function(s, ...) error(string.format(s, ...)) end
local actionDuration = 20
local inputs = {} -- [UP, DOWN, LEFT, RIGHT] as bits (e.g: [1, 0, 0, 1] = upright)
local file = "videos/whomp1/videos/frames.avi"
local savePath = "videos/whomp1/luacache/outcastle"
local inputsFile = io.open("videos/whomp1/videos/inputs.txt", "w")
if not inputsFile then error("file error") return end
inputsFile:write("")
inputsFile:close()
inputsFile = io.open("videos/whomp1/videos/inputs.txt", "a")
if not inputsFile then error("file error") return end
local function randomAction()
local r = math.random(5)
if r == 1 then return "UP", { 1, 0, 0, 0 } end -- up
if r == 2 then return "DOWN", { 0, 1, 0, 0 } end -- down
if r == 3 then return "LEFT", { 0, 0, 1, 0 } end -- left
if r == 4 then return "RIGHT", { 0, 0, 0, 1 } end -- right
return "NONE", { 0, 0, 0, 0 }
end
local function actionToJoypad(bits)
return {
X = ((bits[4] == 1) and 127 or ((bits[3] == 1) and -127 or 0)),
Y = ((bits[1] == 1) and 127 or ((bits[2] == 1) and -127 or 0))
}
end
-- main
print("Going to capture until 'up' is pressed")
print("Unpause to begin")
emu.pause(false) -- opposite
savestate.savefile(savePath .. ".st1")
-- update
local recording = false
local realTime = 0
local f = 0
local actionName, action = randomAction()
local lastSavestate = savePath .. ".st1" -- const!
emu.atinput(function()
if not emu.getpause() and not recording then
recording = true
avi.startcapture(file)
realTime = os.time()
print("Recording started.")
end
if not recording then return end
table.insert(inputs, action)
inputsFile:write(table.concat(action, ",") .. "\n")
joypad.set(actionToJoypad(action))
if joypad.get().up then
avi.stopcapture()
inputsFile:close()
local fInSecs = f / 30 -- float
local diffReal = (os.time() - realTime) -- int
emu.pause(false) -- opposite
errorf("A video was made with %d frames, for a total of %dh,%dm,%.2fs simulated time (real time: %dh,%dm,%ds)", f, fInSecs // 3600, fInSecs // 60, fInSecs, diffReal // 3600, diffReal // 60, diffReal)
-- just in case...
recording = false
return
end
if f > actionDuration then
f = 0
actionName, action = randomAction()
-- 40% to save state
if math.random() < 0.4 then
savestate.savefile(lastSavestate) -- reuse
end
-- 30% to load main state
if math.random() < 0.3 then
savestate.loadfile(lastSavestate)
end
print("Next dataset (Action: " .. actionName .. ")")
return
end
f = f + 1
end)