Cuberite
A lightweight, fast and extensible game server for Minecraft
Public Member Functions | Private Member Functions | Private Attributes | List of all members
cPathFinder Class Reference

This class wraps cPath. More...

#include <PathFinder.h>

Collaboration diagram for cPathFinder:
Collaboration graph
[legend]

Public Member Functions

 cPathFinder (float a_MobWidth, float a_MobHeight)
 Creates a cPathFinder instance. More...
 
ePathFinderStatus GetNextWayPoint (cChunk &a_Chunk, const Vector3d &a_Source, Vector3d *a_Destination, Vector3d *a_OutputWaypoint, bool a_DontCare=false)
 Updates the PathFinder's internal state and returns a waypoint. More...
 

Private Member Functions

bool EnsureProperPoint (Vector3d &a_Vector, cChunk &a_Chunk)
 Ensures the location is not in the air or under water. More...
 
bool IsWaterOrSolid (BLOCKTYPE a_BlockType)
 Return true the the blocktype is either water or solid. More...
 
bool PathIsTooOld () const
 Is the path too old and should be recalculated? When this is true ResetPathFinding() is called. More...
 
void ResetPathFinding (cChunk &a_Chunk)
 Resets a pathfinding task, typically because m_FinalDestination has deviated too much from m_DeviationOrigin. More...
 

Private Attributes

Vector3d m_DeviationOrigin
 When FinalDestination is too far from this, we recalculate. More...
 
Vector3d m_FinalDestination
 Coordinates for where we should go. More...
 
int m_GiveUpCounter
 If 0, will give up reaching the next m_WayPoint and will recalculate path. More...
 
float m_Height
 The height of the Mob which owns this PathFinder. More...
 
bool m_NoPathToTarget
 True if there's no path to target and we're walking to a nearby location instead. More...
 
int m_NotFoundCooldown
 When a path is not found, this cooldown prevents any recalculations for several ticks. More...
 
std::unique_ptr< cPathm_Path
 The current cPath instance we have. More...
 
Vector3d m_PathDestination
 Coordinates for where we are practically going. More...
 
Vector3d m_Source
 Coordinates for where the mob is currently at. More...
 
Vector3d m_WayPoint
 Coordinates of the next position that should be reached. More...
 
float m_Width
 The width of the Mob which owns this PathFinder. More...
 

Detailed Description

This class wraps cPath.

cPath is a "dumb device" - You give it point A and point B, and it returns a full path path. cPathFinder - You give it a constant stream of point A (where you are) and point B (where you want to go), and it tells you where to go next. It manages path recalculation internally, and is much more efficient that calling cPath every step.

Definition at line 11 of file PathFinder.h.

Constructor & Destructor Documentation

◆ cPathFinder()

cPathFinder::cPathFinder ( float  a_MobWidth,
float  a_MobHeight 
)

Creates a cPathFinder instance.

Each mob should have one cPathFinder throughout its lifetime.

Parameters
a_MobWidthThe mob width.
a_MobHeightThe mob height.

Definition at line 11 of file PathFinder.cpp.

Member Function Documentation

◆ EnsureProperPoint()

bool cPathFinder::EnsureProperPoint ( Vector3d a_Vector,
cChunk a_Chunk 
)
private

Ensures the location is not in the air or under water.

May change the Y coordinate of the given vector.

  1. If a_Vector is the position of water, a_Vector's Y will be modified to point to the first air block above it.
  2. If a_Vector is the position of air, a_Vector's Y will be modified to point to the first airblock below it which has solid or water beneath.

Definition at line 177 of file PathFinder.cpp.

◆ GetNextWayPoint()

ePathFinderStatus cPathFinder::GetNextWayPoint ( cChunk a_Chunk,
const Vector3d a_Source,
Vector3d a_Destination,
Vector3d a_OutputWaypoint,
bool  a_DontCare = false 
)

Updates the PathFinder's internal state and returns a waypoint.

A waypoint is a coordinate which the mob can safely move to from its current position in a straight line. The mob is expected to call this function tick as long as it is following a path.

