238 lines
10 KiB
C#
238 lines
10 KiB
C#
using System.Collections.Generic;
|
|
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.
|
|
///
|
|
/// TWO JOBS, TWO POOLS, and the pools do not share. Farmers come out of the infected beehives
|
|
/// and work the ground; haulers come out of the nest after Bonemass and carry food into it.
|
|
/// Nine alive at most between them, and neither trade can starve the other of hands - which is
|
|
/// the whole reason they are counted separately instead of out of one number.
|
|
///
|
|
/// 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 stay where they were born, defend
|
|
/// what is there, and never follow.
|
|
///
|
|
/// Half size, half health, half damage. The size and health are set on the prefab; the damage
|
|
/// is halved in the 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);
|
|
|
|
prefab.AddComponent<SvermWorkerBehaviour>();
|
|
|
|
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 one worker on the ground and ties it to whatever produced it.
|
|
///
|
|
/// The anchor is not decoration: it is where the worker idles, what it defends, and the
|
|
/// centre of the three metres it searches for chests. A worker with no anchor has no job.
|
|
/// </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;
|
|
|
|
Vector3 where = anchor.position + Vector3.up * 0.5f + Random.insideUnitSphere * 0.5f;
|
|
where.y = anchor.position.y + 0.5f;
|
|
|
|
GameObject spawned = Object.Instantiate(prefab, where, Quaternion.identity);
|
|
if (spawned == null) return;
|
|
|
|
Character character = spawned.GetComponent<Character>();
|
|
if (character != null) character.SetTamed(true);
|
|
|
|
SvermWorkerBehaviour behaviour = spawned.GetComponent<SvermWorkerBehaviour>()
|
|
?? spawned.AddComponent<SvermWorkerBehaviour>();
|
|
behaviour.Trade = job;
|
|
behaviour.Anchor = anchor;
|
|
|
|
live.Add(behaviour);
|
|
}
|
|
|
|
public static bool IsWorker(Character character)
|
|
{
|
|
return character != null && character.GetComponent<SvermWorkerBehaviour>() != null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// One worker's whole life: stay home, mend what the player built, and fight what comes.
|
|
///
|
|
/// THIS IS THE THIRD DESIGN AND THE FIRST HONEST ONE. The first two hung the colony's economy
|
|
/// on a creature that walks: workers carried food from chests to the hive, and every time one
|
|
/// wandered off or got killed the supply chain went with it - chests full, tank at zero, and
|
|
/// nothing in the log to blame. Hauling belongs to something that cannot walk away, and
|
|
/// feeding the hive is now the player's own job through the shop.
|
|
///
|
|
/// What is left is what a creature is actually good at: being somewhere, keeping it in repair,
|
|
/// and defending it. No carrying, no farming, no orders.
|
|
/// </summary>
|
|
public class SvermWorkerBehaviour : MonoBehaviour
|
|
{
|
|
public SvermWorker.Job Trade;
|
|
public Transform Anchor;
|
|
|
|
public bool Dead => m_character == null || m_character.IsDead();
|
|
|
|
private Character m_character;
|
|
private MonsterAI m_ai;
|
|
private float m_nextHomeCheck;
|
|
private float m_nextRepair;
|
|
|
|
private void Awake()
|
|
{
|
|
m_character = GetComponent<Character>();
|
|
m_ai = GetComponent<MonsterAI>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Dead || Anchor == null) return;
|
|
|
|
KeepHome();
|
|
|
|
if (Time.time < m_nextRepair) return;
|
|
m_nextRepair = Time.time + SvermConfig.WorkerRepairInterval;
|
|
|
|
RepairRound();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ten metres, and told to the AI rather than forced on the transform.
|
|
///
|
|
/// Writing transform.position does not hold a Valheim Character - a Rigidbody and a
|
|
/// ZSyncTransform both overwrite it on the next frame, which is why the earlier leash did
|
|
/// nothing at all and the workers kept wandering off to die. A patrol point is the game's
|
|
/// own way of saying "this is where you live": the AI walks back on its own pathfinding.
|
|
///
|
|
/// SetPatrolPoint takes no argument and posts the creature where it currently STANDS, so
|
|
/// it is put in place first. That raw write does not survive the frame, but it survives
|
|
/// long enough for this one call to read it - and the patrol point is what holds afterwards.
|
|
/// </summary>
|
|
private void KeepHome()
|
|
{
|
|
if (m_ai == null || Time.time < m_nextHomeCheck) return;
|
|
m_nextHomeCheck = Time.time + 2f;
|
|
|
|
if (m_ai.GetFollowTarget() != null) m_ai.SetFollowTarget(null);
|
|
|
|
if (Vector3.Distance(transform.position, Anchor.position) > SvermConfig.WorkerLeashRange)
|
|
{
|
|
transform.position = Anchor.position + Vector3.up * 0.5f;
|
|
}
|
|
|
|
m_ai.SetPatrolPoint();
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|
|
}
|
|
}
|