# NPCs

> Give an NPC class its health and its agent, spawn it from a spawnable record, and decide the damage it takes.

Part of [Plugins](/docs/plugins) in the Evil Engine docs.

An NPC in Evil Engine is a body, a person or creature record, spawned from a `spawnable` record that names its class. The class is what a Plugin registers: the health its bodies spawn with and the agent record that moves and thinks for them. Half-Life 2's NPCs are made this way. Garry's Mod's mount writes a spawnable and a body for every NPC in its spawn list, and `evil.source` registers each class with the health Garry's Mod gives it and an agent on its Source hull.

## Spawnables

A `spawnable` record of kind `npc` spawns as an NPC. Garry's Mod's are `gmod:npc_zombie`, `gmod:npc_metropolice` and so on, by their spawn list names, each naming its class and its body. Yours go in your Plugin's `Records/`. This page builds one Plugin, `brutes`, on Garry's Mod's models:

```toml
id = "brutes"
name = "Brutes"
version = "1.0.0"
api = 1
code = "Code/brutes.dll"
after = ["evil.source"]

[[mounts]]
game = "gmod"
```

Then `Records/npcs.toml`:

```toml
[spawnable.npc_brute]
kind = "npc"
title = "Brute"
class = "brutes:npc_brute"
body = "brutes:npc/brute"
```

| Key | What it is |
| --- | --- |
| `kind` | `npc` for an NPC. A spawnable of any other kind doesn't spawn as one. |
| `class` | The NPC class, by id, that a Plugin registers. |
| `body` | The person or creature record it spawns as. |
| `title`, `icon`, `groups` | Its name, its icon and the `spawngroup` records it belongs to. |

One class can spawn on several bodies, each from a spawnable of its own, and a body can be a mounted game's, here Garry's Mod's classic zombie:

```toml
[spawnable.npc_brute_runt]
kind = "npc"
title = "Brute Runt"
class = "brutes:npc_brute"
body = "gmod:npc/zombie"
```

A spawnable needs a class, a body or both. With both, and the class registered, the body spawns with what the class says. With the class unregistered, it spawns with the default player class's health and no agent, and the session notes once that no loaded Plugin gives the class. With no body, nothing spawns.

A Plugin can't give `person` records, but it can give `creature` records, so a body of your own, like the brute's, is a creature over a model, with the clips it plays by slot:

```toml
[creature."npc/brute"]
model = "gmod:models/zombie/poison"
motion = { idle = "gmod:models/zombie/poison/anim/@ACT_IDLE", walk = "gmod:models/zombie/poison/anim/@ACT_WALK", run = "gmod:models/zombie/poison/anim/@ACT_RUN" }
```

`model` is the model it draws and is hit on, with `skin` and `body` for its skin and body groups. `motion` names a clip for each slot it plays: `idle` while it stands, and `walk` or `run` as its agent moves it. A Source model's clips are `<model>/anim/<sequence>`, like `gmod:models/headcrabclassic/anim/walk1`, or `/anim/@` and an activity, as here.

The `spawn` command spawns a spawnable where you look, facing you, or at a point:

```
spawn brutes:npc_brute
spawn gmod:npc_zombie 0 1 0
```

`just map-shot` spawns them too, which is handy for checking how one stands:

```
just map-shot gmod:gm_construct --walk=-:1 --spawn=gmod:npc_zombie --screenshot=zombie.png
```

## Registering a class

Register each class you give through your context's [Npcs](/docs/api/plugincontext#npcs) while your Plugin loads:

```csharp
using EvilEngine.Api;
using EvilEngine.Api.Npcs;
using EvilEngine.Formats;

namespace Brutes;

public sealed class BrutePlugin : IPlugin
{
    public void Load(PluginContext context)
    {
        if (context.Realm != Realm.Server)
            return;
        context.Npcs.Register(ContentId.Parse("brutes:npc_brute"), new NpcClass(300)
        {
            MaxHealth = 400,
            Agent = ContentId.Parse("brutes:agent/brute"),
            ModelHealth = new Dictionary<ContentId, int>
            {
                [ContentId.Parse("gmod:models/zombie/classic")] = 150
            }
        });
    }

    public void Unload()
    {
    }
}
```

