PokeBot/util/input.lua

119 lines
2.0 KiB
Lua
Raw Normal View History

2014-07-12 18:47:39 -07:00
local input = {}
local bridge = require "util.bridge"
local memory = require "util.memory"
local lastSend
local currentButton, remainingFrames, setForFrame
local debug
local bCancel = true
local function bridgeButton(btn)
if btn ~= lastSend then
2014-07-12 18:47:39 -07:00
lastSend = btn
bridge.input(btn)
end
end
local function sendButton(button, ab)
local inputTable = {[button] = true}
joypad.set(inputTable)
if debug then
2014-07-12 18:47:39 -07:00
gui.text(0, 7, button.." "..remainingFrames)
end
if ab then
2015-03-29 12:23:29 -07:00
buttonbutton = "A,B"
2014-07-12 18:47:39 -07:00
end
bridgeButton(button)
setForFrame = button
end
function input.press(button, frames)
if setForFrame then
2014-07-12 18:47:39 -07:00
print("ERR: Reassigning "..setForFrame.." to "..button)
return
end
if frames == nil or frames > 0 then
if button == currentButton then
2014-07-12 18:47:39 -07:00
return
end
if not frames then
2014-07-12 18:47:39 -07:00
frames = 1
end
currentButton = button
remainingFrames = frames
else
remainingFrames = 0
end
bCancel = button ~= "B"
sendButton(button)
end
function input.cancel(accept)
if accept and memory.value("menu", "shop_current") == 20 then
2014-07-12 18:47:39 -07:00
input.press(accept)
else
local button
if bCancel then
2014-07-12 18:47:39 -07:00
button = "B"
else
button = "A"
end
remainingFrames = 0
sendButton(button, true)
bCancel = not bCancel
end
end
function input.escape()
local inputTable = {Right=true, Down=true}
joypad.set(inputTable)
2015-03-29 12:23:29 -07:00
bridgeButton("D,R")
2014-07-12 18:47:39 -07:00
end
function input.clear()
currentButton = nil
remainingFrames = -1
end
function input.update()
if currentButton then
2014-07-12 18:47:39 -07:00
remainingFrames = remainingFrames - 1
if remainingFrames >= 0 then
if remainingFrames > 0 then
2014-07-12 18:47:39 -07:00
sendButton(currentButton)
return true
end
else
currentButton = nil
end
end
setForFrame = nil
end
function input.advance()
if not setForFrame then
2014-07-12 18:47:39 -07:00
bridgeButton("e")
end
end
function input.setDebug(enabled)
debug = enabled
end
function input.test(fn, completes)
while true do
if not input.update() then
if fn() and completes then
2014-07-12 18:47:39 -07:00
break
end
end
emu.frameadvance()
end
if completes then
2014-07-12 18:47:39 -07:00
print(completes.." complete!")
end
end
return input