PokeBot-GenerationII/action/battle.lua

316 lines
6.8 KiB
Lua

local Battle = {}
local Textbox = require "action.textbox"
local Combat = require "ai.combat"
local Control = require "ai.control"
local Memory = require "util.memory"
local Menu = require "util.menu"
local Input = require "util.input"
local Utils = require "util.utils"
local Inventory = require "storage.inventory"
local Pokemon = require "storage.pokemon"
-- HELPERS
local function potionsForHit(potion, curr_hp, max_hp)
if not potion then
return
end
local ours, killAmount = Combat.inKillRange()
if ours then
return Utils.canPotionWith(potion, killAmount, curr_hp, max_hp)
end
end
local function recover()
if Control.canRecover() then
local currentHP = Pokemon.index(0, "hp")
if currentHP > 0 then
local maxHP = Pokemon.index(0, "max_hp")
if currentHP < maxHP then
local first, second
if potionIn == "full" then
first, second = "full_restore", "super_potion"
if maxHP - currentHP > 54 then
first = "full_restore"
second = "super_potion"
else
first = "super_potion"
second = "full_restore"
end
else
if maxHP - currentHP > 22 then
first = "super_potion"
second = "potion"
else
first = "potion"
second = "super_potion"
end
end
if first == "potion" or first == "super_potion" or first == "full_restore" then
local potion = Inventory.contains(first, second)
if potionsForHit(potion, currentHP, maxHP) then
Inventory.use(potion, nil)
--Inventory.use(potion, nil, true)
return true
end
end
end
end
end
--[[if Memory.value("battle", "paralyzed") == 64 then
local heals = Inventory.contains("paralyze_heal", "full_restore")
if heals then
Inventory.use(heals, nil)
--Inventory.use(heals, nil, true)
return true
end
end]]
end
local function openBattleMenu()
if Memory.value("battle", "text") == 3 then
Input.cancel()
return false
end
local battleMenu = Memory.value("battle", "menu")
if battleMenu == 106 then
return true
elseif battleMenu == 186 then
local rowSelected = Memory.value("battle", "menuY")
local columnSelected = Memory.value("battle", "menuX")
if columnSelected == 1 then
if rowSelected == 2 then
Input.press("Up", 2)
else
Input.press("A", 2)
end
else
Input.press("Left", 2)
end
else
Input.press("B", 2)
end
end
local function attack(attackIndex)
if Memory.double("battle", "opponent_hp") < 1 then
Input.cancel()
elseif openBattleMenu() then
local AttackRow = Memory.value("menu", "input_row")
--get waiting
local Waiting = Input.isWaiting()
if not Waiting then
if AttackRow < attackIndex then
Input.press("Down", 1)
elseif AttackRow > attackIndex then
Input.press("Up", 1)
else
Input.press("A", 1)
end
end
--Menu.select(attackIndex, true, false, false, 3)
end
end
function movePP(name)
local midx = Pokemon.battleMove(name)
if not midx then
return 0
end
local PokemonID = Memory.value("battle", "our_id")
local PokemonIndex = Pokemon.indexOfbyID(PokemonID)
local SearchString = "pp"..midx
local MovePP = Pokemon.index(PokemonIndex, SearchString)
--return Memory.raw(0x0634 + midx)
return MovePP
end
Battle.pp = movePP
-- UTILS
--[[function Battle.swapMove(sidx, fidx)
if openBattleMenu() then
local selection = Memory.value("menu", "selection_mode")
local swapSelect
if selection == sidx then
swapSelect = fidx
else
swapSelect = sidx
end
if Menu.select(swapSelect, false, false, nil, true, 3) then
Input.press("Select")
end
end
end]]
function Battle.isActive()
return Memory.value("game", "battle") > 0
end
function Battle.isTrainer()
local battleType = Memory.value("game", "battle")
if battleType == 2 then
return true
end
if battleType == 1 then
Battle.handle()
else
Textbox.handle()
end
end
function Battle.opponent()
return Pokemon.getName(Memory.value("battle", "opponent_id"))
end
-- HANDLE
function Battle.run()
if Memory.double("battle", "opponent_hp") < 1 then
Input.cancel()
elseif Memory.value("battle", "menu") ~= 186 then
--if Memory.value("menu", "text_length") == 127 then
-- Input.press("B")
--else
Input.press("B", 2)
--Input.cancel()
--end
elseif Memory.value("battle", "menu") == 186 then
Input.escape()
end
end
function Battle.handle()
if not Control.shouldCatch() then
if Control.shouldFight() then
Battle.fight()
else
Battle.run()
end
end
end
function Battle.handleWild()
if Memory.value("game", "battle") ~= 1 then
return true
end
Battle.handle()
end
function Battle.fight(move, skipBuffs)
if move then
if type(move) ~= "number" then
move = Pokemon.battleMove(move)
end
attack(move)
else
move = Combat.bestMove()
if move then
--Battle.accurateAttack = move.accuracy == 100
attack(move.midx)
--elseif Memory.value("menu", "text_length") == 127 then
-- Input.press("B")
else
Input.cancel()
end
end
end
--[[function Battle.swap(target)
local battleMenu = Memory.value("battle", "menu")
if Utils.onPokemonSelect(battleMenu) then
if Menu.getCol() == 0 then
Menu.select(Pokemon.indexOf(target), true)
else
Input.press("A")
end
elseif battleMenu == 94 then
local selected = Memory.value("menu", "selection")
if selected == 199 then
Input.press("A", 2)
elseif Menu.getCol() == 9 then
Input.press("Right", 0)
else
Input.press("Up", 0)
end
else
Input.cancel()
end
end]]
function Battle.automate(moveName, skipBuffs)
if not recover() then
local state = Memory.value("game", "battle")
if state == 0 then
Input.cancel()
else
if moveName and movePP(moveName) == 0 then
moveName = nil
end
if state == 1 then
if Control.shouldFight() then
Battle.fight(moveName, skipBuffs)
else
Battle.run()
end
elseif state == 2 then
Battle.fight(moveName, skipBuffs)
end
end
end
end
function Battle.inside_menu(inputing)
local battleText = Memory.value("battle", "text")
local MenuShopCurrent = Memory.value("menu", "shop_current")
if battleText ~= 1 or MenuShopCurrent ~= 79 then
if inputing then
Input.press("A", 2)
else
return false
end
elseif battleText == 1 and MenuShopCurrent == 79 then
return true
end
end
-- SACRIFICE
function Battle.sacrifice(...)
local sacrifice = Pokemon.getSacrifice(...)
if sacrifice then
Battle.swap(sacrifice)
return true
end
return false
end
--[[function Battle.redeployNidoking()
if Pokemon.isDeployed("nidoking") then
return false
end
local battleMenu = Memory.value("battle", "menu")
if Utils.onPokemonSelect(battleMenu) then
Menu.select(0, true)
elseif battleMenu == 95 and Menu.getCol() == 1 then
Input.press("A")
else
local __, turns = Combat.bestMove()
if turns == 1 then
if Pokemon.isDeployed("spearow") then
forced = "growl"
else
forced = "sand_attack"
end
end
Battle.automate(forced)
end
return true
end]]
return Battle