From dc3a87c67dc0c22f2c1593ff46c8c841fb1c918c Mon Sep 17 00:00:00 2001 From: Kyle Coburn Date: Mon, 6 Apr 2015 17:29:05 -0700 Subject: [PATCH] Ensure wild battles are not automatically ran from unintentionally, standardize HP retrieval --- action/battle.lua | 14 ++++++++++++-- ai/combat.lua | 15 +++++++++++++++ ai/control.lua | 12 +++++++++++- ai/red/strategies.lua | 38 ++++++++++++++++---------------------- ai/strategies.lua | 5 +---- util/bridge.lua | 8 +++++--- 6 files changed, 60 insertions(+), 32 deletions(-) diff --git a/action/battle.lua b/action/battle.lua index 28a8444..97fd8d5 100644 --- a/action/battle.lua +++ b/action/battle.lua @@ -125,7 +125,7 @@ function Battle.isTrainer() return true end if battleType == 1 then - Battle.run() + Battle.handle() else Textbox.handle() end @@ -154,11 +154,21 @@ function Battle.run() 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.run() + Battle.handle() end function Battle.fight(move, isNumber, skipBuffs) diff --git a/ai/combat.lua b/ai/combat.lua index 2db1064..d79f2de 100644 --- a/ai/combat.lua +++ b/ai/combat.lua @@ -4,6 +4,7 @@ local Movelist = require "data.movelist" local Opponents = require "data.opponents" local Memory = require "util.memory" +local Pokemon = require "storage.pokemon" local damageMultiplier = { -- http://bulbapedia.bulbagarden.net/wiki/Type_chart#Generation_I normal = {normal=1.0, fighting=1.0, flying=1.0, poison=1.0, ground=1.0, rock=0.5, bug=1.0, ghost=0.0, fire=1.0, water=1.0, grass=1.0, electric=1.0, psychic=1.0, ice=1.0, dragon=1.0, }, @@ -295,6 +296,20 @@ local function isConfused() end Combat.isConfused = isConfused +-- HP + +function Combat.hp() + return Pokemon.index(0, "hp") +end + +function Combat.redHP() + return math.ceil(Pokemon.index(0, "max_hp") * 0.2) +end + +function Combat.inRedBar() + return Combat.hp() <= Combat.redHP() +end + -- Combat AI function Combat.factorPP(enabled) diff --git a/ai/control.lua b/ai/control.lua index 56e8f10..9c4347e 100644 --- a/ai/control.lua +++ b/ai/control.lua @@ -24,6 +24,10 @@ Control.areaName = "Unknown" Control.moonEncounters = nil Control.yolo = false +local function withinOneKill(forExp) + return Pokemon.getExp() + 80 > forExp +end + local controlFunctions = { a = function(data) @@ -84,11 +88,17 @@ local controlFunctions = { moon2Exp = function() minExp = 3011 shouldFight = {{name="zubat"}, {name="paras"}} + oneHits = not withinOneKill(minExp) end, moon3Exp = function() + local expTotal = Pokemon.getExp() minExp = 3798 - shouldFight = {{name="zubat"}, {name="geodude",lvl={9}}, {name="paras"}} --TODO geodude? + if withinOneKill(minExp) then + shouldFight = {{name="zubat"}, {name="geodude",lvl={9}}, {name="paras"}} --TODO geodude? + else + shouldFight = nil + end end, catchNidoran = function() diff --git a/ai/red/strategies.lua b/ai/red/strategies.lua index 4148e03..817a88c 100644 --- a/ai/red/strategies.lua +++ b/ai/red/strategies.lua @@ -277,12 +277,8 @@ strategyFunctions.catchNidoran = function() else Input.cancel() end - elseif not Control.shouldCatch() then - if Control.shouldFight() then - Battle.fight() - else - Battle.run() - end + else + Battle.handle() end else local noDSum @@ -368,11 +364,11 @@ strategyFunctions.grabForestPotion = function() if Battle.handleWild() then local potionCount = Inventory.count("potion") if Strategies.initialize() then - status.tempDir = potionCount + status.tries = potionCount end if potionCount > 0 then - if status.tempDir and potionCount > status.tempDir then - status.tempDir = nil + if status.tries and potionCount > status.tries then + status.tries = nil end local healthNeeded = (Pokemon.info("spearow", "level") == 3) and 8 or 15 if Pokemon.info("squirtle", "hp") <= healthNeeded then @@ -382,7 +378,7 @@ strategyFunctions.grabForestPotion = function() else return true end - elseif not status.tempDir then + elseif not status.tries then return true elseif Menu.close() then Player.interact("Up") @@ -743,8 +739,8 @@ strategyFunctions.catchFlierBackup = function() else Input.press("A") end - elseif not Control.shouldCatch() then - Battle.run() + else + Battle.handle() end else local birdPath @@ -941,7 +937,7 @@ strategyFunctions.redbarMankey = function() if not Strategies.setYolo("mankey") then return true end - local curr_hp, red_hp = Pokemon.index(0, "hp"), Strategies.redHP() + local curr_hp, red_hp = Pokemon.index(0, "hp"), Combat.redHP() if curr_hp <= red_hp then return true end @@ -1092,9 +1088,7 @@ strategyFunctions.catchOddish = function() if status.tries == 0 and py == 31 then status.tries = 1 end - if not Control.shouldCatch() then - Battle.run() - end + Battle.handle() end elseif status.tries == 1 and py == 31 then Player.interact("Left") @@ -1266,7 +1260,7 @@ strategyFunctions.fightSurge = function() if not enemyTurns or enemyTurns > 2 then forced = "bubblebeam" elseif enemyTurns == 2 and not Strategies.opponentDamaged() then - local curr_hp, red_hp = Pokemon.index(0, "hp"), Strategies.redHP() + local curr_hp, red_hp = Pokemon.index(0, "hp"), Combat.redHP() local afterHit = curr_hp - 20 if afterHit > 5 and afterHit <= red_hp then forced = "bubblebeam" @@ -1329,7 +1323,7 @@ strategyFunctions.redbarCubone = function() if Pokemon.isOpponent("cubone") then local enemyMove, enemyTurns = Combat.enemyAttack() if enemyTurns then - local curr_hp, red_hp = Pokemon.index(0, "hp"), Strategies.redHP() + local curr_hp, red_hp = Pokemon.index(0, "hp"), Combat.redHP() local clubDmg = enemyMove.damage local afterHit = curr_hp - clubDmg red_hp = red_hp - 2 @@ -1654,7 +1648,7 @@ strategyFunctions.silphRival = function() local opponentName = Battle.opponent() if opponentName == "gyarados" then readyToAttack = true - local hp, red_hp = Pokemon.index(0, "hp"), Strategies.redHP() + local hp, red_hp = Pokemon.index(0, "hp"), Combat.redHP() if hp > gyaradosDamage * 0.98 and hp - gyaradosDamage * 0.975 < red_hp then --TODO if Strategies.prepare("x_special") then forced = "ice_beam" @@ -1735,7 +1729,7 @@ end -- 9: SILPH CO. strategyFunctions.potionBeforeHypno = function() - local curr_hp, red_hp = Pokemon.index(0, "hp"), Strategies.redHP() + local curr_hp, red_hp = Pokemon.index(0, "hp"), Combat.redHP() local healthUnderRedBar = red_hp - curr_hp local yoloHP = Combat.healthFor("HypnoHeadbutt") * 0.9 local useRareCandy = Inventory.count("rare_candy") > 2 @@ -1858,7 +1852,7 @@ strategyFunctions.fightErika = function() if Battle.isActive() then status.canProgress = true local forced - local curr_hp, red_hp = Pokemon.index(0, "hp"), Strategies.redHP() + local curr_hp, red_hp = Pokemon.index(0, "hp"), Combat.redHP() local razorDamage = 34 if curr_hp > razorDamage and curr_hp - razorDamage < red_hp then if Strategies.opponentDamaged() then @@ -2176,7 +2170,7 @@ strategyFunctions.bruno = function() local forced if Pokemon.isOpponent("onix") then forced = "ice_beam" - -- local curr_hp, red_hp = Pokemon.info("nidoking", "hp"), Strategies.redHP() + -- local curr_hp, red_hp = Pokemon.info("nidoking", "hp"), Combat.redHP() -- if curr_hp > red_hp then -- local enemyMove, enemyTurns = Combat.enemyAttack() -- if enemyTurns and enemyTurns > 1 then diff --git a/ai/strategies.lua b/ai/strategies.lua index 923e30e..68411f4 100644 --- a/ai/strategies.lua +++ b/ai/strategies.lua @@ -159,10 +159,6 @@ function Strategies.opponentDamaged(factor) return Memory.double("battle", "opponent_hp") * factor < Memory.double("battle", "opponent_max_hp") end -function Strategies.redHP() - return math.ceil(Pokemon.index(0, "max_hp") * 0.2) -end - function Strategies.buffTo(buff, defLevel) if Battle.isActive() then status.canProgress = true @@ -710,6 +706,7 @@ function Strategies.softReset() Strategies.status = status splitNumber, splitTime = 0, 0 resetting = nil + Strategies.deepRun = false Strategies.resetGame() end diff --git a/util/bridge.lua b/util/bridge.lua index a6d959e..e3f56ae 100644 --- a/util/bridge.lua +++ b/util/bridge.lua @@ -51,9 +51,11 @@ function Bridge.init() end end -function Bridge.tweet(message) -- Two of the same tweet in a row will only send one - print("tweet::"..message) - return send("tweet", message) +function Bridge.tweet(message) + if INTERNAL and STREAMING_MODE then + print("tweet::"..message) + return send("tweet", message) + end end function Bridge.pollForName()