Class AbstractPathfinder

java.lang.Object
de.bsommerfeld.pathetic.engine.pathfinder.AbstractPathfinder
All Implemented Interfaces:
Pathfinder
Direct Known Subclasses:
AStarPathfinder

public abstract class AbstractPathfinder extends Object implements Pathfinder
Provides a skeletal implementation of the Pathfinder interface, defining common behavior for pathfinding algorithms.

This pathfinder operates by iteratively processing nodes from an open set (priority queue) until the target is reached or other termination conditions are met. It supports asynchronous execution and customizable hooks for observing the pathfinding steps. The "tick-wise" nature mentioned previously refers to each main loop iteration processing one node.

  • Field Details

  • Constructor Details

  • Method Details

    • findPath

      public CompletionStage<PathfinderResult> findPath(PathPosition start, PathPosition target, EnvironmentContext environmentContext)
      Description copied from interface: Pathfinder
      Tries to find a path between the specified start and target positions within the provided environment context.
      Specified by:
      findPath in interface Pathfinder
      Parameters:
      start - The starting position for pathfinding.
      target - The target position for pathfinding.
      environmentContext - The environment context that provides additional information for the pathfinding operation. This parameter can be null if no specific context is required.
      Returns:
      A CompletionStage containing the PathfinderResult of the pathfinding operation, indicating the outcome and the resulting path.
    • abort

      public void abort()
      Requests the current pathfinding operation to abort. The abortion is cooperative and might not be immediate.
      Specified by:
      abort in interface Pathfinder
      See Also:
    • registerPathfindingHook

      public void registerPathfindingHook(PathfinderHook hook)
      Description copied from interface: Pathfinder
      Registers a PathfinderHook that will be called on every step of the pathfinding process. This can be used to modify the pathfinding process or to collect data.
      Specified by:
      registerPathfindingHook in interface Pathfinder
      Parameters:
      hook - The hook to register.
    • createStartNode

      protected Node createStartNode(PathPosition startPos, PathPosition targetPos)
      Creates the initial Node for the start position.
      Parameters:
      startPos - The effective start position.
      targetPos - The effective target position.
      Returns:
      The created start node.
    • reconstructPath

      protected Path reconstructPath(Node endNode)
      Reconstructs the path by tracing back from the given end node to the start node.
      Parameters:
      endNode - The node from which to trace back.
      Returns:
      The reconstructed Path.
    • initializeSearch

      protected abstract void initializeSearch()
      Prepares the algorithm-specific initial setup required before executing the pathfinding logic. This method is designed to be overridden by subclasses to implement their respective initialization logic, such as setting up data structures, precomputing values, or resetting internal state. It is called at the beginning of a pathfinding request.
    • markNodeAsExpanded

      protected abstract void markNodeAsExpanded(Node node)
      Marks the given node as expanded (i.e., added to the "closed set"). Subclasses should implement this to update their specific closed set mechanism.
      Parameters:
      node - The node that has been taken from the open set and is being expanded.
    • performAlgorithmCleanup

      protected abstract void performAlgorithmCleanup()
      Abstract method for algorithm-specific cleanup, called after pathfinding execution. To be implemented by subclasses like AStarPathfinder.
    • processSuccessors

      protected abstract void processSuccessors(PathPosition requestStart, PathPosition requestTarget, Node currentNode, org.jheaps.tree.FibonacciHeap<Double,Node> openSet, SearchContext searchContext)
      Abstract method representing the core logic of processing successor nodes for a given currentNode. Implementations (like A*) should:
      1. Generate potential successor positions.
      2. Create Node objects for these successors.
      3. Validate these nodes (e.g., traversability, bounds, visited status). This is where processors will hook in.
      4. Calculate their G and H costs. G-costs will be influenced by cost processors.
      5. Add valid successor nodes with their F-costs to the openSet.
      Parameters:
      requestStart - The original start PathPosition of the pathfinding request.
      requestTarget - The original target PathPosition of the pathfinding request.
      currentNode - The current Node being expanded.
      openSet - The priority queue (open set) to add new successor nodes to.