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

271 lines
8.7 KiB
Plaintext

/mob/proc/say(var/text, var/datum/language/speaking = null, var/verb="says", var/alt_name="", var/ghost_hearing = GHOSTS_ALL_HEAR, var/whisper = FALSE)
return
///what clients use to speak. when you type a message into the chat bar in say mode, this is the first thing that goes off serverside.
/mob/verb/say_verb(message as text)
set name = "Say"
set category = "IC"
if(say_disabled) //This is here to try to identify lag problems
to_chat(usr, SPAN_WARNING("Speech is currently admin-disabled."))
return
message = sanitize(message)
if (src.client.handle_spam_prevention(message, MUTE_IC))
return
//queue this message because verbs are scheduled to process after SendMaps in the tick and speech is pretty expensive when it happens.
//by queuing this for next tick the mc can compensate for its cost instead of having speech delay the start of the next tick
if(message)
QUEUE_OR_CALL_VERB_FOR(VERB_CALLBACK(src, PROC_REF(say), message), SSspeech_controller)
/mob/verb/me_verb(message as text)
set name = "Me"
set category = "IC"
if(say_disabled) //This is here to try to identify lag problems
to_chat(usr, SPAN_WARNING("Speech is currently admin-disabled."))
return
message = sanitize(message)
if (src.client.handle_spam_prevention(message, MUTE_IC))
return
if(use_me)
QUEUE_OR_CALL_VERB_FOR(VERB_CALLBACK(src, PROC_REF(client_emote), "me", usr.emote_type, message), SSspeech_controller)
else
QUEUE_OR_CALL_VERB_FOR(VERB_CALLBACK(src, PROC_REF(emote), message), SSspeech_controller)
/mob/proc/say_dead(var/message)
if(say_disabled) //This is here to try to identify lag problems
to_chat(usr, SPAN_DANGER("Speech is currently admin-disabled."))
return
if(!src.client.holder)
if(!GLOB.config.dsay_allowed)
to_chat(src, SPAN_DANGER("Deadchat is globally muted."))
return
if(client && !(client.prefs.toggles & CHAT_DEAD))
to_chat(usr, SPAN_DANGER("You have deadchat muted."))
return
message = process_chat_markup(message, list("~", "_"))
say_dead_direct("[pick("complains","moans","whines","laments","blubbers")], <span class='message linkify'>\"[message]\"</span>", src)
/mob/proc/say_understands(var/mob/other, var/datum/language/speaking = null)
if(src.stat == DEAD)
return TRUE
// Universal speak makes everything understandable, for obvious reasons.
if(src.universal_speak || src.universal_understand)
return TRUE
// Languages are handled after.
if (!speaking)
if(!other)
return TRUE
if(other.universal_speak)
return TRUE
if(isAI(src) && ispAI(other))
return TRUE
if (istype(other, src.type) || istype(src, other.type))
return TRUE
return FALSE
if(speaking.flags & INNATE)
return TRUE
// Language check.
for(var/datum/language/L in src.languages)
if(speaking.name == L.name)
return TRUE
return FALSE
/*
***Deprecated***
let this be handled at the hear_message proc
This is left in for robot speaking when humans gain binary channel access until I get around to rewriting
robot_talk() proc.
There is no language handling build into it however there is at the /mob level so we accept the call
for it but just ignore it.
*/
/mob/proc/say_quote(var/message, var/datum/language/speaking = null, var/singing = FALSE, var/whisper = FALSE)
. = "says"
if(singing)
return "sings"
if(whisper)
return "whispers"
var/ending = copytext(message, length(message))
var/pre_ending = copytext(message, length(message) - 1, length(message))
if(ending == "!")
if(pre_ending == "!" || pre_ending == "?")
. = pick("shouts", "yells")
else
. = "exclaims"
else if(ending == "?")
. ="asks"
/mob/proc/whisper(var/message, var/datum/language/speaking, var/is_singing = FALSE)
set name = "Whisper"
set category = "IC"
return
/mob/proc/get_ear()
// returns an atom representing a location on the map from which this
// mob can hear things
// should be overloaded for all mobs whose "ear" is separate from their "mob"
return get_turf(src)
/mob/proc/say_test(var/text)
var/ending = copytext(text, length(text))
if (ending == "?")
return "1"
else if (ending == "!")
return "2"
return "0"
/**
* Parses the message mode code (e.g. :h, :w) from text, such as that supplied to Say.
* Standard mode is the mode returned for the special ';' radio code.
*
* * message - the original string being passed
* * standard_mode - the message mode itself
*
* Returns the message mode string.
*/
/mob/proc/parse_message_mode(var/message, var/standard_mode="headset")
if(length(message) >= 1 && copytext(message,1,2) == ";")
return standard_mode
if(length(message) >= 2)
var/channel_prefix = copytext(message, 1 ,3)
return department_radio_keys[channel_prefix]
return null
/**
* Parses the language code (e.g. :j) from text, such as that supplied to say
*
* Returns a `/datum/language` only if the code corresponds to a language that src can speak, otherwise `null`
*
* * message - A string, the message to parse
*/
/mob/proc/parse_language(message)
SHOULD_NOT_SLEEP(TRUE)
SHOULD_BE_PURE(TRUE)
RETURN_TYPE(/datum/language)
var/prefix = copytext(message,1,2)
if(length(message) >= 1 && prefix == "!")
return GLOB.all_languages[LANGUAGE_NOISE]
//Check that the message is at least 2 characters long and is there's a prefix starting it
if(length(message) >= 2 && is_language_prefix(prefix))
//Get the first 2 letters after the prefix (position 2 and 3)
var/language_prefix = lowertext(copytext(message, 2, 4))
//Try to grab a language associated with said prefix
var/datum/language/L = GLOB.language_keys[language_prefix]
//If we didn't find a language, or we found one we cannot speak, try with a single letter identification
if(!istype(L) || (istype(L) && !can_speak(L)))
language_prefix = lowertext(copytext(message, 2, 3))
L = GLOB.language_keys[language_prefix]
//Check if we can speak the language, otherwise return null
if(istype(L) && can_speak(L))
return L
else
return null
/// Splits a message into per-language /datum/say_segment, switching language at each spoken prefix.
/// A forced language takes the whole message with no prefix parsing.
/mob/living/proc/build_say_message(message, datum/language/forced_language)
RETURN_TYPE(/datum/say_message)
var/datum/say_message/say_message = new
say_message.speaker = src
say_message.raw_message = message
if(forced_language)
say_message.collapse_to(forced_language, message)
return say_message
var/datum/language/current = get_default_language()
if(copytext(message, 1, 2) == "!")
current = GLOB.all_languages[LANGUAGE_NOISE]
message = trim_left(copytext(message, 2))
var/regex/trigger = get_language_trigger_regex()
trigger.next = 1
var/segment_start = 1
var/list/datum/say_segment/segments = list()
while(trigger.Find(message))
var/lead = trigger.group[1] //empty at the start of the message, otherwise the whitespace before the prefix
var/prefix_pos = trigger.index + length(lead)
var/key_pos = prefix_pos + 1
var/key_len = 2
var/datum/language/found = resolve_language_key(copytext(message, key_pos, key_pos + 2))
if(!found)
key_len = 1
found = resolve_language_key(copytext(message, key_pos, key_pos + 1))
if(!found)
continue //not a language we can speak, leave it as literal text and keep scanning
var/before = copytext(message, segment_start, prefix_pos)
var/after = key_pos + key_len
if(copytext(message, after, after + 1) == " ")
after++
// If LANG_NO_MULTILANG is present, it must be first. If first,
// set the whole message to that language. If not, skip it.
if(found.flags & LANG_NO_MULTILANG)
if(!length(segments) && !length(before))
say_message.collapse_to(found, copytext(message, after))
return say_message
continue
segments += new /datum/say_segment(before, current)
segment_start = after
current = found
trigger.next = after
segments += new /datum/say_segment(copytext(message, segment_start), current)
say_message.segments = clean_segments(segments)
say_message.single_language = (length(say_message.segments) == 1) ? say_message.segments[1].language : null
return say_message
/// Returns the language for a prefix key if we can speak it, otherwise null.
/mob/proc/resolve_language_key(key)
RETURN_TYPE(/datum/language)
if(!key)
return null
var/datum/language/found = GLOB.language_keys[lowertext(key)]
return (istype(found) && can_speak(found)) ? found : null
/// Drops empty segments and merges adjacent ones sharing a language.
/proc/clean_segments(list/datum/say_segment/segments)
var/list/datum/say_segment/cleaned = list()
for(var/datum/say_segment/segment as anything in segments)
if(!length(segment.text))
continue
var/datum/say_segment/last = cleaned.len ? cleaned[cleaned.len] : null
if(last && last.language == segment.language)
last.text += segment.text
else
cleaned += segment
return cleaned