Skip to content

Commit

Permalink
Update to 1.6.7
Browse files Browse the repository at this point in the history
  • Loading branch information
sswelm committed Feb 14, 2016
1 parent cb00fb9 commit 6ab2b0b
Show file tree
Hide file tree
Showing 24 changed files with 1,233 additions and 58 deletions.
44 changes: 22 additions & 22 deletions FNPlugin/Extensions/OrbitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ public static Orbit Clone(this Orbit orbit0)
}

// Perturb an orbit by a deltaV vector
public static void Perturb(this Orbit orbit, Vector3d deltaVV, double UniversalTime, double deltaTime)
public static void Perturb(this Orbit orbit, Vector3d deltaVV, double universalTime, double deltaTime)
{
// If there is a deltaV, perturb orbit
if (deltaVV.magnitude <= 0) return;

// Transpose deltaVV Y and Z to match orbit frame
Vector3d deltaVV_orbit = deltaVV.xzy;
Vector3d position = orbit.getRelativePositionAtUT(UniversalTime);
//Orbit orbit2 = orbit.Clone();
//orbit2.UpdateFromStateVectors(position, orbit.getOrbitalVelocityAtUT(UniversalTime) + deltaVV_orbit, orbit.referenceBody, UniversalTime);
//if (!double.IsNaN(orbit2.inclination) && !double.IsNaN(orbit2.eccentricity) && !double.IsNaN(orbit2.semiMajorAxis) && orbit2.timeToAp > deltaTime)
//{
// orbit.inclination = orbit2.inclination;
// orbit.eccentricity = orbit2.eccentricity;
// orbit.semiMajorAxis = orbit2.semiMajorAxis;
// orbit.LAN = orbit2.LAN;
// orbit.argumentOfPeriapsis = orbit2.argumentOfPeriapsis;
// orbit.meanAnomalyAtEpoch = orbit2.meanAnomalyAtEpoch;
// orbit.epoch = orbit2.epoch;
// orbit.referenceBody = orbit2.referenceBody;
// orbit.Init();
// orbit.UpdateFromUT(UniversalTime);
//}
//else
//{
orbit.UpdateFromStateVectors(position, orbit.getOrbitalVelocityAtUT(UniversalTime) + deltaVV_orbit, orbit.referenceBody, UniversalTime);
Vector3d position = orbit.getRelativePositionAtUT(universalTime);
Orbit orbit2 = orbit.Clone();
orbit2.UpdateFromStateVectors(position, orbit.getOrbitalVelocityAtUT(universalTime) + deltaVV_orbit, orbit.referenceBody, universalTime);
if (!double.IsNaN(orbit2.inclination) && !double.IsNaN(orbit2.eccentricity) && !double.IsNaN(orbit2.semiMajorAxis) && orbit2.timeToAp > deltaTime)
{
orbit.inclination = orbit2.inclination;
orbit.eccentricity = orbit2.eccentricity;
orbit.semiMajorAxis = orbit2.semiMajorAxis;
orbit.LAN = orbit2.LAN;
orbit.argumentOfPeriapsis = orbit2.argumentOfPeriapsis;
orbit.meanAnomalyAtEpoch = orbit2.meanAnomalyAtEpoch;
orbit.epoch = orbit2.epoch;
orbit.referenceBody = orbit2.referenceBody;
orbit.Init();
orbit.UpdateFromUT(UniversalTime);
//}
orbit.UpdateFromUT(universalTime);
}
else
{
orbit.UpdateFromStateVectors(position, orbit.getOrbitalVelocityAtUT(universalTime) + deltaVV_orbit, orbit.referenceBody, universalTime);
orbit.Init();
orbit.UpdateFromUT(universalTime);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions FNPlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.6.6.0")]
[assembly: AssemblyFileVersion("1.6.6.0")]
[assembly: AssemblyVersion("1.6.7.0")]
[assembly: AssemblyFileVersion("1.6.7.0")]
8 changes: 4 additions & 4 deletions FNPlugin/Propulsion/AlcubierreDrive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,13 +646,13 @@ public void FixedUpdate() // FixedUpdate is also called when not activated
{
vesselTotalMass = vessel.GetTotalMass();
gravityPull = (float)FlightGlobals.getGeeForceAtPosition(vessel.GetWorldPos3D()).magnitude;
maximumWarpForGravityPull = 1 / Mathf.Pow(gravityPull, 2);
maximumWarpForGravityPull = 1 / (Mathf.Pow(gravityPull, 1.5f) * 2);
maximumWarpSpeedFactor = GetMaximumFactor(maximumWarpForGravityPull);
maximumAllowedWarpThrotle = engine_throtle[maximumWarpSpeedFactor];
minimumPowerAllowedFactor = maximumWarpSpeedFactor > minimum_selected_factor ? maximumWarpSpeedFactor : minimum_selected_factor;
}

if (vesselTotalMass != 0)
if (sumOfAlcubierreDrives != 0 && vesselTotalMass != 0)
{
warpToMassRatio = sumOfAlcubierreDrives / vesselTotalMass;
exotic_power_required = (GameConstants.initial_alcubierre_megajoules_required * vesselTotalMass) / warpToMassRatio;
Expand Down Expand Up @@ -697,7 +697,7 @@ public override void OnFixedUpdate()

WarpdriveCharging();

UpdateWarpspeed();
UpdateWarpSpeed();
}

private void UpdateWarpDriveStatus(float currentExoticMatter, double lostWarpFieldForWarp)
Expand Down Expand Up @@ -780,7 +780,7 @@ private float GetPowerRequirementForWarp(float lightspeedFraction)

private int insufficientPowerTimeout = 10;

public void UpdateWarpspeed()
public void UpdateWarpSpeed()
{
if (!IsEnabled || exotic_power_required <= 0) return;

Expand Down
4 changes: 1 addition & 3 deletions FNPlugin/Propulsion/ModuleEnginesWarp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

namespace FNPlugin
{
//public class ModuleEnginesWarp: WarpModuleEnginesFX {}

public class ModuleEnginesWarp : ModuleEnginesFX
{
// GUI display values
Expand Down Expand Up @@ -135,7 +133,7 @@ public override void OnFixedUpdate()
{
double vesselMass = this.vessel.GetTotalMass(); // Current mass
double m1 = vesselMass - dm; // Mass at end of burn
double deltaV = IspPersistent * 9.81 * Math.Log(vesselMass / m1); // Delta V from burn
double deltaV = IspPersistent * PluginHelper.GravityConstant * Math.Log(vesselMass / m1); // Delta V from burn

Vector3d thrustV = this.part.transform.up; // Thrust direction
Vector3d deltaVV = deltaV * thrustV; // DeltaV vector
Expand Down
192 changes: 192 additions & 0 deletions FNPlugin/Propulsion/ModuleEnginesWarpvISTA.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
using System;
using System.Linq;
using UnityEngine;
using FNPlugin.Extensions;

namespace FNPlugin
{
public class ModuleEnginesWarpVista : ModuleEngines
{
// GUI display values
[KSPField(isPersistant = true)]
bool IsForceActivated;

[KSPField(guiActive = true, guiName = "Warp Thrust")]
protected string Thrust = "";
[KSPField(guiActive = true, guiName = "Warp Isp")]
protected string Isp = "";
[KSPField(guiActive = true, guiName = "Warp Throttle")]
protected string Throttle = "";

[KSPField(guiActive = true, guiName = "Mass Flow", guiFormat = "F8", guiUnits=" U")]
public float totalMassFlow;
[KSPField(guiActive = true, guiName = "Hydrogen Used", guiFormat = "F6")]
public float propellantUsed;
[KSPField(guiActive = true, guiName = "Deuterium Used", guiFormat = "F6")]
public float deuteriumUsed;
[KSPField(guiActive = true, guiName = "Tritium Used", guiFormat = "F6")]
public float tritiumUsed;

// Numeric display values
protected double thrust_d = 0;
protected double isp_d = 0;
protected double throttle_d = 0;

// Persistent values to use during timewarp
float IspPersistent = 0;
float ThrustPersistent = 0;
float ThrottlePersistent = 0;
float previousThrottle = 0;

// Are we transitioning from timewarp to reatime?
[KSPField]
bool warpToReal = false;

// Density of resource
float densityHydrogen;
float densityDeuterium;
float densityTritium;

Propellant hydrogenPropellant;
Propellant deuteriumPropellant;
Propellant tritiumPropellant;

// Update
public override void OnUpdate()
{
// When transitioning from timewarp to real update throttle
if (warpToReal)
{
vessel.ctrlState.mainThrottle = ThrottlePersistent;
warpToReal = false;
}

// Persistent thrust GUI
Fields["Thrust"].guiActive = isEnabled;
Fields["Isp"].guiActive = isEnabled;
Fields["Throttle"].guiActive = isEnabled;

// Update display values
Thrust = FormatThrust(thrust_d);
Isp = Math.Round(isp_d, 2).ToString() + " s";
Throttle = Math.Round(throttle_d * 100).ToString() + "%";

if (!IsForceActivated && isEnabled && isOperational)
{
IsForceActivated = true;
part.force_activate();
}
}

// Initialization
public override void OnLoad(ConfigNode node)
{
// Run base OnLoad method
base.OnLoad(node);

// Initialize density of propellant used in deltaV and mass calculations
densityHydrogen = PartResourceLibrary.Instance.GetDefinition(InterstellarResourcesConfiguration.Instance.Hydrogen).density;
densityDeuterium = PartResourceLibrary.Instance.GetDefinition(InterstellarResourcesConfiguration.Instance.Deuterium).density;
densityTritium = PartResourceLibrary.Instance.GetDefinition(InterstellarResourcesConfiguration.Instance.Tritium).density;

hydrogenPropellant = this.propellants.FirstOrDefault(pr => pr.name == InterstellarResourcesConfiguration.Instance.Hydrogen);
deuteriumPropellant = this.propellants.FirstOrDefault(pr => pr.name == InterstellarResourcesConfiguration.Instance.Deuterium);
tritiumPropellant = this.propellants.FirstOrDefault(pr => pr.name == InterstellarResourcesConfiguration.Instance.Tritium);
}

// Physics update
public override void OnFixedUpdate()
{
if (FlightGlobals.fetch == null || !isEnabled) return;

totalMassFlow = this.requestedMassFlow;

// retreive ratios
var propellant_ratio = hydrogenPropellant.ratio;
var deuterium_ratio = deuteriumPropellant.ratio;
var tritium_ratio = tritiumPropellant.ratio;
var sumOfRatios = propellant_ratio + deuterium_ratio + tritium_ratio;

// Resource demand
float demandReqPropellant = ((propellant_ratio / sumOfRatios) * this.requestedMassFlow) / densityHydrogen;
float demandReqDeuterium = ((deuterium_ratio / sumOfRatios) * this.requestedMassFlow) / densityDeuterium;
float demandReqTritium = ((tritium_ratio / sumOfRatios) * this.requestedMassFlow) / densityTritium;

// Realtime mode
if (!this.vessel.packed)
{
// Resource demand
propellantUsed = demandReqPropellant;
deuteriumUsed = demandReqDeuterium;
tritiumUsed = demandReqTritium;

// if not transitioning from warp to real
// Update values to use during timewarp
if (!warpToReal) //&& vessel.ctrlState.mainThrottle == previousThrottle)
{
IspPersistent = realIsp;
ThrottlePersistent = vessel.ctrlState.mainThrottle;

this.CalculateThrust();
// verify we have thrust
if ((vessel.ctrlState.mainThrottle > 0 && finalThrust > 0) || (vessel.ctrlState.mainThrottle == 0 && finalThrust == 0))
ThrustPersistent = finalThrust;
}
}
else if (part.vessel.situation != Vessel.Situations.SUB_ORBITAL)
{
// Timewarp mode: perturb orbit using thrust
warpToReal = true; // Set to true for transition to realtime
double UT = Planetarium.GetUniversalTime(); // Universal time

propellantUsed = (float)part.RequestResource(InterstellarResourcesConfiguration.Instance.Hydrogen, demandReqPropellant * TimeWarp.fixedDeltaTime) / TimeWarp.fixedDeltaTime;
deuteriumUsed = (float)part.RequestResource(InterstellarResourcesConfiguration.Instance.Deuterium, demandReqDeuterium * TimeWarp.fixedDeltaTime) / TimeWarp.fixedDeltaTime;
tritiumUsed = (float)part.RequestResource(InterstellarResourcesConfiguration.Instance.Tritium, demandReqTritium * TimeWarp.fixedDeltaTime) / TimeWarp.fixedDeltaTime;

// Calculate thrust and deltaV if demand output > 0
if (propellantUsed > 0 && deuteriumUsed > 0 && tritiumUsed > 0)
{
double vesselMass = this.vessel.GetTotalMass(); // Current mass
double m1 = vesselMass - (this.requestedMassFlow * TimeWarp.fixedDeltaTime); // Mass at end of burn

if (m1 <= 0 || vesselMass <= 0)
return;

double deltaV = IspPersistent * PluginHelper.GravityConstant * Math.Log(vesselMass / m1); // Delta V from burn

Vector3d thrustV = this.part.transform.up; // Thrust direction
Vector3d deltaVV = deltaV * thrustV; // DeltaV vector
vessel.orbit.Perturb(deltaVV, UT, TimeWarp.fixedDeltaTime); // Update vessel orbit
}
// Otherwise, if throttle is turned on, and demand out is 0, show warning
else if (ThrottlePersistent > 0)
{
ScreenMessages.PostScreenMessage("Out of resource", 5.0f);
}
}
else //if (vessel.ctrlState.mainThrottle > 0)
{
ScreenMessages.PostScreenMessage("Cannot accelerate and timewarp durring sub orbital spaceflight!", 5.0f, ScreenMessageStyle.UPPER_CENTER);
}


// Update display numbers
thrust_d = ThrustPersistent;
isp_d = IspPersistent;
throttle_d = ThrottlePersistent;
previousThrottle = vessel.ctrlState.mainThrottle;
}

// Format thrust into mN, N, kN
public static string FormatThrust(double thrust)
{
if (thrust < 0.001)
return Math.Round(thrust * 1000000.0, 3).ToString() + " mN";
else if (thrust < 1.0)
return Math.Round(thrust * 1000.0, 3).ToString() + " N";
else
return Math.Round(thrust, 3).ToString() + " kN";
}
}

}
11 changes: 8 additions & 3 deletions FNPlugin/Propulsion/VistaEngineController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public override void OnFixedUpdate()

if (curEngineT == null) return;

float throttle = curEngineT.currentThrottle;
float throttle = curEngineT.currentThrottle > 0 ? Mathf.Max(curEngineT.currentThrottle, 0.01f) : 0;

//double atmo_thrust_factor = Math.Min(1.0, Math.Max(1.0 - Math.Pow(vessel.atmDensity, 0.2), 0));

Expand Down Expand Up @@ -270,12 +270,17 @@ public override void OnFixedUpdate()
}
else
{
var currentIsp = minISP * 100;

FloatCurve newISP = new FloatCurve();
newISP.Add(0, (float)minISP);
newISP.Add(0, (float)currentIsp);
curEngineT.atmosphereCurve = newISP;

var maxFuelFlow = MaximumThrust / minISP / PluginHelper.GravityConstant;
var maxFuelFlow = MaximumThrust / currentIsp / PluginHelper.GravityConstant;
curEngineT.maxFuelFlow = (float)maxFuelFlow;

curEngineT.propellants.FirstOrDefault(pr => pr.name == InterstellarResourcesConfiguration.Instance.Deuterium).ratio = (float)(standard_deuterium_rate);
curEngineT.propellants.FirstOrDefault(pr => pr.name == InterstellarResourcesConfiguration.Instance.Tritium).ratio = (float)(standard_tritium_rate);
}

radiatorPerformance = (float)Math.Max(1 - (float)(coldBathTemp / maxTempatureRadiators), 0.000001);
Expand Down
Loading

0 comments on commit 6ab2b0b

Please sign in to comment.