Skip to content

Commit

Permalink
Create BodyResourceData class to encapsulate scan data and body resou…
Browse files Browse the repository at this point in the history
…rce provider
  • Loading branch information
Majiir committed Sep 2, 2014
1 parent 6d0bf36 commit ec63cf4
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 64 deletions.
68 changes: 68 additions & 0 deletions Plugin/BodyResourceData.cs
@@ -0,0 +1,68 @@
using Kethane.GeodesicGrid;
using System;
using UnityEngine;

namespace Kethane
{
public class BodyResourceData
{
private readonly IBodyResources resources;
private readonly CellSet scans;

protected BodyResourceData(IBodyResources resources, CellSet scans)
{
this.resources = resources;
this.scans = scans;
}

public double MaxDepositQuantity
{
get { return resources.MaxQuantity; }
}

public ICellResource GetCellDeposit(Cell cell)
{
return resources.GetResource(cell);
}

public bool IsCellScanned(Cell cell)
{
return scans[cell];
}

public void ScanCell(Cell cell)
{
scans[cell] = true;
}

public static BodyResourceData Load(IResourceGenerator generator, CelestialBody body, ConfigNode bodyNode)
{
var resources = generator.Load(body, bodyNode.GetNode("GeneratorData"));
var scans = new CellSet(MapOverlay.GridLevel);

var scanMask = bodyNode.GetValue("ScanMask");
if (scanMask != null)
{
try
{
scans = new CellSet(MapOverlay.GridLevel, Misc.FromBase64String(scanMask));
}
catch (FormatException e)
{
Debug.LogError(String.Format("[Kethane] Failed to parse {0} scan string, resetting ({1})", body.name, e.Message));
}
}

return new BodyResourceData(resources, scans);
}

public void Save(ConfigNode bodyNode)
{
bodyNode.AddValue("ScanMask", Misc.ToBase64String(scans.ToByteArray()));

var node = resources.Save() ?? new ConfigNode();
node.name = "GeneratorData";
bodyNode.AddNode(node);
}
}
}
1 change: 1 addition & 0 deletions Plugin/Kethane.csproj
Expand Up @@ -46,6 +46,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BodyResourceData.cs" />
<Compile Include="EnumerableExtensions\Edges.cs" />
<Compile Include="EnumerableExtensions\AppendPrepend.cs" />
<Compile Include="EnumerableExtensions\MinMaxBy.cs" />
Expand Down
70 changes: 15 additions & 55 deletions Plugin/KethaneData.cs
Expand Up @@ -28,25 +28,23 @@ public static KethaneData Current
}
}

public Dictionary<string, Dictionary<string, IBodyResources>> PlanetDeposits = new Dictionary<string,Dictionary<string,IBodyResources>>();
public Dictionary<string, Dictionary<string, CellSet>> Scans = new Dictionary<string,Dictionary<string,CellSet>>();
public Dictionary<string, Dictionary<string, BodyResourceData>> ResourceData;

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

public ICellResource GetCellDeposit(string resourceName, CelestialBody body, Cell cell)
{
if (resourceName == null || body == null || !PlanetDeposits.ContainsKey(resourceName) || !PlanetDeposits[resourceName].ContainsKey(body.name)) { return null; }
if (resourceName == null || body == null || !ResourceData.ContainsKey(resourceName) || !ResourceData[resourceName].ContainsKey(body.name)) { return null; }

return PlanetDeposits[resourceName][body.name].GetResource(cell);
return ResourceData[resourceName][body.name].GetCellDeposit(cell);
}

public override void OnLoad(ConfigNode config)
{
var timer = System.Diagnostics.Stopwatch.StartNew();

PlanetDeposits.Clear();
Scans.Clear();
ResourceData.Clear();

var resourceNodes = config.GetNodes("Resource");

Expand All @@ -55,27 +53,24 @@ public override void OnLoad(ConfigNode config)
var resourceName = resource.Resource;
var resourceNode = resourceNodes.SingleOrDefault(n => n.GetValue("Resource") == resourceName) ?? new ConfigNode();

Dictionary<string, IBodyResources> resourceDeposits;
Dictionary<string, CellSet> resourceScans;
Dictionary<string, BodyResourceData> bodyResources;
ConfigNode generatorNode;
IResourceGenerator generator;

LoadResource(resource, resourceNode, out resourceDeposits, out resourceScans, out generatorNode, out generator);
LoadResource(resource, resourceNode, out bodyResources, out generatorNode, out generator);

generatorNodes[resourceName] = generatorNode;
generators[resourceName] = generator;
PlanetDeposits[resourceName] = resourceDeposits;
Scans[resourceName] = resourceScans;
ResourceData[resourceName] = bodyResources;
}

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

