ants/src/Config.ts

65 lines
1.7 KiB
TypeScript

const STORAGE_KEY = "ants-simulation-config";
const defaults = {
worldSize: 1024,
antsStartCount: 4,
antBudget: 512,
seedWorld: true,
simulationStepsPerSecond: 60,
scentThreshold: 0.01,
scentFadeOutFactor: 0.001,
scentBlurRadius: 0.1,
scentMaxStorage: 1e6,
scentPerMarker: 200,
scentMaxPerCell: 10,
antSpeed: 1,
antRotationAngle: Math.PI / 30,
brushRadius: 20,
brushMaterial: -1,
cameraZoom: 0,
gravityDirection: "down" as const,
viewMode: "side" as "side" | "top",
// per-channel pheromone params
repellentFadeOutFactor: 0.0005,
repellentBlurRadius: 0.05,
repellentMaxPerCell: 10,
repellentThreshold: 0.01,
};
type ConfigType = typeof defaults;
function loadSaved(): Partial<ConfigType> {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw) return JSON.parse(raw);
} catch {
// corrupted data, ignore
}
return {};
}
const saved = loadSaved();
const Config: ConfigType = { ...defaults, ...saved };
export function saveConfig(): void {
const toSave: Partial<ConfigType> = {};
for (const key of Object.keys(defaults) as (keyof ConfigType)[]) {
if (Config[key] !== defaults[key]) {
// biome-ignore lint/suspicious/noExplicitAny: generic key/value copy
(toSave as any)[key] = Config[key];
}
}
if (Object.keys(toSave).length > 0) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(toSave));
} else {
localStorage.removeItem(STORAGE_KEY);
}
}
export function resetConfig(): void {
Object.assign(Config, defaults);
localStorage.clear();
}
export { defaults };
export default Config;