bugfix 06/10/26 + bone ship

This commit is contained in:
Ferdinando 2026-10-06 11:36:18 +02:00
commit 553303d0fd
27 changed files with 3206 additions and 226 deletions

View file

@ -38,6 +38,18 @@ namespace RacesOfValheim
// 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>
@ -109,16 +121,41 @@ namespace RacesOfValheim
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
{
@ -255,22 +292,62 @@ namespace RacesOfValheim
/// </summary>
private void GoHome()
{
Vector3? anchor = Anchor;
if (anchor == null || m_ai == null) return;
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[] { anchor.Value });
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(anchor.Value);
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)
@ -278,6 +355,9 @@ namespace RacesOfValheim
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;
@ -296,8 +376,14 @@ namespace RacesOfValheim
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;
}
@ -320,6 +406,8 @@ namespace RacesOfValheim
// 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);
@ -328,12 +416,47 @@ namespace RacesOfValheim
}
/// <summary>
/// Follow if it is not, come home if it is - the wolf's own single-key behaviour, plus an escort
/// if this thrall leads one.
/// 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)
{
bool follow = !IsFollowing(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;
@ -399,32 +522,48 @@ namespace RacesOfValheim
public const float TauntRange = 30f;
/// <summary>
/// The emote that calls thralls to their master. A SET rather than one string because the emote
/// 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 this one could not be read off the game and
/// 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> TauntEmotes = new HashSet<string>
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)
{
"comehere", "come_here", "followme",
};
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 falls in behind them.
///
/// Only ever a call, never a dismissal: "come here" means come here. Sending them home stays
/// with the station and with a click on the thrall itself.
/// 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.
/// 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 CallToArms(Player player)
public static void GroupOrder(Player player, Order order)
{
if (player == null) return;
int came = 0;
int answered = 0;
int refused = 0;
foreach (Character character in Character.GetAllCharacters())
@ -432,18 +571,40 @@ namespace RacesOfValheim
if (character == null || character.IsDead()) continue;
RaisedThrall thrall = character.GetComponent<RaisedThrall>();
if (thrall == null || thrall.IsFollowing(player)) continue;
if (thrall == null) continue;
if (!thrall.MayBeCommandedBy(player)) continue;
if (Vector3.SqrMagnitude(character.transform.position - player.transform.position) >
TauntRange * TauntRange) continue;
if (thrall.SetFollowing(player, true, announce: false)) came++;
else refused++;
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 (came > 0)
if (answered > 0)
{
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_thrall_answer_call", came));
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)
{
@ -452,9 +613,12 @@ namespace RacesOfValheim
}
}
/// <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 emote != null && TauntEmotes.Contains(emote.ToLowerInvariant());
return OrderOfEmote(emote) == Order.Follow;
}
public void Kill()
@ -502,10 +666,11 @@ namespace RacesOfValheim
{
if (__instance != Player.m_localPlayer) return;
Jotunn.Logger.LogInfo($"[Thrall] emote '{__0}'" +
(RaisedThrall.IsTauntEmote(__0) ? " - calling thralls." : "."));
RaisedThrall.Order? order = RaisedThrall.OrderOfEmote(__0);
if (RaisedThrall.IsTauntEmote(__0)) RaisedThrall.CallToArms(__instance);
Jotunn.Logger.LogInfo($"[Thrall] emote '{__0}'" + (order != null ? $" - order: {order}." : "."));
if (order != null) RaisedThrall.GroupOrder(__instance, order.Value);
}
}