Skip to content

Commit

Permalink
Clean up UI units and other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisAdderley committed Aug 13, 2024
1 parent e30a0bc commit 5329440
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 100 deletions.
8 changes: 4 additions & 4 deletions GameData/SpaceDust/Localization/en-us.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ Localization
#LOC_SpaceDust_UI_ResourceAreaHeader = Harvestable Resources
#LOC_SpaceDust_UI_NoResources = No resources discovered!
#LOC_SpaceDust_UI_UnknownResource = Unknown
#LOC_SpaceDust_UI_UnknownBand = ? u/m³
#LOC_SpaceDust_UI_UnknownBand = ? t/m³

#LOC_SpaceDust_UI_BandData = <<1>> u/m³
#LOC_SpaceDust_UI_BandData = <<1>> t/m³

// System
#LOC_SpaceDust_Message_Discovery = [Space Dust] We have discovered a band of <<1>> near <<2>>!
Expand Down Expand Up @@ -164,7 +164,7 @@ Localization

#LOC_SpaceDust_ModuleSpaceDustScanner_Field_Resources_NoPower = Not enough Electric Charge!
#LOC_SpaceDust_ModuleSpaceDustScanner_Field_Disabled = Disabled
#LOC_SpaceDust_ModuleSpaceDustScanner_Field_Resources_SingleSample = <b><<1>></b>: <<2>> u/m³
#LOC_SpaceDust_ModuleSpaceDustScanner_Field_Resources_SingleSample = <b><<1>></b>: <<2>> t/m³

// ModuleSpaceDustHarvester
#LOC_SpaceDust_ModuleSpaceDustHarvester_DisplayName = Space Dust Harvester
Expand Down Expand Up @@ -194,7 +194,7 @@ Localization
#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource_None = No resources to harvest
#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_NeedsAtmo = Cannot harvest in vacuum!
#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_NeedsVacuum = Cannot harvest in atmosphere!
#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource = <b><<1>></b>: <<2>> u/s
#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource = <b><<1>></b>: <<2>> t/s
#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Blocked = Intake Blocked

