PokeBot/main.lua

171 lines
4.2 KiB
Lua
Raw Normal View History

2015-04-06 01:18:46 -07:00
-- OPTIONS
2014-07-12 18:47:39 -07:00
2015-04-15 13:40:24 -07:00
RESET_FOR_TIME = false -- Set to false if you just want to see the bot finish a run
2014-07-12 18:47:39 -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!)
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-04-15 13:40:24 -07:00
VERSION = "1.4.3"
YELLOW = memory.getcurrentmemorydomainsize() > 30000
GAME_NAME = YELLOW and "yellow" or "red"
local START_WAIT = 99
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"
local Strategies = require("ai."..GAME_NAME..".strategies")
2014-07-12 18:47:39 -07:00
2015-04-06 01:18:46 -07:00
local Bridge = require "util.bridge"
local Input = require "util.input"
local Memory = require "util.memory"
local Menu = require "util.menu"
local Paint = require "util.paint"
local Utils = require "util.utils"
local Settings = require "util.settings"
2014-07-12 18:47:39 -07:00
2015-04-17 13:40:56 -07:00
local Data = require "data.data"
2015-04-06 01:18:46 -07:00
local Pokemon = require "storage.pokemon"
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
local lastHP
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()
print("PokeBot v"..VERSION..": 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-04-07 19:06:29 -07:00
p("Welcome to PokeBot "..GAME_NAME.." version "..VERSION, true)
2015-04-06 01:18:46 -07:00
Control.init()
2014-07-12 18:47:39 -07:00
2015-04-06 01:18:46 -07:00
STREAMING_MODE = not Walk.init()
if 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
2015-04-06 01:18:46 -07:00
Strategies.init(hasAlreadyStartedPlaying)
if RESET_FOR_TIME and hasAlreadyStartedPlaying then
2014-07-12 18:47:39 -07:00
RESET_FOR_TIME = false
2015-04-07 19:06:29 -07:00
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)
2014-07-12 18:47:39 -07:00
end
if STREAMING_MODE then
2015-04-16 12:34:13 -07:00
if not CUSTOM_SEED and INTERNAL then
RESET_FOR_TIME = true
end
2015-04-06 01:18:46 -07:00
Bridge.init()
2014-07-12 18:47:39 -07:00
else
2015-04-06 01:18:46 -07:00
Input.setDebug(true)
2014-07-12 18:47:39 -07:00
end
-- Main loop
2014-07-12 18:47:39 -07:00
local previousMap
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
if not Utils.ingame() then
if currentMap == 0 then
if running then
if not hasAlreadyStartedPlaying then
2014-07-12 18:47:39 -07:00
client.reboot_core()
hasAlreadyStartedPlaying = true
else
resetAll()
end
else
2015-04-08 01:51:38 -07:00
Settings.startNewAdventure(START_WAIT)
2014-07-12 18:47:39 -07:00
end
else
if not running then
2015-04-06 01:18:46 -07:00
Bridge.liveSplit()
2014-07-12 18:47:39 -07:00
running = true
end
2015-04-07 19:06:29 -07:00
Settings.choosePlayerNames()
2014-07-12 18:47:39 -07:00
end
else
2015-04-06 01:18:46 -07:00
local battleState = Memory.value("game", "battle")
Control.encounter(battleState)
2015-04-08 01:51:38 -07:00
local curr_hp = Pokemon.index(0, "hp")
-- if curr_hp ~= lastHP then
-- Bridge.hp(curr_hp, Pokemon.index(0, "max_hp"))
-- lastHP = curr_hp
-- end
2015-04-08 01:51:38 -07:00
if curr_hp == 0 and not Control.canDie() and Pokemon.index(0) > 0 then
2015-04-06 01:18:46 -07:00
Strategies.death(currentMap)
elseif Walk.strategy then
if Strategies.execute(Walk.strategy) then
Walk.traverse(currentMap)
2014-07-12 18:47:39 -07:00
end
elseif battleState > 0 then
2015-04-06 01:18:46 -07:00
if not Control.shouldCatch(partySize) then
Battle.automate()
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
elseif Textbox.handle() then
Walk.traverse(currentMap)
2014-07-12 18:47:39 -07:00
end
end
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()