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

@ -103,6 +103,31 @@ namespace RacesOfValheim
/// with every instance the table raises.</summary>
public ButlerFamily m_family = ButlerFamily.Forge;
/// <summary>
/// Whether this one stands and fights instead of walking home when trouble comes. Off for the
/// Bone Servants, which are pacifists by decision; on for the Sverm's Seeker Workers, which
/// keep the forge AND defend the nest. See <see cref="HandsToVanilla"/>.
/// </summary>
public bool m_fightsBack = false;
/// <summary>
/// How close to the anchor counts as home, and how far past the configured radius it works.
/// 2.5 and 0 for a table. A Sverm nest is metres of collider that grows with its stage, so
/// SvermWorkerBehaviour sets both to the nest's measured edge - otherwise an idle worker would
/// spend its life walking into the trunk, and the work circle would be mostly nest.
/// </summary>
public float m_homeDistance = 2.5f;
public float m_radiusBonus = 0f;
/// <summary>For a servant that fights back: how close an enemy has to be to stop the work, how
/// often that is asked, and how long the fight holds after the last sign of one.</summary>
private const float FightRange = 12f;
private const float FightScanInterval = 1f;
private const float FightLinger = 5f;
private float m_nextFightScan;
private float m_fightUntil;
private ZNetView m_nview;
private Character m_character;
private MonsterAI m_ai;
@ -207,13 +232,58 @@ namespace RacesOfValheim
}
/// <summary>Hit by something: stop working and walk home. It never fights back - there is
/// nothing in this class that could.</summary>
/// nothing in this class that could. A servant that fights back ignores this: vanilla already
/// holds the attacker as its target (MonsterAI.OnDamaged calls SetTarget, read off the IL), and
/// <see cref="HandsToVanilla"/> gives it the tick on the next frame.</summary>
public void NoteDamage()
{
if (m_fightsBack) return;
m_fleeUntil = Time.time + FleeSeconds;
m_job = null;
}
/// <summary>
/// True while vanilla's MonsterAI should have the tick instead of the work - only ever for a
/// servant that fights back, and only on the client that runs it.
///
/// VANILLA DOES THE FIGHTING, this only decides when. Two things start a fight: a target the
/// MonsterAI already holds - which covers being hit, see NoteDamage - and an enemy inside
/// FightRange, found with BaseAI.FindClosestEnemy. That one is a static sweep of distance and
/// IsEnemy with no line of sight (IL), which is why the range is short. Either holds the fight
/// for FightLinger seconds, so the worker does not flicker between swinging and walking to the
/// chest on alternate ticks.
///
/// The job is dropped, not the payload: whatever it was carrying stays in the ZDO, and the
/// first plan after the fight carries it on to where it was going.
/// </summary>
public bool HandsToVanilla()
{
if (!m_fightsBack || m_ai == null || m_character == null || m_character.IsDead()) return false;
if (m_nview == null || !m_nview.IsValid() || !m_nview.IsOwner()) return false;
Character target = m_ai.GetTargetCreature();
if (target != null && !target.IsDead())
{
m_fightUntil = Time.time + FightLinger;
}
else if (Time.time >= m_nextFightScan)
{
m_nextFightScan = Time.time + FightScanInterval;
if (BaseAI.FindClosestEnemy(m_character, transform.position, FightRange) != null)
{
m_fightUntil = Time.time + FightLinger;
}
}
if (Time.time >= m_fightUntil) return false;
m_job = null;
m_nextPlan = 0f; // the fight moved it; plan afresh the moment it hands back
SetStatus("Fighting.");
return true;
}
// ================================================================ the tick
/// <summary>Everything. Returns what MonsterAI.UpdateAI would have returned.</summary>
@ -327,7 +397,7 @@ namespace RacesOfValheim
List<Piece> pieces = AllPiecesField != null ? AllPiecesField.GetValue(null) as List<Piece> : null;
if (pieces == null) return;
float radius = ButlerConfig.WorkRadius.Value;
float radius = ButlerConfig.WorkRadius.Value + m_radiusBonus;
float radiusSq = radius * radius;
for (int i = 0; i < pieces.Count; i++)
@ -923,7 +993,7 @@ namespace RacesOfValheim
return;
}
MoveToPoint(dt, anchor.Value, 2.5f, false);
MoveToPoint(dt, anchor.Value, m_homeDistance, false);
}
// ================================================================ the ground
@ -1356,6 +1426,10 @@ namespace RacesOfValheim
BoneServantAI servant = __instance.GetComponent<BoneServantAI>();
if (servant == null) return true;
// The one way back into vanilla: a servant that fights back hands over the whole tick for
// as long as there is a fight. Always false for the Bone Servants themselves.
if (servant.HandsToVanilla()) return true;
__result = servant.Tick(__0);
return false;
}