private static void LoadResource(ResourceDefinition resource, ConfigNode resourceNode, out Dictionary<string, IBodyResources> resourceDeposits, out Dictionary<string, CellSet> resourceScans, out ConfigNode generatorNode, out IResourceGenerator generator)
private static void LoadResource(ResourceDefinition resource, ConfigNode resourceNode, out Dictionary<string, BodyResourceData> bodyResources, out ConfigNode generatorNode, out IResourceGenerator generator)
{
resourceDeposits = new Dictionary<string, IBodyResources>();
resourceScans = new Dictionary<string, CellSet>();
bodyResources = new Dictionary<string, BodyResourceData>();

generatorNode = resourceNode.GetNode("Generator") ?? resource.Generator;

Expand All @@ -91,41 +86,14 @@ private static void LoadResource(ResourceDefinition resource, ConfigNode resourc
foreach (var body in FlightGlobals.Bodies)
{
var bodyNode = bodyNodes.SingleOrDefault(n => n.GetValue("Name") == body.name) ?? new ConfigNode();

IBodyResources resources;
CellSet scans;

LoadBodyResources(generator, body, bodyNode, out resources, out scans);

resourceDeposits[body.name] = resources;
resourceScans[body.name] = scans;
}
}

private static void LoadBodyResources(IResourceGenerator generator, CelestialBody body, ConfigNode bodyNode, out IBodyResources resources, out CellSet scans)
{
resources = generator.Load(body, bodyNode.GetNode("GeneratorData"));
scans = new CellSet(MapOverlay.GridLevel);

var scanMask = bodyNode.GetValue("ScanMask");
if (scanMask != null)
{
try
{
scans = new CellSet(MapOverlay.GridLevel, Misc.FromBase64String(scanMask));
}
catch (FormatException e)
{
Debug.LogError(String.Format("[Kethane] Failed to parse {0} scan string, resetting ({1})", body.name, e.Message));
}
bodyResources[body.name] = BodyResourceData.Load(generator, body, bodyNode);
}
}

public void ResetBodyData(ResourceDefinition resource, CelestialBody body)
{
var resourceName = resource.Resource;
PlanetDeposits[resourceName][body.name] = generators[resourceName].Load(body, null);
Scans[resourceName][body.name] = new CellSet(MapOverlay.GridLevel);
ResourceData[resourceName][body.name] = BodyResourceData.Load(generators[resourceName], body, null);
}

public void ResetGeneratorConfig(ResourceDefinition resource)
Expand Down Expand Up @@ -182,15 +150,15 @@ public override void OnSave(ConfigNode configNode)

configNode.AddValue("Version", System.Reflection.Assembly.GetExecutingAssembly().GetInformationalVersion());

foreach (var resource in PlanetDeposits)
foreach (var resource in ResourceData)
{
var resourceNode = new ConfigNode("Resource");
resourceNode.AddValue("Resource", resource.Key);
resourceNode.AddNode(generatorNodes[resource.Key]);

foreach (var body in resource.Value)
{
var bodyNode = saveBodyData(resource.Key, body.Key, body.Value);
var bodyNode = saveBodyData(body.Key, body.Value);
resourceNode.AddNode(bodyNode);
}

Expand All @@ -201,19 +169,11 @@ public override void OnSave(ConfigNode configNode)
Debug.LogWarning(String.Format("Kethane deposits saved ({0}ms)", timer.ElapsedMilliseconds));
}

private ConfigNode saveBodyData(string resourceName, string bodyName, IBodyResources bodyResources)
private ConfigNode saveBodyData(string bodyName, BodyResourceData bodyResources)
{
var bodyNode = new ConfigNode("Body");
bodyNode.AddValue("Name", bodyName);

if (Scans.ContainsKey(resourceName) && Scans[resourceName].ContainsKey(bodyName))
{
bodyNode.AddValue("ScanMask", Misc.ToBase64String(Scans[resourceName][bodyName].ToByteArray()));
}

var node = bodyResources.Save() ?? new ConfigNode();
node.name = "GeneratorData";
bodyNode.AddNode(node);
bodyResources.Save(bodyNode);
return bodyNode;
}
}
Expand Down
12 changes: 6 additions & 6 deletions Plugin/MapOverlay.cs
Expand Up @@ -243,8 +243,8 @@ private void refreshCellColors()
private void refreshCellColor(Cell cell, CelestialBody body, Color32[] colors, KethaneData data)
{
var deposit = data.GetCellDeposit(resource.Resource, body, cell);
var scanned = data.Scans[resource.Resource][body.name][cell];
var bodyResources = data.PlanetDeposits[resource.Resource][body.name];
var scanned = data.ResourceData[resource.Resource][body.name].IsCellScanned(cell);
var bodyResources = data.ResourceData[resource.Resource][body.name];
var color = (revealAll ? deposit != null : scanned) ? getDepositColor(resource, bodyResources, deposit) : colorUnknown;
setCellColor(cell, color, colors);
}
Expand All @@ -258,12 +258,12 @@ private static void setCellColor(Cell cell, Color32 color, Color32[] colors)
}
}

