tarinai/js/tarinai_update_step_frame.js

206 lines
11 KiB
JavaScript
Raw Normal View History

2026-06-26 12:46:32 +09:00
"use strict";
2026-06-27 19:22:38 +09:00
2026-06-26 12:46:32 +09:00
// Layer: entity-runtime/tarinai/update-step
// Owns per-frame timers, passive metabolism, disease/effect ticks, colony mood
2026-06-26 19:04:32 +09:00
// pressure, and early freeze exits. This is extracted from the former
2026-06-26 12:46:32 +09:00
// Tarinai.prototype.update body without changing behavior.
(function (global) {
2026-06-28 23:07:40 +09:00
function detRange(worldRef, salt, min, max, ...parts) {
2026-06-29 16:21:58 +09:00
return global.deterministicRange(worldRef, salt, min, max, ...parts);
2026-06-28 23:07:40 +09:00
}
function detSigned(worldRef, salt, ...parts) {
2026-06-29 16:21:58 +09:00
return global.deterministicSigned(worldRef, salt, ...parts);
2026-06-28 23:07:40 +09:00
}
function detChance(worldRef, salt, chance, ...parts) {
2026-06-29 16:21:58 +09:00
return global.deterministicChance(worldRef, salt, chance, ...parts);
2026-06-28 23:07:40 +09:00
}
2026-06-26 12:46:32 +09:00
function update(t, dt, context = {}) {
if (!t || t.dead) return { done: true };
t.prevX = t.x;
t.prevY = t.y;
const minute = dt / 60;
t.age += dt;
t.reproductionTimer -= dt;
t.eatTimer = Math.max(0, t.eatTimer - dt);
if (t.state === "eat" && t.eatTimer <= 0.02) {
2026-06-28 13:40:41 +09:00
t.setActionState?.("idle", { target: null, reason: "\u98df\u3079\u7d42\u3048\u305f", sleeping: false, clearTarget: true });
2026-06-26 12:46:32 +09:00
}
t.eatCooldown = Math.max(0, t.eatCooldown - dt);
t.foodReactTimer = Math.max(0, t.foodReactTimer - dt);
t.surpriseTimer = Math.max(0, t.surpriseTimer - dt);
t.fearTimer = Math.max(0, t.fearTimer - dt);
t.intimidateTimer = Math.max(0, t.intimidateTimer - dt);
t.intimidatedTimer = Math.max(0, t.intimidatedTimer - dt);
t.blastSpinTimer = Math.max(0, (t.blastSpinTimer || 0) - dt);
t.postBirthPeaceTimer = Math.max(0, (t.postBirthPeaceTimer || 0) - dt);
t.hpBarTimer = Math.max(0, (t.hpBarTimer || 0) - dt);
t.stressBarTimer = Math.max(0, (t.stressBarTimer || 0) - dt);
t.updateZunchiDisease(dt);
t.updateSpecialDiseases(dt);
if (t.updateItemEffects) t.updateItemEffects(dt);
if (t.dead) return { done: true };
t.loveMochiTimer = Math.max(0, (t.loveMochiTimer || 0) - dt);
t.fightMochiTimer = Math.max(0, (t.fightMochiTimer || 0) - dt);
if (t.fightMochiTimer > 0.04) {
applyNeedShock(t, { safety: dt * 4 });
if (t.state !== "fight") t.fearTimer = Math.max(t.fearTimer || 0, 0.10);
}
if (t.intimidateTimer <= 0.04) t.intimidateTargetId = null;
if (t.birthRitualTimer > 0) {
const beforeBirthTimer = t.birthRitualTimer;
t.birthRitualTimer = Math.max(0, t.birthRitualTimer - dt);
if (beforeBirthTimer > 0 && t.birthRitualTimer <= 0.04 && t.birthRitualLeader) {
const partner = t.world.liveTarinaiById?.(t.birthPartnerId);
if (partner) t.world.finishBirthRitual(t, partner);
t.birthRitualLeader = false;
}
}
t.pokeFlashTimer = Math.max(0, t.pokeFlashTimer - dt);
t.focusPulseTimer = Math.max(0, (t.focusPulseTimer || 0) - dt);
t.cursorPetting = Math.max(0, t.cursorPetting - dt * 1.2);
t.pinchThoughtTimer = Math.max(0, (t.pinchThoughtTimer || 0) - dt);
t.sunbathCooldown = Math.max(0, (t.sunbathCooldown || 0) - dt);
if (t.freezeSleepDiseaseMotion(dt)) {
t.checkLife(dt);
return { done: true };
}
2026-06-29 16:21:58 +09:00
global.TarinaiCursorCareSystem.updateOne(t, dt);
2026-06-26 12:46:32 +09:00
const wasFighting = t.fightTimer > 0.04;
2026-06-30 13:40:36 +09:00
const endingFightTargetIds = wasFighting ? [...new Set([t.fightTargetId, ...(Array.isArray(t.fightTargetIds) ? t.fightTargetIds : [])].filter(Boolean))] : [];
2026-06-26 12:46:32 +09:00
t.fightTimer = Math.max(0, t.fightTimer - dt);
t.fightCooldown = Math.max(0, t.fightCooldown - dt);
2026-06-30 13:40:36 +09:00
t.postConflictPeaceTimer = Math.max(0, (t.postConflictPeaceTimer || 0) - dt);
2026-06-26 12:46:32 +09:00
t.groundStumbleCooldown = Math.max(0, (t.groundStumbleCooldown || 0) - dt);
t.defeatedTimer = Math.max(0, t.defeatedTimer - dt);
if (wasFighting && t.fightTimer <= 0.04 && t.defeatedById) {
const winner = t.world.liveTarinaiById?.(t.defeatedById) || null;
2026-06-28 23:07:40 +09:00
t.defeatedTimer = Math.max(t.defeatedTimer, detRange(t.world, "defeated-recover-timer", 3.4, 5.2, t));
2026-06-26 12:46:32 +09:00
t.fearTimer = Math.max(t.fearTimer, 1.0 * t.personalityProfile().fear);
t.hurtTimer = Math.max(t.hurtTimer, 1.8);
t.fallTimer = Math.max(t.fallTimer, 1.15);
t.fallMax = Math.max(t.fallMax || 1.15, t.fallTimer);
t.fallDir = winner ? (t.x < winner.x ? -1 : 1) : t.fallDir;
t.world.spawnFallEffect(t.x, t.y + t.radius * 0.45);
if (winner) t.world.resolveFightOutcome(winner, t);
t.fightTargetIds = [];
t.fightTargetId = null;
}
2026-06-30 13:40:36 +09:00
if (wasFighting && t.fightTimer <= 0.04) {
const rivals = endingFightTargetIds.map(id => t.world?.liveTarinaiById?.(id)).filter(Boolean);
const cooldown = CONFIG.fightCooldown + detRange(t.world, "fight-ended-cooldown", 8, 14, t, endingFightTargetIds.join("/"));
t.fightCooldown = Math.max(t.fightCooldown || 0, cooldown);
t.postConflictPeaceTimer = Math.max(t.postConflictPeaceTimer || 0, detRange(t.world, "fight-ended-peace", 5.5, 9.0, t, endingFightTargetIds.join("/")));
for (const rival of rivals) t.world?.markFightPairCooldown?.(t, rival, cooldown + 2);
if (!t.defeatedById) {
t.fightTargetIds = [];
t.fightTargetId = null;
t.counterAttackFromId = null;
if (t.state === "fight" || t.state === "seek_enemy") t.setActionState?.("idle", { target: null, reason: "喧嘩が終わって少し距離を置いている。", sleeping: false, clearTarget: true });
}
}
2026-06-26 12:46:32 +09:00
t.grassEatTimer = Math.max(0, t.grassEatTimer - dt);
t.hurtTimer = Math.max(0, t.hurtTimer - dt);
t.fallTimer = Math.max(0, t.fallTimer - dt);
if ((t.world?.groundType || "soil") === "foot_massage"
&& (t.groundStumbleCooldown || 0) <= 0.04
&& (t.fallTimer || 0) <= 0.04
&& !t.dead
&& !["sleep", "fight", "panic", "birth_ritual", "frozen"].includes(String(t.state || ""))
2026-06-28 23:07:40 +09:00
&& detChance(t.world, "foot-massage-stumble", dt * 0.055, t)) {
const dir = detSigned(t.world, "foot-massage-stumble-dir", t);
t.groundStumbleCooldown = detRange(t.world, "foot-massage-stumble-cooldown", 8.5, 16.0, t);
t.fallTimer = Math.max(t.fallTimer || 0, detRange(t.world, "foot-massage-stumble-fall", 0.86, 1.18, t));
2026-06-26 12:46:32 +09:00
t.fallMax = Math.max(t.fallMax || 1.05, t.fallTimer);
t.fallDir = dir;
2026-06-28 23:07:40 +09:00
t.vx += dir * detRange(t.world, "foot-massage-stumble-vx", 26, 52, t);
t.vy += detRange(t.world, "foot-massage-stumble-vy", -18, 18, t);
2026-06-26 12:46:32 +09:00
t.world?.spawnFallEffect?.(t.x, t.y + t.radius * 0.45);
2026-06-28 13:40:41 +09:00
t.setActionState?.("idle", { target: null, reason: "\u8db3\u3064\u307c\u306e\u7a81\u8d77\u306b\u3064\u307e\u3065\u3044\u305f\u3002", sleeping: false, clearTarget: false });
if ((t.world?.time || 0) >= (t.nextBubbleAt || 0)) t.bubble?.("\u3046\u3063", 1.6, "rgba(86,70,96,0.78)");
2026-06-26 12:46:32 +09:00
}
2026-06-28 23:07:40 +09:00
if (t.stress > 92 && t.state !== "sleep" && t.state !== "fight" && t.state !== "eat" && detChance(t.world, "stress-breath-bubble", dt * 0.050, t)) {
2026-06-28 13:40:41 +09:00
t.bubble("\u306f\u3063...\u306f\u3063...", 3.4, "rgba(58,48,63,0.80)");
2026-06-26 12:46:32 +09:00
}
2026-06-28 23:07:40 +09:00
if (t.state === "idle" && t.stress < 78 && detChance(t.world, "idle-bubble", dt * 0.018, t)) {
2026-06-28 13:40:41 +09:00
t.bubble(pick(["\u306f\u3046", "\u306f\u3046\u3045", "\u306f\u3046\u3063", "\u306f\u3045"]), 4.2, "rgba(62,84,45,0.76)");
2026-06-26 12:46:32 +09:00
}
2026-06-28 23:07:40 +09:00
if (t.hurtTimer > 0 && detChance(t.world, "hurt-bleed-effect", dt * 5.2, t)) {
2026-06-26 12:46:32 +09:00
const side = t.facingDir();
2026-06-28 23:07:40 +09:00
t.world.spawnBleedEffect(t.x - side * t.radius * detRange(t.world, "hurt-bleed-x", 0.15, 0.42, t), t.y - t.radius * detRange(t.world, "hurt-bleed-y", 0.08, 0.34, t));
2026-06-26 12:46:32 +09:00
}
t.tempTimer += dt;
if (t.tempTimer >= 0.55) {
2026-06-30 13:40:36 +09:00
const felt = t.world?.feltTemperatureFor?.(t) ?? t.world?.temperatureAt?.(t.x, t.y, t) ?? (CONFIG.standardTemperature ?? 15);
2026-06-30 10:57:06 +09:00
t.tempComfort = felt;
t.feltTemperature = felt;
2026-06-30 13:40:36 +09:00
t.temperatureStatus = t.world?.temperatureStatusFor?.(felt, t) || null;
2026-06-26 12:46:32 +09:00
t.tempTimer = 0;
}
2026-06-30 13:40:36 +09:00
const tempStatus = t.temperatureStatus || t.world?.temperatureStatusFor?.(t.tempComfort ?? (CONFIG.standardTemperature ?? 15), t) || { discomfort: 0, stressExcess: 0, harmExcess: 0, direction: "comfort" };
2026-06-30 10:57:06 +09:00
const coldDiscomfort = tempStatus.direction === "cold" ? Math.max(0, tempStatus.discomfort || 0) : 0;
2026-06-26 12:46:32 +09:00
const hungerScale = t.metabolicHungerMultiplier ? t.metabolicHungerMultiplier() : (typeof tarinaiMetabolicHungerScale === "function" ? tarinaiMetabolicHungerScale(t) : 1);
2026-06-30 10:57:06 +09:00
t.hunger += CONFIG.hungerPerMinute * minute * t.trait.hunger * hungerScale * (1 + coldDiscomfort * 0.018);
2026-06-26 12:46:32 +09:00
t.loneliness += CONFIG.lonelinessPerMinute * minute * (2 - t.trait.social);
t.energy -= CONFIG.energyPerMinute * minute * t.trait.sleep * (t.state === "sleep" ? 0.025 : 1);
2026-06-27 21:50:05 +09:00
if (t.state === "sleep" || t.sleeping) {
const sleepBedLike = t.target && typeof t.isSleepFurniture === "function" && t.isSleepFurniture(t.target);
const sleepEnergyMax = Math.max(1, typeof tarinaiMaxEnergy === "function" ? tarinaiMaxEnergy(t) : (Number(t.maxEnergy) || 100));
t.energy = Math.min(sleepEnergyMax, (Number(t.energy || 0) || 0) + dt * (sleepBedLike ? 0.52 : 0.24));
if (typeof applyNeedRelief === "function") applyNeedRelief(t, { sleep: -dt * (sleepBedLike ? 2.0 : 1.1) });
}
2026-06-26 12:46:32 +09:00
// stress is derived from needs; no independent stress decay.
2026-06-30 10:57:06 +09:00
if ((tempStatus.stressExcess || 0) > 0) {
const excess = tempStatus.stressExcess || 0;
const stressRate = excess * (0.66 + Math.min(20, excess) * 0.035);
applyNeedShock(t, { safety: stressRate * dt, health: stressRate * dt * 0.42 });
if ((t.world?.time || 0) >= (t.nextBubbleAt || 0) && detChance(t.world, "temperature-stress-bubble", dt * 0.18, t)) {
t.bubble(tempStatus.direction === "cold" ? "ぶるぶる" : "あちゅい", 2.4, "rgba(76,62,72,0.78)");
}
}
if ((tempStatus.harmExcess || 0) > 0) {
const harm = tempStatus.harmExcess || 0;
t.energy -= dt * (0.10 + harm * 0.075 + harm * harm * 0.006);
t.lastDamageCause = tempStatus.direction === "cold" ? "凍結" : "熱中症";
t.lastTemperatureDamageAt = t.world?.time || 0;
}
2026-06-26 12:46:32 +09:00
const phase = t.world.lightLevel();
if (phase < 0.25) t.energy += dt * 0.07;
if (phase > 0.75) applyNeedShock(t, { health: dt * 1.2 });
if (t.world.weather === "cloudy") {
applyNeedRelief(t, { safety: -dt * 1 });
t.vx *= Math.pow(0.996, dt * 60);
t.vy *= Math.pow(0.996, dt * 60);
} else if (t.world.weather === "light_rain") {
t.energy += dt * 0.018;
applyNeedRelief(t, { health: -dt * 1, safety: -dt * 1 });
} else if (t.world.weather === "sunny") {
t.vx *= Math.pow(1.001, dt * 60);
t.vy *= Math.pow(1.001, dt * 60);
}
if (t.generation > 1 && t.syncGrowthScale) t.syncGrowthScale();
t.hunger = clamp(t.hunger, 0, 115);
t.loneliness = clamp(t.loneliness, 0, 110);
t.energy = clamp(t.energy, 0, typeof tarinaiMaxEnergy === "function" ? tarinaiMaxEnergy(t) : (Number(t.maxEnergy) || 100));
t.stress = applyGroundStressModifier(t, calculateStressFromNeeds(t.needRaw || t.needs || createDefaultNeeds()));
context.wasSleepingAtFrameStart = t.state === "sleep" || t.sleeping;
t.awakeLockTimer = Math.max(0, (t.awakeLockTimer || 0) - dt);
if (t.freezeSleepDiseaseMotion(dt)) {
t.checkLife(dt);
return { done: true };
}
return { done: false };
}
global.TarinaiFrameUpdateStep = Object.freeze({ update });
})(typeof window !== "undefined" ? window : globalThis);