BlueGit

timfa questward

Files

questward / server / src / simulation / freeFlight.js

freeFlight.js

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