692 lines
32 KiB
C#
692 lines
32 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using HarmonyLib;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// One conjured servant, of any race. Keeps itself within its leash of the station that made it,
|
|
/// unless the right kind of player has told it to follow.
|
|
///
|
|
/// This started life as DraughThrall and was generalised the moment the Skeleton needed the same
|
|
/// thing - which is the call ItemProgression_Guida.txt 8.3 says to make BEFORE writing the second
|
|
/// copy rather than after. Nothing in here is draugr-specific: a leash, a wither timer, a follow
|
|
/// command and an anchor are the same problem whoever raised the body.
|
|
///
|
|
/// The leash is not custom pathing - it is vanilla's own patrol behaviour, pointed at the station.
|
|
/// BaseAI already has m_patrol and m_patrolPoint and already wanders around that point inside
|
|
/// m_randomMoveRange, which each prefab sets to its own leash. All this component does is keep the
|
|
/// point pinned, which is also what makes "stay" send one home rather than root it wherever it
|
|
/// happened to be standing.
|
|
///
|
|
/// The anchor lives in the creature's own ZDO as a Vector3 and doubles as the mark of which
|
|
/// station owns it - so a station can count its own even when one is three biomes away following
|
|
/// its master.
|
|
/// </summary>
|
|
public class RaisedThrall : MonoBehaviour
|
|
{
|
|
// Deliberately still spelled "draugh". These keys are in players' save files already, on
|
|
// thralls raised before this class was generalised; renaming them would leave every existing
|
|
// thrall anchorless - no leash, no wither, no orders - for the sake of a tidier string.
|
|
public const string KeyAnchor = "RoV_draughAnchor";
|
|
public const string KeyOrphanAt = "RoV_draughOrphanAt";
|
|
|
|
// A hard, station-independent lifespan - unlike the wither above, which only starts once
|
|
// an anchor's station is gone. Nothing sets this by default; a thrall raised at a real
|
|
// pile never expires on its own. SummonOnKill uses it because its skeletons are never
|
|
// bound to a pile in the first place, so the anchor/wither pair has nothing to key off.
|
|
public const string KeyExpiresAt = "RoV_thrallExpiresAt";
|
|
|
|
// WAIT: where it was told to stay. A Vector3 in the creature's own ZDO, zero meaning "not waiting".
|
|
// It has to be state that survives, because TickLeash sends anything that is not following home
|
|
// every two seconds - a "stay" that was only a follow target of null would walk back to the station
|
|
// on the next tick. With this set, the leash pins THERE instead of at the anchor.
|
|
public const string KeyWaitPoint = "RoV_waitPoint";
|
|
|
|
// A ship's troop sent home: the ship's roster takes these aboard (BoneShipRoster).
|
|
public const string KeyRecall = "RoV_bshRecall";
|
|
|
|
/// <summary>How far a waiting thrall will wander from the spot it was told to hold, metres.</summary>
|
|
private const float WaitRoam = 2.5f;
|
|
|
|
// ─── Set on the prefab at registration ───────────────────────────────
|
|
|
|
/// <summary>Groups thralls for follower caps and for a station's per-kind roster.</summary>
|
|
public string m_kind = "";
|
|
|
|
/// <summary>Only a player of this race may give orders. Empty means anyone may.</summary>
|
|
public string m_commandingRace = "";
|
|
|
|
/// <summary>How many of this KIND may follow one player at once. -1 for no limit.</summary>
|
|
public int m_maxFollowers = -1;
|
|
|
|
public float m_witherSeconds = 60f;
|
|
public float m_homeCheckRange = 20f;
|
|
|
|
/// <summary>Shown in the refusal when a station is not this player's to command.</summary>
|
|
public string m_refusalMessage = "$rov_msg_thrall_refusal_default";
|
|
|
|
/// <summary>
|
|
/// How many thralls of OTHER kinds, from the same station, come along when this one is
|
|
/// commanded directly. 0 for a thrall that only speaks for itself.
|
|
///
|
|
/// This is what makes a Rancid Remains a squad leader: clicking it takes it and five of the
|
|
/// rank and file, rather than the whole garrison the pile would send. Only applies to a direct
|
|
/// click - a station moving its whole line calls SetFollowing itself, so the two cannot nest.
|
|
/// </summary>
|
|
public int m_escortCount = 0;
|
|
|
|
// SetPatrolPoint(Vector3) is private - only the parameterless overload, which uses the
|
|
// creature's current position, is public. Resolved once here rather than per call.
|
|
private static readonly System.Reflection.MethodInfo SetPatrolPointMethod =
|
|
AccessTools.Method(typeof(BaseAI), "SetPatrolPoint", new[] { typeof(Vector3) });
|
|
|
|
/// <summary>
|
|
/// Every station currently loaded that owns thralls. Registered by the station's own behaviour
|
|
/// so a thrall can ask "is my station still standing?" without sweeping ZDOs or raycasting on a
|
|
/// two-second timer, ten thralls at a time.
|
|
///
|
|
/// TRANSFORMS, not positions, and that is the whole bug this once had: the first version cached
|
|
/// transform.position at Awake, which is not yet the object's final placement. Every lookup then
|
|
/// missed, every thrall decided it was an orphan, and with the Skeleton's zero-second wither the
|
|
/// whole garrison died about four seconds after it rose. Reading .position at query time is what
|
|
/// the Draugh's own list did before it was generalised, and why that one worked.
|
|
/// </summary>
|
|
private static readonly List<Transform> LiveStations = new List<Transform>();
|
|
|
|
public static void RegisterStation(Transform station)
|
|
{
|
|
if (station != null && !LiveStations.Contains(station)) LiveStations.Add(station);
|
|
}
|
|
|
|
public static void UnregisterStation(Transform station) => LiveStations.Remove(station);
|
|
|
|
/// <summary>True if a loaded station stands at that point. Only meaningful when the asker is
|
|
/// close enough that the station would be loaded if it existed - see m_homeCheckRange.</summary>
|
|
public static bool StationStandsAt(Vector3 position)
|
|
{
|
|
for (int i = LiveStations.Count - 1; i >= 0; i--)
|
|
{
|
|
Transform station = LiveStations[i];
|
|
if (station == null) { LiveStations.RemoveAt(i); continue; } // destroyed without notice
|
|
|
|
if (Vector3.SqrMagnitude(station.position - position) < 0.25f) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private Character m_character;
|
|
private BaseAI m_ai;
|
|
private MonsterAI m_monsterAI;
|
|
private ZNetView m_nview;
|
|
|
|
// The prefab's own roaming radius, kept so a wait (which tightens it) can give it back.
|
|
private float m_roamRange;
|
|
|
|
private void Awake()
|
|
{
|
|
m_character = GetComponent<Character>();
|
|
m_ai = GetComponent<BaseAI>();
|
|
m_monsterAI = GetComponent<MonsterAI>();
|
|
m_nview = GetComponent<ZNetView>();
|
|
m_roamRange = m_ai != null ? m_ai.m_randomMoveRange : 0f;
|
|
|
|
InvokeRepeating("TickLeash", 2f, 2f);
|
|
}
|
|
|
|
/// <summary>The three orders a thrall answers to, however they are given.</summary>
|
|
public enum Order { Follow, Wait, Home }
|
|
|
|
/// <summary>Where it was told to hold, or null if it is not waiting.</summary>
|
|
public Vector3? WaitPoint
|
|
{
|
|
get
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid()) return null;
|
|
|
|
Vector3 point = m_nview.GetZDO().GetVec3(KeyWaitPoint, Vector3.zero);
|
|
return point == Vector3.zero ? (Vector3?)null : point;
|
|
}
|
|
}
|
|
|
|
public bool IsWaiting => WaitPoint != null;
|
|
|
|
/// <summary>True for a troop that belongs to a bone ship: it answers to anyone, as the ship does.</summary>
|
|
public bool IsShipTroop =>
|
|
m_nview != null && m_nview.IsValid() && m_nview.GetZDO().GetZDOID(BoneShipRoster.KeyShip) != ZDOID.None;
|
|
|
|
/// <summary>Where the station that made this one stands, or null if it was not made by one.</summary>
|
|
public Vector3? Anchor
|
|
{
|
|
get
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid()) return null;
|
|
|
|
Vector3 anchor = m_nview.GetZDO().GetVec3(KeyAnchor, Vector3.zero);
|
|
return anchor == Vector3.zero ? (Vector3?)null : anchor;
|
|
}
|
|
}
|
|
|
|
public string Kind => m_kind;
|
|
|
|
public bool IsFollowing(Player player)
|
|
{
|
|
return m_monsterAI != null && player != null && m_monsterAI.GetFollowTarget() == player.gameObject;
|
|
}
|
|
|
|
/// <summary>Called at spawn by the station. Tames it, marks it, and sends it home.</summary>
|
|
public void Bind(Vector3 anchor)
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid()) return;
|
|
|
|
m_nview.GetZDO().Set(KeyAnchor, anchor);
|
|
if (m_monsterAI != null) m_monsterAI.MakeTame();
|
|
if (m_character != null) m_character.SetTamed(true);
|
|
|
|
GoHome();
|
|
}
|
|
|
|
// ---------------------------------------------------------------- the leash
|
|
|
|
private void TickLeash()
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid() || !m_nview.IsOwner()) return;
|
|
if (m_character == null || m_character.IsDead()) return;
|
|
|
|
// A hard cap, checked before anything anchor-based - it has to fire regardless of
|
|
// where this thrall is standing or whether it is following, unlike the wither below.
|
|
if (TickExpiry()) return;
|
|
|
|
// Checked before anything else, and NOT skipped while following: a thrall out with its
|
|
// master still has to wither if the station behind it has fallen.
|
|
if (TickWither()) return;
|
|
|
|
// Following overrides the leash entirely; that is the whole point of the command.
|
|
if (m_monsterAI != null && m_monsterAI.GetFollowTarget() != null) return;
|
|
|
|
GoHome();
|
|
}
|
|
|
|
/// <summary>Sets a hard lifespan from now, independent of any station anchor. Overwrites
|
|
/// any expiry already set.</summary>
|
|
public void SetExpiry(float seconds)
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid() || ZNet.instance == null) return;
|
|
m_nview.GetZDO().Set(KeyExpiresAt, (ZNet.instance.GetTime() + TimeSpan.FromSeconds(seconds)).Ticks);
|
|
}
|
|
|
|
/// <summary>True once it has killed this one for running out the clock set by SetExpiry.
|
|
/// A no-op for any thrall that never had one set (the ZDO key stays at its default 0).</summary>
|
|
private bool TickExpiry()
|
|
{
|
|
long expiresAt = m_nview.GetZDO().GetLong(KeyExpiresAt, 0L);
|
|
if (expiresAt == 0L || ZNet.instance.GetTime() < new DateTime(expiresAt)) return false;
|
|
|
|
Kill();
|
|
return true;
|
|
}
|
|
|
|
/// <summary>Starts the clock when the station is gone, and stops the thrall when it runs out.
|
|
/// Returns true once it has killed this one, so the caller stops bothering with it.</summary>
|
|
private bool TickWither()
|
|
{
|
|
Vector3? anchor = Anchor;
|
|
if (anchor == null || ZNet.instance == null) return false;
|
|
|
|
long stamp = m_nview.GetZDO().GetLong(KeyOrphanAt, 0L);
|
|
|
|
if (stamp == 0L)
|
|
{
|
|
// The station marks its own thralls when it falls, but it can only mark the ones that
|
|
// are loaded at that moment. This is the other half: a thrall that was away comes
|
|
// home, finds nothing there, and marks itself.
|
|
//
|
|
// Deliberately only asked near home. Further out the station's zone is almost
|
|
// certainly unloaded, so "nothing here" would be a lie - and acting on it would kill
|
|
// the escort of anyone who walked away from their base.
|
|
if (Vector3.SqrMagnitude(transform.position - anchor.Value) >
|
|
m_homeCheckRange * m_homeCheckRange) return false;
|
|
|
|
if (StationStandsAt(anchor.Value)) return false;
|
|
|
|
MarkOrphaned();
|
|
return false;
|
|
}
|
|
|
|
// Rebuilt in time, on the same spot: the clock is cleared and the thrall lives. Safe to
|
|
// ask unconditionally - StationStandsAt can only answer true where one is actually
|
|
// loaded, so being far from home simply never rescues anything. And it opens no exploit:
|
|
// a station on the same spot adopts these thralls by anchor anyway.
|
|
if (StationStandsAt(anchor.Value))
|
|
{
|
|
m_nview.GetZDO().Set(KeyOrphanAt, 0L);
|
|
return false;
|
|
}
|
|
|
|
double elapsed = (ZNet.instance.GetTime() - new DateTime(stamp)).TotalSeconds;
|
|
if (elapsed < m_witherSeconds) return false;
|
|
|
|
Kill();
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts the wither clock, once. Called by the station as it is destroyed, and by the thrall
|
|
/// itself if it comes home to nothing. Idempotent so the two cannot restart each other's clock
|
|
/// and keep an orphan alive forever.
|
|
/// </summary>
|
|
public void MarkOrphaned()
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid() || ZNet.instance == null) return;
|
|
if (m_nview.GetZDO().GetLong(KeyOrphanAt, 0L) != 0L) return;
|
|
|
|
m_nview.ClaimOwnership();
|
|
m_nview.GetZDO().Set(KeyOrphanAt, ZNet.instance.GetTime().Ticks);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pins the patrol point back onto the station. Cheap enough to call on a timer, and calling it
|
|
/// repeatedly is what stops a thrall drifting after it has been pushed, knocked back or told to
|
|
/// stay somewhere far from home.
|
|
/// </summary>
|
|
private void GoHome()
|
|
{
|
|
if (m_ai == null) return;
|
|
|
|
// A wait wins over the anchor: the leash holds the spot it was told to hold, and tightly.
|
|
Vector3? wait = WaitPoint;
|
|
if (wait != null)
|
|
{
|
|
SetRoam(Mathf.Min(WaitRoam, m_roamRange > 0f ? m_roamRange : WaitRoam));
|
|
PinPatrol(wait.Value);
|
|
return;
|
|
}
|
|
|
|
SetRoam(m_roamRange);
|
|
|
|
Vector3? anchor = Anchor;
|
|
if (anchor == null) return;
|
|
|
|
PinPatrol(anchor.Value);
|
|
}
|
|
|
|
/// <summary>Points the patrol at a place. Also how a ship's roster walks a recalled troop to its ship.</summary>
|
|
public void WalkTo(Vector3 point)
|
|
{
|
|
if (m_ai != null) PinPatrol(point);
|
|
}
|
|
|
|
private void PinPatrol(Vector3 point)
|
|
{
|
|
if (SetPatrolPointMethod != null)
|
|
{
|
|
SetPatrolPointMethod.Invoke(m_ai, new object[] { point });
|
|
}
|
|
else
|
|
{
|
|
// Fall back to writing the two fields the private method writes.
|
|
Traverse traverse = Traverse.Create(m_ai);
|
|
traverse.Field("m_patrolPoint").SetValue(point);
|
|
traverse.Field("m_patrol").SetValue(true);
|
|
}
|
|
}
|
|
|
|
/// <summary>Only for a creature that roams at all: the sentries and golems are rooted at zero and stay so.</summary>
|
|
private void SetRoam(float range)
|
|
{
|
|
if (m_ai != null && m_roamRange > 0f) m_ai.m_randomMoveRange = range;
|
|
}
|
|
|
|
/// <summary>Forgets a wait and a recall: any new order starts from nothing.</summary>
|
|
private void ClearStay()
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid()) return;
|
|
|
|
m_nview.ClaimOwnership();
|
|
m_nview.GetZDO().Set(KeyWaitPoint, Vector3.zero);
|
|
m_nview.GetZDO().Set(KeyRecall, false);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- commands
|
|
|
|
public bool MayBeCommandedBy(Player player)
|
|
{
|
|
if (player == null) return false;
|
|
if (string.IsNullOrEmpty(m_commandingRace)) return true;
|
|
|
|
// A bone ship is open to every race, so its crew answers to whoever is on the deck.
|
|
if (IsShipTroop) return true;
|
|
|
|
return player.m_customData != null &&
|
|
player.m_customData.TryGetValue("Race", out string race) &&
|
|
race == m_commandingRace;
|
|
}
|
|
|
|
/// <summary>Sends it after the player, or home. Returns false if it refused.</summary>
|
|
public bool SetFollowing(Player player, bool follow, bool announce)
|
|
{
|
|
if (player == null || m_monsterAI == null || m_character == null) return false;
|
|
|
|
if (!MayBeCommandedBy(player))
|
|
{
|
|
if (announce) player.Message(MessageHud.MessageType.Center, m_refusalMessage);
|
|
return false;
|
|
}
|
|
|
|
if (!follow)
|
|
{
|
|
ClearStay();
|
|
m_monsterAI.SetFollowTarget(null);
|
|
GoHome();
|
|
|
|
// A ship's troop has no anchor to go home to: home is the ship, and the ship's roster, which
|
|
// sees this flag, walks it there and takes it aboard.
|
|
if (IsShipTroop) m_nview.GetZDO().Set(KeyRecall, true);
|
|
|
|
if (announce) player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_thrall_returns", m_character.m_name));
|
|
return true;
|
|
}
|
|
|
|
// Counted fresh on every command so deaths and dismissals free slots up on their own,
|
|
// with no tally to drift out of step.
|
|
if (m_maxFollowers >= 0 && CountFollowers(player, m_kind) >= m_maxFollowers)
|
|
{
|
|
if (announce)
|
|
{
|
|
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_thrall_only_may_walk_with", m_maxFollowers));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// THE PATROL POINT GOES OFF while it follows, and GoHome puts it back when it is let go.
|
|
// MonsterAI.UpdateTarget measures a tame creature's target from its patrol point whenever
|
|
// it has one, and only falls back to the follow target when it has none (IL). Left on, a
|
|
// thrall walking beside its master kept judging every enemy by its distance from the
|
|
// STATION - so the further from home, the fewer fights it would take. ResetPatrolPoint
|
|
// writes the ZDO as well as the field, which matters: GetPatrolPoint re-reads the ZDO
|
|
// every second and would switch it straight back on.
|
|
ClearStay();
|
|
SetRoam(m_roamRange);
|
|
if (m_ai != null) m_ai.ResetPatrolPoint();
|
|
|
|
m_monsterAI.SetFollowTarget(player.gameObject);
|
|
if (announce) player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_thrall_follows", m_character.m_name));
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tells it to hold where it stands: it stops following, forgets the station for now, and keeps to a
|
|
/// couple of metres of this spot until given another order. Returns false if it refused.
|
|
/// </summary>
|
|
public bool SetWaiting(Player player, bool announce)
|
|
{
|
|
if (player == null || m_monsterAI == null || m_character == null) return false;
|
|
if (m_nview == null || !m_nview.IsValid()) return false;
|
|
|
|
if (!MayBeCommandedBy(player))
|
|
{
|
|
if (announce) player.Message(MessageHud.MessageType.Center, m_refusalMessage);
|
|
return false;
|
|
}
|
|
|
|
ClearStay();
|
|
m_nview.GetZDO().Set(KeyWaitPoint, transform.position);
|
|
|
|
m_monsterAI.SetFollowTarget(null);
|
|
GoHome();
|
|
|
|
if (announce) player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_thrall_waits", m_character.m_name));
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// One key, three orders in a ring: follow, then wait, then home, then follow again. It used to be
|
|
/// two (follow, home); "wait" slots in between because it is the step you want most often and the
|
|
/// one you can least afford to get by going all the way home and back. Escorts (a Rancid Remains'
|
|
/// squad) go with it on follow and home, as before; a wait is for this one alone.
|
|
/// </summary>
|
|
public void ToggleCommand(Player player)
|
|
{
|
|
Order next = IsFollowing(player) ? Order.Wait : IsWaiting ? Order.Home : Order.Follow;
|
|
|
|
if (next == Order.Wait)
|
|
{
|
|
SetWaiting(player, announce: true);
|
|
return;
|
|
}
|
|
|
|
bool follow = next == Order.Follow;
|
|
if (!SetFollowing(player, follow, announce: m_escortCount <= 0)) return;
|
|
if (m_escortCount <= 0) return;
|
|
|
|
// A squad, not the whole line. Anchor-bound, other kinds only - the rank and file, never
|
|
// another leader - and capped at m_escortCount.
|
|
Vector3? anchor = Anchor;
|
|
if (anchor == null) return;
|
|
|
|
int taken = 0;
|
|
foreach (RaisedThrall other in BoundTo(anchor.Value))
|
|
{
|
|
if (other == this || other.m_kind == m_kind) continue;
|
|
|
|
// Going out: only pick up ones that are not already out. Coming home: only send back
|
|
// the ones that are actually with this player, so another squad is left alone.
|
|
if (follow ? other.IsFollowing(player) : !other.IsFollowing(player)) continue;
|
|
if (taken >= m_escortCount) break;
|
|
|
|
if (other.SetFollowing(player, follow, announce: false)) taken++;
|
|
}
|
|
|
|
player.Message(MessageHud.MessageType.Center, (follow ? ModText.Get("rov_msg_thrall_rises_with", m_character.m_name, taken) : ModText.Get("rov_msg_thrall_stands_down_with", m_character.m_name, taken)));
|
|
}
|
|
|
|
private static int CountFollowers(Player player, string kind)
|
|
{
|
|
int count = 0;
|
|
foreach (Character character in Character.GetAllCharacters())
|
|
{
|
|
if (character == null || character.IsDead()) continue;
|
|
|
|
RaisedThrall thrall = character.GetComponent<RaisedThrall>();
|
|
if (thrall == null || thrall.m_kind != kind) continue;
|
|
if (thrall.IsFollowing(player)) count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/// <summary>Every living thrall bound to the station at this position.</summary>
|
|
public static List<RaisedThrall> BoundTo(Vector3 anchor)
|
|
{
|
|
List<RaisedThrall> bound = new List<RaisedThrall>();
|
|
|
|
foreach (Character character in Character.GetAllCharacters())
|
|
{
|
|
if (character == null || character.IsDead()) continue;
|
|
|
|
RaisedThrall thrall = character.GetComponent<RaisedThrall>();
|
|
if (thrall == null) continue;
|
|
|
|
Vector3? own = thrall.Anchor;
|
|
// A generous epsilon: a station never moves, so this only has to survive float
|
|
// round-tripping through the ZDO.
|
|
if (own != null && Vector3.SqrMagnitude(own.Value - anchor) < 0.25f) bound.Add(thrall);
|
|
}
|
|
|
|
return bound;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- the call
|
|
|
|
/// <summary>Earshot. A shouted gesture reaches what is nearby, not the whole world.</summary>
|
|
public const float TauntRange = 30f;
|
|
|
|
/// <summary>
|
|
/// The emotes that give the three group orders. SETS rather than single strings because the emote
|
|
/// names are not in the assembly at all - Valheim builds them from what the player types in
|
|
/// chat, so unlike every other name in this project these could not be read off the game and
|
|
/// verified. The patch below logs whatever it actually receives, so a wrong guess here is one
|
|
/// line away from being right instead of a mystery.
|
|
///
|
|
/// comehere - follow (the original)
|
|
/// thumbsup - wait (decided 2026-10-05; "sit" was ruled out, it is what you do at a fire)
|
|
/// wave - go home (decided; a greeting to another player will also send a nearby crew home -
|
|
/// the price of the choice, to be watched in play)
|
|
/// </summary>
|
|
private static readonly HashSet<string> FollowEmotes = new HashSet<string> { "comehere", "come_here", "followme" };
|
|
private static readonly HashSet<string> WaitEmotes = new HashSet<string> { "thumbsup", "thumbs_up" };
|
|
private static readonly HashSet<string> HomeEmotes = new HashSet<string> { "wave" };
|
|
|
|
/// <summary>Which group order an emote gives, or null for none.</summary>
|
|
public static Order? OrderOfEmote(string emote)
|
|
{
|
|
if (emote == null) return null;
|
|
|
|
string name = emote.ToLowerInvariant();
|
|
if (FollowEmotes.Contains(name)) return Order.Follow;
|
|
if (WaitEmotes.Contains(name)) return Order.Wait;
|
|
if (HomeEmotes.Contains(name)) return Order.Home;
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Everything within earshot that answers to this player gets the order. Follow is the old call to
|
|
/// arms; wait and home are its two new sisters, and "home" is a dismissal after all - which the old
|
|
/// design kept for the station and for a click, and now has a gesture of its own.
|
|
///
|
|
/// The per-race follower cap needs no special handling - SetFollowing enforces m_maxFollowers
|
|
/// itself, so the Skeleton's whole garrison answers (-1, no limit) and the Draugh's gets three
|
|
/// draugr and one officer, from exactly the same call. The Butler is not a RaisedThrall (it was kept
|
|
/// out of this class on purpose) so it never answers any of them, which is the decision.
|
|
/// </summary>
|
|
public static void GroupOrder(Player player, Order order)
|
|
{
|
|
if (player == null) return;
|
|
|
|
int answered = 0;
|
|
int refused = 0;
|
|
|
|
foreach (Character character in Character.GetAllCharacters())
|
|
{
|
|
if (character == null || character.IsDead()) continue;
|
|
|
|
RaisedThrall thrall = character.GetComponent<RaisedThrall>();
|
|
if (thrall == null) continue;
|
|
if (!thrall.MayBeCommandedBy(player)) continue;
|
|
if (Vector3.SqrMagnitude(character.transform.position - player.transform.position) >
|
|
TauntRange * TauntRange) continue;
|
|
|
|
bool following = thrall.IsFollowing(player);
|
|
|
|
switch (order)
|
|
{
|
|
case Order.Follow:
|
|
if (following) continue;
|
|
if (thrall.SetFollowing(player, true, announce: false)) answered++;
|
|
else refused++;
|
|
break;
|
|
|
|
case Order.Wait:
|
|
if (thrall.IsWaiting && !following) continue;
|
|
if (thrall.SetWaiting(player, announce: false)) answered++;
|
|
break;
|
|
|
|
case Order.Home:
|
|
// Already home: not following, not waiting. A wait and a follow both end here.
|
|
if (!following && !thrall.IsWaiting) continue;
|
|
if (thrall.SetFollowing(player, false, announce: false)) answered++;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (answered > 0)
|
|
{
|
|
string key = order == Order.Follow ? "rov_msg_thrall_answer_call"
|
|
: order == Order.Wait ? "rov_msg_thrall_answer_wait"
|
|
: "rov_msg_thrall_answer_home";
|
|
player.Message(MessageHud.MessageType.Center, ModText.Get(key, answered));
|
|
}
|
|
else if (refused > 0)
|
|
{
|
|
// The only way SetFollowing refuses a thrall that may be commanded is the cap.
|
|
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_thrall_no_more_may_walk_with"));
|
|
}
|
|
}
|
|
|
|
/// <summary>The original call, kept under its old name for the places that still use it.</summary>
|
|
public static void CallToArms(Player player) => GroupOrder(player, Order.Follow);
|
|
|
|
public static bool IsTauntEmote(string emote)
|
|
{
|
|
return OrderOfEmote(emote) == Order.Follow;
|
|
}
|
|
|
|
public void Kill()
|
|
{
|
|
if (m_character == null || m_character.IsDead()) return;
|
|
|
|
HitData hit = new HitData();
|
|
hit.m_damage.m_damage = 1E+10f;
|
|
hit.m_hitType = HitData.HitType.Undefined;
|
|
m_character.ApplyDamage(hit, true, false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Routes E on a thrall to <see cref="RaisedThrall.ToggleCommand"/>, before vanilla resolves the
|
|
/// target itself.
|
|
///
|
|
/// This is a prefix on Player.Interact rather than an Interactable on the thrall, and that is not
|
|
/// a style choice. AswidPatches' own Character.Awake postfix adds AswidThrall - also Interactable
|
|
/// - to essentially every mob carrying a MonsterAI, and it wins vanilla's
|
|
/// GetComponentInParent<Interactable>() race on whatever it touches. That is the exact bug
|
|
/// FactionMerchantInteractPatch exists to work around, where pressing E called AswidThrall.Interact
|
|
/// instead of Trader.Interact, silently and with no error. Handling our own thralls here sidesteps
|
|
/// component order entirely instead of depending on it.
|
|
///
|
|
/// The feedback is a centre-screen message and not hover text for the same reason: Character is
|
|
/// itself Hoverable and sits ahead of everything added later, so hover is a race this cannot win
|
|
/// cleanly. The station's own hover text carries the roster and the rules.
|
|
/// </summary>
|
|
/// <summary>
|
|
/// The "come here" gesture calls every thrall in earshot to its master.
|
|
///
|
|
/// A POSTFIX with positional binding and no __result, which is the safest shape available: postfixes
|
|
/// need no return value, so this cannot repeat the PlacePiece failure where asking for a __result on
|
|
/// a void method threw at patch time and stopped the rest of the mod being patched.
|
|
///
|
|
/// The emote name is logged on every emote the local player performs, because the names are the one
|
|
/// class of string in this project that could not be verified against the game - they are built from
|
|
/// the player's own chat input, not stored in the assembly. Do the gesture once and the log names it.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Player), "StartEmote")]
|
|
public static class RaisedThrallTauntPatch
|
|
{
|
|
public static void Postfix(Player __instance, string __0)
|
|
{
|
|
if (__instance != Player.m_localPlayer) return;
|
|
|
|
RaisedThrall.Order? order = RaisedThrall.OrderOfEmote(__0);
|
|
|
|
Jotunn.Logger.LogInfo($"[Thrall] emote '{__0}'" + (order != null ? $" - order: {order}." : "."));
|
|
|
|
if (order != null) RaisedThrall.GroupOrder(__instance, order.Value);
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Player), "Interact")]
|
|
public static class RaisedThrallInteractPatch
|
|
{
|
|
public static bool Prefix(Player __instance, GameObject go, bool hold, bool alt)
|
|
{
|
|
if (go == null || hold) return true;
|
|
if (__instance != Player.m_localPlayer) return true;
|
|
|
|
RaisedThrall thrall = go.GetComponentInParent<RaisedThrall>();
|
|
if (thrall == null) return true; // not ours - let vanilla's own resolution run
|
|
|
|
thrall.ToggleCommand(__instance);
|
|
return false;
|
|
}
|
|
}
|
|
}
|