Jump to content

Wait Till Attack Next NPC


sesamest

Recommended Posts

My KillCows Class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tasks;

import core.Constants;
import core.Task;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.osbot.script.MethodProvider;
import static org.osbot.script.MethodProvider.random;
import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.mouse.RectangleDestination;
import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.GroundItem;
import org.osbot.script.rs2.model.NPC;
import org.osbot.script.rs2.model.RS2Object;
import org.osbot.script.rs2.ui.Option;

/**
 *
 * @author LivingRoom
 */
public class KillCows extends Task {
    
    List<NPC> NPCList;
    Thread CameraThread;
    
    public KillCows(Script sA) {
        super(sA);
    }
        
    @Override
    public String status() {
        return status;
    }

    String status = "Killing Cows";

    @Override
    public boolean validate() throws InterruptedException {
        //return Constants.FARM_AREA.contains(sA.client.getMyPlayer().getPosition());
        return (!sA.client.getInventory().isFull()) && sA.client.getMyPlayer().getPosition().getY() <= 3313;
    }
    
    public void TurnCameraToGroundItem(final GroundItem npc) {
        CameraThread = null;
        CameraThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            sA.client.moveCameraToPosition(npc.getPosition());
                        } catch (InterruptedException ex) {
                            Logger.getLogger(KillCows.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                   });
        CameraThread.start();
        
    }
    
    public void TurnCameraToNPC(final NPC npc) {
        CameraThread = null;
        CameraThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            sA.client.moveCameraToPosition(npc.getPosition());
                        } catch (InterruptedException ex) {
                            Logger.getLogger(KillCows.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                   });
        CameraThread.start();
        
    }
    
    public void setRun() throws InterruptedException {
        if (sA.client.getRunEnergy() > (15 + MethodProvider.random(0, 35)) && !sA.isRunning()) {
                sA.setRunning(true);
        }
    }

    @Override
    public boolean execute() throws InterruptedException {
        NPCList = sA.closestNPCList(Constants.COWS);
        setRun();
        for(NPC npc : NPCList) {
            
            if(sA.client.getMyPlayer().isUnderAttack()) {
                sA.log("HOVER AREA");
                TurnCameraToNPC(npc);
                while(sA.client.getMyPlayer().isUnderAttack()) {
                    if(npc.isVisible() && !sA.client.isMenuOpen()) {
                        for(Option opt : sA.client.getMenu().subList(0, sA.client.getMenu().size())) {
                            if(!opt.action.contains("Attack") && !opt.noun.contains("Cow")) {
                                sA.client.moveMouseTo(npc.getMouseDestination(), false, false, true);
                            }
                        }
                    }
                    sA.sleep(250);
                    if(npc.isUnderAttack())
                        break;
                }
                if(sA.client.isMenuOpen()) {
                        for(Option opt : sA.client.getMenu().subList(0, sA.client.getMenu().size())) {
                            if(opt.action.contains("Attack") && opt.noun.contains("Cow")) {
                                if(!npc.isUnderAttack())
                                   if(npc.getHealth() == 100)
                                        npc.interact("Attack");
                            }
                        }
                }
            } 
            
            if(!npc.isUnderAttack()) {
                if(!npc.isVisible()) {
                     TurnCameraToNPC(npc);
                     if(npc.getPosition().distance(sA.client.getMyPlayer().getPosition()) >= 12) {
                         sA.walk(new Position(npc.getPosition().getX()+random(-2,2), npc.getPosition().getY()+random(-2,2), 0));
                     }
                }
                if(npc.getHealth() == 100)
                    npc.interact("Attack");
                sA.sleep(random(500,700));
                while(sA.client.getMyPlayer().isMoving()) {
                    sA.sleep(250);
                }
                sA.sleep(random(500,700));
                while(npc.getHealth() == 100) {
                    if(!sA.client.getMyPlayer().isUnderAttack())
                        break;
                    sA.sleep(250);
                }
            }
        }
        GroundItem cowhide = sA.closestGroundItem(Constants.COWHIDE);
        while(cowhide != null) {
            if(!cowhide.isVisible()) {
                     TurnCameraToGroundItem(cowhide);
                     if(cowhide.getPosition().distance(sA.client.getMyPlayer().getPosition()) >= 12) {
                         sA.walk(new Position(cowhide.getPosition().getX()+random(-2,2), cowhide.getPosition().getY()+random(-2,2), 0));
                     }
            }
            cowhide.interact("Take");
            sA.sleep(random(500,700));
            while(sA.client.getMyPlayer().isMoving()) {
                sA.sleep(250);
            }
            sA.sleep(random(500,700));
        }
        return true;
    }
    
}

I understand the function Execute is rather large. I'll clean it up later and optimize it, but right now it attacks the first cow, then moves onto the next one right away without waiting for the cow to die.

 

 

Also I am having an issue with the script recognizing whether it is inside an area. This is for the validate function.

public boolean validate() throws InterruptedException {
        return (!sA.client.getInventory().isFull()) && Constants.FARM_AREA.contains(sA.client.getMyPlayer().getPosition());
 }

and the area FARM_AREA:

public static Area FARM_AREA = new Area(3021, 3297, 3313, 3042);

the first two integers being the x,y of the SW tile, and the second two integers the NE tile.

 

 

I thank you for any help received + advice!

Link to comment
Share on other sites

You should be checking things such as facing , in combat, etc (all of those are avaliable in the API) to see if you're in combat. If and only if you're not in combat should you attack a cow.

 

As for the area problem, I'm not sure, try something like this:

if (client.getMyPlayer().isInArea(FARM_AREA)) {
     doSomthing .. .
}
Link to comment
Share on other sites

 

You should be checking things such as facing , in combat, etc (all of those are avaliable in the API) to see if you're in combat. If and only if you're not in combat should you attack a cow.

 

As for the area problem, I'm not sure, try something like this:

if (client.getMyPlayer().isInArea(FARM_AREA)) {
     doSomthing .. .
}

 

I cannot seem to find isInCombat() in the API. I can only seem to find isUnderAttack().

 

I think I will check if the character is facing the NPC, and I will just have to store the NPC a variable outside the loop.

 

Link to comment
Share on other sites

You could always use a combat timer which relies on Animation. It is not ideal, but it does the job.

Heres a snippet, using a timer class:

 

top:

Timer t;

onstart:

this.t = new Timer(0L);

method:

private void attackTimer() throws InterruptedException {
		if (this.myPlayer().isAnimating()) {
			t.reset();
		}
	}

And seeing as your onPaint is async you may aswell call it in your onPaint as it is refreshing all the time:

@Override
	public void onPaint(Graphics2D g) {
              try {
			attackTimer();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
}

The idea behind this is so that you only attack another cow if t.getElapsed() > 1500 for example, as well as other conditions. This kinda method is handy for when you are safespotting something or using ranged or w/ever, where you can't use myplayer.isunderattack etc

 

Edited by Apaec
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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