tarinai/js/health.js

154 lines
7 KiB
JavaScript
Raw Normal View History

2026-06-20 02:07:01 +09:00
"use strict";
const HEALTH = (() => {
const MAJOR_DAMAGE_WINDOW = 10;
const WEAKENED_SUFFIX = "\u3067\u8870\u5f31";
const CAUSES = Object.freeze({
fight: "\u55a7\u5629",
hunger: "\u98e2\u9913",
stress: "\u30b9\u30c8\u30ec\u30b9\u904e\u591a",
poke: "\u3064\u3064\u304b\u308c\u3059\u304e\u305f",
firecracker: "\u7206\u7af9",
accident: "\u4e8b\u6545",
2026-06-20 15:55:52 +09:00
collision: "衝突",
outOfBounds: "仕様により画面外で削除",
lifespan: "天寿を全うした",
2026-06-20 02:07:01 +09:00
sleepDisease: "\u306d\u3080\u308a\u75c5",
explosionDisease: "\u7206\u767a\u75c5",
2026-06-20 15:55:52 +09:00
fightDisease: "きずつき病",
2026-06-20 02:07:01 +09:00
zunchiDisease: "\u305a\u3093\u3061\u75c5",
});
const DISEASE_RE = /([^\u3001\u3002\s]+\u75c5)/;
function causeLabel(reason = "") {
const text = String(reason || "");
if (!text) return "";
if (text.includes(WEAKENED_SUFFIX)) return text.replace(/\u3067\u8870\u5f31.*$/, "");
if (/\u55a7\u5629|fight|headbutt/.test(text)) return CAUSES.fight;
if (/\u7206\u7af9|firecracker|explosion/.test(text)) return CAUSES.firecracker;
2026-06-20 15:55:52 +09:00
if (/仕様により画面外で削除|画面外で削除|out.*bounds|offscreen/.test(text)) return CAUSES.outOfBounds;
if (/衝突|collision/.test(text)) return CAUSES.collision;
2026-06-20 02:07:01 +09:00
if (/\u3064\u3064\u304b\u308c|\u3064\u3064\u304d|poke/.test(text)) return CAUSES.poke;
if (/\u306d\u3080\u308a\u75c5|sleep.*disease/.test(text)) return CAUSES.sleepDisease;
if (/\u7206\u767a\u75c5|explosion.*disease/.test(text)) return CAUSES.explosionDisease;
2026-06-20 15:55:52 +09:00
if (/\u3051\u3093\u304b\u75c5|喧嘩傷病|きずつき病|fight.*disease/.test(text)) return CAUSES.fightDisease;
2026-06-20 02:07:01 +09:00
if (/\u305a\u3093\u3061\u75c5|zunchi_sick|zunchi.*\u75c5/.test(text)) return CAUSES.zunchiDisease;
if (/\u75c5/.test(text)) {
const disease = text.match(DISEASE_RE);
return disease ? disease[1] : CAUSES.zunchiDisease;
}
if (/\u98e2|\u7a7a\u8179|\u98df\u3079\u7269|hunger/.test(text)) return CAUSES.hunger;
if (/\u30b9\u30c8\u30ec\u30b9|stress/.test(text)) return CAUSES.stress;
2026-06-20 15:55:52 +09:00
if (/\u30dc\u30fc\u30eb|\u843d\u3061|\u843d\u4e0b|\u77f3|\u9053\u5177|\u4e8b\u6545|\u5f53\u305f|ball|drop|stone|accident/.test(text)) return CAUSES.accident;
if (/\u5bff\u547d|天寿を全うした/.test(text)) return CAUSES.lifespan;
2026-06-20 02:07:01 +09:00
return "";
}
function recentDamageCause(t, maxAge = MAJOR_DAMAGE_WINDOW) {
const now = t.world?.time || 0;
if (t.lastDamageCause && now - (t.lastDamageAt || -999) <= maxAge) return t.lastDamageCause;
return "";
}
function rememberDamage(t, amount, reason = "") {
const cause = causeLabel(reason) || CAUSES.accident;
const now = t.world?.time || 0;
t.lastDamageCause = cause;
t.lastDamageAmount = Math.max(0, amount || 0);
t.lastDamageAt = now;
if (t.lastDamageAmount >= 10 || t.lastDamageAmount >= Math.max(6, (t.energy || 0) * 0.36)) {
t.lastMajorDamageCause = cause;
t.lastMajorDamageAmount = t.lastDamageAmount;
t.lastMajorDamageAt = now;
}
return cause;
}
function weakenedDeathReasonFor(cause = "") {
const text = String(cause || "").trim();
if (!text) return "";
return text.includes(WEAKENED_SUFFIX) ? text : `${text}${WEAKENED_SUFFIX}`;
}
function hasRecentMajorDamage(t) {
const now = t.world?.time || 0;
return Boolean(t.lastMajorDamageCause && now - (t.lastMajorDamageAt || -999) <= MAJOR_DAMAGE_WINDOW);
}
function isFightingContext(t, recent) {
return Boolean(t.fightTimer > 0.04 || t.defeatedTimer > 0.04 || t.defeatedById || t.fightWinnerId || recent === CAUSES.fight);
}
function dominantDeathCause(t, reason = "", opts = {}) {
const text = String(reason || "").trim();
const direct = causeLabel(text);
const recentMajor = hasRecentMajorDamage(t);
const recent = recentDamageCause(t, 20);
const fighting = isFightingContext(t, recent);
2026-06-20 15:55:52 +09:00
if (recentMajor && t.lastMajorDamageCause === CAUSES.collision) return CAUSES.collision;
2026-06-20 02:07:01 +09:00
if (recentMajor && (!direct || direct === t.lastMajorDamageCause || opts.fromDamage || t.energy <= 22 || t.mood <= 20 || t.stress >= 118)) {
return weakenedDeathReasonFor(t.lastMajorDamageCause);
}
if (direct && direct !== CAUSES.stress) return direct;
if (t.zunchiDisease && ((t.zunchiDiseaseSeverity || 0) >= 0.30 || /\u75c5|zunchi/.test(text))) return CAUSES.zunchiDisease;
if (t.hunger >= 108 || (t.hunger >= 94 && t.energy <= 26) || /\u98e2|\u7a7a\u8179|hunger/.test(text)) return CAUSES.hunger;
if (fighting && (t.energy <= 44 || t.stress >= 112 || t.mood <= 22 || opts.fromDamage)) return CAUSES.fight;
if (recent && recent !== CAUSES.stress && (t.energy <= 34 || t.mood <= 20 || t.stress >= 118 || opts.fromDamage)) return recent;
2026-06-20 15:55:52 +09:00
if (text.includes(CAUSES.lifespan) || text.includes("\u5bff\u547d")) return CAUSES.lifespan;
2026-06-20 02:07:01 +09:00
if (direct) return direct;
if (t.stress >= 118 || /\u30b9\u30c8\u30ec\u30b9|stress|\u6c17\u5206/.test(text)) return CAUSES.stress;
if (t.energy <= 0.5) return CAUSES.accident;
return "";
}
function normalizeDeathReason(t, reason = "", opts = {}) {
const text = String(reason || "").trim();
if (text.includes(WEAKENED_SUFFIX)) return text;
const cause = dominantDeathCause(t, text, opts) || CAUSES.accident;
if (cause.includes(WEAKENED_SUFFIX)) return cause;
2026-06-20 15:55:52 +09:00
const canWeaken = cause && !cause.endsWith("\u75c5") && cause !== CAUSES.hunger && cause !== CAUSES.stress && cause !== CAUSES.lifespan && cause !== CAUSES.outOfBounds && cause !== CAUSES.collision;
2026-06-20 02:07:01 +09:00
if (canWeaken && (opts.weakened || (hasRecentMajorDamage(t) && cause === t.lastMajorDamageCause))) return weakenedDeathReasonFor(cause);
return cause;
}
function applyDamage(t, amount, reason = "") {
const before = t.energy;
const loss = Math.max(0, amount || 0);
t.energy = clamp(t.energy - loss, 0, 100);
const actualLoss = Math.max(0, before - t.energy);
let cause = "";
if (actualLoss > 0.01) {
cause = rememberDamage(t, actualLoss, reason);
if (cause === CAUSES.fight || /\u55a7\u5629|fight/.test(String(reason || ""))) t.recordBleedExposure();
if (t.sleepDisease) t.recoverSleepDisease("\u30c0\u30e1\u30fc\u30b8\u3067\u306d\u3080\u308a\u75c5\u304c\u6cbb\u3063\u305f");
2026-06-20 15:55:52 +09:00
if (t.maybeBecomeTimidAfterDamage) t.maybeBecomeTimidAfterDamage(cause || reason);
2026-06-20 02:07:01 +09:00
t.showHpBar();
}
if (t.energy <= 0.5) {
const majorNow = actualLoss >= Math.max(10, before * 0.42);
t.die(normalizeDeathReason(t, reason || cause, { fromDamage: true, weakened: majorNow }));
}
}
function briefDeathReason(t, reason = t.deathReason) {
const text = String(reason || "");
if (!text) return "";
const normalized = normalizeDeathReason(t, text);
return normalized.length > 12 ? `${normalized.slice(0, 12)}\u2026` : normalized;
}
return Object.freeze({
MAJOR_DAMAGE_WINDOW,
CAUSES,
causeLabel,
recentDamageCause,
rememberDamage,
weakenedDeathReasonFor,
dominantDeathCause,
normalizeDeathReason,
applyDamage,
briefDeathReason,
});
})();
window.HEALTH = HEALTH;