| 1 | import { CHUNK_SIZE, MessageType } from "../../../shared/protocol/constants.js"; |
| 2 | import { obscureSpeech, speechReadability } from "../../../shared/speech/proximity.js"; |
| 3 | |
| 4 | let nextMessageId = 1; |
| 5 | |
| 6 | export function broadcastSpeech (sessions, simulation, speakerSession, rawText) |
| 7 | { |
| 8 | const maxChars = Math.max(1, Math.round(simulation.settings.chatMaxCharacters || 100)); |
| 9 | const text = String(rawText ?? "").trim().slice(0, maxChars); |
| 10 | if (!text) |
| 11 | { return; } |
| 12 | |
| 13 | const speakerPos = simulation.positionOf(speakerSession.entityId); |
| 14 | if (!speakerPos) |
| 15 | { return; } |
| 16 | |
| 17 | const messageId = nextMessageId++; |
| 18 | const createdAt = Date.now(); |
| 19 | for (const other of sessions.values()) |
| 20 | { |
| 21 | if (!other.entityId) |
| 22 | { continue; } |
| 23 | let readable = text; |
| 24 | let readability = 1; |
| 25 | if (other !== speakerSession) |
| 26 | { |
| 27 | const listenerPos = simulation.positionOf(other.entityId); |
| 28 | if (!listenerPos) |
| 29 | { continue; } |
| 30 | const distance = Math.hypot(listenerPos.tileX - speakerPos.tileX, listenerPos.tileY - speakerPos.tileY); |
| 31 | readability = speechReadability(distance, CHUNK_SIZE); |
| 32 | if (readability <= 0) |
| 33 | { continue; } |
| 34 | readable = obscureSpeech(text, readability, messageId ^ other.id); |
| 35 | if (!readable) |
| 36 | { continue; } |
| 37 | } |
| 38 | if (other.socket.readyState === 1) |
| 39 | { |
| 40 | other.socket.send(JSON.stringify({ |
| 41 | type: MessageType.chatBubble, |
| 42 | messageId, |
| 43 | entityId: speakerSession.entityId, |
| 44 | characterId: speakerSession.characterId, |
| 45 | text: readable, |
| 46 | readability, |
| 47 | createdAt |
| 48 | })); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Product icon above the crafter's head. Same proximity opacity as chat, |
| 55 | * but no text and no letter obfuscation. |
| 56 | */ |
| 57 | export function broadcastItemBubble (sessions, simulation, speakerEntityId, itemId, quantity = 1) |
| 58 | { |
| 59 | const speakerPos = simulation.positionOf(speakerEntityId); |
| 60 | if (!speakerPos || itemId == null) |
| 61 | { return; } |
| 62 | |
| 63 | const messageId = nextMessageId++; |
| 64 | const createdAt = Date.now(); |
| 65 | for (const other of sessions.values()) |
| 66 | { |
| 67 | if (!other.entityId) |
| 68 | { continue; } |
| 69 | let readability = 1; |
| 70 | if (other.entityId !== speakerEntityId) |
| 71 | { |
| 72 | const listenerPos = simulation.positionOf(other.entityId); |
| 73 | if (!listenerPos) |
| 74 | { continue; } |
| 75 | const distance = Math.hypot(listenerPos.tileX - speakerPos.tileX, listenerPos.tileY - speakerPos.tileY); |
| 76 | readability = speechReadability(distance, CHUNK_SIZE); |
| 77 | if (readability <= 0) |
| 78 | { continue; } |
| 79 | } |
| 80 | if (other.socket.readyState === 1) |
| 81 | { |
| 82 | other.socket.send(JSON.stringify({ |
| 83 | type: MessageType.itemBubble, |
| 84 | messageId, |
| 85 | entityId: speakerEntityId, |
| 86 | itemId, |
| 87 | quantity: Math.max(1, quantity | 0 || 1), |
| 88 | readability, |
| 89 | createdAt |
| 90 | })); |
| 91 | } |
| 92 | } |
| 93 | } |