Jump to content

Checking distance between an NPC and GroundItem?


Recommended Posts

Posted (edited)

Hey guys, I'm trying to check the distance between an NPC and GroundItem, so that whichever one is closer, I'll either attack it or loot it. How would I go upon doing this?

 

Also, what's the most efficient and fastest way of attacking an NPC?

 

Thanks!

 

EDIT: I think I figured it out:

if (myPlayer().getPosition().distance(npc) < myPlayer()
                        .getPosition().distance(grounditem))

Thanks anyways!

Edited by Annointed
Posted (edited)

Hey guys, I'm trying to check the distance between an NPC and GroundItem, so that whichever one is closer, I'll either attack it or loot it. How would I go upon doing this?

 

Also, what's the most efficient and fastest way of attacking an NPC?

 

Thanks!

 

You can get the real (walking) distance to the npc / ground item using:

int distance = getMap().realDistance(entity); // entity is the npc or ground item

Or the direct distance:

int distance = myPosition().distance(entity.getPosition());

Then just compare the two distances to find which is less.

 

For attacking the NPC you can just do something like:

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

if (cow != null && cow.interact("Attack")) {
   new ConditionalSleep(5000) {
       @ Override
       public boolean condition() throws InterruptedException {
           return !cow.isAttackable();
       }
   }.sleep();
}
Edited by Explv
Posted

 

You can get the real (walking) distance to the npc / ground item using:

int distance = getMap().realDistance(entity); // entity is the npc or ground item

Or the direct distance:

int distance = myPosition().distance(entity.getPosition());

Then just compare the two distances to find which is less.

 

For attacking the NPC you can just do something like:

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

if (cow != null && cow.interact("Attack")) {
   new ConditionalSleep(5000) {
       @ Override
       public boolean condition() throws InterruptedException {
           return !cow.isAttackable();
       }
   }.sleep();
}

 

Thank you! Also, what's the difference between the normal sleep method, and ConditionalSleep? When would I use which?

Posted

Thank you! Also, what's the difference between the normal sleep method, and ConditionalSleep? When would I use which?

 

ConditionalSleep will sleep for MAX the amount of time you specify (in this case 5 seconds) OR until the condition is satisfied.

 

So in the example I gave it would sleep for up to 5 seconds or until the NPC is no longer attackable (either it is dead, someone else is attacking it, or we are attacking it)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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