Parameters
a_ChunkThe chunk in which the mob is currently at.
a_SourceThe mob's position. a_Source's coordinates are expected to be within the chunk given in a_Chunk.
a_DestinationThe position the mob would like to reach. If a_ExactPath is true, the PathFinder may modify this.
a_OutputWaypointAn output parameter: The next waypoint to go to.
a_DontCareIf true, the mob doesn't care where to go, and the Pathfinder may modify a_Destination. This should usually be false. An exception is a wandering idle mob which doesn't care about its final destination. In the future, idle mobs shouldn't use A* at all.

Returns an ePathFinderStatus. ePathFinderStatus:CALCULATING - The PathFinder is still processing a path. Nothing was written to a_OutputWaypoint. The mob should probably not move. ePathFinderStatus:PATH_FOUND - The PathFinder has found a path to the target. The next waypoint was written a_OutputWaypoint. The mob should probably move to a_OutputWaypoint. ePathFinderStatus:NEARBY_FOUND - The PathFinder did not find a destination to the target but did find a nearby spot. The next waypoint was written a_OutputWaypoint. The mob should probably move to a_OutputWaypoint. ePathFinderStatus:PATH_NOT_FOUND - The PathFinder did not find a destination to the target. Nothing was written to a_OutputWaypoint. The mob should probably not move.

Note: Once NEARBY_FOUND is returned once, subsequent calls return PATH_FOUND.

Definition at line 23 of file PathFinder.cpp.

◆ IsWaterOrSolid()

bool cPathFinder::IsWaterOrSolid ( BLOCKTYPE  a_BlockType)
private

Return true the the blocktype is either water or solid.

Definition at line 269 of file PathFinder.cpp.

◆ PathIsTooOld()

bool cPathFinder::PathIsTooOld ( ) const
private

Is the path too old and should be recalculated? When this is true ResetPathFinding() is called.

Definition at line 278 of file PathFinder.cpp.

◆ ResetPathFinding()

void cPathFinder::ResetPathFinding ( cChunk a_Chunk)
private

Resets a pathfinding task, typically because m_FinalDestination has deviated too much from m_DeviationOrigin.

Definition at line 164 of file PathFinder.cpp.

Member Data Documentation

◆ m_DeviationOrigin

Vector3d cPathFinder::m_DeviationOrigin
private

When FinalDestination is too far from this, we recalculate.

This usually equals PathDestination. Except when m_NoPathToTarget is true.

Definition at line 66 of file PathFinder.h.

◆ m_FinalDestination

Vector3d cPathFinder::m_FinalDestination
private

Coordinates for where we should go.

This is out ultimate, final destination.

Definition at line 59 of file PathFinder.h.

◆ m_GiveUpCounter

int cPathFinder::m_GiveUpCounter
private

If 0, will give up reaching the next m_WayPoint and will recalculate path.

Definition at line 53 of file PathFinder.h.

◆ m_Height

float cPathFinder::m_Height
private

The height of the Mob which owns this PathFinder.

Definition at line 47 of file PathFinder.h.

◆ m_NoPathToTarget

bool cPathFinder::m_NoPathToTarget
private

True if there's no path to target and we're walking to a nearby location instead.

Definition at line 73 of file PathFinder.h.

◆ m_NotFoundCooldown

int cPathFinder::m_NotFoundCooldown
private

When a path is not found, this cooldown prevents any recalculations for several ticks.

Definition at line 76 of file PathFinder.h.

◆ m_Path

std::unique_ptr<cPath> cPathFinder::m_Path
private

The current cPath instance we have.

This is discarded and recreated when a path recalculation is needed.

Definition at line 50 of file PathFinder.h.

◆ m_PathDestination

Vector3d cPathFinder::m_PathDestination
private

Coordinates for where we are practically going.

Definition at line 62 of file PathFinder.h.

◆ m_Source

Vector3d cPathFinder::m_Source
private

Coordinates for where the mob is currently at.

Definition at line 70 of file PathFinder.h.

◆ m_WayPoint

Vector3d cPathFinder::m_WayPoint
private

Coordinates of the next position that should be reached.

Definition at line 56 of file PathFinder.h.

◆ m_Width

float cPathFinder::m_Width
private

The width of the Mob which owns this PathFinder.

Definition at line 44 of file PathFinder.h.


The documentation for this class was generated from the following files: