Skip to content

Commit

Permalink
Merge pull request #2 from Kopernicus/1.8
Browse files Browse the repository at this point in the history
1.8
  • Loading branch information
linuxgurugamer committed Jan 18, 2020
2 parents acd5529 + 57608d3 commit 0d109b8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 180 deletions.
104 changes: 62 additions & 42 deletions src/Kopernicus/Components/HazardousBody.cs
Expand Up @@ -34,101 +34,121 @@ namespace Kopernicus.Components
public class HazardousBody : MonoBehaviour
{
/// <summary>
/// The average heat rate on the surface of the body. Gets multiplied by latitude, longitude and altitude curves
/// The maximum temperature that will eventually be reached.
/// </summary>
public Double heatRate;
public Double maxTemp = 0;

/// <summary>
/// How many seconds should pass between applying the heat rate to the ship.
/// How many seconds it'll take to get halfway to maxTemp.
/// </summary>
public Single heatInterval = 0.05f;
public Single lambda = 0;

/// <summary>
/// Controls the heat rate at a certain latitude
/// Multiplier curve to change maxTemp with latitude
/// </summary>
public FloatCurve latitudeCurve;

/// <summary>
/// Controls the heat rate at a certain longitude
/// Multiplier curve to change maxTemp with longitude
/// </summary>
public FloatCurve longitudeCurve;

/// <summary>
/// Controls the heat rate at a certain altitude
/// Multiplier curve to change maxTemp with altitude
/// </summary>
public FloatCurve altitudeCurve;

/// <summary>
/// Controls the amount of heat that is applied on each spot of the planet
/// Multiplier map for maxTemp
/// </summary>
public MapSO heatMap;
}

private CelestialBody _body;

// A timer that counts how much time is left until the next heating time. Negative value means the heating is
// late and there is heating "debt" that needs to be paid off.
private Single _heatPosition;
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class HazardousTracker : MonoBehaviour
{
void Start()
{
GameEvents.onVesselLoaded.Add(OnVesselLoaded);
GameEvents.onVesselCreate.Add(OnVesselCreate);
DontDestroyOnLoad(this);
}

/// <summary>
/// Get the body
/// </summary>
private void Start()
private void OnVesselCreate(Vessel data)
{
data.gameObject.AddOrGetComponent<HazardousVessel>();
}

void OnVesselLoaded(Vessel vessel)
{
vessel.gameObject.AddOrGetComponent<HazardousVessel>();
}
}

public class HazardousVessel : MonoBehaviour
{
Vessel vessel;
CelestialBody _body;
HazardousBody hazardousBody;

void Start()
{
_body = GetComponent<CelestialBody>();
vessel = GetComponent<Vessel>();
}

/// <summary>
/// Update the heat. Heating is physics phenomenon so do it in the physics loop.
/// Update the temperature. This is physics phenomenon so do it in the physics loop.
/// </summary>
/// <returns></returns>
private void FixedUpdate()
{
// Check for an active vessel
if (!FlightGlobals.ActiveVessel || FlightGlobals.ActiveVessel.packed)
// Check if the vessel is near the body
if (FlightGlobals.currentMainBody != vessel.mainBody)
{
return;
}

// Check if the vessel is near the body
if (FlightGlobals.currentMainBody != _body)
// Check if the body has changed
if (_body != FlightGlobals.currentMainBody)
{
return;
_body = FlightGlobals.currentMainBody;
hazardousBody = _body.GetComponent<HazardousBody>();
}

// Decrement timer. If it becomes zero or less, it's time to apply heating.
_heatPosition -= TimeWarp.fixedDeltaTime;
if (_heatPosition > 0f)
// Check if the body is hazardous
if (!hazardousBody)
{
return;
}

// We got past the counter - update time
Double altitude =
altitudeCurve.Evaluate((Single) Vector3d.Distance(FlightGlobals.ActiveVessel.transform.position,
hazardousBody.altitudeCurve.Evaluate((Single)Vector3d.Distance(vessel.transform.position,
_body.transform.position));
Double latitude = latitudeCurve.Evaluate((Single) FlightGlobals.ActiveVessel.latitude);
Double longitude = longitudeCurve.Evaluate((Single) FlightGlobals.ActiveVessel.longitude);
Double latitude = hazardousBody.latitudeCurve.Evaluate((Single)vessel.latitude);
Double longitude = hazardousBody.longitudeCurve.Evaluate((Single)vessel.longitude);

Double heat = altitude * latitude * longitude * heatRate;
if (heatMap)
Double maxTemp = altitude * latitude * longitude * hazardousBody.maxTemp;
if (hazardousBody.heatMap)
{
const Single FULL_CIRCLE = 1f / 360f;
const Single HALF_CIRCLE = 1f / 180f;

heat *= heatMap.GetPixelFloat((longitude + 180) * FULL_CIRCLE, (latitude + 90) * HALF_CIRCLE);
maxTemp *= hazardousBody.heatMap.GetPixelFloat((longitude + 180) * FULL_CIRCLE, (latitude + 90) * HALF_CIRCLE);
}

// How many rounds of heating need to be applied before the timer is positive again (heating "debt" is paid
// off).
UInt32 heatingRounds = (UInt32) Math.Floor(-_heatPosition / heatInterval + 1);

foreach (Part part in FlightGlobals.ActiveVessel.Parts)
for (int i = vessel.Parts.Count; i > 0; i--)
{
part.temperature += heat * heatingRounds;
}
Part part = vessel.Parts[i - 1];

// Increment timer so that it becomes positive.
_heatPosition += heatInterval * heatingRounds;
if (part.temperature < maxTemp)
{
part.temperature += ((maxTemp - part.temperature) * 0.69420 / hazardousBody.lambda) * TimeWarp.fixedDeltaTime;

if (part.temperature > maxTemp)
part.temperature = maxTemp;
}
}
}
}
}
87 changes: 0 additions & 87 deletions src/Kopernicus/Components/HazardousOcean.cs

