From 9ee1af64bc69067e9653cf579385bfd0f0ae251e Mon Sep 17 00:00:00 2001 From: Seris02 <49109742+Seris02@users.noreply.github.com> Date: Tue, 1 Aug 2023 14:29:52 +0800 Subject: [PATCH] improves language scrambling (#6697) --- code/modules/mob/hear_say.dm | 2 +- code/modules/mob/language/language.dm | 2 + .../code/modules/mob/language/language.dm | 110 ++++++++++++++++++ vorestation.dme | 1 + 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 modular_chomp/code/modules/mob/language/language.dm diff --git a/code/modules/mob/hear_say.dm b/code/modules/mob/hear_say.dm index 91d955f81a..6e2b08bdce 100644 --- a/code/modules/mob/hear_say.dm +++ b/code/modules/mob/hear_say.dm @@ -58,7 +58,7 @@ /mob/proc/saypiece_scramble(datum/multilingual_say_piece/SP) if(SP.speaking) - return SP.speaking.scramble(SP.message) + return SP.speaking.scramble(SP.message, languages) //CHOMPEdit: fix for partial understanding else return stars(SP.message) diff --git a/code/modules/mob/language/language.dm b/code/modules/mob/language/language.dm index 5ab9416da3..8d596422a0 100644 --- a/code/modules/mob/language/language.dm +++ b/code/modules/mob/language/language.dm @@ -44,6 +44,7 @@ /datum/language var/list/scramble_cache = list() +/* CHOMPEdit: moved to modular_chomp because it was edited so much. /datum/language/proc/scramble(var/input, var/list/known_languages) var/understand_chance = 0 for(var/datum/language/L in known_languages) @@ -113,6 +114,7 @@ scramble_cache.Cut(1, scramble_cache.len-SCRAMBLE_CACHE_LEN-1) return scrambled_text +*/ /datum/language/proc/format_message(message, verb) return "[message]" diff --git a/modular_chomp/code/modules/mob/language/language.dm b/modular_chomp/code/modules/mob/language/language.dm new file mode 100644 index 0000000000..965baa26c6 --- /dev/null +++ b/modular_chomp/code/modules/mob/language/language.dm @@ -0,0 +1,110 @@ +#define SCRAMBLE_CACHE_LEN 40 +//basically, how many words can it store that it remembers to use? Upped from 20 to 40, because 20 is very s m a l l + +/datum/language/proc/partial_scramble(var/input, var/capitalize = FALSE) + if(!syllables || !syllables.len) + return capitalize ? capitalize(input) : input + + var/static/regex/punctuation = new/regex(@"([.,?!-]+)$") + var/static/regex/prefixpunct = new/regex(@"^([.,?!-]+)") + var/found = punctuation.Find(input) + var/input_ending = "" + if (found) + input_ending = punctuation.match + input = copytext_char(input, 1, found) + var/foundprefix = prefixpunct.Find(input) + var/input_start = "" + if (foundprefix) + input_start = prefixpunct.match + input = copytext_char(input, 1+length(prefixpunct.match)) + + var/input_size = length(input) + var/picked = pick(syllables) + + if (length(picked) < input_size) + var/pos = rand(1, input_size-length(picked)) + input = splicetext_char(input, pos, pos+length(picked), picked) + + if (capitalize) + input = capitalize(input) + + return input_start + input + input_ending + +/datum/language/proc/scramble(var/input, var/list/known_languages) + var/understand_chance = 0 + for(var/datum/language/L in known_languages) + if(partial_understanding && partial_understanding[L.name]) + understand_chance += partial_understanding[L.name] + if(L.partial_understanding && L.partial_understanding[name]) + understand_chance += L.partial_understanding[name] * 0.5 + var/scrambled_text = "" + var/list/words = splittext(input, " ") + for(var/w in words) + if(w == "") continue + var/nword = w + var/ending = copytext(scrambled_text, length(scrambled_text)-1) + var/capitalize = FALSE + if(findtext(ending,".") || findtext(ending,"!") || findtext(ending,"?")) + capitalize = TRUE + if(prob(understand_chance)) + if (prob(100-(understand_chance*1.5))) + nword = partial_scramble(w, capitalize) + else + nword = scramble_word(w, capitalize) + scrambled_text += " " + nword + var/static/regex/commadrift = new/regex(@"(\w) +([,?!.]+[^\w]*)", "g") //basically captures two groups- the last letter of a word, and any punctuation that comes after [one or more] grammatically incorrect spaces. Such as "Once ?", it captures "e" and "?" + var/static/regex/multispace = new/regex(@" +", "g") + scrambled_text = multispace.Replace(scrambled_text, " ") + scrambled_text = trim(scrambled_text) + scrambled_text = capitalize(scrambled_text) + scrambled_text = commadrift.Replace(scrambled_text, "$1$2") //with the above example, "Once ?" will be turned into "Once?" with this replacement - this is here because of weird issues that happen when scrambling sometimes + + return scrambled_text + +/datum/language/proc/scramble_word(var/input, var/capitalize = FALSE) + if(!syllables || !syllables.len) + return capitalize ? capitalize(stars(input)) : stars(input) + + var/static/regex/punctuation = new/regex(@"([.,?!-]+)$") + var/static/regex/prefixpunct = new/regex(@"^([.,?!-]+)") + var/found = punctuation.Find(input) + var/input_ending = "" + if (found) + input_ending = punctuation.match + input = copytext_char(input, 1, found) + var/foundprefix = prefixpunct.Find(input) + var/input_start = "" + if (foundprefix) + input_start = prefixpunct.match + input = copytext_char(input, 1+length(prefixpunct.match)) + + // If the input is cached already, move it to the end of the cache and return it + var/k = lowertext(input) + if(k in scramble_cache) + var/n = scramble_cache[k] + scramble_cache -= input + scramble_cache[k] = n + if (capitalize) + n = capitalize(n) + return input_start + n + input_ending + + var/input_size = length(input) + var/scrambled_text = "" + + while(length(scrambled_text) < input_size) + var/next = pick(syllables) + scrambled_text += next + if(rand(1,100) <= space_chance && length(scrambled_text) + 1 < input_size) + scrambled_text += " " + + // Add it to cache, cutting old entries if the list is too long + scramble_cache[k] = scrambled_text + if(scramble_cache.len > SCRAMBLE_CACHE_LEN) + scramble_cache.Cut(1, scramble_cache.len-SCRAMBLE_CACHE_LEN+1) + + if (capitalize) + scrambled_text = capitalize(scrambled_text) + + return input_start + scrambled_text + input_ending + +#undef SCRAMBLE_CACHE_LEN diff --git a/vorestation.dme b/vorestation.dme index 41fc14ca8c..8288eff6cd 100644 --- a/vorestation.dme +++ b/vorestation.dme @@ -4635,6 +4635,7 @@ #include "modular_chomp\code\modules\mob\holder.dm" #include "modular_chomp\code\modules\mob\mob.dm" #include "modular_chomp\code\modules\mob\dead\observer\observer.dm" +#include "modular_chomp\code\modules\mob\language\language.dm" #include "modular_chomp\code\modules\mob\living\emote.dm" #include "modular_chomp\code\modules\mob\living\living.dm" #include "modular_chomp\code\modules\mob\living\carbon\carbon.dm"