BlueGit

timfa questward

Files

questward / server / src / ecs / serialize.js

serialize.js

serialize.js 7.5 KiB Download Raw
1import { Component } from "./components.js";
2import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
3import { equippedLightRadiusTiles, equippedLightIntensity } from "../../../shared/definitions/items.js";
4import {
5 statusExtraLightRadius,
6 statusVisualScale,
7 publicStatusEffectsView
8} from "../../../shared/statusEffects/rules.js";
9import { mobDefinition, isTownGuardMob } from "../../../shared/definitions/mobs.js";
10import { mobCanFight, mobIsAggressive } from "../../../shared/definitions/mobCombat.js";
11import { shopHasInterface } from "../shop/service.js";
12import { composeAppearanceLayers } from "../../../shared/appearance/hair.js";
13
14/** Matches server aggression: spawnOrigin.aggressive, else mob def (not town guards / summons). */
15export function entityIsAggressiveHostile (ecs, handle)
16{
17 const character = ecs.get(handle, Component.character);
18 const creature = ecs.get(handle, Component.creature);
19 if (!creature || character)
20 { return false; }
21 if (ecs.get(handle, Component.summon))
22 { return false; }
23 const origin = ecs.get(handle, Component.spawnOrigin);
24 if (origin?.townGuard || isTownGuardMob(creature.mobTypeId))
25 { return false; }
26 if (origin)
27 { return !!origin.aggressive; }
28 return mobIsAggressive(mobDefinition(creature.mobTypeId));
29}
30export function normalizeMobileComponents (components)
31{
32 const next = { ...(components ?? {}) };
33 if (next.position && typeof next.position === "object")
34 {
35 next.position = {
36 ...next.position,
37 dimensionId: normalizeDimensionId(next.position.dimensionId ?? OVERWORLD_ID)
38 };
39 }
40 if (next.movement && Array.isArray(next.movement.path) && !next.path)
41 {
42 const { path, ...movement } = next.movement;
43 next.movement = movement;
44 next.path = { tiles: path };
45 }
46 if (next.movement && !next.path)
47 { next.path = { tiles: [] }; }
48 if (next.character && Object.prototype.hasOwnProperty.call(next.character, "sessionId"))
49 {
50 const { sessionId, ...character } = next.character;
51 next.character = character;
52 if (sessionId != null)
53 { next.networkIdentity = { ...(next.networkIdentity ?? {}), sessionId }; }
54 }
55 return next;
56}
57
58export function serializeEntity (ecs, entityId)
59{
60 const handle = ecs.handleOf(entityId);
61 if (handle == null)
62 { return null; }
63 return {
64 entityId,
65 components: structuredClone(ecs.componentsOf(handle))
66 };
67}
68
69export function deserializeEntity (ecs, payload)
70{
71 const handle = ecs.spawn(payload.entityId);
72 const components = normalizeMobileComponents(payload.components);
73 for (const [name, data] of Object.entries(components))
74 { ecs.add(handle, name, structuredClone(data)); }
75 return payload.entityId;
76}
77
78export function publicEntityView (ecs, entityId)
79{
80 const handle = ecs.handleOf(entityId);
81 if (handle == null)
82 { return null; }
83 const position = ecs.get(handle, Component.position);
84 if (!position)
85 { return null; }
86 const movement = ecs.get(handle, Component.movement);
87 const appearance = ecs.get(handle, Component.appearance);
88 const character = ecs.get(handle, Component.character);
89 const creature = ecs.get(handle, Component.creature);
90 const flight = ecs.get(handle, Component.flight);
91 const health = ecs.get(handle, Component.health);
92 const equipment = ecs.get(handle, Component.equipment);
93 const statusEffects = ecs.get(handle, Component.statusEffects);
94 const now = Date.now();
95 const layers = appearanceLayers(ecs, handle, appearance);
96 const mob = creature ? mobDefinition(creature.mobTypeId) : null;
97 const isMob = !!creature && !character;
98 const gearLight = equippedLightRadiusTiles(equipment);
99 const gearIntensity = equippedLightIntensity(equipment);
100 const effectLight = statusExtraLightRadius(statusEffects, now);
101 const creatureLight = creature?.lightRadiusTiles
102 ?? (mob?.lightRadiusTiles ?? 0);
103 return {
104 entityId,
105 tileX: position.tileX,
106 tileY: position.tileY,
107 dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),
108 worldX: flight?.x ?? position.tileX + 0.5,
109 worldY: flight?.y ?? position.tileY + 0.5,
110 gait: movement?.gait ?? "walk",
111 sex: appearance?.sex ?? null,
112 firstName: appearance?.firstName ?? "",
113 lastName: appearance?.lastName ?? "",
114 characterId: character?.characterId ?? null,
115 accountId: character?.accountId ?? null,
116 mobTypeId: creature?.mobTypeId ?? null,
117 element: creature?.element ?? null,
118 canFight: mob ? mobCanFight(mob) : (!!character && !ecs.has(handle, Component.dead)),
119 aggressive: entityIsAggressiveHostile(ecs, handle),
120 hostile: entityIsAggressiveHostile(ecs, handle),
121 talkable: (!!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop))) && !ecs.has(handle, Component.dead),
122 hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId) && !ecs.has(handle, Component.dead),
123 hasShop: shopHasInterface(ecs.get(handle, Component.shop)) && !ecs.has(handle, Component.dead),
124 untargetable: !!mob?.untargetable || ecs.has(handle, Component.dead),
125 dead: ecs.has(handle, Component.dead),
126 freeFlight: !!mob?.freeFlight,
127 level: creature?.level ?? null,
128 health: health ? health.current : undefined,
129 maxHealth: health ? health.max : undefined,
130 sprite: layers[0]?.sprite ?? null,
131 layers,
132 lightRadiusTiles: Math.max(gearLight, effectLight, creatureLight),
133 lightIntensity: Math.max(gearLight, effectLight, creatureLight) > 0
134 ? (gearLight >= effectLight && gearLight >= creatureLight ? gearIntensity : 1)
135 : 1,
136 visualScale: statusVisualScale(statusEffects, now),
137 statusEffects: publicStatusEffectsView(statusEffects, now)
138 };
139}
140
141export function appearanceLayers (ecs, handle, appearance)
142{
143 const equipment = ecs.get(handle, Component.equipment);
144 return composeAppearanceLayers(appearance, equipment?.slots ?? {});
145}
146
147export function replicationSpawn (view)
148{
149 if (!view)
150 { return null; }
151 return {
152 type: "entity-spawn",
153 entityId: view.entityId,
154 characterId: view.characterId,
155 accountId: view.accountId ?? null,
156 tileX: view.tileX,
157 tileY: view.tileY,
158 dimensionId: view.dimensionId,
159 worldX: view.worldX,
160 worldY: view.worldY,
161 gait: view.gait,
162 sex: view.sex,
163 firstName: view.firstName,
164 lastName: view.lastName,
165 sprite: view.sprite ?? null,
166 layers: view.layers ?? [],
167 mobTypeId: view.mobTypeId ?? null,
168 canFight: !!view.canFight,
169 aggressive: !!view.aggressive,
170 hostile: !!view.hostile,
171 talkable: !!view.talkable,
172 hasDialogue: !!view.hasDialogue,
173 hasShop: !!view.hasShop,
174 untargetable: !!view.untargetable,
175 freeFlight: !!view.freeFlight,
176 level: view.level ?? null,
177 health: view.health,
178 maxHealth: view.maxHealth,
179 lightRadiusTiles: view.lightRadiusTiles ?? 0,
180 lightIntensity: view.lightIntensity ?? 1,
181 visualScale: view.visualScale ?? 1,
182 skullValue: view.skullValue | 0
183 };
184}
185
186export function replicationMove (view, fromTileX, fromTileY)
187{
188 if (!view)
189 { return null; }
190 return {
191 type: "entity-move",
192 entityId: view.entityId,
193 tileX: view.tileX,
194 tileY: view.tileY,
195 dimensionId: view.dimensionId,
196 worldX: view.worldX,
197 worldY: view.worldY,
198 gait: view.gait,
199 fromTileX,
200 fromTileY,
201 freeFlight: !!view.freeFlight
202 };
203}
204
205export function replicationAppearance (view)
206{
207 if (!view)
208 { return null; }
209 return {
210 type: "entity-appearance",
211 entityId: view.entityId,
212 tileX: view.tileX,
213 tileY: view.tileY,
214 sprite: view.sprite ?? null,
215 layers: view.layers ?? [],
216 lightRadiusTiles: view.lightRadiusTiles ?? 0,
217 lightIntensity: view.lightIntensity ?? 1,
218 visualScale: view.visualScale ?? 1
219 };
220}
221
222export function replicationDespawn (view)
223{
224 if (!view)
225 { return null; }
226 return {
227 type: "entity-despawn",
228 entityId: view.entityId,
229 tileX: view.tileX,
230 tileY: view.tileY,
231 dimensionId: view.dimensionId
232 };
233}