Pigbrain
Based on game build 722832 | Last updated: 2026-04-22
Overview
Pigbrain defines the complete AI behavior tree for pig NPCs in Don't Starve Together. It manages daily routines including following leaders, trading with players, finding food, combat responses, and special event participation (Year of the Beast contests, minigames). The brain prioritizes survival behaviors (panic, fleeing fire) over normal activities and adapts behavior based on day/night cycles and leader presence.
Usage example
local inst = SpawnPrefab("pigman")
local brain = require "brains/pigbrain"
RunBrain(inst, brain:new(inst))
-- Brain automatically handles:
-- Following assigned leaders
-- Trading when players approach with trade actions
-- Finding and eating food during day/night
-- Panicking from fire, monsters, or haunted entities
-- Participating in contest events when tagged
Dependencies & tags
External dependencies:
behaviours/wander-- wandering behavior nodebehaviours/follow-- following leader behavior nodebehaviours/faceentity-- facing target entity nodebehaviours/chaseandattack-- combat chase and attack nodebehaviours/runaway-- fleeing behavior nodebehaviours/doaction-- action execution nodebehaviours/findlight-- light source seeking nodebehaviours/panic-- panic state behavior nodebehaviours/chattynode-- chatter dialog during behaviorsbehaviours/leash-- home leash constraint nodebrains/braincommon-- shared brain utility functions
Components used:
trader-- checksIsTryingToTradeWithMe()for trade interactionsinventory-- usesFindItem(),Has(),GetItemByName()for item managementeater-- usesCanEat(),TimeSinceLastEating(),GetEdibleTags()for food logicedible-- checksfoodtypeproperty for diet restrictionsfollower-- usesGetLeader(),GetLoyaltyPercent()for leader followinghomeseeker-- usesGetHomePos(),homeproperty for home locationburnable-- checksIsBurning()for fire detection on homehealth-- checkstakingfiredamageproperty for self-fire statuscombat-- usesHasTarget(),InCooldown(),TargetIs(),targetfor combathauntable-- checkspanicproperty for ghost hauntingpinnable-- checksIsStuck()for rescuing stuck leadersminigame_spectator-- usesGetMinigame()for minigame participationminigame-- accessesgametype,watchdist_min,watchdist_target,watchdist_maxtimer-- usesTimerExists()for contest panic timershelf-- checksitemonshelf,cantakeitemfor shelf food access
Tags:
outofreach-- excluded from food findingtakeshelfitem-- identifies shelf items for food findinglightsource-- identifies light sources for night behaviorplayer-- checked for trade and runaway behaviorplayerlight-- checked for safe light distance calculationpig-- checked for pig-vs-pig combat avoidance_combat-- checked for pig-vs-pig combat avoidanceNPC_contestant-- enables contest behavior tree branchINLIMBO-- excluded from prize collectionspider-- triggers runaway behavior at nightminigame_participator-- triggers runaway during minigamesburnt-- checked for valid home status
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
| None | No properties are defined. |
Main functions
OnStart()
- Description: Initializes the pig behavior tree with prioritized nodes for survival, daily routines, and special events. Builds a
PriorityNoderoot with branches for panic states, combat, trading, leader rescue, contest participation, minigame watching, and day/night cycles. - Parameters: None
- Returns: None
- Error states: Errors if
self.instis nil when accessing components (no nil guard onself.inst.components.Xcalls throughout the function).
Events & listeners
Not applicable — this brain file uses behavior tree nodes rather than direct event listeners. Events are handled through the behavior system (e.g., ChattyNode, Panic, RunAway nodes respond to entity state changes).