| 1 | import { CHUNK_SIZE } from "../protocol/constants.js"; |
| 2 | |
| 3 | export function speechReadability (distanceTiles, chunkSize = CHUNK_SIZE) |
| 4 | { |
| 5 | const dropStart = chunkSize * 0.5; |
| 6 | const silent = chunkSize; |
| 7 | if (distanceTiles <= dropStart) |
| 8 | { return 1; } |
| 9 | if (distanceTiles >= silent) |
| 10 | { return 0; } |
| 11 | return (silent - distanceTiles) / (silent - dropStart); |
| 12 | } |
| 13 | |
| 14 | export function obscureSpeech (text, readability, seed = 1) |
| 15 | { |
| 16 | if (readability >= 1) |
| 17 | { return text; } |
| 18 | if (readability <= 0) |
| 19 | { return ""; } |
| 20 | |
| 21 | let rng = seed >>> 0 || 1; |
| 22 | let result = ""; |
| 23 | for (const char of text) |
| 24 | { |
| 25 | rng = stepRng(rng); |
| 26 | if (isStableSpeechChar(char) || rng / 0x100000000 <= readability) |
| 27 | { result += char; } |
| 28 | else |
| 29 | { result += "."; } |
| 30 | } |
| 31 | return result; |
| 32 | } |
| 33 | |
| 34 | function isStableSpeechChar (char) |
| 35 | { |
| 36 | return !/[\p{L}\p{N}]/u.test(char); |
| 37 | } |
| 38 | |
| 39 | function stepRng (value) |
| 40 | { |
| 41 | return (Math.imul(value, 1664525) + 1013904223) >>> 0; |
| 42 | } |