Guide for mod authors · Damian Sirbu · 2026-04-05
xlibs is an engine abstraction layer for STALKER Anomaly. It wraps X-Ray engine APIs in safe Lua functions: squad operations, smart terrain queries, entity resolution, an event bus, MCM config, PDA messaging, logging, and profiling.
AlifePlus is a reactive behavior framework built on xlibs. Engine callbacks produce causes (a squad entered a smart terrain, an NPC died, a stash was looted). The pipeline routes causes to registered consequences (a nearby faction investigates, a revenge squad is dispatched). Throttling, protection, rate limiting, ownership, and lifecycle are handled by the framework. Mods register a predicate and a handler.
Both are published under the PolyForm Perimeter License. Addons, integrations, and modpacks are encouraged with visible credit.
| Allowed | Not Allowed |
|---|---|
| Calling xlibs/AP functions from your mod Addons and integrations that depend on them Modpacks (encouraged) Reading source to learn, extend, debug Using AI tools to learn the public API |
Reproducing the implementation as a standalone replacement Reverse engineering internals to reproduce design Automated extraction for reproduction |
Required: Visible credit -- "xlibs by Damian Sirbu" and/or "AlifePlus by Damian Sirbu".
xlibs wraps X-Ray engine APIs based on reading the engine source (X-Ray Monolith C++), the Anomaly scripting layer, modded executable extensions, and established patterns from reputable community mods. The API surface was validated through tracing and load testing the Anomaly engine using Grafana k6 and a custom benchmarking framework. Key modules:
| Module | What it does | Example |
|---|---|---|
xbus | Event bus (pub/sub) | xbus.subscribe("cause:massacre", fn, "my_handler") |
xsquad | Find, script, release, protect squads | xsquad.find_squads(pos, {factions=t, max_distance=500}) |
xsmart | Smart terrain queries | xsmart.find_smart(pos, {filter=xsmart.is_base}) |
xcreature | Entity identity and queries | xcreature.community(id), xcreature.query():stalkers():each(fn) |
xpda | PDA messages and map markers | xpda.send("Title", "Message") |
xmcm | MCM config management | xmcm.create_config("mymod", defaults, path_builder) |
xlog | Buffered file logging | xlog.get_logger("MY.MOD", {outfile="mymod.log"}) |
xlevel | Level/map queries | xlevel.get_actor_level_id() |
xobject | Entity resolution, item creation | xobject.se(any_input), xobject.create_item(sec, npc_id) |
xtime | Game time | xtime.game_sec() |
xmath | RNG, probability, weighted choice | xmath.chance(30), xmath.sample(tbl) |
xevent | Function hooking, synthetic callbacks | xevent.hook("module", "func", wrapper) |
Full docs: github.com/damiansirbu-stalker/xlibs
Subscribe to AlifePlus cause events via xbus. This is the recommended starting point.
function on_game_start()
xbus.subscribe("cause:massacre", function(data)
-- data.position, data.level_id, data.total_deaths
my_response(data)
end, "my_mod_massacre")
end
Available events: cause:elite, cause:elitekill, cause:massacre, cause:squadkill, cause:basekill, cause:area, cause:wounded, cause:stash, cause:harvest, cause:needs.
Register your behavior with AP's pipeline. You get throttling, protection, rate limiting, and PDA for free.
-- Cause: detect something interesting
local function _predicate(trace, squad)
if not is_interesting(squad) then return nil end
return { cause = "cause:my_event", squad_id = squad.id, ... }
end
-- Consequence: react to it
local function _handler(event_data)
if not xmath.chance(30) then return { code = ap_const.RESULT.CHANCE_NEXT } end
local squad = xobject.se(event_data.squad_id)
local base = xsmart.find_smart(squad.position, { filter = xsmart.is_base })
xsquad.control_squad(squad, base, true)
return { code = ap_const.RESULT.OK_STOP }
end
function on_game_start()
for i = 1, #ap_const.RADIANT_CALLBACKS do
ap_producer_reactive.register("cause:my_event",
{ callback = ap_const.RADIANT_CALLBACKS[i] }, _predicate)
end
ap_consumer.register("consequence:my_reaction",
{ event = "cause:my_event", priority = 20 }, _handler)
end
If you need access to AP's tracker, chase system, territory conquest, or other internal systems -- reach out via the GitHub page. These APIs are not yet stable or documented for external use. The plan is to stabilize them as proper public APIs based on what mod authors actually need.
Most alife mods for Anomaly hook engine callbacks independently, iterate over squads on a timer, implement their own throttling, check squad validity in their own way, and set engine fields directly. Each mod operates as a silo with no awareness of other mods controlling the same squads.
xlibs and AlifePlus are designed as a shared alife layer that mods register to rather than build around.
Mods that register with the pipeline get these facilities without implementing them:
| Facility | What it does |
|---|---|
| Bresenham rate limiter | Integer-only ratio gate based on the Bresenham line algorithm. Distributes callback budget fairly across causes with zero floating-point arithmetic and no timer dependencies. |
| TTL bucket collections | Time-bounded data structures with automatic expiry. Used for cooldowns, claim lifetimes, and deferred cleanup. |
| Deferred queues | Expensive or unsafe operations are batched and flushed outside the callback, keeping the tick lightweight. |
| Protection stack | Unified squad safety checks applied everywhere: the pipeline, squad search functions, and scripting calls all use the same protection layer. |
| Ownership protocol | Claim/release with TTL. One owner at a time. Framework auto-releases expired claims. |
| Tracing | Hierarchical trace context flows through the entire pipeline. Every gate decision, squad action, and result is recorded. |
| Logging | Structured file logging with buffered writes. Per-module log files, configurable verbosity. |
| Performance monitoring | Wall-clock and game-time profiling per cause, per consequence, per gate. Identifies bottlenecks without external tools. |
Every squad event passes through the same sequence of gates. Each gate either passes the event forward or rejects it.
| Mod | As AP plugin |
|---|---|
| Warfare | Base-capture and patrol as AP causes/consequences. Squad ownership via framework claim/release. |
| Semi Radiant AI | Patrols as radiant causes with arrival-based release. |
| Guards Spawner | Guard-return as cause. Long-TTL ownership claim with auto-renew. |
| Night Mutants | Spawn/despawn as causes with time-of-day conditions. |
| Bounty Squads Extended | Bounty as consequence using the chase template. |
| Faction Relations | Pure xbus subscriber. Listens to cause events, adjusts faction goodwill. |
| Mugging Squads | Loot detection as reactive cause, hostile response as consequence. |