Auto-Resurrect was decided in the Character.Damage prefix on the raw hit: before armour, blocking and resistances, with poison and fire counted in full. With good armour it fired on almost any hit. Damage also runs on the attacker's owner, so on a dedicated server a mob owned by the server or another player skipped the check entirely. It now runs in an ApplyDamage prefix, on the victim's own client, against the damage that is about to land. The rising-from-the-ground immunity is checked there too. Summon on Kill gets a toggle key (G, Skeleton - Keybinds) and a gamepad radial entry. The choice is saved on the character, on by default. A "Raise the Dead" status icon with the Dead Raiser's picture shows while it is on. Texts in English, Italian and Russian.
212 lines
11 KiB
C#
212 lines
11 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<bool> 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 = "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}");
|
|
}
|
|
}
|
|
}
|
|
}
|