| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { mobDefinition } from "../../../shared/definitions/mobs.js"; |
| 3 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 4 | import { tileToChunk, tileToRegion } from "../world/coordinates.js"; |
| 5 | |
| 6 | export function ensureFlightComponent (ecs, handle, position, def) |
| 7 | { |
| 8 | let flight = ecs.get(handle, Component.flight); |
| 9 | if (flight) |
| 10 | { return flight; } |
| 11 | const x = (position.tileX ?? 0) + 0.5; |
| 12 | const y = (position.tileY ?? 0) + 0.5; |
| 13 | flight = ecs.add(handle, Component.flight, { |
| 14 | x, |
| 15 | y, |
| 16 | targetX: x, |
| 17 | targetY: y, |
| 18 | speed: def?.flightSpeedTilesPerSec ?? 2.5, |
| 19 | nextPickAt: 0, |
| 20 | lastAt: 0 |
| 21 | }); |
| 22 | return flight; |
| 23 | } |
| 24 | |
| 25 | export function stepFreeFlight (world, now = Date.now()) |
| 26 | { |
| 27 | const events = []; |
| 28 | for (const entity of world.ecs.query(Component.position, Component.creature)) |
| 29 | { |
| 30 | const def = mobDefinition(entity.creature.mobTypeId); |
| 31 | if (!def?.freeFlight) |
| 32 | { continue; } |
| 33 | const existingFlight = world.ecs.get(entity.handle, Component.flight); |
| 34 | if (existingFlight?.courierId != null) |
| 35 | { continue; } |
| 36 | const dimensionId = normalizeDimensionId(entity.position.dimensionId ?? OVERWORLD_ID); |
| 37 | if (!world.chunkIsSimulating(entity.position.tileX, entity.position.tileY, dimensionId)) |
| 38 | { continue; } |
| 39 | if (world.frozen?.has?.(entity.entityId)) |
| 40 | { continue; } |
| 41 | |
| 42 | const flight = ensureFlightComponent(world.ecs, entity.handle, entity.position, def); |
| 43 | if (!flight.lastAt) |
| 44 | { flight.lastAt = now; } |
| 45 | const dt = Math.min(0.25, Math.max(0, (now - flight.lastAt) / 1000)); |
| 46 | flight.lastAt = now; |
| 47 | |
| 48 | const arrived = Math.hypot(flight.targetX - flight.x, flight.targetY - flight.y) < 0.08; |
| 49 | if (arrived || now >= (flight.nextPickAt || 0)) |
| 50 | { |
| 51 | const next = pickFlightTarget(world, flight.x, flight.y, dimensionId); |
| 52 | if (next) |
| 53 | { |
| 54 | flight.targetX = next.x; |
| 55 | flight.targetY = next.y; |
| 56 | flight.nextPickAt = now + 1800 + Math.floor(Math.random() * 2400); |
| 57 | } |
| 58 | else |
| 59 | { flight.nextPickAt = now + 1000; } |
| 60 | } |
| 61 | |
| 62 | const dx = flight.targetX - flight.x; |
| 63 | const dy = flight.targetY - flight.y; |
| 64 | const dist = Math.hypot(dx, dy); |
| 65 | if (dist < 1e-6 || dt <= 0) |
| 66 | { continue; } |
| 67 | |
| 68 | const step = Math.min(dist, (flight.speed || 2.5) * dt); |
| 69 | flight.x += (dx / dist) * step; |
| 70 | flight.y += (dy / dist) * step; |
| 71 | |
| 72 | const fromTileX = entity.position.tileX; |
| 73 | const fromTileY = entity.position.tileY; |
| 74 | const tileX = Math.floor(flight.x); |
| 75 | const tileY = Math.floor(flight.y); |
| 76 | entity.position.tileX = tileX; |
| 77 | entity.position.tileY = tileY; |
| 78 | |
| 79 | const destRegionX = tileToRegion(tileX); |
| 80 | const destRegionY = tileToRegion(tileY); |
| 81 | if (!world.ownsRegion(destRegionX, destRegionY, dimensionId)) |
| 82 | { |
| 83 | world.frozen.add(entity.entityId); |
| 84 | events.push({ |
| 85 | type: "transfer-request", |
| 86 | entityId: entity.entityId, |
| 87 | fromTileX, |
| 88 | fromTileY, |
| 89 | destTileX: tileX, |
| 90 | destTileY: tileY, |
| 91 | toRegionX: destRegionX, |
| 92 | toRegionY: destRegionY, |
| 93 | dimensionId |
| 94 | }); |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | const lerpMs = Math.max(50, Math.round(dt * 1000)); |
| 99 | events.push({ |
| 100 | type: "entity-move", |
| 101 | entityId: entity.entityId, |
| 102 | fromTileX, |
| 103 | fromTileY, |
| 104 | tileX, |
| 105 | tileY, |
| 106 | worldX: flight.x, |
| 107 | worldY: flight.y, |
| 108 | gait: entity.movement?.gait ?? "walk", |
| 109 | lerpMs, |
| 110 | freeFlight: true |
| 111 | }); |
| 112 | } |
| 113 | return events; |
| 114 | } |
| 115 | |
| 116 | function pickFlightTarget (world, fromX, fromY, dimensionId = OVERWORLD_ID) |
| 117 | { |
| 118 | const dim = normalizeDimensionId(dimensionId); |
| 119 | for (let attempt = 0; attempt < 16; attempt++) |
| 120 | { |
| 121 | const angle = Math.random() * Math.PI * 2; |
| 122 | const dist = 2 + Math.random() * 6; |
| 123 | const x = fromX + Math.cos(angle) * dist; |
| 124 | const y = fromY + Math.sin(angle) * dist; |
| 125 | const tileX = Math.floor(x); |
| 126 | const tileY = Math.floor(y); |
| 127 | if (!world.ownsRegion(tileToRegion(tileX), tileToRegion(tileY), dim)) |
| 128 | { continue; } |
| 129 | const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY), dim); |
| 130 | if (!data?.open) |
| 131 | { continue; } |
| 132 | return { x: tileX + 0.5, y: tileY + 0.5 }; |
| 133 | } |
| 134 | return null; |
| 135 | } |