A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
113 lines
4.4 KiB
C#
113 lines
4.4 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace GrassCutter
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static class PowerGrid
|
|
{
|
|
class Net
|
|
{
|
|
public readonly List<PowerNode> nodes = new List<PowerNode>();
|
|
public readonly List<Machine> machines = new List<Machine>();
|
|
public readonly List<PowerBattery> batteries = new List<PowerBattery>();
|
|
}
|
|
|
|
static readonly List<Net> s_nets = new List<Net>();
|
|
static readonly HashSet<Machine> s_seenM = new HashSet<Machine>();
|
|
static readonly HashSet<PowerNode> s_seenN = new HashSet<PowerNode>();
|
|
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;
|
|
}
|
|
|
|
/// <summary>Is there power reaching this spot?</summary>
|
|
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);
|
|
}
|
|
}
|
|
}
|