g0ldPRO / Questing.lua

A Lua script for PROShine that plays Pokemon Revolution Online for you from the very Start to as far as possible.
http://g0ldpro.github.io/Questing.lua/
Do What The F*ck You Want To Public License
5 stars 7 forks source link

CatchManager Class #48

Closed ghost closed 8 years ago

ghost commented 8 years ago

I've been trying to develop a more generic/comprehensive way of catching pokemon in Questing.lua: I added these lines of code to the "Quests/Quest.lua" file:

function Quest:throwPokeball()
    if isOpponentShiny() then
        if hasItem("Ultra Ball") or hasItem("Great Ball") or hasItem("Pokeball") then
            return true
        end
    elseif not isAlreadyCaught() then
        if hasItem("Pokeball") or hasItem("Great Ball") or hasItem("Ultra Ball") then
            return true
        end
    end
end

hp1Move = {"False Swipe", "Hold Back"}
sleepMove = {"Hypnosis", "Sleep Powder", "Spore", "Dark Void", "Grass Whistle", "Lovely Kiss", "Sing", "Yawn"}

function Quest:catchPokemon()
    if getOpponentHealth() == 1 then
        if getOpponentStatus() == "SLEEP" then
            return self:throwPokeball()
        else
            if game.hasPokemonWithMove(sleepMove) then
                if game.isActivePokemonWithMove(sleepMove) then
                    return useMove(sleepMove)
                else
                    return game.sendPokemonWithMove(sleepMove)
                end
            else
                return self:throwPokeball()
            end
        end
    else
        if game.hasPokemonWithMove(hp1Move) then
            if game.isActivePokemonWithMove(hp1Move) then
                return self:throwPokeball()
            else
                return game.sendPokemonWithMove(hp1Move)
            end
        else
            if getOpponentHealthPercent() < 50 then
                return self:throwPokeball()
            else
                return weakAttack() or attack() or sendUsablePokemon() or run() or sendAnyPokemon()
            end
        end
    end
end

function Quest:wildBattle()
    if isOpponentShiny() or not isAlreadyCaught() then
        return self:catchPokemon()
    end

    -- if we do not try to catch it

    if getTeamSize() == 1 or getUsablePokemonCount() > 1 then
        local opponentLevel = getOpponentLevel()
        local myPokemonLvl  = getPokemonLevel(getActivePokemonNumber())
        if opponentLevel >= myPokemonLvl then
            local requestedId, requestedLevel = game.getMaxLevelUsablePokemon()
            if requestedId ~= nil and requestedLevel > myPokemonLvl then
                return sendPokemon(requestedId)
            end
        end
        return attack() or sendUsablePokemon() or run() or sendAnyPokemon()
    else
        if not self.canRun then
            return attack() or game.useAnyMove()
        end
        return run() or attack() or sendUsablePokemon() or sendAnyPokemon()
    end
end

So I would like to use the functions game.hasPokemonWithMove(Move), game.isActivePokemonwithMove(Move) and game.sendPokemonWithMove(Move).

The 1st one is already stated in "Libs/gamelib.lua" but I doesn't work if Move is stated as a table with more than 1 move, and also add one more condition to return true if the specific move (or moves from a table) has more than 0 remaining PPs.

The other 2 functions I've tried writing:

function game.isActivePokemonWithMove(Move)
    for pokemonId=1, getTeamsize(), 1 do
        if hasMove(pokemonId, Move) and getRemainingPowerPoints(pokemonId, Move) > 0 then
            return true
        end
    end
    return false
end

function game.sendPokemonWithMove(Move)
    for pokemonId=1, getTeamSize(), 1 do
        if hasMove(pokemonId, Move) and getRemainingPowerPoints(pokemonId, Move) > 0 then
            return sendPokemon(pokemonId)
        end     
    end
end

But I think it also need some fixing. Could anyone help me please?

Rympex commented 8 years ago

so many errors, the idea is not bad, for example u can't use game.hasPokemonWithMove(sleepMove)

sleepMove is a table and game,hasPoemonWithMove() require a string game.hasPokemonWithMove(sleepMove[0]) is ok u need write some custom function for parse the right move

ghost commented 8 years ago

"So many errors" :laughing: I know I know, I'm still new. So what you are suggesting is creating 2 separate functions for each set of moves? game.hasPokemonWithMove(sleepMove) and game.hasPokemonWithMove(hp1Move)? I'm not sure what's your point with game.hasPokemonWithMove(sleepMove[0]) :confused:

Rympex commented 8 years ago

u need create a function for parse every move on the table, and for every move parse all team, when is found return the move name or pokemon index

for move in sleepMove do

ghost commented 8 years ago

Oh dear, I think it's gotten way out of my league. I'll try fixing it later. thank you for your input.

ghost commented 8 years ago
function game.hasPokemonWithMove(Move)
    if type(Move) == "string" then
        for pokemonId=1, getTeamSize(), 1 do
            if hasMove(pokemonId, Move) then
                return true
            end
        end
        return false
    elseif type(Move) == "table" then
        for pokemonId=1, getTeamSize(), 1 do
            for i, v in ipairs(Move) do
                if v == Move then
                    if hasMove(pokemonId, v) then
                        return true
                    end
                end
            end
        end
        return false
    end             
end

This is a far as I can go trying to crate the appropriate function, not even sure if I'm in the right path, syntax is "ok" but it returns false when I actually have a pokemon with the move "False Swipe", it attempts to catch the pokemon with weakAttack() to reduce opponent's health below 50%. If anyone can help me with this function it would be much appreciated.

g0ldPRO commented 8 years ago

Have a look at some simple classes like Dialog.lua to understand how to create a class.

ghost commented 8 years ago

I just did and like a said before, I still don't now what else I need to do or fix, this is honestly beyond me, I can't make any more progress about it without REAL guidance, thank you all for the input anyways, I may push this a bit more when I have a better understanding of the language overall and way more time in my plate, but for now I'm closing this thread.