Werepigbrain
Based on game build 714014 | Last updated: 2026-02-27
Overview
The WerePigBrain component implements the behavior tree for Werepig entities. It orchestrates core AI behaviors including panic reactions to hazards, opportunistic eating of edible items (when not under threat), chasing and attacking aggressive targets, and wandering within a fixed radius from a remembered "home" location. The behavior prioritizes survival and aggression: panic triggers override all other actions, followed by safe eating, combat, and finally idle wandering.
Usage example
This component is typically added automatically by the game engine when spawning a Werepig entity (e.g., via the werepig prefab definition). It is not intended for direct manual instantiation in mod code. A minimal example of how it integrates into an entity's setup is shown below:
inst:AddComponent("brain")
inst.components.brain:SetBrainClass("werepigbrain")
Dependencies & tags
Components used:
combat— accessed viainst.components.combat.targetandinst.components.combat.defaultdamageeater— accessed viainst.components.eater:CanEat()andinst.components.eater:GetEdibleTags()knownlocations— accessed viainst.components.knownlocations:GetLocation("home")andinst.components.knownlocations:RememberLocation("home", ...)
Tags: None identified (no tags are added, removed, or checked directly in this file).
Properties
No public instance properties are explicitly initialized in the constructor or behavior setup. All logic is embedded in methods and local functions.
Main functions
WerePigBrain:OnStart()
- Description: Initializes and assigns the behavior tree (BT) for the Werepig. It constructs a priority-based behavior tree root node that evaluates panic triggers first, then safe eating, followed by combat, and finally wandering.
- Parameters: None.
- Returns: None.
- Error states: None documented. Behavior composition depends on external behavior modules (
behaviours/wander,behaviours/chaseandattack) andBrainCommonhelpers.
WerePigBrain:OnInitializationComplete()
- Description: Records the entity's current world position as the "home" location using
knownlocations:RememberLocation. This position is used later by theWanderbehavior as a base point. - Parameters: None.
- Returns: None.
- Error states: May error if
PointorGetWorldPositionfails due to transform issues (not explicitly handled in this file).
IsFoodValid(item, inst)
- Description: Local helper function used by the food-finding logic. Determines if an item is valid for eating based on whether the eater can consume it and whether the item is on a passable point.
- Parameters:
item: Entity representing a potential food item.inst: The Werepig instance (owner of this component).
- Returns:
trueifinst.components.eater:CanEat(item)istrueanditem:IsOnPassablePoint()istrue; otherwisefalse. - Error states: None documented.
FindFoodAction(inst)
- Description: Attempts to locate an edible item within
SEE_FOOD_DIST(10 units) and returns a buffered action to eat it. UsesFindEntitywith tags derived fromeater:GetEdibleTags()and excludes items with tags inFINDFOOD_CANT_TAGS. - Parameters:
inst: The Werepig instance.
- Returns: A
BufferedActiontargeting the found food and usingACTIONS.EAT, ornilif no valid food is found. - Error states: Returns
nilifFindEntityfinds no valid target.
TargetIsAggressive(inst)
- Description: Local helper that checks if the Werepig's current combat target is actively aggressive — specifically, if the target has a
combatcomponent, deals positive damage, and is targeting the Werepig back. - Parameters:
inst: The Werepig instance.
- Returns:
trueif all conditions are met (target,target.components.combat,defaultdamage > 0, andtarget == inst); otherwisefalse. - Error states: Returns
falseif any component check fails (e.g.,combatisnil,targetisnil).
Events & listeners
None — this component does not register or push any events.