RacesOfValheim/JotunnModStub/HunterSenses.cs
kanym fc5d6b8abc feat: Fenrir forms sense nearby creatures like the Aswid
Every Fenrir form (Wolf, Ulv, Fenring, Cultist) now lights each untamed creature within 100 m with the Aswid's green glow; the human does not. The glow moved out of AswidRace into a shared HunterSenses, which the Aswid now calls too, unchanged. New setting Fenrir - Superhuman Senses / Detection Radius; the light follows the Aswid's Marker Light settings. Compendium line in English, Italian and Russian.

Asked for by a friend of MrBiffty.
2026-10-29 19:33:21 +01:00

92 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The hunter's senses the Aswid and the Fenrir share: every untamed creature within a radius
/// gets a green light, seen only by the local player. The Aswid has it as a passive from Moder,
/// the Fenrir in any of its four forms.
///
/// The lights are local GameObjects parented to the creature, so nothing goes on the network.
/// The light's range and strength are the Aswid's settings; each race brings its own radius.
/// </summary>
public static class HunterSenses
{
private static readonly Dictionary<Character, GameObject> lights = new Dictionary<Character, GameObject>();
private static float timer;
/// <summary>
/// Called every frame while the senses are on. Sweeps once per refresh interval;
/// onSensed runs for each creature in range (the Aswid marks them with its status effect).
/// </summary>
public static void Tick(float radius, Action<Character> onSensed = null)
{
Player player = Player.m_localPlayer;
if (player == null) return;
timer -= Time.deltaTime;
if (timer > 0f) return;
timer = AswidConfig.SensesRefreshInterval.Value;
Vector3 myPos = player.transform.position;
HashSet<Character> inRange = new HashSet<Character>();
foreach (Character c in Character.GetAllCharacters())
{
if (c == null || c == player || c.IsDead() || c.IsTamed()) continue;
if (Vector3.Distance(c.transform.position, myPos) > radius) continue;
try
{
inRange.Add(c);
onSensed?.Invoke(c);
if (!lights.ContainsKey(c))
{
GameObject lightObj = new GameObject("AswidSensesLight");
lightObj.transform.SetParent(c.transform);
lightObj.transform.position = c.GetCenterPoint();
Light pLight = lightObj.AddComponent<Light>();
pLight.type = LightType.Point;
pLight.color = Color.green;
pLight.range = AswidConfig.SensesLightRange.Value;
pLight.intensity = AswidConfig.SensesLightIntensity.Value;
pLight.shadows = LightShadows.None;
lights[c] = lightObj;
}
}
catch (Exception ex)
{
Jotunn.Logger.LogError($"[Senses] Error processing mob {c.name}: {ex}");
}
}
List<Character> keysToRemove = new List<Character>();
foreach (var kvp in lights)
{
if (kvp.Key == null || kvp.Key.IsDead() || !inRange.Contains(kvp.Key))
{
if (kvp.Value != null) UnityEngine.Object.Destroy(kvp.Value);
keysToRemove.Add(kvp.Key);
}
}
foreach (var k in keysToRemove) lights.Remove(k);
}
/// <summary>Puts every light out, for when the senses end (a form reverts, a tier is lost).</summary>
public static void Clear()
{
if (lights.Count == 0) return;
foreach (var kvp in lights)
{
if (kvp.Value != null) UnityEngine.Object.Destroy(kvp.Value);
}
lights.Clear();
timer = 0f;
}
}
}