Skip to content

Commit

Permalink
start wiring up onVesselStandardModification hook
Browse files Browse the repository at this point in the history
starting to go down the road of hooking up onVesselStandardModification to
reduce constant re-walking of the parts tree.

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Dec 31, 2018
1 parent 44d60d4 commit a1deeb7
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 113 deletions.
8 changes: 6 additions & 2 deletions MechJeb2/ComputerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public bool enabled
// Has this module config changed and should it be saved
public bool dirty = false;

//The UserPool is an alternative way to handle enabling/disabling of a ComputerModule.
//The UserPool is an alternative way to handle enabling/disabling of a ComputerModule.
//Users can add and remove themselves from the user pool and the ComputerModule will be
//enabled if and only if there is at least one user. For consistency, it's probably
//best that a given ComputerModule be controlled either entirely through enabled, or
Expand Down Expand Up @@ -121,6 +121,10 @@ public virtual void OnUpdate()
{
}

public virtual void OnVesselStandardModification(Vessel v)
{
}

public virtual void OnLoad(ConfigNode local, ConfigNode type, ConfigNode global)
{
try
Expand Down Expand Up @@ -225,7 +229,7 @@ public enum Pass
Global = 4
}

//Lets multiple users enable and disable a computer module, such that the
//Lets multiple users enable and disable a computer module, such that the
//module only gets disabled when all of its users have disabled it.
public class UserPool : List<object>
{
Expand Down
31 changes: 31 additions & 0 deletions MechJeb2/MechJebCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ public override void OnStart(PartModule.StartState state)
GameEvents.onShowUI.Add(OnShowGUI);
GameEvents.onHideUI.Add(OnHideGUI);
GameEvents.onVesselChange.Add(UnlockControl);
GameEvents.onVesselStandardModification.Add(OnVesselStandardModification);

lastSettingsSaveTime = Time.time;

Expand Down Expand Up @@ -979,6 +980,7 @@ public void OnDestroy()
GameEvents.onShowUI.Remove(OnShowGUI);
GameEvents.onHideUI.Remove(OnHideGUI);
GameEvents.onVesselChange.Remove(UnlockControl);
GameEvents.onVesselStandardModification.Remove(OnVesselStandardModification);

if (weLockedInputs)
{
Expand All @@ -1004,6 +1006,35 @@ public void OnDestroy()
controlledVessel = null;
}

public void OnVesselStandardModification(Vessel v)
{
if (v != vessel)
return;

Profiler.BeginSample("vesselState");
vesselState.OnVesselStandardModification(vessel);
Profiler.EndSample();

Profiler.BeginSample("MechJebCore.OnVesselStandardModification");
if (this == vessel.GetMasterMechJeb())
{
foreach (ComputerModule module in GetComputerModules<ComputerModule>())
{
Profiler.BeginSample("OnVesselStandardModification: " + module.profilerName);
try
{
if (module.enabled) module.OnVesselStandardModification(vessel);
}
catch (Exception e)
{
Debug.LogError("MechJeb module " + module.GetType().Name + " threw an exception in OnVesselStandardModification: " + e);
}
Profiler.EndSample();
}
}
Profiler.EndSample();
}

private void OnFlyByWire(FlightCtrlState s)
{
if (deactivateControl || !CheckControlledVessel() || this != vessel.GetMasterMechJeb())
Expand Down
31 changes: 15 additions & 16 deletions MechJeb2/MechJebModuleLandingAutopilot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public Vector3d ComputeCourseCorrection(bool allowPrograde)
deltas[i] = landingDelta / perturbationDeltaV; //normalize by the delta-V considered, so that deltas now has units of meters per (meter/second) [i.e., seconds]
}

// Now deltas stores the predicted offsets in landing position produced by each of the three perturbations.
// Now deltas stores the predicted offsets in landing position produced by each of the three perturbations.
// We now figure out the offset we actually want

// First we compute the target landing position. We have to convert the latitude and longitude of the target
Expand All @@ -302,7 +302,7 @@ public Vector3d ComputeCourseCorrection(bool allowPrograde)
Vector3d downrangeDelta;
if (allowPrograde)
{
// Construct the linear combination of the prograde and radial+ perturbations
// Construct the linear combination of the prograde and radial+ perturbations
// that produces the largest effect on the landing position. The Math.Sign is to
// detect and handle the case where radial+ burns actually bring the landing sign closer
// (e.g. when we are traveling close to straight up)
Expand Down Expand Up @@ -374,9 +374,9 @@ void DeployParachutes()
{
if (vesselState.mainBody.atmosphere && deployChutes)
{
for (int i = 0; i < vesselState.parachutes.Count; i++)
for (int i = 0; i < vesselState.parachuteModulesList.Count; i++)
{
ModuleParachute p = vesselState.parachutes[i];
ModuleParachute p = vesselState.parachuteModulesList[i];
// what is the ASL at which we should deploy this parachute? It is the actual deployment height above the surface + the ASL of the predicted landing point.
double LandingSiteASL = LandingAltitude;
double ParachuteDeployAboveGroundAtLandingSite = p.deployAltitude * this.parachutePlan.Multiplier;
Expand All @@ -399,9 +399,9 @@ public bool ParachutesDeployable()
if (!vesselState.mainBody.atmosphere) return false;
if (!deployChutes) return false;

for (int i = 0; i < vesselState.parachutes.Count; i++)
for (int i = 0; i < vesselState.parachuteModulesList.Count; i++)
{
ModuleParachute p = vesselState.parachutes[i];
ModuleParachute p = vesselState.parachuteModulesList[i];
if (Math.Max(p.part.inverseStage,0) >= limitChutesStage && p.deploymentState == ModuleParachute.deploymentStates.STOWED)
{
return true;
Expand Down Expand Up @@ -475,7 +475,7 @@ public double DecelerationEndAltitude()
//ensure a safe touchdown speed. How do we tell if the atmosphere is thick enough? We check
//to see if there is an altitude within the atmosphere for which the characteristic distance
//over which drag slows the ship is smaller than the altitude above the terrain. If so, we can
//expect to get slowed to near terminal velocity before impacting the ground.
//expect to get slowed to near terminal velocity before impacting the ground.
public bool UseAtmosphereToBrake()
{
double landingSiteDragLength = mainBody.DragLength(LandingAltitude, vesselAverageDrag + ParachuteAddedDragCoef(), vesselState.mass);
Expand All @@ -495,14 +495,14 @@ public double VesselAverageDrag()
for (int i = 0; i < vessel.parts.Count; i++)
{
Part part = vessel.parts[i];
if (part.DragCubes.None || part.ShieldedFromAirstream)
if (part.DragCubes.None || part.ShieldedFromAirstream)
{
continue;
}
//DragCubeList.CubeData data = part.DragCubes.AddSurfaceDragDirection(Vector3.back, 1);
//
//dragCoef += data.areaDrag;

float partAreaDrag = 0;
for (int f = 0; f < 6; f++)
{
Expand All @@ -519,9 +519,9 @@ public double ParachuteAddedDragCoef()
double addedDragCoef = 0;
if (vesselState.mainBody.atmosphere && deployChutes)
{
for (int i = 0; i < vesselState.parachutes.Count; i++)
for (int i = 0; i < vesselState.parachuteModulesList.Count; i++)
{
ModuleParachute p = vesselState.parachutes[i];
ModuleParachute p = vesselState.parachuteModulesList[i];
if (p.part.inverseStage >= limitChutesStage)
{
//addedDragMass += p.part.DragCubes.Cubes.Where(c => c.Name == "DEPLOYED").m
Expand Down Expand Up @@ -744,7 +744,7 @@ public void AddResult(ReentrySimulation.Result newResult)
}
lastResult = newResult;
}

// What was the overshoot for this new result?
double overshoot = newResult.GetOvershoot(this.autoPilot.core.target.targetLatitude, this.autoPilot.core.target.targetLongitude);

Expand All @@ -770,7 +770,7 @@ public void AddResult(ReentrySimulation.Result newResult)
}
else
{
// How much data is there? If just one datapoint then we need to slightly vary the multplier to avoid doing exactly the same multiplier again and getting a divide by zero!. If there is just two then we will not update the multiplier as we can't conclude much from two points of data!
// How much data is there? If just one datapoint then we need to slightly vary the multplier to avoid doing exactly the same multiplier again and getting a divide by zero!. If there is just two then we will not update the multiplier as we can't conclude much from two points of data!
int dataSetSize = regression.dataSetSize;
if (dataSetSize == 1)
{
Expand Down Expand Up @@ -829,9 +829,9 @@ public void StartPlanning()
parachutePresent = false; // First assume that there are no parachutes.

// TODO should we check if each of these parachutes is withing the staging limit?
for (int i = 0; i < autoPilot.vesselState.parachutes.Count; i++)
for (int i = 0; i < autoPilot.vesselState.parachuteModulesList.Count; i++)
{
ModuleParachute p = autoPilot.vesselState.parachutes[i];
ModuleParachute p = autoPilot.vesselState.parachuteModulesList[i];
if (p.minAirPressureToOpen > minSemiDeployPressure)
// Although this is called "minSemiDeployPressure" we want to find the largest value for each of our parachutes. This can be used to calculate the corresponding height, and hence a height at which we can be guarenteed that all our parachutes will deploy if asked to.
{
Expand Down Expand Up @@ -977,4 +977,3 @@ public double CorrelationCoefficent
}
}
}

Loading

0 comments on commit a1deeb7

Please sign in to comment.