cruxVM

Tick Execution

How the VM processes each simulation tick.

Tick Loop

tick(): void {
  // 1. Clear events from previous tick
  this.state.events = [];

  // 2. Sort rules by priority (higher first)
  const sortedRules = [...this.ir.rules]
    .sort((a, b) => b.priority - a.priority);

  // 3. Execute each rule
  for (const rule of sortedRules) {
    this.executeRule(rule);
  }

  // 4. Increment tick counter
  this.state.tick++;

  // 5. Emit callback
  this.config.onTick?.(this.state);
}

Rule Execution

  1. Select agents matching the rule's selector
  2. For each agent, evaluate the condition
  3. If condition passes, execute the action
  4. Action may modify agent state or emit events

Running Multiple Ticks

const vm = new CruxVM(worldIR);

// Run 100 ticks
const result = vm.run(100);

console.log(result.ticksExecuted);
console.log(result.finalState);
console.log(result.events);