import { Component } from "../ecs/components.js"; import { mobDefinition } from "../../../shared/definitions/mobs.js"; import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; import { tileToChunk, tileToRegion } from "../world/coordinates.js"; export function ensureFlightComponent (ecs, handle, position, def) { let flight = ecs.get(handle, Component.flight); if (flight) { return flight; } const x = (position.tileX ?? 0) + 0.5; const y = (position.tileY ?? 0) + 0.5; flight = ecs.add(handle, Component.flight, { x, y, targetX: x, targetY: y, speed: def?.flightSpeedTilesPerSec ?? 2.5, nextPickAt: 0, lastAt: 0 }); return flight; } export function stepFreeFlight (world, now = Date.now()) { const events = []; for (const entity of world.ecs.query(Component.position, Component.creature)) { const def = mobDefinition(entity.creature.mobTypeId); if (!def?.freeFlight) { continue; } const existingFlight = world.ecs.get(entity.handle, Component.flight); if (existingFlight?.courierId != null) { continue; } const dimensionId = normalizeDimensionId(entity.position.dimensionId ?? OVERWORLD_ID); if (!world.chunkIsSimulating(entity.position.tileX, entity.position.tileY, dimensionId)) { continue; } if (world.frozen?.has?.(entity.entityId)) { continue; } const flight = ensureFlightComponent(world.ecs, entity.handle, entity.position, def); if (!flight.lastAt) { flight.lastAt = now; } const dt = Math.min(0.25, Math.max(0, (now - flight.lastAt) / 1000)); flight.lastAt = now; const arrived = Math.hypot(flight.targetX - flight.x, flight.targetY - flight.y) < 0.08; if (arrived || now >= (flight.nextPickAt || 0)) { const next = pickFlightTarget(world, flight.x, flight.y, dimensionId); if (next) { flight.targetX = next.x; flight.targetY = next.y; flight.nextPickAt = now + 1800 + Math.floor(Math.random() * 2400); } else { flight.nextPickAt = now + 1000; } } const dx = flight.targetX - flight.x; const dy = flight.targetY - flight.y; const dist = Math.hypot(dx, dy); if (dist < 1e-6 || dt <= 0) { continue; } const step = Math.min(dist, (flight.speed || 2.5) * dt); flight.x += (dx / dist) * step; flight.y += (dy / dist) * step; const fromTileX = entity.position.tileX; const fromTileY = entity.position.tileY; const tileX = Math.floor(flight.x); const tileY = Math.floor(flight.y); entity.position.tileX = tileX; entity.position.tileY = tileY; const destRegionX = tileToRegion(tileX); const destRegionY = tileToRegion(tileY); if (!world.ownsRegion(destRegionX, destRegionY, dimensionId)) { world.frozen.add(entity.entityId); events.push({ type: "transfer-request", entityId: entity.entityId, fromTileX, fromTileY, destTileX: tileX, destTileY: tileY, toRegionX: destRegionX, toRegionY: destRegionY, dimensionId }); continue; } const lerpMs = Math.max(50, Math.round(dt * 1000)); events.push({ type: "entity-move", entityId: entity.entityId, fromTileX, fromTileY, tileX, tileY, worldX: flight.x, worldY: flight.y, gait: entity.movement?.gait ?? "walk", lerpMs, freeFlight: true }); } return events; } function pickFlightTarget (world, fromX, fromY, dimensionId = OVERWORLD_ID) { const dim = normalizeDimensionId(dimensionId); for (let attempt = 0; attempt < 16; attempt++) { const angle = Math.random() * Math.PI * 2; const dist = 2 + Math.random() * 6; const x = fromX + Math.cos(angle) * dist; const y = fromY + Math.sin(angle) * dist; const tileX = Math.floor(x); const tileY = Math.floor(y); if (!world.ownsRegion(tileToRegion(tileX), tileToRegion(tileY), dim)) { continue; } const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY), dim); if (!data?.open) { continue; } return { x: tileX + 0.5, y: tileY + 0.5 }; } return null; }