1124 lines
33 KiB
C#
1124 lines
33 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace IdleSpectator;
|
|
|
|
/// <summary>
|
|
/// Snapshot of why a living unit is interesting (caption + chronicle source).
|
|
/// </summary>
|
|
public sealed class UnitDossier
|
|
{
|
|
public long UnitId;
|
|
public string Name = "";
|
|
public string SpeciesId = "";
|
|
public string Headline = "";
|
|
public string ReasonLine = "";
|
|
public string DetailLine = "";
|
|
public string CaptionText = "";
|
|
public string JobLabel = "";
|
|
public float Score;
|
|
public int Kills;
|
|
public int Age;
|
|
public int Level;
|
|
public int Renown;
|
|
public bool IsFavorite;
|
|
public bool IsKing;
|
|
public bool IsCityLeader;
|
|
public bool IsFighting;
|
|
public bool IsSpectacle;
|
|
public bool IsLoneSpecies;
|
|
public bool IsMale;
|
|
public bool IsFemale;
|
|
public string TaskText = "";
|
|
public string KingdomName = "";
|
|
public string CityName = "";
|
|
public readonly List<string> ScoreReasons = new List<string>();
|
|
public readonly List<TraitChip> TopTraits = new List<TraitChip>();
|
|
|
|
public sealed class TraitChip
|
|
{
|
|
public string Id = "";
|
|
public string Name = "";
|
|
public ActorTrait Trait;
|
|
}
|
|
|
|
/// <summary>Harness: force JobLabel on the next dossier build (no live citizen_job).</summary>
|
|
public static string HarnessJobLabelOverride = "";
|
|
|
|
/// <summary>Harness: optional watch-reason fragment used with <see cref="HarnessJobLabelOverride"/>.</summary>
|
|
public static string HarnessReasonOverride = "";
|
|
|
|
public static void ClearHarnessOverrides()
|
|
{
|
|
HarnessJobLabelOverride = "";
|
|
HarnessReasonOverride = "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Minimal dossier for archive subjects with no living unit (harness orphans / legacy deaths).
|
|
/// </summary>
|
|
public static UnitDossier FromFallenArchive(
|
|
long unitId,
|
|
string name,
|
|
string speciesId,
|
|
DeathManner manner = DeathManner.None)
|
|
{
|
|
UnitDossier d = new UnitDossier();
|
|
d.UnitId = unitId;
|
|
d.Name = string.IsNullOrEmpty(name) ? "Nameless" : name;
|
|
d.SpeciesId = string.IsNullOrEmpty(speciesId) ? "creature" : speciesId;
|
|
d.Headline = string.IsNullOrEmpty(d.SpeciesId)
|
|
? d.Name
|
|
: $"{d.Name} ({d.SpeciesId})";
|
|
string mannerLabel = Chronicle.DeathMannerLabel(manner);
|
|
d.ReasonLine = string.IsNullOrEmpty(mannerLabel) || manner == DeathManner.None
|
|
? "Fallen"
|
|
: char.ToUpperInvariant(mannerLabel[0]) + mannerLabel.Substring(1);
|
|
d.DetailLine = "";
|
|
d.CaptionText = JoinCaption(d.Headline, d.ReasonLine, d.DetailLine);
|
|
return d;
|
|
}
|
|
|
|
public static UnitDossier FromActor(Actor actor, string watchLabel = null)
|
|
{
|
|
UnitDossier d = new UnitDossier();
|
|
if (actor == null || !actor.isAlive())
|
|
{
|
|
d.Headline = "Nobody";
|
|
d.ReasonLine = "";
|
|
d.DetailLine = "no living unit";
|
|
d.CaptionText = d.Headline;
|
|
return d;
|
|
}
|
|
|
|
Dictionary<string, int> speciesCounts = WorldActivityScanner.CountSpeciesPopulations();
|
|
d.UnitId = actor.getID();
|
|
d.Name = SafeName(actor);
|
|
d.SpeciesId = actor.asset != null ? actor.asset.id : "creature";
|
|
d.Kills = actor.data != null ? actor.data.kills : 0;
|
|
d.Age = SafeAge(actor);
|
|
d.Level = SafeLevel(actor);
|
|
d.Renown = actor.renown;
|
|
d.IsFavorite = actor.isFavorite();
|
|
d.IsKing = actor.isKing();
|
|
d.IsCityLeader = actor.isCityLeader();
|
|
d.IsFighting = actor.has_attack_target;
|
|
d.IsSpectacle = WorldActivityScanner.IsSpectaclePublic(actor);
|
|
d.IsLoneSpecies = WorldActivityScanner.IsSingletonSpeciesPublic(actor, speciesCounts);
|
|
d.KingdomName = actor.kingdom != null ? actor.kingdom.name : "";
|
|
d.CityName = actor.city != null ? actor.city.name : "";
|
|
d.Score = WorldActivityScanner.ScoreActorPublic(actor, speciesCounts);
|
|
FillSex(d, actor);
|
|
d.TaskText = SafeTask(actor);
|
|
FillTopTraits(d, actor);
|
|
CollectReasons(d, actor, speciesCounts);
|
|
d.JobLabel = ResolveJobLabel(actor);
|
|
|
|
// Species icon is shown separately; keep id in the title for scanability.
|
|
d.Headline = string.IsNullOrEmpty(d.SpeciesId)
|
|
? d.Name
|
|
: $"{d.Name} ({d.SpeciesId})";
|
|
d.ReasonLine = BuildStoryBeat(d, watchLabel, actor);
|
|
d.DetailLine = BuildDetailLine(d);
|
|
d.CaptionText = JoinCaption(d.Headline, d.ReasonLine, d.DetailLine);
|
|
return d;
|
|
}
|
|
|
|
private static string ResolveJobLabel(Actor actor)
|
|
{
|
|
if (!string.IsNullOrEmpty(HarnessJobLabelOverride))
|
|
{
|
|
return HarnessJobLabelOverride.Trim();
|
|
}
|
|
|
|
string jobId = ActivityInterestTable.SafeJobId(actor);
|
|
return ActivityProse.HumanizeJobPublic(jobId);
|
|
}
|
|
|
|
public bool ContainsIgnoreCase(string needle)
|
|
{
|
|
if (string.IsNullOrEmpty(needle))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if ((CaptionText ?? "").IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| (Headline ?? "").IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| (ReasonLine ?? "").IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| (DetailLine ?? "").IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| (Name ?? "").IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| (SpeciesId ?? "").IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| (TaskText ?? "").IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
// Harness probes "age" after we moved age onto an icon chip.
|
|
if (needle.Equals("age", System.StringComparison.OrdinalIgnoreCase) && Age > 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
for (int i = 0; i < TopTraits.Count; i++)
|
|
{
|
|
TraitChip chip = TopTraits[i];
|
|
if (chip == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if ((!string.IsNullOrEmpty(chip.Name)
|
|
&& chip.Name.IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0)
|
|
|| (!string.IsNullOrEmpty(chip.Id)
|
|
&& chip.Id.IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static string JoinCaption(string headline, string reason, string detail)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
if (!string.IsNullOrEmpty(headline))
|
|
{
|
|
sb.Append(headline);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(reason))
|
|
{
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.Append('\n');
|
|
}
|
|
|
|
sb.Append(reason);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(detail))
|
|
{
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.Append('\n');
|
|
}
|
|
|
|
sb.Append(detail);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Orange dossier reason: tracked event that earned this shot.
|
|
/// Empty when no owned active candidate (never invent from live fighting/job).
|
|
/// </summary>
|
|
private static string BuildStoryBeat(UnitDossier d, string watchLabel, Actor actor)
|
|
{
|
|
if (!string.IsNullOrEmpty(HarnessReasonOverride))
|
|
{
|
|
return HarnessReasonOverride.Trim();
|
|
}
|
|
|
|
return OwnedEventReason(actor, d);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Public so caption can refresh reason when the director leaves active dwell
|
|
/// (quiet_grace) without rebuilding the whole dossier.
|
|
/// </summary>
|
|
public static string OwnedEventReason(Actor actor, UnitDossier dossier = null)
|
|
{
|
|
if (actor == null || !actor.isAlive())
|
|
{
|
|
return "";
|
|
}
|
|
|
|
InterestCandidate scene = InterestDirector.TryGetOwnedReasonCandidate(actor);
|
|
if (scene == null)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
// Character-fill vignettes: hide reason (task chip shows live activity).
|
|
if (scene.LeadKind == InterestLeadKind.CharacterLed
|
|
&& !InterestScoring.IsCombatAction(scene))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
UnitDossier d = dossier;
|
|
if (d == null)
|
|
{
|
|
d = new UnitDossier();
|
|
try
|
|
{
|
|
d.UnitId = actor.getID();
|
|
d.Name = SafeName(actor);
|
|
d.SpeciesId = actor.asset != null ? actor.asset.id : "";
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
return SceneLabelBeat(scene, d) ?? "";
|
|
}
|
|
|
|
private static string SceneLabelBeat(InterestCandidate scene, UnitDossier d)
|
|
{
|
|
if (scene == null || string.IsNullOrEmpty(scene.Label))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
string beat = EventLabelBeat(scene.Label, d);
|
|
if (string.IsNullOrEmpty(beat))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
// Ownership: never let the reason name a living stranger (not subject/related).
|
|
if (ReasonNamesStranger(beat, scene))
|
|
{
|
|
InterestDropLog.Record("identity_filter", "stranger:" + beat);
|
|
return "";
|
|
}
|
|
|
|
// Ownership: reject beats that lead with someone else's proper name.
|
|
if (LeadsWithForeignName(beat, scene))
|
|
{
|
|
InterestDropLog.Record("identity_filter", "foreign:" + beat);
|
|
return "";
|
|
}
|
|
|
|
return beat;
|
|
}
|
|
|
|
private static bool LeadsWithForeignName(string reason, InterestCandidate scene)
|
|
{
|
|
if (string.IsNullOrEmpty(reason) || scene == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string first = FirstToken(reason);
|
|
if (string.IsNullOrEmpty(first) || IsOwnedBeatKeyword(first))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string subjectName = EventFeedUtil.SafeName(scene.FollowUnit);
|
|
string relatedName = EventFeedUtil.SafeName(scene.RelatedUnit);
|
|
if (!string.IsNullOrEmpty(subjectName)
|
|
&& first.Equals(FirstToken(subjectName), System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(relatedName)
|
|
&& first.Equals(FirstToken(relatedName), System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Leading token looks like a proper name that is not the owned subject/related.
|
|
return first.Length >= 3 && char.IsLetter(first[0]);
|
|
}
|
|
|
|
private static string FirstToken(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
int i = 0;
|
|
while (i < text.Length && (char.IsWhiteSpace(text[i]) || text[i] == ':' || text[i] == '·'))
|
|
{
|
|
i++;
|
|
}
|
|
|
|
int start = i;
|
|
while (i < text.Length && (char.IsLetterOrDigit(text[i]) || text[i] == '_' || text[i] == '-'))
|
|
{
|
|
i++;
|
|
}
|
|
|
|
return start < i ? text.Substring(start, i - start) : "";
|
|
}
|
|
|
|
private static bool IsOwnedBeatKeyword(string token)
|
|
{
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
switch (token.ToLowerInvariant())
|
|
{
|
|
case "fighting":
|
|
case "war":
|
|
case "battle":
|
|
case "clash":
|
|
case "grief":
|
|
case "mourning":
|
|
case "mourn":
|
|
case "slain":
|
|
case "lovers":
|
|
case "lover":
|
|
case "new":
|
|
case "baby":
|
|
case "book":
|
|
case "building":
|
|
case "boat":
|
|
case "meta":
|
|
case "seeking":
|
|
case "joins":
|
|
case "leaves":
|
|
case "family":
|
|
case "starts":
|
|
case "gathers":
|
|
case "damages":
|
|
case "consumes":
|
|
case "spectacle":
|
|
case "in":
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static bool ReasonNamesStranger(string reason, InterestCandidate scene)
|
|
{
|
|
if (string.IsNullOrEmpty(reason) || scene == null || World.world?.units == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string subjectName = EventFeedUtil.SafeName(scene.FollowUnit);
|
|
string relatedName = EventFeedUtil.SafeName(scene.RelatedUnit);
|
|
try
|
|
{
|
|
foreach (Actor unit in World.world.units)
|
|
{
|
|
if (unit == null || !unit.isAlive())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (unit == scene.FollowUnit || unit == scene.RelatedUnit)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string name = EventFeedUtil.SafeName(unit);
|
|
// Length 3 catches English crumbs in reasons ("egg", "war", "duel").
|
|
if (string.IsNullOrEmpty(name) || name.Length < 4)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(subjectName)
|
|
&& name.Equals(subjectName, System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(relatedName)
|
|
&& name.Equals(relatedName, System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (ContainsWholeName(reason, name))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>True when <paramref name="name"/> appears as a whole token in <paramref name="reason"/>.</summary>
|
|
private static bool ContainsWholeName(string reason, string name)
|
|
{
|
|
if (string.IsNullOrEmpty(reason) || string.IsNullOrEmpty(name))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int start = 0;
|
|
while (start <= reason.Length - name.Length)
|
|
{
|
|
int idx = reason.IndexOf(name, start, System.StringComparison.OrdinalIgnoreCase);
|
|
if (idx < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool leftOk = idx == 0 || !char.IsLetterOrDigit(reason[idx - 1]);
|
|
int end = idx + name.Length;
|
|
bool rightOk = end >= reason.Length || !char.IsLetterOrDigit(reason[end]);
|
|
if (leftOk && rightOk)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
start = idx + 1;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static string EventLabelBeat(string label, UnitDossier d)
|
|
{
|
|
if (string.IsNullOrEmpty(label))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
string s = label.Trim();
|
|
if (s.StartsWith("Watching [", System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
int close = s.IndexOf(']');
|
|
if (close > 0 && close + 1 < s.Length)
|
|
{
|
|
s = s.Substring(close + 1).TrimStart(':', ' ');
|
|
}
|
|
}
|
|
|
|
// EventReason / Name-verb sentences are dossier-ready.
|
|
if (LooksLikeEventSentence(s)
|
|
|| (!string.IsNullOrEmpty(d?.Name) && StartsWithSubjectVerbPhrase(s, d.Name)))
|
|
{
|
|
if (IsWeakFlavorBeat(s, d))
|
|
{
|
|
InterestDropLog.Record("identity_filter", "weak:" + s);
|
|
return "";
|
|
}
|
|
|
|
return CleanBeat(s);
|
|
}
|
|
|
|
if (IsIdentityWhy(s, d) || IsWeakFlavorBeat(s, d))
|
|
{
|
|
InterestDropLog.Record("identity_filter", s);
|
|
return "";
|
|
}
|
|
|
|
return CleanBeat(s);
|
|
}
|
|
|
|
private static bool IsWeakFlavorBeat(string text, UnitDossier d)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
string t = text.Trim();
|
|
if (t.StartsWith("Lone ", System.StringComparison.OrdinalIgnoreCase)
|
|
|| t.Equals("Lone of kind", System.StringComparison.OrdinalIgnoreCase)
|
|
|| t.Equals("lone of kind", System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (d != null
|
|
&& !string.IsNullOrEmpty(d.SpeciesId)
|
|
&& t.IndexOf(d.SpeciesId, System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
&& t.IndexOf("Fight", System.StringComparison.OrdinalIgnoreCase) < 0
|
|
&& t.IndexOf("War", System.StringComparison.OrdinalIgnoreCase) < 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static string CleanBeat(string text)
|
|
{
|
|
return string.IsNullOrEmpty(text) ? "" : text.Trim();
|
|
}
|
|
|
|
private static bool IsIdentityWhy(string text, UnitDossier d)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
string t = text.Trim();
|
|
if (t.StartsWith("King ", System.StringComparison.OrdinalIgnoreCase)
|
|
|| t.StartsWith("King of ", System.StringComparison.OrdinalIgnoreCase)
|
|
|| t.StartsWith("Leader ", System.StringComparison.OrdinalIgnoreCase)
|
|
|| t.StartsWith("Leader of ", System.StringComparison.OrdinalIgnoreCase)
|
|
|| t.StartsWith("Favorite", System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
// Pure title leftovers after ShortenWatchLabel ("King", "Leader", "Favorite").
|
|
if (t.Equals("King", System.StringComparison.OrdinalIgnoreCase)
|
|
|| t.Equals("Leader", System.StringComparison.OrdinalIgnoreCase)
|
|
|| t.Equals("Favorite", System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (d != null
|
|
&& !string.IsNullOrEmpty(d.Name)
|
|
&& t.IndexOf(d.Name, System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
&& t.IndexOf("Fight", System.StringComparison.OrdinalIgnoreCase) < 0
|
|
&& t.IndexOf("War", System.StringComparison.OrdinalIgnoreCase) < 0
|
|
&& t.IndexOf("mourn", System.StringComparison.OrdinalIgnoreCase) < 0
|
|
&& t.IndexOf("New species", System.StringComparison.OrdinalIgnoreCase) < 0)
|
|
{
|
|
// EventReason sentences lead with the subject name - keep them.
|
|
if (LooksLikeEventSentence(t) || StartsWithSubjectVerbPhrase(t, d.Name))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// "Isemward (human, 12 kills)" style still useful via kills - keep if kills mentioned.
|
|
if (t.IndexOf("kill", System.StringComparison.OrdinalIgnoreCase) >= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return IsRedundantWithHeadline(t, d);
|
|
}
|
|
|
|
private static bool LooksLikeEventSentence(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string t = text;
|
|
return t.IndexOf(" is ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" became ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" joins ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" leaves ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" forms ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" gains ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" loses ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" writes ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" founds ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" summons ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" begins ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" ends ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" burns ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" welcomes ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" parted ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" decides ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" casts ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" forges ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" eating ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" foraging ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" damaging ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" plotting ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" lovers ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" hatches ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" mates ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" appears ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" finishes ", System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| t.IndexOf(" bloodlust ", System.StringComparison.OrdinalIgnoreCase) >= 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// "Name verb …" EventReason form (covers hatches/mates/appears without an "is" helper).
|
|
/// Rejects identity crumbs like "Name (species, …)".
|
|
/// </summary>
|
|
private static bool StartsWithSubjectVerbPhrase(string text, string name)
|
|
{
|
|
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(name))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!text.StartsWith(name, System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int i = name.Length;
|
|
if (i >= text.Length || text[i] != ' ')
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int verbStart = i + 1;
|
|
if (verbStart >= text.Length)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
char c = text[verbStart];
|
|
return char.IsLetter(c) && c != '(';
|
|
}
|
|
|
|
private static bool IsShownTraitName(UnitDossier d, string text)
|
|
{
|
|
if (d == null || string.IsNullOrEmpty(text))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < d.TopTraits.Count; i++)
|
|
{
|
|
TraitChip chip = d.TopTraits[i];
|
|
if (chip == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if ((!string.IsNullOrEmpty(chip.Name)
|
|
&& string.Equals(text, chip.Name, System.StringComparison.OrdinalIgnoreCase))
|
|
|| (!string.IsNullOrEmpty(chip.Id)
|
|
&& string.Equals(text, Humanize(chip.Id), System.StringComparison.OrdinalIgnoreCase)))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static bool IsRedundantWithHeadline(string text, UnitDossier d)
|
|
{
|
|
if (string.IsNullOrEmpty(text) || d == null)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (string.Equals(text, d.Headline, System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (string.Equals(text, d.Name, System.StringComparison.OrdinalIgnoreCase)
|
|
|| string.Equals(text, d.SpeciesId, System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
// "Name (species)" variants already covered by headline.
|
|
if (!string.IsNullOrEmpty(d.Name) && !string.IsNullOrEmpty(d.SpeciesId)
|
|
&& text.IndexOf(d.Name, System.StringComparison.OrdinalIgnoreCase) >= 0
|
|
&& text.IndexOf(d.SpeciesId, System.StringComparison.OrdinalIgnoreCase) >= 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static string ShortenWatchLabel(string label, UnitDossier d)
|
|
{
|
|
if (string.IsNullOrEmpty(label))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
string s = label.Trim();
|
|
if (s.StartsWith("New species:", System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return "New species";
|
|
}
|
|
|
|
if (s.StartsWith("Watching [", System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
int close = s.IndexOf(']');
|
|
if (close > 0 && close + 1 < s.Length)
|
|
{
|
|
s = s.Substring(close + 1).TrimStart(':', ' ');
|
|
}
|
|
}
|
|
|
|
// Drop trailing "Name" duplicates like "Lone wolf: Waf" -> keep short form.
|
|
int colon = s.IndexOf(':');
|
|
if (colon > 0 && colon < 28)
|
|
{
|
|
s = s.Substring(0, colon).Trim();
|
|
}
|
|
|
|
if (d != null && IsRedundantWithHeadline(s, d))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
return s;
|
|
}
|
|
|
|
private static void CollectReasons(UnitDossier d, Actor actor, Dictionary<string, int> speciesCounts)
|
|
{
|
|
if (d.IsFavorite)
|
|
{
|
|
d.ScoreReasons.Add("favorite");
|
|
}
|
|
|
|
if (d.IsKing)
|
|
{
|
|
d.ScoreReasons.Add("king");
|
|
}
|
|
else if (d.IsCityLeader)
|
|
{
|
|
d.ScoreReasons.Add("leader");
|
|
}
|
|
else if (actor.is_profession_warrior)
|
|
{
|
|
d.ScoreReasons.Add("warrior");
|
|
}
|
|
|
|
if (d.IsFighting)
|
|
{
|
|
d.ScoreReasons.Add("fighting");
|
|
}
|
|
|
|
if (d.Kills >= 100)
|
|
{
|
|
d.ScoreReasons.Add("100+ kills");
|
|
}
|
|
else if (d.Kills >= 50)
|
|
{
|
|
d.ScoreReasons.Add("50+ kills");
|
|
}
|
|
else if (d.Kills >= 10)
|
|
{
|
|
d.ScoreReasons.Add("10+ kills");
|
|
}
|
|
|
|
if (d.IsLoneSpecies)
|
|
{
|
|
d.ScoreReasons.Add("lone of kind");
|
|
}
|
|
|
|
if (d.IsSpectacle)
|
|
{
|
|
d.ScoreReasons.Add("spectacle");
|
|
}
|
|
|
|
if (d.Renown >= 100)
|
|
{
|
|
d.ScoreReasons.Add("renown " + d.Renown);
|
|
}
|
|
|
|
// Trait names are shown as dossier chips; keep ScoreReasons for non-trait flavor only.
|
|
}
|
|
|
|
private static string BuildDetailLine(UnitDossier d)
|
|
{
|
|
// Compact text fallback for logs/harness: task + optional kingdom.
|
|
StringBuilder sb = new StringBuilder();
|
|
if (!string.IsNullOrEmpty(d.TaskText))
|
|
{
|
|
sb.Append(d.TaskText);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(d.KingdomName)
|
|
&& !string.Equals(d.KingdomName, d.SpeciesId, System.StringComparison.OrdinalIgnoreCase)
|
|
&& !string.Equals(d.KingdomName, d.Name, System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.Append(" · ");
|
|
}
|
|
|
|
sb.Append(d.KingdomName);
|
|
}
|
|
else if (!string.IsNullOrEmpty(d.CityName)
|
|
&& !string.Equals(d.CityName, d.SpeciesId, System.StringComparison.OrdinalIgnoreCase)
|
|
&& !string.Equals(d.CityName, d.Name, System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.Append(" · ");
|
|
}
|
|
|
|
sb.Append(d.CityName);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static void FillSex(UnitDossier d, Actor actor)
|
|
{
|
|
try
|
|
{
|
|
d.IsMale = actor.isSexMale();
|
|
d.IsFemale = actor.isSexFemale();
|
|
}
|
|
catch
|
|
{
|
|
d.IsMale = false;
|
|
d.IsFemale = false;
|
|
}
|
|
}
|
|
|
|
private static void FillTopTraits(UnitDossier d, Actor actor)
|
|
{
|
|
d.TopTraits.Clear();
|
|
try
|
|
{
|
|
if (!actor.hasTraits())
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Pick the most interesting traits: rarity first, then non-default / dramatic.
|
|
List<ActorTrait> ranked = new List<ActorTrait>();
|
|
foreach (ActorTrait trait in actor.getTraits())
|
|
{
|
|
if (trait == null || string.IsNullOrEmpty(trait.id))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
ranked.Add(trait);
|
|
}
|
|
|
|
ranked.Sort((a, b) => TraitInterestScore(b, actor).CompareTo(TraitInterestScore(a, actor)));
|
|
|
|
for (int i = 0; i < ranked.Count && d.TopTraits.Count < 3; i++)
|
|
{
|
|
ActorTrait trait = ranked[i];
|
|
d.TopTraits.Add(new TraitChip
|
|
{
|
|
Id = trait.id,
|
|
Name = TraitDisplayName(trait),
|
|
Trait = trait
|
|
});
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore trait access failures on exotic units
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spectator-facing interest: Legendary/Epic over Normal, non-default over species baseline,
|
|
/// Positive/Negative over bland Other. Ties break on rarer birth rates.
|
|
/// </summary>
|
|
private static int TraitInterestScore(ActorTrait trait, Actor actor)
|
|
{
|
|
if (trait == null)
|
|
{
|
|
return int.MinValue;
|
|
}
|
|
|
|
int score = 0;
|
|
try
|
|
{
|
|
score += (int)trait.rarity * 100;
|
|
}
|
|
catch
|
|
{
|
|
// older builds without rarity
|
|
}
|
|
|
|
try
|
|
{
|
|
if (trait.type == TraitType.Negative)
|
|
{
|
|
score += 25;
|
|
}
|
|
else if (trait.type == TraitType.Positive)
|
|
{
|
|
score += 15;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
|
|
if (!IsDefaultTraitForActor(trait, actor))
|
|
{
|
|
score += 50;
|
|
}
|
|
else
|
|
{
|
|
score -= 40;
|
|
}
|
|
|
|
try
|
|
{
|
|
// Low birth rate = unusual to spawn with; more interesting on a watch card.
|
|
if (trait.rate_birth > 0 && trait.rate_birth <= 5)
|
|
{
|
|
score += 20;
|
|
}
|
|
else if (trait.rate_birth >= 40)
|
|
{
|
|
score -= 15;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
|
|
string id = trait.id ?? "";
|
|
if (id.IndexOf("miracle", StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| id.IndexOf("immortal", StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| id.IndexOf("zombie", StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| id.IndexOf("plague", StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| id.IndexOf("blessed", StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| id.IndexOf("cursed", StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| id.IndexOf("king", StringComparison.OrdinalIgnoreCase) >= 0
|
|
|| id.IndexOf("evil", StringComparison.OrdinalIgnoreCase) >= 0)
|
|
{
|
|
score += 35;
|
|
}
|
|
|
|
return score;
|
|
}
|
|
|
|
private static bool IsDefaultTraitForActor(ActorTrait trait, Actor actor)
|
|
{
|
|
try
|
|
{
|
|
if (trait == null || actor == null || actor.asset == null
|
|
|| trait.default_for_actor_assets == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return trait.default_for_actor_assets.Contains(actor.asset);
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static string TraitDisplayName(ActorTrait trait)
|
|
{
|
|
try
|
|
{
|
|
string locale = trait.getLocaleID();
|
|
if (!string.IsNullOrEmpty(locale))
|
|
{
|
|
string localized = LocalizedTextManager.getText(locale);
|
|
if (!string.IsNullOrEmpty(localized) && localized != locale)
|
|
{
|
|
return localized;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// fall through
|
|
}
|
|
|
|
return Humanize(trait.id);
|
|
}
|
|
|
|
/// <summary>Live AI task label for the nametag chip (localized, else humanized id).</summary>
|
|
public static string ReadLiveTask(Actor actor)
|
|
{
|
|
return SafeTask(actor);
|
|
}
|
|
|
|
private static string SafeTask(Actor actor)
|
|
{
|
|
try
|
|
{
|
|
if (actor == null || !actor.hasTask() || actor.ai == null || actor.ai.task == null)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
string text = actor.ai.task.getLocalizedText();
|
|
if (!string.IsNullOrEmpty(text) && text != "-")
|
|
{
|
|
return text;
|
|
}
|
|
|
|
return Humanize(actor.ai.task.id);
|
|
}
|
|
catch
|
|
{
|
|
try
|
|
{
|
|
if (actor != null && actor.hasTask() && actor.ai?.task != null)
|
|
{
|
|
return Humanize(actor.ai.task.id);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
|
|
return "";
|
|
}
|
|
}
|
|
|
|
private static string Humanize(string raw)
|
|
{
|
|
if (string.IsNullOrEmpty(raw))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
return raw.Replace('_', ' ');
|
|
}
|
|
|
|
private static string SafeName(Actor actor)
|
|
{
|
|
try
|
|
{
|
|
string name = actor.getName();
|
|
if (!string.IsNullOrEmpty(name))
|
|
{
|
|
return name;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// fall through
|
|
}
|
|
|
|
return actor.asset != null ? actor.asset.id : "unit";
|
|
}
|
|
|
|
private static int SafeAge(Actor actor)
|
|
{
|
|
try
|
|
{
|
|
return actor.getAge();
|
|
}
|
|
catch
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private static int SafeLevel(Actor actor)
|
|
{
|
|
try
|
|
{
|
|
return actor.data != null ? actor.data.level : 0;
|
|
}
|
|
catch
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|