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

Implemented coned sensors. #226

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions Parts/kethane_highGain/part.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ MODULE
name = KethaneDetector
DetectingPeriod = 1.5
DetectingHeight = 250000
BeamWidth = 0.25
PowerConsumption = 0.8
Resource
{
Expand Down
5 changes: 3 additions & 2 deletions Parts/kethane_sensor_1m/part.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ category = Science
subcategory = 0
title = KE-S110 Medium Survey Unit
manufacturer = Mechanical Mouse Industries
description = Kethane can be found everywhere in the universe, but to be found, it needs a dedicated sensor. This sensor scans for Kethane pockets in the ground of celestial objects.
description = Kethane can be found everywhere in the universe, but to be found, it needs a dedicated sensor. This sensor scans for Kethane pockets in the ground of celestial objects, and can scan a large area from a great distance, though it requires significant power.

TechRequired = electronics
entryCost = 50
Expand All @@ -47,7 +47,8 @@ MODULE {
name = KethaneDetector
DetectingPeriod = 0.9
DetectingHeight = 1200000
PowerConsumption = 2.5
BeamWidth = 0.5
PowerConsumption = 2.5
Resource
{
Name = Kethane
Expand Down
36 changes: 36 additions & 0 deletions Plugin/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,42 @@ public IEnumerable<Cell> GetNeighbors(int level)
}
}

/// <summary>
/// Enumerates the cell neighbors to a specified distance.
/// A distance of 0 simply returns the cell, a distance of 1 returns the cell and its immediate neighbors.
/// </summary>
public IEnumerable<Cell> GetNeighborhood(int distance)
{
var timer = System.Diagnostics.Stopwatch.StartNew();
var visited = new HashSet<Cell>();
var next_round = new HashSet<Cell>();
var this_round = new HashSet<Cell>();

visited.Add(this);
this_round.Add(this);
for (var round = 0; round < distance && this_round.Count() > 0; round++) {
foreach (var visitee in this_round)
{
// Note the cell as visited
visited.Add(visitee);

// Get the immediate nieghbors, remove ones we've already visited and add the remainer to the next_round
foreach (var neighbor in visitee.GetNeighbors(MapOverlay.GridLevel))
{
if (!visited.Contains(neighbor))
{
next_round.Add(neighbor);
}
}
}
this_round = next_round;
next_round = new HashSet<Cell>();
}
timer.Stop();
Debug.LogWarning(String.Format("Got {0} neighboring cells in ({1}ms)", visited.Count(), timer.ElapsedMilliseconds));
return visited.ToList ();
}

#endregion

#region Grid structure
Expand Down
57 changes: 45 additions & 12 deletions Plugin/KethaneDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public static bool ScanningSound
[KSPField(isPersistant = true)]
public bool IsDetecting;

[KSPField(isPersistant = false)]
public float BeamWidth;

public ConfigNode config;

private List<string> resources;
Expand Down Expand Up @@ -83,7 +86,7 @@ public void DisableSounds()

public override string GetInfo()
{
return String.Format("Maximum Altitude: {0:N0}m\nPower Consumption: {1:F2}/s\nScanning Period: {2:F2}s\nDetects: {3}", DetectingHeight, PowerConsumption, DetectingPeriod, String.Join(", ", resources.ToArray()));
return String.Format("Maximum Altitude: {0:N0}m\nSensor Beam Width: {1:N} degrees\nPower Consumption: {2:F2}/s\nScanning Period: {3:F2}s\nDetects: {4}", DetectingHeight, BeamWidth, PowerConsumption, DetectingPeriod, String.Join(", ", resources.ToArray()));
}

public override void OnStart(PartModule.StartState state)
Expand Down Expand Up @@ -145,13 +148,28 @@ public override void OnUpdate()
animator.IsDetecting = IsDetecting;
animator.PowerRatio = powerRatio;
}

}

public int BeamFootprint()
{
// Calculate the width of an equatorial cell, in meters
var cell_width = 2 * Math.PI * vessel.mainBody.Radius / 256; // The grid implements a 256-cell equator

// Calculate the width of the beam on the surfance
var beam_footprint = 2 * Misc.GetTrueAltitude(vessel) * Math.Sin (2 * Math.PI * BeamWidth / 360);

var detector_width = Math.Max(1,Math.Ceiling(beam_footprint / cell_width));
return (int)detector_width;
}

public override void OnFixedUpdate()
{
double Altitude = Misc.GetTrueAltitude(vessel);
if (IsDetecting && this.vessel != null && this.vessel.gameObject.activeSelf && Altitude <= this.DetectingHeight)
{
var timer = System.Diagnostics.Stopwatch.StartNew();

var energyRequest = PowerConsumption * TimeWarp.fixedDeltaTime;
var energyDrawn = this.part.RequestResource("ElectricCharge", energyRequest);
this.powerRatio = energyDrawn / energyRequest;
Expand All @@ -161,23 +179,38 @@ public override void OnFixedUpdate()

if (TimerEcho >= TimerThreshold)
{
// Do not scan more than the visible half of the planetoid.
var half_spherer_distance = 64;
var scan_area = MapOverlay.GetCellUnder(vessel.mainBody, vessel.transform.position).GetNeighborhood(
Math.Min(half_spherer_distance, (int)Math.Ceiling((double)BeamFootprint() - 1) / 2));

var scanned = false;
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;
// Mark all scanned cells and update the colors
foreach (var cell in scan_area) {
// Skip already scanned cells.
if (!KethaneData.Current.Scans[resource][vessel.mainBody.name][cell])
{
scanned = true;
detected |= KethaneData.Current.GetCellDeposit(resource, vessel.mainBody, cell) != null;
KethaneData.Current.Scans[resource][vessel.mainBody.name][cell] = true;
MapOverlay.Instance.RefreshCellColor(cell, vessel.mainBody);
}
}
}
MapOverlay.Instance.RefreshCellColor(cell, vessel.mainBody);
TimerEcho = 0;
if (vessel == FlightGlobals.ActiveVessel && ScanningSound)
{
(detected ? PingDeposit : PingEmpty).Play();

// If there are any unscanned cells in the scan area, play a sound based on whether the scan reveals a deposit
if (vessel == FlightGlobals.ActiveVessel && ScanningSound && scanned) {
(detected ? PingDeposit : PingEmpty).Play ();
}

TimerEcho = 0;

timer.Stop();
Debug.LogWarning(String.Format("Finished 1 scan in ({0}ms)", timer.ElapsedMilliseconds));
}
}
else
Expand Down