Class LocalPathfinder


  • @Singleton
    public class LocalPathfinder
    extends java.lang.Object

    The LocalPathfinder class is responsible for pathfinding within a local 104x104 tile 3D game scene. It provides methods to compute paths using Breadth First Search (BFS), determine sparse paths for waypoints where directional changes occur, render paths visually, and validate the reachability of points within the currently loaded scene. This class is useful for AI, navigation, and player movement scenarios.

    The class supports the following functionalities:

    • Constructor Summary

      Constructors 
      Constructor Description
      LocalPathfinder()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<net.runelite.api.coords.WorldPoint> findApproximatePath​(net.runelite.api.coords.WorldPoint start, net.runelite.api.coords.WorldArea area)
      Computes an approximate path from a starting point to a destination within a specified area.
      java.util.List<net.runelite.api.coords.WorldPoint> findApproximatePath​(net.runelite.api.coords.WorldPoint start, net.runelite.api.coords.WorldPoint target)
      Finds an approximate path from a starting point to a target point within a given radius.
      java.util.List<net.runelite.api.coords.WorldPoint> findApproximatePath​(net.runelite.api.coords.WorldPoint start, net.runelite.api.coords.WorldPoint target, int radius)
      Finds an approximate path from a starting point to a target point within a given radius.
      java.util.List<net.runelite.api.coords.WorldPoint> findPath​(net.runelite.api.coords.WorldPoint start, net.runelite.api.coords.WorldPoint target)
      Calculates and returns a path from a starting point to a target point within the game world.
      java.util.List<net.runelite.api.coords.WorldPoint> findSparsePath​(net.runelite.api.coords.WorldPoint start, net.runelite.api.coords.WorldPoint target)
      Finds a sparse path between a starting point and a target point by filtering out unnecessary intermediate points from a previously computed dense path.
      boolean isInScene​(net.runelite.api.coords.WorldPoint worldPoint)
      Returns true if the provided WorldPoint is in the currently loaded scene data.
      void renderMinimapPath​(java.util.List<net.runelite.api.coords.WorldPoint> path, java.awt.Graphics2D graphics, java.awt.Color color)
      Renders a path on the minimap.
      void renderPath​(java.util.List<net.runelite.api.coords.WorldPoint> path, java.awt.Graphics2D graphics, java.awt.Color pathColor)
      Renders a series of tiles representing a path on the game canvas.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LocalPathfinder

        public LocalPathfinder()
    • Method Detail

      • findSparsePath

        public java.util.List<net.runelite.api.coords.WorldPoint> findSparsePath​(net.runelite.api.coords.WorldPoint start,
                                                                                 net.runelite.api.coords.WorldPoint target)
        Finds a sparse path between a starting point and a target point by filtering out unnecessary intermediate points from a previously computed dense path.

        The method calculates directional changes in the dense path and retains only the waypoints where the direction changes, along with the final destination. This ensures a simplified path that accurately represents the required turns or path changes while omitting redundant points.

        Parameters:
        start - @WorldPoint representing the starting location of the path.
        target - @WorldPoint representing the destination point of the path.
        Returns:
        A @List of @WorldPoint objects representing the sparse path. Returns an empty list if no path can be computed.
      • findPath

        public java.util.List<net.runelite.api.coords.WorldPoint> findPath​(net.runelite.api.coords.WorldPoint start,
                                                                           net.runelite.api.coords.WorldPoint target)
        Calculates and returns a path from a starting point to a target point within the game world. If the target point is outside the scene, the method attempts to determine the edge of the scene closest to the target and calculates a path to that edge instead.

        If the target point is within the loaded scene, the method directly computes the path to the target using the findScenePath method. If the target is outside the scene, it finds the nearest edge point to the target and calculates a path to that point.

        Parameters:
        start - @WorldPoint representing the starting location of the path.
        target - @WorldPoint representing the destination point of the path.
        Returns:
        A @List of @WorldPoint objects representing the calculated path from the start to the target (or closest reachable edge point). If no path can be calculated, an empty list is returned.
      • findApproximatePath

        public java.util.List<net.runelite.api.coords.WorldPoint> findApproximatePath​(net.runelite.api.coords.WorldPoint start,
                                                                                      net.runelite.api.coords.WorldPoint target)
        Finds an approximate path from a starting point to a target point within a given radius.

        This method searches for paths using a breadth-first search (BFS) approach. If the target point is not within the game scene, a full path is computed to the target. Otherwise, the method will look for an approximate path to a point within the specified radius of the target that is traversable in the scene.

        Parameters:
        start - the WorldPoint representing the starting point of the path.
        target - the WorldPoint representing the target point of the path. If it lies outside the game scene, a full path is calculated for this target.
        Returns:
        a List of WorldPoint instances representing the path from the start to the selected point within the specified radius of the target. If no valid candidates are found, an approximate path to the target is returned. The list is in order of movement from start to destination.

        Steps performed in the method:

        • If the target is not in the scene, calculate and return the full path to the target using findPath.
        • Run a breadth-first search (BFS) to determine distances from the start point and record parent nodes.
        • Search for all points within the specified radius of the target that are in the scene and traversable.
        • If valid points are found and randomly select one of them, return the path to that point.
        • If no valid points are found, calculate and return an approximate path to the target.
      • findApproximatePath

        public java.util.List<net.runelite.api.coords.WorldPoint> findApproximatePath​(net.runelite.api.coords.WorldPoint start,
                                                                                      net.runelite.api.coords.WorldPoint target,
                                                                                      int radius)
        Finds an approximate path from a starting point to a target point within a given radius.

        This method searches for paths using a breadth-first search (BFS) approach. If the target point is not within the game scene, a full path is computed to the target. Otherwise, the method will look for an approximate path to a point within the specified radius of the target that is traversable in the scene.

        Parameters:
        start - the WorldPoint representing the starting point of the path.
        target - the WorldPoint representing the target point of the path. If it lies outside the game scene, a full path is calculated for this target.
        radius - the radius, in tiles, within which the method will search for a traversable point near the target.
        Returns:
        a List of WorldPoint instances representing the path from the start to the selected point within the specified radius of the target. If no valid candidates are found, an approximate path to the target is returned. The list is in order of movement from start to destination.

        Steps performed in the method:

        • If the target is not in the scene, calculate and return the full path to the target using findPath.
        • Run a breadth-first search (BFS) to determine distances from the start point and record parent nodes.
        • Search for all points within the specified radius of the target that are in the scene and traversable.
        • If valid points are found and randomly select one of them, return the path to that point.
        • If no valid points are found, calculate and return an approximate path to the target.
      • findApproximatePath

        public java.util.List<net.runelite.api.coords.WorldPoint> findApproximatePath​(net.runelite.api.coords.WorldPoint start,
                                                                                      net.runelite.api.coords.WorldArea area)
        Computes an approximate path from a starting point to a destination within a specified area. This method identifies candidates within the area that are reachable and constructs a path to one of the candidates. It uses breadth-first search (BFS) to calculate distances and possible paths.

        The method performs pathfinding only if the specified area and starting point are on the same plane.

        Parameters:
        start - the starting WorldPoint from which the pathfinding begins.
        area - the WorldArea that defines the bounds of the target area where the path should lead. The method will consider reachable points only within this area.
        Returns:
        a List of WorldPoints representing the calculated path to a candidate within the defined area, or an empty list if no valid path could be determined.
        • Returns an empty list if the area is outside the current viewport or on a different plane.
        • Returns an empty list if no reachable candidate points exist within the WorldArea.
      • isInScene

        public boolean isInScene​(net.runelite.api.coords.WorldPoint worldPoint)
        Returns true if the provided WorldPoint is in the currently loaded scene data.
        Parameters:
        worldPoint - The point to check.
        Returns:
        True if the point is in the scene, false otherwise.
      • renderMinimapPath

        public void renderMinimapPath​(java.util.List<net.runelite.api.coords.WorldPoint> path,
                                      java.awt.Graphics2D graphics,
                                      java.awt.Color color)
        Renders a path on the minimap.
        Parameters:
        path - The list of WorldPoints representing the path.
        graphics - The graphics context to draw on.
        color - The color to use for drawing the path.
      • renderPath

        public void renderPath​(java.util.List<net.runelite.api.coords.WorldPoint> path,
                               java.awt.Graphics2D graphics,
                               java.awt.Color pathColor)
        Renders a series of tiles representing a path on the game canvas. This includes drawing connected lines between the tiles and optionally highlighting the last tile in the path.

        The method uses the provided Graphics2D instance to draw on the screen and a Color to style the tiles.

        Parameters:
        path - The list of WorldPoint objects representing the path. Each point is rendered on the game canvas.
        graphics - The Graphics2D instance used to render the path on the screen.
        pathColor - The Color used to draw the tiles on the path. The last tile is highlighted in red with partial transparency.