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