Skip to content

Commit

Permalink
Merge pull request #519 from MOARdV/master
Browse files Browse the repository at this point in the history
v0.26.0 final
  • Loading branch information
MOARdV committed Apr 30, 2016
2 parents ecc1302 + 01ac265 commit db55e29
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 75 deletions.
4 changes: 2 additions & 2 deletions GameData/JSI/RasterPropMonitor/RasterPropMonitor.version
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"KSP_VERSION": {
"MAJOR": 1,
"MINOR": 1,
"PATCH": 1
"PATCH": 2
},
"KSP_VERSION_MIN":
{
"MAJOR": 1,
"MINOR": 1,
"PATCH": 1
"PATCH": 2
},
}
67 changes: 19 additions & 48 deletions RasterPropMonitor/Core/CelestialBodyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,23 @@

namespace JSI
{
// This class is imported wholesale from MechJeb.
public static class CelestialBodyExtensions
{
public static double TerrainAltitude(this CelestialBody body, Vector3d worldPosition)
{
return body.TerrainAltitude(body.GetLatitude(worldPosition), body.GetLongitude(worldPosition));
}

public static double TerrainAltitude(this CelestialBody body, double latitude, double longitude)
{
if (body.pqsController == null) return 0;


Vector3d pqsRadialVector = QuaternionD.AngleAxis(longitude, Vector3d.down) * QuaternionD.AngleAxis(latitude, Vector3d.forward) * Vector3d.right;
double ret = body.pqsController.GetSurfaceHeight(pqsRadialVector) - body.pqsController.radius;
if (ret < 0)
ret = 0;
return ret;
}


//The KSP drag law is dv/dt = -b * v^2 where b is proportional to the air density and
//the ship's drag coefficient. In this equation b has units of inverse length. So 1/b
//is a characteristic length: a ship that travels this distance through air will lose a significant
//fraction of its initial velocity
public static double DragLength(this CelestialBody body, Vector3d pos, double dragCoeff)
{
double airDensity = FlightGlobals.getAtmDensity(FlightGlobals.getStaticPressure(pos, body), FlightGlobals.getExternalTemperature(pos, body));

return 1.0 / (0.5 * PhysicsGlobals.DragMultiplier * airDensity * dragCoeff);
}

public static double DragLength(this CelestialBody body, double altitudeASL, double dragCoeff)
{
return body.DragLength(body.GetWorldSurfacePosition(0, 0, altitudeASL), dragCoeff);
}


//CelestialBody.maxAtmosphereAltitude doesn't actually give the upper edge of
//the atmosphere. Use this function instead.
public static double RealMaxAtmosphereAltitude(this CelestialBody body)
{
//#warning check if atmosphereDepth = 0 when !body.atmosphere and remove the whole ext
if (!body.atmosphere)
return 0;
return body.atmosphereDepth;
}
}
// This class was imported wholesale from MechJeb.
public static class CelestialBodyExtensions
{
public static double TerrainAltitude(this CelestialBody body, Vector3d worldPosition)
{
return body.TerrainAltitude(body.GetLatitude(worldPosition), body.GetLongitude(worldPosition));
}


//CelestialBody.maxAtmosphereAltitude doesn't actually give the upper edge of
//the atmosphere. Use this function instead.
public static double RealMaxAtmosphereAltitude(this CelestialBody body)
{
//#warning check if atmosphereDepth = 0 when !body.atmosphere and remove the whole ext
if (!body.atmosphere)
return 0;
return body.atmosphereDepth;
}
}
}
46 changes: 46 additions & 0 deletions RasterPropMonitor/Core/RPMVCVariableToObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,22 @@ VariableEvaluator GetEvaluator(string input, out bool cacheable)
return 1d;
return 0d;
};
case "TARGETISPOSITION":
return (string variable) =>
{
if (target == null)
{
return -1d;
}
else if(target is PositionTarget)
{
return 1d;
}
else
{
return 0d;
}
};
case "TARGETSITUATION":
return (string variable) =>
{
Expand Down Expand Up @@ -1737,6 +1753,10 @@ VariableEvaluator GetEvaluator(string input, out bool cacheable)
return (string variable) => { return (part != null) ? (part.skinTemperature + KelvinToCelsius) : 0.0; };
case "PODSKINTEMPERATUREKELVIN":
return (string variable) => { return (part != null) ? (part.skinTemperature) : 0.0; };
case "PODMAXSKINTEMPERATURE":
return (string variable) => { return (part != null) ? (part.skinMaxTemp + KelvinToCelsius) : 0.0; };
case "PODMAXSKINTEMPERATUREKELVIN":
return (string variable) => { return (part != null) ? (part.skinMaxTemp) : 0.0; };
case "PODMAXTEMPERATURE":
return (string variable) => { return (part != null) ? (part.maxTemp + KelvinToCelsius) : 0.0; };
case "PODMAXTEMPERATUREKELVIN":
Expand All @@ -1747,6 +1767,10 @@ VariableEvaluator GetEvaluator(string input, out bool cacheable)
return (string variable) => { return vessel.externalTemperature + KelvinToCelsius; };
case "EXTERNALTEMPERATUREKELVIN":
return (string variable) => { return vessel.externalTemperature; };
case "AMBIENTTEMPERATURE":
return (string variable) => { return vessel.atmosphericTemperature + KelvinToCelsius; };
case "AMBIENTTEMPERATUREKELVIN":
return (string variable) => { return vessel.atmosphericTemperature; };
case "HEATSHIELDTEMPERATURE":
return (string variable) => { return (double)heatShieldTemperature + KelvinToCelsius; };
case "HEATSHIELDTEMPERATUREKELVIN":
Expand Down Expand Up @@ -2119,6 +2143,28 @@ VariableEvaluator GetEvaluator(string input, out bool cacheable)
}
return -1d;
};
case "ORBITBODYSURFACETEMP":
return (string s) => { return FlightGlobals.currentMainBody.atmosphereTemperatureSeaLevel + KelvinToCelsius; };
case "TARGETBODYSURFACETEMP":
return (string s) =>
{
if (targetBody != null)
{
return targetBody.atmosphereTemperatureSeaLevel + KelvinToCelsius;
}
return -1d;
};
case "ORBITBODYSURFACETEMPKELVIN":
return (string s) => { return FlightGlobals.currentMainBody.atmosphereTemperatureSeaLevel; };
case "TARGETBODYSURFACETEMPKELVIN":
return (string s) =>
{
if (targetBody != null)
{
return targetBody.atmosphereTemperatureSeaLevel;
}
return -1d;
};
}

