91 lines
5.4 KiB
JavaScript
91 lines
5.4 KiB
JavaScript
"use strict";
|
|
|
|
// Layer: simulation/colony-situation
|
|
// Owns colony mood/situation signal collection and priority arbitration.
|
|
(function (global) {
|
|
function avgOf(list, fn) { return list.length ? list.reduce((sum, t) => sum + (Number(fn(t)) || 0), 0) / list.length : 0; }
|
|
function countOf(list, fn) { return list.reduce((sum, t) => sum + (fn(t) ? 1 : 0), 0); }
|
|
function collect(worldRef) {
|
|
const now = worldRef?.time || 0;
|
|
const alive = (worldRef?.tarinai || []).filter(t => t && !t.dead);
|
|
const n = alive.length;
|
|
const ratio = value => n ? value / n : 0;
|
|
const panicCount = countOf(alive, t => t.state === "panic" || (t.fearTimer || 0) > 0.2 || (t.panicTargetUntil || 0) > now);
|
|
const safetyHighCount = countOf(alive, t => (t.needs?.safety || 0) >= 52 || (t.fearTimer || 0) > 0.35);
|
|
const weakCount = countOf(alive, t => (t.energy || 0) < 45 || (t.hunger || 0) > 88 || (t.needs?.health || 0) > 58);
|
|
const criticalCount = countOf(alive, t => (t.energy || 0) < 25 || (t.hunger || 0) > 94 || (t.needs?.health || 0) > 75);
|
|
const diseasedCount = countOf(alive, t => t.zunchiDisease || t.sleepDisease || t.explosionDisease || t.fightDisease);
|
|
const breedingCount = countOf(alive, t => (t.loveMochiTimer || 0) > 0.1 || t.state === "birth_ritual" || (t.birthRitualTimer || 0) > 0.1);
|
|
const recentDeaths = (Array.isArray(worldRef?.recentDeathTimes) ? worldRef.recentDeathTimes : []).filter(t => now - (Number(t) || -999) <= 30).length;
|
|
const previousPopulation = Number(worldRef?.lastColonyMoodPopulation);
|
|
const populationIncreasing = Number.isFinite(previousPopulation) && n > previousPopulation;
|
|
return {
|
|
now,
|
|
alive,
|
|
n,
|
|
previousPopulation,
|
|
populationIncreasing,
|
|
avgEnergy: avgOf(alive, t => t.energy || 0),
|
|
avgStress: avgOf(alive, t => t.stress || 0),
|
|
avgHunger: avgOf(alive, t => t.hunger || 0),
|
|
avgSafety: avgOf(alive, t => t.needs?.safety || 0),
|
|
panicCount,
|
|
safetyHighCount,
|
|
weakCount,
|
|
criticalCount,
|
|
diseasedCount,
|
|
breedingCount,
|
|
recentDeaths,
|
|
recentShock: (now - (worldRef?.lastShootAt || -999) <= 8) || (now - (worldRef?.lastGenkotsuAt || -999) <= 8),
|
|
panicRatio: ratio(panicCount),
|
|
safetyRatio: ratio(safetyHighCount),
|
|
weakRatio: ratio(weakCount),
|
|
criticalRatio: ratio(criticalCount),
|
|
diseaseRatio: ratio(diseasedCount),
|
|
breedingRatio: ratio(breedingCount),
|
|
};
|
|
}
|
|
function choose(worldRef, m) {
|
|
const candidates = [];
|
|
const add = (id, priority, ok) => { if (ok) candidates.push({ id, priority }); };
|
|
const devastationSignal = m.recentDeaths >= 3 || (m.n <= 2 && (worldRef?.deadCount || 0) > 0 && (m.recentDeaths > 0 || m.weakRatio >= 0.35 || m.panicRatio >= 0.20));
|
|
const crisisDeathThreshold = Math.max(5, Math.ceil(m.n * 0.10));
|
|
const crisisMortalitySignal = m.recentDeaths >= crisisDeathThreshold;
|
|
const crisisSystemicStress = m.weakRatio >= 0.50 && m.criticalRatio >= 0.35;
|
|
const crisisCollapseSignal = m.weakRatio >= 0.70 && m.criticalRatio >= 0.60;
|
|
add("devastation", 120, m.n <= 10 && devastationSignal);
|
|
add("crisis", 110, !m.populationIncreasing && ((crisisMortalitySignal && crisisSystemicStress) || crisisCollapseSignal));
|
|
const confusionPanicThreshold = Math.max(4, Math.ceil(m.n * 0.32));
|
|
const confusionShockPanicThreshold = Math.max(3, Math.ceil(m.n * 0.18));
|
|
const confusionShockSafetyThreshold = Math.max(5, Math.ceil(m.n * 0.30));
|
|
const confusionSignal = m.panicCount >= confusionPanicThreshold
|
|
|| (m.recentShock && m.panicCount >= confusionShockPanicThreshold && m.safetyHighCount >= confusionShockSafetyThreshold);
|
|
const breedingThreshold = Math.max(4, Math.ceil(m.n * 0.18));
|
|
const breedingSignal = m.breedingCount >= breedingThreshold && m.breedingRatio >= 0.18;
|
|
add("confusion", 100, confusionSignal);
|
|
add("fearful", 90, (m.avgSafety >= 50 && m.safetyRatio >= 0.28) || (m.recentShock && m.safetyRatio >= 0.18));
|
|
add("disease_spread", 82, m.diseaseRatio >= 0.25 || (m.diseasedCount >= 3 && m.diseaseRatio >= 0.16));
|
|
add("weakened", 74, !m.populationIncreasing && ((m.avgEnergy < 54 && m.weakRatio >= 0.18) || m.avgHunger > 82 || m.weakRatio >= 0.34));
|
|
add("breeding", 66, breedingSignal);
|
|
add("happy", 46, m.avgStress < 28 && m.panicRatio < 0.08 && m.diseaseRatio < 0.12 && m.weakRatio < 0.18);
|
|
add("isolated", 28, m.n <= 1);
|
|
if (!candidates.length) return "relaxed";
|
|
candidates.sort((a, b) => b.priority - a.priority);
|
|
return candidates[0].id;
|
|
}
|
|
function evaluate(worldRef, force = false) {
|
|
const now = worldRef?.time || 0;
|
|
if (!worldRef) return { id: "relaxed", label: "\u5B89\u5B9A", effects: { personality: {} } };
|
|
if (!force && worldRef.colonyMood && now < (worldRef.nextColonyMoodEvalAt || 0)) return worldRef.colonyMood;
|
|
const metrics = collect(worldRef);
|
|
const id = choose(worldRef, metrics);
|
|
const next = worldRef.colonyMoodDefinition?.(id) || { id, label: id, effects: { personality: {} } };
|
|
worldRef.colonyMood = next;
|
|
worldRef.lastColonyMoodPopulation = metrics.n;
|
|
const dayLength = Math.max(1, Number(worldRef?.config?.dayLength ?? global.CONFIG?.dayLength ?? 120) || 120);
|
|
worldRef.nextColonyMoodEvalAt = now + dayLength / 12;
|
|
global.TarinaiAchievements?.evaluateWorld?.(worldRef, next);
|
|
return next;
|
|
}
|
|
global.TarinaiColonySituationSystem = Object.freeze({ evaluate });
|
|
})(typeof window !== "undefined" ? window : globalThis);
|