Mods / Client-side Entity AI API Library

Tags:
Utility Library Entity Logic(AI)
Author:
traugdor
Side:
Client
Created:
Sep 9th at 2:41 AM
Last modified:
Sep 9th at 9:30 PM
Downloads:
62
Recommended download (for Vintage Story 1.22.7):
cliententityailib_1.0.3.zip  1-click install

A library other Vintage Story mods can depend on to spawn and drive client-only entities — entities that render, animate, and move like real creatures, but exist only in one player's local client memory. Never sent to the server, never seen by other players, no server-side health, AI, or combat. Useful for ambient/cosmetic creatures, illusions, ghosts, previews, or any other visual-only actor a mod wants full manual control over.

Installing

Reference the built ClientEntityAILib.dll the same way this project references the game's own mod DLLs — a Reference with a HintPath in your mod's .csproj:

 
<Reference Include="ClientEntityAILib">
<HintPath>path\to\ClientEntityAILib.dll</HintPath>
<Private>False</Private>
</Reference>
 

And declare it as a dependency in your mod's modinfo.json so the game loads it first:

 
{
"dependencies":
{
"cliententityailib": ""
}
}

Quick example

Spawn a drifter near the player, walk it through a triangle, a square, and a circle, then despawn it — chaining each leg via the pathfinding callback:

 
using System;
using ClientEntityAILib;
using ClientEntityAILib.Pathfinding;
using Vintagestory.API.MathTools;

ICoreClientAPI capi = /* from your mod's StartClientSide */;
Vec3d origin = capi.World.Player.Entity.Pos.XYZ.Add(4, 0, 0);

var entity = new ClientControlledEntity(capi, MovementType.CanWalk);
entity.SpawnClient("game:drifter-normal", origin);

Vec3d[] triangle =
{
    origin.AddCopy(3, 0, 0),
    origin.AddCopy(1.5, 0, 3),
    origin
};
Vec3d[] square =
{
    origin.AddCopy(3, 0, 0),
    origin.AddCopy(3, 0, 3),
    origin.AddCopy(0, 0, 3),
    origin
};

int circleSteps = 12;
Vec3d[] circle = new Vec3d[circleSteps + 1];
for (int i = 0; i <= circleSteps; i++)
{
    double angle = i * (2 * Math.PI / circleSteps);
    circle[i] = origin.AddCopy(Math.Cos(angle) * 3, 0, Math.Sin(angle) * 3);
}

void WalkShape(Vec3d[] points, int index, Action onShapeComplete)
{
    if (index >= points.Length) { onShapeComplete(); return; }
    Vec3d p = points[index];
    entity.MoveToFast(p.X, p.Z, ok => WalkShape(points, index + 1, onShapeComplete));
}

WalkShape(triangle, 0, () => WalkShape(square, 0, () => WalkShape(circle, 0, () => entity.Despawn())));

Public API

I was gonna format this but I got tired.

public class ClientControlledEntity : IDisposable
{
    public ClientControlledEntity(ICoreClientAPI capi, MovementType movementType, TerrainPreference terrainPreference = TerrainPreference.Both, int? pathfindingSearchDepth = null);

    public bool SpawnClient(string entityCode, Vec3d spawnPos);
    public bool SpawnClientCustom(string entityCode, Vec3d spawnPos, AnimationKeycodes animKeycodes, bool startHidden = false);

    // Real, obstacle-aware pathfinding - runs on a background thread, callback fires on arrival/failure.
    public void MoveToSlow(double x, double z, Action<bool> onComplete);
    public void MoveToFast(double x, double z, Action<bool> onComplete);
    public void MoveToSlow(double x, double z, double y, Action<bool> onComplete);
    public void MoveToFast(double x, double z, double y, Action<bool> onComplete);

    // Instant relocation - skips pathfinding entirely, no route searched, no obstacle check.
    public void Teleport(double x, double z);
    public void Teleport(double x, double z, double y);

