Skip to content

Commit

Permalink
Merge pull request #274 from Peppie23/flightcomputer_persist
Browse files Browse the repository at this point in the history
Save/Restore Flightcomputer plan
  • Loading branch information
Peppie84 committed Jan 6, 2015
2 parents 370f1f0 + eb76fa1 commit 06d62c7
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 61 deletions.
70 changes: 70 additions & 0 deletions src/RemoteTech/FlightComputer/Commands/AbstractCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,75 @@ public int CompareTo(ICommand dc)
{
return TimeStamp.CompareTo(dc.TimeStamp);
}

/// <summary>
/// Save the basic informations for every command.
/// </summary>
/// <param name="n">Node to save in</param>
/// <param name="fc">Current flightcomputer</param>
public virtual void Save(ConfigNode n, FlightComputer fc)
{
ConfigNode save = new ConfigNode(this.GetType().Name);
try
{
// try to serialize 'this'
ConfigNode.CreateConfigFromObject(this, 0, save);
}
catch (Exception) {}

save.AddValue("TimeStamp", TimeStamp);
save.AddValue("ExtraDelay", ExtraDelay);
n.AddNode(save);
}

/// <summary>
/// Load the basic informations for every command.
/// </summary>
/// <param name="n">Node with the command infos</param>
/// <param name="fc">Current flightcomputer</param>
public virtual void Load(ConfigNode n, FlightComputer fc)
{
// nothing
if (n.HasValue("TimeStamp"))
{
TimeStamp = double.Parse(n.GetValue("TimeStamp"));
}
if (n.HasValue("ExtraDelay"))
{
ExtraDelay = double.Parse(n.GetValue("ExtraDelay"));
}
}

