BlueGit

timfa questward

Files

questward / server / src / simulation / freeFlight.js

freeFlight.js

freeFlight.js 3.4 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 if (!world.chunkIsSimulating(entity.position.tileX, entity.position.tileY))
33 { continue; }
34 if (world.frozen?.has?.(entity.entityId))
35 { continue; }
36
37 const flight = ensureFlightComponent(world.ecs, entity.handle, entity.position, def);
38 if (!flight.lastAt)
39 { flight.lastAt = now; }
40 const dt = Math.min(0.25, Math.max(0, (now - flight.lastAt) / 1000));
41 flight.lastAt = now;
42
43 const arrived = Math.hypot(flight.targetX - flight.x, flight.targetY - flight.y) < 0.08;
44 if (arrived || now >= (flight.nextPickAt || 0))
45 {
46 const next = pickFlightTarget(world, flight.x, flight.y);
47 if (next)
48 {
49 flight.targetX = next.x;
50 flight.targetY = next.y;
51 flight.nextPickAt = now + 1800 + Math.floor(Math.random() * 2400);
52 }
53 else
54 { flight.nextPickAt = now + 1000; }
55 }
56
57 const dx = flight.targetX - flight.x;
58 const dy = flight.targetY - flight.y;
59 const dist = Math.hypot(dx, dy);
60 if (dist < 1e-6 || dt <= 0)
61 { continue; }
62
63 const step = Math.min(dist, (flight.speed || 2.5) * dt);
64 flight.x += (dx / dist) * step;
65 flight.y += (dy / dist) * step;
66
67 const fromTileX = entity.position.tileX;
68 const fromTileY = entity.position.tileY;
69 const tileX = Math.floor(flight.x);
70 const tileY = Math.floor(flight.y);
71 entity.position.tileX = tileX;
72 entity.position.tileY = tileY;
73
74 const destRegionX = tileToRegion(tileX);
75 const destRegionY = tileToRegion(tileY);
76 if (!world.ownsRegion(destRegionX, destRegionY))
77 {
78 world.frozen.add(entity.entityId);
79 events.push({
80 type: "transfer-request",
81 entityId: entity.entityId,
82 fromTileX,
83 fromTileY,
84 destTileX: tileX,
85 destTileY: tileY,
86 toRegionX: destRegionX,
87 toRegionY: destRegionY
88 });
89 continue;
90 }
91
92 const lerpMs = Math.max(50, Math.round(dt * 1000));
93 events.push({
94 type: "entity-move",
95 entityId: entity.entityId,
96 fromTileX,
97 fromTileY,
98 tileX,
99 tileY,
100 worldX: flight.x,
101 worldY: flight.y,
102 gait: entity.movement?.gait ?? "walk",
103 lerpMs,
104 freeFlight: true
105 });
106 }
107 return events;
108}
109
110function pickFlightTarget (world, fromX, fromY)
111{
112 for (let attempt = 0; attempt < 16; attempt++)
113 {
114 const angle = Math.random() * Math.PI * 2;
115 const dist = 2 + Math.random() * 6;
116 const x = fromX + Math.cos(angle) * dist;
117 const y = fromY + Math.sin(angle) * dist;
118 const tileX = Math.floor(x);
119 const tileY = Math.floor(y);
120 if (!world.ownsRegion(tileToRegion(tileX), tileToRegion(tileY)))
121 { continue; }
122 const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY));
123 if (!data?.open)
124 { continue; }
125 return { x: tileX + 0.5, y: tileY + 0.5 };
126 }
127 return null;
128}