PokeBot/action/walk.lua

160 lines
3.0 KiB
Lua
Raw Normal View History

2015-04-06 01:18:46 -07:00
local Walk = {}
2014-07-12 18:47:39 -07:00
2015-04-06 01:18:46 -07:00
local Control = require "ai.control"
2015-04-05 18:31:22 -07:00
local paths = require("data."..GAME_NAME..".paths")
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"
local Player = require "util.player"
2014-07-12 18:47:39 -07:00
2015-04-06 01:18:46 -07:00
local Pokemon = require "storage.pokemon"
2014-07-12 18:47:39 -07:00
local path, stepIdx, currentMap
local pathIdx = 0
local customIdx = 1
local customDir = 1
-- Private functions
local function setPath(index, region)
pathIdx = index
stepIdx = 2
currentMap = region
path = paths[index]
end
local function completeStep(region)
stepIdx = stepIdx + 1
2015-04-06 01:18:46 -07:00
return Walk.traverse(region)
end
2014-07-12 18:47:39 -07:00
-- Helper functions
function dir(px, py, dx, dy)
local direction
if py > dy then
2014-07-12 18:47:39 -07:00
direction = "Up"
elseif py < dy then
2014-07-12 18:47:39 -07:00
direction = "Down"
elseif px > dx then
2014-07-12 18:47:39 -07:00
direction = "Left"
else
direction = "Right"
end
return direction
end
2015-04-06 01:18:46 -07:00
Walk.dir = dir
2014-07-12 18:47:39 -07:00
function step(dx, dy)
2015-04-06 01:18:46 -07:00
local px, py = Player.position()
if px == dx and py == dy then
2014-07-12 18:47:39 -07:00
return true
end
2015-04-06 01:18:46 -07:00
Input.press(dir(px, py, dx, dy), 0)
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
Walk.step = step
2014-07-12 18:47:39 -07:00
-- Table functions
2015-04-06 01:18:46 -07:00
function Walk.reset()
2014-07-12 18:47:39 -07:00
path = nil
pathIdx = 0
customIdx = 1
customDir = 1
currentMap = nil
2015-04-06 01:18:46 -07:00
Walk.strategy = nil
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Walk.init()
local region = Memory.value("game", "map")
local px, py = Player.position()
if region == 0 and px == 0 and py == 0 then
2014-07-12 18:47:39 -07:00
return false
end
for tries=1,2 do
for i,p in ipairs(paths) do
if i > 2 and p[1] == region then
2014-07-12 18:47:39 -07:00
local origin = p[2]
if tries == 2 or (origin[1] == px and origin[2] == py) then
2014-07-12 18:47:39 -07:00
setPath(i, region)
return tries == 1
end
end
end
end
end
2015-04-06 01:18:46 -07:00
function Walk.traverse(region)
2014-07-12 18:47:39 -07:00
local newIndex
if not path or currentMap ~= region then
2015-04-06 01:18:46 -07:00
Walk.strategy = nil
2014-07-12 18:47:39 -07:00
setPath(pathIdx + 1, region)
newIndex = pathIdx
customIdx = 1
customDir = 1
elseif stepIdx > #path then
return
end
local tile = path[stepIdx]
if tile.c then
2015-04-06 01:18:46 -07:00
Control.set(tile)
2014-07-12 18:47:39 -07:00
return completeStep(region)
end
if tile.s then
2015-04-06 01:18:46 -07:00
if Walk.strategy then
Walk.strategy = nil
2014-07-12 18:47:39 -07:00
return completeStep(region)
end
2015-04-06 01:18:46 -07:00
Walk.strategy = tile
2014-07-12 18:47:39 -07:00
elseif step(tile[1], tile[2]) then
2015-04-06 01:18:46 -07:00
Pokemon.updateParty()
2014-07-12 18:47:39 -07:00
return completeStep(region)
end
return newIndex
end
2015-04-06 01:18:46 -07:00
function Walk.canMove()
return Memory.value("player", "moving") == 0 and Memory.value("player", "fighting") == 0
2014-07-12 18:47:39 -07:00
end
-- Custom path
2015-04-06 01:18:46 -07:00
function Walk.invertCustom(silent)
if not silent then
2014-07-12 18:47:39 -07:00
customIdx = customIdx + customDir
end
customDir = customDir * -1
end
2015-04-06 01:18:46 -07:00
function Walk.custom(cpath, increment)
if not cpath then
2014-07-12 18:47:39 -07:00
customIdx = 1
customDir = 1
return
end
if increment then
2014-07-12 18:47:39 -07:00
customIdx = customIdx + customDir
end
local tile = cpath[customIdx]
if not tile then
if customIdx < 1 then
2014-07-12 18:47:39 -07:00
customIdx = #cpath
else
customIdx = 1
end
return customIdx
end
local t1, t2 = tile[1], tile[2]
if t2 == nil then
2015-04-06 01:18:46 -07:00
if Player.face(t1) then
Input.press("A", 2)
2014-07-12 18:47:39 -07:00
end
return t1
end
if step(t1, t2) then
2014-07-12 18:47:39 -07:00
customIdx = customIdx + customDir
end
end
2015-04-06 01:18:46 -07:00
return Walk