PokeBot/util/menu.lua

237 lines
4.6 KiB
Lua
Raw Normal View History

2015-04-06 01:18:46 -07:00
local Menu = {}
2014-07-12 18:47:39 -07:00
2015-04-06 01:18:46 -07:00
local Input = require "util.input"
local Memory = require "util.memory"
2014-07-12 18:47:39 -07:00
local yellow = GAME_NAME == "yellow"
2014-07-12 18:47:39 -07:00
local sliding = false
local alternateStart = 0
2014-07-12 18:47:39 -07:00
Menu.pokemon = yellow and 51 or 103
2014-07-12 18:47:39 -07:00
-- Private functions
local function getRow(menuType, scrolls)
if menuType and menuType == "settings" then
2014-07-12 18:47:39 -07:00
menuType = menuType.."_row"
else
menuType = "row"
end
2015-04-06 01:18:46 -07:00
local row = Memory.value("menu", menuType)
if scrolls then
2015-04-06 01:18:46 -07:00
row = row + Memory.value("menu", "scroll_offset")
2014-07-12 18:47:39 -07:00
end
return row
end
local function setRow(desired, throttle, scrolls, menuType, loop)
local currentRow = getRow(menuType, scrolls)
if throttle == "accelerate" then
if sliding then
2014-07-12 18:47:39 -07:00
throttle = false
else
local dist = math.abs(desired - currentRow)
if dist < 15 then
2014-07-12 18:47:39 -07:00
throttle = true
else
throttle = false
sliding = true
end
end
else
sliding = false
end
2015-04-06 01:18:46 -07:00
return Menu.balance(currentRow, desired, true, loop, throttle)
2014-07-12 18:47:39 -07:00
end
local function isCurrently(desired, menuType)
if menuType then
2014-07-12 18:47:39 -07:00
menuType = menuType.."_current"
else
menuType = "current"
end
2015-04-06 01:18:46 -07:00
return Memory.value("menu", menuType) == desired
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
Menu.isCurrently = isCurrently
2014-07-12 18:47:39 -07:00
-- Menu
2015-04-06 01:18:46 -07:00
function Menu.getCol()
return Memory.value("menu", "column")
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Menu.open(desired, atIndex, menuType)
if isCurrently(desired, menuType) then
2014-07-12 18:47:39 -07:00
return true
end
2015-04-06 01:18:46 -07:00
Menu.select(atIndex, false, false, menuType)
2014-07-12 18:47:39 -07:00
return false
end
2015-04-06 01:18:46 -07:00
function Menu.select(option, throttle, scrolls, menuType, dontPress, loop)
if setRow(option, throttle, scrolls, menuType, loop) then
2014-07-12 18:47:39 -07:00
local delay = 1
if throttle then
2014-07-12 18:47:39 -07:00
delay = 2
end
if not dontPress then
2015-04-06 01:18:46 -07:00
Input.press("A", delay)
2014-07-12 18:47:39 -07:00
end
return true
end
end
2015-04-06 01:18:46 -07:00
function Menu.cancel(desired, menuType)
if not isCurrently(desired, menuType) then
2014-07-12 18:47:39 -07:00
return true
end
2015-04-06 01:18:46 -07:00
Input.press("B")
2014-07-12 18:47:39 -07:00
return false
end
-- Selections
2015-04-06 01:18:46 -07:00
function Menu.balance(current, desired, inverted, looping, throttle)
if current == desired then
2014-07-12 18:47:39 -07:00
sliding = false
return true
end
if not throttle then
2014-07-12 18:47:39 -07:00
throttle = 0
else
throttle = 1
end
local goUp = current > desired == inverted
if looping and math.abs(current - desired) > math.floor(looping / 2) then
2014-07-12 18:47:39 -07:00
goUp = not goUp
end
if goUp then
2015-04-06 01:18:46 -07:00
Input.press("Up", throttle)
2014-07-12 18:47:39 -07:00
else
2015-04-06 01:18:46 -07:00
Input.press("Down", throttle)
2014-07-12 18:47:39 -07:00
end
return false
end
2015-04-06 01:18:46 -07:00
function Menu.sidle(current, desired, looping, throttle)
if current == desired then
2014-07-12 18:47:39 -07:00
return true
end
if not throttle then
2014-07-12 18:47:39 -07:00
throttle = 0
else
throttle = 1
end
local goLeft = current > desired
if looping and math.abs(current - desired) > math.floor(looping / 2) then
2014-07-12 18:47:39 -07:00
goLeft = not goLeft
end
if goLeft then
2015-04-06 01:18:46 -07:00
Input.press("Left", throttle)
2014-07-12 18:47:39 -07:00
else
2015-04-06 01:18:46 -07:00
Input.press("Right", throttle)
2014-07-12 18:47:39 -07:00
end
return false
end
2015-04-06 01:18:46 -07:00
function Menu.setCol(desired)
return Menu.sidle(Menu.getCol(), desired)
2014-07-12 18:47:39 -07:00
end
-- Options
2015-04-06 01:18:46 -07:00
function Menu.setOption(name, desired)
if yellow then
2014-07-12 18:47:39 -07:00
local rowFor = {
text_speed = 0,
battle_animation = 1,
battle_style = 2
}
2015-04-06 01:18:46 -07:00
local currentRow = Memory.raw(0x0D3D, true)
if Menu.balance(currentRow, rowFor[name], true, false, true) then
Input.press("Left")
2014-07-12 18:47:39 -07:00
end
else
local rowFor = {
text_speed = 3,
battle_animation = 8,
battle_style = 13
}
2015-04-06 01:18:46 -07:00
if Memory.value("setting", name) == desired then
2014-07-12 18:47:39 -07:00
return true
end
if setRow(rowFor[name], true, false, "settings") then
2015-04-06 01:18:46 -07:00
Menu.setCol(desired)
2014-07-12 18:47:39 -07:00
end
end
return false
end
-- Pause menu
function Menu.hasTextbox()
return Memory.value("battle", "menu") == (yellow and 19 or 95)
end
function Menu.onPokemonSelect(battleMenu)
if not battleMenu then
battleMenu = Memory.value("battle", "menu")
end
if yellow then
battleMenu = battleMenu - 19
end
return battleMenu == 8 or battleMenu == 48 or battleMenu == 184 or battleMenu == 224
end
function Menu.isOpened()
return Memory.value("game", "textbox") == 1
end
function Menu.hasBeenOpened()
local mainMenu = Memory.value("menu", "main")
if mainMenu > 7 then
return true
end
if (Menu.isOpened() or Menu.onPokemonSelect()) and (mainMenu == 0 or mainMenu == 2 or mainMenu == 4 or mainMenu == 6) then
return true
end
if mainMenu > 0 and INTERNAL and not STREAMING_MODE then
p("DMM", mainMenu)
end
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Menu.close()
if not Menu.hasBeenOpened() then
2014-07-12 18:47:39 -07:00
return true
end
2015-04-06 01:18:46 -07:00
Input.press("B")
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Menu.pause()
if Menu.isOpened() then
if Menu.hasTextbox() then
2015-04-06 01:18:46 -07:00
Input.cancel()
else
2015-04-06 01:18:46 -07:00
local main = Memory.value("menu", "main")
if main > 2 and main ~= 64 then
return true
end
2015-04-06 01:18:46 -07:00
Input.press("B")
2014-07-12 18:47:39 -07:00
end
else
if yellow then
alternateStart = alternateStart + 1
if alternateStart > 1 then
if alternateStart > 2 then
alternateStart = 0
end
return false
end
end
2015-04-06 01:18:46 -07:00
Input.press("Start", 2)
2014-07-12 18:47:39 -07:00
end
end
2015-04-06 01:18:46 -07:00
return Menu