Instructions
Instructions are the core behavioral building blocks of the NPC AI system. They combine sensors (conditions), actions (behaviors), and motions (movement) to create complete NPC behaviors.
Overview
Section titled “Overview”Located at com.hypixel.hytale.server.npc.instructions
An instruction consists of:
- Sensor - Detects when the instruction should execute
- Body Motion (optional) - Controls body/movement steering
- Head Motion (optional) - Controls head orientation
- Actions - Executes specific behaviors
- Child Instructions (optional) - Nested instruction tree
Instruction Class
Section titled “Instruction Class”The Instruction class provides the main instruction implementation:
public class Instruction implements RoleStateChange, IAnnotatedComponentKey Properties
Section titled “Key Properties”sensor- The sensor that determines when to executebodyMotion- Optional body steering controllerheadMotion- Optional head steering controlleractions- List of actions to executeinstructionList- Array of child instructionstreeMode- Whether to use tree-based evaluationcontinueAfter- Whether to continue to next instruction after match
Execution Flow
Section titled “Execution Flow”- Match:
matches()checks if the sensor conditions are met - Execute:
execute()runs the instruction’s actions and motions - Complete: Cleanup and state updates
Sensors
Section titled “Sensors”Sensors determine when an instruction should execute by checking conditions.
Sensor Interface
Section titled “Sensor Interface”Located at com.hypixel.hytale.server.npc.instructions.Sensor
public interface Sensor extends RoleStateChange, IAnnotatedComponent { boolean matches(Ref<EntityStore> ref, Role role, double dt, Store<EntityStore> store); void done(); InfoProvider getSensorInfo();}NullSensor
Section titled “NullSensor”Default sensor that always returns false:
public static final Sensor NULL = new NullSensor();Sensor Info
Section titled “Sensor Info”Sensors can provide information through InfoProvider:
Located at com.hypixel.hytale.server.npc.sensorinfo.InfoProvider
Provides access to:
- Detected entities
- Positions
- Paths
- Custom data
Info Providers
Section titled “Info Providers”Located at com.hypixel.hytale.server.npc.sensorinfo
PositionProvider
Section titled “PositionProvider”Provides position information:
EntityPositionProvider- Position of an entityCachedPositionProvider- Cached position dataIPositionProvider- Interface for custom position providers
PathProvider
Section titled “PathProvider”Provides path information:
PathProvider- Path data for navigationIPathProvider- Interface for custom path providers
Wrapped Providers
Section titled “Wrapped Providers”WrappedInfoProvider- Wraps another providerValueWrappedInfoProvider- Wraps a valueExtraInfoProvider- Additional custom data
Core Component Sensors
Section titled “Core Component Sensors”NPCs use builder-based sensors registered through the core components system:
Located at com.hypixel.hytale.server.npc.corecomponents
Categories:
- Entity sensors - Detect entities
- Combat sensors - Combat-related detection
- Interaction sensors - Player interactions
- World sensors - World state
- Timer sensors - Time-based triggers
- Debug sensors - Debugging tools
Actions
Section titled “Actions”Actions define what happens when an instruction executes.
Action Class
Section titled “Action Class”Located at com.hypixel.hytale.server.npc.instructions.Action
Actions can:
- Modify NPC state
- Trigger events
- Interact with the world
- Execute combat behaviors
- Manage inventory
ActionList
Section titled “ActionList”Located at com.hypixel.hytale.server.npc.instructions.ActionList
Manages multiple actions that execute together:
actionList.execute(ref, role, sensorInfo, dt, store);Features:
- Sequential execution
- Conditional execution
- Action prioritization
Core Component Actions
Section titled “Core Component Actions”Action builders registered through the core components:
Entity Actions
Section titled “Entity Actions”Located at com.hypixel.hytale.server.npc.corecomponents.entity
- Set target entity
- Track entity
- Clear entity reference
Combat Actions
Section titled “Combat Actions”Located at com.hypixel.hytale.server.npc.corecomponents.combat
- Attack target
- Flee from enemy
- Use abilities
Interaction Actions
Section titled “Interaction Actions”Located at com.hypixel.hytale.server.npc.corecomponents.interaction
- Trigger interactions
- Display dialogue
- Trade with player
Lifecycle Actions
Section titled “Lifecycle Actions”Located at com.hypixel.hytale.server.npc.corecomponents.lifecycle
- Spawn entities
- Despawn self
- Change role
State Machine Actions
Section titled “State Machine Actions”Located at com.hypixel.hytale.server.npc.corecomponents.statemachine
- Change state
- Set state variables
- Trigger state transitions
Timer Actions
Section titled “Timer Actions”Located at com.hypixel.hytale.server.npc.corecomponents.timer
- Start timers
- Stop timers
- Reset timers
Item Actions
Section titled “Item Actions”Located at com.hypixel.hytale.server.npc.corecomponents.items
- Equip items
- Use items
- Drop items
Utility Actions
Section titled “Utility Actions”Located at com.hypixel.hytale.server.npc.corecomponents.utility
- Debug logging
- Play sounds
- Spawn particles
World Actions
Section titled “World Actions”Located at com.hypixel.hytale.server.npc.corecomponents.world
- Place blocks
- Break blocks
- Interact with world
Motions
Section titled “Motions”Motions control NPC movement and orientation.
BodyMotion
Section titled “BodyMotion”Located at com.hypixel.hytale.server.npc.instructions.BodyMotion
Controls body movement steering:
void preComputeSteering(Ref<EntityStore> ref, Role role, InfoProvider info, Store<EntityStore> store);Features:
- Steering force calculation
- Pathfinding integration
- Collision avoidance
- Speed control
HeadMotion
Section titled “HeadMotion”Located at com.hypixel.hytale.server.npc.instructions.HeadMotion
Controls head orientation:
void preComputeSteering(Ref<EntityStore> ref, Role role, InfoProvider info, Store<EntityStore> store);Features:
- Look at target
- Head tracking
- Independent of body rotation
Motion Class
Section titled “Motion Class”Base motion interface:
Located at com.hypixel.hytale.server.npc.instructions.Motion
Provides lifecycle methods and steering computation.
Core Component Motions
Section titled “Core Component Motions”Motion builders for common movement patterns:
Located at com.hypixel.hytale.server.npc.corecomponents.movement
Motion types:
- Follow - Follow an entity
- Wander - Random wandering
- Patrol - Follow a path
- Flee - Move away from threat
- Approach - Move toward target
- Circle - Circle around point
- Flock - Flock movement (see Flock System)
Instruction Tree
Section titled “Instruction Tree”Instructions can be organized in a tree structure for complex behaviors.
Tree Mode
Section titled “Tree Mode”When treeMode = true:
- Instructions evaluate children sequentially
- Success/failure propagates up the tree
- Allows complex decision trees
- Similar to behavior trees in other engines
Continue After
Section titled “Continue After”When continueAfter = true:
- Execution continues to next instruction after match
- Allows multiple instructions to execute per tick
- Useful for layered behaviors
Root Instruction
Section titled “Root Instruction”Create a root instruction containing child instructions:
Instruction root = Instruction.createRootInstruction( childInstructions, support);Randomized Instructions
Section titled “Randomized Instructions”Located at com.hypixel.hytale.server.npc.instructions.InstructionRandomized
Randomly selects from a set of instructions:
InstructionRandomized randomBehavior = new InstructionRandomized( possibleInstructions, weights);Role State Change
Section titled “Role State Change”Located at com.hypixel.hytale.server.npc.instructions.RoleStateChange
Interface for components that react to role changes:
void stateChanged(Ref<EntityStore> ref, Role role, ComponentAccessor<EntityStore> accessor);void roleChanged(Ref<EntityStore> ref, Role role, ComponentAccessor<EntityStore> accessor);Implemented by:
- Instructions
- Sensors
- Actions
- Motions
Lifecycle Methods
Section titled “Lifecycle Methods”Instructions respond to NPC lifecycle events:
void loaded(Role role); // NPC asset loadedvoid spawned(Role role); // NPC spawned in worldvoid unloaded(Role role); // NPC asset unloadedvoid removed(Role role); // NPC removed from worldvoid teleported(Role role, World from, World to); // NPC teleportedInstruction Builders
Section titled “Instruction Builders”Located at com.hypixel.hytale.server.npc.instructions.builders
Builder pattern for creating instructions from asset data:
BuilderInstruction builder = new BuilderInstruction();Instruction instruction = builder.build(assetData, support);Component Annotations
Section titled “Component Annotations”Instructions implement IAnnotatedComponent for debugging and inspection:
Located at com.hypixel.hytale.server.npc.util.IAnnotatedComponent
Provides:
- Component hierarchy
- Debug labels
- Component info
- Breadcrumb navigation
Examples
Section titled “Examples”Simple Instruction
Section titled “Simple Instruction”// When enemy nearby, attackInstruction attackInstruction = new Instruction( enemySensor, // Sensor: detect enemy approachMotion, // Motion: move toward enemy lookAtMotion, // Head: look at enemy attackAction // Action: perform attack);Instruction Tree
Section titled “Instruction Tree”// Root behaviorInstruction root = new Instruction( alwaysSensor, null, null, new Instruction[] { combatInstruction, // Priority 1: Combat fleeInstruction, // Priority 2: Flee if low health wanderInstruction // Priority 3: Default wander });Conditional Chain
Section titled “Conditional Chain”// Sequential behavior with continueInstruction chain = new Instruction( triggerSensor, null, null, new Instruction[] { firstAction, // Execute first secondAction, // Then second thirdAction // Then third });// Set continueAfter = true on each childBest Practices
Section titled “Best Practices”- Keep sensors lightweight - They’re evaluated frequently
- Use sensor info - Pass data from sensors to actions efficiently
- Separate motion and actions - Makes behaviors more reusable
- Use tree mode - For complex decision logic
- Cache expensive operations - In the role’s support objects
- Profile instruction execution - Monitor performance of complex behaviors
Performance Tips
Section titled “Performance Tips”- Limit instruction tree depth
- Use early-exit conditions
- Cache sensor results when appropriate
- Avoid creating objects in hot paths
- Use object pools for temporary data
Related Systems
Section titled “Related Systems”- Decision Makers - Condition evaluation
- Navigation - Pathfinding and movement
- Roles - Organize instructions into behaviors
- Blackboard - Data source for sensors