NPCs
Give an NPC class its health and its agent, spawn it from a spawnable record, and decide the damage it takes.
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:
id = "brutes"
name = "Brutes"
version = "1.0.0"
api = 1
code = "Code/brutes.dll"
after = ["evil.source"]
[[mounts]]
game = "gmod"
Then Records/npcs.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:
[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:
[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 while your Plugin loads:
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 writes. NpcClass is:
| Member | What it is |
|---|---|
| Health | The health its bodies spawn with. |
| MaxHealth | The most health one can be raised to, or null for what it spawned with. |
| 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 | 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 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:
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):
[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 tells them apart:
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 with what the engine knows of the hit, its amount, kind, the hitbox’s 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 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.