chore(release): v1.5.4 - Sverm troops keep their own place around the player and the boat
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a047ea45cf
commit
9a3ab78666
6 changed files with 152 additions and 3 deletions
|
|
@ -523,6 +523,26 @@ namespace RacesOfValheim
|
|||
/// </summary>
|
||||
public static readonly float TroopAlertRange = 25f;
|
||||
|
||||
/// <summary>
|
||||
/// How far from its player a following troop stands, and how far it may fall behind before it
|
||||
/// walks back to its place. Vanilla BaseAI.Follow sends every follower to the player's exact
|
||||
/// position and stops it at three metres, so a squad piled onto one point, shoved each other
|
||||
/// and shoved the player. See SvermTroopFollow.
|
||||
/// </summary>
|
||||
public static readonly float TroopFollowRadius = 4f;
|
||||
|
||||
/// <summary>How far past its place a troop may drift before it moves again - the hysteresis
|
||||
/// that keeps a squad from jittering.</summary>
|
||||
public static readonly float TroopFollowSlack = 3f;
|
||||
|
||||
/// <summary>How far from a boat's hull a following troop keeps, measured off the hull's
|
||||
/// bounds. Close enough to be a bodyguard, far enough not to push the ship around.</summary>
|
||||
public static readonly float TroopShipDistance = 5f;
|
||||
|
||||
/// <summary>Past this many metres from its player a following troop gives up the walk and
|
||||
/// goes home to its nest.</summary>
|
||||
public static readonly float TroopFollowGiveUp = 30f;
|
||||
|
||||
/// <summary>How long a troop outlives the nest that hatched it, in seconds.</summary>
|
||||
public static readonly float TroopWitherSeconds = 60f;
|
||||
|
||||
|
|
|
|||
121
JotunnModStub/Sverm/SvermTroopFollow.cs
Normal file
121
JotunnModStub/Sverm/SvermTroopFollow.cs
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
using System.Runtime.CompilerServices;
|
||||
using HarmonyLib;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// How a Sverm troop walks beside its player.
|
||||
///
|
||||
/// Vanilla BaseAI.Follow (read off the IL) sends the follower to the player's exact position,
|
||||
/// runs it past ten metres and stops it inside three. Three troops therefore converged on one
|
||||
/// point, pushed each other and pushed the player - into water, in one report - and on a boat
|
||||
/// the player's position is on the deck, so they drove into the hull and spun the ship.
|
||||
///
|
||||
/// Replaced for Sverm troops only (a RaisedThrall that answers to the Sverm race). Thralls,
|
||||
/// Aswid's and the skeletons keep the vanilla walk.
|
||||
///
|
||||
/// - Each troop has its own place: a fixed bearing off the player's back, taken from its ZDO id,
|
||||
/// so the squad fans out instead of stacking.
|
||||
/// - It walks to that place only when it has drifted TroopFollowSlack metres off it, and stops
|
||||
/// within a metre and a half, so it stands still instead of shuffling.
|
||||
/// - With the player on a ship, the places lie on a ring around the hull, TroopShipDistance
|
||||
/// metres past its bounds.
|
||||
/// - Further than TroopFollowGiveUp from the player, it stops following and goes home.
|
||||
/// </summary>
|
||||
public static class SvermTroopFollow
|
||||
{
|
||||
private const float ArriveDistance = 1.5f;
|
||||
private const float RunDistance = 8f;
|
||||
|
||||
private static readonly System.Reflection.MethodInfo MoveToMethod =
|
||||
AccessTools.Method(typeof(BaseAI), "MoveTo", new[] { typeof(float), typeof(Vector3), typeof(float), typeof(bool) });
|
||||
|
||||
private class State { public bool Moving; }
|
||||
|
||||
private static readonly ConditionalWeakTable<BaseAI, State> States = new ConditionalWeakTable<BaseAI, State>();
|
||||
|
||||
[HarmonyPatch(typeof(BaseAI), "Follow")]
|
||||
public static class Sverm_TroopFollow_Patch
|
||||
{
|
||||
/// <summary>False replaces vanilla, true leaves it alone.</summary>
|
||||
public static bool Prefix(BaseAI __instance, GameObject go, float dt)
|
||||
{
|
||||
if (go == null) return true;
|
||||
|
||||
RaisedThrall thrall = __instance.GetComponent<RaisedThrall>();
|
||||
if (thrall == null || thrall.m_commandingRace != SvermConfig.RaceId) return true;
|
||||
|
||||
Player player = go.GetComponent<Player>();
|
||||
if (player == null) return true;
|
||||
|
||||
Vector3 here = __instance.transform.position;
|
||||
Vector3 playerPos = player.transform.position;
|
||||
|
||||
if (Flat(playerPos - here).magnitude > SvermConfig.TroopFollowGiveUp)
|
||||
{
|
||||
// Stands it down and sends it to its nest, exactly as the command would.
|
||||
thrall.SetFollowing(player, false, announce: false);
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector3 place = PlaceFor(__instance, player, playerPos);
|
||||
float toPlace = Flat(place - here).magnitude;
|
||||
|
||||
State state = States.GetOrCreateValue(__instance);
|
||||
if (state.Moving ? toPlace < ArriveDistance : toPlace < SvermConfig.TroopFollowSlack) state.Moving = false;
|
||||
else state.Moving = true;
|
||||
|
||||
if (!state.Moving)
|
||||
{
|
||||
__instance.StopMoving();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Protected on BaseAI, so reached the way RaisedThrall reaches SetPatrolPoint. If it
|
||||
// ever goes missing, vanilla's walk is the fallback rather than a frozen squad.
|
||||
if (MoveToMethod == null) return true;
|
||||
|
||||
MoveToMethod.Invoke(__instance, new object[] { dt, place, 0f, toPlace > RunDistance });
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Where this troop should stand: behind the player, or round the player's ship.</summary>
|
||||
private static Vector3 PlaceFor(BaseAI ai, Player player, Vector3 playerPos)
|
||||
{
|
||||
ZNetView nview = ai.GetComponent<ZNetView>();
|
||||
int hash = nview != null && nview.IsValid() ? nview.GetZDO().m_uid.GetHashCode() : ai.GetInstanceID();
|
||||
|
||||
// Seven bearings, twenty degrees apart, fanned across the back half. Troops hatched
|
||||
// together draw different ZDO ids, so they mostly land on different bearings.
|
||||
float bearing = 180f + (((hash % 7) + 7) % 7 - 3) * 20f;
|
||||
|
||||
Ship ship = player.GetControlledShip();
|
||||
if (ship == null) ship = player.GetStandingOnShip();
|
||||
|
||||
if (ship != null && ship.m_floatCollider != null)
|
||||
{
|
||||
Bounds bounds = ship.m_floatCollider.bounds;
|
||||
float reach = Mathf.Max(bounds.extents.x, bounds.extents.z) + SvermConfig.TroopShipDistance;
|
||||
return Ring(bounds.center, ship.transform.eulerAngles.y, bearing, reach, playerPos.y);
|
||||
}
|
||||
|
||||
return Ring(playerPos, player.transform.eulerAngles.y, bearing, SvermConfig.TroopFollowRadius, playerPos.y);
|
||||
}
|
||||
|
||||
private static Vector3 Ring(Vector3 center, float facingDegrees, float bearingDegrees, float radius, float y)
|
||||
{
|
||||
Quaternion turn = Quaternion.Euler(0f, facingDegrees + bearingDegrees, 0f);
|
||||
Vector3 point = center + turn * Vector3.forward * radius;
|
||||
point.y = y;
|
||||
return point;
|
||||
}
|
||||
|
||||
private static Vector3 Flat(Vector3 v)
|
||||
{
|
||||
v.y = 0f;
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue