302 lines
13 KiB
C#
302 lines
13 KiB
C#
using System;
|
|
using HarmonyLib;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// One crystal golem: tame, bound to a stonecutter, and unable to get more than its leash away
|
|
/// from it.
|
|
///
|
|
/// Same shape as RaisedThrall - an anchor in the creature's own ZDO, a leash pinned to it, a
|
|
/// wither clock that starts when the station is gone - and deliberately NOT that class. Two
|
|
/// differences make it a copy rather than a reuse, and both are in SkadiGolem's own comment: the
|
|
/// station is vanilla's and cannot register itself, and a golem takes no orders, so the follow,
|
|
/// stand-down and call-to-arms half of RaisedThrall is not merely unused here but wrong.
|
|
///
|
|
/// The leash is three layers, weakest first, because each one covers the case the one before it
|
|
/// cannot:
|
|
/// - vanilla's own chase limit (m_maxChaseDistance, measured from m_spawnPoint, which is pinned
|
|
/// to the stonecutter below) makes it GIVE UP on anything it would have to leave home for;
|
|
/// - the patrol point, re-pinned every tick, is what brings it back and what keeps it wandering
|
|
/// around the station rather than drifting after it has been pushed or knocked back;
|
|
/// - and past half again the leash it is simply put back, because at that distance it is not
|
|
/// walking home slowly, it is failing to walk home at all.
|
|
/// </summary>
|
|
public class SkadiGolemGuard : MonoBehaviour
|
|
{
|
|
public const string KeyAnchor = "RoV_golemAnchor";
|
|
public const string KeyOrphanAt = "RoV_golemOrphanAt";
|
|
|
|
/// <summary>
|
|
/// One second, and not the two RaisedThrall uses. Vanilla will not drop a target it can still
|
|
/// SEE, whatever m_maxChaseDistance says - read off MonsterAI.UpdateTarget, where the give-up
|
|
/// test also wants a second of having lost sight of it - so this tick is what actually holds
|
|
/// the leash, and how far past twenty metres a golem gets is how far it walks between two of
|
|
/// them. A stone golem is slow; at one second that is a stride.
|
|
/// </summary>
|
|
private const float TickSeconds = 1f;
|
|
|
|
// SetPatrolPoint(Vector3) is private - only the parameterless overload, which uses the
|
|
// creature's current position, is public. Resolved once here rather than per call, exactly as
|
|
// RaisedThrall does it.
|
|
private static readonly System.Reflection.MethodInfo SetPatrolPointMethod =
|
|
AccessTools.Method(typeof(BaseAI), "SetPatrolPoint", new[] { typeof(Vector3) });
|
|
|
|
// m_spawnPoint is protected, and it is the point vanilla measures m_maxChaseDistance from -
|
|
// read off MonsterAI.UpdateTarget rather than assumed, because "chase distance" could just as
|
|
// easily have meant distance from the target.
|
|
private static readonly System.Reflection.FieldInfo SpawnPointField =
|
|
AccessTools.Field(typeof(BaseAI), "m_spawnPoint");
|
|
|
|
private Character m_character;
|
|
private BaseAI m_ai;
|
|
private MonsterAI m_monsterAI;
|
|
private ZNetView m_nview;
|
|
private Rigidbody m_body;
|
|
|
|
private void Awake()
|
|
{
|
|
m_character = GetComponent<Character>();
|
|
m_ai = GetComponent<BaseAI>();
|
|
m_monsterAI = GetComponent<MonsterAI>();
|
|
m_nview = GetComponent<ZNetView>();
|
|
m_body = GetComponent<Rigidbody>();
|
|
|
|
InvokeRepeating("Tick", TickSeconds, TickSeconds);
|
|
}
|
|
|
|
/// <summary>Where the stonecutter that cut this one stands, or null if it was cut by none.</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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called once, by the seed piece that placed it. Tames it, marks whose stonecutter it is, and
|
|
/// sends it home - all three, so nothing else has to remember to do any of them.
|
|
/// </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 tick
|
|
|
|
private void Tick()
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid() || !m_nview.IsOwner()) return;
|
|
if (m_character == null || m_character.IsDead()) return;
|
|
|
|
if (TickWither()) return;
|
|
|
|
Vector3? anchor = Anchor;
|
|
if (anchor == null) return;
|
|
|
|
float distance = Vector3.Distance(transform.position, anchor.Value);
|
|
|
|
if (distance > SkadiGolem.LeashRange)
|
|
{
|
|
// Whatever it was going after is outside the circle it defends, so it stops going
|
|
// after it. Vanilla would drop the target itself a second or so later through
|
|
// m_maxChaseDistance; doing it here as well means the leash does not depend on that
|
|
// one field surviving a game update.
|
|
ClearTarget();
|
|
}
|
|
|
|
if (distance > SkadiGolem.HardLeashRange) SnapHome(anchor.Value);
|
|
|
|
// Every tick, and not only when it has strayed: this is also what pins m_spawnPoint after
|
|
// a zone reload, where Bind is never called again.
|
|
GoHome();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts the clock when the stonecutter is gone, and stops the golem when it runs out.
|
|
/// Returns true once it has done so, so the caller stops bothering with it.
|
|
///
|
|
/// Without some form of this a stonecutter is a duplication exploit: cut two golems, take the
|
|
/// stonecutter down, put it up somewhere else, and keep both from every round.
|
|
/// </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)
|
|
{
|
|
// Only asked from close to home - see SkadiGolem.HomeCheckRange. Being leashed to
|
|
// twenty metres, a golem is essentially always close enough, so this guard is about
|
|
// the moment after a zone loads rather than about distance.
|
|
if (Vector3.SqrMagnitude(transform.position - anchor.Value) >
|
|
SkadiGolem.HomeCheckRange * SkadiGolem.HomeCheckRange) return false;
|
|
|
|
if (SkadiGolem.StonecutterStandsAt(anchor.Value)) return false;
|
|
|
|
MarkOrphaned();
|
|
return false;
|
|
}
|
|
|
|
// Put back in time, on the same spot: the clock is cleared and the golem lives. It opens
|
|
// no exploit - a stonecutter on the same spot owns these golems by anchor anyway.
|
|
if (SkadiGolem.StonecutterStandsAt(anchor.Value))
|
|
{
|
|
m_nview.GetZDO().Set(KeyOrphanAt, 0L);
|
|
return false;
|
|
}
|
|
|
|
double elapsed = (ZNet.instance.GetTime() - new DateTime(stamp)).TotalSeconds;
|
|
if (elapsed < SkadiGolem.WitherSeconds) return false;
|
|
|
|
Crumble();
|
|
return true;
|
|
}
|
|
|
|
/// <summary>Starts the wither clock, once. Idempotent, so it cannot be restarted and keep an
|
|
/// orphan alive forever.</summary>
|
|
private 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);
|
|
|
|
if (Player.m_localPlayer != null &&
|
|
Vector3.Distance(Player.m_localPlayer.transform.position, transform.position) < 30f)
|
|
{
|
|
Player.m_localPlayer.Message(MessageHud.MessageType.Center,
|
|
"The stonecutter is gone. What it cut will not outlast it by long.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pins the patrol point - and the spawn point vanilla measures its own chase limit from -
|
|
/// back onto the stonecutter. Cheap enough to call on a timer, and calling it repeatedly is
|
|
/// what stops a golem drifting after it has been pushed or knocked back.
|
|
/// </summary>
|
|
private void GoHome()
|
|
{
|
|
Vector3? anchor = Anchor;
|
|
if (anchor == null || m_ai == null) return;
|
|
|
|
if (SpawnPointField != null) SpawnPointField.SetValue(m_ai, anchor.Value);
|
|
|
|
if (SetPatrolPointMethod != null)
|
|
{
|
|
SetPatrolPointMethod.Invoke(m_ai, new object[] { anchor.Value });
|
|
}
|
|
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_patrol").SetValue(true);
|
|
}
|
|
}
|
|
|
|
/// <summary>Forgets whatever it was going after, creature or otherwise.</summary>
|
|
private void ClearTarget()
|
|
{
|
|
if (m_monsterAI == null) return;
|
|
|
|
Traverse traverse = Traverse.Create(m_monsterAI);
|
|
traverse.Field("m_targetCreature").SetValue(null);
|
|
traverse.Field("m_targetStatic").SetValue(null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The failsafe, and only that. A golem that is this far out has not wandered - it has been
|
|
/// pushed somewhere it cannot path back from, and the alternative to putting it back is one
|
|
/// that stands in a lake until its stonecutter falls.
|
|
/// </summary>
|
|
private void SnapHome(Vector3 anchor)
|
|
{
|
|
Vector2 offset = UnityEngine.Random.insideUnitCircle * 3f;
|
|
Vector3 destination = anchor + new Vector3(offset.x, 0.5f, offset.y);
|
|
|
|
transform.position = destination;
|
|
if (m_body != null) m_body.linearVelocity = Vector3.zero;
|
|
|
|
ClearTarget();
|
|
|
|
Jotunn.Logger.LogWarning($"[Skadi] A golem was {SkadiGolem.HardLeashRange:0} m or more from its " +
|
|
"stonecutter and could not walk back; it was put back.");
|
|
}
|
|
|
|
private void Crumble()
|
|
{
|
|
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>
|
|
/// Lives for one frame: swaps itself for the golem and goes. Owner only, so in multiplayer exactly
|
|
/// one golem stands up rather than one per client.
|
|
///
|
|
/// The stonecutter is looked up again here rather than handed over by the placement gate, and that
|
|
/// is deliberate: both call SkadiGolem.FindStonecutter with the same point, so both get the same
|
|
/// answer, and there is no piece of state between them that can go stale or arrive out of order.
|
|
///
|
|
/// If the lookup comes back empty the materials are lost, and that is the honest outcome rather
|
|
/// than an oversight: it can only happen if the stonecutter was taken down between the gate saying
|
|
/// yes and this frame, and the alternative - a golem anchored to nothing - would wither in a
|
|
/// minute anyway while looking like a bug.
|
|
/// </summary>
|
|
public class SkadiGolemSeed : MonoBehaviour
|
|
{
|
|
private void Start()
|
|
{
|
|
ZNetView nview = GetComponent<ZNetView>();
|
|
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
|
|
|
|
GameObject prefab = ZNetScene.instance != null
|
|
? ZNetScene.instance.GetPrefab(SkadiGolem.GolemName)
|
|
: null;
|
|
|
|
if (prefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[Skadi] No '{SkadiGolem.GolemName}' in ZNetScene; nothing was cut.");
|
|
ZNetScene.instance.Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
CraftingStation station = SkadiGolem.FindStonecutter(transform.position);
|
|
if (station == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("[Skadi] No stonecutter in range at the moment of cutting; " +
|
|
"nothing was raised.");
|
|
ZNetScene.instance.Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
GameObject spawned = Instantiate(prefab, transform.position, transform.rotation);
|
|
|
|
// Bind tames it, marks whose stonecutter it is, and sends it home - all three.
|
|
SkadiGolemGuard guard = spawned.GetComponent<SkadiGolemGuard>();
|
|
if (guard != null) guard.Bind(station.transform.position);
|
|
|
|
ZNetScene.instance.Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|