import { Component } from "./components.js"; import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; import { equippedLightRadiusTiles, equippedLightIntensity } from "../../../shared/definitions/items.js"; import { statusExtraLightRadius, statusVisualScale, publicStatusEffectsView } from "../../../shared/statusEffects/rules.js"; import { mobDefinition, isTownGuardMob } from "../../../shared/definitions/mobs.js"; import { mobCanFight, mobIsAggressive } from "../../../shared/definitions/mobCombat.js"; import { shopHasInterface } from "../shop/service.js"; import { composeAppearanceLayers } from "../../../shared/appearance/hair.js"; /** Matches server aggression: spawnOrigin.aggressive, else mob def (not town guards / summons). */ export function entityIsAggressiveHostile (ecs, handle) { const character = ecs.get(handle, Component.character); const creature = ecs.get(handle, Component.creature); if (!creature || character) { return false; } if (ecs.get(handle, Component.summon)) { return false; } const origin = ecs.get(handle, Component.spawnOrigin); if (origin?.townGuard || isTownGuardMob(creature.mobTypeId)) { return false; } if (origin) { return !!origin.aggressive; } return mobIsAggressive(mobDefinition(creature.mobTypeId)); } export function normalizeMobileComponents (components) { const next = { ...(components ?? {}) }; if (next.position && typeof next.position === "object") { next.position = { ...next.position, dimensionId: normalizeDimensionId(next.position.dimensionId ?? OVERWORLD_ID) }; } if (next.movement && Array.isArray(next.movement.path) && !next.path) { const { path, ...movement } = next.movement; next.movement = movement; next.path = { tiles: path }; } if (next.movement && !next.path) { next.path = { tiles: [] }; } if (next.character && Object.prototype.hasOwnProperty.call(next.character, "sessionId")) { const { sessionId, ...character } = next.character; next.character = character; if (sessionId != null) { next.networkIdentity = { ...(next.networkIdentity ?? {}), sessionId }; } } return next; } export function serializeEntity (ecs, entityId) { const handle = ecs.handleOf(entityId); if (handle == null) { return null; } return { entityId, components: structuredClone(ecs.componentsOf(handle)) }; } export function deserializeEntity (ecs, payload) { const handle = ecs.spawn(payload.entityId); const components = normalizeMobileComponents(payload.components); for (const [name, data] of Object.entries(components)) { ecs.add(handle, name, structuredClone(data)); } return payload.entityId; } export function publicEntityView (ecs, entityId) { const handle = ecs.handleOf(entityId); if (handle == null) { return null; } const position = ecs.get(handle, Component.position); if (!position) { return null; } const movement = ecs.get(handle, Component.movement); const appearance = ecs.get(handle, Component.appearance); const character = ecs.get(handle, Component.character); const creature = ecs.get(handle, Component.creature); const flight = ecs.get(handle, Component.flight); const health = ecs.get(handle, Component.health); const equipment = ecs.get(handle, Component.equipment); const statusEffects = ecs.get(handle, Component.statusEffects); const now = Date.now(); const layers = appearanceLayers(ecs, handle, appearance); const mob = creature ? mobDefinition(creature.mobTypeId) : null; const isMob = !!creature && !character; const gearLight = equippedLightRadiusTiles(equipment); const gearIntensity = equippedLightIntensity(equipment); const effectLight = statusExtraLightRadius(statusEffects, now); const creatureLight = creature?.lightRadiusTiles ?? (mob?.lightRadiusTiles ?? 0); return { entityId, tileX: position.tileX, tileY: position.tileY, dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID), worldX: flight?.x ?? position.tileX + 0.5, worldY: flight?.y ?? position.tileY + 0.5, gait: movement?.gait ?? "walk", sex: appearance?.sex ?? null, firstName: appearance?.firstName ?? "", lastName: appearance?.lastName ?? "", characterId: character?.characterId ?? null, accountId: character?.accountId ?? null, mobTypeId: creature?.mobTypeId ?? null, element: creature?.element ?? null, canFight: mob ? mobCanFight(mob) : (!!character && !ecs.has(handle, Component.dead)), aggressive: entityIsAggressiveHostile(ecs, handle), hostile: entityIsAggressiveHostile(ecs, handle), talkable: (!!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop))) && !ecs.has(handle, Component.dead), hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId) && !ecs.has(handle, Component.dead), hasShop: shopHasInterface(ecs.get(handle, Component.shop)) && !ecs.has(handle, Component.dead), untargetable: !!mob?.untargetable || ecs.has(handle, Component.dead), dead: ecs.has(handle, Component.dead), freeFlight: !!mob?.freeFlight, level: creature?.level ?? null, health: health ? health.current : undefined, maxHealth: health ? health.max : undefined, sprite: layers[0]?.sprite ?? null, layers, lightRadiusTiles: Math.max(gearLight, effectLight, creatureLight), lightIntensity: Math.max(gearLight, effectLight, creatureLight) > 0 ? (gearLight >= effectLight && gearLight >= creatureLight ? gearIntensity : 1) : 1, visualScale: statusVisualScale(statusEffects, now), statusEffects: publicStatusEffectsView(statusEffects, now) }; } export function appearanceLayers (ecs, handle, appearance) { const equipment = ecs.get(handle, Component.equipment); return composeAppearanceLayers(appearance, equipment?.slots ?? {}); } export function replicationSpawn (view) { if (!view) { return null; } return { type: "entity-spawn", entityId: view.entityId, characterId: view.characterId, accountId: view.accountId ?? null, tileX: view.tileX, tileY: view.tileY, worldX: view.worldX, worldY: view.worldY, gait: view.gait, sex: view.sex, firstName: view.firstName, lastName: view.lastName, sprite: view.sprite ?? null, layers: view.layers ?? [], mobTypeId: view.mobTypeId ?? null, canFight: !!view.canFight, aggressive: !!view.aggressive, hostile: !!view.hostile, talkable: !!view.talkable, hasDialogue: !!view.hasDialogue, hasShop: !!view.hasShop, untargetable: !!view.untargetable, freeFlight: !!view.freeFlight, level: view.level ?? null, health: view.health, maxHealth: view.maxHealth, lightRadiusTiles: view.lightRadiusTiles ?? 0, lightIntensity: view.lightIntensity ?? 1, visualScale: view.visualScale ?? 1, skullValue: view.skullValue | 0 }; } export function replicationMove (view, fromTileX, fromTileY) { if (!view) { return null; } return { type: "entity-move", entityId: view.entityId, tileX: view.tileX, tileY: view.tileY, worldX: view.worldX, worldY: view.worldY, gait: view.gait, fromTileX, fromTileY, freeFlight: !!view.freeFlight }; } export function replicationAppearance (view) { if (!view) { return null; } return { type: "entity-appearance", entityId: view.entityId, tileX: view.tileX, tileY: view.tileY, sprite: view.sprite ?? null, layers: view.layers ?? [], lightRadiusTiles: view.lightRadiusTiles ?? 0, lightIntensity: view.lightIntensity ?? 1, visualScale: view.visualScale ?? 1 }; } export function replicationDespawn (view) { if (!view) { return null; } return { type: "entity-despawn", entityId: view.entityId, tileX: view.tileX, tileY: view.tileY }; }