BlueGit

timfa questward

Files

questward / server / src / ecs / serialize.js

serialize.js

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