Files
agentic-labs-challenge/adaptive-loop/src/domain/derived.ts
T
2026-08-13 07:33:43 +02:00

51 lines
1.6 KiB
TypeScript

// Pure derived aggregations over game state. Used by scenarios (win checks),
// the solver, and the UI. No mutation; depends only on types + constants.
import type { GameState, LineageId } from "./types";
export type PopulationField = "producer" | "grazer" | "phage";
export function total(state: GameState, field: PopulationField): number {
let n = 0;
for (const c of state.chambers) n += c[field];
return n;
}
export function totalProducer(state: GameState): number {
return total(state, "producer");
}
export function totalGrazer(state: GameState): number {
return total(state, "grazer");
}
export function totalPhage(state: GameState): number {
return total(state, "phage");
}
export function totalToxin(state: GameState): number {
let n = 0;
for (const c of state.chambers) n += c.toxin;
return n;
}
// Number of chambers where this lineage currently has population ≥ threshold.
export function chambersOccupied(state: GameState, field: PopulationField, threshold = 1): number {
let n = 0;
for (const c of state.chambers) if (c[field] >= threshold) n++;
return n;
}
export function populationAt(state: GameState, slot: number, field: PopulationField): number {
return state.chambers[slot]?.[field] ?? 0;
}
export function totalBiomass(state: GameState): number {
return totalProducer(state) + totalGrazer(state);
}
// Which lineages (producer/grazer) currently have any population anywhere.
export function survivingLineages(state: GameState): LineageId[] {
const out: LineageId[] = [];
if (totalProducer(state) > 0) out.push("producer");
if (totalGrazer(state) > 0) out.push("grazer");
return out;
}