Auto-stage helper
This script will first activate stages until launch clamps are releases(if any).
Then it'll activate a stage in one of the following conditions:
- No engines are in "active" state.
- All the engines in "active" state have ran out of fuel.
// Import Kift.Client.dll, Kift.ThriftGen.dll, Thrift.dll
using System;
using System.Collections.Generic;
using System.Threading;
namespace Kift.Examples
{
public class AutoStage
{
private static List<Part> FindActiveEngines(List<Part> engines)
{
List<Part> result = new List<Part>();
foreach (Part p in engines)
{
if (p.Engine.Active)
{
result.Add(p);
}
}
return result;
}
public static void Main()
{
Client c = new Client("localhost", 9090);
KiftService.Client client = c.Connect();
Console.WriteLine("Server is connected!");
Console.WriteLine("Launch in");
for (int i = 5; i > 0; i--) // count down 5
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
Vessel v = client.currentVessel();
while (true) // fire stage until launch clamps are released
{
List<Part> launchClamps = client.getVesselPartsByType(v.Id, PartType.LaunchClamp);
if (launchClamps.Count != 0)
{
Console.WriteLine("Launch clamps are not released, stage!");
client.stage();
}
else
{
Console.WriteLine("Launch clamps are released!");
break;
}
Thread.Sleep(2000);
}
Console.WriteLine("Start monitoring engines!");
VesselObservor vo = new VesselObservor(v.Id, "localhost", 9090); // create a VesselObservor for current vessel
vo.WatchVessel(OVessel.Stage); // watch vessel's current stage
vo.SyncInterval = 500; // sync every 500ms
vo.StartStream(); // start syncing data
SnapshotResponse s = vo.Snapshot; // read data from "s"
while (true) // fire stage until vessel.stage == 0
{
Thread.Sleep(2000);
if (s.Vessel.Stage == 0) // check current stage, exit the loop if there're no more stages
{
break;
}
List<Part> engines = client.getVesselPartsByType(v.Id, PartType.Engine);
List<Part> activeEngines = FindActiveEngines(engines);
if (activeEngines.Count == 0)
{
Console.WriteLine("No active engines, stage!");
client.stage();
continue;
}
bool shouldStage = true;
foreach (Part p in activeEngines)
{
if (p.Engine.HasFuel) // one of the active engines still has fuel, do not stage
{
shouldStage = false;
break;
}
}
if (shouldStage)
{
Console.WriteLine("All active engines have ran out of fuel, stage!");
client.stage();
}
}
Console.WriteLine("All stages activated! Program exit!");
vo.Close();
c.Close();
}
}
}