using Unity.Netcode;
using UnityEngine;
namespace GrassCutter
{
///
/// The overgrown surface of Herbaria-7. Owns the shared clock and the mowing spots: every blade and mower registers a
/// spot each frame and the grass bends toward it.
///
[DefaultExecutionOrder(-50)]
public class Meadow : MonoBehaviour
{
public static Meadow Instance { get; private set; }
public const int MaxVortex = 12;
static readonly Vector4[] s_vortex = new Vector4[MaxVortex]; // where, strength
static readonly float[] s_reach = new float[MaxVortex];
static int s_vortexCount;
static readonly int VortexId = Shader.PropertyToID("_GcVortex"), VortexCountId = Shader.PropertyToID("_GcVortexCount");
static readonly int ReachId = Shader.PropertyToID("_GcVortexReach"), TimeId = Shader.PropertyToID("_GcTime");
/// Shared clock (server time when networked, so everyone sees the same wind and spurts).
public static float T
{
get
{
if (TeamState.ClockOverride != null) return (float)(TeamState.ClockOverride() % 100000d);
var nm = NetworkManager.Singleton;
return nm && nm.IsListening ? (float)(nm.ServerTime.Time % 100000d) : Time.time;
}
}
void Awake()
{
Instance = this;
Shader.SetGlobalVectorArray(VortexId, s_vortex);
Shader.SetGlobalFloatArray(ReachId, s_reach);
}
void LateUpdate()
{
Shader.SetGlobalFloat(TimeId, T);
Shader.SetGlobalVectorArray(VortexId, s_vortex);
Shader.SetGlobalFloatArray(ReachId, s_reach);
Shader.SetGlobalFloat(VortexCountId, s_vortexCount);
// mowing spots are re-registered every frame by whoever is cutting
for (int i = 0; i < MaxVortex; i++) s_vortex[i].w *= 0.82f;
s_vortexCount = 0;
for (int i = 0; i < MaxVortex; i++) if (s_vortex[i].w > 0.02f) s_vortexCount = i + 1;
}
/// Register a mowing spot for this frame (blades, mowers). strength 0..1, radius in metres.
public static void Vortex(Vector3 at, float strength, float radius)
{
// reuse the nearest slot so one cutter doesn't hog many
int best = -1;
float bestD = 4f;
for (int i = 0; i < MaxVortex; i++)
{
if (s_vortex[i].w <= 0.02f) continue;
float d = ((Vector3)s_vortex[i] - at).sqrMagnitude;
if (d < bestD) { bestD = d; best = i; }
}
if (best < 0)
{
float weakest = 9f;
for (int i = 0; i < MaxVortex; i++) if (s_vortex[i].w < weakest) { weakest = s_vortex[i].w; best = i; }
}
s_vortex[best] = new Vector4(at.x, at.y, at.z, Mathf.Max(s_vortex[best].w, Mathf.Clamp01(strength)));
s_reach[best] = radius;
s_vortexCount = Mathf.Max(s_vortexCount, best + 1);
}
/// Where a ray first meets the ground. False if it never does within range.
public static bool Raycast(Ray ray, float maxDist, out Vector3 hit)
{
hit = default;
float prev = 0f;
float step = 0.25f;
for (float d = 0f; d <= maxDist; d += step)
{
var p = ray.origin + ray.direction * d;
if (Planet.Altitude(p) <= 0f)
{
float a = prev, b = d;
for (int i = 0; i < 6; i++)
{
float m = (a + b) * 0.5f;
var q = ray.origin + ray.direction * m;
if (Planet.Altitude(q) <= 0f) b = m; else a = m;
}
hit = ray.origin + ray.direction * b;
return true;
}
prev = d;
step = Mathf.Min(0.6f, step * 1.15f);
}
return false;
}
}
}