Jump to content

meanzie

Members
  • Posts

    1
  • Joined

  • Last visited

  • Feedback

    0%

Profile Information

  • Gender
    Male

meanzie's Achievements

Newbie

Newbie (1/10)

0

Reputation

  1. Hello there, so ehh, I started working on my first bot script today, And I couldn't really find any good pathfinding utilities to use, so I threw up one on my own. /** * Tries to find the best node in the path to walk on * * @param path * * @return */ public Position getBestNode(Position[] path) { int radius = 15; List<Integer> candidates = new LinkedList<>(); for (int i = 0; i < path.length; i++) { if (path[i].distance(myPosition()) < radius) { candidates.add(i); } } if (candidates.size() == 0) { warn("Tried to find best node for path, but no candidates were found."); return null; } int bestCandidate = 0; for (int i : candidates) { if (path[i].distance(path[path.length - 1]) < path[bestCandidate] .distance(path[path.length - 1])) { bestCandidate = i; } } // Check if this is the last point in the path if (bestCandidate + 1 >= path.length) { return path[bestCandidate]; } // We should try and interpolate between this point and the next point // so that distance is always radius Position pointA = path[bestCandidate]; Position pointB = path[bestCandidate + 1]; // So let's take p2 - p1 and get the direction vector int dirX = pointB.getX() - pointA.getX(); int dirY = pointB.getY() - pointA.getY(); // Now find a's distance to the target double distA = pointA.distance(myPosition()); // Let's normalize our direction vector double dirVecL = Math.sqrt(dirX * dirX + dirY * dirY); double dirVecX = dirX / dirVecL; double dirVecY = dirY / dirVecL; // Now let's create our destination vector Position pointD = new Position( (int) (dirVecX * (radius - distA) + pointA.getX()), (int) (dirVecY * (radius - distA) + pointA.getY()), 0); return pointD; } This function assumes that you are giving it a forward path, meaning that path[0] is the start point and path[n] is the end point. The script will find the node that is closest to the player and closest to the target. Of course there is room for improvements, and I am sure there are scenarios where this method wouldn't work. Also, each path node has to be < 15 tiles away from each other, or else it won't find them.
×
×
  • Create New...