| 1 | import { Component } from "./components.js"; |
| 2 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 3 | import { equippedLightRadiusTiles } from "../../../shared/definitions/items.js"; |
| 4 | import { |
| 5 | statusExtraLightRadius, |
| 6 | statusVisualScale, |
| 7 | publicStatusEffectsView |
| 8 | } from "../../../shared/statusEffects/rules.js"; |
| 9 | import { mobDefinition } from "../../../shared/definitions/mobs.js"; |
| 10 | import { mobCanFight } from "../../../shared/definitions/mobCombat.js"; |
| 11 | import { shopHasInterface } from "../shop/service.js"; |
| 12 | import { composeAppearanceLayers } from "../../../shared/appearance/hair.js"; |
| 13 | |
| 14 | export 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 | |
| 42 | export 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 | |
| 53 | export 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 | |
| 62 | export 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, |
| 102 | hostile: !!mob?.hostile, |
| 103 | talkable: !!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop)), |
| 104 | hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId), |
| 105 | hasShop: shopHasInterface(ecs.get(handle, Component.shop)), |
| 106 | untargetable: !!mob?.untargetable, |
| 107 | freeFlight: !!mob?.freeFlight, |
| 108 | level: creature?.level ?? null, |
| 109 | health: health ? health.current : undefined, |
| 110 | maxHealth: health ? health.max : undefined, |
| 111 | sprite: layers[0]?.sprite ?? null, |
| 112 | layers, |
| 113 | lightRadiusTiles: Math.max(gearLight, effectLight, creatureLight), |
| 114 | visualScale: statusVisualScale(statusEffects, now), |
| 115 | statusEffects: publicStatusEffectsView(statusEffects, now) |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | export function appearanceLayers (ecs, handle, appearance) |
| 120 | { |
| 121 | const equipment = ecs.get(handle, Component.equipment); |
| 122 | return composeAppearanceLayers(appearance, equipment?.slots ?? {}); |
| 123 | } |
| 124 | |
| 125 | export function replicationSpawn (view) |
| 126 | { |
| 127 | if (!view) |
| 128 | { return null; } |
| 129 | return { |
| 130 | type: "entity-spawn", |
| 131 | entityId: view.entityId, |
| 132 | characterId: view.characterId, |
| 133 | accountId: view.accountId ?? null, |
| 134 | tileX: view.tileX, |
| 135 | tileY: view.tileY, |
| 136 | worldX: view.worldX, |
| 137 | worldY: view.worldY, |
| 138 | gait: view.gait, |
| 139 | sex: view.sex, |
| 140 | firstName: view.firstName, |
| 141 | lastName: view.lastName, |
| 142 | sprite: view.sprite ?? null, |
| 143 | layers: view.layers ?? [], |
| 144 | mobTypeId: view.mobTypeId ?? null, |
| 145 | canFight: !!view.canFight, |
| 146 | hostile: !!view.hostile, |
| 147 | talkable: !!view.talkable, |
| 148 | hasDialogue: !!view.hasDialogue, |
| 149 | hasShop: !!view.hasShop, |
| 150 | untargetable: !!view.untargetable, |
| 151 | freeFlight: !!view.freeFlight, |
| 152 | level: view.level ?? null, |
| 153 | health: view.health, |
| 154 | maxHealth: view.maxHealth, |
| 155 | lightRadiusTiles: view.lightRadiusTiles ?? 0, |
| 156 | visualScale: view.visualScale ?? 1, |
| 157 | skullValue: view.skullValue | 0 |
| 158 | }; |
| 159 | } |
| 160 | |
| 161 | export function replicationMove (view, fromTileX, fromTileY) |
| 162 | { |
| 163 | if (!view) |
| 164 | { return null; } |
| 165 | return { |
| 166 | type: "entity-move", |
| 167 | entityId: view.entityId, |
| 168 | tileX: view.tileX, |
| 169 | tileY: view.tileY, |
| 170 | worldX: view.worldX, |
| 171 | worldY: view.worldY, |
| 172 | gait: view.gait, |
| 173 | fromTileX, |
| 174 | fromTileY, |
| 175 | freeFlight: !!view.freeFlight |
| 176 | }; |
| 177 | } |
| 178 | |
| 179 | export function replicationAppearance (view) |
| 180 | { |
| 181 | if (!view) |
| 182 | { return null; } |
| 183 | return { |
| 184 | type: "entity-appearance", |
| 185 | entityId: view.entityId, |
| 186 | tileX: view.tileX, |
| 187 | tileY: view.tileY, |
| 188 | sprite: view.sprite ?? null, |
| 189 | layers: view.layers ?? [], |
| 190 | lightRadiusTiles: view.lightRadiusTiles ?? 0, |
| 191 | visualScale: view.visualScale ?? 1 |
| 192 | }; |
| 193 | } |
| 194 | |
| 195 | export function replicationDespawn (view) |
| 196 | { |
| 197 | if (!view) |
| 198 | { return null; } |
| 199 | return { |
| 200 | type: "entity-despawn", |
| 201 | entityId: view.entityId, |
| 202 | tileX: view.tileX, |
| 203 | tileY: view.tileY |
| 204 | }; |
| 205 | } |