Jump to content

Finding the closest position in a list to my position


creationx

Recommended Posts

So I'm coding my rock crabs script and currently it generates a random number and uses that number to pick a rock crab spot to travel to. This seems a little bad to me and I want to do something different. How would I go about making my character walk to the closest given location in a list of locations. 

 

So the logic would be something like

 

-Look at rock crab positions

-Find rock crab position closest to me

-walk to closest position

 

How would I go about scripting that?

Link to comment
Share on other sites

So I'm coding my rock crabs script and currently it generates a random number and uses that number to pick a rock crab spot to travel to. This seems a little bad to me and I want to do something different. How would I go about making my character walk to the closest given location in a list of locations. 

 

So the logic would be something like

 

-Look at rock crab positions

-Find rock crab position closest to me

-walk to closest position

 

How would I go about scripting that?

 

You can do:

Position[] rockCrabPositions = { new Position(1, 2, 3), new Position(2, 3, 4) };
getWalking().webWalk(rockCrabPositions);

Web walking will walk to the closest position in the array

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

If you mean you want to find the closest rock crab to your player.

Entity rockCrab = getNpcs().closest("Rock crab");
if(rockCrab != null)
{
	getWalking().webWalk(rockCrab.getPosition();
}

If you want to find the closest position from a list of positions and do anything with it.

Change positions to the list of positions you already have.

closestPosition is the end result and will be the closestPosition out of all of them.

 

Later on down the track, learn about lambda expressions, there should be a much nicer and shorter bit of code you could create using lambdas.

ArrayList<Position> positions = new ArrayList<Position>(); // The list of positions
    	Position closestPosition = null; // The closest positon
    	for(Position p : positions) // Loop through the list of positions
    	{
    		if(closestPosition == null) // If closestPosition hasnt been set it, set it to the current position selected by loop
    			closestPosition = p;
    		else if(getMap().distance(p) < getMap().distance(closestPosition)) // Else if the current loops position is closer then closestPosition, set closestPosition to this one.
    				closestPosition = p;
    		}
    	}

Or as Explv said, You could simply input the array into webWalk and it would walk to the closest.

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

 

Later on down the track, learn about lambda expressions, there should be a much nicer and shorter bit of code you could create using lambdas.

ArrayList<Position> positions = new ArrayList<Position>(); // The list of positions
    	Position closestPosition = null; // The closest positon
    	for(Position p : positions) // Loop through the list of positions
    	{
    		if(closestPosition == null) // If closestPosition hasnt been set it, set it to the current position selected by loop
    			closestPosition = p;
    		else if(getMap().distance(p) < getMap().distance(closestPosition)) // Else if the current loops position is closer then closestPosition, set closestPosition to this one.
    				closestPosition = p;
    		}
    	}
Position closest = Arrays.stream(positions)
                         .min((p1, p2) -> Integer.compare(
                             myPosition().distance(p1),
                             myPosition().distance(p2)
                         ))
                         .get();
  • Like 1
Link to comment
Share on other sites

Position closest = Arrays.stream(positions)
                         .min((p1, p2) -> Integer.compare(
                             myPosition().distance(p1),
                             myPosition().distance(p2)
                         ))
                         .get();

Yeah that's real nice, I focused on writing it out fully so I could explain the logic, although I didn't know to even do any of that. Still got a lot of learning about java functions. Very nice.

Link to comment
Share on other sites

Position closest = Arrays.stream(positions)
                         .min((p1, p2) -> Integer.compare(
                             myPosition().distance(p1),
                             myPosition().distance(p2)
                         ))
                         .get();

 

 

First off thanks to both of you for helping me out here. Explv could you break that down and explain the logic behind it? Like let me know what each piece of it does. I know it's cumbersome but that's the best way for me to learn, I'd really appreciate it :)

Link to comment
Share on other sites

First off thanks to both of you for helping me out here. Explv could you break that down and explain the logic behind it? Like let me know what each piece of it does. I know it's cumbersome but that's the best way for me to learn, I'd really appreciate it smile.png

Position[] positions = { new Position(1, 2, 3), new Position(4, 5, 6) };

Position closest = Arrays.stream(positions) // Create a Stream<Position> from the Position[] positions
      .min( // Find the minimum in the Stream using the following Comparator<Position>
          (p1, p2) -> // For each two Positions in the Stream<Position> 
              Integer.compare( // Return the Integer comparsion of
                  myPosition().distance(p1), // The players distance to the first position
                  myPosition().distance(p2)  // And the players distance to the second position
              )
      )
      .get(); // .min() returns an Optional<Position> so we use .get() to retrieve the position

Or more explicitly:

Position[] positions = { new Position(1, 2, 3), new Position(4, 5, 6) };

Stream<Position> positionStream = Arrays.stream(positions);
        
Comparator<Position> positionComparator = new Comparator<Position>() {
    @ Override
    public int compare(Position p1, Position p2) {
        return Integer.compare(
            myPosition().distance(p1),
            myPosition().distance(p2)
        );
    }
};

Optional<Position> positionOptional = positionStream.min(positionComparator);

Position closest = positionOptional.get();
Edited by Explv
  • Like 1
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...