PokeBot/util/input.lua

120 lines
2.0 KiB
Lua
Raw Normal View History

2015-04-06 01:18:46 -07:00
local Input = {}
2014-07-12 18:47:39 -07:00
2015-04-06 01:18:46 -07:00
local Bridge = require "util.bridge"
local Memory = require "util.memory"
2015-04-07 19:06:29 -07:00
local Utils = require "util.utils"
2014-07-12 18:47:39 -07:00
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
2015-04-06 01:18:46 -07:00
Bridge.input(btn)
2014-07-12 18:47:39 -07:00
end
end
local function sendButton(button, ab)
local inputTable = {[button] = true}
joypad.set(inputTable)
if debug then
2015-04-07 19:06:29 -07:00
Utils.drawText(0, 7, button.." "..remainingFrames)
2014-07-12 18:47:39 -07:00
end
if ab then
2015-04-13 01:18:28 -07:00
button = "A,B"
2014-07-12 18:47:39 -07:00
end
bridgeButton(button)
setForFrame = button
end
2015-04-06 01:18:46 -07:00
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
2015-04-06 01:18:46 -07:00
function Input.cancel(accept)
if accept and Memory.value("menu", "shop_current") == 20 then
Input.press(accept)
2014-07-12 18:47:39 -07:00
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
2015-04-06 01:18:46 -07:00
function Input.escape()
2014-07-12 18:47:39 -07:00
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
2015-04-06 01:18:46 -07:00
function Input.clear()
2014-07-12 18:47:39 -07:00
currentButton = nil
remainingFrames = -1
end
2015-04-06 01:18:46 -07:00
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
2015-04-06 01:18:46 -07:00
function Input.advance()
if not setForFrame then
2014-07-12 18:47:39 -07:00
bridgeButton("e")
end
end
2015-04-06 01:18:46 -07:00
function Input.setDebug(enabled)
2014-07-12 18:47:39 -07:00
debug = enabled
end
2015-04-06 01:18:46 -07:00
function Input.test(fn, completes)
while true do
2015-04-06 01:18:46 -07:00
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
2015-04-06 01:18:46 -07:00
return Input