Navigation & Pathfinding
The navigation system provides pathfinding and movement capabilities for NPCs using A* pathfinding algorithms and steering behaviors.
Overview
Section titled “Overview”Located at com.hypixel.hytale.server.npc.navigation and com.hypixel.hytale.server.npc.movement
The navigation system consists of:
- Pathfinding - A* algorithm for finding optimal paths
- Path Following - Following computed paths
- Steering - Low-level movement control
- Motion Controllers - High-level movement behaviors
- Collision Avoidance - Avoiding obstacles and other entities
Pathfinding
Section titled “Pathfinding”A* Implementation
Section titled “A* Implementation”The core A* pathfinding is implemented through several classes:
AStarBase
Section titled “AStarBase”Located at com.hypixel.hytale.server.npc.navigation.AStarBase
Base class for A* pathfinding:
- Node expansion
- Path cost calculation
- Heuristic evaluation
- Open/closed set management
AStarWithTarget
Section titled “AStarWithTarget”Located at com.hypixel.hytale.server.npc.navigation.AStarWithTarget
A* implementation for finding paths to a specific target:
AStarWithTarget pathfinder = new AStarWithTarget();Path path = pathfinder.findPath(startPos, targetPos, evaluator);AStarEvaluator
Section titled “AStarEvaluator”Located at com.hypixel.hytale.server.npc.navigation.AStarEvaluator
Evaluates nodes during pathfinding:
- Determines if a position is walkable
- Calculates movement cost
- Applies penalties for different terrain
- Checks environmental constraints
Node Management
Section titled “Node Management”AStarNode
Section titled “AStarNode”Located at com.hypixel.hytale.server.npc.navigation.AStarNode
Represents a node in the pathfinding graph:
- Position (x, y, z)
- Cost values (g-cost, h-cost, f-cost)
- Parent node reference
- Node state
Node Pools
Section titled “Node Pools”For performance, nodes are pooled and reused:
AStarNodePool
Section titled “AStarNodePool”Located at com.hypixel.hytale.server.npc.navigation.AStarNodePool
Interface for node pool management.
AStarNodePoolSimple
Section titled “AStarNodePoolSimple”Located at com.hypixel.hytale.server.npc.navigation.AStarNodePoolSimple
Simple node pool implementation:
AStarNodePoolSimple pool = new AStarNodePoolSimple(maxNodes);AStarNode node = pool.acquire();// Use node...pool.release(node);AStarNodePoolProvider
Section titled “AStarNodePoolProvider”Located at com.hypixel.hytale.server.npc.navigation.AStarNodePoolProvider
Provides access to node pools:
- Thread-safe pool management
- Pool size configuration
- Pool recycling
Path System
Section titled “Path System”IWaypoint
Section titled “IWaypoint”Located at com.hypixel.hytale.server.npc.navigation.IWaypoint
Interface for waypoints in a path:
Vector3d getPosition();boolean isReached(Vector3d currentPos, double threshold);PathFollower
Section titled “PathFollower”Located at com.hypixel.hytale.server.npc.navigation.PathFollower
Follows a computed path:
- Tracks current waypoint
- Determines when waypoints are reached
- Advances to next waypoint
- Handles path completion
Usage:
PathFollower follower = new PathFollower(path);follower.update(currentPosition, dt);Vector3d targetWaypoint = follower.getCurrentWaypoint();Path Integration
Section titled “Path Integration”Paths integrate with the broader path system:
Located at com.hypixel.hytale.server.npc.path
Provides:
- Path definitions
- Path builders
- Path persistence
Related to the global path system at:
com.hypixel.hytale.builtin.path
Movement System
Section titled “Movement System”Located at com.hypixel.hytale.server.npc.movement
Steering
Section titled “Steering”Located at com.hypixel.hytale.server.npc.movement.Steering
Represents a steering force:
class Steering { Vector3d force; // Steering force direction/magnitude double desiredSpeed; // Desired movement speed}Used by motion controllers to influence NPC movement.
Motion Controllers
Section titled “Motion Controllers”Located at com.hypixel.hytale.server.npc.movement.controllers
Motion controllers provide high-level movement behaviors:
MotionController
Section titled “MotionController”Base interface for all motion controllers:
void computeSteering(Role role, Steering steering, double dt);void update(Role role, double dt);Controller Builders
Section titled “Controller Builders”Located at com.hypixel.hytale.server.npc.movement.controllers.builders
Builder pattern for creating motion controllers from asset data.
Steering Forces
Section titled “Steering Forces”Located at com.hypixel.hytale.server.npc.movement.steeringforces
Specific steering force implementations:
SteeringForceAvoidCollision
Section titled “SteeringForceAvoidCollision”Avoids collisions with obstacles and other entities:
SteeringForceAvoidCollision avoidance = new SteeringForceAvoidCollision();avoidance.compute(role, steering, obstacles);Features:
- Ray casting for obstacle detection
- Collision prediction
- Force calculation based on proximity
- View angle constraints
Group Steering
Section titled “Group Steering”GroupSteeringAccumulator
Section titled “GroupSteeringAccumulator”Located at com.hypixel.hytale.server.npc.movement.GroupSteeringAccumulator
Accumulates steering forces from multiple sources:
GroupSteeringAccumulator accumulator = new GroupSteeringAccumulator();accumulator.add(force1, weight1);accumulator.add(force2, weight2);Vector3d resultForce = accumulator.compute();Used for:
- Combining multiple behaviors
- Weighted force blending
- Flock behaviors
Collision Avoidance
Section titled “Collision Avoidance”The Role class contains collision avoidance parameters:
class Role { double collisionProbeDistance; // How far ahead to check double collisionRadius; // Collision detection radius double collisionForceFalloff; // Force reduction with distance float collisionViewAngle; // Field of view for detection double entityAvoidanceStrength; // Strength of avoidance force AvoidanceMode avoidanceMode; // Avoidance behavior mode}Avoidance Modes
Section titled “Avoidance Modes”Different strategies for avoiding entities:
- Avoid All - Avoid all entities
- Avoid Enemies - Only avoid hostile entities
- Avoid Non-Flock - Avoid entities not in the same flock
- None - No entity avoidance
Separation
Section titled “Separation”Flocking separation behavior:
class Role { double separationDistance; // Desired separation double separationWeight; // Separation force weight double separationDistanceTarget; // Target distance double separationNearRadiusTarget; // Near radius double separationFarRadiusTarget; // Far radius}Environmental Constraints
Section titled “Environmental Constraints”Stay in Environment
Section titled “Stay in Environment”NPCs can be constrained to specific environments:
class Role { boolean stayInEnvironment; // Whether to stay in allowed environments String allowedEnvironments; // Comma-separated environment IDs}If enabled, the NPC will avoid leaving its allowed environment.
Physics Integration
Section titled “Physics Integration”Located at com.hypixel.hytale.server.npc.util.NPCPhysicsMath
Provides physics calculations for NPC movement:
- Velocity integration
- Acceleration application
- Inertia calculations
- Knockback handling
class Role { double inertia; // Movement inertia (resistance to direction change) double knockbackScale; // Knockback force multiplier}Debugging
Section titled “Debugging”Debug Visualization
Section titled “Debug Visualization”Located at com.hypixel.hytale.server.npc.navigation.AStarDebugBase and AStarDebugWithTarget
Provides debugging visualization for pathfinding:
- Visualize explored nodes
- Show final path
- Display cost values
- Render waypoints
Integration with Instructions
Section titled “Integration with Instructions”Navigation integrates with the instruction system through:
- Body Motion - Sets steering forces for movement
- Sensor Info - Provides path and position data
- Path Providers - Supply path information to sensors/actions
Best Practices
Section titled “Best Practices”- Limit path length - Long paths are expensive to compute
- Reuse paths - Cache paths when target doesn’t move
- Update incrementally - Recompute paths periodically, not every frame
- Use path smoothing - Smooth jagged paths for natural movement
- Blend steering forces - Combine multiple behaviors smoothly
- Set appropriate costs - Configure terrain costs for desired behavior
Performance Optimization
Section titled “Performance Optimization”Node Pool Sizing
Section titled “Node Pool Sizing”Configure node pools based on expected path complexity:
// For short pathsAStarNodePoolSimple pool = new AStarNodePoolSimple(256);
// For long pathsAStarNodePoolSimple pool = new AStarNodePoolSimple(2048);Path Update Frequency
Section titled “Path Update Frequency”Don’t recompute paths every frame:
// Recompute every N seconds or when target moves significantlyif (timeSinceLastUpdate > 1.0 || targetMovedDistance > 5.0) { recomputePath();}Early Path Termination
Section titled “Early Path Termination”Stop pathfinding if path becomes too long:
if (pathLength > maxPathLength) { // Use simplified movement or give up}Spatial Partitioning
Section titled “Spatial Partitioning”Use spatial data structures for efficient obstacle queries:
- KD-trees for entity lookups
- Chunk-based world queries
- Cached terrain data
Common Movement Patterns
Section titled “Common Movement Patterns”Follow Entity
Section titled “Follow Entity”// Motion: Follow target entity// - Get target position// - Find path to target// - Follow path with path follower// - Update when target movesPatrol Path
Section titled “Patrol Path”// Motion: Patrol waypoints// - Define waypoint list// - Follow current waypoint// - Advance to next on reach// - Loop back to startWander
Section titled “Wander”// Motion: Random wandering// - Pick random target in radius// - Move to target// - Pick new target on arrival// - Stay within bounds// Motion: Flee from threat// - Get threat position// - Calculate direction away// - Move in opposite direction// - Maintain minimum distanceRelated Systems
Section titled “Related Systems”- Instructions - Uses navigation for motion
- Roles - Configures navigation parameters
- Flock System - Group movement behaviors
- Physics System - Applies movement forces to entities