Files
lew 11a9a2d644 Multi-Languages: a complete Saycode rewrite (#22637)
Rewrites Saycode and Langchat to add support for multiple languages in
one message, including audible emotes.

<img width="1139" height="338" alt="image"
src="https://github.com/user-attachments/assets/25e26932-7a6e-4c54-ab74-56fffb92ecad"
/>

Here's some samples to explain how to mix languages.

<img width="422" height="26" alt="image"
src="https://github.com/user-attachments/assets/e1b176cc-8625-4dc9-83c8-a053d3f310e6"
/>

`Languages ,2 can be mixed ,3 like this.`

<img width="540" height="21" alt="image"
src="https://github.com/user-attachments/assets/19156c67-4670-4d7a-84d7-26e527de2676"
/>

`!explains, ,2Emotes work too. ,0The text will get auto-quoted.`

<img width="592" height="18" alt="image"
src="https://github.com/user-attachments/assets/cfc31c5c-2383-41c8-82be-b36836339321"
/>

`,3Most languages ,0can be ,2mixed ,1arbitrarily, ,3any number of
,0times.`

<img width="636" height="20" alt="image"
src="https://github.com/user-attachments/assets/388b4f9d-192a-4374-ac31-bbd4e4e5dfe4"
/>

`,2Emotes. ,eAnd he nods. ,3They don't have to come first anymore.`

There are some exceptions. The exceptions are currently anything with
any of the flags `SIGNLANG`, `HIVEMIND`, `PRESSUREPROOF`,
`KNOWONLYHEAR`. Exceptions work the same as current languages do: they
must be the first language in the message. If so, they prevent switching
into any other language mid-message; if they're not first, they just
wont trigger.

They're exceptions currently because there's not really a clean way that
I or the people I asked for help on this one to make them look nice.
`SIGNLANG` for example doesn't scramble text, it shows it's own `
gestures a lengthy message.` text for those that don't understand. Could
we just replace every instance of sign language with that if somebody
doesn't understand? Probably. It would look pretty awful though. e.g.
`Alina Eskelinen says, "Hello." Alina Eskelinen gestures a short
message.`

This definitely needs testmerging because it more or less rewrites the
entire pipeline surrounding `say`. The `say` code itself had to be
rewritten to support the multiple languages, as well as all the existing
plumbing for listeners receiving messages. In return though, it's
significantly more straightforward and hopefully by extension easier for
people to add to in the future.

Primarily, instead of having four different `hear_say`, `hear_radio`,
`hear_sleep`, etc., routes for messages to come through, every single
audible message is received by `hear_message`, which is responsible for
figuring out how clear the message is (is the radio damaged? is it a
whisper we're eavesdropping on?), who needs to receive it in their
chatbox, formatting it correctly for each listener, and finally if any
npc or object within range needs to react to it in some way, like a
parrot or a mech.

changelog:
- rscadd: "Adds code-switching: you can now speak in multiple languages
in the same message."
- rscadd: "Adds audible emotes to the language list. They can be
triggered with ,e."
  - rscdel: "Removes SSrunechat."
- refactor: "Rewrote langchat in order to support multiple languages and
partial comprehension."
- refactor: "Rewrote a vast majority of all saycode and the code
responsible for displaying saytext to clients."
- bugfix: "Sleeping mobs are no longer able to understand all
languages."
- bugfix: "Langchat now correctly shows the appropriate comprehension
for all viewers rather than all viewers sharing the comprehension of the
last viewer."
- bugfix: "Languages which are supposed to be invisible when not
understood no longer appear as scrambled overhead text."

Forgive me whoever has to review this. Biggest areas that have room for
error is stuff like a borer inside someone's head, and Dionae stuff. Old
langchat had odd exceptions for those and I was forced to rewrite it
entirely, but I think I got it all back to how it was working before.
2026-06-11 10:47:26 +00:00

188 lines
6.0 KiB
Plaintext

/// A complete audible message made of a list of /datum/say_segment.
/datum/say_message
/// The mob saying the message.
var/mob/speaker
/// Name shown instead of the speaker's real name, if any.
var/alt_name = ""
/// The raw string that was typed. Used in the admin log.
var/raw_message
/// The list of segments that make up the message body.
var/list/datum/say_segment/segments = list()
/// The sole language of this message, or null.
var/datum/language/single_language
/// Which verb to use. Determined by punctuation and language of the first piece.
var/verb = "says"
/// Is the message being whispered?
var/whisper = FALSE
/// Is the message being sung?
var/singing = FALSE
/// The musical note bracketing a sung message.
var/sing_note
/// Should the message render as italic?
var/italics = FALSE
/// Optional font size override for the rendered message.
var/font_size
/// Optional range limit for hearing the message.
var/message_range
/// The requested mode for this message.
var/message_mode
/// Sound played to listeners alongside the message, if any.
var/sound/speech_sound
/// Volume of speech_sound.
var/sound_vol
/// How the message is reaching the listener.
var/say_mode = SAYMODE_SPOKEN
/// Initial clarity. Most of the time this is clear. Sometimes the radio will degrade it.
var/base_clarity = CLARITY_CLEAR
/// Radio envelope parts. This must live here for performance reasons pending a telecomms rewrite.
var/list/radio_parts
/// Renders each segment as the listener perceives it. Returns list(text, is_emote) per segment.
/datum/say_message/proc/render_pieces(mob/listener, clarity = CLARITY_CLEAR)
var/list/pieces = list()
var/cap_next = TRUE
for(var/datum/say_segment/segment as anything in segments)
var/is_emote = (segment.language?.flags & INNATE) ? TRUE : FALSE
var/rendered = segment.plain_text_for(listener, speaker)
if(clarity == CLARITY_FAINT && !is_emote)
rendered = length(rendered) ? stars(rendered) : ""
rendered = trim(rendered) // Strip whitespace for quoting. We'll re-add it later.
if(!length(rendered))
continue
if(is_emote)
cap_next = TRUE // emote re-arms caps for the next spoken run
else if(cap_next)
rendered = capitalize(rendered)
cap_next = FALSE
if(segment.language)
rendered = segment.language.colourize(rendered)
pieces += list(list(rendered, is_emote))
return pieces
/// Quotes the given text if it's not an emote.
/proc/quote_run(text, emote)
text = trim(text)
if(!length(text))
return ""
return emote ? text : "\"[text]\""
/// Renders the body. Speech is quoted. Returns list(body, leads_with_emote).
/datum/say_message/proc/render_body(mob/listener, clarity = CLARITY_CLEAR)
var/list/pieces = render_pieces(listener, clarity)
if(!length(pieces))
return list("", FALSE)
var/leads_with_emote = pieces[1][2]
var/list/runs = list()
var/list/cur = list()
var/cur_emote = pieces[1][2]
for(var/list/piece in pieces)
if(length(cur) && piece[2] != cur_emote)
var/q = quote_run(jointext(cur, " "), cur_emote)
if(length(q))
runs += q
cur = list()
cur_emote = piece[2]
cur += piece[1]
var/last = quote_run(jointext(cur, " "), cur_emote)
if(length(last))
runs += last
var/body = listener.hallucinate_heard(jointext(runs, " "), speaker)
if(singing && length(body))
body = "[sing_note] <span class='singing'>[body]</span> [sing_note]"
return list(body, leads_with_emote)
/// Returns the complete message as the given listener perceives it.
/datum/say_message/proc/text_for(mob/listener, clarity = CLARITY_CLEAR)
if(clarity == CLARITY_DROWSY)
return drowsy_text_for(listener)
var/list/out = list()
for(var/list/piece in render_pieces(listener, clarity))
out += piece[1]
if(!length(out))
return ""
var/body = listener.hallucinate_heard(jointext(out, ""), speaker)
if(singing && length(body))
body = "[sing_note] <span class='singing'>[body]</span> [sing_note]"
return body
/// Returns the message as a bare perceived string.
/datum/say_message/proc/plain_text_for(mob/listener)
var/list/plain = list()
for(var/datum/say_segment/segment as anything in segments)
plain += segment.plain_text_for(listener, speaker)
return jointext(plain, "")
/// Returns the message as decorated text for drowsy listeners.
/datum/say_message/proc/drowsy_text_for(mob/listener)
if(isdeaf(listener))
return ""
if(!prob(15))
return "<span class = 'game_say'>...<i>You almost hear someone talking</i>...</span>"
var/list/messages = text2list(plain_text_for(listener), " ")
if(!length(messages))
return "<span class = 'game_say'>...<i>You almost hear someone talking</i>...</span>"
var/list/punctuation = list(",", "!", ".", ";", "?")
var/heardword = messages[rand(1, messages.len)]
if(copytext(heardword, 1, 1) in punctuation)
heardword = copytext(heardword, 2)
if(copytext(heardword, -1) in punctuation)
heardword = copytext(heardword, 1, length(heardword))
return "<span class = 'game_say'>...You hear something about...[heardword]</span>"
/// Returns a flat string with languages stripped out.
/datum/say_message/proc/to_string()
var/list/out = list()
for(var/datum/say_segment/segment as anything in segments)
out += segment.text
return jointext(out, "")
/// Collapses the whole message into a single segment in the given language.
/datum/say_message/proc/collapse_to(datum/language/language, text)
single_language = language
segments = list(new /datum/say_segment(text, language))
/// Returns a shallow copy.
/datum/say_message/proc/copy()
var/datum/say_message/copy = new
copy.speaker = speaker
copy.alt_name = alt_name
copy.raw_message = raw_message
copy.segments = segments
copy.single_language = single_language
copy.verb = verb
copy.whisper = whisper
copy.singing = singing
copy.sing_note = sing_note
copy.italics = italics
copy.font_size = font_size
copy.message_range = message_range
copy.message_mode = message_mode
copy.speech_sound = speech_sound
copy.sound_vol = sound_vol
copy.say_mode = say_mode
copy.base_clarity = base_clarity
copy.radio_parts = radio_parts
return copy