return null;
Expand Down
29 changes: 7 additions & 22 deletions RasterPropMonitor/Core/RPMVesselComputer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,32 +1478,17 @@ private void FetchPerPartData()

// We can use the stock routines to get at the per-stage resources.
// Except KSP 1.1.1 broke GetActiveResources() and GetActiveResource(resource).
// Like exception-throwing broke.
/*
var activeResources = vessel.GetActiveResources();
for (int i = 0; i < activeResources.Count; ++i)
{
resources.SetActive(activeResources[i]);
}
*/
/*
// Like exception-throwing broke. It was fixed in 1.1.2, but I
// already put together a work-around.
try
{
//var ro = UnityEngine.Object.FindObjectOfType<KSP.UI.Screens.ResourceOverlay>();
var ro = KSP.UI.Screens.ResourceDisplay.Instance;
var resitems = ro.resourceItems;
for (int i = 0; i < resitems.Count; ++i)
var activeResources = vessel.GetActiveResources();
for (int i = 0; i < activeResources.Count; ++i)
{
JUtil.LogMessage(this, "Resource {0}: vrCurrent {1}, delta {2}, vrPrevious {3}, previousAmt {4}",
resitems[i].nameText.text, resitems[i].vesselResourceCurrent, resitems[i].delta, resitems[i].vesselResourcePrevious, resitems[i].previousAmount);
resources.SetActive(activeResources[i]);
}
// Notes:
// ResourceDisplay exists.
// .delta is consumption per second (at least, I assume that's the units - it's what is displayed).
// .vesselResourceCurrent == .previousAmount == total resource
}
catch { JUtil.LogMessage(this, "Nope. No ResourceOverlay"); }
*/
} catch {}

resources.EndLoop(Planetarium.GetUniversalTime());

// MOARdV TODO: Migrate this to a callback system:
Expand Down
75 changes: 72 additions & 3 deletions RasterPropMonitor/Core/ResourceDataStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class ResourceDataStorage
private readonly string[] sortedResourceNames;
private int numValidResourceNames = 0;

// A dictionary mapping resourceIDs to dictionaries that map
private readonly Dictionary<int, List<PartResource>> activeResources = new Dictionary<int, List<PartResource>>();

