Files
TheriapolisV3/Theriapolis.Godot/UI/AbilityCalc.cs
T
Christopher Wiebe bb986d49f9 M6.6: StepReview signing + hybrid math revision
- New Step VIII (Review): name input and Confirm button that
  saves the finalized character to user://character.tres.
- Hybrid lineage rules simplified per project decision: drop
  the "no-stack on overlap, take +1 free elsewhere" rule from
  theriapolis-rpg-clades.md. Hybrids now pick one ability mod
  from each parent clade and they sum if they overlap.
  Removes HybridFreeAbility, the free-bonus picker row, and the
  overlap special case from AbilityCalc + WizardValidation.
- StepClade bonus rows now mutate in place (sync ButtonPressed)
  instead of tearing down on every Refresh — the old path freed
  the very button mid-Pressed-signal, leaving stale buttons next
  to the new ones.
- StepSkills drops the redundant "Calling: X · History: Y" meta
  line; both are already shown in the Aside summary.
- Aside hybrid section adds dual-species traits and the
  universal-hybrid detriment pills.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-03 20:51:55 -07:00

85 lines
3.6 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Theriapolis.Core.Data;
namespace Theriapolis.GodotHost.UI;
/// <summary>
/// Final ability score math — base assignment + clade and species mods.
/// Used by StepStats and Aside to render the score breakdown with a
/// "+N from Canidae · +2 from Wolf" hover popover.
///
/// For hybrids the lineage contribution comes from BOTH parent clades
/// (each tagged with its lineage in the source label) and the dominant
/// species. Core's CharacterBuilder.TryBuildHybrid is the authority on
/// the final mechanical rules at character-finalize time; this is a
/// preview-display helper.
/// </summary>
public static class AbilityCalc
{
public readonly record struct ModSource(string Label, int Value);
public static List<ModSource> Sources(string ability, CharacterDraft draft)
{
var list = new List<ModSource>();
if (draft.IsHybrid)
{
// Hybrids: take ONE ability modifier from each parent clade.
// Picks stack if they happen to land on the same ability — the
// original "no stack, take +1 + free elsewhere" rule was
// dropped per project decision. Species mods don't apply.
if (draft.SireChosenAbility == ability)
AddCladeChoice(list, ability, CodexContent.Clade(draft.SireCladeId), " (sire)");
if (draft.DamChosenAbility == ability)
AddCladeChoice(list, ability, CodexContent.Clade(draft.DamCladeId), " (dam)");
}
else
{
AddCladeSource(list, ability, CodexContent.Clade(draft.CladeId), "");
var sp = CodexContent.SpeciesById(draft.EffectiveSpeciesId);
if (sp is not null) AddDictSource(list, ability, sp.Name, sp.AbilityMods);
}
return list;
}
/// <summary>The chosen-mod path: one mod from a parent clade. The value
/// is the clade's actual mod for that ability (so picking CON from
/// canidae yields +1 while picking CON from ursidae yields +2).</summary>
private static void AddCladeChoice(List<ModSource> list, string ability, CladeDef? clade, string suffix)
{
if (clade is null) return;
if (clade.AbilityMods.TryGetValue(ability, out int v) && v != 0)
list.Add(new ModSource(clade.Name + suffix, v));
}
public static int TotalBonus(string ability, CharacterDraft draft)
=> Sources(ability, draft).Sum(s => s.Value);
public static int BaseScore(string ability, CharacterDraft draft)
=> draft.StatAssign.ContainsKey(ability) ? (int)draft.StatAssign[ability] : 0;
public static int FinalScore(string ability, CharacterDraft draft)
=> BaseScore(ability, draft) + TotalBonus(ability, draft);
public static int D20Modifier(int score) => (int)System.Math.Floor((score - 10) / 2.0);
public static string FormatSigned(int n) => n >= 0 ? $"+{n}" : n.ToString();
/// <summary>"+1 from Canidae · +2 from Wolf" — empty when no sources.</summary>
public static string FormatBreakdown(IEnumerable<ModSource> sources) =>
string.Join(" · ", sources.Select(s => $"{FormatSigned(s.Value)} from {s.Label}"));
private static void AddCladeSource(List<ModSource> list, string ability, CladeDef? clade, string suffix)
{
if (clade is null) return;
AddDictSource(list, ability, clade.Name + suffix, clade.AbilityMods);
}
private static void AddDictSource(List<ModSource> list, string ability, string sourceName, Dictionary<string, int> mods)
{
if (mods.TryGetValue(ability, out int v) && v != 0)
list.Add(new ModSource(sourceName, v));
}
}