Skip to content

Commit

Permalink
Merge pull request MuMech#1517 from DRVeyl/CachedDeployableHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
lamont-granquist authored Mar 18, 2022
2 parents 214d815 + 24bc1c6 commit f3e247e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 68 deletions.
8 changes: 8 additions & 0 deletions MechJeb2/ComputerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public virtual void Drive(FlightCtrlState s)
{
}

public virtual void OnVesselWasModified(Vessel v)
{
}

public virtual void OnVesselStandardModification(Vessel v)
{
}

public virtual void OnStart(PartModule.StartState state)
{
}
Expand Down
50 changes: 50 additions & 0 deletions MechJeb2/MechJebCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ public override void OnStart(PartModule.StartState state)
GameEvents.onShowUI.Add(OnShowGUI);
GameEvents.onHideUI.Add(OnHideGUI);
GameEvents.onVesselChange.Add(UnlockControl);
GameEvents.onVesselWasModified.Add(OnVesselWasModified);
GameEvents.onVesselStandardModification.Add(OnVesselStandardModification);

lastSettingsSaveTime = Time.time;

Expand Down Expand Up @@ -1046,6 +1048,8 @@ public void OnDestroy()
GameEvents.onShowUI.Remove(OnShowGUI);
GameEvents.onHideUI.Remove(OnHideGUI);
GameEvents.onVesselChange.Remove(UnlockControl);
GameEvents.onVesselWasModified.Remove(OnVesselWasModified);
GameEvents.onVesselStandardModification.Remove(OnVesselStandardModification);

if (weLockedInputs)
{
Expand Down Expand Up @@ -1242,6 +1246,52 @@ void UnlockControl()
weLockedInputs = false;
}

private void OnVesselWasModified(Vessel v)
{
if (v == vessel && this == vessel.GetMasterMechJeb())
{
string name = "MechJebCore.OnVesselWasModified";
Profiler.BeginSample(name);
foreach (ComputerModule module in GetComputerModules<ComputerModule>().Where(x => x.enabled))
{
Profiler.BeginSample(module.profilerName);
try
{
module.OnVesselWasModified(v);
}
catch (Exception e)
{
Debug.LogError($"MechJeb module {module.GetType().Name} threw an exception in {name}: {e}");
}
Profiler.EndSample();
}
Profiler.EndSample();
}
}

// Copy-pasta, but preferred over a Reflection-based approach (pass MethodInfos around)
private void OnVesselStandardModification(Vessel v)
{
if (v == vessel && this == vessel.GetMasterMechJeb())
{
string name = "MechJebCore.OnVesselStandardModification";
Profiler.BeginSample(name);
foreach (ComputerModule module in GetComputerModules<ComputerModule>().Where(x => x.enabled))
{
Profiler.BeginSample(module.profilerName);
try
{
module.OnVesselStandardModification(v);
}
catch (Exception e)
{
Debug.LogError($"MechJeb module {module.GetType().Name} threw an exception in {name}: {e}");
}
Profiler.EndSample();
}
Profiler.EndSample();
}
}

