← Projects

Engine Programmer

AngelScript Rules Engine

Engine Programmer · 2024

C++AngelScriptScriptingEngineGame DevModding
Language
AngelScript (bytecode VM)
Host
Custom C++ engine
Scope
Economics, AI, events, scenarios
Interop
Registered C++ types and functions
  • AngelScript bytecode VM embedded into the engine; core simulation types (provinces, nations, trade routes, ledger entries) registered as first-class script objects
  • The entire economics model lives in script: tax rates, production modifiers, trade flow calculations, and deficit resolution are AngelScript functions called by the engine each tick
  • Nation AI wired to registered callback functions; strategy tuning is a script edit, not a recompile
  • Event system built on script blocks: trigger condition and outcome action functions evaluated by the VM each tick; designers write branching event chains without C++ knowledge
  • Bytecode caching: scripts compile once on load, serialize to disk; subsequent launches skip the parse and compile step
  • Sandboxed module system with explicit import declarations; one mod's globals cannot silently alias another's
Outcome Full rules engine authored in AngelScript and hot-swappable without recompiling the engine.

The Long Century’s simulation is fully data-driven. Economics models, AI behavior, event logic, and scenario setup are authored in AngelScript rather than compiled into the engine. Rules change without a recompile, and modders get the same authoring surface the developers use.

Embedding AngelScript

AngelScript’s registration API lets C++ types and functions appear as native objects inside script. The engine registers the core simulation types (Province, Nation, TradeRoute, Ledger) with their relevant fields and methods. Script code reads and writes simulation state through these handles; the VM enforces the type system at bytecode-compile time, so type errors surface at script load rather than mid-simulation.

The engine owns the AngelScript context and drives execution: each simulation phase calls into the VM at defined entry points (economics tick, AI turn, event evaluation), passes current world state through registered references, and reads results back. Scripts never call back into engine internals outside the registered API surface.

Economics and Rules Authoring

The entire economics model lives in script. Tax revenue, production output, trade flow allocation, and deficit resolution are implemented as AngelScript functions invoked by the simulation tick. The C++ engine provides raw world state; the script computes derived values and writes them back through the registered types. Changing a tariff formula or adding a new resource type is an edit to a script file.

AI Decision Layer

Nation AI is wired to script-side weight functions. On each AI turn the engine iterates available actions, calls the registered callback for each, and selects the highest-weighted option. The callback reads world state through the registered API and returns a numeric weight. Tuning AI aggression, economic priorities, or diplomatic posture is a script edit.

Event System

Events are script blocks: a trigger condition function and one or more outcome action functions. The engine evaluates all loaded event triggers each tick; a trigger returning true queues the event for resolution. Outcome functions fire on player or AI selection and write state changes back through registered types. Designers write branching event chains without any C++ involvement.

Module System and Bytecode Caching

Each script file is a separate AngelScript module with explicit import declarations. Modules see only what they import, which prevents one mod’s globals from silently aliasing another’s. On first load the engine compiles each module to bytecode and serializes it to disk; subsequent launches deserialize directly and skip the parse and compile step. The cache is invalidated by modification time.