`brutes:agent/brute` is an agent record of the Plugin's own, which [Agents and brains](/docs/plugins/agents) writes. [NpcClass](/docs/api/npcclass) is:

| Member | What it is |
| --- | --- |
| [Health](/docs/api/npcclass#health) | The health its bodies spawn with. |
| [MaxHealth](/docs/api/npcclass#maxhealth) | The most health one can be raised to, or `null` for what it spawned with. |
| [ModelHealth](/docs/api/npcclass#modelhealth) | Health by the body's model, where some models of the class get other health, as Half-Life 2's Combine elite gets 70 to the soldier's 50. |
| [Agent](/docs/api/npcclass#agent) | The `agent` record its bodies attach as they spawn, which gives their hull, their movement and their brain, or `null` for bodies that stand as they are. |

So a brute spawns with 300 health, and a runt, on the classic zombie's model, with 150. The class keeps `ModelHealth` as it was when you registered it, so changing your dictionary afterwards changes nothing. Health, most health or a model's health below 0, or a `null` `ModelHealth`, throws. A body spawned at 0 health dies to its first hit.

[Register](/docs/api/inpcs#register) only works inside `Load`: a call after it has returned throws `InvalidOperationException`. The classes you register go when your Plugin unloads.

## Taking over a class

A class can be registered by more than one Plugin. The later Plugin in load order wins, and when it unloads the earlier's comes back. So a Plugin that loads after `evil.source`, as `after = ["evil.source"]` in the brutes' manifest makes it, can give Half-Life 2's zombie its own health or its own agent:

```csharp
context.Npcs.Register(ContentId.Parse("gmod:npc_zombie"), new NpcClass(80)
{
    Agent = ContentId.Parse("brutes:agent/zombie")
});
```

Your registration replaces the whole class, so give it everything you want it to keep. An agent record can build on `evil.source`'s with a `base` and change only what it names, a brain for instance ([Agents and brains](/docs/plugins/agents)):

```toml
[agent."agent/zombie"]
base = "evil.source:agent/npc_zombie"
brain = "brutes:brain/fetch"
```

## Damage

NPCs take damage as players do. Every hit on a person, a player's body or an NPC's, runs through every Plugin's damage deciders in load order before it lands, and [Player](/docs/api/damage#player) tells them apart:

```csharp
context.Damage.Decide(damage => damage.Player || damage.Kind != DamageKind.Blast
    ? damage
    : damage with { Amount = damage.Amount * 0.5 });
```

That halves what blasts do to NPCs and leaves players alone. A decider gets a [Damage](/docs/api/damage) with what the engine knows of the hit, its amount, [kind](/docs/api/damagekind), the hitbox's [Group](/docs/api/damage#group), where it hit and the health and armour before it lands, and returns what lands. It doesn't know the body's class.

`evil.source`'s own decider is Garry's Mod's: a bullet to the head does double, to an arm, leg or gear a quarter, armour takes all but a fifth of a hit while it lasts, and everything but a hitscan bullet pushes. It applies to NPCs as to players. A bullet does a weapon's `damage` to an NPC and its `person_damage`, where the weapon gives one, to a player's body, as Garry's Mod's weapons do.

A decider that throws, or decides a number that isn't finite, is left out of that hit, and each kind of failure is noted once. Add deciders through [Decide](/docs/api/idamage#decide) inside `Load`, as you register classes.

An NPC whose health reaches 0 dies. Its agent lets go, and it falls as a ragdoll when its model has one.

---

- [Previous: Physics from a Plugin](/docs/plugins/physics)
- [Next: Agents and brains](/docs/plugins/agents)