    // Generic, entity-agnostic building blocks for a caller's own AI logic - this library has no
    // built-in concept of "attack" or "in range"; these just play whatever animation and report
    // whatever distance the caller asks for.
    public bool PlayOneShotAnimation(string animationCode);
    public Vec3d GetPosition();
    public double DistanceTo(double x, double y, double z);
    public bool PlaySound(string soundLocation, bool relativeToEntity = true,
SoundDirection direction = SoundDirection.Front, double distance = 0,
double heightOffset = 0, bool randomizePitch = true, float range = 32f, float volume = 1f);

    // Toggle visibility without despawning - movement/pathfinding keep running while hidden.
    public void Hide();
    public void Show();
    public bool IsHidden();

// Completely remove entity from client
    public void Despawn();
}

[Flags]
public enum MovementType { CanWalk = 1, CanFly = 2, CanSwim = 4, CanClimb = 8 }
public enum TerrainPreference { Water, Land, Both }
public enum SoundDirection { Front, FrontRight, Right, BackRight, Back, BackLeft, Left, FrontLeft }

public class AnimationKeycodes
{
    public string Idle;
    public string MoveSlow;
    public string MoveFast; // falls back to MoveSlow's code when left null/empty
}


  • MovementType is what the entity can physically do (a hard legality gate for pathfinding, not a preference) — combine flags freely, e.g. MovementType.CanWalk | MovementType.CanSwim for an amphibious creature, or MovementType.CanSwim alone for a fish that can never leave water.
  • TerrainPreference only matters when MovementType makes more than one terrain type legal — it biases pathfinding cost toward Water or Land when a comparable route through either exists; Both (the default) is unbiased.
  • Teleport instantly relocates the entity, skipping pathfinding entirely — no route searched, no obstacle check made, no walking animation. It cancels any in-progress MoveToSlow/MoveToFast call with no callback (same "a superseded call gets no callback" rule as calling a new MoveTo*), since the old destination no longer applies once the entity's been moved elsewhere. The 2-arg overload auto-resolves Y from the terrain at that column, same as MoveToSlow/MoveToFast's (x, z) overloads.
  • Speeds for MoveToSlow/MoveToFast are derived automatically from the spawned entity's own AI-task JSON where present, falling back to fixed defaults otherwise — no configuration needed. If the entity's JSON has no matching wander/seekentity movement tasks to derive a speed from (a minimal custom entity, for example), both tiers fall back to fixed constants (1.2 / 3.0 blocks/sec) taken from Remedy & Ruin's (WIP mod) own shipped, hand-tuned drifter apparition behavior — proven-good values, not the derived speed of vanilla's actual drifter.
  • SpawnClientCustom works for any entity code — vanilla or a custom mod-added one — since it takes the animation names to use directly rather than assuming vanilla's "idle"/"walk" convention (which SpawnClient still uses as a shorthand for the common case).
  • PlayOneShotAnimation/GetPosition/DistanceTo are the entity-agnostic building blocks for writing your own AI on top of this library — this library has no built-in idea of "attack" or "in range." Poll DistanceTo(x, y, z) against whatever threshold your entity cares about, call PlayOneShotAnimation("whatever-that-entity-calls-it") when you decide it's time, and Despawn() on your own timer. GetPosition() returns the real (logicalPos) position, not entity.Pos, which becomes a smoothed/lagging value once movement starts — reading entity.Pos directly (or calling AnimManager yourself) would get stale answers or fight this library's own bookkeeping, which is why the Entity object itself isn't exposed. PlayOneShotAnimation works for any real animation clip regardless of whether the entity's own JSON happened to also expose it as a named client.animations code (many don't — e.g. the real drifter's "standattack" is only ever played by its own AI task constructing the animation data directly, the same fallback this method uses).
  • PlaySound plays a one-shot sound using the engine's own real 3D positional audio — no custom panning trick, just computing the right world position and letting the engine's audio listener do the rest. relativeToEntity: true (the default) anchors it to the entity's own position and facing; false anchors it to the player's instead. direction/distance offset it from that anchor along a fixed 45°-step angle relative to whichever facing applies — e.g. PlaySound("game:creature/drifter/hurt", relativeToEntity: false, direction: SoundDirection.Back, distance: 5) plays a sound 5 blocks behind wherever the player is currently facing, regardless of where the entity actually is. distance: 0 (the default) means no offset at all — just play at the anchor.
  • Hide/Show/IsHidden/startHidden let an entity exist and keep ticking without being drawn — useful for e.g. spawning ahead of time and revealing on a trigger. Movement, pathfinding, and position/rotation smoothing all keep running while hidden exactly as if shown. Animation pose advancement does not — the engine only advances an entity's animator when it has a renderer, so whatever animation is active when Hide() is called stays frozen on that pose until Show() brings it back; it isn't lost or reset, just paused. SpawnClientCustom(..., startHidden: true) spawns already hidden.

