Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background scanning #257

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 169 additions & 1 deletion Plugin/KethaneData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public static KethaneData Current

if (!game.scenarios.Any(p => p.moduleName == typeof(KethaneData).Name))
{
var proto = game.AddProtoScenarioModule(typeof(KethaneData), GameScenes.FLIGHT, GameScenes.TRACKSTATION);
var proto = game.AddProtoScenarioModule(typeof(KethaneData),
GameScenes.SPACECENTER,
GameScenes.EDITOR,
GameScenes.FLIGHT,
GameScenes.TRACKSTATION,
GameScenes.SPH);
if (proto.targetScenes.Contains(HighLogic.LoadedScene))
{
proto.Load(ScenarioRunner.fetch);
Expand All @@ -27,12 +32,25 @@ public static KethaneData Current
}
}

public class ScanSensor
{
public float DetectingPeriod;
public float DetectingHeight;
public List<string> resources;
public double TimerEcho;
public float powerRatio;
}

public Dictionary<string, Dictionary<string, IBodyResources>> PlanetDeposits = new Dictionary<string,Dictionary<string,IBodyResources>>();

public Dictionary<string, Dictionary<string, Cell.Set>> Scans = new Dictionary<string,Dictionary<string,Cell.Set>>();

private Dictionary<string, ConfigNode> generatorNodes = new Dictionary<string, ConfigNode>();
private Dictionary<string, IResourceGenerator> generators = new Dictionary<string, IResourceGenerator>();


private Dictionary<Guid, Dictionary<uint, ScanSensor>> sensors = new Dictionary<Guid, Dictionary<uint, ScanSensor>>();

public ICellResource GetDepositUnder(string resourceName, Vessel vessel)
{
return GetCellDeposit(resourceName, vessel.mainBody, MapOverlay.GetCellUnder(vessel.mainBody, vessel.transform.position));
Expand Down Expand Up @@ -140,6 +158,32 @@ public override void OnLoad(ConfigNode config)

timer.Stop();
Debug.LogWarning(String.Format("Kethane deposits loaded ({0}ms)", timer.ElapsedMilliseconds));

timer.Reset();
timer.Start();

ConfigNode sensorsNode = config.GetNode("Sensors");
if (sensorsNode != null)
{
foreach (ConfigNode i in sensorsNode.GetNodes("Sensor"))
{
Guid id = new Guid(i.GetValue("Vessel"));
ScanSensor sensor = new ScanSensor();
sensor.DetectingHeight = (float)Convert.ToDouble(i.GetValue("DetectingHeight"));
sensor.DetectingPeriod = (float)Convert.ToDouble(i.GetValue("DetectingPeriod"));
sensor.powerRatio = (float)Convert.ToDouble(i.GetValue("powerRatio"));
sensor.resources = new List<string>(i.GetValue("resources").Split(new char[] { ',' }));
sensor.TimerEcho = 0.0;

if (!sensors.ContainsKey(id))
sensors.Add(id, new Dictionary<uint, ScanSensor>());

sensors[id][(uint)sensors[id].Count] = sensor;
}
}

timer.Stop();
Debug.LogWarning(String.Format("Kethane sensor list loaded ({0}ms)", timer.ElapsedMilliseconds));
}

public void ResetBodyData(ResourceDefinition resource, CelestialBody body)
Expand Down Expand Up @@ -268,6 +312,130 @@ public override void OnSave(ConfigNode configNode)

timer.Stop();
Debug.LogWarning(String.Format("Kethane deposits saved ({0}ms)", timer.ElapsedMilliseconds));


timer.Reset ();
timer.Start ();
ConfigNode sensorsNode = new ConfigNode ("Sensors");
foreach(var i in sensors)
{
foreach(var j in i.Value)
{
ConfigNode node = new ConfigNode("Sensor");
node.AddValue("Vessel", i.Key);
node.AddValue("DetectingHeight", j.Value.DetectingHeight);
node.AddValue("DetectingPeriod", j.Value.DetectingPeriod);
node.AddValue("powerRatio", j.Value.powerRatio);
node.AddValue("resources", String.Join(",", j.Value.resources.ToArray()));
sensorsNode.AddNode(node);
}
}
configNode.AddNode (sensorsNode);

timer.Stop ();
Debug.LogWarning(String.Format("Kethane sensor list saved ({0}ms)", timer.ElapsedMilliseconds));
}

