How solarint/sain works
Project Type & Purpose
• This is a plugin-based AI system for “Single-player Escape From Tarkov” (SPT-AKI).
• It consists of two main subsystems:
a) A Unity/BepInEx C# plugin (SAIN) that replaces the in-game bot AI.
b) A Node/TypeScript ServerMod loaded by SPT-AKI to provide REST-style endpoints, configuration synchronization, version checks, and mod-lifecycle hooks.
Top-Level Components
A. Unity/C# Plugin (under root folder)
• BepInEx Loader & Plugin Entry (Plugin/SAINPlugin.cs, AssemblyInfoClass.cs)
• BotComponentSpace – Core AI component attached to every bot: decision trees, vision, hearing, movement, memory
• BotControllerSpace – Manages squads, spawning logic, periodic jobs
• GameWorldSpace – Environment queries: doors, lights, player positions
• PlayerComponentSpace – Reads player state, equipment, illumination, sound
• Layers – Sets of AI behaviors organized by priority (Combat/Solo, Combat/Squad, Peace, Extract, Unstuck, Debug)
• Patches – BepInEx/Harmony patches that disable default BigBrain layers and inject SAIN layers
• Preset & Config System (Preset, Plugin/PresetHandler.cs, GUI editors) – Loads/saves JSON presets, delivers live updates via in-game GUI (F6)
• Helpers & Interfaces – Utility classes, reflection, JSON, math, navmesh helpers
• Coroutine/Threading – CoroutineManager for multi-threaded vision raycasts, AI updates
• External Dependencies – BigBrain plugin, Waypoints expanded navmesh
B. ServerMod (under ServerMod/ folder)
• Mod Loader Bootstrap (mod.ts, build scripts) loaded by SPT-AKI
• Dependency Injection Container (tsyringe)
• Routers – HTTP, WebSocket, ItemEvent, Dynamic & Static routers
• Controllers – One per REST endpoint (GameController, BotController, ProfileController, etc.)
• Services – Business logic (BotGeneratorService, RagfairService, ProfileSnapshotService, etc.)
• Generators – Factories for bots, items, loot, quests
• Models & Types – Strongly typed DTOs for all SPT-AKI callbacks and request/response schemas
• Utilities – Database importer, HTTP file util, random, UUID, logging
Relationships & Data Flows
• Game Engine → BepInEx Loader
BepInEx starts up and loads SAINPlugin.
• SAINPlugin
• On bot spawn, attaches SAINComponent → instantiates a BotComponent.
• BotComponent reads environment from GameWorldSpace and PlayerComponentSpace, then runs DecisionManager in its update loop.
• Layers register actions in a priority queue; selected actions command the bot’s Mover, WeaponFunction, Search, Talk, etc.
• CoroutineManager dispatches multi-threaded vision raycasts to a thread pool and returns results to the component.
• PresetHandler loads JSON presets from disk; ConfigEditingTracker watches for GUI changes and pushes updates live into the AI components.
• Patches use Harmony to disable default BigBrain combat layers and inject SAIN layers in parallel.
• SPT-AKI ServerMod
• The Node application is launched by SPT-AKI’s mod loader.
• It registers Routers with internal HTTP server → incoming API calls.
• HTTPRouter dispatches to Controllers → calls into Services → may call Generators or Utils → returns JSON.
• Used for mod lifecycle hooks (OnLoad, OnUpdate), preset import/export endpoints, version checks, dependency validation, and any server-side computations.
Architectural Patterns & Principles
• Component-Based Architecture in Unity: one SAIN component per bot, plus subcomponents for cover-finding, memory, movement.
• Layered Behavior System: stacks of actions (Combat/Solo, Combat/Squad, etc.) selected by priority.
• Plugin & Patch: BepInEx/Harmony injection to override default AI without touching core game code.
• Dependency Injection in ServerMod (tsyringe) for testability and modularity.
• Modular Organization: separate domains into folders (Components, Layers, Preset, Patches, Helpers).
• Multi-Threading: offload expensive vision raycasts from main thread.
• Live Configuration: in-game GUI writes JSON, components subscribe to config events.