Class AbstractPathfinder<S extends de.bsommerfeld.pathetic.engine.pathfinder.SearchState>

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

public abstract class AbstractPathfinder<S extends de.bsommerfeld.pathetic.engine.pathfinder.SearchState> 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. The main loop runs uninterrupted at full speed; each iteration expands exactly one node, and there is no timed scheduling or per-iteration delay of any kind. It supports asynchronous execution and customizable hooks for observing the pathfinding steps.

  • Field Details

  • Constructor Details

  • Method Details

    • findPath

      public PathfindingSearch 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 PathfindingSearch instance that can be used to configure and retrieve the result of the pathfinding operation.
    • registerPathfindingHook

      public void registerPathfindingHook(PathfinderHook hook)
      Description copied from interface: Pathfinder
      Registers a PathfinderHook that will be called on every step of subsequent pathfinding operations.

      Each search snapshots the currently registered hooks at the moment it starts. Hooks registered after that snapshot apply only to future searches, never to a search that is already running.

      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(PathPosition start, PathPosition target, 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.
    • createSearchState

      protected abstract S createSearchState(PathPosition start, int expectedNodes)
      Creates the per-search state holding this algorithm's open and closed sets. Called once at the start of every request; the returned instance lives as a stack local and is passed back into processSuccessors(de.bsommerfeld.pathetic.api.wrapper.PathPosition, de.bsommerfeld.pathetic.api.wrapper.PathPosition, de.bsommerfeld.pathetic.engine.Node, S, de.bsommerfeld.pathetic.api.pathing.processing.context.SearchContext), so it never needs to be stored on the pathfinder.
      Parameters:
      start - The effective (floored) start position of the request; implementations that key their per-search state relative to the search origin derive that origin from it.
      expectedNodes - Estimated number of nodes the search will touch; implementations should size their per-search structures from it so small requests pay small setup costs.
      Returns:
      a fresh, empty search state for this request.
    • processSuccessors

      protected abstract void processSuccessors(PathPosition requestStart, PathPosition requestTarget, Node currentNode, S state, 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 open set held by state.
      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.
      state - The per-search state holding the open and closed sets.