Jump to content

npcs.closest ?


Recommended Posts

Posted (edited)

fbee9c17158b55d7d3614d831776a98e.gif

NPC FishingSpot = npcs.closest("Fishing spot");

works if you do it once, the second time its like it picks a random one between the two. the one it's currently fishing at is obviously closer

 

 

 

Yes I noticed this too. It's because of:

 

It picks the closest, those tiles all around you (even the diagonals) are considered 1 tile away. Then it picks a random one OR it picks the first one on the checking order.

 

Common distance calculations should be: direct tiles: 10 g cost, diaganol tiles: 14 g cost. It doesn't look like osbot is using this standard in .closest.

 

It just sees all the tiles around you as just 1 away.

 

Edited by MegaManAlpha
Posted

Yes I noticed this too. It's because of:

 

It picks the closest, those tiles all around you (even the diagonals) are considered 1 tile away. Then it picks a random one OR it picks the first one on the checking order.

 

Common distance calculations should be: direct tiles: 10 g cost, diaganol tiles: 14 g cost. It doesn't look like osbot is using this standard in .closest.

 

It just sees all the tiles around you as just 1 away.

"Then it picks a random one OR it picks the first one on the checking order."

I expected this, i didn't realize a diagonal tile was 1 tile though

//try

NPC FishingSpot = npcs.closest(true, "Fishing spot");

what does it do?

Posted (edited)

"Then it picks a random one OR it picks the first one on the checking order."

I expected this, i didn't realize a diagonal tile was 1 tile though

what does it do?

 

 

TEMP FIX - this should work (if it doesn't let me know, I'll make a more advanced one with A* heuristics):

public NPC megaGetClosestNpc(String npcName) {

   int bestDistance = Integer.MAX_VALUE;
   NPC best = null;

   for(NPC npc : npcs.getAll()) {
      if(npc != null && npc.getName().equals(npcName)) {
         int thisDistance = map.realDistance(npc);
         if(thisDistance < bestDistance) {
            best = npc;
            bestDistance = thisDistance;
         }
      }
   }

   return best;

}

 

Edited by MegaManAlpha
Posted

OSBot has 2 distance methods, distance and realDistance. By default the closest method uses distance which is just a rounded pythagora's algorithm computation which is why a tile diagonally next to you is computed as round(sqrt(2)) = 1 even though its 1.4 and the ones directly accessible have distance equal to 1. Using realDistance on the other hand will yield a result of 0 on tiles north west east or south of you and a 2 for diagonal tiles.

 

The reason why we don't use realDistance is the fact that it can only compute distances in the area local to your player (~30 tiles around you) so we mostly rely on distance. As for accurately determining long distances I implemented an algorithm that computes a path length that is used for webwalking (there is no support in the API for such thing). 

Posted

"Then it picks a random one OR it picks the first one on the checking order."

I expected this, i didn't realize a diagonal tile was 1 tile though

what does it do?

 

the boolean is whether or not it should use "Real Distance"

When using real distance, the LocalPathFinder is used to find the length of the actual path to the tile, rather than euclidean distance

 

  • Like 2
Posted (edited)

 

 

TEMP FIX - this should work (if it doesn't let me know, I'll make a more advanced one with A* heuristics):

public NPC megaGetClosestNpc(String npcName) {

   int bestDistance = Integer.MAX_VALUE;
   NPC best = null;

   for(NPC npc : npcs.getAll()) {
      if(npc != null && npc.getName().equals(npcName)) {
         int thisDistance = map.realDistance(npc);
         if(thisDistance < bestDistance) {
            best = npc;
            bestDistance = thisDistance;
         }
      }
   }

   return best;

}

 

ill try adding the parameter true first, if it doesnt work ill try this

 

i appreciate you helping with this code, but apparently you can enable realdistance just by adding true as parameter to .closest ;)

//try

NPC FishingSpot = npcs.closest(true, "Fishing spot");

 

 

OSBot has 2 distance methods, distance and realDistance. By default the closest method uses distance which is just a rounded pythagora's algorithm computation which is why a tile diagonally next to you is computed as round(sqrt(2)) = 1 even though its 1.4 and the ones directly accessible have distance equal to 1. Using realDistance on the other hand will yield a result of 0 on tiles north west east or south of you and a 2 for diagonal tiles.

 

The reason why we don't use realDistance is the fact that it can only compute distances in the area local to your player (~30 tiles around you) so we mostly rely on distance. As for accurately determining long distances I implemented an algorithm that computes a path length that is used for webwalking (there is no support in the API for such thing). 

 

 

the boolean is whether or not it should use "Real Distance"

When using real distance, the LocalPathFinder is used to find the length of the actual path to the tile, rather than euclidean distance

 

 

thanks for the explanation and the solution guys! cheers boge.png

Edited by The Hero of Time
  • Like 1

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...