Jump to content

My First Combat Related Script


ChadMcBro

Recommended Posts

I thought it would be a good exercise to create a task based script that kills and loots chaos druids. However, my attack task seems to always be bugging out on me. Occasionally ending the conditional sleep when it is not complete. The script is overall me learning some java and programming through many of the discussion posts on here.

package tasks;

import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.MethodProvider;

import utils.Sleep;
import utils.Vals;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static org.osbot.rs07.script.MethodProvider.random;

public class Attack extends Task {
    public Attack(MethodProvider api) {
        super(api);
    }

    NPC currentTarget;

    @Override
    public boolean canProcess() {
        return Vals.combatReady && !Vals.inCombat;
    }

    @Override
    public void process() throws InterruptedException {

        Vals.combatReady = false;

        // Check if any npc is targeting me
        List<NPC> druidsFocusingMe = api.npcs.getAll().stream()
                .filter(npc -> api.myPlayer().equals(npc.getInteracting()))
                .filter(npc -> npc.getName().equals("Chaos druid"))
                .sorted(Comparator.comparingInt(npc -> api.getMap().realDistance(npc)))
                .collect(Collectors.toList());

        if (!druidsFocusingMe.isEmpty()){
            //There is a druid targeting me
            currentTarget = druidsFocusingMe.get(0);
            api.log("COMBAT STATE 1: NPC is attempting to attack");
            Sleep.sleepUntil(() -> !api.myPlayer().isUnderAttack(), 3000);
            api.sleep(random(500,1000));

            // If the druid could not reach me go attack it
            if (!api.getCombat().isFighting() && !((api.myPlayer().isInteracting(currentTarget) && currentTarget.isInteracting(api.myPlayer())))) {
                if (currentTarget.interact("Attack")){
                    api.log("NPC stuck, attacking NPC targetting me");
                    Sleep.sleepUntil(() -> (api.myPlayer().isInteracting(currentTarget) && currentTarget.isInteracting(api.myPlayer())), 5000);
                    if (api.getCombat().isFighting()){
                        api.log("(1) Attack success, in combat");
                        Vals.combatReady = false;
                        Vals.inCombat = true;
                    }
                    //AN error has occured
                    else{
                        api.log("(1) Was unable to get into combat, retrying");
                        api.sleep(random(1000,2000));
                        Vals.combatReady = true;
                    }
                }
            }
            else {
                api.log("(0) Attack success, in combat");
                Vals.inCombat = true;
                Vals.combatReady = false;
            }
        }

        //Else if still not in combat
        else if (!Vals.inCombat) {
            NPC chaosDruid = api.getNpcs().closest(Vals.CHAOS_DRUID);
            api.log("COMBAT STATE2: Attacking nearest NPC");
            currentTarget = chaosDruid;
            if (chaosDruid.interact("Attack")){
                Sleep.sleepUntil(() -> (api.myPlayer().isInteracting(chaosDruid) && chaosDruid.isInteracting(api.myPlayer())), 5000);
                if (api.getCombat().isFighting()) {
                    api.log("(2) Attack success, in combat");
                    Vals.inCombat = true;
                    Vals.combatReady = false;
                }
                //An error has occured
                else{
                    api.log("(2) Was unable to get into combat, retrying");
                    api.sleep(random(1000,2000));
                    Vals.combatReady = true;
                }
            }
        }

        if (Vals.inCombat){
            Sleep.sleepUntil(() -> (!api.myPlayer().isUnderAttack() && !api.getCombat().isFighting() && !api.myPlayer().isAnimating() && currentTarget.getHealthPercent()==0 && !currentTarget.exists() && currentTarget == null), 5000);
            api.sleep(random(250,550));
            api.log("No longer in combat");
            Vals.inCombat = false;
            Vals.readyToLoot = true;
        }
    }
}

I was too lazy to start a GUI so all of my error checking is done through the terminal/logger. I would love some feedback, I can also post the rest of the code if anyone is interested, but it is fairly basic banking and looting.

Link to comment
Share on other sites

1 hour ago, Protoprize said:

Nice script :D

Reason why your sleeps are ending before conditions are met: You have set them all to timeout in 5000ms (5 seconds), so even if conditions haven't been met, it will continue with next step after 5 seconds :D

 

Thanks :), also wow... how did I not realize that... However, that does make me wonder, I do want to do some kind of conditional sleep rather than an while loop or something to just keep going while in combat, just to avoid situation where it might mess this up. But I also eventually want to use this attack task to kill higher leveled monsters and therefore the time for a kill will get longer and longer. Any suggestions? Or is a conditional sleep just not optimal for this kind of application?

Link to comment
Share on other sites

Having done some tests and updates I realized I had simply over done it with the logic checking. In the end what works best for checking of you are in combat: (in my experience)

Sleep.sleepUntil(() -> (!api.getCombat().isFighting() && !currentTarget.exists()), random(10000,15000));

Where I am doing this massive sleep, (random because I am paranoid), as temporary use for some sort of other logic to replace this in the future.

  • Boge 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...