creationx Posted September 25, 2016 Share Posted September 25, 2016 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? Quote Link to comment Share on other sites More sharing options...
Night Posted September 25, 2016 Share Posted September 25, 2016 Create an enum with all the positions containing the position of each. Loop through the enum comparing the distance to each of the positions, and return the smallest. Quote Link to comment Share on other sites More sharing options...
Explv Posted September 25, 2016 Share Posted September 25, 2016 (edited) 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 September 25, 2016 by Explv 1 Quote Link to comment Share on other sites More sharing options...
venetox Posted September 25, 2016 Share Posted September 25, 2016 (edited) 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 September 25, 2016 by venetox 1 Quote Link to comment Share on other sites More sharing options...
Explv Posted September 25, 2016 Share Posted September 25, 2016 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(); 1 Quote Link to comment Share on other sites More sharing options...
venetox Posted September 25, 2016 Share Posted September 25, 2016 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. Quote Link to comment Share on other sites More sharing options...
creationx Posted September 25, 2016 Author Share Posted September 25, 2016 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 Quote Link to comment Share on other sites More sharing options...
Explv Posted September 25, 2016 Share Posted September 25, 2016 (edited) 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 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 September 25, 2016 by Explv 1 Quote Link to comment Share on other sites More sharing options...
Trees Posted September 26, 2016 Share Posted September 26, 2016 Position closest = Arrays.stream(positions) .min((p1, p2) -> Integer.compare( myPosition().distance(p1), myPosition().distance(p2) )) .get(); It's funny how it tries to mimic linq in .net but it still looks so gross. Quote Link to comment Share on other sites More sharing options...