BlueGit

timfa questward

Files

questward / server / src / ecs / serialize.js

serialize.js

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