Merge branch 'fix/radial-swallows-attack-input'

This commit is contained in:
kanym 2026-09-26 18:12:36 +02:00
commit 58874457e0
2 changed files with 207 additions and 8 deletions

View file

@ -15,13 +15,16 @@ namespace RacesOfValheim
/// (InventoryGui covers the backpack and chests alike), so mirror that instead of maintaining
/// a parallel list of GUI checks that would drift as the game changes.
///
/// ONE GUI THAT TakeInput DOES NOT COVER: the hammer's piece selection panel. Read off the IL,
/// TWO GUIS THAT TakeInput DOES NOT COVER. The first is the hammer's piece selection panel. Read off the IL,
/// TakeInput tests Chat, Console, TextInput, StoreGui, InventoryGui, Menu, TextViewer, the map,
/// free-fly and the barber - and nothing else. Vanilla does not need it there because build
/// input is handled separately in UpdatePlacement, but a mod ability reading the device
/// directly does: Skadi's cold spell is bound to middle mouse and kept casting while the build
/// menu was open. Hud.IsPieceSelectionVisible is static, public and null-safe on its own
/// instance, so it costs one call and no reflection.
///
/// The second is the radial menu, and it is handled in AttackDown rather than here - see
/// RadialOpen for why the difference matters.
/// </summary>
public static class RaceInput
{
@ -77,8 +80,18 @@ namespace RacesOfValheim
/// </summary>
public static bool IsDownIgnoringHeldKeys(this KeyboardShortcut shortcut)
{
// The queued press is taken FIRST and without any gate, because that is the radial menu
// firing this very ability - see Press(). Everything below is the device being read raw.
if (ConsumePress(shortcut)) return true;
if (shortcut.MainKey == KeyCode.None) return false;
// A keybind that sits on a mouse button is the radial's own click as far as the device is
// concerned, so the wheel closes this door too. AttackDown covers the forms' attacks;
// this covers abilities bound to a button instead - Skadi's cold spell is on middle
// mouse, and middle-clicking an entry in the wheel used to cast it. Keys at the keyboard
// are left alone: nothing about the wheel makes pressing K ambiguous.
if (RadialOpen && IsMouseButton(shortcut.MainKey)) return false;
if (!Input.GetKeyDown(shortcut.MainKey)) return false;
foreach (KeyCode modifier in shortcut.Modifiers)
@ -143,15 +156,75 @@ namespace RacesOfValheim
/// </summary>
public static bool AttackDown(int mouseButton)
{
if (RadialOpen) return false;
return Input.GetMouseButtonDown(mouseButton) || PadAttackDown(mouseButton);
}
/// <summary>Held-down counterpart of <see cref="AttackDown"/>.</summary>
public static bool AttackHeld(int mouseButton)
{
if (RadialOpen) return false;
return Input.GetMouseButton(mouseButton) || PadAttackHeld(mouseButton);
}
/// <summary>
/// Whether the radial menu is up, which is a third GUI TakeInput knows nothing about.
///
/// Clicking an entry in the wheel was also swinging the transformed form's attack: the click
/// picks the entry AND is read here as a raw mouse button. Vanilla attacks are untouched by
/// this because they go through ZInput, which the radial already blocks - only input read
/// straight off the device gets through, which is all of ours.
///
/// CHECKED HERE AND NOT IN <see cref="Allowed"/>, deliberately. The radial fires an ability by
/// queueing its keybind through Press(), and the ability's own reader picks that up inside an
/// Allowed gate - so blocking Allowed while the wheel is open would risk the wheel swallowing
/// the very abilities it exists to trigger. The queued press does survive, since it holds for
/// PressWindow rather than a single frame, but there is no reason to lean on that when only
/// the mouse reads are the problem. Keybinds pressed at the keyboard while the wheel is open
/// are left alone for the same reason.
/// </summary>
private static bool IsMouseButton(KeyCode key)
{
return key >= KeyCode.Mouse0 && key <= KeyCode.Mouse6;
}
// Said once for each answer it ever gives, and then never again. The whole gate rests on what
// Hud.InRadial() means, and that was read off the method's name rather than its body - so it
// is worth one line of proof in every player's log that it reports False with no wheel on
// screen. If it ever logs only True, the gate is eating input it has no business eating.
private static bool loggedOpen;
private static bool loggedClosed;
private static bool RadialOpen
{
get
{
try
{
bool open = Hud.InRadial();
if (open && !loggedOpen)
{
loggedOpen = true;
Jotunn.Logger.LogInfo("[RaceInput] Radial menu open: form attacks and mouse-bound abilities are held.");
}
else if (!open && !loggedClosed)
{
loggedClosed = true;
Jotunn.Logger.LogInfo("[RaceInput] Radial menu closed: form attacks and mouse-bound abilities are live.");
}
return open;
}
catch
{
// Fail open, like the TakeInput path above: a game update that moves this must
// cost the fix, not every attack in every form.
return false;
}
}
}
/// <summary>The gamepad half of <see cref="AttackDown"/> alone, for attacks whose mouse
/// side is a rebindable KeyboardShortcut rather than a fixed button.</summary>
public static bool PadAttackDown(int mouseButton)

View file

@ -232,6 +232,12 @@ namespace RacesOfValheim
{
HandleColdSpell(rules);
}
else if (ColdSpellKeyPressed())
{
ColdSpellBail(!rules.ColdSpell
? $"the tier does not grant it - state {BossProgressionManager.GetCurrentProgressionState()}, needs 2"
: "RaceInput.Allowed is false - a GUI has the input");
}
if (isTransformingSequence) return;
@ -334,16 +340,84 @@ namespace RacesOfValheim
Jotunn.Logger.LogInfo($"=== END {prefabName.ToUpper()} DEBUG INFO ===");
}
/// <summary>
/// Whether the Cold Spell's own key went down this frame, read raw and with no gate at all.
/// Only for the diagnostics below: five different conditions can swallow this spell and every
/// one of them is silent, so "it does nothing" was impossible to tell apart from "it was
/// never pressed".
/// </summary>
private static bool ColdSpellKeyPressed()
{
KeyCode key = SkadiConfig.ColdSpellKey.Value.MainKey;
return key != KeyCode.None && Input.GetKeyDown(key);
}
private static readonly System.Collections.Generic.HashSet<string> coldSpellBails =
new System.Collections.Generic.HashSet<string>();
/// <summary>One step of the cast, said before it is taken. Once per distinct step.</summary>
private static void ColdSpellStep(string step)
{
if (coldSpellBails.Add("step:" + step))
{
Jotunn.Logger.LogInfo($"[Skadi] Cold Spell: {step}.");
}
}
/// <summary>
/// What a borrowed prefab actually is. The root flag matters for the same reason it did on
/// the Skeleton's fire aura: a parented object is a node inside somebody else's hierarchy,
/// not a prefab, and spawning one on its own is how this mod has crashed before.
/// </summary>
private static string Describe(GameObject prefab)
{
if (prefab == null) return "null";
string path = prefab.name;
for (Transform t = prefab.transform.parent; t != null; t = t.parent) path = t.name + "/" + path;
var parts = new System.Collections.Generic.List<string>();
foreach (Component comp in prefab.GetComponents<Component>())
{
if (comp != null && !(comp is Transform)) parts.Add(comp.GetType().Name);
}
return $"{path}' root={prefab.transform.parent == null} carries=[{string.Join(", ", parts.ToArray())}]";
}
/// <summary>Says why a press did not become a cast, once per reason per session.</summary>
private static void ColdSpellBail(string reason)
{
if (coldSpellBails.Add(reason))
{
Jotunn.Logger.LogInfo($"[Skadi] Cold Spell key pressed but not cast: {reason}.");
}
}
private void HandleColdSpell(SkadiProgression.FormRules rules)
{
if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) return;
if (isTransformed) return; // Do not cast human spell while in dragon form
bool pressed = ColdSpellKeyPressed();
if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead())
{
if (pressed) ColdSpellBail("the player is dead");
return;
}
if (isTransformed)
{
if (pressed) ColdSpellBail("you are in a dragon form");
return;
}
// Hammer, hoe or cultivator in hand: every build button is vanilla's. Middle mouse is
// Remove on the keyboard, and on the pad JoySecondaryAttack shares its physical button
// with JoyRemove (Classic) or JoyPlace (Alt2) - read off ZInput.AddGamepad*Buttons - so
// each piece built or removed also cast the spell and drained the stamina for it.
if (Player.m_localPlayer.InPlaceMode()) return;
if (Player.m_localPlayer.InPlaceMode())
{
if (pressed) ColdSpellBail("a hammer, hoe or cultivator is in hand");
return;
}
// Middle Mouse Button to cast, by default - rebindable via SkadiConfig.ColdSpellKey.
// On a gamepad, the secondary attack button (the pad's own middle-mouse equivalent) -
@ -354,7 +428,11 @@ namespace RacesOfValheim
{
// The improved cast is a burst rather than a rapid-fire, so it waits longer.
float cooldown = rules.ImprovedColdSpell ? IMPROVED_SPELL_COOLDOWN : SPELL_COOLDOWN;
if (Time.time - lastSpellTime < cooldown) return;
if (Time.time - lastSpellTime < cooldown)
{
ColdSpellBail($"it is on cooldown - {cooldown - (Time.time - lastSpellTime):F1}s left of {cooldown:F0}s");
return;
}
// Stamina, never eitr: the spell unlocks at Eikthyr but eitr food only exists from
// the Mistlands onward, so the old HaveEitr gate left it unusable for five tiers.
@ -378,8 +456,17 @@ namespace RacesOfValheim
zanim.SetTrigger("gpower");
}
if (coldSpellBails.Add("cast")) Jotunn.Logger.LogInfo("[Skadi] Cold Spell cast.");
StartCoroutine(ColdSpellKickCoroutine(rules));
}
else if (pressed)
{
// The key physically went down and the wrapper still said no. That is the last place
// left for this to fail, and the only one with no condition of its own above it.
ColdSpellBail($"'{SkadiConfig.ColdSpellKey.Value.MainKey}' went down but " +
"IsDownIgnoringHeldKeys refused it");
}
}
/// <summary>
@ -402,10 +489,31 @@ namespace RacesOfValheim
if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) yield break;
// EVERY STEP BELOW IS SAID BEFORE IT IS TAKEN, once per session. Casting this spell exits
// the process outright - no exception, no stack, the log simply stops - which is the same
// signature as sfx_fenring_howl, the sound that cannot be spawned either. The only reason
// we know where either one dies is that the line before it had already been flushed.
ColdSpellStep("resolving 'StaffIceShards'");
GameObject staffPrefab = PrefabManager.Cache.GetPrefab<GameObject>("StaffIceShards");
if (staffPrefab == null) yield break;
if (staffPrefab == null)
{
ColdSpellStep("'StaffIceShards' did not resolve - no shards fired");
yield break;
}
ItemDrop staffDrop = staffPrefab.GetComponent<ItemDrop>();
if (staffDrop == null) yield break;
if (staffDrop == null)
{
ColdSpellStep("'StaffIceShards' has no ItemDrop - no shards fired");
yield break;
}
// NOT described here, deliberately, and this is an experiment rather than a style choice.
// Describe() reads the prefab's components, and reading them may be what forces a
// soft-referenced asset to finish loading - which would mean the diagnostic was quietly
// fixing the crash it was added to measure. Both descriptions now run AFTER the shards
// are spawned, so nothing touches either prefab before the Instantiate that was dying.
// If the crash comes back, the touch was the fix and becomes a real one. If it does not,
// the crash was never about loading.
string projName = rules.ImprovedColdSpell ? "hatchling_cold_projectile" : null;
GameObject projPrefab = null;
@ -418,7 +526,13 @@ namespace RacesOfValheim
{
projPrefab = staffDrop.m_itemData.m_shared.m_attack.m_attackProjectile; // Default Staff of Frost projectile
}
if (projPrefab == null) yield break;
if (projPrefab == null)
{
ColdSpellStep("no projectile prefab at all - no shards fired");
yield break;
}
ColdSpellStep("projectile chosen from " +
(projName != null ? $"'{projName}' or the staff" : "the staff"));
int count = rules.ImprovedColdSpell ? IMPROVED_SPELL_SHARDS : 1;
@ -446,7 +560,19 @@ namespace RacesOfValheim
Vector3 spawnPos = Player.m_localPlayer.transform.position + Vector3.up * 1.4f + lookDir * 0.6f;
ColdSpellStep($"instantiating shard {i + 1} of {count}");
GameObject projInstance = Instantiate(projPrefab, spawnPos, Quaternion.LookRotation(lookDir));
ColdSpellStep("shard instantiated");
// Described only now that a shard has survived being spawned, so the reading itself
// cannot be what made the spawning safe. See the note above the staff lookup.
if (i == 0)
{
ColdSpellStep($"staff was '{Describe(staffPrefab)}");
ColdSpellStep($"projectile was '{Describe(projPrefab)}");
}
Projectile proj = projInstance.GetComponent<Projectile>();
if (proj != null)