using UnityEngine;
namespace GrassCutter
{
///
/// Things to find on Herbaria-7: 195 survey stakes the wind has tangled and 26 survey points. Hold E at one and the crew
/// is paid for it, once. They stand in the same places on every peer, spread evenly over the ball, and only the ones
/// nearby are switched on.
///
public class Landmarks : MonoBehaviour
{
public static Landmarks Instance { get; private set; }
public const int Stakes = 195, Points = 26, Count = Stakes + Points;
const float StakeHold = 1.6f, PointHold = 3f, DrawDistance = 900f, Bare = 1.3f;
static readonly Vector3[] s_spots = new Vector3[Count];
readonly Landmark[] _all = new Landmark[Count];
Landmark _busy;
float _held;
int _wake;
Material _wait, _done;
public static bool IsPoint(int id) => id >= Stakes;
public static double Pay(int id) => IsPoint(id) ? GameDefs.PointPay : GameDefs.StakePay;
/// Where it stands on the ground.
public static Vector3 Spot(int id) => s_spots[id];
/// How far along the one in your sights is, 0..1.
public float Held(Landmark l) => l == _busy ? Mathf.Clamp01(_held / HoldFor(l.id)) : 0f;
static float HoldFor(int id) => IsPoint(id) ? PointHold : StakeHold;
void Awake() => Instance = this;
void Start()
{
_wait = Paint("M_LightAmber", new Color(1f, 0.7f, 0.15f));
_done = Paint("M_LightGreen", new Color(0.3f, 1f, 0.4f));
for (int i = 0; i < Count; i++)
{
s_spots[i] = Planet.OnSurface(Place(i));
_all[i] = Build(i);
MowMap.BlockRound(s_spots[i], Bare);
}
}
/// The ball changed size: the stakes and points stand again where they belong on it.
public void Replace()
{
foreach (var l in _all) if (l) Destroy(l.gameObject);
Start();
}
static Material Paint(string name, Color fallback)
{
var m = AssetBank.I ? AssetBank.I.Mat(name) : null;
return m ? m : new Material(Shader.Find("Universal Render Pipeline/Lit")) { color = fallback };
}
/// Spirals down the ball so they spread evenly, nudged off the regular pattern and out of the rocks.
static Vector3 Place(int id)
{
bool point = IsPoint(id);
int n = point ? Points : Stakes, i = point ? id - Stakes : id;
float z = 1f - (i + 0.5f) * 2f / n, a = i * 2.39996f + (point ? 1.1f : 0.3f), r = Mathf.Sqrt(1f - z * z);
var d = new Vector3(r * Mathf.Cos(a), z, r * Mathf.Sin(a));
var rng = new System.Random(id * 7919 + 13);
float R() => (float)rng.NextDouble() * 2f - 1f;
d = (d + new Vector3(R(), R(), R()) * Mathf.Sqrt(4f / n) * 0.25f).normalized;
for (int tries = 0; tries < 12 && Ground.Instance && Ground.Instance.RockAt(Planet.OnSurface(d)); tries++)
d = (d + new Vector3(R(), R(), R()) * 8f / Planet.Radius).normalized;
return d;
}
Landmark Build(int id)
{
var go = new GameObject((IsPoint(id) ? "Point " : "Stake ") + id);
go.transform.SetParent(transform, false);
var up = Planet.Up(s_spots[id]);
go.transform.SetPositionAndRotation(s_spots[id], Quaternion.FromToRotation(Vector3.up, up));
var l = go.AddComponent();
l.id = id;
var col = go.AddComponent();
col.height = 2.6f;
col.radius = 0.45f;
col.center = Vector3.up * 1.3f;
var model = AssetBank.I ? AssetBank.I.Model(IsPoint(id) ? "SM_SurveyPoint" : "SM_SurveyStake") : null;
if (model)
foreach (var t in Instantiate(model, go.transform).GetComponentsInChildren(true))
{
if (t.name == "Lamp") l.lamp = t;
else if (t.name == "Post") { l.post = t; l.upright = t.localRotation; }
else if (t.name == "ClothKnot") l.knot = t.gameObject;
else if (t.name == "ClothFlag") l.flag = t.gameObject;
}
go.SetActive(false);
l.Show(false);
return l;
}
/// The crew's list changed: each one shows whether it has been done.
public void Refresh()
{
var ts = TeamState.Instance;
if (!ts) return;
foreach (var l in _all) l.Show(ts.Found.Contains(l.id));
}
public void SetLamp(Landmark l, bool done)
{
var r = l.lamp ? l.lamp.GetComponent() : null;
var mat = done ? _done : _wait;
if (r && mat) r.sharedMaterial = mat;
}
/// The nearest one nobody has done yet, -1 when they all are.
public int Nearest(Vector3 from, bool points)
{
var ts = TeamState.Instance;
int best = -1;
float d = float.MaxValue;
for (int i = 0; i < Count; i++)
{
if (IsPoint(i) != points || (ts && ts.Found.Contains(i))) continue;
float m = (s_spots[i] - from).sqrMagnitude;
if (m < d) { d = m; best = i; }
}
return best;
}
public void Begin(Landmark l)
{
_busy = l;
_held = 0f;
}
void Update()
{
var cam = CameraFx.Cam;
if (cam)
for (int i = 0; i < 16; i++)
{
_wake = (_wake + 1) % Count;
bool on = (s_spots[_wake] - cam.transform.position).sqrMagnitude < DrawDistance * DrawDistance;
if (_all[_wake].gameObject.activeSelf != on) _all[_wake].gameObject.SetActive(on);
}
if (!_busy) return;
var me = PlayerAgent.Local;
var ts = TeamState.Instance;
if (!me || !ts || !Ctl.InteractHeld || me.AimInteractable as Landmark != _busy) { _busy = null; return; }
_held += Time.deltaTime;
if (_held < HoldFor(_busy.id)) return;
ts.ClaimMarkRpc(_busy.id);
Sfx.Play("build_place", 0.7f, 1.2f);
_busy = null;
}
}
/// One stake or survey point.
public class Landmark : MonoBehaviour, IInteractable
{
public int id;
public Transform post, lamp;
public GameObject knot, flag;
public Quaternion upright = Quaternion.identity;
bool Done => TeamState.Instance && TeamState.Instance.Found.Contains(id);
public string Prompt
{
get
{
if (Done) return Landmarks.IsPoint(id) ? Loc.T("Survey point: already read") : Loc.T("Stake: straight and tidy");
float held = Landmarks.Instance.Held(this);
bool point = Landmarks.IsPoint(id);
if (held > 0f) return point ? Loc.F("Read the survey point: {0:0}%", held * 100f) : Loc.F("Untangle the stake: {0:0}%", held * 100f);
string pay = UIK.Money(Landmarks.Pay(id));
return point ? Loc.F("Hold: Read the survey point ({0})", pay) : Loc.F("Hold: Untangle the stake ({0})", pay);
}
}
public bool CanInteract => TeamState.Instance && !Done;
public void Interact()
{
if (CanInteract) Landmarks.Instance.Begin(this);
}
/// Knotted round a leaning post until somebody sorts it out.
public void Show(bool done)
{
if (post)
{
post.localRotation = (done ? Quaternion.identity : Quaternion.Euler(16f, 0f, 9f)) * upright;
if (knot) knot.SetActive(!done);
if (flag) flag.SetActive(done);
}
if (lamp) Landmarks.Instance.SetLamp(this, done);
}
}
}