Skip to content

Commit

Permalink
Eliminate ICellResource in favor of parameterized methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Majiir committed Sep 6, 2014
1 parent 5dd0306 commit f11229c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 66 deletions.
17 changes: 4 additions & 13 deletions Plugin/Kethane/BodyResourceData.cs
Expand Up @@ -6,25 +6,16 @@ namespace Kethane
{
public class BodyResourceData
{
private readonly IBodyResources resources;
private readonly CellSet scans;

public IBodyResources Resources { get; private set; }

protected BodyResourceData(IBodyResources resources, CellSet scans)
{
this.resources = resources;
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];
Expand Down Expand Up @@ -60,7 +51,7 @@ public void Save(ConfigNode bodyNode)
{
bodyNode.AddValue("ScanMask", Misc.ToBase64String(scans.ToByteArray()));

var node = resources.Save() ?? new ConfigNode();
var node = Resources.Save() ?? new ConfigNode();
node.name = "GeneratorData";
bodyNode.AddNode(node);
}
Expand Down
30 changes: 10 additions & 20 deletions Plugin/Kethane/Generators/CellularResourceGenerator.cs
Expand Up @@ -77,30 +77,20 @@ public ConfigNode Save()
return node;
}

public ICellResource GetResource(Cell cell)
{
if (amounts[cell] <= 0) { return null; }
return new CellResource(this, cell);
}

public double MaxQuantity { get; private set; }

private class CellResource : ICellResource
public double? GetQuantity(Cell cell)
{
private readonly BodyResources resources;
private readonly Cell cell;

public CellResource(BodyResources resources, Cell cell)
{
this.resources = resources;
this.cell = cell;
}
double? amount = amounts[cell];
return amount > 0 ? amount : null;
}

public double Quantity
{
get { return resources.amounts[cell]; }
set { resources.amounts[cell] = value; }
}
public double Extract(Cell cell, double amount)
{
var current = amounts[cell];
var delta = Math.Min(current, Math.Max(0, amount));
amounts[cell] = current - delta;
return delta;
}
}
}
Expand Down
22 changes: 20 additions & 2 deletions Plugin/Kethane/Generators/LegacyResourceGenerator.cs
Expand Up @@ -107,7 +107,7 @@ public BodyDeposits(GeneratorConfiguration resource, ConfigNode node)
MaxQuantity = resource.MaxQuantity;
}

public ICellResource GetResource(Cell cell)
private Deposit getDeposit(Cell cell)
{
var pos = cell.Position;
var lat = (float)(Math.Atan2(pos.y, Math.Sqrt(pos.x * pos.x + pos.z * pos.z)) * 180 / Math.PI);
Expand All @@ -119,6 +119,24 @@ public ICellResource GetResource(Cell cell)
return deposits.FirstOrDefault(d => d.Shape.PointInPolygon(new Vector2(x, y)));
}

public double? GetQuantity(Cell cell)
{
var deposit = getDeposit(cell);
if (deposit == null) { return null; }
return deposit.Quantity;
}

public double Extract(Cell cell, double amount)
{
var deposit = getDeposit(cell);
if (deposit == null) { return 0; }

var current = deposit.Quantity;
var delta = Math.Min(current, Math.Max(0, amount));
deposit.Quantity = current - delta;
return delta;
}

public ConfigNode Save()
{
var node = new ConfigNode();
Expand All @@ -131,7 +149,7 @@ public ConfigNode Save()
}
}

private class Deposit : ICellResource
private class Deposit
{
public Polygon Shape;

Expand Down
12 changes: 5 additions & 7 deletions Plugin/Kethane/IResourceGenerator.cs
@@ -1,4 +1,5 @@
using Kethane.GeodesicGrid;
using System;

namespace Kethane
{
Expand All @@ -10,13 +11,9 @@ public interface IResourceGenerator
public interface IBodyResources
{
ConfigNode Save();
ICellResource GetResource(Cell cell);
double MaxQuantity { get; }
}

public interface ICellResource
{
double Quantity { get; set; }
double? GetQuantity(Cell cell);
double Extract(Cell cell, double amount);
}

internal class EmptyResourceGenerator : IResourceGenerator
Expand All @@ -32,7 +29,8 @@ private class BodyResources : IBodyResources
{
public double MaxQuantity { get { return 0; } }
public ConfigNode Save() { return new ConfigNode(); }
public ICellResource GetResource(Cell cell) { return null; }
public double? GetQuantity(Cell cell) { return null; }
public double Extract(Cell cell, double amount) { throw new Exception("No deposit here"); }
}
}
}
6 changes: 0 additions & 6 deletions Plugin/Kethane/KethaneData.cs
Expand Up @@ -37,12 +37,6 @@ public static KethaneData Current
get { return resources[resourceName]; }
}

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