/// <summary>
/// Load and creates a command after saving a command. Returns null if no object
/// has been loaded.
/// </summary>
/// <param name="n">Node with the command infos</param>
/// <param name="fc">Current flightcomputer</param>
public static ICommand LoadCommand(ConfigNode n, FlightComputer fc)
{
ICommand command = null;

// switch the different commands
switch (n.name)
{
case "AttitudeCommand": { command = new AttitudeCommand(); break; }
case "ActionGroupCommand": { command = new ActionGroupCommand(); break; }
case "BurnCommand": { command = new BurnCommand(); break; }
case "ManeuverCommand": { command = new ManeuverCommand(); break; }
case "CancelCommand": { command = new CancelCommand(); break; }
case "TargetCommand": { command = new TargetCommand(); break; }
case "EventCommand": { command = new EventCommand(); break; }
}

if (command != null)
{
ConfigNode.LoadObjectFromConfig(command, n);
// additional loadings
command.Load(n, fc);
}

return command;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace RemoteTech
{
public class ActionGroupCommand : AbstractCommand
{
public KSPActionGroup ActionGroup { get; private set; }
[Persistent] public KSPActionGroup ActionGroup;

public override string Description
{
Expand Down
11 changes: 6 additions & 5 deletions src/RemoteTech/FlightComputer/Commands/AttitudeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ public class AttitudeCommand : AbstractCommand
{ ReferenceFrame.World, "World" },
};

public FlightMode Mode { get; set; }
public FlightAttitude Attitude { get; set; }
public ReferenceFrame Frame { get; set; }
public Quaternion Orientation { get; set; }
public float Altitude { get; set; }
[Persistent] public FlightMode Mode;
[Persistent] public FlightAttitude Attitude;
[Persistent] public ReferenceFrame Frame;
[Persistent] public Quaternion Orientation;
[Persistent] public float Altitude;

public override int Priority { get { return 0; } }

public override string Description
Expand Down
7 changes: 4 additions & 3 deletions src/RemoteTech/FlightComputer/Commands/BurnCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ namespace RemoteTech
{
public class BurnCommand : AbstractCommand
{
public float Throttle { get; set; }
public double Duration { get; set; }
public double DeltaV { get; set; }
[Persistent] public float Throttle;
[Persistent] public double Duration;
[Persistent] public double DeltaV;

public override int Priority { get { return 2; } }

public override String Description
Expand Down
22 changes: 21 additions & 1 deletion src/RemoteTech/FlightComputer/Commands/CancelCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace RemoteTech
public class CancelCommand : AbstractCommand
{
public override double ExtraDelay { get { return base.ExtraDelay; } set { return; } }
public ICommand Command { get; set; }
public ICommand Command;
[Persistent] public int queueIndex;

public override string Description { get { return "Cancelling a command." + Environment.NewLine + base.Description; } }
public override string ShortName { get { return "Cancel command"; } }
Expand All @@ -28,6 +29,7 @@ public override bool Pop(FlightComputer f)

public static CancelCommand WithCommand(ICommand cmd)
{

return new CancelCommand()
{
Command = cmd,
Expand All @@ -43,5 +45,23 @@ public static CancelCommand ResetActive()
TimeStamp = RTUtil.GameTime,
};
}

/// <summary>
/// Load the saved CancelCommand and find the element to cancel, based on the saved queue position
/// </summary>
public override void Load(ConfigNode n, FlightComputer fc)
{
base.Load(n, fc);
Command = fc.QueuedCommands.ElementAt(queueIndex);
}

/// <summary>
/// Saves the queue index for this command to the persist
/// </summary>
public override void Save(ConfigNode n, FlightComputer fc)
{
queueIndex = fc.QueuedCommands.ToList().IndexOf(Command);
base.Save(n, fc);
}
}
}
3 changes: 3 additions & 0 deletions src/RemoteTech/FlightComputer/Commands/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ public interface ICommand : IComparable<ICommand>
bool Pop(FlightComputer f);
bool Execute(FlightComputer f, FlightCtrlState fcs);
void Abort();
///
void Save(ConfigNode n, FlightComputer fc);
void Load(ConfigNode n, FlightComputer fc);
}
}
43 changes: 28 additions & 15 deletions src/RemoteTech/FlightComputer/Commands/ManeuverCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace RemoteTech
{
public class ManeuverCommand : AbstractCommand
{
public ManeuverNode Node { get; set; }
public double OriginalDelta { get; set; }
public double RemainingTime { get; set; }
public double RemainingDelta { get; set; }
// Index id of this maneuver node from patchedConicSolver.maneuverNodes list
[Persistent] public int NodeIndex;
public double OriginalDelta;
public double RemainingTime;
public double RemainingDelta;
public ManeuverNode Node;
public bool EngineActivated { get; set; }
public override int Priority { get { return 0; } }

Expand Down Expand Up @@ -93,9 +95,10 @@ public double getMaxBurnTime(FlightComputer f)
return Node.DeltaV.magnitude / (FlightCore.GetTotalThrust(f.Vessel) / f.Vessel.GetTotalMass());
}

public static ManeuverCommand WithNode(ManeuverNode node, FlightComputer f)
public static ManeuverCommand WithNode(int nodeIndex, FlightComputer f)
{
double thrust = FlightCore.GetTotalThrust(f.Vessel);
ManeuverNode node = f.Vessel.patchedConicSolver.maneuverNodes[nodeIndex];
double advance = f.Delay;

if (thrust > 0) {
Expand All @@ -104,19 +107,29 @@ public static ManeuverCommand WithNode(ManeuverNode node, FlightComputer f)

var newNode = new ManeuverCommand()
{
Node = new ManeuverNode()
{
DeltaV = node.DeltaV,
patch = node.patch,
solver = node.solver,
scaledSpaceTarget = node.scaledSpaceTarget,
nextPatch = node.nextPatch,
UT = node.UT,
nodeRotation = node.nodeRotation,
},
Node = node,
NodeIndex = nodeIndex,
TimeStamp = node.UT - advance,
};
return newNode;
}

/// <summary>
/// Find the maneuver node by the saved node id (index id of the meneuver list)
/// </summary>
/// <param name="n">Node with the command infos</param>
/// <param name="fc">Current flightcomputer</param>
public override void Load(ConfigNode n, FlightComputer fc)
{
base.Load(n,fc);
if(n.HasValue("NodeIndex"))
{
int nodeIndex = int.Parse(n.GetValue("NodeIndex"));
RTLog.Notify("Trying to get Maneuver {0}",nodeIndex);
// Set the ManeuverNode into this command
Node = fc.Vessel.patchedConicSolver.maneuverNodes[nodeIndex];
RTLog.Notify("Found Maneuver {0} with {1} dV", nodeIndex, Node.DeltaV);
}
}
}
}
77 changes: 76 additions & 1 deletion src/RemoteTech/FlightComputer/Commands/TargetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace RemoteTech
{
public class TargetCommand : AbstractCommand
{
/// Defines which target we have. Can be CelestialBody or Vessel
[Persistent] public String TargetType;
/// Target identifier, CelestialBody=Body-id or Vessel=GUID. Depends on TargetType
[Persistent] public String TargetId;

public override double ExtraDelay { get { return 0.0; } set { return; } }
public ITargetable Target { get; set; }
public override int Priority { get { return 1; } }
Expand All @@ -23,10 +28,17 @@ public override String Description
public override bool Pop(FlightComputer f)
{
f.DelayedTarget = Target;
f.lastTarget = this;
// Switch the vessels target
FlightGlobals.fetch.SetVesselTarget(Target);

return true;
}

public override bool Execute(FlightComputer f, FlightCtrlState fcs) { return false; }
public override bool Execute(FlightComputer f, FlightCtrlState fcs) {

return false;
}

public static TargetCommand WithTarget(ITargetable target)
{
Expand All @@ -36,5 +48,68 @@ public static TargetCommand WithTarget(ITargetable target)
TimeStamp = RTUtil.GameTime,
};
}

/// <summary>
/// Load the saved TargetCommand. Open a new ITargetable object by
/// the objects id.
/// </summary>
/// <param name="n">Node with the command infos</param>
/// <param name="fc">Current flightcomputer</param>
public override void Load(ConfigNode n, FlightComputer fc)
{
base.Load(n, fc);

switch (TargetType)
{
case "Vessel":
{
Guid Vesselid = new Guid(TargetId);
Target = FlightGlobals.Vessels.Where(v => v.id == Vesselid).FirstOrDefault();
break;
}
case "CelestialBody":
{
Target = FlightGlobals.Bodies.ElementAt(int.Parse(TargetId));
break;
}
default:
{
Target = null;
break;
}
}
}

/// <summary>
/// Save the TargetCommand. By targeting a vessel we'll save the guid,
/// by a CelestialBody we save the bodys id.
/// </summary>
/// <param name="n">Node to save in</param>
/// <param name="fc">Current flightcomputer</param>
public override void Save(ConfigNode n, FlightComputer fc)
{
if (Target != null)
TargetType = Target.GetType().ToString();

switch (TargetType)
{
case "Vessel":
{
TargetId = ((Vessel)Target).id.ToString();
break;
}
case "CelestialBody":
{
TargetId = FlightGlobals.Bodies.ToList().IndexOf(((CelestialBody)Target)).ToString();
break;
}
default:
{
TargetId = null;
break;
}
}
base.Save(n, fc);
}
}
}
34 changes: 31 additions & 3 deletions src/RemoteTech/FlightComputer/EventCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ namespace RemoteTech
{
public class EventCommand : AbstractCommand
{
public BaseEvent BaseEvent { get; private set; }
[Persistent] public string GUIName;
[Persistent] public int PartId;
[Persistent] public string Module;

public BaseEvent BaseEvent = null;

public override String Description
{
get
{
return BaseEvent.listParent.part.partInfo.title + ": " +
BaseEvent.GUIName + Environment.NewLine + base.Description;
return BaseEvent.listParent.part.partInfo.title + ": " + BaseEvent.GUIName +
Environment.NewLine + base.Description;
}
}
public override string ShortName
Expand All @@ -28,6 +32,7 @@ public override string ShortName
public override bool Pop(FlightComputer f)
{
BaseEvent.Invoke();

return false;
}

Expand All @@ -36,8 +41,31 @@ public static EventCommand Event(BaseEvent ev)
return new EventCommand()
{
BaseEvent = ev,
GUIName = ev.GUIName,
PartId = FlightGlobals.ActiveVessel.parts.ToList().IndexOf(ev.listParent.part),
Module = ev.listParent.module.ClassName.ToString(),
TimeStamp = RTUtil.GameTime,
};
}

/// <summary>
/// Load infos into this object and create a new BaseEvent
/// </summary>
public override void Load(ConfigNode n, FlightComputer fc)
{
base.Load(n, fc);

PartId = int.Parse(n.GetValue("PartId"));
Module = n.GetValue("Module");
GUIName = n.GetValue("GUIName");

Part part = FlightGlobals.ActiveVessel.parts[PartId];
PartModule partmodule = part.Modules[Module];
BaseEventList eventlist = new BaseEventList(part, partmodule);
if (eventlist.Count > 0)
{
BaseEvent = eventlist.Where(ba => ba.GUIName == GUIName).FirstOrDefault();
}
}
}
}
Loading

0 comments on commit 06d62c7

Please sign in to comment.