PokeBot/ai/control.lua

365 lines
8.4 KiB
Lua
Raw Normal View History

2015-04-06 01:18:46 -07:00
local Control = {}
2014-07-12 18:47:39 -07:00
local Battle
2015-04-06 01:18:46 -07:00
local Combat = require "ai.combat"
local Strategies
2014-07-12 18:47:39 -07:00
2015-04-06 01:18:46 -07:00
local Bridge = require "util.bridge"
local Memory = require "util.memory"
local Paint = require "util.paint"
local Utils = require "util.utils"
2014-07-12 18:47:39 -07:00
2015-04-06 01:18:46 -07:00
local Inventory = require "storage.inventory"
local Pokemon = require "storage.pokemon"
2014-07-12 18:47:39 -07:00
local potionInBattle = true
local encounters = 0
2014-07-12 18:47:39 -07:00
local canDie, shouldFight, minExp
2014-07-12 18:47:39 -07:00
local shouldCatch, attackIdx
local extraEncounter, maxEncounters
2015-04-06 00:50:00 -07:00
local battleYolo
2014-07-12 18:47:39 -07:00
2015-04-07 05:01:40 -07:00
local yellow = YELLOW
2015-04-06 01:18:46 -07:00
Control.areaName = "Unknown"
Control.moonEncounters = nil
Control.getMoonExp = true
2015-04-06 01:18:46 -07:00
Control.yolo = false
2014-07-12 18:47:39 -07:00
local function withinOneKill(forExp)
return Pokemon.getExp() + 80 > forExp
end
2014-07-12 18:47:39 -07:00
local controlFunctions = {
2015-04-05 18:50:30 -07:00
a = function(data)
2015-04-06 01:18:46 -07:00
Control.areaName = data.a
2015-04-05 18:50:30 -07:00
return true
end,
2014-07-12 18:47:39 -07:00
potion = function(data)
if data.b ~= nil then
2015-04-06 01:18:46 -07:00
Control.battlePotion(data.b)
2014-07-12 18:47:39 -07:00
end
battleYolo = data.yolo
end,
encounters = function(data)
if RESET_FOR_TIME then
2014-07-12 18:47:39 -07:00
maxEncounters = data.limit
extraEncounter = data.extra
end
end,
pp = function(data)
2015-04-06 01:18:46 -07:00
Combat.factorPP(data.on)
2014-07-12 18:47:39 -07:00
end,
setThrash = function(data)
2015-04-06 01:18:46 -07:00
Combat.disableThrash = data.disable
2014-07-12 18:47:39 -07:00
end,
disableCatch = function()
shouldCatch = nil
shouldFight = nil
end,
-- RED
2014-07-12 18:47:39 -07:00
viridianExp = function()
minExp = 210
shouldFight = {{name="rattata",lvl={2,3}}, {name="pidgey",lvl={2}}}
end,
viridianBackupExp = function()
minExp = 210
shouldFight = {{name="rattata",lvl={2,3}}, {name="pidgey",lvl={2,3}}}
end,
nidoranBackupExp = function()
minExp = 210
shouldFight = {{name="rattata"}, {name="pidgey"}, {name="nidoran"}, {name="nidoranf",lvl={2}}}
end,
startMtMoon = function()
Control.moonEncounters = 0
Control.canDie(false)
Control.getMoonExp = true
if not yellow then
local nidoStats = Strategies.stats.nidoran
if nidoStats.attack == 16 and not nidoStats.level4 then
Control.getMoonExp = false
end
end
end,
2014-07-12 18:47:39 -07:00
moon1Exp = function()
if Control.getMoonExp then
minExp = 2704
2015-04-08 01:52:34 -07:00
local levels = Strategies.stats.nidoran.level4 and {9, 10} or {10}
shouldFight = {{name="zubat",lvl=levels}}
oneHits = true
end
2014-07-12 18:47:39 -07:00
end,
moon2Exp = function()
if Control.getMoonExp then
minExp = 3011
shouldFight = {{name="zubat"}, {name="paras"}}
oneHits = not withinOneKill(minExp)
end
2014-07-12 18:47:39 -07:00
end,
moon3Exp = function()
if Control.getMoonExp then
local expTotal = Pokemon.getExp()
minExp = 3798
if withinOneKill(minExp) then
shouldFight = {{name="zubat"}, {name="paras"}}
else
shouldFight = nil
end
end
2014-07-12 18:47:39 -07:00
end,
catchNidoran = function()
shouldCatch = {{name="nidoran",lvl={3,4}}, {name="spearow"}}
end,
catchFlier = function()
shouldCatch = {{name="spearow",alt="pidgey",hp=15}, {name="pidgey",alt="spearow",hp=15}}
end,
catchParas = function()
shouldCatch = {{name="paras",hp=16}}
end,
catchOddish = function()
shouldCatch = {{name="oddish",alt="paras",hp=26}}
end,
2015-04-07 05:01:40 -07:00
-- YELLOW
catchNidoranYellow = function()
shouldCatch = {{name="nidoran",lvl={6}}}
end,
moonExpYellow = function()
minExp = 2704 --TODO
shouldFight = {{name="geodude"}, {name="clefairy",lvl={12,13}}}
oneHits = true
end,
catchSandshrew = function()
shouldCatch = {{name="sandshrew"}}
end,
2014-07-12 18:47:39 -07:00
}
-- COMBAT
2015-04-06 01:18:46 -07:00
function Control.battlePotion(enable)
2015-04-05 18:50:30 -07:00
potionInBattle = enable
end
2015-04-06 01:18:46 -07:00
function Control.canDie(enabled)
if enabled == nil then
return canDie
end
canDie = enabled
end
2014-07-12 18:47:39 -07:00
local function isNewFight()
if Memory.double("battle", "opponent_hp") == Memory.double("battle", "opponent_max_hp") then
2014-07-12 18:47:39 -07:00
return true
end
end
2015-04-06 01:18:46 -07:00
function Control.shouldFight()
if not shouldFight then
2014-07-12 18:47:39 -07:00
return false
end
2015-04-06 01:18:46 -07:00
local expTotal = Pokemon.getExp()
if expTotal < minExp then
2015-04-06 01:18:46 -07:00
local oid = Memory.value("battle", "opponent_id")
local olvl = Memory.value("battle", "opponent_level")
2014-07-12 18:47:39 -07:00
for i,p in ipairs(shouldFight) do
2015-04-06 01:18:46 -07:00
if oid == Pokemon.getID(p.name) and (not p.lvl or Utils.match(olvl, p.lvl)) then
if oneHits then
2015-04-06 01:18:46 -07:00
local move = Combat.bestMove()
if move and move.maxDamage * 0.925 < Memory.double("battle", "opponent_hp") then
2014-07-12 18:47:39 -07:00
return false
end
end
return true
end
end
end
end
2015-04-06 01:18:46 -07:00
function Control.canCatch(partySize)
if not partySize then
2015-04-06 01:18:46 -07:00
partySize = Memory.value("player", "party_size")
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
local pokeballs = Inventory.count("pokeball")
2015-04-07 05:01:40 -07:00
local minimumCount = (yellow and 3 or 4) - partySize
if pokeballs < minimumCount then
if yellow and Pokemon.inParty("nidoran") and Pokemon.inParty("pidgey", "spearow") then
return false
end
2015-04-06 01:18:46 -07:00
Strategies.reset("Not enough PokeBalls", pokeballs)
2014-07-12 18:47:39 -07:00
return false
end
return true
end
2015-04-06 01:18:46 -07:00
function Control.shouldCatch(partySize)
if maxEncounters and encounters > maxEncounters then
2015-04-06 01:18:46 -07:00
local extraCount = extraEncounter and Pokemon.inParty(extraEncounter)
if not extraCount or encounters > maxEncounters + 1 then
2015-04-06 01:18:46 -07:00
Strategies.reset("Too many encounters", encounters)
2014-07-12 18:47:39 -07:00
return false
end
end
if not shouldCatch then
2014-07-12 18:47:39 -07:00
return false
end
if not partySize then
2015-04-06 01:18:46 -07:00
partySize = Memory.value("player", "party_size")
2014-07-12 18:47:39 -07:00
end
if partySize == 4 then
2014-07-12 18:47:39 -07:00
shouldCatch = nil
return false
end
2015-04-06 01:18:46 -07:00
if not Control.canCatch(partySize) then
2014-07-12 18:47:39 -07:00
return true
end
2015-04-06 01:18:46 -07:00
local oid = Memory.value("battle", "opponent_id")
2014-07-12 18:47:39 -07:00
for i,poke in ipairs(shouldCatch) do
2015-04-06 01:18:46 -07:00
if oid == Pokemon.getID(poke.name) and not Pokemon.inParty(poke.name, poke.alt) then
if not poke.lvl or Utils.match(Memory.value("battle", "opponent_level"), poke.lvl) then
local penultimate = poke.hp and Memory.double("battle", "opponent_hp") > poke.hp
if penultimate then
2015-04-06 01:18:46 -07:00
penultimate = Combat.nonKill()
2014-07-12 18:47:39 -07:00
end
if penultimate then
2015-04-09 16:53:56 -07:00
require("action.battle").fight(penultimate.midx)
2014-07-12 18:47:39 -07:00
else
2015-04-06 01:18:46 -07:00
Inventory.use("pokeball", nil, true)
2014-07-12 18:47:39 -07:00
end
return true
end
end
end
end
-- Items
2015-04-06 01:18:46 -07:00
function Control.canRecover()
return potionInBattle and (not battleYolo or not Control.yolo)
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Control.set(data)
2014-07-12 18:47:39 -07:00
controlFunctions[data.c](data)
end
2015-04-06 01:18:46 -07:00
function Control.setYolo(enabled)
Control.yolo = enabled
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Control.setPotion(enabled)
2015-01-19 17:11:52 -08:00
potionInBattle = enabled
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Control.encounters()
2014-07-12 18:47:39 -07:00
return encounters
end
function Control.encounter(battleState)
if battleState > 0 then
local wildBattle = battleState == 1
local isCritical
local battleMenu = Memory.value("battle", "menu")
if battleMenu == 94 then
isCritical = false
Control.missed = false
elseif Memory.double("battle", "our_hp") == 0 then
if Memory.value("battle", "critical") == 1 then
isCritical = true
end
elseif not Control.missed then
local turnMarker = Memory.value("battle", "our_turn")
if turnMarker == 100 or turnMarker == 128 then
local isMiss = Memory.value("battle", "miss") == 1
if isMiss then
2015-04-10 00:34:50 -07:00
if not Control.ignoreMiss and Battle.accurateAttack and Memory.value("battle", "accuracy") == 7 then
Bridge.chat("gen 1 missed :( (1 in 256 chance)")
end
Control.missed = true
end
end
end
if isCritical ~= nil and isCritical ~= Control.criticaled then
Control.criticaled = isCritical
end
if wildBattle then
local opponentHP = Memory.double("battle", "opponent_hp")
if not Control.inBattle then
if opponentHP > 0 then
Control.killedCatch = false
Control.inBattle = true
encounters = encounters + 1
Paint.wildEncounters(encounters)
Bridge.encounter()
if Control.moonEncounters then
if Pokemon.isOpponent("zubat") then
Bridge.chat("NightBat")
end
Control.moonEncounters = Control.moonEncounters + 1
end
end
else
if opponentHP == 0 and shouldCatch and not Control.killedCatch then
local gottaCatchEm = {"pidgey", "spearow", "paras", "oddish"}
local opponent = Battle.opponent()
for i,catch in ipairs(gottaCatchEm) do
if opponent == catch then
if not Pokemon.inParty(catch) then
2015-04-10 00:34:50 -07:00
Bridge.chat("accidentally killed "..Utils.capitalize(catch).." with a "..(isCritical and "critical" or "high damage range").." :(")
Control.killedCatch = true
end
break
end
end
end
end
end
elseif Control.inBattle then
Control.inBattle = false
Control.escaped = Memory.value("battle", "battle_turns") == 0
2015-04-06 00:50:00 -07:00
end
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Control.reset()
canDie = false
2014-07-12 18:47:39 -07:00
oneHits = false
shouldCatch = nil
shouldFight = nil
extraEncounter = nil
2015-01-19 17:11:52 -08:00
potionInBattle = true
2014-07-12 18:47:39 -07:00
encounters = 0
battleYolo = false
maxEncounters = nil
Control.yolo = false
Control.inBattle = false
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Control.init()
Battle = require("action.battle")
2015-04-06 01:18:46 -07:00
Strategies = require("ai."..GAME_NAME..".strategies")
2015-04-06 00:50:00 -07:00
end
2015-04-06 01:18:46 -07:00
return Control