public void ResetGeneratorConfig(ResourceDefinition resource)
{
resources[resource.Resource] = Kethane.ResourceData.Load(resource, new ConfigNode());
Expand Down
2 changes: 1 addition & 1 deletion Plugin/Kethane/PartModules/KethaneDetector.cs
Expand Up @@ -168,7 +168,7 @@ public override void OnFixedUpdate()
foreach (var resource in resources)
{
KethaneData.Current[resource][vessel.mainBody].ScanCell(cell);
if (KethaneData.Current.GetCellDeposit(resource, vessel.mainBody, cell) != null)
if (KethaneData.Current[resource][vessel.mainBody].Resources.GetQuantity(cell) != null)
{
detected = true;
}
Expand Down
26 changes: 17 additions & 9 deletions Plugin/Kethane/PartModules/KethaneExtractor.cs
@@ -1,8 +1,9 @@
using System;
using Kethane.GeodesicGrid;
using Kethane.UserInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections.Generic;
using Kethane.UserInterface;

namespace Kethane.PartModules
{
Expand Down Expand Up @@ -183,7 +184,7 @@ public void Update()
{
if (animator.CurrentState == ExtractorState.Deployed)
{
emitter.Emit = hit && GetDepositUnder("Kethane") != null;
emitter.Emit = hit && getBodyResources("Kethane").GetQuantity(getCellUnder()) != null;
}
else
{
Expand All @@ -210,18 +211,25 @@ public override void OnFixedUpdate()

foreach (var resource in resources)
{
var deposit = GetDepositUnder(resource.Name);
var cell = getCellUnder();
var bodyResources = getBodyResources(resource.Name);
var deposit = bodyResources.GetQuantity(cell);
if (deposit == null) { continue; }

double amount = TimeWarp.fixedDeltaTime * resource.Rate * energyRatio;
amount = Math.Min(amount, deposit.Quantity);
deposit.Quantity += this.part.RequestResource(resource.Name, -amount);
amount = Math.Min(amount, deposit.Value);
bodyResources.Extract(cell, -this.part.RequestResource(resource.Name, -amount));
}
}

private ICellResource GetDepositUnder(string resourceName)
private Cell getCellUnder()
{
return MapOverlay.GetCellUnder(this.vessel.mainBody, this.vessel.transform.position);
}

private IBodyResources getBodyResources(string resourceName)
{
return KethaneData.Current.GetCellDeposit(resourceName, this.vessel.mainBody, MapOverlay.GetCellUnder(this.vessel.mainBody, this.vessel.transform.position));
return KethaneData.Current[resourceName][this.vessel.mainBody].Resources;
}

private bool raycastGround()
Expand Down
16 changes: 8 additions & 8 deletions Plugin/Kethane/UserInterface/MapOverlay.cs
Expand Up @@ -240,9 +240,9 @@ 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[resource.Resource][body].IsCellScanned(cell);
var bodyResources = data[resource.Resource][body];
var deposit = bodyResources.Resources.GetQuantity(cell);
var scanned = bodyResources.IsCellScanned(cell);
var color = (revealAll ? deposit != null : scanned) ? getDepositColor(resource, bodyResources, deposit) : colorUnknown;
setCellColor(cell, color, colors);
}
Expand All @@ -256,12 +256,12 @@ private static void setCellColor(Cell cell, Color32 color, Color32[] colors)
}
}

private static Color32 getDepositColor(ResourceDefinition definition, BodyResourceData bodyResources, ICellResource deposit)
private static Color32 getDepositColor(ResourceDefinition definition, BodyResourceData bodyResources, double? deposit)
{
Color32 color;
if (deposit != null)
{
var ratio = (float)(deposit.Quantity / bodyResources.MaxDepositQuantity);
var ratio = (float)(deposit.Value / bodyResources.Resources.MaxQuantity);
color = (Color32)(definition.ColorFull * ratio + definition.ColorEmpty * (1 - ratio));
}
else
Expand Down Expand Up @@ -380,8 +380,8 @@ private void mouseWindow(int windowId)
GUILayout.FlexibleSpace();
if (revealAll || KethaneData.Current[definition.Resource][body].IsCellScanned(cell))
{
var deposit = KethaneData.Current.GetCellDeposit(definition.Resource, body, cell);
GUILayout.Label(deposit != null ? String.Format("{0:N1}", deposit.Quantity) : "(none)");
var deposit = KethaneData.Current[definition.Resource][body].Resources.GetQuantity(cell);
GUILayout.Label(deposit != null ? String.Format("{0:N1}", deposit.Value) : "(none)");
}
else
{
Expand Down Expand Up @@ -495,14 +495,14 @@ private static void export()
foreach (var cell in Cell.AtLevel(KethaneData.GridLevel))
{
var scanned = KethaneData.Current[resource.Resource][body].IsCellScanned(cell);
var deposit = KethaneData.Current.GetCellDeposit(resource.Resource, body, cell);
var deposit = KethaneData.Current[resource.Resource][body].Resources.GetQuantity(cell);

sb.Append(String.Format("{0},{1},", body.name, resource.Resource));
sb.Append(cells[cell]);
sb.Append(scanned ? "true" : "false");
if ((revealAll || scanned) && deposit != null)
{
sb.Append(String.Format(CultureInfo.InvariantCulture, ",{0}", deposit.Quantity));
sb.Append(String.Format(CultureInfo.InvariantCulture, ",{0}", deposit.Value));
}
else
{
Expand Down

0 comments on commit f11229c

Please sign in to comment.