Advertisement
cheetah970

Radar helper class

Aug 13th, 2017
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.01 KB | None | 0 0
  1. sealed class AdvAntenna
  2. {
  3.     public IMyRadioAntenna Antenna { get; private set; }
  4.     public int DatagramsStored => Antenna.GetValue<int>("DatagramsStored");
  5.     public bool AllowReceive
  6.     {
  7.         get { return Antenna.GetValueBool("AllowReceive"); }
  8.         set { Antenna.SetValueBool("AllowReceive", value); }
  9.     }
  10.  
  11.     public AdvAntenna(IMyRadioAntenna Antenna)
  12.     {
  13.         this.Antenna = Antenna;
  14.     }
  15.  
  16.     public void Broadcast(string Datagram)
  17.     {
  18.         var Broadcast = Antenna.GetValue<Action<string>>("Broadcast");
  19.         Broadcast(Datagram);
  20.     }
  21.  
  22.     public void Send(string Datagram, string Protocol)
  23.     {
  24.         var Send = Antenna.GetValue<Action<string, string>>("Send");
  25.         Send(Datagram, Protocol);
  26.     }
  27.  
  28.     public void AllowProtocol(string Protocol)
  29.     {
  30.         var AllowProtocol = Antenna.GetValue<Action<string>>("AllowProtocol");
  31.         AllowProtocol(Protocol);
  32.     }
  33.  
  34.     public void DisallowProtocol(string Protocol)
  35.     {
  36.         var DisallowProtocol = Antenna.GetValue<Action<string>>("DisallowProtocol");
  37.         DisallowProtocol(Protocol);
  38.     }
  39.  
  40.     public void ClearProtocols()
  41.     {
  42.         var ClearProtocols = Antenna.GetValue<Action>("ClearProtocolList");
  43.         ClearProtocols();
  44.     }
  45.  
  46.     public string ReadFirstDatagram()
  47.     {
  48.         var ReadFirstDatagram = Antenna.GetValue<Func<string>>("ReadFirstDatagram");
  49.         return ReadFirstDatagram();
  50.     }
  51.  
  52.     public string ReadFirstDatagram(string Protocol)
  53.     {
  54.         var ReadFirstDatagram = Antenna.GetValue<Func<string, string>>("ReadFirstDatagramByProtocol");
  55.         return ReadFirstDatagram(Protocol);
  56.     }
  57.  
  58.     public void ClearDatagrams()
  59.     {
  60.         var ClearDatagramStorage = Antenna.GetValue<Action>("ClearDatagramStorage");
  61.         ClearDatagramStorage();
  62.     }
  63.  
  64.     public bool AnyDatagrams(string Protocol)
  65.     {
  66.         var AnyDatagrams = Antenna.GetValue<Func<string, bool>>("AnyDatagrams");
  67.         return AnyDatagrams(Protocol);
  68.     }
  69.  
  70.     public bool AnyDatagrams()
  71.     {
  72.         return DatagramsStored > 0;
  73.     }
  74. }
  75.  
  76. sealed class AdvRemote
  77. {
  78.     public IMyRemoteControl RCBlock { get; private set; }
  79.     public bool Enabled
  80.     {
  81.         get { return RCBlock.IsAutoPilotEnabled; }
  82.         set { RCBlock.SetAutoPilotEnabled(value); }
  83.     }
  84.     public bool CollisionAvoidance
  85.     {
  86.         get { return RCBlock.GetValueBool("CollisionAvoidance"); }
  87.         set { RCBlock.SetValueBool("CollisionAvoidance", value); }
  88.     }
  89.     public float SpeedLimit
  90.     {
  91.         get { return RCBlock.GetValueFloat("SpeedLimit"); }
  92.         set { RCBlock.SetValueFloat("SpeedLimit", value); }
  93.     }
  94.  
  95.     /// <summary>
  96.     /// Note that this acquires the list of waypoints for Advanced Autopilot.
  97.     /// </summary>
  98.     public List<Vector3D> Waypoints => RCBlock.GetValue<List<Vector3D>>("AdvAutopilotListWP");
  99.  
  100.     public enum FlightModes : int
  101.     {
  102.         OneWay = 2,
  103.         Circle = 1,
  104.         Patrol = 0
  105.     }
  106.  
  107.     public FlightModes FlightMode
  108.     {
  109.         get { return (FlightModes)RCBlock.GetValue<long>("FlightMode"); }
  110.         set { RCBlock.SetValue<long>("FlightMode", (int)value); }
  111.     }
  112.  
  113.     public AdvRemote(IMyRemoteControl RCBlock)
  114.     {
  115.         this.RCBlock = RCBlock;
  116.     }
  117.  
  118.     /// <summary>
  119.     /// Adds waypoint for the Advanced Autopilot.
  120.     /// </summary>
  121.     public void AddWaypoint(Vector3D Waypoint)
  122.     {
  123.         var AddWP = RCBlock.GetValue<Action<Vector3D>>("AdvAutopilotAddWP");
  124.         AddWP(Waypoint);
  125.     }
  126.  
  127.     /// <summary>
  128.     /// Attention! If you aren't going to add a new waypoint immediately after clearing, disable the autopilot.
  129.     /// <para/>
  130.     /// Otherwise, the ship may start spinning uncontrollably due to a bug in the game itself.
  131.     /// </summary>
  132.     public void ClearWaypoints(bool DisableAutopilot = true)
  133.     {
  134.         if (DisableAutopilot) Enabled = false;
  135.         var ClearWP = RCBlock.GetValue<Action>("AdvAutopilotClearWP");
  136.         ClearWP();
  137.     }
  138.  
  139.     public void Enable() { Enabled = true; }
  140.     public void Disable() { Enabled = false; }
  141. }
  142.  
  143. sealed class AdvTurret
  144. {
  145.     public IMyLargeTurretBase TurretBlock { get; private set; }
  146.     public bool Enabled
  147.     {
  148.         get { return TurretBlock.Enabled; }
  149.         set { TurretBlock.Enabled = value; }
  150.     }
  151.  
  152.     public AdvTurret(IMyLargeTurretBase TurretBlock)
  153.     {
  154.         this.TurretBlock = TurretBlock;
  155.     }
  156.  
  157.     public MyDetectedEntityInfo Target => TurretBlock.GetValue<MyDetectedEntityInfo>("CurrentTarget");
  158.  
  159.     public bool TrackTarget(MyDetectedEntityInfo Target, long SubtargetEntityID)
  160.     {
  161.         var Track = TurretBlock.GetValue<Func<MyDetectedEntityInfo, long, bool>>("TrackSubtarget");
  162.         return Track(Target, SubtargetEntityID);
  163.     }
  164.  
  165.     public bool TrackTarget(MyDetectedEntityInfo Target)
  166.     {
  167.         var Track = TurretBlock.GetValue<Func<MyDetectedEntityInfo, bool>>("TrackTarget");
  168.         return Track(Target);
  169.     }
  170. }
  171.  
  172. sealed class IMyRadar
  173. {
  174.     public IMyUpgradeModule RadarBlock { get; private set; }
  175.     public Vector3D Position { get; private set; }
  176.     public HashSet<MyDetectedEntityInfo> RadarData => RadarBlock.GetValue<HashSet<MyDetectedEntityInfo>>("RadarData");
  177.     public bool Enabled
  178.     {
  179.         get { return RadarBlock.Enabled; }
  180.         set { RadarBlock.Enabled = value; }
  181.     }
  182.     public int RadarPower
  183.     {
  184.         get { return (int)RadarBlock.GetValueFloat("RadarPower"); }
  185.         set { RadarBlock.SetValueFloat("RadarPower", value); }
  186.     }
  187.     public int MaxRadarPower => (int)RadarBlock.GetProperty("RadarPower").AsFloat().GetMaximum(RadarBlock);
  188.     public bool ActiveMode
  189.     {
  190.         get { return RadarBlock.GetValueBool("ActiveMode"); }
  191.         set { RadarBlock.SetValueBool("ActiveMode", value); }
  192.     }
  193.  
  194.     public IMyRadar(IMyTerminalBlock RadarBlock)
  195.     {
  196.         this.RadarBlock = RadarBlock as IMyUpgradeModule;
  197.         if (this.RadarBlock == null) throw new ArgumentNullException("Radar(): Block is not valid");
  198.         if (this.RadarBlock.GetValue<HashSet<MyDetectedEntityInfo>>("RadarData") == null) throw new ArgumentException("Radar(): supplied block isn't a Radar");
  199.     }
  200.  
  201.     public bool CanScan(MyDetectedEntityInfo Target)
  202.     {
  203.         var CanScanFunc = RadarBlock.GetValue<Func<MyDetectedEntityInfo, bool>>("CanScan");
  204.         if (CanScanFunc == null) return false;
  205.         return CanScanFunc(Target);
  206.     }
  207.  
  208.     public List<TargetBlock> ScanTarget(MyDetectedEntityInfo Target)
  209.     {
  210.         var SerializedData = RadarBlock.GetValue<Func<MyDetectedEntityInfo, List<Dictionary<string, string>>>>("ScanTarget")(Target);
  211.         if (SerializedData == null) return null;
  212.         List<TargetBlock> retval = new List<TargetBlock>();
  213.         foreach (var Data in SerializedData) retval.Add(new TargetBlock(Data));
  214.         return retval;
  215.     }
  216.  
  217.     public class TargetBlock
  218.     {
  219.         public readonly string TypeID;
  220.         public readonly string SubtypeID;
  221.         public readonly long EntityID;
  222.         public readonly long GridID;
  223.         public readonly bool? Enabled;
  224.         public readonly bool Functional;
  225.         public readonly Vector3D WorldPosition;
  226.         public readonly long OwnerID;
  227.         public readonly bool Accessible;
  228.  
  229.         public TargetBlock(Dictionary<string, string> SerializedData)
  230.         {
  231.             TypeID = SerializedData["Type"];
  232.             SubtypeID = SerializedData["Subtype"];
  233.             EntityID = long.Parse(SerializedData["EntityID"]);
  234.             GridID = long.Parse(SerializedData["Grid"]);
  235.             string enabled = SerializedData["Enabled"];
  236.             if (enabled == "True") Enabled = true;
  237.             if (enabled == "False") Enabled = false;
  238.             if (enabled == "null") Enabled = null;
  239.             Functional = bool.Parse(SerializedData["Functional"]);
  240.             Vector3D.TryParse(SerializedData["WorldPosition"], out WorldPosition);
  241.             OwnerID = long.Parse(SerializedData["OwnerID"]);
  242.             Accessible = bool.Parse(SerializedData["Accessible"]);
  243.         }
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement