Skip to main content

Creatures and AI

Every creature in the game is an Agent: a component from Alta.Intelligence in Root.Township that picks an action, runs it, and picks again. Spriggulls, turabadas, wyrms, goteras, phantoms and the rest are each one subclass. This section covers that framework, the creatures built on it, how they move, how they deal and take damage, and how they spawn.

The AI runs on the server​

Creature AI is server only. The code makes sure of it in three places (checked in the server build, whose AI code is the same as the client download's):

Clients just see the result: the creature's position through its network transform, its animations through NetworkAnimator, and a few things the server sends on purpose (channel events, a wyrm surfacing, a phantom fading). So a creature mod is almost always a server mod. Clients don't need it unless you change something they draw.

The one big exception is creature melee hits on players. Hits from collision hitboxes (the spriggull's beak, the wyrm's scythes) are detected only by the victim's client, which reports them to the server. Trigger hitboxes (turabada and babu attacks, gotera vines) are probably seen by the server too. See Combat, damage and death.

How a creature thinks​

  1. AgentUpdater ticks every registered agent in the physics update (FixedUpdate), under the update module "AI Agent Updater".
  2. Each tick, every agent updates its current action. A few agents (15 a tick in the main world scene, 5 by the code's default) also get to make a new decision: whoever has waited longest, weighted toward creatures near players.
  3. To decide, the agent's ActionDecider scores every action in its current ActionSet against every possible target, and starts the best one.
  4. Possible targets come from TargetCollectors (anything alive within a radius, 20 m by default) and from whoever hurt the creature.
  5. Actions move the creature through Director, Navigator and Walker (pathfinding on the server's navmesh), play animations, and fire channel events that the prefab turns into attacks and effects.

Pages in this section​

PageWhat it covers
AgentThe base class of every creature: members, lifecycle, finding instances
AgentUpdaterThe scheduler that ticks agents, its priorities and costs
Actions and decisionsAction components, action sets, scoring, AI hooks and channel events
Targets and sensesWhat a creature can target, how it notices players, and aggression
Movement and pathfindingDirector, Navigator, Walker and the navmesh
Combat, damage and deathHow creatures hit, get hit, die and drop things
Spawning and despawningThe creature side of populations
Common creaturesSpriggull, turabada, wyrm and gotera in detail
Other creaturesPhantom, firebug, schmeechee and the rest
Creature dataHealth, damage, senses, drops and populations from the shipped prefabs

Creature classes​

All in Alta.Intelligence, all derived from Agent<TThis>:

ClassWhat it is
SpriggullBeaked creature that wanders, eats food items, nests at night and jump-attacks
TurabadaWalking fighter with fist combos, a blocking stance and heal or self-destruct spheres; eight ore variants
WyrmBurrowing creature: hidden underground until it emerges to bite, spin and spit
Gotera, GreaterGoteraTree creatures that walk, plant themselves and attack with vines, seed sprays and darts; the greater gotera is a 150-health boss
Phantom, PlayerLikeSpectralGhostNight creatures that materialize and fade at day
FireBugA firefly you can catch and shake for light
SchmeecheeMushroom critter you can pick up
Babu, Dragonfly, DeathFernThe babu, a tusked animal that gives off the "Swine" sense; Dragonfly also drives the Dais herd animals; DeathFern drives ferns, geysers and other stationary hazards

Creature data lists which shipped prefab uses which class, and the generated Creatures reference has every creature prefab with its hash, health, stats and action sets. Find creatures by component type rather than by name: one class covers many prefabs. At runtime agent.Entity.OriginalName gives the prefab's name.

Finding creatures​

On the server:

  • UnityEngine.Object.FindObjectsOfType<Agent>() finds every active agent. On a client it finds nothing, because the AI objects are turned off there.
  • AgentUpdater.AgentCount is how many agents are being ticked (alive ones).
  • Agent<Spriggull>.SpawnedCount (and so on for each class) counts instances that exist. It goes up in Awake on any machine, so clients count too.
  • A postfix on Agent.InitializeAsServer sees every creature as it spawns. Agent has an example.
  • A creature that came from a population has agent.PopulationItem, which points back to its PopulationSample.

This console command counts creatures by type, and freezes or restarts all of them. It uses the game's own console (see Console commands for registering it).

using System.Linq;
using System.Text;
using Alta.Console;
using Alta.Intelligence;
using UnityEngine;

[Module("creatures", "Creature tools")]
public static class CreatureCommands
{
[Command("list", "Counts the creatures the server is running")]
[ServerOnly]
public static string List()
{
var text = new StringBuilder();
foreach (var group in Object.FindObjectsOfType<Agent>().GroupBy(a => a.GetType().Name))
text.AppendLine($"{group.Key}: {group.Count()}");
text.Append($"Registered with AgentUpdater: {AgentUpdater.AgentCount}");
return text.ToString();
}

[Command("freeze", "Stops or restarts every creature's AI")]
[ServerOnly]
public static string Freeze(bool frozen)
{
int count = 0;
foreach (var agent in Object.FindObjectsOfType<Agent>())
{
if (!agent.IsAlive) continue;
agent.IsEnabled = !frozen;
count++;
}
return $"{(frozen ? "Froze" : "Restarted")} {count} creatures";
}
}

Where to hook​

You want toHookPage
Know when a creature spawnsPostfix Agent.InitializeAsServerAgent
See what creatures decidePostfix ActionComponent.HandleUsedActions and decisions
Change how fast they movePostfix a navigation action's OnStart (ActiveWander, ActiveFollow)Movement and pathfinding
Hide a player from creaturesPrefix Agent.AddTargetTargets and senses
Change damage either wayPrefix PlayerHealth.ReceiveDamage or HealthObject.ReceiveDamageCombat, damage and death
Cap or block spawnsPrefix PopulationSample.CheckSpawnedSpawning and despawning
Remove one attack from a creatureEdit its ActionSet after InitializeAsServerCommon creatures
Generic types

Most of the framework is generic: Agent<TThis>, ActionDecider<TAgent>, BasicAction<TAgent, TActive, TSettings>, ActiveNavigation<...>. Mono shares one compiled body between closed generic types over reference types, so a patch on one of them can land on every creature type, or not apply at all. Patch a non-generic type, or a concrete subclass's own override (like ActiveWander.OnStart or Wyrm.ChangeAction), and check __instance's type inside your patch.

Not covered here​