Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
remove vessels without wrecking the indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Jun 13, 2015
1 parent 4e4b465 commit fe097a8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
Binary file modified GameData/Pilot Assistant/PilotAssistant.dll
Binary file not shown.
1 change: 1 addition & 0 deletions PilotAssistant/FlightModules/AsstVesselModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void OnDestroy()
{
vesselAsst.OnDestroy();
vesselSSAS.OnDestroy();
PilotAssistantFlightCore.Instance.removeVessel(this);
}

public bool isActiveVessel()
Expand Down
6 changes: 5 additions & 1 deletion PilotAssistant/FlightModules/SurfSAS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ public void SurfaceSAS(FlightCtrlState state)

void setCtrlState(SASList ID, double error, double rate, ref float ctrlState)
{
PIDmode mode = PIDmode.PID;
if (!controlledVessel.checkLanded() && controlledVessel.IsControllable)
mode = PIDmode.PD; // no integral when it can't do anything

if (allowControl(ID))
ctrlState = ID.GetSAS(this).ResponseF(error, rate, !controlledVessel.checkLanded() && controlledVessel.IsControllable);
ctrlState = ID.GetSAS(this).ResponseF(error, rate, mode);
else if (!Utils.hasInput(ID))
ctrlState = 0; // kill off stock SAS inputs
}
Expand Down
22 changes: 16 additions & 6 deletions PilotAssistant/PID/PIDErrorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ namespace PilotAssistant.PID
{
using Utility;
using FlightModules;

public enum PIDmode
{
PID,
PD,
D
}

public class PIDErrorController : SASController
{
public PIDErrorController(SASList ID, double Kp, double Ki, double Kd, double OutputMin, double OutputMax, double intClampLower, double intClampUpper, double scalar = 1, double easing = 1)
Expand All @@ -17,19 +25,21 @@ public PIDErrorController(SASList ID, double[] gains)
: base(ID, gains)
{ }

public virtual double ResponseD(double error, double rate, bool useIntegral)
public virtual double ResponseD(double error, double rate, PIDmode mode)
{
double res_d, res_i, res_p;
double res_d = 0, res_i = 0, res_p = 0;
res_d = derivativeError(rate);
res_i = integralError(error, useIntegral);
res_p = proportionalError(error);
if (mode == PIDmode.PID)
res_i = integralError(error, true);
if (mode == PIDmode.PD || mode == PIDmode.PID)
res_p = proportionalError(error);

return Utils.Clamp(res_p + res_i + res_d, OutMin, OutMax);
}

public virtual float ResponseF(double error, double rate, bool useIntegral)
public virtual float ResponseF(double error, double rate, PIDmode mode)
{
return (float)ResponseD(error, rate, useIntegral);
return (float)ResponseD(error, rate, mode);
}

protected override double derivativeError(double rate)
Expand Down
40 changes: 29 additions & 11 deletions PilotAssistant/PilotAssistantFlightCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,29 @@ IEnumerator clearUnloadedVessels()
{
while (HighLogic.LoadedSceneIsFlight)
{
for (int i = 0; i < 1000; i++)
for (int i = 0; i < 50; i++)
yield return null;
Vessel ves = controlledVessels[selectedVesselIndex].vesselRef;
controlledVessels.RemoveAll(v => !v.vesselRef.loaded);
controlledVessels.RemoveAll(v => v == null || v.vesselRef == null || !v.vesselRef.loaded);
selectedVesselIndex = controlledVessels.FindIndex(vm => vm.vesselRef == ves);
}
}

public void removeVessel(AsstVesselModule avm)
{
if (avm.vesselRef != controlledVessels[selectedVesselIndex].vesselRef)
{
Vessel ves = controlledVessels[selectedVesselIndex].vesselRef;
controlledVessels.Remove(avm);
selectedVesselIndex = controlledVessels.FindIndex(vm => vm.vesselRef == ves);
}
else
{
controlledVessels.Remove(avm);
selectedVesselIndex = 0;
}
}

public void LoadConfig()
{
try
Expand Down Expand Up @@ -151,16 +166,19 @@ public void Update()

void vesselSwitch(Vessel v)
{
if (v.IsControllable && !controlledVessels.Any(ves => ves.vesselRef == v))
{
AsstVesselModule newVesMod = new AsstVesselModule(v);
controlledVessels.Add(newVesMod);
newVesMod.Start();
selectedVesselIndex = controlledVessels.Count - 1;
}
else
if (v.IsControllable)
{
selectedVesselIndex = controlledVessels.FindIndex(vm => vm.vesselRef == v);
if (!controlledVessels.Any(ves => ves.vesselRef == v))
{
AsstVesselModule newVesMod = new AsstVesselModule(v);
controlledVessels.Add(newVesMod);
newVesMod.Start();
selectedVesselIndex = controlledVessels.Count - 1;
}
else
{
selectedVesselIndex = controlledVessels.FindIndex(vm => vm.vesselRef == v);
}
}
}

Expand Down

0 comments on commit fe097a8

Please sign in to comment.