Jump to content

Why is my script attacking NPCs already in combat?


dontbuzz

Recommended Posts

Am learning to script, this is my first script.

 

I thought this line would stop my player from attacking another chicken that is already in combat but it still seems to sometimes. (and not just when another player beats me to the chicken).

  if (chicken != null && chicken.isVisible() && !chicken.isUnderAttack() && chicken.getHealthPercent() > 0 && map.canReach(chicken)) {

Edit:   Script sometimes tries to switch chickens mid combat, anyone know why that could be aswell?

 

 

plz halp.

 

 

Full Code:

import org.osbot.rs07.api.Combat;
import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;


import java.awt.*;


@ScriptManifest(name = "dontbuzz Fried Chicken", author = "dontbuzz", version = 1.0, info = "Kills chickens in Lumbridge", logo = "")
public class ChickenKiller extends Script {

    Area chickenArea = new Area(3183, 3290, 3173, 3306);


    @[member='Override']
    public void onStart() {
        log("Welcome to dontbuzz Chicken Killer");


    }

    @[member='Override']
    public void onExit() {
        //Code here will execute after the script ends


    }


    @[member='Override']
    public int onLoop() throws InterruptedException {

        checkArea();
        attack();
        dropJunk();


        return random(600, 4000);
    }
    // The amount of time in milliseconds before the loop starts over


    @[member='Override']
    public void onPaint(Graphics2D g) {
        //This is where you will put your code for paint(s)


    }

    public void attack() {


        if (!myPlayer().isUnderAttack() && !myPlayer().isMoving() && !getCombat().isFighting()) {

            NPC chicken = npcs.closest("Chicken");

            if (chicken != null && chicken.isVisible() && !chicken.isUnderAttack() && chicken.getHealthPercent() > 0 && map.canReach(chicken)) {

                chicken.interact("Attack");

                new ConditionalSleep(2000) { // Sleep until the player is out of combat, or for 2 seconds
                    @[member='Override']
                    public boolean condition() throws InterruptedException {
                        return !myPlayer().isUnderAttack() && !myPlayer().isMoving() && !getCombat().isFighting();
                    }
                }.sleep();
            }
        }
    }




    public void checkArea() {

        NPC chicken = npcs.closest(chickenArea, "Chicken");

        if (!chickenArea.contains(myPlayer()) && !map.canReach(chicken)) {

            RS2Object chickenGate = objects.closest(1558, 1560);

            chickenGate.interact("Open");

            new ConditionalSleep(3000) { // Sleep until the player is out of combat, or for 5 seconds
                @[member='Override']
                public boolean condition() throws InterruptedException {
                    return map.canReach(chicken);
                }
            }.sleep();

            getWalking().webWalk(chickenArea);


        }
    }

    public void dropJunk() {
        if (!inventory.isEmpty()) {
            inventory.dropAll();
        }
    }


}
Edited by dontbuzz
Link to comment
Share on other sites

 

Am learning to script, this is my first script.

 

I thought this line would stop my player from attacking another chicken that is already in combat but it still seems to sometimes. (and not just when another player beats me to the chicken).

  if (chicken != null && chicken.isVisible() && !chicken.isUnderAttack() && chicken.getHealthPercent() > 0 && map.canReach(chicken)) {

Edit:   Script sometimes tries to switch chickens mid combat, anyone know why that could be aswell?

 

 

plz halp.

 

try this fam

 

hB1xFB1.png

1cwvBTe.png

Edited by Christopher
  • Like 1
Link to comment
Share on other sites

 

Am learning to script, this is my first script.

 

I thought this line would stop my player from attacking another chicken that is already in combat but it still seems to sometimes. (and not just when another player beats me to the chicken).

 

Edit:   Script sometimes tries to switch chickens mid combat, anyone know why that could be aswell?

 

 

plz halp.

 

 

Instead of checking if your player is under attack (the chicken might not be fighting back), instead you can check if your player is interacting with a Chicken.

 

Also, in your ConditionalSleep, considering it takes time for your player to walk to the chicken, and another player might attack it first, you should check in your sleep condition if the chicken is under attack, or dead etc.

 

Consider this short example (untested):

import org.osbot.rs07.api.filter.NameFilter;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

@ScriptManifest(name = "Chicken Killer", info = "Kills Chickens...", author = "Explv", version = 0.1, logo = "")
public final class ChickenKiller extends Script {

    private NPC currentChicken;

    @[member='Override']
    public final int onLoop() throws InterruptedException {

        if(!isAttackingChicken()) attackChicken();

        return random(150, 200);
    }

    private boolean isAttackingChicken() {
        return currentChicken != null &&
               currentChicken.exists() &&
               currentChicken.getHealthPercent() > 0 &&
               myPlayer().isInteracting(currentChicken) &&
               currentChicken.isUnderAttack();
    }

    private void attackChicken() {

        currentChicken = getClosestChicken();

        if(currentChicken != null && currentChicken.interact("Attack")) {

            new ConditionalSleep(5000) {
                @[member='Override']
                public boolean condition() throws InterruptedException {
                    return myPlayer().isInteracting(currentChicken) ||
                           !isAttackableTarget(currentChicken);
                }
            }.sleep();
        }
    }

    private NPC getClosestChicken() {
        return getNpcs().closest(
                new NameFilter<>("Chicken"),
                this::isAttackableTarget
        );
    }

    private boolean isAttackableTarget(final NPC target) {
        return target != null &&
               target.exists() &&
               target.getHealthPercent() > 0 &&
               !target.isUnderAttack();
    }
}

Edited by Explv
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...