tarinai/js/tarinai_disease_nest.js

625 lines
36 KiB
JavaScript
Raw Normal View History

2026-06-21 14:09:35 +09:00
"use strict";
2026-06-27 19:22:38 +09:00
2026-06-21 14:09:35 +09:00
(function (global) {
const Tarinai = global.Tarinai;
if (!Tarinai) throw new Error("Tarinai is not available for mixin: tarinai_disease_nest.js");
Object.defineProperties(Tarinai.prototype, Object.getOwnPropertyDescriptors({
infectZunchiDisease(source = null, force = false) {
if (this.dead || this.zunchiDisease) return false;
if (!force && (this.zunchiDiseaseCooldown || 0) > 0) return false;
this.zunchiDisease = true;
this.zunchiDiseaseSeverity = Math.max(this.zunchiDiseaseSeverity || 0, source ? 0.72 : 0.86);
this.zunchiDiseaseCooldown = 12;
this.fearTimer = Math.max(this.fearTimer || 0, 0.35);
this.surpriseTimer = Math.max(this.surpriseTimer || 0, 0.45);
this.thought = "\u305a\u3093\u3061\u75c5\u306b\u304b\u304b\u3063\u305f";
audio.disease?.();
this.world?.spawnBubble?.(this.x, this.y - this.radius * 1.20, "\u3046\u3045\u2026", "rgba(38,82,38,0.82)");
2026-06-21 22:29:00 +09:00
this.world?.emit?.("disease:infected", { tarinai: this, diseaseId: "disease_zunchi", source });
this.recordChangeCause?.("\u75c5\u6c17\u767a\u75c7", "\u305a\u3093\u3061\u75c5");
2026-06-21 14:09:35 +09:00
return true;
},
recoverZunchiDisease(reason = "") {
if (!this.zunchiDisease) return false;
this.zunchiDisease = false;
this.zunchiDiseaseSeverity = 0;
this.zunchiDiseaseCooldown = 18;
this.zunchiStain = clamp((this.zunchiStain || 0) - 35, 0, 100);
2026-06-23 18:11:42 +09:00
if (typeof applyNeedRelief === "function") applyNeedRelief(this, { health: -18, safety: -6 });
2026-06-21 14:09:35 +09:00
this.thought = reason || "\u305a\u3093\u3061\u75c5\u304c\u6cbb\u307e\u3063\u305f";
audio.heal?.();
this.world?.spawnBubble?.(this.x, this.y - this.radius * 1.18, "\u306f\u3046", "rgba(62,104,62,0.78)");
2026-06-21 22:29:00 +09:00
this.world?.emit?.("disease:cured", { tarinai: this, diseaseId: "disease_zunchi", reason });
this.recordChangeCause?.("\u75c5\u6c17\u56de\u5fa9", "\u305a\u3093\u3061\u75c5");
2026-06-21 14:09:35 +09:00
return true;
},
emitZunchiDiseaseEffect(dt) {
if (!this.zunchiDisease || !this.world?.effects) return;
2026-06-27 19:22:38 +09:00
if (deterministicChance(this.world, "zunchi-disease-miasma", dt * (1.2 + (this.zunchiDiseaseSeverity || 0) * 1.4), this)) {
this.world.effects.push(new Effect("zunchi_miasma", this.x + deterministicRange(this.world, "zunchi-miasma-x", -this.radius * 0.45, this.radius * 0.45, this), this.y - this.radius * deterministicRange(this.world, "zunchi-miasma-y", 0.05, 0.72, this), {
vx: deterministicRange(this.world, "zunchi-miasma-vx", -5, 5, this),
vy: deterministicRange(this.world, "zunchi-miasma-vy", -10, -3, this),
life: deterministicRange(this.world, "zunchi-miasma-life", 1.4, 2.4, this),
size: deterministicRange(this.world, "zunchi-miasma-size", 8, 18, this) * (0.8 + (this.zunchiDiseaseSeverity || 0) * 0.45),
2026-06-21 14:09:35 +09:00
color: "rgba(16,78,28,0.70)",
}));
}
},
updateZunchiDisease(dt) {
this.zunchiStain = clamp(this.zunchiStain || 0, 0, 100);
this.zunchiDiseaseCooldown = Math.max(0, (this.zunchiDiseaseCooldown || 0) - dt);
if (this.world?.weather === "light_rain") {
this.zunchiStain = clamp(this.zunchiStain - dt * 2.4, 0, 100);
if (this.zunchiDisease) this.zunchiDiseaseSeverity = Math.max(0, (this.zunchiDiseaseSeverity || 1) - dt * 0.006);
}
2026-06-27 19:22:38 +09:00
const zunchiSelfInfectChance = this.diseaseChance ? this.diseaseChance(dt * clamp((this.zunchiStain - 82) / 18, 0, 1) * 0.045) : dt * clamp((this.zunchiStain - 82) / 18, 0, 1) * 0.045;
if (!this.zunchiDisease && this.zunchiStain > 82 && deterministicChance(this.world, "zunchi-self-infect", zunchiSelfInfectChance, this)) {
2026-06-21 14:09:35 +09:00
this.infectZunchiDisease(null, true);
this.world?.log?.(`${this.name}\u306f\u305a\u3093\u3061\u75c5\u3092\u767a\u75c7\u3057\u305f\u3002`, "accident", { participants: [this] });
}
if (this.zunchiDisease) {
this.zunchiDiseaseSeverity = clamp((this.zunchiDiseaseSeverity || 0.7) + dt * 0.004, 0, 1.25);
2026-06-23 18:11:42 +09:00
if (typeof applyNeedShock === "function") applyNeedShock(this, { health: dt * (5 + 4 * this.zunchiDiseaseSeverity) });
2026-06-26 12:46:32 +09:00
this.energy = clamp(this.energy - dt * 0.035 * this.zunchiDiseaseSeverity, 0, typeof tarinaiMaxEnergy === "function" ? tarinaiMaxEnergy(this) : (Number(this.maxEnergy) || 100));
2026-06-21 14:09:35 +09:00
if (this.energy > 82) this.zunchiDiseaseSeverity = Math.max(0, this.zunchiDiseaseSeverity - dt * (this.energy > 94 ? 0.020 : 0.010));
this.emitZunchiDiseaseEffect(dt);
if (this.zunchiDiseaseSeverity <= 0.04) {
if (this.recoverZunchiDisease("\u4f53\u529b\u304c\u623b\u308a\u3001\u305a\u3093\u3061\u75c5\u304c\u6cbb\u307e\u3063\u305f")) this.world?.log?.(`${this.name}\u306e\u305a\u3093\u3061\u75c5\u304c\u6cbb\u307e\u3063\u305f\u3002`, "accident", { participants: [this] });
}
}
},
infectSleepDisease(source = null) {
if (this.dead || this.sleepDisease) return false;
this.sleepDisease = true;
this.sleepDiseaseCooldown = 8;
this.sleepDiseaseAnchorX = this.x;
this.sleepDiseaseAnchorY = this.y;
2026-06-24 13:48:22 +09:00
this.startSleeping(null, "\u306d\u3080\u308a\u75c5\u3067\u7720\u308a\u7d9a\u3051\u3066\u3044\u308b");
2026-06-21 14:09:35 +09:00
this.fightTimer = 0;
this.intimidateTimer = 0;
this.fightTargetIds = [];
this.thought = "\u306d\u3080\u308a\u75c5\u3067\u7720\u308a\u7d9a\u3051\u3066\u3044\u308b";
audio.disease?.();
this.world?.spawnBubble?.(this.x, this.y - this.radius * 1.18, "Zzz...", "rgba(72,76,120,0.76)");
this.world?.log?.(`${this.name}\u306f\u306d\u3080\u308a\u75c5\u306b\u304b\u304b\u3063\u305f\u3002`, "accident", { participants: [this] });
2026-06-21 22:29:00 +09:00
this.world?.emit?.("disease:infected", { tarinai: this, diseaseId: "disease_sleep", source });
this.recordChangeCause?.("\u75c5\u6c17\u767a\u75c7", "\u306d\u3080\u308a\u75c5");
2026-06-21 14:09:35 +09:00
return true;
},
recoverSleepDisease(reason = "") {
if (!this.sleepDisease) return false;
this.sleepDisease = false;
this.sleepDiseaseCooldown = 14;
this.sleepDiseaseAnchorX = null;
this.sleepDiseaseAnchorY = null;
2026-06-24 13:48:22 +09:00
this.goIdle(reason || "\u306d\u3080\u308a\u75c5\u304c\u6cbb\u3063\u305f");
2026-06-21 14:09:35 +09:00
this.sleeping = false;
2026-06-21 16:26:12 +09:00
this.awakeLockTimer = Math.max(this.awakeLockTimer || 0, 10);
2026-06-21 14:09:35 +09:00
audio.heal?.();
this.surpriseTimer = Math.max(this.surpriseTimer || 0, 0.65);
this.world?.spawnBubble?.(this.x, this.y - this.radius * 1.18, "!", "rgba(72,76,120,0.76)");
this.world?.log?.(`${this.name}\u306e\u306d\u3080\u308a\u75c5\u304c\u6cbb\u3063\u305f\u3002`, "accident", { participants: [this] });
2026-06-21 22:29:00 +09:00
this.world?.emit?.("disease:cured", { tarinai: this, diseaseId: "disease_sleep", reason });
this.recordChangeCause?.("\u75c5\u6c17\u56de\u5fa9", "\u306d\u3080\u308a\u75c5");
2026-06-21 14:09:35 +09:00
return true;
},
infectExplosionDisease(source = null) {
if (this.dead || this.explosionDisease) return false;
this.explosionDisease = true;
2026-06-21 22:29:00 +09:00
this.explosionDiseaseTimer = Math.max(this.explosionDiseaseTimer || 0, 60);
2026-06-21 14:09:35 +09:00
this.fearTimer = Math.max(this.fearTimer || 0, 0.55);
this.thought = "\u7206\u767a\u75c5\u3067\u843d\u3061\u7740\u304b\u306a\u3044";
audio.disease?.();
this.world?.spawnBubble?.(this.x, this.y - this.radius * 1.2, "!", "rgba(178,78,42,0.82)");
this.world?.log?.(`${this.name}\u306f\u7206\u767a\u75c5\u3092\u767a\u75c7\u3057\u305f\u3002`, "accident", { participants: [this] });
2026-06-21 22:29:00 +09:00
this.world?.emit?.("disease:infected", { tarinai: this, diseaseId: "disease_explosion", source });
this.recordChangeCause?.("\u75c5\u6c17\u767a\u75c7", "\u7206\u767a\u75c5");
2026-06-21 14:09:35 +09:00
return true;
},
recoverExplosionDisease(reason = "") {
if (!this.explosionDisease) return false;
this.explosionDisease = false;
this.explosionDiseaseTimer = 0;
this.thought = reason || "\u7206\u767a\u75c5\u304c\u6cbb\u3063\u305f";
audio.heal?.();
2026-06-23 18:11:42 +09:00
if (typeof applyNeedRelief === "function") applyNeedRelief(this, { health: -22, safety: -8 });
2026-06-21 14:09:35 +09:00
this.world?.spawnBubble?.(this.x, this.y - this.radius * 1.18, "\u306f\u3046", "rgba(82,126,88,0.76)");
this.world?.log?.(`${this.name}\u306e\u7206\u767a\u75c5\u304c\u6cbb\u3063\u305f\u3002`, "accident", { participants: [this] });
2026-06-21 22:29:00 +09:00
this.world?.emit?.("disease:cured", { tarinai: this, diseaseId: "disease_explosion", reason });
this.recordChangeCause?.("\u75c5\u6c17\u56de\u5fa9", "\u7206\u767a\u75c5");
2026-06-21 14:09:35 +09:00
return true;
},
recordBleedExposure() {
this.lastBleedAt = this.world?.time || 0;
},
infectFightDisease(source = null) {
if (this.dead || this.fightDisease) return false;
this.fightDisease = true;
this.fightDiseaseCooldown = 18;
this.fearTimer = Math.max(this.fearTimer || 0, 0.35);
this.thought = "\u304d\u305a\u3064\u304d\u75c5\u3067\u5f53\u3066\u3082\u306a\u304f\u5f77\u5fa8\u3063\u3066\u3044\u308b";
audio.disease?.();
this.world?.spawnBubble?.(this.x, this.y - this.radius * 1.20, "?", "rgba(150,78,44,0.80)");
this.world?.log?.(`${this.name}\u306f\u304d\u305a\u3064\u304d\u75c5\u3092\u767a\u75c7\u3057\u305f\u3002`, "fight", { participants: [this] });
2026-06-21 22:29:00 +09:00
this.world?.emit?.("disease:infected", { tarinai: this, diseaseId: "disease_fight", source });
this.recordChangeCause?.("\u75c5\u6c17\u767a\u75c7", "\u304d\u305a\u3064\u304d\u75c5");
2026-06-21 14:09:35 +09:00
return true;
},
recoverFightDisease(reason = "") {
if (!this.fightDisease) return false;
this.fightDisease = false;
this.fightDiseaseCooldown = 20;
this.thought = reason || "\u304d\u305a\u3064\u304d\u75c5\u304c\u6cbb\u3063\u305f";
audio.heal?.();
2026-06-23 18:11:42 +09:00
if (typeof applyNeedRelief === "function") applyNeedRelief(this, { health: -24, safety: -10 });
2026-06-21 14:09:35 +09:00
this.world?.spawnBubble?.(this.x, this.y - this.radius * 1.18, "\u306f\u3046", "rgba(82,126,88,0.76)");
this.world?.log?.(`${this.name}\u306e\u304d\u305a\u3064\u304d\u75c5\u304c\u6cbb\u3063\u305f\u3002`, "fight", { participants: [this] });
2026-06-21 22:29:00 +09:00
this.world?.emit?.("disease:cured", { tarinai: this, diseaseId: "disease_fight", reason });
this.recordChangeCause?.("\u75c5\u6c17\u56de\u5fa9", "\u304d\u305a\u3064\u304d\u75c5");
2026-06-21 14:09:35 +09:00
return true;
},
freezeSleepDiseaseMotion(dt = 0) {
if (!this.sleepDisease) return false;
2026-06-21 16:26:12 +09:00
const beingDraggedByAnt = Boolean((this.antDraggedTimer || 0) > 0 && this.antDraggedBy);
2026-06-27 21:50:05 +09:00
const heldByPlayer = Boolean(this.playerHeld || this._heldByPlayer);
2026-06-21 14:09:35 +09:00
if (!Number.isFinite(this.sleepDiseaseAnchorX)) this.sleepDiseaseAnchorX = this.x;
if (!Number.isFinite(this.sleepDiseaseAnchorY)) this.sleepDiseaseAnchorY = this.y;
2026-06-28 13:40:41 +09:00
this.startSleeping(null, beingDraggedByAnt ? "\u7720\u3063\u305f\u307e\u307e\u30a2\u30ea\u306b\u904b\u3070\u308c\u3066\u3044\u308b" : "\u306d\u3080\u308a\u75c5\u3067\u7720\u308a\u7d9a\u3051\u3066\u3044\u308b");
// \u306d\u3080\u308a\u75c5\u306f\u610f\u8b58\u3060\u3051\u3092\u56fa\u5b9a\u3057\u3001\u7269\u7406\u4f4d\u7f6e\u306f\u56fa\u5b9a\u3057\u306a\u3044\u3002
// \u4ee5\u524d\u306f anchor \u306b\u6bce\u56de\u30b9\u30ca\u30c3\u30d7\u3057\u3066\u3044\u305f\u305f\u3081\u3001\u5916\u529b\u30fb\u3064\u307e\u307f\u30fb\u6a5f\u68b0\u63a5\u89e6\u3092\u7121\u8996\u3057\u3066\u3044\u305f\u3002
2026-06-27 21:50:05 +09:00
if (heldByPlayer || beingDraggedByAnt || Math.hypot(Number(this.vx || 0) + Number(this.impulseVx || 0), Number(this.vy || 0) + Number(this.impulseVy || 0)) > 0.1) {
2026-06-21 16:26:12 +09:00
this.sleepDiseaseAnchorX = this.x;
this.sleepDiseaseAnchorY = this.y;
}
2026-06-21 14:09:35 +09:00
this.wanderAngle = this.wanderAngle || 0;
2026-06-28 13:40:41 +09:00
this.thought = beingDraggedByAnt ? "\u7720\u3063\u305f\u307e\u307e\u30a2\u30ea\u306b\u904b\u3070\u308c\u3066\u3044\u308b" : "\u306d\u3080\u308a\u75c5\u3067\u7720\u308a\u7d9a\u3051\u3066\u3044\u308b";
2026-06-21 14:09:35 +09:00
if (this.world && this.world.time > (this.nextSleepBubbleAt || 0)) {
this.nextSleepBubbleAt = this.world.time + 5.0;
this.world.spawnBubble?.(this.x, this.y - this.radius * 1.18, "Zzz...", "rgba(65,70,92,0.72)");
}
return true;
},
nestBoxById(id) {
if (!id) return null;
return this.world?.items?.find?.(it => it && !it.dead && it.id === id && it.type === "nest_box") || null;
},
2026-06-28 23:07:40 +09:00
nestBoxEntryActive() {
const now = this.world?.time || 0;
if (now < (this._nestBoxExitSmoothUntil || -Infinity)) return true;
if (!this.insideNestBoxId) return false;
return now < (this._nestBoxEntrySmoothUntil || -Infinity) || ((this.nestFade || 0) > 0.01 && (this.nestFade || 0) < 0.985);
},
2026-06-30 22:30:37 +09:00
abortNestBoxEntry(box = null, reason = "\u5DE3\u7BB1\u304C\u3044\u3063\u3071\u3044\u3067\u5165\u308C\u306A\u3044") {
2026-06-28 23:07:40 +09:00
const now = this.world?.time || 0;
if (this.insideNestBoxId && (!box || this.insideNestBoxId === box.id)) this.leaveNestBox?.(box);
if (!this.insideNestBoxId) this.nestFade = 0;
if (!box || this.target === box) {
this.target = null;
this.targetKey = "";
}
this.sleeping = false;
this.sleepSession = null;
this._nestBoxAvoidId = box?.id || this._nestBoxAvoidId || "";
this._nestBoxAvoidUntil = now + 10.0;
this.actionIdCooldowns = this.actionIdCooldowns || {};
this.actionCooldowns = this.actionCooldowns || {};
this.actionIdCooldowns.sleep_in_bed = Math.max(this.actionIdCooldowns.sleep_in_bed || 0, 8.0);
this.actionIdCooldowns.return_owned_structure = Math.max(this.actionIdCooldowns.return_owned_structure || 0, 5.0);
this.actionCooldowns.sleep = Math.max(this.actionCooldowns.sleep || 0, 4.5);
if (box) {
const entry = this.world?.nestBoxEntryPoint ? this.world.nestBoxEntryPoint(box) : { x: box.x, y: box.y };
const dx = this.x - (entry.x || box.x || this.x);
const dy = this.y - (entry.y || box.y || this.y);
const d = Math.hypot(dx, dy) || 1;
// Do not teleport/push away from the nest. A failed entry becomes a normal
// short avoidance target so ordinary movement handles the exit path.
this.vx = clamp((this.vx || 0) + dx / d * 10, -80, 80);
this.vy = clamp((this.vy || 0) + dy / d * 8, -80, 80);
}
this.awakeLockTimer = Math.max(this.awakeLockTimer || 0, 0.7);
this._nestBoxSeekStartedAt = NaN;
this._nestBoxSeekTargetId = "";
this.goIdle?.(reason);
this.thought = reason;
this.pinchThoughtTimer = Math.max(this.pinchThoughtTimer || 0, 1.0);
return false;
},
2026-06-21 14:09:35 +09:00
wantsNestBox(box = null) {
if (!box || box.dead || box.type !== "nest_box") return false;
2026-06-28 23:07:40 +09:00
const now = this.world?.time || 0;
if (this._nestBoxAvoidId === box.id && now < (this._nestBoxAvoidUntil || -Infinity)) return false;
2026-06-24 13:48:22 +09:00
if (this.insideNestBoxId === box.id && (this.world?.time || 0) < (this.nestBoxStayUntil || -Infinity) && (this.state === "sleep" || this.state === "seek_bed" || this.sleeping)) return true;
if (!(this.state === "sleep" || this.state === "seek_bed" || this.sleeping)) return false;
2026-06-21 14:09:35 +09:00
return this.target === box || this.insideNestBoxId === box.id;
},
2026-06-30 22:30:37 +09:00
enterNestBox(box, dt = 0.016, options = {}) {
2026-06-21 14:09:35 +09:00
if (!box || box.dead || box.type !== "nest_box" || this.dead) return false;
const capacity = this.world?.nestBoxCapacity ? this.world.nestBoxCapacity(box) : 5;
2026-06-30 22:30:37 +09:00
let occupants = this.world?.nestBoxOccupants ? this.world.nestBoxOccupants(box, Infinity) : [];
2026-06-21 14:09:35 +09:00
const alreadyInside = this.insideNestBoxId === box.id;
2026-06-28 23:07:40 +09:00
const now = this.world?.time || 0;
2026-06-30 22:30:37 +09:00
if (!alreadyInside && occupants.length >= capacity) {
if (options?.forceDisplace) {
const victim = occupants.find(t => t && t !== this && !t.dead && !t.isTarinaiChampion)
|| occupants.find(t => t && t !== this && !t.dead);
if (victim) {
victim.leaveNestBox?.(box);
victim.awakeLockTimer = Math.max(victim.awakeLockTimer || 0, 0.8);
victim.fearTimer = Math.max(victim.fearTimer || 0, 0.22);
victim.thought = "\u5DE3\u7BB1\u304B\u3089\u62BC\u3057\u51FA\u3055\u308C\u305F";
victim.pinchThoughtTimer = Math.max(victim.pinchThoughtTimer || 0, 0.65);
victim.goIdle?.("\u5DE3\u7BB1\u304B\u3089\u62BC\u3057\u51FA\u3055\u308C\u305F");
occupants = this.world?.nestBoxOccupants ? this.world.nestBoxOccupants(box, Infinity) : occupants.filter(t => t !== victim);
}
}
if (occupants.length >= capacity) return this.abortNestBoxEntry?.(box, "\u5DE3\u7BB1\u304C\u3044\u3063\u3071\u3044\u3067\u5165\u308C\u306A\u3044") || false;
}
2026-06-21 14:09:35 +09:00
if (!alreadyInside) {
2026-06-28 23:07:40 +09:00
this._nestBoxSeekStartedAt = NaN;
this._nestBoxSeekTargetId = "";
const entry = this.world?.nestBoxEntryPoint ? this.world.nestBoxEntryPoint(box) : { x: box.x, y: box.y };
this.nestBoxStayUntil = Math.max(this.nestBoxStayUntil || -Infinity, now + 14);
2026-06-28 13:40:41 +09:00
this.nestBoxSlotIndex = Math.min(occupants.length, Math.max(0, capacity - 1));
2026-06-28 23:07:40 +09:00
this._nestBoxEntryStartX = Number.isFinite(this.x) ? this.x : entry.x;
this._nestBoxEntryStartY = Number.isFinite(this.y) ? this.y : entry.y;
this._nestBoxEntryDoorX = Number.isFinite(entry.x) ? entry.x : box.x;
this._nestBoxEntryDoorY = Number.isFinite(entry.y) ? entry.y : box.y;
this._nestBoxEntryDuration = 0.72;
this._nestBoxEntrySmoothUntil = now + this._nestBoxEntryDuration;
this.nestFade = Math.min(this.nestFade || 0, 0.14);
2026-06-21 14:09:35 +09:00
}
this.insideNestBoxId = box.id;
this.target = box;
2026-06-24 13:48:22 +09:00
if (this.state === "seek_bed") this.startSleeping(box, "\u5de3\u7bb1\u306e\u4e2d\u3067\u7720\u3063\u3066\u3044\u308b");
2026-06-21 14:09:35 +09:00
if (this.state === "sleep") this.sleeping = true;
const inner = this.world?.nestBoxInnerPoint ? this.world.nestBoxInnerPoint(box, this) : { x: box.x, y: box.y };
2026-06-28 13:40:41 +09:00
const smoothDt = clamp(Number(dt || 0.016) || 0.016, 0.008, 0.045);
2026-06-28 23:07:40 +09:00
const duration = Math.max(0.35, Number(this._nestBoxEntryDuration || 0.95) || 0.95);
const progress = clamp(1 - Math.max(0, (this._nestBoxEntrySmoothUntil || now) - now) / duration, 0, 1);
const eased = progress * progress * (3 - 2 * progress);
if (progress < 1) {
const sx = Number.isFinite(this._nestBoxEntryStartX) ? this._nestBoxEntryStartX : this.x;
const sy = Number.isFinite(this._nestBoxEntryStartY) ? this._nestBoxEntryStartY : this.y;
const doorX = Number.isFinite(this._nestBoxEntryDoorX) ? this._nestBoxEntryDoorX : box.x;
const doorY = Number.isFinite(this._nestBoxEntryDoorY) ? this._nestBoxEntryDoorY : box.y;
const ax = lerp(sx, doorX, eased);
const ay = lerp(sy, doorY, eased);
const bx = lerp(doorX, inner.x, eased);
const by = lerp(doorY, inner.y, eased);
this.x = lerp(ax, bx, eased);
this.y = lerp(ay, by, eased);
this.nestFade = clamp(Math.max(this.nestFade || 0, eased * 0.92), 0, 1);
} else {
const pull = clamp(1 - Math.exp(-smoothDt * 5.2), 0, 0.22);
this.x = lerp(this.x, inner.x, pull);
this.y = lerp(this.y, inner.y, pull);
if (this.nestFade >= 0.97 || distXY(this.x, this.y, inner.x, inner.y) < 1.6) {
this.x = inner.x;
this.y = inner.y;
}
this.nestFade = clamp((this.nestFade || 0) + smoothDt * 1.8, 0, 1);
2026-06-21 14:09:35 +09:00
}
this.vx = 0;
this.vy = 0;
this.impulseVx = 0;
this.impulseVy = 0;
return true;
},
2026-06-28 23:07:40 +09:00
beginNestBoxExitAnimation(box = null) {
const b = box || this.nestBoxById(this.insideNestBoxId);
const now = this.world?.time || 0;
if (!b || !this.world?.nestBoxEntryPoint) {
this._nestBoxExitSmoothUntil = -Infinity;
this._nestBoxExitDuration = 0;
return false;
}
const inner = this.world?.nestBoxInnerPoint ? this.world.nestBoxInnerPoint(b, this) : { x: b.x, y: b.y };
const door = this.world.nestBoxEntryPoint(b);
const exit = this.world.nestBoxExitPoint ? this.world.nestBoxExitPoint(b, this) : door;
this._nestBoxExitStartX = Number.isFinite(this.x) ? this.x : inner.x;
this._nestBoxExitStartY = Number.isFinite(this.y) ? this.y : inner.y;
if ((this.nestFade || 0) > 0.82 || distXY(this._nestBoxExitStartX, this._nestBoxExitStartY, inner.x, inner.y) > (b.r || 42) * 0.7) {
this._nestBoxExitStartX = inner.x;
this._nestBoxExitStartY = inner.y;
}
this._nestBoxExitDoorX = Number.isFinite(door.x) ? door.x : b.x;
this._nestBoxExitDoorY = Number.isFinite(door.y) ? door.y : b.y;
this._nestBoxExitEndX = Number.isFinite(exit.x) ? exit.x : this._nestBoxExitDoorX;
this._nestBoxExitEndY = Number.isFinite(exit.y) ? exit.y : this._nestBoxExitDoorY;
this._nestBoxExitDuration = 0.58;
this._nestBoxExitSmoothUntil = now + this._nestBoxExitDuration;
this.nestFade = Math.max(this.nestFade || 0, 0.96);
return true;
},
updateNestBoxExitAnimation(dt = 0.016) {
const now = this.world?.time || 0;
const until = this._nestBoxExitSmoothUntil || -Infinity;
if (!(now < until)) return false;
const duration = Math.max(0.20, Number(this._nestBoxExitDuration || 0.58) || 0.58);
const progress = clamp(1 - Math.max(0, until - now) / duration, 0, 1);
const eased = progress * progress * (3 - 2 * progress);
const sx = Number.isFinite(this._nestBoxExitStartX) ? this._nestBoxExitStartX : this.x;
const sy = Number.isFinite(this._nestBoxExitStartY) ? this._nestBoxExitStartY : this.y;
const dx = Number.isFinite(this._nestBoxExitDoorX) ? this._nestBoxExitDoorX : sx;
const dy = Number.isFinite(this._nestBoxExitDoorY) ? this._nestBoxExitDoorY : sy;
const ex = Number.isFinite(this._nestBoxExitEndX) ? this._nestBoxExitEndX : dx;
const ey = Number.isFinite(this._nestBoxExitEndY) ? this._nestBoxExitEndY : dy;
const ax = lerp(sx, dx, eased);
const ay = lerp(sy, dy, eased);
const bx = lerp(dx, ex, eased);
const by = lerp(dy, ey, eased);
const pad = CONFIG.worldPadding || 30;
this.x = clamp(lerp(ax, bx, eased), pad, Math.max(pad, (this.world?.w || this.x) - pad));
this.y = clamp(lerp(ay, by, eased), pad, Math.max(pad, (this.world?.h || this.y) - pad));
this.nestFade = clamp(1 - eased, 0, 1);
this.vx = 0;
this.vy = 0;
if (progress >= 0.995 || until - now <= Math.max(0.018, dt || 0.016)) {
this.x = clamp(ex, pad, Math.max(pad, (this.world?.w || ex) - pad));
this.y = clamp(ey, pad, Math.max(pad, (this.world?.h || ey) - pad));
const outDx = ex - dx;
const outDy = ey - dy;
const outD = Math.hypot(outDx, outDy) || 1;
this.vx = outDx / outD * deterministicRange(this.world, "nest-exit-vx", 6, 14, this);
this.vy = outDy / outD * deterministicRange(this.world, "nest-exit-vy", 8, 18, this);
this.nestFade = 0;
this._nestBoxExitSmoothUntil = -Infinity;
this._nestBoxExitDuration = 0;
this._nestBoxExitStartX = this._nestBoxExitStartY = this._nestBoxExitDoorX = this._nestBoxExitDoorY = this._nestBoxExitEndX = this._nestBoxExitEndY = NaN;
}
return true;
},
2026-06-21 14:09:35 +09:00
leaveNestBox(box = null) {
const b = box || this.nestBoxById(this.insideNestBoxId);
2026-06-28 23:07:40 +09:00
const hadInside = Boolean(this.insideNestBoxId);
if (b && this.world?.nestBoxEntryPoint && hadInside) this.beginNestBoxExitAnimation?.(b);
else if (b && this.world?.nestBoxEntryPoint) {
2026-06-21 14:09:35 +09:00
const entry = this.world.nestBoxExitPoint ? this.world.nestBoxExitPoint(b, this) : this.world.nestBoxEntryPoint(b);
this.x = clamp(entry.x, CONFIG.worldPadding, this.world.w - CONFIG.worldPadding);
this.y = clamp(entry.y, CONFIG.worldPadding, this.world.h - CONFIG.worldPadding);
}
this.insideNestBoxId = null;
2026-06-28 23:07:40 +09:00
if (this.target === b) {
this.target = null;
this.targetKey = "";
}
this.sleeping = false;
this.sleepSession = null;
2026-06-30 22:30:37 +09:00
if (this.state === "sleep" || this.state === "seek_bed") this.goIdle?.("\u5DE3\u7BB1\u304B\u3089\u51FA\u305F");
2026-06-21 14:09:35 +09:00
this.nestBoxStayUntil = -Infinity;
2026-06-28 13:40:41 +09:00
this.nestBoxSlotIndex = -1;
this._nestBoxEntrySmoothUntil = -Infinity;
2026-06-28 23:07:40 +09:00
this._nestBoxEntryStartX = NaN;
this._nestBoxEntryStartY = NaN;
this._nestBoxEntryDoorX = NaN;
this._nestBoxEntryDoorY = NaN;
this._nestBoxEntryDuration = 0;
this._nestBoxSeekStartedAt = NaN;
this._nestBoxSeekTargetId = "";
2026-06-21 14:09:35 +09:00
return true;
},
updateNestBoxPresence(dt = 0) {
2026-06-28 23:07:40 +09:00
if (this.updateNestBoxExitAnimation?.(dt || 0.016)) return null;
2026-06-21 14:09:35 +09:00
const currentBox = this.nestBoxById(this.insideNestBoxId);
const targetBox = this.target && !this.target.dead && this.target.type === "nest_box" ? this.target : null;
const box = currentBox || targetBox;
if (!box) {
this.insideNestBoxId = null;
this.nestFade = clamp((this.nestFade || 0) - dt * 3.0, 0, 1);
return null;
}
if (currentBox && !this.wantsNestBox(currentBox)) {
this.leaveNestBox(currentBox);
return null;
}
if (currentBox) {
2026-06-24 13:48:22 +09:00
if ((this.world?.time || 0) < (this.nestBoxStayUntil || -Infinity) && (this.state === "sleep" || this.state === "seek_bed" || this.sleeping)) {
this.startSleeping(box, "\u5de3\u7bb1\u306e\u4e2d\u3067\u7720\u3063\u3066\u3044\u308b");
2026-06-21 14:09:35 +09:00
this.sleeping = true;
this.target = currentBox;
}
this.enterNestBox(currentBox, dt);
return currentBox;
}
if (!this.wantsNestBox(targetBox)) {
this.nestFade = clamp((this.nestFade || 0) - dt * 3.0, 0, 1);
2026-06-28 23:07:40 +09:00
if (targetBox && this.target === targetBox && !this.insideNestBoxId) {
const now = this.world?.time || 0;
this.target = null;
this.targetKey = "";
this._nestBoxAvoidId = targetBox.id || this._nestBoxAvoidId || "";
this._nestBoxAvoidUntil = Math.max(this._nestBoxAvoidUntil || -Infinity, now + 3.2);
if (this.state === "idle" || this.state === "wander") this.wanderAngle = (this.wanderAngle || 0) + deterministicRange(this.world, "nest-idle-dispersal-angle", -1.1, 1.1, this, targetBox);
}
2026-06-21 14:09:35 +09:00
return null;
}
2026-06-28 23:07:40 +09:00
if (!Number.isFinite(this._nestBoxSeekStartedAt) || this._nestBoxSeekTargetId !== targetBox.id) {
this._nestBoxSeekStartedAt = this.world?.time || 0;
this._nestBoxSeekTargetId = targetBox.id;
}
2026-06-29 16:21:58 +09:00
if (!this.insideNestBoxId && global.TarinaiNestSleepSystem.sleepPlaceAvailableFor && !global.TarinaiNestSleepSystem.sleepPlaceAvailableFor(this.world, this, targetBox)) {
2026-06-30 22:30:37 +09:00
return this.abortNestBoxEntry?.(targetBox, "\u5DE3\u7BB1\u304C\u3044\u3063\u3071\u3044\u3067\u5165\u308C\u306A\u3044") || null;
2026-06-28 23:07:40 +09:00
}
2026-06-21 14:09:35 +09:00
const entry = this.world?.nestBoxEntryPoint ? this.world.nestBoxEntryPoint(targetBox) : { x: targetBox.x, y: targetBox.y };
const base = this.world?.nestBoxBaseRect ? this.world.nestBoxBaseRect(targetBox) : null;
const r = targetBox.r || 42;
const entryD = distXY(this.x, this.y, entry.x, entry.y);
const centerD = distXY(this.x, this.y, targetBox.x, targetBox.y);
2026-06-28 23:07:40 +09:00
const inDoorColumn = base && this.x >= base.left + r * 0.30 && this.x <= base.right - r * 0.30 && this.y >= base.top + r * 0.16 && this.y <= base.bottom + r * 0.34;
const captureRange = Math.max(58, r * 1.18, (this.radius || 20) + 30);
const nearEnough = entryD <= captureRange || centerD <= Math.max(58, r * 1.08) || inDoorColumn;
2026-06-21 14:09:35 +09:00
if (nearEnough) {
2026-06-28 23:07:40 +09:00
if (this.enterNestBox(targetBox, dt || 0.016)) return targetBox;
return null;
2026-06-21 14:09:35 +09:00
}
this.nestFade = clamp((this.nestFade || 0) - dt * 3.0, 0, 1);
return null;
},
applyWaterEffect(dt, source = "water") {
const power = Math.max(0, dt || 0);
2026-06-23 18:11:42 +09:00
if (typeof applyNeedRelief === "function") applyNeedRelief(this, { health: -power * 12, safety: -power * 6 });
2026-06-26 12:46:32 +09:00
this.energy = clamp(this.energy + power * 0.5, 0, typeof tarinaiMaxEnergy === "function" ? tarinaiMaxEnergy(this) : (Number(this.maxEnergy) || 100));
2026-06-21 14:09:35 +09:00
this.hunger = clamp(this.hunger - power * 0.2, 0, 115);
this.zunchiStain = clamp((this.zunchiStain || 0) - power * 10.5, 0, 100);
if (this.zunchiDisease) {
this.zunchiDiseaseSeverity = Math.max(0, (this.zunchiDiseaseSeverity || 1) - power * (this.energy > 70 ? 0.050 : 0.028));
if (this.zunchiDiseaseSeverity <= 0.04 && this.recoverZunchiDisease("\u6c34\u3067\u305a\u3093\u3061\u75c5\u304c\u6cbb\u307e\u3063\u305f")) {
this.world?.log?.(`${this.name}\u306f\u6c34\u3067\u305a\u3093\u3061\u75c5\u304c\u6cbb\u307e\u3063\u305f\u3002`, "accident", { participants: [this] });
}
}
if (this.explosionDisease) this.recoverExplosionDisease("\u6c34\u3067\u7206\u767a\u75c5\u304c\u6cbb\u3063\u305f");
},
2026-06-30 22:30:37 +09:00
igniteFire(source = null, force = false) {
if (this.dead) return false;
const already = Boolean(this.burning || (this.burnTimer || 0) > 0.02);
if (already && !force) return false;
const text = globalThis.TarinaiItemDynamicToolSystem?.fireBurningText || "\u71c3\u3048\u3066\u9003\u3052\u307e\u3069\u3063\u3066\u3044\u308b\u3002";
this.burning = true;
this.burnTimer = Math.max(Number(this.burnTimer || 0) || 0, 5.2 + deterministicRange(this.world, "tarinai-burn-duration", 0, 2.8, this, source || null));
this.firePanicTimer = 0;
this.lastPanicCause = "burning";
this.lastPanicDetail = text;
this.thought = text;
this.sleeping = false;
this.awakeLockTimer = Math.max(this.awakeLockTimer || 0, 0.8);
this.fearTimer = Math.max(this.fearTimer || 0, 2.2);
this.surpriseTimer = Math.max(this.surpriseTimer || 0, 0.65);
this.eatTimer = 0;
this.target = null;
this.burnWanderAngle = Number.isFinite(this.burnWanderAngle) ? this.burnWanderAngle : deterministicAngle(this.world, "tarinai-burn-angle", this, source || null);
if (typeof this.enterPanic === "function") this.enterPanic({ threat: source, target: null, reason: text, fear: 2.2, wake: true, cause: "burning", surpriseTimer: 0.65, awakeLockTimer: 0.8 });
else this.setActionState?.("panic", { target: null, reason: text, wake: true });
this.world?.effects?.push(new Effect("ring", this.x, this.y, { size: Math.max(26, this.radius * 1.35), life: 0.26, color: "rgba(255,110,36,0.58)" }));
2026-07-01 16:55:49 +09:00
const now = Number(this.world?.time || 0);
if (force || !already || now >= Number(this.nextBurnVoiceAt || -999)) {
this.nextBurnVoiceAt = now + 2.6;
this.bubble?.("\u3042\u3061\u3085\u3044\uff01", 1.35, "rgba(214,82,38,0.90)", { force: true });
}
2026-06-30 22:30:37 +09:00
this.world?.emit?.("fire:ignite", { tarinai: this, source });
2026-07-01 16:55:49 +09:00
this.recordChangeCause?.("\u708e\u4e0a", "\u708e\u4e0a");
2026-06-30 22:30:37 +09:00
return true;
},
extinguishFire(reason = "") {
if (!this.burning && !(this.burnTimer > 0)) return false;
this.burning = false;
this.burnTimer = 0;
this.firePanicTimer = Math.max(this.firePanicTimer || 0, 0.25);
this.hurtTimer = Math.max(this.hurtTimer || 0, 0.35);
if (reason) this.thought = reason;
this.world?.effects?.push(new Effect("ring", this.x, this.y, { size: Math.max(22, this.radius * 1.2), life: 0.22, color: "rgba(108,192,236,0.60)" }));
this.world?.emit?.("fire:extinguish", { tarinai: this, reason });
return true;
},
updateBurning(dt) {
this.firePanicTimer = Math.max(0, Number(this.firePanicTimer || 0) - dt);
if (!this.burning && !(this.burnTimer > 0)) return;
const text = globalThis.TarinaiItemDynamicToolSystem?.fireBurningText || "\u71c3\u3048\u3066\u9003\u3052\u307e\u3069\u3063\u3066\u3044\u308b\u3002";
this.burning = true;
this.burnTimer = Math.max(0, Number(this.burnTimer || 0) - dt);
this.lastPanicCause = "burning";
this.lastPanicDetail = text;
this.thought = text;
this.fearTimer = Math.max(this.fearTimer || 0, 0.8);
this.sleeping = false;
this.eatTimer = 0;
if (typeof applyNeedShock === "function") applyNeedShock(this, { health: dt * 6.2, safety: dt * 3.0 });
2026-07-01 16:55:49 +09:00
if (typeof this.damage === "function") this.damage(dt * 3.8, "\u708e\u4e0a");
2026-06-30 22:30:37 +09:00
else this.energy = clamp((this.energy || 0) - dt * 0.12, 0, typeof tarinaiMaxEnergy === "function" ? tarinaiMaxEnergy(this) : (Number(this.maxEnergy) || 100));
this.hurtTimer = Math.max(this.hurtTimer || 0, 0.22);
const water = globalThis.TarinaiItemDynamicToolSystem?.contactWaterDrop?.(this.world, this.x, this.y, Math.max(this.radius || 18, 16));
if (water) {
globalThis.TarinaiItemDynamicToolSystem?.consumeWaterForFire?.(this.world, water, Math.max(10, (this.radius || 18) * 0.55));
this.extinguishFire("\u6c34\u3067\u6d88\u706b\u3055\u308c\u305f");
return;
}
const mul = globalThis.TarinaiItemDynamicToolSystem?.fireTempMultiplier?.(this.world, this.x, this.y, this) || 1;
if (deterministicChance(this.world, "tarinai-fire-extinguish", dt * 0.016 * mul, this)) {
this.extinguishFire("\u81ea\u7136\u306b\u706b\u304c\u6d88\u3048\u305f");
return;
}
for (const viewer of [...(this.world?.nearbyTarinai?.(this.x, this.y, 190, true) || [])]) {
if (!viewer || viewer === this || viewer.dead) continue;
globalThis.TarinaiItemDynamicToolSystem?.panicTarinaiFromFire?.(viewer, this, this.world);
}
const contactRadius = Math.max(20, (this.radius || 18) * 1.1);
for (const other of [...(this.world?.nearbyTarinai?.(this.x, this.y, contactRadius + 28, true) || [])]) {
if (!other || other === this || other.dead) continue;
if (distXY(this.x, this.y, other.x, other.y) <= contactRadius + Math.max(other.radius || 18, 12) * 0.72) other.igniteFire?.(this, false);
}
for (const ant of [...(this.world?.nearbyAnts?.(this.x, this.y, contactRadius + 18, true) || [])]) {
if (!ant || ant.dead) continue;
if (distXY(this.x, this.y, ant.x, ant.y) <= contactRadius + Math.max(ant.r || 4, 4)) globalThis.TarinaiItemDynamicToolSystem?.igniteAntByFire?.(ant, this, this.world);
}
for (const it of [...(this.world?.nearbyItems?.(this.x, this.y, contactRadius + 32, true) || [])]) {
if (!it || it.dead || it.type !== "grass") continue;
if (distXY(this.x, this.y, it.x, it.y) <= contactRadius + Math.max(it.r || 16, 12)) globalThis.TarinaiItemDynamicToolSystem?.igniteGrassByFire?.(it, this, this.world, dt);
}
if (this.burnTimer <= 0.01) this.extinguishFire("\u81ea\u7136\u306b\u706b\u304c\u6d88\u3048\u305f");
},
2026-06-21 14:09:35 +09:00
updateSpecialDiseases(dt) {
this.sleepDiseaseCooldown = Math.max(0, (this.sleepDiseaseCooldown || 0) - dt);
this.fightDiseaseCooldown = Math.max(0, (this.fightDiseaseCooldown || 0) - dt);
if (this.explosionDisease) {
this.explosionDiseaseTimer = Math.max(0, (this.explosionDiseaseTimer || 0) - dt);
2026-06-23 18:11:42 +09:00
if (this.addStress) this.addStress(dt * 0.22, { threshold: 9, duration: 3.6 });
2026-06-27 19:22:38 +09:00
if (deterministicChance(this.world, "explosion-disease-spark", dt * 2.1, this)) {
const a = deterministicAngle(this.world, "explosion-disease-spark-angle", this);
this.world?.effects?.push(new Effect("explosion", this.x + Math.cos(a) * this.radius * deterministicRange(this.world, "explosion-disease-spark-rx", 0.2, 0.9, this), this.y + Math.sin(a) * this.radius * deterministicRange(this.world, "explosion-disease-spark-ry", 0.1, 0.8, this), {
vx: Math.cos(a) * deterministicRange(this.world, "explosion-disease-spark-vx", 10, 38, this), vy: Math.sin(a) * deterministicRange(this.world, "explosion-disease-spark-vy", 10, 38, this) - deterministicRange(this.world, "explosion-disease-spark-up", 8, 22, this), size: deterministicRange(this.world, "explosion-disease-spark-size", 5, 12, this), life: deterministicRange(this.world, "explosion-disease-spark-life", 0.18, 0.42, this), color: "rgba(255,64,48,0.86)"
2026-06-21 14:09:35 +09:00
}));
}
if (this.explosionDiseaseTimer <= 0.01) {
this.world?.explodeDiseaseTarinai?.(this);
return;
}
2026-06-27 19:22:38 +09:00
if (this.explosionDiseaseTimer < 8 && deterministicChance(this.world, "explosion-disease-bubble", dt * 0.45, this)) this.bubble("!", 1.0, "rgba(178,78,42,0.82)");
2026-06-21 14:09:35 +09:00
}
const bleedAge = (this.world?.time || 0) - (this.lastBleedAt || -999);
if (!this.fightDisease && this.fightDiseaseCooldown <= 0 && bleedAge >= (CONFIG.dayLength || 120) * 5 / 24 && bleedAge < (CONFIG.dayLength || 120) * 0.85) {
2026-06-27 19:22:38 +09:00
if (deterministicChance(this.world, "fight-disease-from-bleed", this.diseaseChance ? this.diseaseChance(dt * 0.000375) : dt * 0.000375, this)) this.infectFightDisease();
2026-06-21 14:09:35 +09:00
}
2026-06-27 19:22:38 +09:00
if (this.zunchiDisease && deterministicChance(this.world, "zunchi-natural-recover", dt * 0.00055 * (this.energy > 66 ? 1.8 : 1.0), this)) {
2026-06-21 14:09:35 +09:00
if (this.recoverZunchiDisease("\u81ea\u7136\u306b\u305a\u3093\u3061\u75c5\u304c\u6cbb\u307e\u3063\u305f")) this.world?.log?.(`${this.name}\u306e\u305a\u3093\u3061\u75c5\u304c\u81ea\u7136\u306b\u6cbb\u3063\u305f\u3002`, "accident", { participants: [this] });
}
2026-06-27 19:22:38 +09:00
if (this.fightDisease && deterministicChance(this.world, "fight-disease-natural-recover", dt * 0.00070 * (this.energy > 58 ? 1.4 : 1.0), this)) {
2026-06-21 14:09:35 +09:00
this.recoverFightDisease("\u81ea\u7136\u306b\u304d\u305a\u3064\u304d\u75c5\u304c\u6cbb\u3063\u305f");
}
if (this.fightDisease) {
2026-06-23 18:11:42 +09:00
if (typeof applyNeedShock === "function") applyNeedShock(this, { health: dt * 3, safety: dt * 2 });
2026-06-27 19:22:38 +09:00
if (deterministicChance(this.world, "fight-disease-wander", dt * 0.30, this)) this.wanderAngle += deterministicRange(this.world, "fight-disease-wander-angle", -2.8, 2.8, this);
if (deterministicChance(this.world, "fight-disease-bubble", dt * 0.08, this)) this.bubble("?", 2.6, "rgba(150,78,44,0.80)");
2026-06-21 14:09:35 +09:00
}
}
}));
})(typeof window !== "undefined" ? window : globalThis);