diff --git a/action/battle.lua b/action/battle.lua index 6c28eda..eea39c8 100644 --- a/action/battle.lua +++ b/action/battle.lua @@ -13,28 +13,15 @@ local utils = require "util.utils" local inventory = require "storage.inventory" local pokemon = require "storage.pokemon" -local function potionsForHit(potion, currHP, maxHP) +local function potionsForHit(potion, curr_hp, max_hp) if not potion then return end local ours, killAmount = combat.inKillRange() if ours then - local potionHP - if potion == "full_restore" then - potionHP = 999 - elseif potion == "super_potion" then - potionHP = 50 - else - potionHP = 20 - end - if not currHP then - currHP = pokemon.index(0, "hp") - maxHP = pokemon.index(0, "max_hp") - end - return math.min(currHP + potionHP, maxHP) >= killAmount - 2 + return utils.canPotionWith(potion, killAmount, curr_hp, max_hp) end end -battle.potionsForHit = potionsForHit local function recover() if control.canRecover() then diff --git a/ai/combat.lua b/ai/combat.lua index 5eb0300..05d10e5 100644 --- a/ai/combat.lua +++ b/ai/combat.lua @@ -163,7 +163,7 @@ local function modPlayerStats(user, enemy, move) end local function calcBestHit(attacker, defender, ours, rng) - local bestTurns, bestMinTurns = 9999, 9999 + local bestTurns, bestMinTurns = 9001, 9001 local bestDmg = -1 local ourMaxHit local ret = nil @@ -173,7 +173,7 @@ local function calcBestHit(attacker, defender, ours, rng) if maxDmg then local minTurns, maxTurns if maxDmg <= 0 then - minTurns, maxTurns = 9999, 9999 + minTurns, maxTurns = 9001, 9001 else minTurns = math.ceil(defender.hp / maxDmg) maxTurns = math.ceil(defender.hp / minDmg) diff --git a/ai/strategies.lua b/ai/strategies.lua index 62f3c60..caa5e64 100644 --- a/ai/strategies.lua +++ b/ai/strategies.lua @@ -215,19 +215,12 @@ end local function canHealFor(damage) local curr_hp = pokemon.index(0, "hp") local max_hp = pokemon.index(0, "max_hp") - if max_hp - curr_hp < 5 then - return nil - end - local healChecks = { - {"full_restore", 9001}, - {"super_potion", 50}, - {"potion", 20}, - } - for idx,potion in ipairs(healChecks) do - local name = potion[1] - local result_hp = math.min(curr_hp + potion[2], max_hp) - if result_hp >= damage and inventory.contains(name) then - return name + if max_hp - curr_hp > 3 then + local healChecks = {"full_restore", "super_potion", "potion"} + for idx,potion in ipairs(healChecks) do + if inventory.contains(potion) and utils.canPotionWith(potion, damage, curr_hp, max_hp) then + return potion + end end end end diff --git a/util/utils.lua b/util/utils.lua index 7241691..e1806cf 100644 --- a/util/utils.lua +++ b/util/utils.lua @@ -2,14 +2,12 @@ local utils = {} local memory = require "util.memory" +-- GENERAL + function utils.dist(x1, y1, x2, y2) return math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2)) end -function utils.ingame() - return memory.raw(0x020E) > 0 -end - function utils.each(table, func) for key,val in pairs(table) do func(key.." = "..tostring(val)..",") @@ -44,11 +42,22 @@ function utils.key(needle, haystack) return nil end -function utils.igt() - local secs = memory.raw(0xDA44) - local mins = memory.raw(0xDA43) - local hours = memory.raw(0xDA41) - return secs + mins * 60 + hours * 3600 +-- GAME + +function utils.canPotionWith(potion, forDamage, curr_hp, max_hp) + local potion_hp + if potion == "full_restore" then + potion_hp = 9001 + elseif potion == "super_potion" then + potion_hp = 50 + else + potion_hp = 20 + end + return math.min(curr_hp + potion_hp, max_hp) >= forDamage - 1 +end + +function utils.ingame() + return memory.raw(0x020E) > 0 end function utils.onPokemonSelect(battleMenu) @@ -57,6 +66,13 @@ end -- TIME +function utils.igt() + local secs = memory.raw(0xDA44) + local mins = memory.raw(0xDA43) + local hours = memory.raw(0xDA41) + return secs + mins * 60 + hours * 3600 +end + local function clockSegment(unit) if unit < 10 then unit = "0"..unit