private static Color32 getDepositColor(ResourceDefinition definition, IBodyResources bodyResources, ICellResource deposit)
private static Color32 getDepositColor(ResourceDefinition definition, BodyResourceData bodyResources, ICellResource deposit)
{
Color32 color;
if (deposit != null)
{
var ratio = (float)(deposit.Quantity / bodyResources.MaxQuantity);
var ratio = (float)(deposit.Quantity / bodyResources.MaxDepositQuantity);
color = (Color32)(definition.ColorFull * ratio + definition.ColorEmpty * (1 - ratio));
}
else
Expand Down Expand Up @@ -380,7 +380,7 @@ private void mouseWindow(int windowId)

GUILayout.Label(String.Format("{0}:", definition.Resource));
GUILayout.FlexibleSpace();
if (revealAll || KethaneData.Current.Scans[definition.Resource][body.name][cell])
if (revealAll || KethaneData.Current.ResourceData[definition.Resource][body.name].IsCellScanned(cell))
{
var deposit = KethaneData.Current.GetCellDeposit(definition.Resource, body, cell);
GUILayout.Label(deposit != null ? String.Format("{0:N1}", deposit.Quantity) : "(none)");
Expand Down Expand Up @@ -497,7 +497,7 @@ private static void export()
{
foreach (var cell in Cell.AtLevel(MapOverlay.GridLevel))
{
var scanned = KethaneData.Current.Scans[resource.Resource][body.name][cell];
var scanned = KethaneData.Current.ResourceData[resource.Resource][body.name].IsCellScanned(cell);
var deposit = KethaneData.Current.GetCellDeposit(resource.Resource, body, cell);

sb.Append(String.Format("{0},{1},", body.name, resource.Resource));
Expand Down
4 changes: 2 additions & 2 deletions Plugin/PartModules/KethaneDetector.cs
Expand Up @@ -163,10 +163,10 @@ public override void OnFixedUpdate()
{
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; }
if (resources.All(r => KethaneData.Current.ResourceData[r][vessel.mainBody.name].IsCellScanned(cell))) { return; }
foreach (var resource in resources)
{
KethaneData.Current.Scans[resource][vessel.mainBody.name][cell] = true;
KethaneData.Current.ResourceData[resource][vessel.mainBody.name].ScanCell(cell);
if (KethaneData.Current.GetCellDeposit(resource, vessel.mainBody, cell) != null)
{
detected = true;
Expand Down
2 changes: 1 addition & 1 deletion Plugin/Scenarios/KethaneScanningTutorial.cs
Expand Up @@ -187,7 +187,7 @@ protected override void OnTutorialSetup()

timewarp.OnUpdate = () =>
{
surfaceCoverage = (float)Cell.AtLevel(MapOverlay.GridLevel).Count(c => KethaneData.Current.Scans["Kethane"][FlightGlobals.currentMainBody.name][c]) / Cell.CountAtLevel(MapOverlay.GridLevel);
surfaceCoverage = (float)Cell.AtLevel(MapOverlay.GridLevel).Count(c => KethaneData.Current.ResourceData["Kethane"][FlightGlobals.currentMainBody.name].IsCellScanned(c)) / Cell.CountAtLevel(MapOverlay.GridLevel);
if (surfaceCoverage >= 0.02f)
{
Tutorial.GoToNextPage();
Expand Down

0 comments on commit ec63cf4

Please sign in to comment.