From 1e478f64f0c94593c504ebf0fd08cb70687456c0 Mon Sep 17 00:00:00 2001 From: Inari Tommiska Date: Fri, 27 Jun 2025 01:53:43 +0300 Subject: [PATCH 1/2] generalize language string randomization Refactor the language string randomization to be a feature of the LanguageHander instead of being tied to the AdvancedDeathTracker. This allows re-using the same code for randomizing strings that are not death messages or funny notes. --- Coroner/AdvancedCauseOfDeath.cs | 25 +++++++++---------------- Coroner/LanguageHandler.cs | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Coroner/AdvancedCauseOfDeath.cs b/Coroner/AdvancedCauseOfDeath.cs index ecd34e4..254534e 100644 --- a/Coroner/AdvancedCauseOfDeath.cs +++ b/Coroner/AdvancedCauseOfDeath.cs @@ -251,34 +251,32 @@ public static string StringifyCauseOfDeath(AdvancedCauseOfDeath? causeOfDeath) public static string StringifyCauseOfDeath(AdvancedCauseOfDeath? causeOfDeath, Random random) { - var result = SelectCauseOfDeath(causeOfDeath); + var languageTag = SelectCauseOfDeathLanguageTag(causeOfDeath); - var shouldRandomize = result.Length > 1 && (causeOfDeath == null || !Plugin.Instance.PluginConfig.ShouldUseSeriousDeathMessages()); + var shouldRandomize = causeOfDeath == null || !Plugin.Instance.PluginConfig.ShouldUseSeriousDeathMessages(); if (shouldRandomize) { - var index = random.Next(result.Length); - return result[index]; + return Plugin.Instance.LanguageHandler.GetRandomValueByTag(languageTag, random); } else { - return result[0]; + // NOTE: First cause of death in the list should be the "serious" entry. + return Plugin.Instance.LanguageHandler.GetFirstValueByTag(languageTag); } } - public static string[] SelectCauseOfDeath(AdvancedCauseOfDeath? causeOfDeath) + public static string SelectCauseOfDeathLanguageTag(AdvancedCauseOfDeath? causeOfDeath) { - if (causeOfDeath == null) return Plugin.Instance.LanguageHandler.GetValuesByTag(LanguageHandler.TAG_FUNNY_NOTES); - - // NOTE: First cause of death in the list should be the "serious" entry. + if (causeOfDeath == null) return LanguageHandler.TAG_FUNNY_NOTES; if (AdvancedCauseOfDeath.IsCauseOfDeathRegistered(causeOfDeath)) { - return ((AdvancedCauseOfDeath)causeOfDeath).GetLanguageValues(); + return ((AdvancedCauseOfDeath)causeOfDeath).GetLanguageTag(); } else { - return Plugin.Instance.LanguageHandler.GetValuesByTag(LanguageHandler.TAG_DEATH_UNKNOWN); + return LanguageHandler.TAG_DEATH_UNKNOWN; } } } @@ -489,11 +487,6 @@ public string GetLanguageTag() return languageTag; } - public string[] GetLanguageValues() - { - return Plugin.Instance.LanguageHandler.GetValuesByTag(languageTag); - } - public override bool Equals(object obj) { return obj is AdvancedCauseOfDeath code && diff --git a/Coroner/LanguageHandler.cs b/Coroner/LanguageHandler.cs index ad1319f..07c4385 100644 --- a/Coroner/LanguageHandler.cs +++ b/Coroner/LanguageHandler.cs @@ -233,6 +233,21 @@ public string GetFirstValueByTag(string tag) return GetValuesByTag(tag)[0]; } + public string GetRandomValueByTag(string tag, Random random) + { + var result = GetValuesByTag(tag); + + // NOTE: obtaining a random number is skipped if there is only one result. This may be relavant for synced randoms. + if (result.Length > 1) + { + var index = random.Next(result.Length); + return result[index]; + } + + // NOTE: Assumes GetValuesByTag always returns a non-empty array. + return result[0]; + } + public string[] GetValuesByTag(string tag) { if (languageData.Count == 0 && languageCode != DEFAULT_LANGUAGE) From c79542684dd8315b60e60fe54a1775f74f715c50 Mon Sep 17 00:00:00 2001 From: Inari Tommiska Date: Fri, 27 Jun 2025 01:58:27 +0300 Subject: [PATCH 2/2] allow randomizing the first line of the report Tweak the report generating code to pick a random first line for the report, in case there are multiple available. This makes the reports slightly more customizable. I am likely going to use this as a hack to get rare alternative titles for the reports by e.g. copy-pasting the default "Cause of death" a few times in the xml file and adding rare variants after those. No need to implement e.g. weighted random or anything more sophisticated, this works good enough. --- Coroner/Patch/HUDManagerPatch.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Coroner/Patch/HUDManagerPatch.cs b/Coroner/Patch/HUDManagerPatch.cs index 911db84..5d52241 100644 --- a/Coroner/Patch/HUDManagerPatch.cs +++ b/Coroner/Patch/HUDManagerPatch.cs @@ -59,7 +59,7 @@ static void OverridePerformanceReport(HUDManager __instance) { Plugin.Instance.PluginLogger.LogInfo("[REPORT] Player " + playerIndex + " is dead! Replacing notes with Cause of Death..."); // Reset the notes. - textMesh.text = Plugin.Instance.LanguageHandler.GetFirstValueByTag(LanguageHandler.TAG_UI_DEATH) + "\n"; + textMesh.text = CreateMessagePrefixFromTag(LanguageHandler.TAG_UI_DEATH, syncedRandom) + "\n"; } else { @@ -82,7 +82,7 @@ static void OverridePerformanceReport(HUDManager __instance) { Plugin.Instance.PluginLogger.LogInfo("[REPORT] Player " + playerIndex + " has no notes! Injecting something funny..."); - textMesh.text = Plugin.Instance.LanguageHandler.GetFirstValueByTag(LanguageHandler.TAG_UI_NOTES) + "\n"; + textMesh.text = CreateMessagePrefixFromTag(LanguageHandler.TAG_UI_NOTES, syncedRandom) + "\n"; textMesh.text += "* " + AdvancedDeathTracker.StringifyCauseOfDeath(null, syncedRandom) + "\n"; } else @@ -100,5 +100,16 @@ static void OverridePerformanceReport(HUDManager __instance) // We are done with the death tracker, so clear it. AdvancedDeathTracker.ClearDeathTracker(); } + + private static string CreateMessagePrefixFromTag(string prefixLanguageTag, Random random) + { + if (Plugin.Instance.PluginConfig.ShouldUseSeriousDeathMessages()) + { + // NOTE: First entry in the list should be the "serious" entry. + return Plugin.Instance.LanguageHandler.GetFirstValueByTag(prefixLanguageTag); + } + + return Plugin.Instance.LanguageHandler.GetRandomValueByTag(prefixLanguageTag, random); + } } } \ No newline at end of file