Skip to main content

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>.

Constructors​

MemberWhat it is
protected Agent()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

MemberWhat it is
protected HashSet<TargetCollector> collectorsProtected: target collectors added with AddCollector.
protected HashSet<ITarget> hurtKeysProtected: the attackers currently tracked in Hurt.
protected bool isHurtAddingTargetProtected: when true (default), being hurt adds the attacker as a target, and removes it when the hurt fades.
18 private and internal fields
MemberWhat it is
private float actionStartTimeTime the current action started. Used by ActionDuration and NextUpdateProgress.
private NetworkAnimator animatorBacking field for NetworkAnimator.
private Agent.ChannelEvent[] channelEventsBacking field for ChannelEvents: Unity events fired by actions on each hook channel, optionally networked.
private ColliderGroup collisionBacking field for Collision.
private Director directorBacking field for Director.
private Dictionary<HookEventChannel, Agent.ChannelEvent> eventMapChannel to event lookup built in Initialize. Clients only get networked channels.
private bool hasWalkerBacking field for HasWalker, set in OnValidate when a walker is assigned.
private float hurtReducePerSecondHow fast hurt values per attacker fade. 0.1 per second by default.
private Dictionary<ITarget, TimeSpan> ignoreTimersTargets that RemoveTarget told the agent to ignore, with the time the ignore ends.
private bool isEnabledBacking field for IsEnabled.
private static Logger loggerNLog logger for this class.
private float nextUpdateTime of the next action re-pick. Set to max for actions that can't be cancelled, 0 when an action ends.
private HashSet<ITarget> providedTargetsTargets added through AddTarget. Hurt targets not in here are dropped when their hurt fades.
private Rigidbody rigidbodyBacking field for Rigidbody.
private MethodSyncStruct<int> syncEventMethod sync the server uses to send networked channel events to clients.
private static List<ITarget> tempRemovingShared scratch list used by UpdateHurt.
private float updateIntervalBacking field for UpdateInterval: seconds between action re-picks. 0.1 by default.
private Walker walkerBacking field for Walker, the pathfinding walker. Its object is off until the AI initializes.

Properties​

MemberWhat 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
MemberWhat 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​

MemberWhat it is
event Action NoActionFoundRaised when the decider finds no action to start.

Methods​

MemberWhat 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
MemberWhat 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: