RacesOfValheim/JotunnModStub/Sverm/SvermGjallPatches.cs
2026-10-05 11:39:38 +02:00

124 lines
5.1 KiB
C#

using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// What a Sverm Gjall's bombs do once they land: no friendly fire, and the ticks they hatch are ours.
///
/// Two reports in one. The Gjall's bile bomb is an AOE projectile, and Projectile.DoAOE (read off
/// the IL) only spares a victim when m_noDamageFriendly is set on the projectile - the vanilla
/// Gjall never needed it, because nothing it fights has friends. Ours has the player, so a bomb
/// aimed at a turtle killed the player on a boat beside it. And the ticks the bomb spawns
/// (Projectile.SpawnOnHit) are plain wild Ticks, hostile to everyone, the Gjall that threw them
/// included.
///
/// Both are keyed on the projectile's owner being a Sverm troop (a RaisedThrall that answers to the
/// Sverm race), so a wild Gjall in the Mistlands behaves exactly as before.
/// </summary>
public static class SvermGjallPatches
{
/// <summary>How long a hatched tick lives. Wild ones never despawn, and ours would pile up.</summary>
private const float TickLifetime = 45f;
private static bool IsSvermTroop(Character owner)
{
if (owner == null) return false;
RaisedThrall thrall = owner.GetComponent<RaisedThrall>();
return thrall != null && thrall.m_commandingRace == SvermConfig.RaceId;
}
// ── The nest is immune to its own brood ──────────────────────────────
/// <summary>A Sverm troop, or a tick one of them hatched: anything the colony itself put on the field.</summary>
private static bool IsColonyMember(Character attacker)
{
if (attacker == null) return false;
return IsSvermTroop(attacker) || attacker.GetComponent<SvermGjallTickLife>() != null;
}
// RPC_Damage and not Damage: that is where the hit lands on the piece's owner, whoever threw it.
// A raid is where it showed - bombs, ticks' bursts and troop swings all go through here.
[HarmonyPatch(typeof(WearNTear), "RPC_Damage")]
public static class Sverm_Nest_BroodImmunity_Patch
{
public static bool Prefix(WearNTear __instance, HitData hit)
{
if (hit == null || __instance.GetComponent<SvermHiveBehaviour>() == null) return true;
return !IsColonyMember(hit.GetAttacker());
}
}
// ── Friendly fire ────────────────────────────────────────────────────
[HarmonyPatch(typeof(Projectile), nameof(Projectile.Setup))]
public static class Sverm_Projectile_Setup_Patch
{
public static void Postfix(Projectile __instance, Character ___m_owner)
{
if (IsSvermTroop(___m_owner)) __instance.m_noDamageFriendly = true;
}
}
// ── The ticks ────────────────────────────────────────────────────────
// Characters that came to life while a Sverm projectile was spawning its payload. Awake has
// run on every component by the time SpawnOnHit returns, which is when they are tamed.
private static bool collecting;
private static readonly List<Character> born = new List<Character>();
[HarmonyPatch(typeof(Projectile), "SpawnOnHit")]
public static class Sverm_Projectile_SpawnOnHit_Patch
{
public static void Prefix(Character ___m_owner)
{
born.Clear();
collecting = IsSvermTroop(___m_owner);
}
public static void Postfix()
{
collecting = false;
foreach (Character spawned in born)
{
if (spawned == null || spawned.IsPlayer() || spawned.IsDead()) continue;
spawned.SetTamed(true);
spawned.gameObject.AddComponent<SvermGjallTickLife>().m_seconds = TickLifetime;
}
born.Clear();
}
}
[HarmonyPatch(typeof(Character), "Awake")]
public static class Sverm_Character_Awake_Patch
{
public static void Postfix(Character __instance)
{
if (collecting) born.Add(__instance);
}
}
}
/// <summary>A tick born of a Sverm Gjall's bomb: tame, and gone after a short while.</summary>
public class SvermGjallTickLife : MonoBehaviour
{
public float m_seconds = 45f;
private void Start()
{
Invoke(nameof(Expire), m_seconds);
}
private void Expire()
{
ZNetView nview = GetComponent<ZNetView>();
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
ZNetScene.instance.Destroy(gameObject);
}
}
}