feat(trenta): composter and heartwood grove

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Ferdinando 2026-08-07 17:02:47 +02:00
commit 1a741978bf
7 changed files with 1659 additions and 19 deletions

View file

@ -7,13 +7,22 @@ namespace RacesOfValheim
/// <summary>
/// The swarm itself. Not a Character - it has no health, no hitbox anything else can target,
/// so "indistruttibile" falls out of what it simply isn't rather than needing invulnerability
/// code of its own. Idles in a slow orbit around the Trenta; the instant an enemy comes within
/// range it breaks off, hovers over that enemy instead, and sting-ticks it on a timer until it
/// dies or leaves range, then returns to orbiting.
/// code of its own. Idles in a slow orbit around whatever it is tied to; the instant an enemy
/// comes within range it breaks off, hovers over that enemy instead, and sting-ticks it on a
/// timer until it dies or leaves range, then returns to orbiting.
///
/// Two things it can be tied to, and the difference is only in what counts as an enemy:
/// - a Trenta player (the Hornet's Nest perk), which can be asked BaseAI.IsEnemy directly;
/// - a Heartwood Grove's trunk (the tier-2 station), which cannot, because a piece is not a
/// Character. See IsHostile.
/// Deliberately one class rather than two copies: the sting is combat logic, and a second copy
/// means the next tweak to the venom gets made in one place and forgotten in the other.
/// </summary>
public class TrentaHornetSwarm : MonoBehaviour
{
private Player owner;
private Player owner; // null for a grove swarm
private Transform anchor; // what it orbits; the owner's own transform for a player swarm
private float engageRadius;
private Character target;
private float tickTimer;
private float orbitAngle;
@ -21,11 +30,38 @@ namespace RacesOfValheim
public static GameObject Spawn(Player player)
{
GameObject go = new GameObject("TrentaHornetsNest");
go.transform.position = player.transform.position + Vector3.up;
GameObject go = Build("TrentaHornetsNest", player.transform.position + Vector3.up);
TrentaHornetSwarm swarm = go.GetComponent<TrentaHornetSwarm>();
swarm.owner = player;
swarm.anchor = player.transform;
swarm.engageRadius = TrentaConfig.HornetsNestEngageRadius;
return go;
}
/// <summary>
/// The grove's swarm: same bees, tied to a fixed point instead of to a player. The radius is
/// its own number because 6 metres is tuned for something that walks - a tree that cannot
/// chase has to reach further, and it matches the ground the grove fertilises.
/// </summary>
public static GameObject SpawnAtAnchor(Transform anchorPoint, float radius)
{
GameObject go = Build("TrentaGroveBees", anchorPoint.position + Vector3.up);
TrentaHornetSwarm swarm = go.GetComponent<TrentaHornetSwarm>();
swarm.anchor = anchorPoint;
swarm.engageRadius = radius;
return go;
}
private static GameObject Build(string name, Vector3 position)
{
GameObject go = new GameObject(name);
go.transform.position = position;
TrentaHornetSwarm swarm = go.AddComponent<TrentaHornetSwarm>();
swarm.owner = player;
BuildFliesVfx(go.transform);
swarm.buzz = BuildBuzzAudio(go.transform);
@ -33,15 +69,20 @@ namespace RacesOfValheim
return go;
}
private Vector3 AnchorPosition => anchor != null ? anchor.position : transform.position;
private void Update()
{
if (owner == null || owner.IsDead())
// A player swarm dies with its player; a grove swarm dies with its tree. Either way the
// check is "is there still something to orbit", which is also what makes the swarm clean
// itself up when the grove is destroyed without the grove having to say so.
if (anchor == null || (owner != null && owner.IsDead()))
{
Destroy(gameObject);
return;
}
if (target != null && (target.IsDead() || Vector3.Distance(target.transform.position, owner.transform.position) > TrentaConfig.HornetsNestEngageRadius * 1.5f))
if (target != null && (target.IsDead() || Vector3.Distance(target.transform.position, AnchorPosition) > engageRadius * 1.5f))
{
target = null;
}
@ -74,7 +115,7 @@ namespace RacesOfValheim
// them - reads as "trying to stay on him" without looking glued in place.
orbitAngle += Time.deltaTime * 60f;
Vector3 orbitOffset = new Vector3(Mathf.Cos(orbitAngle * Mathf.Deg2Rad), 0.3f, Mathf.Sin(orbitAngle * Mathf.Deg2Rad)) * TrentaConfig.HornetsNestOrbitRadius;
Vector3 restPoint = owner.transform.position + Vector3.up + orbitOffset;
Vector3 restPoint = AnchorPosition + Vector3.up + orbitOffset;
transform.position = Vector3.MoveTowards(transform.position, restPoint, TrentaConfig.HornetsNestFollowSpeed * Time.deltaTime);
}
}
@ -83,16 +124,17 @@ namespace RacesOfValheim
{
Character nearest = null;
float nearestDist = float.MaxValue;
Vector3 centre = AnchorPosition;
foreach (Collider collider in Physics.OverlapSphere(owner.transform.position, TrentaConfig.HornetsNestEngageRadius))
foreach (Collider collider in Physics.OverlapSphere(centre, engageRadius))
{
if (collider == null) continue;
Character character = collider.GetComponentInParent<Character>();
if (character == null || character == owner || character.IsDead()) continue;
if (!BaseAI.IsEnemy(owner, character)) continue;
if (character == null || character == owner) continue;
if (!IsHostile(character)) continue;
float dist = Vector3.Distance(character.transform.position, owner.transform.position);
float dist = Vector3.Distance(character.transform.position, centre);
if (dist < nearestDist)
{
nearestDist = dist;
@ -103,6 +145,59 @@ namespace RacesOfValheim
return nearest;
}
/// <summary>
/// Who gets stung.
///
/// For a player swarm this is one line - BaseAI.IsEnemy - and it is the most valuable line in
/// the file: RaceHostilityManager is a postfix on that very method, so the Trenta's own
/// alliances (greyling, greydwarf, fuling, troll, leech, deathsquito) are honoured for free.
///
/// A grove has no Character to ask on its own behalf, so the rule is rebuilt from parts:
/// never players, never tamed, and only things carrying a MonsterAI - which is the cut that
/// spares wild deer and boar, since vanilla gives passive animals AnimalAI instead. And if
/// there is a Trenta standing inside the radius, IsEnemy is consulted against them too, so
/// the alliances come back the moment someone is there to carry them.
/// </summary>
private bool IsHostile(Character character)
{
if (character == null || character.IsDead()) return false;
if (character.IsPlayer()) return false;
if (character.IsTamed()) return false;
if (owner != null) return BaseAI.IsEnemy(owner, character);
if (character.GetComponent<MonsterAI>() == null) return false;
Player trenta = NearestTrenta();
return trenta == null || BaseAI.IsEnemy(trenta, character);
}
/// <summary>
/// The nearest Trenta inside the swarm's own radius, or null. Used both to borrow their
/// alliances and to attribute the sting - a hit with no attacker at all is what the old eitr
/// refinery's mystery damage looked like, and worth avoiding whenever there is a real one.
/// </summary>
private Player NearestTrenta()
{
Player nearest = null;
float nearestDist = float.MaxValue;
Vector3 centre = AnchorPosition;
foreach (Player player in Player.GetAllPlayers())
{
if (player == null || player.IsDead() || !TrentaVisuals.IsTrentaPlayer(player)) continue;
float dist = Vector3.Distance(player.transform.position, centre);
if (dist <= engageRadius && dist < nearestDist)
{
nearestDist = dist;
nearest = player;
}
}
return nearest;
}
/// <summary>
/// Two hits, matching "come se il giocatore fosse costantemente avvelenato dalle api, più
/// lo stesso danno ma perforante da sopra" literally: one Poison hit at the wild Bee
@ -111,16 +206,20 @@ namespace RacesOfValheim
/// </summary>
private void Sting(Character victim)
{
// A grove's bees are still somebody's bees when a Trenta is standing under the tree;
// when nobody is, the sting lands with no attacker rather than picking one at random.
Character attacker = owner != null ? owner : NearestTrenta();
HitData poison = new HitData();
poison.m_damage.m_poison = TrentaConfig.HornetsNestDamagePerTick;
poison.SetAttacker(owner);
if (attacker != null) poison.SetAttacker(attacker);
poison.m_point = victim.GetCenterPoint();
poison.m_dir = Vector3.zero;
victim.Damage(poison);
HitData pierce = new HitData();
pierce.m_damage.m_pierce = TrentaConfig.HornetsNestDamagePerTick;
pierce.SetAttacker(owner);
if (attacker != null) pierce.SetAttacker(attacker);
pierce.m_point = victim.GetCenterPoint();
pierce.m_dir = Vector3.down;
victim.Damage(pierce);
@ -165,8 +264,10 @@ namespace RacesOfValheim
source.spatialBlend = 1f; // 3D - it should sound like it is coming from the swarm
source.volume = 0.6f;
GameObject beehivePrefab = PrefabManager.Cache.GetPrefab<GameObject>(TrentaConfig.HornetsNestBeehivePrefab);
Beehive beehive = beehivePrefab != null ? beehivePrefab.GetComponent<Beehive>() : null;
// Resolved through TrentaConfig, which tries "piece_beehive" before "Beehive". This used
// to look up "Beehive" alone and got nothing, every time, which is why the swarm has been
// silent since it was written - found in the BepInEx log, not by listening.
Beehive beehive = TrentaConfig.FindVanillaBeehive();
GameObject beeEffect = beehive != null ? Traverse.Create(beehive).Field<GameObject>("m_beeEffect").Value : null;
AudioSource sourceClipHolder = beeEffect != null ? beeEffect.GetComponentInChildren<AudioSource>(true) : null;