Knightbrain
Based on game build 714014 | Last updated: 2026-02-27
Overview
Knightbrain implements the behavior tree (BT) for the Knight entity in DST, specifically handling group coordination (for "Gilded" variants), combat engagement (including dodging and jousting), and environment-aware navigation. It extends Brain and constructs a hierarchical priority-based behavior tree in OnStart, integrating with components like combat, follower, stageactor, and entitytracker to support context-sensitive actions such as formation positioning, stage seat detection, and leader-based movement.
Dependencies & Tags
- Components used:
combat(viainst.components.combat:HasTarget(),InCooldown(), andtargetproperty)entitytracker(viainst.components.entitytracker:GetEntity(name))follower(viainst.components.follower:GetLeader())locomotor(viainst.components.locomotor.allow_platform_hopping)stageactor(viainst.components.stageactor:GetStage())
- Tags:
gilded_knight: Checked to enable gilded-specific logic (formation, seating, extended chase distance).notarget: Checked to filter potential face targets and leader tracking.charlie_seat: Used in seat searching logic.NOCLICK,DECOR,FX: Exclusion tags for seat blocker detection.
- Behaviors used:
standstill,runaway,doaction,follow,chaseandattack,wander
Properties
The constructor initializes no explicit instance properties beyond the base Brain class. All configuration is done via local constants (e.g., START_FACE_DIST, GILDED_FORMATION_RANGE) and nested function closures.
Main Functions
KnightBrain:OnStart()
- Description: Constructs the behavior tree root and assigns it to
self.bt. Initializes gilded-specific node subgraphs and tunes chase/follow distances, formation ranges, and seat detection logic based on thegilded_knighttag. This is the primary initialization method called when the brain is attached. - Parameters: None.
- Returns: None. Sets
self.btinternally.
GoHomeAction(inst)
- Description: Returns a
BufferedActionto walk to the entity's home position (obtained viaclockwork_common.GetHomePosition(inst)) only if the entity has no combat target and is not at home. Used by theShouldGoHomecondition node. - Parameters:
inst: The entity instance.
- Returns: A
BufferedActioninstance ornil.
GetFaceTargetFn(inst)
- Description: Locates the closest player within
START_FACE_DISTthat is not tagged"notarget". Used for general face-target behavior. - Parameters:
inst: The entity instance.
- Returns: A target
Entityinstance ornil.
KeepFaceTargetFn(inst, target)
- Description: Continues face behavior if the provided target is within
KEEP_FACE_DISTand not tagged"notarget". - Parameters:
inst: The entity instance.target: TheEntitybeing faced.
- Returns:
trueif the target remains valid for facing; otherwisefalse.
GetLeader(inst)
- Description: Returns the leader entity using
follower:GetLeader(). - Parameters:
inst: The entity instance.
- Returns: An
Entityinstance ornil.
GetFaceLeaderFn(inst)
- Description: Returns
GetLeader(inst)for face-on-leader logic. - Parameters:
inst: The entity instance.
- Returns: Leader
Entityornil.
KeepFaceLeaderFn(inst, target)
- Description: Verifies the provided target is the current leader.
- Parameters:
inst: The entity instance.target: TheEntityto verify as leader.
- Returns:
trueifGetLeader(inst) == target; otherwisefalse.
ShouldGoHome(inst)
- Description: Checks if the entity is farther than
GO_HOME_DIST_SQfrom its home position and if a home position exists. - Parameters:
inst: The entity instance.
- Returns:
trueif the entity should return home; otherwisefalse.
ShouldDodge(inst)
- Description: Returns
trueif the entity has a combat target, is in combat cooldown (combat:InCooldown()), and setsinst.hit_recoverytoTUNING.KNIGHT_DODGE_HIT_RECOVERY. Otherwise, clearshit_recovery. - Parameters:
inst: The entity instance.
- Returns:
trueif dodging should occur; otherwisefalse.
ShouldAttack(inst)
- Description: Returns
trueif dodging should not occur (invertedShouldDodge). - Parameters:
inst: The entity instance.
- Returns:
trueif attacking is allowed; otherwisefalse.
GetRunAwayTarget(inst)
- Description: Returns
combat.targetfor use by theRunAwaybehavior. - Parameters:
inst: The entity instance.
- Returns: The current combat target
Entityornil.
AreDifferentPlatforms(inst, target)
- Description: Checks if
instandtargetoccupy different platforms, respectinglocomotor.allow_platform_hopping. - Parameters:
inst: The entity instance.target: The targetEntity.
- Returns:
trueif platforms differ and platform hopping is disabled; otherwisefalse.
TryJoust(inst)
- Description: If
inst.canjoustistrue, checks whether the combat target is within the configured joust range (TUNING.YOTH_KNIGHT_JOUST_RANGE) and on the same platform. If all conditions are met, fires the"dojoust"event with the target. - Parameters:
inst: The entity instance.
- Returns: None.
AnyHorsemenAllies(inst)
- Description: Checks if any fellow horsemen allies (defined in
YOTH_HORSE_NAMES) exist and are alive. - Parameters:
inst: The entity instance.
- Returns:
trueif at least one ally exists and is alive; otherwisefalse.
GetFormationIndexAndCount(inst)
- Description: Computes the 1-based index of
instwithin the formation (ordered byYOTH_HORSE_NAMES) and the total count of alive horsemen (including self). Used to assign positions relative to the leader. - Parameters:
inst: The entity instance.
- Returns:
(index, count)—indexis the 1-based formation index,countis the total alive horsemen count.
GetKnightAtFormationIndex(inst, index)
- Description: Returns the knight entity at a given formation index, using the same ordering logic as
GetFormationIndexAndCount. Falls back toinstif the index is invalid. - Parameters:
inst: The entity instance.index: 1-based integer index.
- Returns: The
Entityat the specified index orinst.
GetLocationInFormation(inst)
- Description: Computes the ideal world position for
instbased on the leader's position and its own formation index. - Parameters:
inst: The entity instance.
- Returns: A
Vector3position ornilif no leader exists.
GetWanderFormationPos(inst)
- Description: Calculates the target position during formation wandering based on a periodically rotated leader selection. Reverse-engineers the virtual leader position used by the current leader.
- Parameters:
inst: The entity instance.
- Returns: A
Vector3position ornil.
IsWanderFormationLeader(inst)
- Description: Determines if
instis currently selected as the wandering leader for the current time slice (using deterministic PRNG seeding). - Parameters:
inst: The entity instance.
- Returns:
trueifinstis the wander leader; otherwisefalse.
GetStageSeatPosition(inst)
- Description: For gilded knights, finds the seat assigned to
instbased on its formation index. Filters out blocked seats and marksinst.is_sitting = trueon success. - Parameters:
inst: The entity instance.
- Returns: A
Vector3seat position ornil.
GetStageFollowMinDist(inst)
- Description: Returns a minimal follow distance (0.1) if
inst.is_sitting; otherwise, the defaultFOLLOW_MIN_DIST. - Parameters:
inst: The entity instance.
- Returns: A number.
_calc_formation_pos(leaderpt, formation_index, count)
- Description: Calculates the ideal position around the leader using a circular formation formula with
GILDED_FORMATION_RANGE. Attempts to offset to land if the computed point is not passable. - Parameters:
leaderpt: AVector3of the leader's position.formation_index: The 1-based index of this entity in the formation.count: Total number of entities in the formation.
- Returns: A
Vector3passable position.
GetWanderFormationLeaderIndex(inst, count)
- Description: Uses a deterministic seed (based on GUID of the first knight and current time) to pick a leader index for the current
WANDER_SWITCH_PERIODinterval, ensuring synchronized leader rotation across knights. - Parameters:
inst: The entity instance.count: Total number of knights.
- Returns: An integer index between
1andcount.
MatchWanderLeaderFacing(inst)
- Description: If
instis idle and not the current wander leader, aligns its facing direction with the current wander leader (to maintain visual consistency). - Parameters:
inst: The entity instance.
- Returns: None.
Events & Listeners
- Listens to: None explicitly defined in this component. Behavior tree execution drives state transitions.
- Pushes:
"dojoust", target— Emitted viainst:PushEventwhen joust conditions are met during combat.