Mod Version Mod IdentifierFor Game version Downloads Released Changelog Download 1-click mod install*
1.0.3 cliententityailib 26 Sep 9th at 9:30 PM cliententityailib_1.0.3.zip 1-click install

What's new in 1.0.3

  • PlaySound — a directional one-shot sound API using the engine's real 3D positional audio. Anchor it to the entity's own position/facing or the player's (relativeToEntity), offset it in one of 8 facing-relative directions (SoundDirection) at a given distance, plus heightOffset/volume/range/randomizePitch controls.
  • Hide() / Show() / IsHidden() — toggle visibility without despawning. Movement, pathfinding, and position/rotation smoothing all keep running while hidden; only rendering (and, as a side effect, animation pose advancement) stops until Show() is called. SpawnClientCustom(..., startHidden: true) spawns already hidden.
  • Teleport(x, z) / Teleport(x, z, y) — instant relocation that skips pathfinding entirely, snaps the position interpolator instead of sliding, and cancels any in-progress MoveToSlow/MoveToFast call.
  • Removal of the old non-pathfinding MoveTo/MoveToSlow/MoveToFast overloads (straight-line movers with no obstacle avoidance) — the callback-based, real-pathfinding overloads are now the only way to move an entity.
1.0.2 cliententityailib 18 Sep 9th at 4:40 PM cliententityailib_1.0.2.zip 1-click install

removed MoveTo/MoveToSlow/MoveToFast (the non-callback, straight-line, no-obstacle-avoidance overloads) along with their backing fields/methods (hasMoveTarget, moveTarget, StepDirectTarget, MoveToDirect, the now-dead StepUpHeightLimit constant). MoveToSlow(...)/MoveToFast(...) (callback) are now the only way to move an entity — both always run the real, capability-aware A* search, so nothing in the public API can walk an entity through or over terrain anymore. FindGroundY itself stays; it's still used once per pathfind call to resolve a target Y for the 2-arg overloads. README and design-notes.md updated to match. This is a breaking API change for any mod using this code. You will need to drop any calls to the removed overloads and switch to the callback versions. A bit more code, but it makes accurate pathfinding a lot more reliable.

1.0.1 cliententityailib 16 Sep 9th at 5:13 AM cliententityailib_1.0.1.zip 1-click install

Fixed so it's client-side only. This is a client-side ONLY entity AI API library

1.0.0 cliententityailib 2 Sep 9th at 2:42 AM Release Retracted

Retraction Reason:

This version incorrectly declared itself as universal for both client and server, but it's for client - only. Using this version or having the mod declare itself as being for both Client and Server may make it impossible to use and include and cause server-side mods to function incorrectly.

Changelog:

It probably works on older versions, but I built and tested it against 1.22.7

Disclaimer

By downloading a mod you consent to our Terms of Use.

We tell you this specifically because we want to make sure you are aware that we cannot validate every mod, and you are responsible for any damages you might incur by installing or loading mods.

This prompt will not be shown to you again (for the current browser session) if you proceed.


0 Comments (oldest first | newest first) (threaded | flat)