Fruitflybrain
Based on game build 714014 | Last updated: 2026-02-27
Overview
FruitFlyBrain is a brain component responsible for defining the AI behavior of fruit fly entities in Don't Starve Together. It uses a behavior tree (BT) to orchestrate high-level decision-making, including wandering, attacking plants, sowing weeds on soil tiles, returning home, and optionally summoning minions (for lord fruit flies). The brain integrates with components such as combat, follower, leader, and knownlocations to coordinate movement and targeting logic. It inherits from the base Brain class and customizes behavior based on whether the entity has the lordfruitfly tag.
Dependencies & Tags
-
Components used:
combat— to check for active targets and attack cooldownsfollower— to retrieve leader position for home navigationleader— to determine if this entity is a leader and to prevent duplicate targetingknownlocations— to retrieve the "home" location when no leader exists
-
Tags:
lordfruitfly— determines extended behavior: enables child summoning, dodge, and attack staging logic.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
self.inst | Entity | — | The entity instance this brain controls. |
self.bt | BT | nil | Behavior tree instance, initialized in OnStart. |
inst.soiltarget | Entity or nil | nil | Cached soil target for sowing weeds; set by ShouldSowWeeds. |
inst.planttarget | Entity or nil | nil | Target plant for attack/farm actions (set externally by FindFarmPlant). |
Main Functions
CanSpawnChild(inst)
- Description: Determines whether the fruit fly is allowed to spawn a child. Requires sufficient age, remaining spawn quota, and an active combat target or pending plant/soil target.
- Parameters:
inst(Entity): The fruit fly entity instance.
- Returns:
boolean—trueif all conditions are met for spawning a child.
GetFollowPos(inst)
- Description: Computes the position the fruit fly should use for navigation and home return logic. Prioritizes the leader's position (via
follower), falls back to the "home" location (viaknownlocations), or defaults to the entity's own position. - Parameters:
inst(Entity): The fruit fly entity instance.
- Returns:
Vector— The target position for follow/home behavior.
GetLeader(inst)
- Description: Determines the effective leader of this fruit fly. If the entity itself has a
leadercomponent, it is considered the leader; otherwise, it returns the leader of itsfollowercomponent. - Parameters:
inst(Entity): The fruit fly entity instance.
- Returns:
Entityornil— The leader entity, if any.
GoHomeAction(inst)
- Description: Constructs a
WALKTOaction that moves the entity to its home position (viaGetFollowPos) if no combat target is present. - Parameters:
inst(Entity): The fruit fly entity instance.
- Returns:
Actionornil— A buffered walk action, ornilif there is a combat target.
ShouldGoHome(inst)
- Description: Checks whether the entity is farther away from its home position than
GO_HOME_DIST(30 world units). Uses squared distance to avoidsqrt. - Parameters:
inst(Entity): The fruit fly entity instance.
- Returns:
boolean—trueif distance exceeds threshold.
IsNearFollowPos(inst, soil)
- Description: Checks whether a given soil entity is within
SEE_DIST(20 world units) of the entity's home/follow position. - Parameters:
inst(Entity): The fruit fly entity instance.soil(Entity): The soil entity to check.
- Returns:
boolean—trueif the soil is near the follow position.
SowWeedsAction(inst)
- Description: Constructs a
PLANTWEEDaction on the cachedsoiltargetif set. - Parameters:
inst(Entity): The fruit fly entity instance.
- Returns:
Actionornil— A buffered plant weeds action, ornilif no soil target.
ShouldSowWeeds(inst)
- Description: Searches for a suitable soil tile within
SEE_DISTusingFindEntity, ensuring the tile is near the home/follow position and not already targeted by another entity (vialeader:IsTargetedByOther). - Parameters:
inst(Entity): The fruit fly entity instance.
- Returns:
boolean—trueif a valid soil target is found.
ShouldTargetPlant(inst, plant)
- Description: Checks whether a given plant can be safely targeted. Ensures no other entity (especially the leader) is already targeting it.
- Parameters:
inst(Entity): The fruit fly entity instance.plant(Entity): The plant entity to evaluate.
- Returns:
boolean—trueif no conflict exists.
GetRunAwayTarget(inst)
- Description: Returns the current combat target to run away from (used by the
RunAwaybehavior). - Parameters:
inst(Entity): The fruit fly entity instance.
- Returns:
Entityornil— The combat target to avoid.
FruitFlyBrain:OnStart()
- Description: Initializes the behavior tree root node. Configures behaviors conditionally based on the
lordfruitflytag:- Lord fruit flies get: summon child node (with
MinPeriod), attack staging (attack/dodge phases), and dodging during cooldown. - Regular fruit flies get:
ChaseAndAttackwith a simpleCanTargetAndAttackcondition.
- Lord fruit flies get: summon child node (with
- Parameters: None.
- Returns:
nil.
Events & Listeners
None identified.