tarinai/js/item_update_policy.js
2026-06-29 21:02:04 +09:00

84 lines
3.5 KiB
JavaScript

"use strict";
// Layer: entity-runtime/items/policy
// Owns the item update cadence and scheduled-item candidate set. Simulation
// helpers and the item scheduler both read this single policy instead of
// duplicating item-type timing rules.
(function (global) {
const REALTIME_ITEM_TYPES = new Set(["ball", "genkotsu", "firecracker", "robot_cleaner"]);
const PIN_ITEM_TYPES = new Set(["pushpin", "oshibyo"]);
const SCHEDULED_ITEM_TYPES = Object.freeze([
"ball", "genkotsu", "firecracker", "pushpin", "oshibyo",
"plushie", "grass_bed", "zunchi", "robot_cleaner", "duplicator", "water", "trace", "splat", "ant_corpse", "food",
"sweet", "love_mochi", "fight_mochi", "sleep_drug", "laxative", "protein", "niteropu", "ammo", "mystery_drug", "mercury", "giant_drug", "dwarf_drug", "zunda_juice",
"stone", "bed", "nest_box", "ant_nest", "signboard", "fence_v", "fence_h", "bounce_fence", "bounce_fence_v", "gate_fence", "rotator", "poison_block", "reciprocator", "rope", "rod",
]);
function mobilePerfHint() {
return Boolean(global.TarinaiInputMode.snapshot().touchFirst || (global.innerWidth || 9999) <= 760);
}
function itemUpdateInterval(item) {
if (!item || item.dead) return Infinity;
const mobile = mobilePerfHint();
// Mechanical bodies and links are owned by the central physics step.
// Keeping them out of the per-item scheduler prevents N independent
// collision passes and makes future physics-format changes cheaper.
if (item.type === "rotator" || item.type === "reciprocator" || item.type === "poison_block" || item.type === "rope" || item.type === "rod") return Infinity;
if (REALTIME_ITEM_TYPES.has(item.type)) return 0;
if (PIN_ITEM_TYPES.has(item.type)) return 0;
if (item.isStructure && item.type === "plushie") return 0;
if (item.isStructure && item.type === "grass_bed") return 3.0;
if ((item.dropTimer || 0) > 0 || Math.hypot(item.vx || 0, item.vy || 0) > 0.08) return 0;
if (item.type === "zunchi") return 2.4;
if (item.type === "grass") return Infinity;
if (item.type === "duplicator") return mobile ? 0.8 : 0.5;
if (item.type === "water") return mobile ? 2.4 : 1.8;
if (item.type === "trace" || item.type === "splat") return mobile ? 1.8 : 1.2;
if (item.type === "ant_corpse") return 3.5;
if (typeof global.isServingFoodType === "function" && global.isServingFoodType(item.type)) return 5.0;
if (isServingFoodType(item.type)) return 5.0;
return Infinity;
}
function ensureBuckets(worldRef) {
if (worldRef?.ensureItemBuckets) worldRef.ensureItemBuckets("item-update-policy");
}
function forScheduledItems(worldRef, visitor) {
if (!worldRef || typeof visitor !== "function") return 0;
let count = 0;
if (!worldRef.itemsOfType) {
for (const it of worldRef.items || []) {
if (!it || it.dead || it.type === "grass") continue;
visitor(it);
count += 1;
}
return count;
}
ensureBuckets(worldRef);
for (const type of SCHEDULED_ITEM_TYPES) {
const bucket = worldRef.itemsOfType(type);
for (const it of bucket || []) {
if (!it || it.dead) continue;
visitor(it);
count += 1;
}
}
return count;
}
function candidateItems(worldRef) {
const out = [];
forScheduledItems(worldRef, (item) => out.push(item));
return out;
}
global.TarinaiItemUpdatePolicy = Object.freeze({
itemUpdateInterval,
forScheduledItems,
ensureBuckets,
candidateItems,
});
})(typeof window !== "undefined" ? window : globalThis);