public void Update()
{
KethaneData data = KethaneData.Current;

foreach(Vessel v in FlightGlobals.Vessels)
{
if (!sensors.ContainsKey(v.id))
continue;

double altitude = Misc.GetTrueAltitude(v);
bool activeVessel = FlightGlobals.ActiveVessel == v;
bool detected = false;

if (activeVessel)
{
List<uint> to_remove = sensors[v.id].Keys.Except(v.FindPartModulesImplementing<KethaneDetector>().Select(x => x.part.uid)).ToList();

foreach(uint i in to_remove)
{
sensors[v.id].Remove(i);
}
}

foreach (var i in sensors[v.id])
{
ScanSensor sensor = i.Value;
if (altitude > sensor.DetectingHeight)
continue;

double TimerThreshold = sensor.DetectingPeriod * (1 + altitude * 0.000002d);

sensor.TimerEcho += Time.deltaTime * sensor.powerRatio;

if (sensor.TimerEcho < TimerThreshold)
continue;

var cell = MapOverlay.GetCellUnder(v.mainBody, v.transform.position);

if (sensor.resources.All(r => KethaneData.Current.Scans[r][v.mainBody.name][cell]))
{
continue;
}
foreach (var resource in sensor.resources)
{
KethaneData.Current.Scans[resource][v.mainBody.name][cell] = true;
if (KethaneData.Current.GetCellDeposit(resource, v.mainBody, cell) != null)
{
detected = true;
}
}

MapOverlay.Instance.RefreshCellColor(cell, v.mainBody);
sensor.TimerEcho = 0;

if (activeVessel)
{
v.FindPartModulesImplementing<KethaneDetector>().Single(x => x.part.uid == i.Key).Ping(detected);
}
}
}
}

public void register(KethaneDetector detector)
{
Part p = detector.part;
Vessel v = p.vessel;

Dictionary<uint, ScanSensor> vessel;
if(sensors.ContainsKey(v.id))
vessel = sensors[v.id];
else
sensors.Add(v.id, vessel = new Dictionary<uint, ScanSensor>());

ScanSensor sensor = new ScanSensor();

if (vessel.ContainsKey(p.uid))
sensor.TimerEcho = vessel[p.uid].TimerEcho;

sensor.DetectingHeight = detector.DetectingHeight;
sensor.DetectingPeriod = detector.DetectingPeriod;
sensor.resources = detector.resources;
sensor.powerRatio = detector.powerRatio;

vessel[p.uid] = sensor;
}

public void unregister(KethaneDetector detector)
{
Part p = detector.part;
Vessel v = p.vessel;

if(sensors.ContainsKey(v.id))
{
if(sensors[v.id].ContainsKey(p.uid))
{
sensors[v.id].Remove(p.uid);
if(sensors[v.id].Count == 0)
sensors.Remove(v.id);
}
}
}
}
}
43 changes: 15 additions & 28 deletions Plugin/KethaneDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ public static bool ScanningSound

public ConfigNode config;

private List<string> resources;
internal List<string> resources;

private double TimerEcho;

private float powerRatio;
internal float powerRatio;

private static AudioSource PingEmpty;
private static AudioSource PingDeposit;
Expand Down Expand Up @@ -145,6 +143,11 @@ public override void OnUpdate()
animator.IsDetecting = IsDetecting;
animator.PowerRatio = powerRatio;
}

if (IsDetecting)
KethaneData.Current.register(this);
else
KethaneData.Current.unregister(this);
}

public override void OnFixedUpdate()
Expand All @@ -155,35 +158,19 @@ public override void OnFixedUpdate()
var energyRequest = PowerConsumption * TimeWarp.fixedDeltaTime;
var energyDrawn = this.part.RequestResource("ElectricCharge", energyRequest);
this.powerRatio = energyDrawn / energyRequest;
TimerEcho += TimeWarp.deltaTime * this.powerRatio;

var TimerThreshold = this.DetectingPeriod * (1 + Altitude * 0.000002d);

if (TimerEcho >= TimerThreshold)
{
var detected = false;
var cell = MapOverlay.GetCellUnder(vessel.mainBody, vessel.transform.position);
if (resources.All(r => KethaneData.Current.Scans[r][vessel.mainBody.name][cell])) { return; }
foreach (var resource in resources)
{
KethaneData.Current.Scans[resource][vessel.mainBody.name][cell] = true;
if (KethaneData.Current.GetCellDeposit(resource, vessel.mainBody, cell) != null)
{
detected = true;
}
}
MapOverlay.Instance.RefreshCellColor(cell, vessel.mainBody);
TimerEcho = 0;
if (vessel == FlightGlobals.ActiveVessel && ScanningSound)
{
(detected ? PingDeposit : PingEmpty).Play();
}
}
}
else
{
this.powerRatio = 0;
}
}

public void Ping(bool detected)
{
if (ScanningSound)
{
(detected ? PingDeposit : PingEmpty).Play();
}
}
}
}