#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Thermal = Efficiency
Expand Down
Binary file modified GameData/SpaceDust/Plugins/SpaceDust.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This mod is for others too! See the [wiki](https://github.com/ChrisAdderley/Spac

### Required
These components are required for the mod to function and are bundled as part of any download:
* [ModuleManager (4.1.4)](https://github.com/sarbian/ModuleManager)
* [ModuleManager (4.2.3)](https://github.com/sarbian/ModuleManager)
* [B9PartSwitch (2.18.0)](https://github.com/blowfishpro/B9PartSwitch)

## Installation
Expand Down
156 changes: 80 additions & 76 deletions Source/SpaceDust/Modules/ModuleSpaceDustHarvester.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using KSP.Localization;
using Smooth.Algebraics;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -412,7 +411,7 @@ void HandleAnimation()
void DoFocusedHarvesting(double scale)
{

if (HarvestType == HarvestType.Atmosphere && part.vessel.atmDensity > 0d)
if (HarvestType == HarvestType.Atmosphere)
{
if (part.vessel.atmDensity <= 0d)
{
Expand All @@ -422,54 +421,56 @@ void DoFocusedHarvesting(double scale)
Fields["IntakeSpeed"].guiActive = false;
return;
}
Vector3d worldVelocity = part.vessel.srf_velocity;
double mach = part.vessel.mach;

Vector3 intakeVector;
Transform intakeTransform;
if (HarvestIntakeTransform == null)
intakeTransform = this.transform;
else
intakeTransform = HarvestIntakeTransform;

if (CheckOcclusion && Physics.Raycast(intakeTransform.position, intakeTransform.forward, RaycastDistance))
{
ScoopUI = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Blocked");
Fields["ThermalUI"].guiActive = false;
Fields["IntakeSpeed"].guiActive = false;
return;
}
Vector3d worldVelocity = part.vessel.srf_velocity;
double mach = part.vessel.mach;

intakeVector = intakeTransform.forward;
double dot = Vector3d.Dot(worldVelocity, intakeVector);
float intakeVolume = (float)(worldVelocity.magnitude * MathExtensions.Clamp(dot, 0d, 1d) * IntakeVelocityScale.Evaluate((float)mach) + IntakeSpeedStatic) * IntakeArea;
IntakeSpeed = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_IntakeSpeed_Normal", Utils.ToSI(intakeVolume, "G2"));
ScoopUI = "";

for (int i = 0; i < resources.Count; i++)
{
double resourceSample = SpaceDustResourceMap.Instance.SampleResource(resources[i].Name,
part.vessel.mainBody,
vessel.altitude + part.vessel.mainBody.Radius,
vessel.latitude,
vessel.longitude);
Vector3 intakeVector;
Transform intakeTransform;
if (HarvestIntakeTransform == null)
intakeTransform = this.transform;
else
intakeTransform = HarvestIntakeTransform;

if (resourceSample > resources[i].MinHarvestValue)
if (CheckOcclusion && Physics.Raycast(intakeTransform.position, intakeTransform.forward, RaycastDistance))
{
double resAmt = resourceSample * intakeVolume * 1d / resources[i].density * resources[i].BaseEfficiency * scale;
if (ScoopUI != "")
ScoopUI += "\n";
ScoopUI += Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource", resources[i].Name, resAmt.ToString("G5"));
part.RequestResource(resources[i].Name, -resAmt * TimeWarp.fixedDeltaTime, ResourceFlowMode.ALL_VESSEL, false);
ScoopUI = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Blocked");
Fields["ThermalUI"].guiActive = false;
Fields["IntakeSpeed"].guiActive = false;
return;
}

intakeVector = intakeTransform.forward;
double dot = Vector3d.Dot(worldVelocity, intakeVector);
float intakeVolume = (float)(worldVelocity.magnitude * MathExtensions.Clamp(dot, 0d, 1d) * IntakeVelocityScale.Evaluate((float)mach) + IntakeSpeedStatic) * IntakeArea;
IntakeSpeed = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_IntakeSpeed_Normal", Utils.ToSI(intakeVolume, "G2"));
ScoopUI = "";

for (int i = 0; i < resources.Count; i++)
{
double resourceSample = SpaceDustResourceMap.Instance.SampleResource(resources[i].Name,
part.vessel.mainBody,
vessel.altitude + part.vessel.mainBody.Radius,
vessel.latitude,
vessel.longitude);

if (resourceSample > resources[i].MinHarvestValue)
{
double resAmt = (resourceSample * intakeVolume) * (1d / resources[i].density) * resources[i].BaseEfficiency * scale;
ScoopUI += "\n ";
ScoopUI += Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource", resources[i].Name, resAmt.ToString("G5"));
part.RequestResource(resources[i].Name, -resAmt * resources[i].density * TimeWarp.fixedDeltaTime, ResourceFlowMode.ALL_VESSEL, false);
}

}
if (ScoopUI == "")
ScoopUI = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource_None");
}
if (ScoopUI == "")
ScoopUI = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource_None");
}


if (HarvestType == HarvestType.Exosphere && part.vessel.atmDensity == 0d)
if (HarvestType == HarvestType.Exosphere)
{
if (part.vessel.atmDensity > 0d)
{
Expand All @@ -478,52 +479,55 @@ void DoFocusedHarvesting(double scale)
Fields["IntakeSpeed"].guiActive = false;
return;
}


Vector3d worldVelocity = part.vessel.obt_velocity;
Vector3 intakeVector;
if (HarvestIntakeTransform == null)
intakeVector = this.transform.forward;
else
intakeVector = HarvestIntakeTransform.forward;
{


double dot = Vector3d.Dot(worldVelocity.normalized, intakeVector.normalized);
float intakeVolume = (float)(worldVelocity.magnitude * MathExtensions.Clamp(dot, 0d, 1d) + IntakeSpeedStatic) * IntakeArea;
IntakeSpeed = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_IntakeSpeed_Normal", intakeVolume.ToString("G2"));
Vector3d worldVelocity = part.vessel.obt_velocity;
Vector3 intakeVector;
if (HarvestIntakeTransform == null)
intakeVector = this.transform.forward;
else
intakeVector = HarvestIntakeTransform.forward;

ScoopUI = "";

for (int i = 0; i < resources.Count; i++)
{
double resourceSample = SpaceDustResourceMap.Instance.SampleResource(resources[i].Name,
part.vessel.mainBody,
vessel.altitude + part.vessel.mainBody.Radius,
vessel.latitude,
vessel.longitude);
double dot = Vector3d.Dot(worldVelocity.normalized, intakeVector.normalized);
float intakeVolume = (float)(worldVelocity.magnitude * MathExtensions.Clamp(dot, 0d, 1d) + IntakeSpeedStatic) * IntakeArea;
IntakeSpeed = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_IntakeSpeed_Normal", intakeVolume.ToString("G2"));

ScoopUI = "";

if (resourceSample * intakeVolume * resources[i].BaseEfficiency > resources[i].MinHarvestValue)
for (int i = 0; i < resources.Count; i++)
{
//Utils.Log($"[SpaceDustHarvesterd] sampled {resources[i].Name} @ {resourceSample}, " +
// $"minH {resources[i].MinHarvestValue}," +
// $"effic {resources[i].BaseEfficiency}," +
// $"volume {intakeVolume}," +
// $"area {IntakeArea}," +
// $"speedstatic {IntakeSpeedStatic}," +
// $"worldvel {worldVelocity.magnitude}");

double resAmt = resourceSample * intakeVolume * 1d / resources[i].density * resources[i].BaseEfficiency * scale;
if (ScoopUI != "")
ScoopUI += "\n";
ScoopUI += Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource", resources[i].Name, resAmt.ToString("G3"));

//Utils.Log($"[SpaceDustHarveste] sampled {resources[i].Name} @ {resourceSample}. Harvesting {resAmt * TimeWarp.fixedDeltaTime} at step {TimeWarp.fixedDeltaTime}");
part.RequestResource(resources[i].Name, -resAmt * TimeWarp.fixedDeltaTime, ResourceFlowMode.ALL_VESSEL, false);
}
double resourceSample = SpaceDustResourceMap.Instance.SampleResource(resources[i].Name,
part.vessel.mainBody,
vessel.altitude + part.vessel.mainBody.Radius,
vessel.latitude,
vessel.longitude);

if (resourceSample * intakeVolume * resources[i].BaseEfficiency > resources[i].MinHarvestValue)
{
//Utils.Log($"[SpaceDustHarvesterd] sampled {resources[i].Name} @ {resourceSample}, " +
// $"minH {resources[i].MinHarvestValue}," +
// $"effic {resources[i].BaseEfficiency}," +
// $"volume {intakeVolume}," +
// $"area {IntakeArea}," +
// $"speedstatic {IntakeSpeedStatic}," +
// $"worldvel {worldVelocity.magnitude}");

double resAmt = resourceSample * intakeVolume * 1d / resources[i].density * resources[i].BaseEfficiency * scale;
if (ScoopUI != "")
ScoopUI += "\n";
ScoopUI += Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource", resources[i].Name, resAmt.ToString("G3"));

//Utils.Log($"[SpaceDustHarveste] sampled {resources[i].Name} @ {resourceSample}. Harvesting {resAmt * TimeWarp.fixedDeltaTime} at step {TimeWarp.fixedDeltaTime}");
part.RequestResource(resources[i].Name, -resAmt * TimeWarp.fixedDeltaTime, ResourceFlowMode.ALL_VESSEL, false);
}

}
if (ScoopUI == "")
ScoopUI = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource_None");
}
if (ScoopUI == "")
ScoopUI = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustHarvester_Field_Scoop_Resource_None");
}

}
Expand Down
15 changes: 3 additions & 12 deletions Source/SpaceDust/Modules/ModuleSpaceDustScanner.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using KSP.Localization;

namespace SpaceDust
Expand Down Expand Up @@ -185,7 +183,6 @@ void FixedUpdate()
{
double consumption = part.RequestResource(PartResourceLibrary.ElectricityHashcode, chargeRequest);
if (consumption >= chargeRequest - 0.0001)

{
// do scanning
for (int i = 0; i < resources.Count; i++)
Expand Down Expand Up @@ -232,9 +229,7 @@ void FixedUpdate()
// This mode discovers all bands at the body
if (resources[i].IdentifyMode == DiscoverMode.SOI)
{

SpaceDustScenario.Instance.IdentifyResourceBandsAtBody(resources[i].Name, vessel.mainBody);

}
// This mode discovers modes if we are close enough
if (resources[i].IdentifyMode == DiscoverMode.Altitude)
Expand Down Expand Up @@ -265,8 +260,8 @@ void FixedUpdate()
}
}

if (message != "")
message += "\n";
//if (message != "")
message += "\n ";
message += Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustScanner_Field_Resources_SingleSample", resources[i].Name, resourceSample.ToString("G2"));
}

Expand All @@ -281,8 +276,6 @@ void FixedUpdate()
message = Localizer.Format("#LOC_SpaceDust_ModuleSpaceDustScanner_Field_Resources_NoPower");
}
}


}
else
{
Expand All @@ -304,7 +297,6 @@ void HandleAnimation()
{
if (Enabled)
{

foreach (AnimationState anim in scanState)
{
anim.speed = 1f;
Expand All @@ -313,7 +305,6 @@ void HandleAnimation()
}
else
{

foreach (AnimationState anim in scanState)
{
anim.speed = -1f;
Expand Down
1 change: 0 additions & 1 deletion Source/SpaceDust/Modules/ModuleSpaceDustTelescope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace SpaceDust
{

public class InstrumentSlot
{
public string SlotName;
Expand Down
2 changes: 1 addition & 1 deletion Source/SpaceDust/Overlay/MapOverlayInspectorPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void SetInspectIdentified(ResourceBand band)
{
concentration.enabled = true;
}
double smple = band.Abundance / PartResourceLibrary.Instance.GetDefinition(band.ResourceName).density;
double smple = band.Abundance;
concentration.text = Localizer.Format(BAND_CONCENTRATION_KEY, smple.ToString("G3"));
icon.sprite = SpaceDustAssets.Sprites[SpriteMap[band.BandType]];
currentBand = band;
Expand Down
6 changes: 6 additions & 0 deletions Source/SpaceDust/ResourceBand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ public bool CheckDistanceToCenter(double testAltitude, double proximityThreshold
return false;
}

/// <summary>
/// Sample the resource band
/// </summary>
/// <param name="altitude"></param>
/// <param name="latitude"></param>
/// <param name="longitude"></param>
/// <returns>amount of resource in t/m3</returns>
public double Sample(double altitude, double latitude, double longitude)
{
double sampleResult = Abundance * Distribution.Sample(altitude, latitude, longitude);
Expand Down
3 changes: 1 addition & 2 deletions Source/SpaceDust/ResourceDistribution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Initialize()
}

/// <summary>
/// Sample all the bands at this coordinate
/// Sample all the bands at this coordinate and returns a value in t/m3
/// </summary>
/// <param name="altitude"></param>
/// <param name="latitude"></param>
Expand All @@ -52,7 +52,6 @@ public double Sample(double altitude, double latitude, double longitude)
double sampleResult = 0d;
for (int i = 0; i< Bands.Count ;i++)
{

sampleResult += Bands[i].Sample(altitude, latitude, longitude);
}
return sampleResult;
Expand Down
4 changes: 2 additions & 2 deletions Source/SpaceDust/ResourceMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Load()
}
}
/// <summary>
/// Samples a specified resource based on a location
/// Samples a specified resource based on a location and return a value in t/m3
/// </summary>
/// <param name="ResourceName"></param>
/// <param name="body"></param>
Expand All @@ -64,7 +64,7 @@ public double SampleResource(string ResourceName, CelestialBody body, double alt
{
if (Resources[body.name][i].ResourceName == ResourceName)
{
sampledTotal += Resources[body.name][i].Sample(altitude, latitude, longitude) / PartResourceLibrary.Instance.GetDefinition(ResourceName).density;
sampledTotal += Resources[body.name][i].Sample(altitude, latitude, longitude);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SpaceDust/UI/BandResourceElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected void SetBandData(CelestialBody body, ResourceBand bnd)
concentration.enabled = true;

bandName.text = bnd.title;
double smple = bnd.Abundance / PartResourceLibrary.Instance.GetDefinition(bnd.ResourceName).density;
double smple = bnd.Abundance;
concentration.text = Localizer.Format(BAND_CONCENTRATION_KEY, smple.ToString("G3"));
icon.sprite = SpaceDustAssets.Sprites[SpriteMap[bnd.BandType]];
}
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
v0.5.0
------
- Updated bundled MM to latest version
- UI now generally displays in t/m3 and t/s. This is to facilitiate comparisons between resources
- Improved the Resource Overlay
-- When a Resource is toggled on, mousing over a distribution in the map view will show a tooltip that describes the resource
-- Resources that are identified will show a max concentration, unidentified will show unknown
Expand Down

0 comments on commit 5329440

Please sign in to comment.