1026 lines
45 KiB
JavaScript
1026 lines
45 KiB
JavaScript
"use strict";
|
|
|
|
(function (global) {
|
|
const World = global.World;
|
|
if (!World) throw new Error("World is not available for mixin: world_environment.js");
|
|
|
|
function normalizedAngleLocal(angle = 0, fallback = 0) {
|
|
if (typeof normalizedItemAngle === "function") return normalizedItemAngle(angle, fallback);
|
|
const twoPi = Math.PI * 2;
|
|
const n = Number(angle);
|
|
const base = Number.isFinite(n) ? n : fallback;
|
|
return ((base % twoPi) + twoPi) % twoPi;
|
|
}
|
|
|
|
function angleForItemLocal(it) {
|
|
if (typeof itemAngleFor === "function") return itemAngleFor(it);
|
|
const type = String(it?.type || "");
|
|
const fallback = (type === "fence_v" || type === "bounce_fence_v") ? Math.PI / 2 : 0;
|
|
return normalizedAngleLocal(it?.angle, fallback);
|
|
}
|
|
|
|
|
|
function orientedRectAabb(cx, cy, halfW, halfH, angle) {
|
|
const c = Math.cos(angle);
|
|
const s = Math.sin(angle);
|
|
const ex = Math.abs(c) * halfW + Math.abs(s) * halfH;
|
|
const ey = Math.abs(s) * halfW + Math.abs(c) * halfH;
|
|
return { left: cx - ex, right: cx + ex, top: cy - ey, bottom: cy + ey, cos: c, sin: s };
|
|
}
|
|
|
|
function obstacleQueryDistanceLocal(x, y, it) {
|
|
if (!it) return Infinity;
|
|
const type = String(it.type || "");
|
|
let reach = Math.max(32, Number(it.r || it.radius || 32) || 32);
|
|
if (type === "fence" || type === "fence_v" || type === "bounce_fence" || type === "bounce_fence_v" || type === "gate_fence") reach = Math.max(120, reach * 3.9);
|
|
else if (type === "nest_box") reach = Math.max(92, reach * 1.9);
|
|
else reach = global.TarinaiMechanicalShapeSystem?.reach?.(it) || Math.max(60, reach * 1.6);
|
|
return Math.max(0, distXY(x, y, Number(it.x) || 0, Number(it.y) || 0) - reach);
|
|
}
|
|
|
|
function rectDistanceToPointLocal(rect, x, y) {
|
|
if (!rect) return Infinity;
|
|
const px = Math.max(Number(rect.left || 0), Math.min(Number(rect.right || 0), Number(x) || 0));
|
|
const py = Math.max(Number(rect.top || 0), Math.min(Number(rect.bottom || 0), Number(y) || 0));
|
|
return Math.hypot((Number(x) || 0) - px, (Number(y) || 0) - py);
|
|
}
|
|
|
|
function visitNearestObstaclesLocal(x, y, candidates, limit, visitor, opts = {}) {
|
|
if (!candidates || typeof visitor !== "function") return 0;
|
|
const source = candidates;
|
|
const n = Number(source.length || 0) || 0;
|
|
if (n <= 0) return 0;
|
|
const keepMax = Math.max(1, Math.min(n, Math.max(limit + 6, limit * 3)));
|
|
const kept = [];
|
|
const dists = [];
|
|
const include = opts.include || null;
|
|
const exclude = opts.exclude || null;
|
|
const stamp = opts.stamp;
|
|
|
|
// Hot path: candidate counts are usually small. Keep a bounded sorted
|
|
// prefix without allocating intermediate Array.from()/sort() output. For
|
|
// large candidate sets, insertion is limited to the top keepMax entries.
|
|
for (let i = 0; i < n; i++) {
|
|
const it = source[i];
|
|
if (!it || it === exclude || it.dead || it._obstacleRectQueryStamp === stamp) continue;
|
|
if (include && !include(it)) continue;
|
|
const d = obstacleQueryDistanceLocal(x, y, it);
|
|
if (kept.length >= keepMax && d >= dists[dists.length - 1]) continue;
|
|
let pos = kept.length;
|
|
while (pos > 0 && d < dists[pos - 1]) pos -= 1;
|
|
if (pos >= keepMax) continue;
|
|
kept.splice(pos, 0, it);
|
|
dists.splice(pos, 0, d);
|
|
if (kept.length > keepMax) {
|
|
kept.length = keepMax;
|
|
dists.length = keepMax;
|
|
}
|
|
}
|
|
|
|
let visited = 0;
|
|
for (let i = 0; i < kept.length; i++) {
|
|
visited += 1;
|
|
if (visitor(kept[i]) === true) break;
|
|
}
|
|
return visited;
|
|
}
|
|
|
|
|
|
|
|
function sanitizeRotatorSegmentsLocal(item) {
|
|
const raw = global.TarinaiPhysicsBodySystem?.segments?.(item) || [];
|
|
const out = [];
|
|
const clampCoord = (v) => Math.max(-420, Math.min(420, Number(v) || 0));
|
|
for (const seg of raw) {
|
|
if (!Array.isArray(seg) || seg.length < 4) continue;
|
|
const x1 = clampCoord(seg[0]);
|
|
const y1 = clampCoord(seg[1]);
|
|
const x2 = clampCoord(seg[2]);
|
|
const y2 = clampCoord(seg[3]);
|
|
if (Math.hypot(x2 - x1, y2 - y1) < 4) continue;
|
|
out.push([x1, y1, x2, y2]);
|
|
if (out.length >= 96) break;
|
|
}
|
|
if (!out.length) out.push([-78, 0, 78, 0], [0, -52, 0, 52]);
|
|
return out;
|
|
}
|
|
|
|
function rotatorExtentLocal(item) {
|
|
let maxD = 48;
|
|
for (const seg of sanitizeRotatorSegmentsLocal(item)) {
|
|
maxD = Math.max(maxD, Math.hypot(seg[0], seg[1]), Math.hypot(seg[2], seg[3]));
|
|
}
|
|
return Math.min(460, maxD + Math.max(8, global.TarinaiPhysicsBodySystem?.scalar?.(item, "thickness", 12) || 12) + 8);
|
|
}
|
|
|
|
function rotatorWorldSegmentsLocal(item) {
|
|
const angle = angleForItemLocal(item);
|
|
const c = Math.cos(angle);
|
|
const ss = Math.sin(angle);
|
|
const cx = Number(item?.x) || 0;
|
|
const cy = Number(item?.y) || 0;
|
|
return sanitizeRotatorSegmentsLocal(item).map(([x1, y1, x2, y2]) => [
|
|
cx + x1 * c - y1 * ss,
|
|
cy + x1 * ss + y1 * c,
|
|
cx + x2 * c - y2 * ss,
|
|
cy + x2 * ss + y2 * c,
|
|
]);
|
|
}
|
|
|
|
function rotatorSegmentRectLocal(item, x1, y1, x2, y2) {
|
|
const len = Math.max(4, Math.hypot(x2 - x1, y2 - y1));
|
|
const angle = Math.atan2(y2 - y1, x2 - x1);
|
|
const halfW = len / 2;
|
|
const halfH = Math.max(4, Math.min(34, global.TarinaiPhysicsBodySystem?.scalar?.(item, "thickness", 12) || 12)) / 2;
|
|
const cx = (x1 + x2) / 2;
|
|
const cy = (y1 + y2) / 2;
|
|
const aabb = orientedRectAabb(cx, cy, halfW, halfH, angle);
|
|
return {
|
|
left: aabb.left, right: aabb.right, top: aabb.top, bottom: aabb.bottom,
|
|
cx, cy, halfW, halfH, angle, cos: aabb.cos, sin: aabb.sin, oriented: true,
|
|
type: "rotator", item, rotator: true, restitution: 0.72,
|
|
angularVelocity: (global.TarinaiPhysicsBodySystem?.scalar?.(item, "motorOn", true) === false ? 0 : (Number(global.TarinaiPhysicsBodySystem?.scalar?.(item, "motorSpeed", 0) || 0) || 0)) + (Number(global.TarinaiPhysicsBodySystem?.scalar?.(item, "spin", 0) || 0) || 0),
|
|
centerX: Number(item?.x) || 0, centerY: Number(item?.y) || 0,
|
|
};
|
|
}
|
|
|
|
function rotatorObstacleRectsLocal(item) {
|
|
if (!item || item.dead || item.type !== "rotator") return [];
|
|
const rects = [];
|
|
for (const seg of rotatorWorldSegmentsLocal(item)) rects.push(rotatorSegmentRectLocal(item, seg[0], seg[1], seg[2], seg[3]));
|
|
return rects;
|
|
}
|
|
|
|
function reciprocatorAxisLocal(item) {
|
|
const fallback = angleForItemLocal(item);
|
|
const angle = typeof normalizedItemAngle === "function" ? normalizedItemAngle(global.TarinaiPhysicsBodySystem?.scalar?.(item, "railAxis", fallback), fallback) : (global.TarinaiPhysicsBodySystem?.scalar?.(item, "railAxis", fallback) || fallback);
|
|
return { x: Math.cos(angle), y: Math.sin(angle), angle };
|
|
}
|
|
|
|
function reciprocatorSegmentRectLocal(item, x1, y1, x2, y2) {
|
|
const len = Math.max(4, Math.hypot(x2 - x1, y2 - y1));
|
|
const segAngle = Math.atan2(y2 - y1, x2 - x1);
|
|
const halfW = len / 2;
|
|
const halfH = Math.max(4, Math.min(34, global.TarinaiPhysicsBodySystem?.scalar?.(item, "thickness", 12) || 12)) / 2;
|
|
const cx = (x1 + x2) / 2;
|
|
const cy = (y1 + y2) / 2;
|
|
const aabb = orientedRectAabb(cx, cy, halfW, halfH, segAngle);
|
|
const axis = reciprocatorAxisLocal(item);
|
|
const itemX = Number(item?.x || 0) || 0;
|
|
const itemY = Number(item?.y || 0) || 0;
|
|
const railDirection = Math.sign(Number(global.TarinaiPhysicsBodySystem?.scalar?.(item, "railDir", 1) || 1) || 1) || 1;
|
|
const motorVelocity = global.TarinaiPhysicsBodySystem?.scalar?.(item, "railOn", true) === false ? 0 : railDirection * Math.max(0, Number(global.TarinaiPhysicsBodySystem?.scalar?.(item, "railMotorSpeed", 0) || 0) || 0);
|
|
const totalVelocity = motorVelocity + (Number(global.TarinaiPhysicsBodySystem?.scalar?.(item, "slideSpeed", 0) || 0) || 0);
|
|
const vx = axis.x * totalVelocity;
|
|
const vy = axis.y * totalVelocity;
|
|
return {
|
|
left: aabb.left, right: aabb.right, top: aabb.top, bottom: aabb.bottom,
|
|
cx, cy, halfW, halfH, angle: segAngle, cos: aabb.cos, sin: aabb.sin, oriented: true,
|
|
type: "reciprocator", item, reciprocator: true, restitution: 0.70,
|
|
motionVelocityX: vx, motionVelocityY: vy, axisX: axis.x, axisY: axis.y,
|
|
};
|
|
}
|
|
|
|
function reciprocatorObstacleRectsLocal(item) {
|
|
if (!item || item.dead || item.type !== "reciprocator") return [];
|
|
const rects = [];
|
|
for (const seg of rotatorWorldSegmentsLocal(item)) rects.push(reciprocatorSegmentRectLocal(item, seg[0], seg[1], seg[2], seg[3]));
|
|
return rects;
|
|
}
|
|
|
|
function rectLocalPoint(r, x, y) {
|
|
const shared = global.TarinaiCollisionFootprints?.rectLocalPoint;
|
|
if (typeof shared === "function") return shared(r, x, y);
|
|
const dx = x - (r.cx ?? 0);
|
|
const dy = y - (r.cy ?? 0);
|
|
const c = Number.isFinite(r.cos) ? r.cos : Math.cos(r.angle || 0);
|
|
const s = Number.isFinite(r.sin) ? r.sin : Math.sin(r.angle || 0);
|
|
return { x: dx * c + dy * s, y: -dx * s + dy * c };
|
|
}
|
|
|
|
function rectWorldNormal(r, nx, ny) {
|
|
const c = Number.isFinite(r.cos) ? r.cos : Math.cos(r.angle || 0);
|
|
const s = Number.isFinite(r.sin) ? r.sin : Math.sin(r.angle || 0);
|
|
return { x: nx * c - ny * s, y: nx * s + ny * c };
|
|
}
|
|
|
|
|
|
|
|
function segmentAabbHitLocal(x1, y1, x2, y2, left, top, right, bottom) {
|
|
let t0 = 0;
|
|
let t1 = 1;
|
|
const dx = x2 - x1;
|
|
const dy = y2 - y1;
|
|
const clip = (p, q) => {
|
|
if (Math.abs(p) < 1e-9) return q >= 0;
|
|
const t = q / p;
|
|
if (p < 0) { if (t > t1) return false; if (t > t0) t0 = t; }
|
|
else { if (t < t0) return false; if (t < t1) t1 = t; }
|
|
return true;
|
|
};
|
|
if (!clip(-dx, x1 - left)) return null;
|
|
if (!clip(dx, right - x1)) return null;
|
|
if (!clip(-dy, y1 - top)) return null;
|
|
if (!clip(dy, bottom - y1)) return null;
|
|
if (t1 < 0 || t0 > 1) return null;
|
|
const t = clamp(t0, 0, 1);
|
|
const hx = x1 + dx * t;
|
|
const hy = y1 + dy * t;
|
|
const dl = Math.abs(hx - left);
|
|
const dr = Math.abs(right - hx);
|
|
const dt = Math.abs(hy - top);
|
|
const db = Math.abs(bottom - hy);
|
|
const m = Math.min(dl, dr, dt, db);
|
|
let nx = 0, ny = 0;
|
|
if (m === dl) nx = -1;
|
|
else if (m === dr) nx = 1;
|
|
else if (m === dt) ny = -1;
|
|
else ny = 1;
|
|
return { t, nx, ny };
|
|
}
|
|
|
|
function sweptCircleRectHitLocal(x1, y1, x2, y2, radius, r, padding = 0) {
|
|
if (!r) return null;
|
|
const rr = Math.max(0, Number(radius || 0) || 0) + Math.max(0, Number(padding || 0) || 0);
|
|
if (r.oriented) {
|
|
const a = rectLocalPoint(r, x1, y1);
|
|
const b = rectLocalPoint(r, x2, y2);
|
|
const hit = segmentAabbHitLocal(a.x, a.y, b.x, b.y, -(r.halfW || 0) - rr, -(r.halfH || 0) - rr, (r.halfW || 0) + rr, (r.halfH || 0) + rr);
|
|
if (!hit) return null;
|
|
const n = rectWorldNormal(r, hit.nx, hit.ny);
|
|
return { t: hit.t, nx: n.x, ny: n.y, rect: r };
|
|
}
|
|
const hit = segmentAabbHitLocal(x1, y1, x2, y2, (r.left || 0) - rr, (r.top || 0) - rr, (r.right || 0) + rr, (r.bottom || 0) + rr);
|
|
return hit ? { t: hit.t, nx: hit.nx, ny: hit.ny, rect: r } : null;
|
|
}
|
|
|
|
function firstSweptCircleObstacleHitLocal(worldRef, t, x1, y1, x2, y2, radius, opts = {}) {
|
|
if (!worldRef?.nearbySolidObstacleRects) return null;
|
|
const midX = (x1 + x2) * 0.5;
|
|
const midY = (y1 + y2) * 0.5;
|
|
const travel = Math.hypot(x2 - x1, y2 - y1);
|
|
const searchRadius = Math.max(Number(opts.searchRadius || 0) || 0, radius + travel * 0.5 + 180);
|
|
const maxChecks = Math.max(4, Number(opts.maxChecks || 0) || 48);
|
|
let best = null;
|
|
for (const rect of worldRef.nearbySolidObstacleRects(midX, midY, searchRadius, { maxChecks })) {
|
|
if (rect?.type === "nest_box" && worldRef.shouldIgnoreNestBoxCollisionFor?.(t, rect.item)) continue;
|
|
const hit = sweptCircleRectHitLocal(x1, y1, x2, y2, radius, rect, 0.5);
|
|
if (!hit) continue;
|
|
if (!best || hit.t < best.t) best = hit;
|
|
}
|
|
return best;
|
|
}
|
|
|
|
|
|
function segmentIntersectsOrientedRectLocal(x1, y1, x2, y2, r, padding = 0) {
|
|
const a = rectLocalPoint(r, x1, y1);
|
|
const b = rectLocalPoint(r, x2, y2);
|
|
const minX = -(r.halfW || 0) - padding;
|
|
const maxX = (r.halfW || 0) + padding;
|
|
const minY = -(r.halfH || 0) - padding;
|
|
const maxY = (r.halfH || 0) + padding;
|
|
let t0 = 0, t1 = 1;
|
|
const clip = (p, q) => {
|
|
if (Math.abs(p) < 1e-9) return q >= 0;
|
|
const t = q / p;
|
|
if (p < 0) { if (t > t1) return false; if (t > t0) t0 = t; }
|
|
else { if (t < t0) return false; if (t < t1) t1 = t; }
|
|
return true;
|
|
};
|
|
const dx = b.x - a.x;
|
|
const dy = b.y - a.y;
|
|
return clip(-dx, a.x - minX) && clip(dx, maxX - a.x) && clip(-dy, a.y - minY) && clip(dy, maxY - a.y);
|
|
}
|
|
|
|
Object.defineProperties(World.prototype, Object.getOwnPropertyDescriptors({
|
|
nearest(entity, types, maxDist = Infinity) {
|
|
let best = null, bestD = maxDist;
|
|
const candidates = Number.isFinite(maxDist) ? this.nearbyItems(entity.x, entity.y, maxDist) : this.items;
|
|
for (const it of candidates) {
|
|
if (it.dead || !types.includes(it.type)) continue;
|
|
if (entity?.shouldAvoidTarget && entity.shouldAvoidTarget(it)) continue;
|
|
const d = dist(entity, it);
|
|
if (d < bestD) { best = it; bestD = d; }
|
|
}
|
|
return best;
|
|
},
|
|
|
|
nearestOther(entity, maxDist = Infinity, predicate = null) {
|
|
let best = null, bestD = maxDist;
|
|
const candidates = Number.isFinite(maxDist) ? this.nearbyTarinai(entity.x, entity.y, maxDist) : this.tarinai;
|
|
for (const o of candidates) {
|
|
if (o === entity || o.dead || this.isTarinaiHiddenInNestBox(o)) continue;
|
|
if (predicate && !predicate(o)) continue;
|
|
if (entity?.shouldAvoidTarget && entity.shouldAvoidTarget(o)) continue;
|
|
const d = dist(entity, o);
|
|
if (d < bestD) { best = o; bestD = d; }
|
|
}
|
|
return best;
|
|
},
|
|
|
|
temperatureAt(x, y) {
|
|
let temp = 0.54 + Math.sin(this.time / 55) * 0.05;
|
|
for (const it of this.nearbyItems(x, y, 190)) {
|
|
if (it.type === "stone") {
|
|
const d = distXY(x, y, it.x, it.y);
|
|
temp += clamp(1 - d / 190, 0, 1) * 0.35;
|
|
}
|
|
}
|
|
return clamp(temp, 0, 1);
|
|
},
|
|
|
|
fenceRect(it) {
|
|
const type = it?.type || "";
|
|
if (!this.isFenceType(type)) return null;
|
|
const bounce = type === "bounce_fence" || type === "bounce_fence_v";
|
|
const gate = type === "gate_fence";
|
|
const len = Math.max(112, (it.r || 42) * 3.55);
|
|
const thick = Math.max(10, (it.r || 42) * 0.31);
|
|
const angle = angleForItemLocal(it);
|
|
const halfW = len / 2;
|
|
const halfH = thick / 2;
|
|
const aabb = orientedRectAabb(it.x || 0, it.y || 0, halfW, halfH, angle);
|
|
return {
|
|
left: aabb.left, right: aabb.right, top: aabb.top, bottom: aabb.bottom,
|
|
cx: it.x || 0, cy: it.y || 0, halfW, halfH, angle, cos: aabb.cos, sin: aabb.sin, oriented: true,
|
|
vertical: Math.abs(Math.sin(angle)) > Math.abs(Math.cos(angle)), horizontal: Math.abs(Math.cos(angle)) >= Math.abs(Math.sin(angle)),
|
|
bounce, gate, gateOpen: Boolean(it.gateOpen),
|
|
restitution: bounce ? 1.08 : undefined, minBounceSpeed: bounce ? 260 : undefined
|
|
};
|
|
},
|
|
|
|
|
|
nestBoxCapacity() {
|
|
return 5;
|
|
},
|
|
|
|
nestBoxBaseRect(it) {
|
|
if (!it || it.dead || it.type !== "nest_box") return null;
|
|
const r = it.r || 42;
|
|
return {
|
|
left: it.x - r * 1.28,
|
|
right: it.x + r * 1.28,
|
|
top: it.y - r * 0.80,
|
|
bottom: it.y + r * 0.72,
|
|
type: "nest_box",
|
|
item: it,
|
|
};
|
|
},
|
|
|
|
nestBoxSolidRects(it) {
|
|
const base = this.nestBoxBaseRect(it);
|
|
if (!base) return [];
|
|
const w = base.right - base.left;
|
|
const h = base.bottom - base.top;
|
|
const colW = w / 3;
|
|
const rowH = h / 3;
|
|
const mk = (left, right, top, bottom, cell) => ({ left, right, top, bottom, type: "nest_box", cell, item: it });
|
|
return [
|
|
mk(base.left, base.right, base.top, base.top + rowH, "top"),
|
|
mk(base.left, base.left + colW, base.top + rowH, base.top + rowH * 2, "middle-left"),
|
|
mk(base.right - colW, base.right, base.top + rowH, base.top + rowH * 2, "middle-right"),
|
|
];
|
|
},
|
|
nestBoxEntryPoint(box) {
|
|
const base = this.nestBoxBaseRect(box);
|
|
if (!base) return { x: box?.x || 0, y: box?.y || 0 };
|
|
const r = box.r || 42;
|
|
// Move the approach point to the center of the open middle cell. The
|
|
// collision resolver also ignores this nest box for active sleepers near
|
|
// the door, so tarinai can cross the threshold instead of sliding off posts.
|
|
return {
|
|
x: clamp(box.x, CONFIG.worldPadding, this.w - CONFIG.worldPadding),
|
|
y: clamp(box.y + r * 0.02, base.top + r * 0.46, base.bottom - r * 0.18),
|
|
};
|
|
},
|
|
|
|
nestBoxExitPoint(box, occupant = null) {
|
|
const base = this.nestBoxBaseRect(box);
|
|
if (!base) return this.nestBoxEntryPoint(box);
|
|
const r = box.r || 42;
|
|
const side = occupant ? (stableUnit(occupant.id || "nest", `nest-exit-${box.id || "box"}`) - 0.5) * r * 0.36 : 0;
|
|
return {
|
|
x: clamp(box.x + side, CONFIG.worldPadding, this.w - CONFIG.worldPadding),
|
|
y: clamp(base.bottom + r * 0.22, CONFIG.worldPadding, this.h - CONFIG.worldPadding),
|
|
};
|
|
},
|
|
|
|
nestBoxInnerPoint(box, occupant = null) {
|
|
const base = this.nestBoxBaseRect(box);
|
|
if (!base) return { x: box?.x || 0, y: box?.y || 0 };
|
|
const occupants = this.nestBoxOccupants ? this.nestBoxOccupants(box, Infinity) : [];
|
|
let index = Number.isInteger(occupant?.nestBoxSlotIndex) && occupant.nestBoxSlotIndex >= 0 ? occupant.nestBoxSlotIndex : occupants.indexOf(occupant);
|
|
if (index < 0) index = Math.min(occupants.length, this.nestBoxCapacity(box) - 1);
|
|
index = clamp(index, 0, Math.max(0, this.nestBoxCapacity(box) - 1));
|
|
const r = box.r || 42;
|
|
const slots = [
|
|
[0.00, -0.02], [-0.22, 0.04], [0.22, 0.04], [-0.11, 0.16], [0.11, 0.16],
|
|
];
|
|
const slot = slots[index % slots.length];
|
|
return {
|
|
x: clamp(box.x + slot[0] * r, base.left + r * 0.46, base.right - r * 0.46),
|
|
y: clamp(box.y + slot[1] * r, base.top + r * 0.34, base.bottom - r * 0.18),
|
|
};
|
|
},
|
|
|
|
|
|
mechanicalSegments(it) {
|
|
return global.TarinaiMechanicalSystem?.sanitizeSegments?.(it) || sanitizeRotatorSegmentsLocal(it);
|
|
},
|
|
|
|
rotatorWorldSegments(it) {
|
|
return global.TarinaiMechanicalShapeSystem?.worldSegments?.(it) || rotatorWorldSegmentsLocal(it);
|
|
},
|
|
|
|
rotatorExtent(it) {
|
|
return global.TarinaiMechanicalShapeSystem?.extent?.(it) || rotatorExtentLocal(it);
|
|
},
|
|
|
|
rotatorObstacleRects(it) {
|
|
if (it?.type === "rotator") return global.TarinaiMechanicalShapeSystem?.obstacleRects?.(it) || rotatorObstacleRectsLocal(it);
|
|
return [];
|
|
},
|
|
|
|
reciprocatorObstacleRects(it) {
|
|
if (it?.type === "reciprocator") return global.TarinaiMechanicalShapeSystem?.obstacleRects?.(it) || reciprocatorObstacleRectsLocal(it);
|
|
return [];
|
|
},
|
|
|
|
rotatorBoundsAabb(it) {
|
|
const bounds = global.TarinaiMechanicalShapeSystem?.boundsAabb?.(it);
|
|
if (bounds) return bounds;
|
|
const rects = rotatorObstacleRectsLocal(it);
|
|
if (!rects.length) return null;
|
|
return {
|
|
left: Math.min(...rects.map(r => r.left)),
|
|
right: Math.max(...rects.map(r => r.right)),
|
|
top: Math.min(...rects.map(r => r.top)),
|
|
bottom: Math.max(...rects.map(r => r.bottom)),
|
|
type: "rotator", item: it,
|
|
};
|
|
},
|
|
|
|
solidObstacleRects(it) {
|
|
if (!it || it.dead) return [];
|
|
if (this.isFenceType(it.type)) {
|
|
if (it.type === "gate_fence" && it.gateOpen) return [];
|
|
const rect = this.fenceRect(it);
|
|
return rect ? [{ ...rect, type: it.type, item: it }] : [];
|
|
}
|
|
if (global.TarinaiMechanicalShapeSystem?.isMechanicalType?.(it.type)) return global.TarinaiMechanicalShapeSystem?.obstacleRects?.(it) || [];
|
|
if (it.type === "nest_box") return this.nestBoxSolidRects(it);
|
|
return [];
|
|
},
|
|
|
|
shouldIgnoreNestBoxCollisionFor(t, box) {
|
|
if (!t || !box || box.dead || box.type !== "nest_box") return false;
|
|
if (t.insideNestBoxId) return true;
|
|
if (t.target !== box || (t.state !== "seek_bed" && t.state !== "sleep")) return false;
|
|
const entry = this.nestBoxEntryPoint(box);
|
|
const base = this.nestBoxBaseRect(box);
|
|
const r = box.r || 42;
|
|
const entryD = distXY(t.x, t.y, entry.x, entry.y);
|
|
const centerD = distXY(t.x, t.y, box.x, box.y);
|
|
const aroundDoor = entryD <= Math.max(68, r * 1.32) || centerD <= Math.max(66, r * 1.18);
|
|
const inDoorColumn = base && t.x >= base.left + r * 0.30 && t.x <= base.right - r * 0.30 && t.y >= base.top + r * 0.12 && t.y <= base.bottom + r * 0.42;
|
|
return Boolean(aroundDoor || inDoorColumn);
|
|
},
|
|
|
|
nearbySolidObstacleRects(x, y, radius, { include = null, exclude = null, maxChecks = CONFIG.maxFenceCollisionChecks ?? 24, maxRects: requestedMaxRects = 0 } = {}) {
|
|
const limit = Math.max(1, Number(maxChecks || 0) || 24);
|
|
const maxRects = Math.max(12, Math.min(72, Number(requestedMaxRects || 0) || Math.ceil(limit * 2.25)));
|
|
const cacheable = !include && !exclude;
|
|
const stats = this.nearbyQueryStatsThisFrame || null;
|
|
// Quantize cacheable probes so smooth movement reuses obstacle rect lists
|
|
// across nearby frames while the inflated radius keeps collisions conservative.
|
|
const q = 48;
|
|
const qx = Math.floor((Number(x) || 0) / q);
|
|
const qy = Math.floor((Number(y) || 0) / q);
|
|
const qcx = (qx + 0.5) * q;
|
|
const qcy = (qy + 0.5) * q;
|
|
const inflatedRadius = Math.max(1, Number(radius || 0) || 1) + q * 0.82;
|
|
const qr = Math.ceil(inflatedRadius / q);
|
|
const cacheLimit = Math.min(72, Math.max(limit + 6, Math.ceil(limit * 1.35)));
|
|
const cacheKey = cacheable
|
|
? `${this.spatialVersion || 0}:${this.spatialDirtyMarksTotal || 0}:${qx},${qy},${qr},${cacheLimit},${maxRects}`
|
|
: "";
|
|
if (cacheable) {
|
|
if (!this._solidObstacleRectQueryCache) this._solidObstacleRectQueryCache = new Map();
|
|
const cached = this._solidObstacleRectQueryCache.get(cacheKey);
|
|
if (cached) {
|
|
if (stats) stats.obstacleRectHits = (stats.obstacleRectHits || 0) + 1;
|
|
return cached;
|
|
}
|
|
} else if (stats) {
|
|
stats.obstacleRectBypass = (stats.obstacleRectBypass || 0) + 1;
|
|
}
|
|
|
|
const queryX = cacheable ? qcx : x;
|
|
const queryY = cacheable ? qcy : y;
|
|
const queryRadius = cacheable ? qr * q : radius;
|
|
const queryLimit = cacheable ? cacheLimit : limit;
|
|
const rects = [];
|
|
const stamp = (this._obstacleRectQueryStamp = (this._obstacleRectQueryStamp || 0) + 1);
|
|
let checked = 0;
|
|
const addRectsFor = (it) => {
|
|
if (!it || it === exclude || it.dead || it._obstacleRectQueryStamp === stamp) return false;
|
|
if (include && !include(it)) return false;
|
|
const partRects = this.solidObstacleRects(it);
|
|
if (!partRects.length) return false;
|
|
it._obstacleRectQueryStamp = stamp;
|
|
checked += 1;
|
|
const localLimit = it.type === "rotator" || it.type === "poison_block" || it.type === "reciprocator" ? maxRects : maxRects + 8;
|
|
for (const rect of partRects) {
|
|
if (rects.length >= localLimit) break;
|
|
// Dense custom mechanical shapes can produce dozens of segment rects.
|
|
// Keep only rects that can plausibly interact with this query point;
|
|
// otherwise one smooth-motion collision probe spends most of its time
|
|
// iterating far-away segments of the same object.
|
|
if (rectDistanceToPointLocal(rect, queryX, queryY) > queryRadius + 42) continue;
|
|
rects.push(rect);
|
|
}
|
|
return true;
|
|
};
|
|
const candidates = this.nearbyObstacles?.(queryX, queryY, queryRadius, false) || this.nearbyItems?.(queryX, queryY, queryRadius, false) || [];
|
|
visitNearestObstaclesLocal(queryX, queryY, candidates, queryLimit, (it) => {
|
|
addRectsFor(it);
|
|
return checked >= queryLimit;
|
|
}, { include, exclude, stamp });
|
|
if (checked < queryLimit && !this.nearbyObstacles) {
|
|
const mech = global.TarinaiMechanicalSystem;
|
|
const fallback = [];
|
|
for (const type of ["rotator", "poison_block", "reciprocator"]) {
|
|
for (const it of this.itemsOfType?.(type) || []) {
|
|
if (!it || it.dead || it._obstacleRectQueryStamp === stamp) continue;
|
|
const reach = mech?.reach?.(it) || Math.max(60, it.r || 64);
|
|
if (distXY(queryX, queryY, it.x, it.y) > queryRadius + reach + 36) continue;
|
|
fallback.push(it);
|
|
}
|
|
}
|
|
visitNearestObstaclesLocal(queryX, queryY, fallback, queryLimit - checked, (it) => {
|
|
addRectsFor(it);
|
|
return checked >= queryLimit;
|
|
}, { include, exclude, stamp });
|
|
}
|
|
if (stats) {
|
|
stats.obstacleRectMisses = (stats.obstacleRectMisses || 0) + 1;
|
|
stats.obstacleRectBuilt = (stats.obstacleRectBuilt || 0) + rects.length;
|
|
}
|
|
if (cacheable) {
|
|
const cache = this._solidObstacleRectQueryCache || (this._solidObstacleRectQueryCache = new Map());
|
|
if (cache.size > 160) cache.clear();
|
|
cache.set(cacheKey, rects);
|
|
}
|
|
return rects;
|
|
},
|
|
|
|
pointInRect(x, y, r, padding = 0) {
|
|
return global.TarinaiCollisionFootprints?.pointInRect?.(x, y, r, padding) ?? false;
|
|
},
|
|
|
|
|
|
nearbyPoisonBlockHazardRects(x, y, radius, { maxChecks = 24 } = {}) {
|
|
if ((this.itemCounts?.poison_block || 0) <= 0) return [];
|
|
const rects = [];
|
|
const stamp = (this._poisonHazardQueryStamp = (this._poisonHazardQueryStamp || 0) + 1);
|
|
let checked = 0;
|
|
const addRectsFor = (it) => {
|
|
if (!it || it.dead || it.type !== "poison_block" || it._poisonHazardQueryStamp === stamp) return false;
|
|
const reach = global.TarinaiMechanicalShapeSystem?.reach?.(it) || Math.max(60, it.r || 64);
|
|
if (distXY(x, y, it.x, it.y) > radius + reach + 36) return false;
|
|
const partRects = global.TarinaiMechanicalSystem?.poisonHazardRects?.(it) || [];
|
|
if (!partRects.length) return false;
|
|
it._poisonHazardQueryStamp = stamp;
|
|
checked += 1;
|
|
for (const rect of partRects) rects.push(rect);
|
|
return true;
|
|
};
|
|
const candidates = this.nearbyHazards?.(x, y, radius + 48, false) || [];
|
|
for (const it of candidates) {
|
|
addRectsFor(it);
|
|
if (checked >= maxChecks) break;
|
|
}
|
|
if (checked < maxChecks && !this.nearbyHazards) {
|
|
for (const it of this.itemsOfType?.("poison_block") || []) {
|
|
if (addRectsFor(it) && checked >= maxChecks) break;
|
|
}
|
|
}
|
|
return rects;
|
|
},
|
|
|
|
circleOverlapsObstacleRect(x, y, radius, r, padding = 0) {
|
|
if (!r) return false;
|
|
const rr = Math.max(0, Number(radius || 0) || 0) + Math.max(0, Number(padding || 0) || 0);
|
|
if (r.oriented) {
|
|
const local = rectLocalPoint(r, x, y);
|
|
const clx = clamp(local.x, -(r.halfW || 0), r.halfW || 0);
|
|
const cly = clamp(local.y, -(r.halfH || 0), r.halfH || 0);
|
|
return Math.hypot(local.x - clx, local.y - cly) < rr;
|
|
}
|
|
const cx = clamp(x, r.left, r.right);
|
|
const cy = clamp(y, r.top, r.bottom);
|
|
return Math.hypot(x - cx, y - cy) < rr;
|
|
},
|
|
|
|
applyPoisonBlockContactDamage(t, rect, rr) {
|
|
if (!t || t.dead || !rect?.poisonBlock || !rect.item || rect.item.dead) return false;
|
|
if (!this.circleOverlapsObstacleRect(t.x, t.y, rr, rect, 1.0)) return false;
|
|
const now = this.time || 0;
|
|
const key = rect.item.id || "poison";
|
|
t._poisonBlockHitAt = t._poisonBlockHitAt || Object.create(null);
|
|
const body = global.TarinaiPhysicsBodySystem?.ensureBody?.(rect.item, this, { syncFromLegacy: false });
|
|
const solid = body?.collision ? body.collision.solid !== false : true;
|
|
const cooldown = solid ? 0.30 : 0.42;
|
|
if ((t._poisonBlockHitAt[key] || -999) + cooldown > now) return false;
|
|
t._poisonBlockHitAt[key] = now;
|
|
const velocity = body?.velocity || {};
|
|
const vx = Number((velocity.x ?? rect.item.vx) || 0) || 0;
|
|
const vy = Number((velocity.y ?? rect.item.vy) || 0) || 0;
|
|
const omega = Number((velocity.angular ?? global.TarinaiPhysicsBodySystem?.scalar?.(rect.item, "spin", 0)) || 0) || 0;
|
|
const speed = Math.hypot(vx, vy) + Math.abs(omega) * Math.max(28, rect.item.r || 64) * 0.35;
|
|
const base = Math.max(1, Number((body?.hazard?.damage ?? global.TarinaiPhysicsBodySystem?.scalar?.(rect.item, "damage", 7)) || 7) || 7);
|
|
const damage = clamp(base * (solid ? 1.0 : 0.72) + speed * 0.018, 2.0, 18.0);
|
|
if (typeof this.applyImpactDamage === "function") this.applyImpactDamage(t, damage, "\u6bd2\u30d6\u30ed\u30c3\u30af", { source: rect.item, x: t.x, y: t.y });
|
|
else if (typeof t.damage === "function") t.damage(damage, "\u6bd2\u30d6\u30ed\u30c3\u30af");
|
|
// HP\u30d0\u30fc\u306f\u63cf\u753b\u5074\u3067\u300c\u30c0\u30e1\u30fc\u30b8\u4e2d\u300d\u306e\u8868\u793a\u6761\u4ef6\u3092\u898b\u308b\u305f\u3081\u3001\u6bd2\u30d6\u30ed\u30c3\u30af\u3067\u3082\u660e\u793a\u7684\u306b\u50b7\u307f\u72b6\u614b\u3092\u7acb\u3066\u308b\u3002
|
|
t.showHpBar?.(4.8);
|
|
t.hurtTimer = Math.max(t.hurtTimer || 0, 0.82);
|
|
t.pokeFlashTimer = Math.max(t.pokeFlashTimer || 0, 0.28);
|
|
t.surpriseTimer = Math.max(t.surpriseTimer || 0, 0.24);
|
|
if (typeof t.bubble === "function" && now >= (t.lastPoisonBlockBubbleAt || -999) + 0.85) {
|
|
t.lastPoisonBlockBubbleAt = now;
|
|
t.bubble("!", 0.48, "rgba(94,166,76,0.78)");
|
|
}
|
|
// 物理ブロック衝突の視覚エフェクトは出さない。接触時の状態変化はHPバーと吹き出しだけに限定する。
|
|
return true;
|
|
},
|
|
|
|
resolvePoisonBlockContactDamage(t, rr) {
|
|
if (!t || t.dead || this.isTarinaiHiddenInNestBox(t)) return false;
|
|
if ((this.itemCounts?.poison_block || 0) <= 0) return false;
|
|
const now = this.time || 0;
|
|
const interval = Math.max(0.045, Math.min(0.10, Number(CONFIG?.poisonBlockProbeInterval || 0.075) || 0.075));
|
|
if ((t._nextPoisonBlockProbeAt || 0) > now) return false;
|
|
t._nextPoisonBlockProbeAt = now + interval;
|
|
let hit = false;
|
|
for (const rect of this.nearbyPoisonBlockHazardRects(t.x, t.y, rr + 150, { maxChecks: 32 })) {
|
|
hit = this.applyPoisonBlockContactDamage(t, rect, rr) || hit;
|
|
}
|
|
return hit;
|
|
},
|
|
|
|
|
|
resolvePoisonBlockItemContacts(opts = {}) {
|
|
if ((this.itemCounts?.poison_block || 0) <= 0) return 0;
|
|
const meltTypes = new Set(["grass_bed", "zunchi", "water", "grass"]);
|
|
let possible = 0;
|
|
for (const type of meltTypes) possible += this.itemCounts?.[type] || 0;
|
|
if (possible <= 0) return 0;
|
|
const maxPoison = Math.max(1, Math.min(48, Number(opts.maxPoison || 24) || 24));
|
|
const maxTargets = Math.max(1, Math.min(160, Number(opts.maxTargets || 80) || 80));
|
|
const poisonBlocks = this.itemsOfType?.("poison_block") || [];
|
|
let removed = 0;
|
|
let checkedPoison = 0;
|
|
const lifecycle = global.TarinaiStructureLifecycle;
|
|
for (const poison of poisonBlocks) {
|
|
if (!poison || poison.dead || poison.type !== "poison_block") continue;
|
|
checkedPoison += 1;
|
|
if (checkedPoison > maxPoison) break;
|
|
const rects = global.TarinaiMechanicalSystem?.poisonHazardRects?.(poison) || [];
|
|
if (!rects.length) continue;
|
|
const reach = global.TarinaiMechanicalSystem?.reach?.(poison) || Math.max(72, poison.r || 64);
|
|
const source = this.nearbyItems?.(poison.x, poison.y, reach + 96, true) || this.items || [];
|
|
let checkedTargets = 0;
|
|
for (const target of source) {
|
|
if (!target || target.dead || target === poison || !meltTypes.has(target.type)) continue;
|
|
checkedTargets += 1;
|
|
if (checkedTargets > maxTargets) break;
|
|
const rr = Math.max(5, Number(target.r || target.radius || itemRadiusFor?.(target.type, 10) || 10) || 10) * (target.type === "grass_bed" ? 1.15 : 0.92);
|
|
let touching = false;
|
|
for (const rect of rects) {
|
|
if (this.circleOverlapsObstacleRect(target.x, target.y, rr, rect, 1.5)) { touching = true; break; }
|
|
}
|
|
if (!touching) continue;
|
|
const wasLive = !target.dead;
|
|
const destroyed = (globalThis.TarinaiItemDestruction || lifecycle)?.deleteItem?.(this, target, {
|
|
reason: "poison-block-contact",
|
|
userReason: "毒ブロックに触れて消えた",
|
|
wake: true,
|
|
breaker: poison,
|
|
panicOwner: false,
|
|
});
|
|
if (wasLive && destroyed) {
|
|
removed += 1;
|
|
this.markTerrainDirtyAt?.(target.x, target.y, Math.max(36, rr + 16), "poison-block-contact");
|
|
}
|
|
}
|
|
}
|
|
if (removed > 0) {
|
|
this.compactItems?.();
|
|
this.updateItemCounts?.("poison-block-contact");
|
|
this.markSpatialDirty?.("poison-block-contact");
|
|
this.drawListDirty = true;
|
|
}
|
|
return removed;
|
|
},
|
|
|
|
pushTarinaiOutOfRect(t, r, rr, opts = {}) {
|
|
if (!t || !r) return false;
|
|
const preVx = Number(t.vx || 0) || 0;
|
|
const preVy = Number(t.vy || 0) || 0;
|
|
let cx, cy, dx, dy, d, nx, ny;
|
|
let insidePenetration = 0;
|
|
if (r.oriented) {
|
|
const local = rectLocalPoint(r, t.x, t.y);
|
|
const clx = clamp(local.x, -(r.halfW || 0), r.halfW || 0);
|
|
const cly = clamp(local.y, -(r.halfH || 0), r.halfH || 0);
|
|
dx = local.x - clx;
|
|
dy = local.y - cly;
|
|
d = Math.hypot(dx, dy);
|
|
if (d >= rr) return false;
|
|
if (d < 0.001) {
|
|
const left = Math.abs(local.x + (r.halfW || 0));
|
|
const right = Math.abs((r.halfW || 0) - local.x);
|
|
const top = Math.abs(local.y + (r.halfH || 0));
|
|
const bottom = Math.abs((r.halfH || 0) - local.y);
|
|
const m = Math.min(left, right, top, bottom);
|
|
insidePenetration = m;
|
|
if (m === left) { dx = -1; dy = 0; d = 1; }
|
|
else if (m === right) { dx = 1; dy = 0; d = 1; }
|
|
else if (m === top) { dx = 0; dy = -1; d = 1; }
|
|
else { dx = 0; dy = 1; d = 1; }
|
|
}
|
|
const n = rectWorldNormal(r, dx / d, dy / d);
|
|
nx = n.x; ny = n.y;
|
|
cx = t.x - dx; cy = t.y - dy;
|
|
} else {
|
|
cx = clamp(t.x, r.left, r.right);
|
|
cy = clamp(t.y, r.top, r.bottom);
|
|
dx = t.x - cx;
|
|
dy = t.y - cy;
|
|
d = Math.hypot(dx, dy);
|
|
if (d >= rr) return false;
|
|
if (d < 0.001) {
|
|
const left = Math.abs(t.x - r.left);
|
|
const right = Math.abs(r.right - t.x);
|
|
const top = Math.abs(t.y - r.top);
|
|
const bottom = Math.abs(r.bottom - t.y);
|
|
const m = Math.min(left, right, top, bottom);
|
|
if (m === left) { dx = -1; dy = 0; d = 1; }
|
|
else if (m === right) { dx = 1; dy = 0; d = 1; }
|
|
else if (m === top) { dx = 0; dy = -1; d = 1; }
|
|
else { dx = 0; dy = 1; d = 1; }
|
|
}
|
|
nx = dx / d;
|
|
ny = dy / d;
|
|
}
|
|
const slop = Number.isFinite(opts.slop) ? opts.slop : 0.8;
|
|
const defaultMaxPush = r.oriented && insidePenetration > 0 ? Math.max(18, rr * 2.8) : Math.max(10, rr * 0.92);
|
|
const maxPush = Math.max(1, Number.isFinite(opts.maxPush) ? opts.maxPush : defaultMaxPush);
|
|
const push = r.oriented && insidePenetration > 0
|
|
? Math.min(insidePenetration + rr + slop, maxPush)
|
|
: Math.min(rr - d + slop, maxPush);
|
|
t.x += nx * push;
|
|
t.y += ny * push;
|
|
t.lastSolidObstacleCollisionSource = r.item || null;
|
|
t.lastSolidObstacleCollisionAt = this.time || 0;
|
|
if (r.bounce) {
|
|
const restitution = Number.isFinite(r.restitution) ? r.restitution : 1.08;
|
|
const vx = t.vx || 0;
|
|
const vy = t.vy || 0;
|
|
const toward = vx * nx + vy * ny;
|
|
if (toward < 0) {
|
|
t.vx = vx - (1 + restitution) * toward * nx;
|
|
t.vy = vy - (1 + restitution) * toward * ny;
|
|
} else {
|
|
const kick = Math.max(46, Math.min(120, rr * 3.2));
|
|
t.vx = vx + nx * kick;
|
|
t.vy = vy + ny * kick;
|
|
}
|
|
const minBounceSpeed = Math.max(0, Number(r.minBounceSpeed || 0) || 0);
|
|
if (minBounceSpeed > 0) {
|
|
const currentOut = (t.vx || 0) * nx + (t.vy || 0) * ny + (t.impulseVx || 0) * nx + (t.impulseVy || 0) * ny;
|
|
const now = this.time || 0;
|
|
if (currentOut < minBounceSpeed && ((t.lastBounceFenceImpulseAt || -999) + 0.16 <= now || toward < 0)) {
|
|
const add = minBounceSpeed - currentOut;
|
|
t.impulseVx = clamp((t.impulseVx || 0) + nx * add, -520, 520);
|
|
t.impulseVy = clamp((t.impulseVy || 0) + ny * add, -520, 520);
|
|
t.lastBounceFenceImpulseAt = now;
|
|
}
|
|
}
|
|
t.vx = clamp(t.vx || 0, -520, 520);
|
|
t.vy = clamp(t.vy || 0, -520, 520);
|
|
t.surpriseTimer = Math.max(t.surpriseTimer || 0, 0.18);
|
|
if (t.bubble && (this.time || 0) >= (t.lastBounceFenceBubbleAt || -999) + 0.55) {
|
|
t.lastBounceFenceBubbleAt = this.time || 0;
|
|
t.bubble("!", 0.45, "rgba(80,130,210,0.72)");
|
|
}
|
|
if ((this.time || 0) >= (t.lastBounceFenceEffectAt || -999) + 0.08) {
|
|
t.lastBounceFenceEffectAt = this.time || 0;
|
|
this.effects?.push(new Effect("ring", t.x, t.y, { size: Math.max(18, rr * 1.15), life: 0.18, color: "rgba(82,153,230,0.46)" }));
|
|
}
|
|
} else if (r.mechanical) {
|
|
global.TarinaiMechanicalSystem?.applySurfaceVelocityToCircle?.(t, r, nx, ny, preVx, preVy, {
|
|
damping: r.rotator ? 0.78 : (r.poisonBlock ? 0.74 : 0.80),
|
|
surfaceScale: r.rotator ? 0.52 : (r.poisonBlock ? 0.38 : 0.64),
|
|
normalBoost: r.rotator ? 42 : (r.poisonBlock ? 30 : 36),
|
|
maxSpeed: r.rotator ? 680 : (r.poisonBlock ? 560 : 620),
|
|
impulseVScale: r.rotator ? 0.10 : (r.poisonBlock ? 0.05 : 0.08),
|
|
impulseMax: r.rotator ? 360 : (r.poisonBlock ? 260 : 320),
|
|
passiveImpulseScale: 0.82,
|
|
spinKick: 0,
|
|
});
|
|
t.surpriseTimer = Math.max(t.surpriseTimer || 0, r.rotator ? 0.12 : 0.10);
|
|
} else if (Math.abs(dx) > Math.abs(dy)) t.vx *= -0.18;
|
|
else t.vy *= -0.18;
|
|
return true;
|
|
},
|
|
|
|
moveTarinaiWithCollision(t, dx, dy, opts = {}) {
|
|
if (!t || t.dead) return false;
|
|
const moveX = Number(dx || 0) || 0;
|
|
const moveY = Number(dy || 0) || 0;
|
|
const dist = Math.hypot(moveX, moveY);
|
|
if (dist <= 0.0001) return this.resolveSolidObstacleCollision(t, { maxPasses: 2, reason: opts.reason || "tarinai-stationary" });
|
|
if (this.isTarinaiHiddenInNestBox?.(t)) {
|
|
t.x += moveX;
|
|
t.y += moveY;
|
|
return false;
|
|
}
|
|
const rr = Math.max(8, (Number(t.radius) || 22) * 0.74);
|
|
const maxStep = Math.max(5, Math.min(12, Number(opts.maxStep || rr * 0.48) || 8));
|
|
const steps = Math.max(1, Math.min(48, Math.ceil(dist / maxStep)));
|
|
const stepX = moveX / steps;
|
|
const stepY = moveY / steps;
|
|
let collided = false;
|
|
for (let i = 0; i < steps; i++) {
|
|
const sx = t.x;
|
|
const sy = t.y;
|
|
const ex = sx + stepX;
|
|
const ey = sy + stepY;
|
|
const hit = firstSweptCircleObstacleHitLocal(this, t, sx, sy, ex, ey, rr, {
|
|
searchRadius: Math.max(rr + 110, rr + Math.hypot(stepX, stepY) + 170),
|
|
maxChecks: Math.max(opts.light ? 10 : 32, Number(opts.maxChecks || 0) || CONFIG.maxFenceCollisionChecks || 24),
|
|
});
|
|
if (hit && hit.t <= 1) {
|
|
const safeT = Math.max(0, hit.t - 0.035);
|
|
t.x = sx + stepX * safeT;
|
|
t.y = sy + stepY * safeT;
|
|
const toward = (t.vx || 0) * hit.nx + (t.vy || 0) * hit.ny;
|
|
if (toward < 0) {
|
|
t.vx -= toward * hit.nx;
|
|
t.vy -= toward * hit.ny;
|
|
}
|
|
collided = this.resolveSolidObstacleCollision(t, {
|
|
maxPasses: 3,
|
|
searchRadius: Math.max(rr + 110, rr + Math.hypot(stepX, stepY) + 170),
|
|
maxChecks: Math.max(opts.light ? 10 : 32, Number(opts.maxChecks || 0) || CONFIG.maxFenceCollisionChecks || 24),
|
|
slop: 0.30,
|
|
reason: opts.reason || "tarinai-swept-hit",
|
|
}) || true;
|
|
continue;
|
|
}
|
|
t.x = ex;
|
|
t.y = ey;
|
|
const passHit = this.resolveSolidObstacleCollision(t, {
|
|
maxPasses: 2,
|
|
searchRadius: Math.max(rr + 96, rr + Math.hypot(stepX, stepY) + 140),
|
|
maxChecks: Math.max(opts.light ? 10 : 28, Number(opts.maxChecks || 0) || CONFIG.maxFenceCollisionChecks || 24),
|
|
slop: 0.35,
|
|
reason: opts.reason || "tarinai-swept-move",
|
|
});
|
|
collided = passHit || collided;
|
|
}
|
|
if (collided) {
|
|
t.lastSolidObstacleCollisionAt = this.time || 0;
|
|
this.markSpatialDirty?.("tarinai-moved-collision");
|
|
}
|
|
return collided;
|
|
},
|
|
|
|
resolveSolidObstacleCollision(t, opts = {}) {
|
|
if (!t || t.dead || this.isTarinaiHiddenInNestBox(t)) return false;
|
|
const rr = Math.max(8, (Number(t.radius) || 22) * 0.74);
|
|
this.resolvePoisonBlockContactDamage(t, rr);
|
|
let pushed = false;
|
|
let bounced = false;
|
|
const maxPasses = Math.max(1, Math.min(4, Number(opts.maxPasses ?? 2) || 2));
|
|
const searchRadius = Math.max(rr + 40, Number(opts.searchRadius || rr + 150) || (rr + 150));
|
|
const maxChecks = Math.max(1, Number(opts.maxChecks || CONFIG.maxFenceCollisionChecks || 24) || 24);
|
|
for (let pass = 0; pass < maxPasses; pass++) {
|
|
let passPushed = false;
|
|
for (const rect of this.nearbySolidObstacleRects(t.x, t.y, searchRadius, { maxChecks })) {
|
|
if (rect?.type === "nest_box" && this.shouldIgnoreNestBoxCollisionFor(t, rect.item)) continue;
|
|
if (this.pushTarinaiOutOfRect(t, rect, rr, opts)) {
|
|
pushed = true;
|
|
passPushed = true;
|
|
if (rect?.bounce) bounced = true;
|
|
}
|
|
}
|
|
if (!passPushed) break;
|
|
}
|
|
if (pushed) {
|
|
const pad = CONFIG.worldPadding + Math.max(2, (Number(t.radius) || 22) * 0.18);
|
|
t.x = clamp(t.x, pad, this.w - pad);
|
|
t.y = clamp(t.y, pad, this.h - pad);
|
|
const maxV = bounced ? 520 : 130;
|
|
t.vx = clamp(t.vx || 0, -maxV, maxV);
|
|
t.vy = clamp(t.vy || 0, -maxV, maxV);
|
|
}
|
|
return pushed;
|
|
},
|
|
|
|
resolveFenceCollision(t) {
|
|
return this.resolveSolidObstacleCollision(t, { maxPasses: 2, reason: "resolve-fence" });
|
|
},
|
|
|
|
segmentIntersectsRect(x1, y1, x2, y2, r) {
|
|
if (!r) return false;
|
|
if (r.oriented) return segmentIntersectsOrientedRectLocal(x1, y1, x2, y2, r, 0);
|
|
if ((x1 >= r.left && x1 <= r.right && y1 >= r.top && y1 <= r.bottom) || (x2 >= r.left && x2 <= r.right && y2 >= r.top && y2 <= r.bottom)) return true;
|
|
const intersects = (ax, ay, bx, by, cx, cy, dx, dy) => {
|
|
const ccw = (px, py, qx, qy, rx, ry) => (ry - py) * (qx - px) > (qy - py) * (rx - px);
|
|
return ccw(ax, ay, cx, cy, dx, dy) !== ccw(bx, by, cx, cy, dx, dy) && ccw(ax, ay, bx, by, cx, cy) !== ccw(ax, ay, bx, by, dx, dy);
|
|
};
|
|
return intersects(x1, y1, x2, y2, r.left, r.top, r.right, r.top)
|
|
|| intersects(x1, y1, x2, y2, r.right, r.top, r.right, r.bottom)
|
|
|| intersects(x1, y1, x2, y2, r.right, r.bottom, r.left, r.bottom)
|
|
|| intersects(x1, y1, x2, y2, r.left, r.bottom, r.left, r.top);
|
|
},
|
|
|
|
pathBlockedByFence(x1, y1, x2, y2, padding = 16) {
|
|
const cx = (x1 + x2) / 2;
|
|
const cy = (y1 + y2) / 2;
|
|
const maxD = Math.hypot(x2 - x1, y2 - y1) / 2 + 120;
|
|
for (const rect of this.nearbySolidObstacleRects(cx, cy, maxD + padding + 170)) {
|
|
if (rect?.oriented) {
|
|
const r = { ...rect, halfW: (rect.halfW || 0) + padding, halfH: (rect.halfH || 0) + padding };
|
|
if (this.segmentIntersectsRect(x1, y1, x2, y2, r)) return true;
|
|
} else {
|
|
const r = { left: rect.left - padding, right: rect.right + padding, top: rect.top - padding, bottom: rect.bottom + padding };
|
|
if (this.segmentIntersectsRect(x1, y1, x2, y2, r)) return true;
|
|
}
|
|
}
|
|
return false;
|
|
},
|
|
|
|
grassBlockedAt(x, y, self = null, opts = {}) {
|
|
const ignoreSoft = Boolean(opts?.ignoreSoftPlacement);
|
|
for (const it of this.nearbyItems(x, y, 96)) {
|
|
if (it === self || it.dead) continue;
|
|
if (ignoreSoft && global.TarinaiPhysicsSoftClear?.isSoftPlacementType?.(it.type)) continue;
|
|
for (const r of this.solidObstacleRects(it)) {
|
|
if (this.pointInRect(x, y, r, 10)) return true;
|
|
}
|
|
if (it.type !== "zunchi") continue;
|
|
const dx = (x - it.x) / Math.max(22, it.r * 1.35);
|
|
const dy = (y - it.y) / Math.max(12, it.r * 0.82);
|
|
if (dx * dx + dy * dy < 1) return true;
|
|
}
|
|
return false;
|
|
},
|
|
|
|
grassCrowdedAt(x, y, minGap = 30, self = null) {
|
|
for (const it of this.nearbyItems(x, y, Math.max(42, minGap + 16))) {
|
|
if (it === self || it.dead || it.type !== "grass") continue;
|
|
if (distXY(x, y, it.x, it.y) < minGap) return true;
|
|
}
|
|
return false;
|
|
},
|
|
|
|
grassOnTarinaiAt(x, y, minGap = 24) {
|
|
for (const t of this.nearbyTarinai(x, y, minGap + 36)) {
|
|
if (t.dead || this.isTarinaiHiddenInNestBox(t)) continue;
|
|
if (distXY(x, y, t.x, t.y) < Math.max(minGap, t.radius * 1.15)) return true;
|
|
}
|
|
return false;
|
|
},
|
|
|
|
grassSpotOpen(x, y, { minGrassGap = 30, avoidTarinai = true, self = null } = {}) {
|
|
if (x < 44 || y < 44 || x > this.w - 44 || y > this.h - 44) return false;
|
|
if (this.grassBlockedAt(x, y, self)) return false;
|
|
if (this.grassCrowdedAt(x, y, minGrassGap, self)) return false;
|
|
if (avoidTarinai && this.grassOnTarinaiAt(x, y)) return false;
|
|
return true;
|
|
},
|
|
|
|
findGrassPlantingSpot(x, y, { allowOriginal = true, minRadius = 18, maxRadius = 160, attempts = 36, avoidTarinai = true } = {}) {
|
|
const cx = clamp(x, 44, this.w - 44);
|
|
const cy = clamp(y, 44, this.h - 44);
|
|
if (allowOriginal && this.grassSpotOpen(cx, cy, { avoidTarinai })) return { x: cx, y: cy };
|
|
const blockedAtCenter = this.grassBlockedAt(cx, cy);
|
|
const inner = blockedAtCenter ? Math.max(minRadius, 34) : minRadius;
|
|
const outer = Math.max(maxRadius, inner + 42);
|
|
const golden = Math.PI * (3 - Math.sqrt(5));
|
|
for (let i = 0; i < attempts; i++) {
|
|
const t = attempts <= 1 ? 1 : i / (attempts - 1);
|
|
const radius = lerp(inner, outer, Math.sqrt(t));
|
|
const angle = i * golden + stableUnit(`${cx},${cy}`, "grass-plant") * Math.PI * 2;
|
|
const px = clamp(cx + Math.cos(angle) * radius, 44, this.w - 44);
|
|
const py = clamp(cy + Math.sin(angle) * radius * 0.72, 44, this.h - 44);
|
|
if (this.grassSpotOpen(px, py, { avoidTarinai })) return { x: px, y: py };
|
|
}
|
|
return null;
|
|
},
|
|
}));
|
|
})(typeof window !== "undefined" ? window : globalThis);
|