private class ResourceComparer : IComparer<ResourceData>
{
public int Compare(ResourceData a, ResourceData b)
Expand All @@ -40,6 +43,11 @@ public int Compare(ResourceData a, ResourceData b)
}
}

private static bool IsFreeFlow(ResourceFlowMode flowMode)
{
return (flowMode == ResourceFlowMode.ALL_VESSEL || flowMode == ResourceFlowMode.ALL_VESSEL_BALANCE || flowMode == ResourceFlowMode.STAGE_PRIORITY_FLOW);
}

public ResourceDataStorage()
{
int resourceCount = 0;
Expand All @@ -58,6 +66,9 @@ public ResourceDataStorage()
rs[index] = new ResourceData();
rs[index].name = thatResource.name;
rs[index].density = thatResource.density;
rs[index].resourceId = thatResource.id;

activeResources.Add(thatResource.id, new List<PartResource>());

nameResources.Add(thatResource.name, rs[index]);
sysrResources.Add(nameSysr, rs[index]);
Expand All @@ -77,6 +88,8 @@ public void StartLoop()
rs[i].stage = 0.0f;
rs[i].stagemax = 0.0f;
rs[i].ispropellant = false;

activeResources[rs[i].resourceId].Clear();
}
}

Expand All @@ -98,6 +111,24 @@ public void EndLoop(double time)
sortedResourceNames[numValidResourceNames] = rs[i].name;
++numValidResourceNames;
}

// See if any engines marked these resources as propellants.
// If so, we have stage and stageMax info available, so we can
// sum them up.
var list = activeResources[rs[i].resourceId];
if (list.Count > 0)
{
float stage = 0.0f, stageMax = 0.0f;

for (int j = 0; j < list.Count; ++j)
{
stage += (float)list[j].amount;
stageMax += (float)list[j].maxAmount;
}

rs[i].stage = stage;
rs[i].stagemax = stageMax;
}
}
}

Expand All @@ -117,16 +148,43 @@ public string GetActiveResourceByIndex(int index)

public void MarkPropellant(Propellant propel)
{
foreach (PartResource resource in propel.connectedResources)
var connectedResources = propel.connectedResources;
for (int resourceIdx = 0; resourceIdx < connectedResources.Count; ++resourceIdx)
{
try
{
ResourceData r = nameResources[resource.info.name];
ResourceData r = nameResources[connectedResources[resourceIdx].info.name];
r.ispropellant = true;

// If the resoruce in question isn't a "free flow" -
// that is, an ALL_VESSEL_* resource - then add the
// PartResource to the list we will consider for stage
// resource availability. But also don't add it if the
// particular PartResource is in the list (as based on
// checking GetHashCode()).
// MOARdV TODO: I *could* make a dictionary instead of list,
// but I don't know if it's worthwhile.
if (!IsFreeFlow(connectedResources[resourceIdx].info.resourceFlowMode))
{
var list = activeResources[r.resourceId];
bool needsAdded = true;
for (int listIndex = 0; listIndex < list.Count; ++listIndex)
{
if (list[listIndex].GetHashCode() == connectedResources[resourceIdx].GetHashCode())
{
needsAdded = false;
break;
}
}
if (needsAdded)
{
list.Add(connectedResources[resourceIdx]);
}
}
}
catch (Exception e)
{
JUtil.LogErrorMessage(this, "Error in MarkPropellant({0}): {1}", resource.info.name, e);
JUtil.LogErrorMessage(this, "Error in MarkPropellant({0}): {1}", connectedResources[resourceIdx].info.name, e);
}
}
}
Expand Down Expand Up @@ -333,6 +391,13 @@ public void Add(PartResource resource)
ResourceData res = nameResources[resource.info.name];
res.current += (float)resource.amount;
res.max += (float)resource.maxAmount;

var flowmode = resource.info.resourceFlowMode;
if (IsFreeFlow(flowmode))
{
res.stage += (float)resource.amount;
res.stagemax += (float)resource.maxAmount;
}
}
catch (Exception e)
{
Expand All @@ -347,6 +412,8 @@ public void SetActive(Vessel.ActiveResource resource)
ResourceData res = nameResources[resource.info.name];
res.stage = (float)resource.amount;
res.stagemax = (float)resource.maxAmount;
var list = activeResources[resource.info.id];
list.Clear();
}
catch (Exception e)
{
Expand All @@ -368,6 +435,8 @@ private class ResourceData
public float density;
public float delta;

public int resourceId;

public bool ispropellant;
}
}
Expand Down

0 comments on commit db55e29

Please sign in to comment.