RacesOfValheim/JotunnModStub/RaceRadial.cs
Ferdinando 3f8cf1ee27 feat(alfar): v1.6.0 - the Alfar, an eleventh race
Grey, tall and thin, with black eyes. Eitr is armour and fuel (shield,
diet, soft fall, levitation), Heimkall sends a lethal blow home at one
health every 20 minutes. Caster powers from Skrid to the archon
Sameining, with on-screen cooldowns. Automated base around the
Ljoskjarne: drones, three constructs, crystals, Ljosgard, Prismavard.
Compendium page in EN/IT/RU.

README: New in block, race section, progression, station line, changelog.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 17:38:28 +01:00

225 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using Valheim.UI;
using Object = UnityEngine.Object;
namespace RacesOfValheim
{
/// <summary>
/// The race's powers on a gamepad: one more group in Valheim 1.0's radial menu, named after the
/// player's race, with one entry per form and ability.
///
/// WHY THE RADIAL AND NOT A BUTTON. Read off the 1.0 assemblies with Cecil: every physical pad
/// button is bound in all four layouts, the only chords vanilla leaves alone are LT+X and LT+R3
/// in Classic only, and LT is already the transformed forms' Mouse1 attack through
/// RaceInput.PadAttackButtons. The radial is opened the way vanilla opens it (hold R3, or the
/// keyboard's own radial key), follows whatever layout is picked, and has room for every race.
///
/// NO RACE CODE CHANGES. An entry does not call into the race: it "presses" the key the player
/// already has bound (RaceInput.Press), and the race's own IsDownIgnoringHeldKeys check picks it
/// up on its next Update. Unlock rules, cooldowns and the messages that go with them therefore
/// behave exactly as they do from the keyboard, and a rebound key is followed for free.
///
/// Everything vanilla-side was read, not guessed: ValheimRadialConfig.InitRadialConfig builds
/// its top page by calling AddEmoteGroup(radial, elements) among others, a group is a
/// GroupElement instantiated from RadialData.SO and Init'ed with (config, back, radial), and its
/// Interact just calls radial.QueuedOpen(config, back). Leaves are elements whose Interact and
/// CloseOnInteract are public Func&lt;bool&gt; setters; Name's setter is private, hence the
/// reflection.
/// </summary>
public static class RaceRadial
{
private class Entry
{
public string Race;
public string Label;
public Func<ConfigEntry<KeyboardShortcut>> Key;
public string Icon; // an item prefab whose icon is borrowed; may not exist
public Func<Player, bool> Available; // null means always, for that race
}
private static readonly List<Entry> Entries = new List<Entry>
{
new Entry { Race = "Fenrir", Label = "Wolf Form", Key = () => FenrirConfig.WolfFormKey, Icon = "TrophyWolf" },
new Entry { Race = "Fenrir", Label = "Fenrir Form", Key = () => FenrirConfig.FenrirFormKey, Icon = "TrophyFenring" },
new Entry { Race = "Fenrir", Label = "Cultist Form", Key = () => FenrirConfig.CultistFormKey, Icon = "TrophyCultist" },
new Entry { Race = "Fenrir", Label = "Howl", Key = () => FenrirConfig.HowlKey, Icon = "TrophyUlv" },
new Entry { Race = "Aswid", Label = "Bat Form", Key = () => AswidConfig.BatFormKey, Icon = "TrophyDeathsquito" },
new Entry { Race = "Aswid", Label = "Mist Form", Key = () => AswidConfig.MistFormKey, Icon = "TrophyWraith" },
new Entry { Race = "Aswid", Label = "Eclipse", Key = () => AswidConfig.EclipseKey, Icon = "TrophyFader" },
new Entry { Race = "Aswid", Label = "Tame", Key = () => AswidConfig.TameKey, Icon = "TrophyWolf" },
new Entry { Race = "Aswid", Label = "Devour", Key = () => AswidConfig.ToggleDevourKey, Icon = "TrophyDraugr" },
new Entry { Race = "Skadi", Label = "Drake Form", Key = () => SkadiConfig.DrakeFormKey, Icon = "TrophyHatchling" },
new Entry { Race = "Skadi", Label = "Moder Form", Key = () => SkadiConfig.ModerFormKey, Icon = "TrophyDragonQueen" },
new Entry { Race = "Skadi", Label = "Fader Form", Key = () => SkadiConfig.FaderFormKey, Icon = "TrophyFader" },
new Entry { Race = "Trol", Label = "True Form", Key = () => TrolConfig.TrueFormKey, Icon = "TrophyFrostTroll" },
new Entry { Race = "Trol", Label = "Throw Rock", Key = () => TrolConfig.ThrowRockKey, Icon = "Stone" },
new Entry { Race = "Trol", Label = "Grab Log", Key = () => TrolConfig.GrabLogKey, Icon = "Wood" },
new Entry { Race = "Trenta", Label = "Purple Rain", Key = () => TrentaConfigManagement.PurpleRainKey, Icon = "Blueberries" },
new Entry { Race = "Trenta", Label = "Homecoming Portal", Key = () => TrentaConfigManagement.PortalKey, Icon = "SurtlingCore" },
new Entry { Race = "Trenta", Label = "Muspell Form", Key = () => TrentaConfigManagement.MuspellKey, Icon = "TrophySurtling" },
new Entry { Race = "Dvergar", Label = "Runic Ping", Key = () => DvergarConfigManagement.RunicPingKey, Icon = "TrophyDvergr" },
new Entry { Race = "Dvergar", Label = "Runic Overcharge", Key = () => DvergarConfigManagement.RunicOverchargeKey, Icon = "Eitr" },
new Entry { Race = AlfarConfig.RaceId, Label = "Skrid (Blink)", Key = () => AlfarPowerConfig.SkridKey, Icon = "Eitr",
Available = p => BossProgressionManager.GetCurrentProgressionState() >= AlfarPowerConfig.SkridState },
new Entry { Race = AlfarConfig.RaceId, Label = "Mining Drones", Key = () => AlfarPowerConfig.GrafvaettirKey, Icon = "TrophyDeathsquito",
Available = p => BossProgressionManager.GetCurrentProgressionState() >= AlfarPowerConfig.GrafvaettirState },
new Entry { Race = AlfarConfig.RaceId, Label = "Kall (Telekinesis)", Key = () => AlfarPowerConfig.KallKey, Icon = "Thunderstone",
Available = p => BossProgressionManager.GetCurrentProgressionState() >= AlfarPowerConfig.KallState },
new Entry { Race = AlfarConfig.RaceId, Label = "Ljosstorm", Key = () => AlfarPowerConfig.LjosstormKey, Icon = "OrbThunderBlood",
Available = p => BossProgressionManager.GetCurrentProgressionState() >= AlfarPowerConfig.LjosstormState },
new Entry { Race = AlfarConfig.RaceId, Label = "Kyrr (Stasis)", Key = () => AlfarPowerConfig.KyrrKey, Icon = "FreezeGland",
Available = p => BossProgressionManager.GetCurrentProgressionState() >= AlfarPowerConfig.KyrrState },
new Entry { Race = AlfarConfig.RaceId, Label = "Sameining (Archon)", Key = () => AlfarPowerConfig.SameiningKey, Icon = "TrophyFader",
Available = p => BossProgressionManager.GetCurrentProgressionState() >= AlfarPowerConfig.SameiningState },
new Entry { Race = "Sverm", Label = "Ravager Strain", Key = () => SvermConfigManagement.RavagerKey, Icon = "TrophySeekerBrute" },
new Entry { Race = "Skeleton", Label = "Summon on Kill", Key = () => SkeletonConfig.SummonToggleKey, Icon = "StaffSkeleton",
Available = player => SkeletonProgression.GetRules(BossProgressionManager.GetCurrentProgressionState()).SummonOnKill },
// Any race: shown to whoever RaceClawSet has claws for.
new Entry { Race = null, Label = "Grow Claws", Key = () => RaceClawsConfig.ActivateKey, Icon = "TrophyWolf",
Available = player => RaceClawSet.For(player) != null },
};
private static readonly MethodInfo SetName = AccessTools.PropertySetter(typeof(RadialMenuElement), "Name");
/// <summary>Called from the AddEmoteGroup postfix: appends the race's group to vanilla's top page.</summary>
public static void AddGroup(IRadialConfig back, RadialBase radial, List<RadialMenuElement> elements)
{
Player player = Player.m_localPlayer;
if (player == null || radial == null || elements == null) return;
if (RadialData.SO == null || RadialData.SO.GroupElement == null) return;
string race = RaceOf(player);
List<Entry> mine = For(player, race);
if (mine.Count == 0) return;
GroupElement group = Object.Instantiate(RadialData.SO.GroupElement);
group.Init(new RaceRadialConfig(race, mine), back, radial);
elements.Add(group);
}
private static List<Entry> For(Player player, string race)
{
List<Entry> mine = new List<Entry>();
foreach (Entry entry in Entries)
{
if (entry.Race != null && entry.Race != race) continue;
if (entry.Available != null && !entry.Available(player)) continue;
if (entry.Key() == null) continue; // that race's config never bound: nothing to press
mine.Add(entry);
}
return mine;
}
/// <summary>The same two sources RaceClaws.GetRace reads, in the same order, "Human" in the
/// ZDO counting as no answer.</summary>
private static string RaceOf(Player player)
{
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
{
string race = nview.GetZDO().GetString("RacesOfValheim_Race", "");
if (!string.IsNullOrEmpty(race) && race != "Human") return race;
}
if (player.m_customData != null && player.m_customData.TryGetValue("Race", out string custom))
{
return custom;
}
return "Human";
}
/// <summary>An item's inventory icon, or null - a missing prefab only costs the picture.</summary>
private static Sprite IconOf(string prefabName)
{
if (ObjectDB.instance == null || string.IsNullOrEmpty(prefabName)) return null;
GameObject prefab = ObjectDB.instance.GetItemPrefab(prefabName);
ItemDrop drop = prefab != null ? prefab.GetComponent<ItemDrop>() : null;
if (drop == null || drop.m_itemData?.m_shared?.m_icons == null || drop.m_itemData.m_shared.m_icons.Length == 0) return null;
return drop.m_itemData.GetIcon();
}
/// <summary>The group's own page: one leaf per entry, built fresh every time it is opened so a
/// rebound key or a newly grown claw set is always current.</summary>
private class RaceRadialConfig : IRadialConfig
{
private readonly string m_race;
private readonly List<Entry> m_entries;
public RaceRadialConfig(string race, List<Entry> entries)
{
m_race = race;
m_entries = entries;
}
public string LocalizedName => m_race;
public Sprite Sprite => m_entries.Count > 0 ? IconOf(m_entries[0].Icon) : null;
public void InitRadialConfig(RadialBase radial)
{
List<RadialMenuElement> leaves = new List<RadialMenuElement>();
foreach (Entry entry in m_entries)
{
RadialMenuElement leaf = Object.Instantiate(RadialData.SO.EmoteElement);
Entry captured = entry;
string key = entry.Key().Value.ToString();
SetName?.Invoke(leaf, new object[] { string.IsNullOrEmpty(key) ? entry.Label : $"{entry.Label} ({key})" });
leaf.Interact = () =>
{
RaceInput.Press(captured.Key().Value);
return true;
};
leaf.CloseOnInteract = () => true;
Sprite icon = IconOf(entry.Icon);
if (leaf.Icon != null)
{
leaf.Icon.gameObject.SetActive(icon != null);
leaf.Icon.sprite = icon;
}
leaves.Add(leaf);
}
radial.ConstructRadial(leaves);
}
}
}
[HarmonyPatch(typeof(ValheimRadialConfig), "AddEmoteGroup")]
public static class RaceRadial_AddEmoteGroup_Patch
{
// Wrapped: a mistake here must cost the race group, never the player's whole radial menu.
public static void Postfix(ValheimRadialConfig __instance, RadialBase radial, List<RadialMenuElement> elements)
{
try
{
RaceRadial.AddGroup(__instance, radial, elements);
}
catch (Exception e)
{
Jotunn.Logger.LogWarning($"[RaceRadial] Could not add the race group to the radial menu: {e}");
}
}
}
}