Jump to content

Closest attackable npc


d0zza

Recommended Posts

So I'm trying to make a combat script. If another player is in combat with the closest monster to me how do I make my script attack the next closest monster?

I've tried making a function to do this:

public NPC closestAttackable() {
        List<NPC> npcs = getNpcs().getAll();
        for (i = 0; i < npcs.size(); i++) {
            if (!npcs.get(i).isUnderAttack() && myPlayer().getArea(7).contains(npcs.get(i))) {
                return npcs.get(i);
            }
        }
        return null;
    }

but this seems to always return null, any advice?

Edited by d0zza
Link to comment
Share on other sites

I'd personally use a filter for this, not a for loop. something like:
 

npcname = npcs.closest(n -> n.getName().equals("npcname") && areawithnpcs.contains(n) && !n.isHitBarVisible() && n.getHealthPercent() > 0 && !n.isUnderAttack());   
            if(npcname!= null && !myPlayer().isUnderAttack() && !myPlayer().isMoving()) {
                npcname.interact("Attack");
            }

 

  • Like 1
Link to comment
Share on other sites

Optional<NPC> getAll = getNpcs().getAll().stream().filter(n -> !n.isUnderAttack() && n.getHealthPercent() > 0 && n.getName().equals("X")).findFirst();

if(getAll().get != null) {

for(Player player : getPlayers().getAll()) {

if(player != myPlayer() && !player.isInteracting(getAll().get()) {

getAll().get().interact("Attack") 

cond sleep

}

}

}

probs a bad way to do it but idk

if you dont use the optional you can do the same java stream but add .orElse(null) at the end

  • Like 1
Link to comment
Share on other sites

I just set a field variable for the filter such as below

 

		private Predicate<NPC> npcFilter = n -> n != null && myPlayer().getArea(6).contains(n)
					&& n.getName().contains("name") && n.hasAction("Attack") && n.isAttackable() && !n.isUnderAttack();
	
		NPC npc = s.npcs.getAll().stream().filter(npcFilter).findFirst().get();
  
		if (npc.interact("Attack")) {
			new ConditionalSleep(6500, 500) {
				@Override
				public boolean condition() throws InterruptedException {
					return combat.isFighting() || myPlayer().isInteracting(npc);
				}
			}.sleep(); //max sleep 6.5s, recheck every 500ms. Stops sleeping if fighting or interacting with the filtered npc
		}

 

Link to comment
Share on other sites

I just set a field variable for the filter such as below

 

		private Predicate<NPC> npcFilter = n -> n != null && myPlayer().getArea(6).contains(n)
					&& n.getName().contains("name") && n.hasAction("Attack") && n.isAttackable() && !n.isUnderAttack();
	
		NPC npc = s.npcs.getAll().stream().filter(npcFilter).findFirst().get();
  
		if (npc.interact("Attack")) {
			new ConditionalSleep(6500, 500) {
				@Override
				public boolean condition() throws InterruptedException {
					return combat.isFighting() || myPlayer().isInteracting(npc);
				}
			}.sleep(); //max sleep 6.5s, recheck every 500ms. Stops sleeping if fighting or interacting with the filtered npc
		}

 

Edit:

 

It'd also be useful to add map#canReach to the Predicate and sort the NPCs in the stream by distance

.sorted((n1, n2) -> Integer.compare(s.map.realDistance(n1), s.map.realDistance(n2)))

 

Link to comment
Share on other sites

On 2/19/2017 at 8:35 AM, d0zza said:

So I'm trying to make a combat script. If another player is in combat with the closest monster to me how do I make my script attack the next closest monster?

I've tried making a function to do this:


 

but this seems to always return null, any advice?

 

Not too sure what everyone else in this thread is smoking, all you need is:

NPC cow = getNpcs().closest(npc -> npc.getName().startsWith("Cow") && npc.isAttackable());

 

The isAttackable() method in the Character class returns true if:

It is multiway combat and the character has health > 0

OR

The character has health > 0 and is not under attack

OR

The character is interacting with your player

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