BlueGit

timfa questward

Files

questward / server / src / ecs / serialize.js

serialize.js

serialize.js 6.3 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 const creatureLight = creature?.lightRadiusTiles
85 ?? (mob?.lightRadiusTiles ?? 0);
86 return {
87 entityId,
88 tileX: position.tileX,
89 tileY: position.tileY,
90 dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID),
91 worldX: flight?.x ?? position.tileX + 0.5,
92 worldY: flight?.y ?? position.tileY + 0.5,
93 gait: movement?.gait ?? "walk",
94 sex: appearance?.sex ?? null,
95 firstName: appearance?.firstName ?? "",
96 lastName: appearance?.lastName ?? "",
97 characterId: character?.characterId ?? null,
98 accountId: character?.accountId ?? null,
99 mobTypeId: creature?.mobTypeId ?? null,
100 element: creature?.element ?? null,
101 canFight: mob ? mobCanFight(mob) : (!!character && !ecs.has(handle, Component.dead)),
102 hostile: !!mob?.hostile,
103 talkable: (!!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop))) && !ecs.has(handle, Component.dead),
104 hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId) && !ecs.has(handle, Component.dead),
105 hasShop: shopHasInterface(ecs.get(handle, Component.shop)) && !ecs.has(handle, Component.dead),
106 untargetable: !!mob?.untargetable || ecs.has(handle, Component.dead),
107 dead: ecs.has(handle, Component.dead),
108 freeFlight: !!mob?.freeFlight,
109 level: creature?.level ?? null,
110 health: health ? health.current : undefined,
111 maxHealth: health ? health.max : undefined,
112 sprite: layers[0]?.sprite ?? null,
113 layers,
114 lightRadiusTiles: Math.max(gearLight, effectLight, creatureLight),
115 visualScale: statusVisualScale(statusEffects, now),
116 statusEffects: publicStatusEffectsView(statusEffects, now)
117 };
118}
119
120export function appearanceLayers (ecs, handle, appearance)
121{
122 const equipment = ecs.get(handle, Component.equipment);
123 return composeAppearanceLayers(appearance, equipment?.slots ?? {});
124}
125
126export function replicationSpawn (view)
127{
128 if (!view)
129 { return null; }
130 return {
131 type: "entity-spawn",
132 entityId: view.entityId,
133 characterId: view.characterId,
134 accountId: view.accountId ?? null,
135 tileX: view.tileX,
136 tileY: view.tileY,
137 worldX: view.worldX,
138 worldY: view.worldY,
139 gait: view.gait,
140 sex: view.sex,
141 firstName: view.firstName,
142 lastName: view.lastName,
143 sprite: view.sprite ?? null,
144 layers: view.layers ?? [],
145 mobTypeId: view.mobTypeId ?? null,
146 canFight: !!view.canFight,
147 hostile: !!view.hostile,
148 talkable: !!view.talkable,
149 hasDialogue: !!view.hasDialogue,
150 hasShop: !!view.hasShop,
151 untargetable: !!view.untargetable,
152 freeFlight: !!view.freeFlight,
153 level: view.level ?? null,
154 health: view.health,
155 maxHealth: view.maxHealth,
156 lightRadiusTiles: view.lightRadiusTiles ?? 0,
157 visualScale: view.visualScale ?? 1,
158 skullValue: view.skullValue | 0
159 };
160}
161
162export function replicationMove (view, fromTileX, fromTileY)
163{
164 if (!view)
165 { return null; }
166 return {
167 type: "entity-move",
168 entityId: view.entityId,
169 tileX: view.tileX,
170 tileY: view.tileY,
171 worldX: view.worldX,
172 worldY: view.worldY,
173 gait: view.gait,
174 fromTileX,
175 fromTileY,
176 freeFlight: !!view.freeFlight
177 };
178}
179
180export function replicationAppearance (view)
181{
182 if (!view)
183 { return null; }
184 return {
185 type: "entity-appearance",
186 entityId: view.entityId,
187 tileX: view.tileX,
188 tileY: view.tileY,
189 sprite: view.sprite ?? null,
190 layers: view.layers ?? [],
191 lightRadiusTiles: view.lightRadiusTiles ?? 0,
192 visualScale: view.visualScale ?? 1
193 };
194}
195
196export function replicationDespawn (view)
197{
198 if (!view)
199 { return null; }
200 return {
201 type: "entity-despawn",
202 entityId: view.entityId,
203 tileX: view.tileX,
204 tileY: view.tileY
205 };
206}