121 lines
5.4 KiB
C#
121 lines
5.4 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|