RacesOfValheim/JotunnModStub/Sverm/SvermAntennae.cs
2026-08-11 16:30:20 +02:00

170 lines
6.9 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Sensory Antennae: from The Elder on, a Sverm feels every living thing around it and reads
/// what each one is by colour.
///
/// This is the Aswid's Auspex (AswidRace, the sensesCheckTimer loop) with one thing added: the
/// Aswid marks everything green because it only wants to know that something is THERE. An
/// insect's antennae are a chemical sense, and a chemical sense tells kin from prey from
/// threat - so the light is the same trick and the colour carries the information.
///
/// red - hostile
/// yellow - invertebrate: kin, and by Hive Pheromones already harmless to you
/// azure - yours: tamed, and in due course the colony's own brood
/// green - alive and indifferent
///
/// NOTHING HERE IS REPLICATED. A Light created on this client and parented to a creature costs
/// other players nothing and never touches the ZDO - the same reason the Aswid and the Dvergar
/// both mark with lights rather than with anything the server would have to carry.
///
/// The cost that IS real is local: this is a passive with no cooldown, so in a busy forest it
/// can hold a dozen or more point lights at once, and Valheim renders few per pixel. If it
/// ever misbehaves the lever is the radius, not the mechanism.
/// </summary>
public static class SvermAntennae
{
private const string LightName = "SvermAntennaeLight";
private static readonly Dictionary<Character, GameObject> lights = new Dictionary<Character, GameObject>();
private static readonly List<Character> stale = new List<Character>();
private static float nextSweep;
public static void Tick(Player player)
{
bool active = player != null
&& player == Player.m_localPlayer
&& SvermVisuals.IsSvermPlayer(player)
&& !player.IsDead()
&& SvermProgression.Current.SensoryAntennae;
if (!active)
{
ClearAll();
return;
}
if (Time.time < nextSweep) return;
nextSweep = Time.time + SvermConfig.AntennaeInterval;
Sweep(player);
}
private static void Sweep(Player player)
{
Vector3 centre = player.transform.position;
float radius = SvermConfig.AntennaeRadius;
HashSet<Character> inRange = new HashSet<Character>();
foreach (Character c in Character.GetAllCharacters())
{
if (c == null || c == player || c.IsDead()) continue;
if (Vector3.Distance(c.transform.position, centre) > radius) continue;
inRange.Add(c);
Color colour = ColourFor(player, c);
if (lights.TryGetValue(c, out GameObject existing) && existing != null)
{
// Already lit. Only the colour can have changed - a boar that turns hostile,
// a creature that gets tamed - and re-colouring is cheaper than rebuilding.
Light live = existing.GetComponent<Light>();
if (live != null && live.color != colour) live.color = colour;
continue;
}
lights[c] = BuildLight(c, colour);
}
// Anything no longer in range, or dead, or gone. Collected first and removed after:
// deleting from a dictionary while enumerating it throws.
stale.Clear();
foreach (KeyValuePair<Character, GameObject> pair in lights)
{
if (pair.Key == null || !inRange.Contains(pair.Key)) stale.Add(pair.Key);
}
foreach (Character gone in stale)
{
if (lights.TryGetValue(gone, out GameObject go) && go != null) Object.Destroy(go);
lights.Remove(gone);
}
}
/// <summary>
/// Order matters more than the colours do.
///
/// Tamed is checked FIRST because a tamed wolf is still an enemy of nothing and a friend of
/// yours, and asking IsEnemy first would paint your own animals as threats. Invertebrates
/// come next because Hive Pheromones has already made them harmless, so calling them
/// hostile would contradict the rest of the race. Only then does the hostile test run, and
/// everything left over is simply alive.
/// </summary>
private static Color ColourFor(Player player, Character c)
{
if (c.IsTamed()) return SvermConfig.AntennaeKinColor;
if (IsInvertebrate(c)) return SvermConfig.AntennaeInvertebrateColor;
if (BaseAI.IsEnemy(player, c)) return SvermConfig.AntennaeHostileColor;
return SvermConfig.AntennaePassiveColor;
}
/// <summary>
/// The same substring test RaceHostilityManager uses for Hive Pheromones, and deliberately
/// so: whatever counts as kin for the truce has to count as kin for the antennae, or the
/// player is told one thing and shown another.
/// </summary>
private static bool IsInvertebrate(Character c)
{
string prefabName = c.gameObject.name.ToLowerInvariant();
if (prefabName.Contains("bonemass")) return false;
return prefabName.Contains("blob")
|| prefabName.Contains("leech")
|| prefabName.Contains("tick")
|| prefabName.Contains("deathsquito")
|| prefabName.Contains("gjall")
|| prefabName.Contains("seeker");
}
private static GameObject BuildLight(Character c, Color colour)
{
GameObject go = new GameObject(LightName);
go.transform.SetParent(c.transform);
go.transform.position = c.GetCenterPoint();
Light light = go.AddComponent<Light>();
light.type = LightType.Point;
light.color = colour;
light.range = SvermConfig.AntennaeLightRange;
light.intensity = SvermConfig.AntennaeLightIntensity;
// No shadows, ever. A dozen shadow-casting point lights in a forest is a slideshow,
// and none of these lights is meant to light the world - only to say "here".
light.shadows = LightShadows.None;
return go;
}
/// <summary>Puts out every light. Called when the perk stops applying, and on death.</summary>
public static void ClearAll()
{
if (lights.Count == 0) return;
foreach (KeyValuePair<Character, GameObject> pair in lights)
{
if (pair.Value != null) Object.Destroy(pair.Value);
}
lights.Clear();
}
}
}