minor bugfix and pad support

This commit is contained in:
Ferdinando 2026-09-12 10:42:01 +02:00
commit 6e87be3c77
21 changed files with 574 additions and 85 deletions

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text;
using Jotunn.Managers;
using UnityEngine;
@ -8,18 +9,21 @@ namespace RacesOfValheim
/// 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.
/// 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 stay where they were born, defend
/// what is there, and never follow.
/// 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 the damage patch, because a creature's blow comes from its default weapon and
/// 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
@ -68,8 +72,22 @@ namespace RacesOfValheim
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.");
}
@ -99,11 +117,18 @@ namespace RacesOfValheim
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 and ties it to whatever produced it.
/// Puts one worker on the ground, bound to the nest that 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.
/// 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)
{
@ -112,50 +137,87 @@ namespace RacesOfValheim
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;
// 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.identity);
GameObject spawned = Object.Instantiate(prefab, where, Quaternion.Euler(0f, Random.Range(0f, 360f), 0f));
if (spawned == null) return;
Character character = spawned.GetComponent<Character>();
if (character != null) character.SetTamed(true);
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;
behaviour.Anchor = anchor;
live.Add(behaviour);
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>
/// One worker's whole life: stay home, mend what the player built, and fight what comes.
/// 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.
///
/// 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.
/// 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
{
public SvermWorker.Job Trade;
public Transform Anchor;
/// <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;
@ -163,13 +225,28 @@ namespace RacesOfValheim
{
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 || Anchor == null) return;
if (Dead) return;
KeepHome();
// 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;
@ -178,30 +255,33 @@ namespace RacesOfValheim
}
/// <summary>
/// Ten metres, and told to the AI rather than forced on the transform.
/// The three things the smith's brain cannot know about a nest.
///
/// 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.
/// 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.
///
/// 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.
/// 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 || Time.time < m_nextHomeCheck) return;
m_nextHomeCheck = Time.time + 2f;
if (m_ai != null && m_ai.GetFollowTarget() != null) m_ai.SetFollowTarget(null);
if (m_ai.GetFollowTarget() != null) m_ai.SetFollowTarget(null);
if (m_smith == null) return;
if (Vector3.Distance(transform.position, Anchor.position) > SvermConfig.WorkerLeashRange)
{
transform.position = Anchor.position + Vector3.up * 0.5f;
}
Vector3? anchor = m_smith.Anchor;
SvermHiveBehaviour nest = anchor != null
? SvermHive.NearestNest(anchor.Value, 1f)
: SvermHive.NearestNest(transform.position, AdoptRange);
if (nest == null) return;
m_ai.SetPatrolPoint();
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>