Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions Coroner/AdvancedCauseOfDeath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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 &&
Expand Down
15 changes: 15 additions & 0 deletions Coroner/LanguageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Comment on lines +240 to +248

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left the superfluous length check in to better match the old behaviour, just in case there is some effect to synced randoms that I missed.

If skipping the random isn't critical for synced randoms, then as we anyway assume that GetValuesByTag returns a non-empty array, this could be simplified down to just:

var index = random.Next(result.Length);
return result[index];

}

public string[] GetValuesByTag(string tag)
{
if (languageData.Count == 0 && languageCode != DEFAULT_LANGUAGE)
Expand Down
15 changes: 13 additions & 2 deletions Coroner/Patch/HUDManagerPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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);
}
}
}