PokeBot/main.lua

180 lines
4.4 KiB
Lua
Raw Permalink Normal View History

2015-04-06 01:18:46 -07:00
-- OPTIONS
2014-07-12 18:47:39 -07:00
2015-04-19 01:18:23 -07:00
RESET_FOR_TIME = false -- Set to true if you're trying to break the record, not just finish a run
2015-04-28 00:19:04 -07:00
BEAST_MODE = false -- WARNING: Do not engage. Will yolo everything, and reset at every opportunity in the quest for 1:47.
2014-07-12 18:47:39 -07:00
2015-04-17 18:26:42 -07:00
local CUSTOM_SEED = nil -- Set to a known seed to replay it, or leave nil for random runs
local NIDORAN_NAME = "A" -- Set this to the single character to name Nidoran (note, to replay a seed, it MUST match!)
2015-04-17 18:26:42 -07:00
local PAINT_ON = true -- Display contextual information while the bot runs
2014-07-12 18:47:39 -07:00
-- START CODE (hard hats on)
2014-07-12 18:47:39 -07:00
2015-05-29 11:27:55 -07:00
VERSION = "2.1"
2015-04-24 21:00:33 -07:00
local Data = require "data.data"
Data.init()
2015-04-06 01:18:46 -07:00
local Battle = require "action.battle"
local Textbox = require "action.textbox"
local Walk = require "action.walk"
2014-07-12 18:47:39 -07:00
2015-04-06 01:18:46 -07:00
local Combat = require "ai.combat"
local Control = require "ai.control"
2015-04-24 21:00:33 -07:00
local Strategies = require("ai."..Data.gameName..".strategies")
2014-07-12 18:47:39 -07:00
local Pokemon = require "storage.pokemon"
2015-04-06 01:18:46 -07:00
local Bridge = require "util.bridge"
local Input = require "util.input"
local Memory = require "util.memory"
local Paint = require "util.paint"
local Utils = require "util.utils"
local Settings = require "util.settings"
2014-07-12 18:47:39 -07:00
local hasAlreadyStartedPlaying = false
2015-04-10 01:14:27 -07:00
local oldSeconds
2014-07-12 18:47:39 -07:00
local running = true
2015-04-28 10:00:27 -07:00
local previousMap
2014-07-12 18:47:39 -07:00
2015-04-07 19:06:29 -07:00
-- HELPERS
2014-07-12 18:47:39 -07:00
local function resetAll()
2015-04-06 01:18:46 -07:00
Strategies.softReset()
Combat.reset()
Control.reset()
Walk.reset()
Paint.reset()
Bridge.reset()
2015-04-10 01:14:27 -07:00
oldSeconds = 0
2014-07-12 18:47:39 -07:00
running = false
if CUSTOM_SEED then
2015-04-17 13:40:56 -07:00
Data.run.seed = CUSTOM_SEED
2015-04-11 10:48:12 -07:00
Strategies.replay = true
2015-04-17 13:40:56 -07:00
p("RUNNING WITH A FIXED SEED ("..NIDORAN_NAME.." "..Data.run.seed.."), every run will play out identically!", true)
2014-07-12 18:47:39 -07:00
else
2015-04-17 13:40:56 -07:00
Data.run.seed = os.time()
2015-04-28 00:19:04 -07:00
print("PokeBot v"..VERSION..": "..(BEAST_MODE and "BEAST MODE seed" or "starting a new run with seed").." "..Data.run.seed)
2014-07-12 18:47:39 -07:00
end
2015-04-17 13:40:56 -07:00
math.randomseed(Data.run.seed)
2014-07-12 18:47:39 -07:00
end
2015-04-06 00:50:00 -07:00
-- EXECUTE
2015-05-04 19:38:27 -07:00
p("Welcome to PokeBot "..Utils.capitalize(Data.gameName).." version "..VERSION, true)
2015-04-07 19:06:29 -07:00
2015-04-06 01:18:46 -07:00
Control.init()
2014-07-12 18:47:39 -07:00
STREAMING_MODE = not Walk.init() and INTERNAL
2015-04-29 11:39:14 -07:00
if not INTERNAL or CUSTOM_SEED then
2014-07-12 18:47:39 -07:00
client.reboot_core()
else
2015-04-06 01:18:46 -07:00
hasAlreadyStartedPlaying = Utils.ingame()
2014-07-12 18:47:39 -07:00
end
if STREAMING_MODE then
if not CUSTOM_SEED then
2015-04-16 12:34:13 -07:00
RESET_FOR_TIME = true
end
Bridge.init(Data.gameName)
2015-04-29 11:39:14 -07:00
elseif BEAST_MODE then
RESET_FOR_TIME = true
else
if hasAlreadyStartedPlaying and RESET_FOR_TIME then
RESET_FOR_TIME = false
p("Disabling time-limit resets as the game is already running. Please reset the emulator and restart the script if you'd like to go for a fast time.", true)
end
if PAINT_ON then
Input.setDebug(true)
end
2014-07-12 18:47:39 -07:00
end
2015-04-22 23:00:28 -07:00
Strategies.init(hasAlreadyStartedPlaying)
2015-04-28 10:00:27 -07:00
-- LOOP
2015-04-28 10:00:27 -07:00
local function generateNextInput(currentMap)
if not Utils.ingame() then
if currentMap == 0 then
if running then
if not hasAlreadyStartedPlaying then
2015-05-30 13:14:10 -07:00
if emu.framecount() ~= 1 then client.reboot_core() end
2015-04-28 10:00:27 -07:00
hasAlreadyStartedPlaying = true
else
resetAll()
end
else
2015-05-25 12:42:07 -07:00
Settings.startNewAdventure()
2015-04-28 10:00:27 -07:00
end
else
if not running then
Bridge.liveSplit()
running = true
end
Settings.choosePlayerNames()
end
else
local battleState = Memory.value("game", "battle")
Control.encounter(battleState)
local curr_hp = Combat.hp()
2015-05-01 11:10:37 -07:00
Combat.updateHP(curr_hp)
2015-04-28 10:00:27 -07:00
if curr_hp == 0 and not Control.canDie() and Pokemon.index(0) > 0 then
Strategies.death(currentMap)
elseif Walk.strategy then
if Strategies.execute(Walk.strategy) then
if Walk.traverse(currentMap) == false then
return generateNextInput(currentMap)
end
end
elseif battleState > 0 then
2015-05-25 12:42:07 -07:00
if not Control.shouldCatch() then
2015-04-28 10:00:27 -07:00
Battle.automate()
end
elseif Textbox.handle() then
if Walk.traverse(currentMap) == false then
return generateNextInput(currentMap)
end
end
end
end
2014-07-12 18:47:39 -07:00
while true do
2015-04-06 01:18:46 -07:00
local currentMap = Memory.value("game", "map")
if currentMap ~= previousMap then
2015-04-06 01:18:46 -07:00
Input.clear()
2014-07-12 18:47:39 -07:00
previousMap = currentMap
end
2015-04-06 01:18:46 -07:00
if Strategies.frames then
if Memory.value("game", "battle") == 0 then
Strategies.frames = Strategies.frames + 1
end
2015-04-07 19:06:29 -07:00
Utils.drawText(0, 80, Strategies.frames)
end
2015-04-06 01:18:46 -07:00
if Bridge.polling then
Settings.pollForResponse(NIDORAN_NAME)
end
2015-04-06 01:18:46 -07:00
if not Input.update() then
2015-04-28 10:00:27 -07:00
generateNextInput(currentMap)
2014-07-12 18:47:39 -07:00
end
if STREAMING_MODE then
2015-04-10 01:14:27 -07:00
local newSeconds = Memory.value("time", "seconds")
if newSeconds ~= oldSeconds and (newSeconds > 0 or Memory.value("time", "frames") > 0) then
2015-04-06 01:18:46 -07:00
Bridge.time(Utils.elapsedTime())
2015-04-10 01:14:27 -07:00
oldSeconds = newSeconds
end
elseif PAINT_ON then
2015-04-06 01:18:46 -07:00
Paint.draw(currentMap)
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
Input.advance()
2014-07-12 18:47:39 -07:00
emu.frameadvance()
end
2015-04-06 01:18:46 -07:00
Bridge.close()