public static new void print(object message)
{
Expand Down
103 changes: 35 additions & 68 deletions MechJeb2/MechJebModuleDeployableController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,67 +25,40 @@ public MechJebModuleDeployableController(MechJebCore core) : base(core)
public bool prev_autoDeploy = true;

protected string type = "";


protected List<ModuleDeployablePart> cachedPartModules = new List<ModuleDeployablePart>(16);
protected void DiscoverDeployablePartModules()
{
cachedPartModules.Clear();
foreach (Part p in vessel.Parts)
foreach (PartModule pm in p.Modules)
if (pm != null && pm is ModuleDeployablePart mdp && isModules(mdp))
cachedPartModules.Add(mdp);
}

protected bool isDeployable(ModuleDeployablePart sa)
{
return (sa.Events["Extend"].active || sa.Events["Retract"].active);
}

public void ExtendAll()
{
List<Part> vp = vessel.parts;
for (int i = 0; i < vp.Count; i++)
{
Part p = vp[i];

if (p.ShieldedFromAirstream)
return;

for (int j = 0; j < p.Modules.Count; j++)
{
ModuleDeployablePart mdp = p.Modules[j] as ModuleDeployablePart;
if (mdp != null && isModules(mdp) && isDeployable(mdp))
{
mdp.Extend();
}
}
};
foreach (ModuleDeployablePart mdp in cachedPartModules)
if (mdp != null && isDeployable(mdp) && !mdp.part.ShieldedFromAirstream)
mdp.Extend();
}

public void RetractAll()
{
List<Part> vp = vessel.parts;
for (int i = 0; i < vp.Count; i++) {
Part p = vp[i];

if (p.ShieldedFromAirstream)
return;

for (int j = 0; j < p.Modules.Count; j++)
{
ModuleDeployablePart mdp = p.Modules[j] as ModuleDeployablePart;
if (mdp != null && isModules(mdp) && isDeployable(mdp))
{
mdp.Retract();
}
}
}
foreach (ModuleDeployablePart mdp in cachedPartModules)
if (mdp != null && isDeployable(mdp) && !mdp.part.ShieldedFromAirstream)
mdp.Retract();
}

public bool AllRetracted()
{
for (int i = 0; i < vessel.parts.Count; i++)
{
Part p = vessel.parts[i];
for (int j = 0; j < p.Modules.Count; j++)
{
ModuleDeployablePart mdp = p.Modules[j] as ModuleDeployablePart;
if (mdp != null && isModules(mdp) && isDeployable(mdp) && mdp.deployState != ModuleDeployablePart.DeployState.RETRACTED)
{
return false;
}
}
}
foreach (ModuleDeployablePart mdp in cachedPartModules)
if (mdp != null && isDeployable(mdp) && mdp.deployState != ModuleDeployablePart.DeployState.RETRACTED)
return false;
return true;
}

Expand Down Expand Up @@ -138,32 +111,19 @@ public override void OnFixedUpdate()
prev_autoDeploy = false;
}

bool allRetracted = AllRetracted();
if (allRetracted)
buttonText = getButtonText(DeployablePartState.RETRACTED);
else
buttonText = getButtonText(DeployablePartState.EXTENDED);
bool extendedThisPass = !AllRetracted();
if (extended != extendedThisPass)
buttonText = getButtonText(extendedThisPass ? DeployablePartState.EXTENDED : DeployablePartState.RETRACTED);

extended = !allRetracted;
extended = extendedThisPass;
}

protected bool ExtendingOrRetracting()
{
for (int i = 0; i < vessel.parts.Count; i++)
{
Part p = vessel.parts[i];
for (int j = 0; j < p.Modules.Count; j++)
{
ModuleDeployablePart mdp = p.Modules[j] as ModuleDeployablePart;

if (mdp != null && isModules(mdp) && isDeployable(mdp)
&& (mdp.deployState == ModuleDeployablePart.DeployState.EXTENDING
|| mdp.deployState == ModuleDeployablePart.DeployState.RETRACTING))
{
return true;
}
}
}
foreach (ModuleDeployablePart mdp in cachedPartModules)
if (mdp != null && isDeployable(mdp)
&& (mdp.deployState == ModuleDeployablePart.DeployState.EXTENDING || mdp.deployState == ModuleDeployablePart.DeployState.RETRACTING))
return true;
return false;
}

Expand All @@ -177,5 +137,12 @@ protected enum DeployablePartState
}

protected abstract string getButtonText(DeployablePartState deployablePartState);
public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);
if (HighLogic.LoadedSceneIsFlight)
DiscoverDeployablePartModules();
}
public override void OnVesselWasModified(Vessel v) => DiscoverDeployablePartModules();
}
}

0 comments on commit f3e247e

Please sign in to comment.