Agent
An abstract class in Root.Township, Alta.Intelligence.
Base for creature AI on a networked creature. On the server it registers with AgentUpdater, picks actions through its action decider and tracks who hurt it. Concrete creatures derive from Agent<TThis>.
- Listed in Creatures and AI
- Inherits or implements
Target,IAgentDebugger,ILinkedAgent,IAgent,ITarget,ITargetPointer,INetworkEntityBehaviour - Derived types
ActiveAttackDirectionReact,ActiveCircleReposition,ActiveCircleTarget,ActiveCrawl,ActiveDelayEnterSet,ActiveDeleted,ActiveDisabled,ActiveDistanceFromTarget,ActiveFaceTarget,ActiveFollow,ActiveIdle,ActiveInteractApproach,ActiveJumpAt,ActiveNest,ActiveRegainControl,ActiveSwapActionSet,ActiveThreaten,ActiveWander,Agent<TThis>,AgentRollUpInPlace,BasicActiveAgentMoveCloser<T> - Nested types
ChannelEvent - Attributes
SelectionBase - Documented in Agent
Constructors
| Member | What it is |
|---|---|
protected Agent() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
| Member | What it is |
|---|---|
protected HashSet<TargetCollector> collectors | Protected: target collectors added with AddCollector. |
protected HashSet<ITarget> hurtKeys | Protected: the attackers currently tracked in Hurt. |
protected bool isHurtAddingTarget | Protected: when true (default), being hurt adds the attacker as a target, and removes it when the hurt fades. |
18 private and internal fields
| Member | What it is |
|---|---|
private float actionStartTime | Time the current action started. Used by ActionDuration and NextUpdateProgress. |
private NetworkAnimator animator | Backing field for NetworkAnimator. |
private Agent.ChannelEvent[] channelEvents | Backing field for ChannelEvents: Unity events fired by actions on each hook channel, optionally networked. |
private ColliderGroup collision | Backing field for Collision. |
private Director director | Backing field for Director. |
private Dictionary<HookEventChannel, Agent.ChannelEvent> eventMap | Channel to event lookup built in Initialize. Clients only get networked channels. |
private bool hasWalker | Backing field for HasWalker, set in OnValidate when a walker is assigned. |
private float hurtReducePerSecond | How fast hurt values per attacker fade. 0.1 per second by default. |
private Dictionary<ITarget, TimeSpan> ignoreTimers | Targets that RemoveTarget told the agent to ignore, with the time the ignore ends. |
private bool isEnabled | Backing field for IsEnabled. |
private static Logger logger | NLog logger for this class. |
private float nextUpdate | Time of the next action re-pick. Set to max for actions that can't be cancelled, 0 when an action ends. |
private HashSet<ITarget> providedTargets | Targets added through AddTarget. Hurt targets not in here are dropped when their hurt fades. |
private Rigidbody rigidbody | Backing field for Rigidbody. |
private MethodSyncStruct<int> syncEvent | Method sync the server uses to send networked channel events to clients. |
private static List<ITarget> tempRemoving | Shared scratch list used by UpdateHurt. |
private float updateInterval | Backing field for UpdateInterval: seconds between action re-picks. 0.1 by default. |
private Walker walker | Backing field for Walker, the pathfinding walker. Its object is off until the AI initializes. |
Properties
| Member | What it is |
|---|---|
float ActionDuration { get; } | Seconds since the current action started. |
IActiveAction ActiveAction { get; protected set; } | The running action instance, or null when idle. |
Agent.ChannelEvent[] ChannelEvents { get; } | UnityEvents per hook channel that actions fire, optionally networked to clients. |
ColliderGroup Collision { get; } | The collider group for the creature's body. |
float CreationTime { get; private set; } | Server: Time.time when the agent was set up on the server. |
Director Director { get; } | The Director set on the prefab. |
abstract IActionDecider GeneralActionDecider { get; } | The decider that scores possible actions and targets. |
bool HasTarget { get; private set; } | True while the current action uses a target and that target still exists. |
bool HasWalker { get; } | Whether a pathfinding Walker is set on the prefab. |
Dictionary<ITarget, float> Hurt { get; } | How much each attacker has hurt this agent, as a fraction of max health. Fades over time. |
bool IsActing { get; private set; } | True while an action is running. |
bool IsEnabled { get; set; } | Server: turning it off cancels the current action and disables the walker; on picks a new action. |
NetworkAnimator NetworkAnimator { get; } | The networked animator for the creature. |
float NextUpdateProgress { get; } | How far through the current decision interval the agent is, 0 to 1. |
PopulationItem PopulationItem { get; private set; } | Server: the PopulationItem on the prefab root, if it came from a population. |
Rigidbody Rigidbody { get; } | The creature's rigidbody. |
static Agent Selected { get; set; } | Static: the agent picked for debugging. Nothing sets it in this build. |
abstract ITarget Target { get; } | The target of the current action. |
float UpdateInterval { get; } | Seconds between decision checks (default 0.1). Actions that can't be cancelled skip checks until done. |
Walker Walker { get; } | The pathfinding Walker, if any. |
2 private and internal properties
| Member | What it is |
|---|---|
private LinkedListNode<ILinkedAgent> Alta.Intelligence.ILinkedAgent.Node { get; set; } | This agent's node in the AgentUpdater list. |
private int Alta.Intelligence.ILinkedAgent.Priority { get; set; } | Update priority set by AgentUpdater. Gizmos colour agents by it. |
Events
| Member | What it is |
|---|---|
event Action NoActionFound | Raised when the decider finds no action to start. |
Methods
| Member | What it is |
|---|---|
void AddCollector(TargetCollector collector) | Registers a target collector. They are force-updated each time the agent picks a new action. |
bool AddTarget(ITarget target) | Server: gives the decider a target to consider. False if it is the agent itself, already added or still in an ignore timer. |
void CancelCurrentAction() | Cancels the running action, if any. |
abstract void DoAction(ActionComponent action, ITarget target) | Starts a given action component against a target, skipping the decider. |
void FireChannelEvent(HookEventChannel channel, IActiveAction action) | Runs the event on a hook channel. The server also sends networked ones to clients in the chunk. |
override void Initialize() | Sets up event syncing and maps the channel events (only networked ones on clients). |
override void InitializeAsServer(SpawnData spawnData) | Server: records creation time, finds the PopulationItem, starts the AI and registers with AgentUpdater if alive. |
bool RemoveTarget(ITarget target, float duration = -1) | Server: removes a target, or with a duration, ignores it for that many seconds. Attackers in Hurt stay. |
abstract void UseActionComponentSet(ActionComponentSet set) | Switches the decider to a different set of possible actions. |
protected virtual void Awake() | Virtual Unity Awake. In Agent<TThis> it counts spawned agents and turns the walker off until the AI starts. |
protected abstract bool ChangeAction() | Protected: picks and starts the best action. Called when the update interval is up. |
protected virtual void InitializeAi() | Protected: sets up the AI on the server. Agent<TThis> creates the action decider and turns on the walker. |
protected bool InternalRestartCurrentAction() | Protected: restarts timing for the current action without starting it again. |
protected bool InternalStartAction<T>(IActionTarget actionTarget, IAction<T> action, T thisAgent) | Protected: starts an action and hooks its finish. Raises NoActionFound when given none. |
protected override void Killed(HealthObject healthobject) | Server: on death, stops updating the agent and cancels its action. |
protected override void OnValidate() | Updates HasWalker from whether a walker is set. |
8 private and internal methods
| Member | What it is |
|---|---|
private void ActiveActionFinished(IActiveAction finished) | Clears the active action when it finishes or is cancelled, so the agent picks a new one on its next update. |
private bool Alta.Intelligence.ILinkedAgent.CheckChange() | Called by AgentUpdater: picks a new action once the update interval has passed, if enabled. |
private void Destroyed(NetworkEntity entity, bool isUnload) | Server: deregisters from AgentUpdater and cancels the active action when the entity is destroyed. |
private void HealthChanged(HealthObject healthObject, float changeAmount, DamageData damage) | Server: when the agent takes damage from a wielder, adds that target to Hurt and probably to the decider's targets. |
private void OnDrawGizmos() | Editor: draws a small cube coloured by update priority. |
private void SyncEvent(IPlayer player, Stream stream, int eventIndex) | Method sync handler: remote clients fire the channel event the server sent. |
private void Alta.Intelligence.ILinkedAgent.UpdateAgent() | Called by AgentUpdater each update: decays hurt, checks the target still exists and updates the active action. |
private void UpdateHurt() | Lowers each hurt value by hurtReducePerSecond and forgets targets that hit 0 or no longer exist. |
Used in
Members of other types that take, return or hold a Agent:
ActionComponentSet.actionSet, ActiveIdle.GetAction, ActiveSchmeecheePlant.GetAction, Chance.Evaluate, FaceTarget.agent, FailTargetOperation.Evaluate, Formula<T>.Evaluate, Grounded.Evaluate, InverseLerpSenseSelf.Evaluate, InverseLerpSenseTarget.Evaluate, Lifetime.Evaluate, PickupAgent.agent, ProjectileShooter.agent, RaycastGrounded.Evaluate, RepeatIfHasTarget.agent, RotateToTargetWorldXZ.agent, RVOBlockedValue.Evaluate, SameTarget.Evaluate, Schmeechee.Operation.Evaluate, StatValue.Evaluate, TargetCollector.agent, TargetCollector.Agent, TargetOperation.Evaluate, VelocityLimit.Evaluate, IndependentWalker.baseTopic, EruptAreaAttack.agent.