using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
///
/// Server: who powers whom. Nodes and machines that reach each other make one network. The sun goes to the machines first,
/// batteries make up what's missing, and whatever is left over charges them. A machine gets the share of its power that the
/// network can give, and that share is how fast it runs.
///
public static class PowerGrid
{
class Net
{
public readonly List nodes = new List();
public readonly List machines = new List();
public readonly List batteries = new List();
}
static readonly List s_nets = new List();
static readonly HashSet s_seenM = new HashSet();
static readonly HashSet s_seenN = new HashSet();
static int s_version = -1, s_nodes, s_machines;
public static bool Reaches(PowerNode n, Vector3 at) => (n.transform.position - at).sqrMagnitude <= n.Radius * n.Radius;
public static bool Joined(PowerNode a, PowerNode b)
{
float r = Mathf.Max(a.Radius, b.Radius);
return (a.transform.position - b.transform.position).sqrMagnitude <= r * r;
}
/// Is there power reaching this spot?
public static bool Covers(Vector3 at)
{
foreach (var n in PowerNode.All) if (n && Reaches(n, at)) return true;
return false;
}
public static void Tick(float dt)
{
if (s_version != BuildPiece.Version || s_nodes != PowerNode.All.Count || s_machines != Machine.All.Count) Rebuild();
foreach (var net in s_nets) Balance(net, dt);
}
static void Rebuild()
{
s_version = BuildPiece.Version;
s_nodes = PowerNode.All.Count;
s_machines = Machine.All.Count;
s_nets.Clear();
s_seenM.Clear();
s_seenN.Clear();
foreach (var m in Machine.All)
{
if (!m || s_seenM.Contains(m)) continue;
var net = new Net();
Walk(m, net);
s_nets.Add(net);
}
foreach (var n in PowerNode.All)
{
if (!n || s_seenN.Contains(n)) continue;
var net = new Net();
Walk(n, net);
s_nets.Add(net);
}
}
static void Walk(Machine m, Net net)
{
s_seenM.Add(m);
net.machines.Add(m);
foreach (var n in PowerNode.All)
if (n && !s_seenN.Contains(n) && Reaches(n, m.transform.position)) Walk(n, net);
}
static void Walk(PowerNode n, Net net)
{
s_seenN.Add(n);
net.nodes.Add(n);
if (n is PowerBattery b) net.batteries.Add(b);
foreach (var other in PowerNode.All)
if (other && !s_seenN.Contains(other) && Joined(n, other)) Walk(other, net);
foreach (var m in Machine.All)
if (m && !s_seenM.Contains(m) && Reaches(n, m.transform.position)) Walk(m, net);
}
static void Balance(Net net, float dt)
{
float sun = 0f, asked = 0f, held = 0f, cap = 0f;
foreach (var n in net.nodes) if (n) sun += n.Sunlight;
foreach (var m in net.machines) if (m && m.Busy) asked += m.Kw;
// short of sun the batteries top it up, with sun to spare they fill, shared out evenly among them
float want = asked - sun, given = 0f;
for (int i = 0; i < net.batteries.Count; i++)
{
var b = net.batteries[i];
if (!b) continue;
float out_ = b.Charge(-(want - given) / (net.batteries.Count - i), dt);
given -= out_;
held += b.Stored.Value;
cap += b.Capacity;
}
float supply = sun + Mathf.Max(0f, given);
float pace = net.nodes.Count == 0 ? 0f : asked > 0f ? Mathf.Clamp01(supply / asked) : supply > 0f || held > 0f ? 1f : 0f;
foreach (var m in net.machines) if (m && !Mathf.Approximately(m.Power.Value, pace)) m.Power.Value = pace;
foreach (var n in net.nodes) if (n) n.Report(supply, asked, held, cap);
}
}
}