318 lines
14 KiB
C#
318 lines
14 KiB
C#
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The Seeker Worker: half a Seeker, and the reason the colony feels like a colony rather than
|
|
/// like a barracks.
|
|
///
|
|
/// ONE TRADE. They come out of the nest after Bonemass, up to the hauler cap, and they earn their
|
|
/// keep twice over: the nest eats out of the chests at its foot once per living worker (see
|
|
/// SvermHiveBehaviour.Haul), and the workers themselves keep the forge the way the Skeleton's
|
|
/// Bone Smith does - the same brain, the same sigil chests, the same stations. Section 19.3's
|
|
/// farmers were the other trade; farming was tried and rejected, and nothing ever spawned one.
|
|
/// The Job enum keeps the name only so the cap entries in players' .cfg files still mean what
|
|
/// they say.
|
|
///
|
|
/// They are FREE. No biomass, no egg, and they are replaced when they die. That is deliberate:
|
|
/// they are labour, not army. Nothing they do competes with Brood, Seeker, Soldier or Gjall for
|
|
/// the tank, and nothing about them is commandable - they work, they fight what comes near, and
|
|
/// they never follow.
|
|
///
|
|
/// Half size, half health, half damage. The size and health are set on the prefab; the damage
|
|
/// is halved in Sverm_Damage_Patch, because a creature's blow comes from its default weapon and
|
|
/// that weapon's shared data belongs to every Seeker in the world.
|
|
/// </summary>
|
|
public static class SvermWorker
|
|
{
|
|
public const string PrefabName = "SvermSeekerWorker";
|
|
private const string SourcePrefab = "Seeker";
|
|
|
|
/// <summary>Which trade a given worker was born into. Kept on the worker itself.</summary>
|
|
public enum Job { Farmer, Hauler }
|
|
|
|
private static bool registered;
|
|
|
|
// Live workers, by job. Rebuilt lazily rather than tracked with events: a creature can die
|
|
// in ways nothing tells us about, so the honest count is the one taken by looking.
|
|
private static readonly List<SvermWorkerBehaviour> live = new List<SvermWorkerBehaviour>();
|
|
|
|
// ─── Registration ────────────────────────────────────────────────────
|
|
|
|
public static void Register()
|
|
{
|
|
if (registered) return;
|
|
|
|
GameObject source = PrefabManager.Cache.GetPrefab<GameObject>(SourcePrefab);
|
|
if (source == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[Sverm] No '{SourcePrefab}' to build the worker from; no workers.");
|
|
return;
|
|
}
|
|
|
|
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(PrefabName, source);
|
|
if (prefab == null) return;
|
|
|
|
registered = true;
|
|
|
|
prefab.transform.localScale = source.transform.localScale * SvermConfig.WorkerScale;
|
|
|
|
Character character = prefab.GetComponent<Character>();
|
|
if (character != null)
|
|
{
|
|
character.m_health = character.m_health * SvermConfig.WorkerHealthFraction;
|
|
character.m_name = SvermConfig.WorkerDisplayName;
|
|
}
|
|
|
|
// Nothing to farm. A worker that dropped Seeker parts would turn a free, self-replacing
|
|
// labourer into a trophy printer - the same reasoning that strips the thralls' tables.
|
|
CharacterDrop drop = prefab.GetComponent<CharacterDrop>();
|
|
if (drop != null) Object.DestroyImmediate(drop);
|
|
|
|
BaseAI ai = prefab.GetComponent<BaseAI>();
|
|
if (ai != null)
|
|
{
|
|
ai.m_avoidFire = false; // it works next to kilns for a living
|
|
ai.m_afraidOfFire = false;
|
|
}
|
|
|
|
prefab.AddComponent<SvermWorkerBehaviour>();
|
|
|
|
// The Bone Smith's brain, whole: the Forge family of stations, fed from chests that hold
|
|
// the sigil. It takes the MonsterAI tick over completely, so the worker only ever swings at
|
|
// anything because m_fightsBack hands the tick back to vanilla while there is a fight.
|
|
BoneServantAI smith = prefab.AddComponent<BoneServantAI>();
|
|
smith.m_family = ButlerFamily.Forge;
|
|
smith.m_fightsBack = true;
|
|
|
|
PrefabManager.Instance.AddPrefab(prefab);
|
|
Jotunn.Logger.LogInfo($"[Sverm] Registered '{PrefabName}' at {SvermConfig.WorkerScale:P0} of a Seeker.");
|
|
}
|
|
|
|
// ─── Counting and spawning ───────────────────────────────────────────
|
|
|
|
/// <summary>How many of this trade are alive right now. Public: the nest hauls at a rate
|
|
/// set by how many haulers it has, so it has to be able to ask.</summary>
|
|
public static int Count(Job job) => CountLive(job);
|
|
|
|
private static int CountLive(Job job)
|
|
{
|
|
live.RemoveAll(w => w == null || w.Dead);
|
|
|
|
int count = 0;
|
|
foreach (SvermWorkerBehaviour worker in live)
|
|
{
|
|
if (worker.Trade == job) count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
public static int Cap(Job job)
|
|
{
|
|
return job == Job.Farmer ? SvermConfigManagement.Farmers : SvermConfigManagement.Haulers;
|
|
}
|
|
|
|
public static bool HasRoom(Job job) => CountLive(job) < Cap(job);
|
|
|
|
/// <summary>Puts a worker on the roll. Idempotent - Spawn and the worker's own Awake both
|
|
/// call it, and a worker loaded back from the save only ever gets the second.</summary>
|
|
internal static void Track(SvermWorkerBehaviour worker)
|
|
{
|
|
if (worker != null && !live.Contains(worker)) live.Add(worker);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Puts one worker on the ground, bound to the nest that produced it.
|
|
///
|
|
/// The binding lives in the worker's own ZDO, written by the smith's brain: it is the centre
|
|
/// of the work circle, where the worker goes when idle, and what its wither clock asks after.
|
|
/// </summary>
|
|
public static void Spawn(Job job, Transform anchor)
|
|
{
|
|
if (anchor == null || !HasRoom(job)) return;
|
|
|
|
GameObject prefab = ZNetScene.instance != null ? ZNetScene.instance.GetPrefab(PrefabName) : null;
|
|
if (prefab == null) return;
|
|
|
|
// Outside the nest, on the ring the troops hatch on. Put down at the nest's own centre a
|
|
// worker comes out wedged in several metres of collider - fatal for a creature whose whole
|
|
// job is walking to a furnace.
|
|
SvermHiveBehaviour hive = anchor.GetComponent<SvermHiveBehaviour>();
|
|
Vector3 where = hive != null ? SvermBrood.SpawnSpot(hive, 0.5f) : anchor.position + Vector3.up * 0.5f;
|
|
|
|
GameObject spawned = Object.Instantiate(prefab, where, Quaternion.Euler(0f, Random.Range(0f, 360f), 0f));
|
|
if (spawned == null) return;
|
|
|
|
BoneServantAI smith = spawned.GetComponent<BoneServantAI>();
|
|
if (smith != null)
|
|
{
|
|
smith.Bind(anchor.position); // tames it, too
|
|
}
|
|
else
|
|
{
|
|
Character character = spawned.GetComponent<Character>();
|
|
if (character != null) character.SetTamed(true);
|
|
}
|
|
|
|
SvermWorkerBehaviour behaviour = spawned.GetComponent<SvermWorkerBehaviour>()
|
|
?? spawned.AddComponent<SvermWorkerBehaviour>();
|
|
behaviour.Trade = job;
|
|
Track(behaviour);
|
|
}
|
|
|
|
public static bool IsWorker(Character character)
|
|
{
|
|
return character != null && character.GetComponent<SvermWorkerBehaviour>() != null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// One line per worker bound to this nest, for the nest's hover.
|
|
///
|
|
/// The smith's brain writes what it is doing into the worker's ZDO, and the Bone Smith shows it
|
|
/// on its table - the one feedback channel that feature has. A worker's table is the nest, so
|
|
/// this is the same channel. Read on every client: the roll is filled by Awake, which runs
|
|
/// everywhere, and the status comes off the ZDO.
|
|
/// </summary>
|
|
public static string Readout(Vector3 nest)
|
|
{
|
|
StringBuilder text = new StringBuilder();
|
|
|
|
foreach (SvermWorkerBehaviour worker in live)
|
|
{
|
|
if (worker == null || worker.Dead) continue;
|
|
|
|
BoneServantAI smith = worker.GetComponent<BoneServantAI>();
|
|
if (smith == null || !smith.IsBoundTo(nest)) continue;
|
|
|
|
string status = smith.Status;
|
|
text.Append("Worker: ").Append(string.IsNullOrEmpty(status) ? "Waiting." : status).Append('\n');
|
|
}
|
|
|
|
return text.ToString();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// What a worker does on top of the smith's brain: keep what the player built in repair, keep
|
|
/// its orders its own, and tell the brain how big home is.
|
|
///
|
|
/// No farming and no orders. The carrying is the Bone Smith's and lives in BoneServantAI - the
|
|
/// earlier hauling that hung the colony's economy on a walking creature is still gone: the nest
|
|
/// feeds itself from its own chests, and a dead worker only ever costs the forge a trip.
|
|
/// </summary>
|
|
public class SvermWorkerBehaviour : MonoBehaviour
|
|
{
|
|
/// <summary>Hauler by default, and the default matters: a worker loaded back from the save
|
|
/// never goes through Spawn, so this is the trade it is counted under.</summary>
|
|
public SvermWorker.Job Trade = SvermWorker.Job.Hauler;
|
|
|
|
public bool Dead => m_character == null || m_character.IsDead();
|
|
|
|
/// <summary>How far a worker that has no nest on its ZDO looks for one to adopt.</summary>
|
|
private const float AdoptRange = 40f;
|
|
|
|
private Character m_character;
|
|
private MonsterAI m_ai;
|
|
private ZNetView m_nview;
|
|
private BoneServantAI m_smith;
|
|
private float m_nextHomeCheck;
|
|
private float m_nextRepair;
|
|
|
|
private void Awake()
|
|
{
|
|
m_character = GetComponent<Character>();
|
|
m_ai = GetComponent<MonsterAI>();
|
|
m_nview = GetComponent<ZNetView>();
|
|
m_smith = GetComponent<BoneServantAI>();
|
|
|
|
// Counted from the moment it exists, not from the moment it was spawned. The roll used to
|
|
// be kept by Spawn alone, so every worker that came back from the save was invisible to it
|
|
// and the nest hatched a fresh one on top, two minutes at a time, reload after reload.
|
|
SvermWorker.Track(this);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Dead) return;
|
|
|
|
// The owner's round only, like the smith's brain it sits beside: two clients repairing and
|
|
// rebinding the same worker is two answers to one question.
|
|
if (m_nview == null || !m_nview.IsValid() || !m_nview.IsOwner()) return;
|
|
|
|
if (Time.time >= m_nextHomeCheck)
|
|
{
|
|
m_nextHomeCheck = Time.time + 2f;
|
|
KeepHome();
|
|
}
|
|
|
|
if (Time.time < m_nextRepair) return;
|
|
m_nextRepair = Time.time + SvermConfig.WorkerRepairInterval;
|
|
|
|
RepairRound();
|
|
}
|
|
|
|
/// <summary>
|
|
/// The three things the smith's brain cannot know about a nest.
|
|
///
|
|
/// Orders: none, from anyone. The brain never follows, but vanilla holds the tick during a
|
|
/// fight, and an Aswid's Dominate writes a follow target straight into it.
|
|
///
|
|
/// A home: a worker with nothing on its ZDO - hatched before the workers had this brain -
|
|
/// adopts the nearest nest, or it would stand where it is forever with an empty circle.
|
|
///
|
|
/// Its size: re-measured every round, because the nest grows with its stage.
|
|
/// </summary>
|
|
private void KeepHome()
|
|
{
|
|
if (m_ai != null && m_ai.GetFollowTarget() != null) m_ai.SetFollowTarget(null);
|
|
|
|
if (m_smith == null) return;
|
|
|
|
Vector3? anchor = m_smith.Anchor;
|
|
SvermHiveBehaviour nest = anchor != null
|
|
? SvermHive.NearestNest(anchor.Value, 1f)
|
|
: SvermHive.NearestNest(transform.position, AdoptRange);
|
|
if (nest == null) return;
|
|
|
|
if (anchor == null) m_smith.Bind(nest.transform.position);
|
|
|
|
float edge = SvermBrood.RingRadius(nest);
|
|
m_smith.m_homeDistance = edge;
|
|
m_smith.m_radiusBonus = edge;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mends whatever the player built, near where it stands, the way a hammer would.
|
|
///
|
|
/// ONLY WHAT A PLAYER PLACED. Repairing everything would also mend the ruins, the abandoned
|
|
/// buildings and anything else in the world with a WearNTear on it, which is both wrong and
|
|
/// a great deal of work every few seconds. Piece.IsPlacedByPlayer is the game's own answer
|
|
/// to "is this somebody's house".
|
|
///
|
|
/// One piece per round, deliberately. A worker that healed everything in ten metres in a
|
|
/// single frame would read as a cheat rather than as somebody working.
|
|
/// </summary>
|
|
private void RepairRound()
|
|
{
|
|
foreach (Collider collider in Physics.OverlapSphere(transform.position, SvermConfig.WorkerRepairRange))
|
|
{
|
|
if (collider == null) continue;
|
|
|
|
WearNTear wear = collider.GetComponentInParent<WearNTear>();
|
|
if (wear == null) continue;
|
|
|
|
Piece piece = wear.GetComponent<Piece>();
|
|
if (piece == null || !piece.IsPlacedByPlayer()) continue;
|
|
|
|
// Repair returns false when there was nothing to mend, which is how the round
|
|
// moves on to the next piece instead of stopping at the first healthy wall.
|
|
if (!wear.Repair()) continue;
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|