This file was deleted.

40 changes: 20 additions & 20 deletions src/Kopernicus/Configuration/HazardousBodyLoader.cs
Expand Up @@ -44,54 +44,54 @@ public class HazardousBodyLoader : BaseLoader, ITypeParser<HazardousBody>
// Set-up our parental objects
public HazardousBody Value { get; set; }

// The average heat on the body
[ParserTarget("heat")]
[KittopiaDescription("The average heat on the body.")]
public NumericParser<Double> Heat
// The maximum temperature that will eventually be reached.
[ParserTarget("maxTemp")]
[KittopiaDescription("The maximum temperature that will eventually be reached.")]
public NumericParser<Double> MaxTemp
{
get { return Value.heatRate; }
set { Value.heatRate = value; }
get { return Value.maxTemp; }
set { Value.maxTemp = value; }
}

// How much time passes between applying the heat to a vessel
[ParserTarget("interval")]
[KittopiaDescription("How much time passes between applying the heat to a vessel.")]
public NumericParser<Single> Interval
// How many seconds it'll take to get halfway to maxTemp.
[ParserTarget("lambda")]
[KittopiaDescription("How many seconds it'll take to get halfway to maxTemp.")]
public NumericParser<Single> Lambda
{
get { return Value.heatInterval; }
set { Value.heatInterval = value; }
get { return Value.lambda; }
set { Value.lambda = value; }
}

// Controls the how much of the average heat gets applied at a certain altitude
// Multiplier curve to change maxTemp with altitude
[ParserTarget("AltitudeCurve")]
[KittopiaDescription("Controls the how much of the average heat gets applied at a certain altitude.")]
[KittopiaDescription("Multiplier curve to change maxTemp with altitude.")]
public FloatCurveParser AltitudeCurve
{
get { return Value.altitudeCurve; }
set { Value.altitudeCurve = value; }
}

// Controls the how much of the average heat gets applied at a certain latitude
// Multiplier curve to change maxTemp with latitude
[ParserTarget("LatitudeCurve")]
[KittopiaDescription("Controls the how much of the average heat gets applied at a certain latitude.")]
[KittopiaDescription("Multiplier curve to change maxTemp with latitude.")]
public FloatCurveParser LatitudeCurve
{
get { return Value.latitudeCurve; }
set { Value.latitudeCurve = value; }
}

// Controls the how much of the average heat gets applied at a certain longitude
// Multiplier curve to change maxTemp with longitude
[ParserTarget("LongitudeCurve")]
[KittopiaDescription("Controls the how much of the average heat gets applied at a certain longitude.")]
[KittopiaDescription("Multiplier curve to change maxTemp with longitude.")]
public FloatCurveParser LongitudeCurve
{
get { return Value.longitudeCurve; }
set { Value.longitudeCurve = value; }
}

// Controls the how much of the average heat gets applied at a certain longitude
// Multiplier map for maxTemp
[ParserTarget("HeatMap")]
[KittopiaDescription("Greyscale map for fine control of the heat on a planet. black = 0, white = 1")]
[KittopiaDescription("Greyscale map for fine control of the maxTemp on a planet. black = 0, white = 1")]
public MapSOParserGreyScale<MapSO> HeatMap
{
get { return Value.heatMap; }
Expand Down
30 changes: 0 additions & 30 deletions src/Kopernicus/Configuration/OceanLoader.cs
Expand Up @@ -163,36 +163,6 @@ public Material FallbackMaterial
[SuppressMessage("ReSharper", "CollectionNeverQueried.Global")]
public readonly List<IModLoader> Mods = new List<IModLoader>();

// Killer-Ocean
[ParserTarget("HazardousOcean", AllowMerge = true)]
public FloatCurveParser HazardousOcean
{
get
{
HazardousOcean ocean = Value.gameObject.GetComponent<HazardousOcean>();
return ocean == null ? null : ocean.heatCurve;
}
set
{
HazardousOcean ocean = Value.gameObject.GetComponent<HazardousOcean>();
if (value == null && ocean != null)
{
Object.Destroy(ocean);
return;
}

if (value != null && ocean == null)
{
ocean = Value.gameObject.AddComponent<HazardousOcean>();
}

if (value != null)
{
ocean.heatCurve = value;
}
}
}

// Ocean-Fog
[ParserTarget("Fog", AllowMerge = true)]
[KittopiaUntouchable]
Expand Down
1 change: 0 additions & 1 deletion src/Kopernicus/Kopernicus.csproj
Expand Up @@ -68,7 +68,6 @@
<ItemGroup>
<Compile Include="Components\DrawTools.cs" />
<Compile Include="Components\HazardousBody.cs" />
<Compile Include="Components\HazardousOcean.cs" />
<Compile Include="Components\KopernicusOrbitRendererData.cs" />
<Compile Include="Components\KopernicusSimplexWrapper.cs" />
<Compile Include="Components\KopernicusSolarPanels.cs" />
Expand Down

0 comments on commit 0d109b8

Please sign in to comment.