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

292 lines
10 KiB
Plaintext

/// Resolves how clearly this listener perceives the message.
/mob/proc/get_clarity(datum/say_message/msg)
var/mob/speaker = msg.speaker
if(msg.say_mode == SAYMODE_SIGN)
if(isghost(src))
return msg.base_clarity
if((src.sdisabilities & BLIND) || src.blinded || !speaker || !(speaker in view(src)))
return CLARITY_NONE
return msg.base_clarity
if(msg.say_mode != SAYMODE_RADIO && !(msg.single_language?.flags & PRESSUREPROOF) && !isghost(src))
var/turf/T = get_turf(src)
if(T)
var/datum/gas_mixture/air = T.return_air()
var/pressure = SAFE_XGM_PRESSURE(air)
if(pressure < SOUND_MINIMUM_PRESSURE && get_dist(speaker, src) > 1)
if(get_dist(speaker, src) <= 4 && !msg.italics)
to_chat(src, SPAN_NOTICE("[speaker?.name] talks, but you're in a vacuum. Maybe if you were close enough for the sound to transmit through touch..."))
return CLARITY_NONE
// vr_mob is a Psionic mob in the dream. They hear clearly even when not conscious.
if(!vr_mob && (sleeping || stat == UNCONSCIOUS))
return CLARITY_DROWSY
if(isdeaf(src))
// INNATE is an audible emote. Gives no 'cannot hear' message.
if(!(msg.single_language?.flags & INNATE))
if(msg.say_mode == SAYMODE_RADIO)
if(prob(20))
to_chat(src, SPAN_WARNING("You feel your headset vibrate but can hear nothing from it!"))
else if(speaker == src)
to_chat(src, SPAN_WARNING("You cannot hear yourself speak!"))
else
to_chat(src, "<span class='name'>[speaker?.name]</span>[msg.alt_name] talks but you cannot hear them.")
return CLARITY_NONE
return msg.base_clarity
return msg.base_clarity
/// Whether a message can be shown to this mob at all.
/mob/proc/has_chat_sink()
return client || vr_mob
/mob/living/carbon/has_chat_sink()
. = ..()
if(.)
return TRUE
var/datum/dionastats/DS = get_dionastats()
return DS?.nym ? TRUE : FALSE
/mob/living/carbon/alien/diona/has_chat_sink()
. = ..()
if(.)
return TRUE
return (detached && gestalt) ? TRUE : FALSE
/// Builds the correct envelope around the body depending on SAYMODE.
/mob/proc/format_envelope(datum/say_message/msg, muffled = FALSE, clarity = CLARITY_CLEAR)
switch(msg.say_mode)
if(SAYMODE_SIGN)
return format_envelope_sign(msg, msg.text_for(src, clarity))
if(SAYMODE_RADIO)
return format_envelope_radio(msg, clarity)
else
return format_envelope_spoken(msg, clarity, muffled)
/// Wraps a say envelope around the message body.
/mob/proc/format_envelope_spoken(datum/say_message/msg, clarity = CLARITY_CLEAR, muffled = FALSE)
var/list/rendered = msg.render_body(src, clarity)
var/body = rendered[1]
if(!length(body))
return ""
var/emote_led = rendered[2]
var/mob/speaker = msg.speaker
var/speaker_name = speaker?.name
if(ishuman(speaker))
var/mob/living/carbon/human/H = speaker
speaker_name = H.GetVoice()
var/datum/language/accent_language
for(var/datum/say_segment/segment as anything in msg.segments)
if(segment.language?.allow_accents)
accent_language = segment.language
break
var/accent_icon = accent_language ? speaker?.get_accent_icon(accent_language, src) : null
if(msg.italics || muffled)
body = "<i>[body]</i>"
var/track = null
if(isghost(src))
if(speaker_name != speaker.real_name && speaker.real_name)
speaker_name = "[speaker.real_name] ([speaker_name])"
track = "[ghost_follow_link(speaker, src)] "
if((client.prefs.toggles & CHAT_GHOSTEARS) && (get_turf(speaker) in view(src)))
body = "<b>[body]</b>"
var/leading = emote_led ? "" : "[msg.verb], "
var/font_open = msg.font_size ? "<font size='[msg.font_size]'>" : ""
var/font_close = msg.font_size ? "</font>" : ""
return "[track][accent_icon ? accent_icon + " " : ""][font_open]<span class='game say'><span class='name'>[speaker_name]</span>[msg.alt_name] [leading]<span class='message'>[body]</span></span>[font_close]"
/// Wraps a radio envelope around the message body.
/mob/proc/format_envelope_radio(datum/say_message/msg, clarity = CLARITY_CLEAR)
var/mob/speaker = msg.speaker
var/hard_to_hear = (msg.base_clarity == CLARITY_FAINT)
var/list/radio_parts = msg.radio_parts
var/part_a = LAZYACCESS(radio_parts, 1)
var/part_b = LAZYACCESS(radio_parts, 2)
var/part_c = LAZYACCESS(radio_parts, 3)
var/speaker_name = speaker ? speaker.name : "Unknown"
if(ishuman(speaker))
speaker_name = speaker.GetVoice()
if(hard_to_hear)
speaker_name = "Unknown"
var/datum/language/language = msg.single_language
var/accent_icon = speaker?.get_accent_icon(language, src)
accent_icon = accent_icon ? accent_icon + " " : ""
part_a = replacetext(part_a, "%ACCENT%", accent_icon)
var/track = null
if(istype(src, /mob/living/silicon/ai) && !hard_to_hear)
var/changed_voice
var/jobname
var/mob/living/carbon/human/impersonating
if(ishuman(speaker))
var/mob/living/carbon/human/H = speaker
if(H.wear_mask && istype(H.wear_mask, /obj/item/clothing/mask/gas/voice))
changed_voice = 1
var/mob/living/carbon/human/I
for(var/mob/living/carbon/human/M in GLOB.mob_list)
if(M.real_name == speaker_name)
I = M
break
if(I && !(I.name != speaker_name && I.wear_id && istype(I.wear_id, /obj/item/card/id/syndicate)))
impersonating = I
jobname = impersonating.get_assignment()
else
jobname = "Unknown"
else
jobname = H.get_assignment()
else if(iscarbon(speaker))
jobname = "No id"
else if(isAI(speaker))
jobname = "AI"
else if(isrobot(speaker))
jobname = "Cyborg"
else if(istype(speaker, /mob/living/silicon/pai))
jobname = "Personal AI"
else
jobname = "Unknown"
if(changed_voice)
if(impersonating)
track = "<a class='ai_tracking' href='byond://?src=[REF(src)];trackname=[html_encode(speaker_name)];track=[REF(impersonating)]'>[speaker_name] ([jobname])</a>"
else
track = "[speaker_name] ([jobname])"
else
track = "<a class='ai_tracking' href='byond://?src=[REF(src)];trackname=[html_encode(speaker_name)];track=[REF(speaker)]'>[speaker_name] ([jobname])</a>"
if(isghost(src))
if(speaker != null)
if(speaker_name != speaker.real_name && !isAI(speaker))
speaker_name = "[speaker.real_name] ([speaker_name])"
track = "[ghost_follow_link(speaker, src)] "
var/list/rendered = msg.render_body(src, clarity)
var/body = rendered[1]
if(!length(body))
return ""
var/leading = rendered[2] ? "" : "[msg.verb], "
var/formatted = "[leading]<span class='message'>[body]</span>[part_c]"
if(istype(src, /mob/living/silicon/ai) && track && !hard_to_hear)
return "[part_a][track][part_b][formatted]"
if(isghost(src))
return "[track][part_a][speaker_name][part_b][formatted]"
return "[part_a][speaker_name][part_b][formatted]"
/mob/proc/format_envelope_sign(datum/say_message/msg, body)
var/mob/speaker = msg.speaker
if(say_understands(speaker, msg.single_language))
return "<B>[speaker]</B> [msg.verb], \"[body]\""
var/list/sign_adv_length = msg.single_language?.sign_adv_length
if(length(sign_adv_length) <= 4)
sign_adv_length = list(" briefly", " a short message", " a message", " a lengthy message", " a very lengthy message")
var/adverb
var/length = length(body) * pick(0.8, 0.9, 1.0, 1.1, 1.2) // a little fuzziness
switch(length)
if(0 to 12) adverb = sign_adv_length[1]
if(12 to 30) adverb = sign_adv_length[2]
if(30 to 48) adverb = sign_adv_length[3]
if(48 to 90) adverb = sign_adv_length[4]
else adverb = sign_adv_length[5]
return "<B>[speaker]</B> [msg.verb][adverb]."
/// Handles a mob receiving a say.
/mob/proc/hear_message(datum/say_message/msg)
var/clarity = get_clarity(msg)
if(clarity == CLARITY_NONE)
return FALSE
. = FALSE
if(has_chat_sink())
display_message(msg, clarity)
. = TRUE
react_to_message(msg)
/// Noop by default. Overriden by clientless mobs that react to speech.
/mob/proc/react_to_message(datum/say_message/msg)
return
/// Renders the body for this listener, then decorates and outputs it.
/mob/proc/display_message(datum/say_message/msg, clarity)
var/mob/speaker = msg.speaker
// Ghosts skip clientless out-of-view chatter.
if(msg.say_mode != SAYMODE_RADIO && isghost(src) && speaker && !speaker.client && (client?.prefs.toggles & CHAT_GHOSTEARS) && !(speaker in view(src)))
return
if(clarity == CLARITY_DROWSY)
var/drowsy = msg.text_for(src, clarity)
if(length(drowsy))
on_hear_message(drowsy)
return
// In thin air, non-pressureproof languages are muffled.
var/muffled = FALSE
var/sound_vol = msg.sound_vol
if(msg.say_mode != SAYMODE_RADIO && !isghost(src) && !(msg.single_language?.flags & PRESSUREPROOF))
var/turf/T = get_turf(src)
if(T)
var/datum/gas_mixture/air = T.return_air()
if(SAFE_XGM_PRESSURE(air) < ONE_ATMOSPHERE * 0.4)
muffled = TRUE
sound_vol *= 0.5
var/envelope = format_envelope(msg, muffled, clarity)
if(!length(envelope))
return
on_hear_message(envelope)
// This is a special case for internal mobs like cortical borers.
// They see through the eyes of their host so we must relay the message.
if(msg.say_mode == SAYMODE_SIGN && (status_flags & PASSEMOTES))
for(var/obj/item/holder/H in contents)
H.show_message(envelope)
for(var/mob/living/M in contents)
M.show_message(envelope)
if(msg.say_mode != SAYMODE_RADIO)
play_speech_sound(msg, sound_vol)
/// Plays the speech sound for a given message.
/mob/proc/play_speech_sound(datum/say_message/msg, sound_vol)
var/mob/speaker = msg.speaker
if(msg.speech_sound && (get_dist(speaker, src) <= world.view && src.z == speaker?.z))
var/turf/source = speaker ? get_turf(speaker) : get_turf(src)
playsound(source, msg.speech_sound, sound_vol, vary = TRUE)
/proc/say_timestamp()
return "<span class='say_quote'>\[[worldtime2text()]\]</span>"
/// The output sink for a given message.
/mob/proc/on_hear_message(message)
to_chat(src, message)
if(vr_mob)
to_chat(vr_mob, message)
/// Dionae require special treatment. Messages should be forwarded to the nymph.
/mob/living/carbon/on_hear_message(message)
..()
var/datum/dionastats/DS = get_dionastats()
if(DS?.nym)
var/mob/living/carbon/alien/diona/D = DS.nym.resolve()
if(D)
to_chat(D, message)
/// A detached gestalt should be forwarded heard messages.
/mob/living/carbon/alien/diona/on_hear_message(message)
to_chat(src, message)
if(detached && gestalt)
to_chat(gestalt, message)
/// Silicons receive a timestamp with heard messages.
/mob/living/silicon/on_hear_message(message)
var/time = say_timestamp()
to_chat(src, "[time] [message]")
if(vr_mob)
to_chat(vr_mob, "[time] [message]")