cheetah970

Torpedo Receiver

Jun 11th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. IMyGridTerminalSystem Term;
  2. JComms Comms;
  3. JComms.JMessage MyMessage;
  4.  
  5. public Program()
  6. {
  7.     Term = GridTerminalSystem;
  8.     Comms = new JComms("Missile", (IMyRadioAntenna)Term.GetBlockWithName("Торпеда"));
  9.     Comms.Channel = 16;
  10.     Comms.Callback = Term.GetBlockWithName("Сброс бомбы");
  11. }
  12.  
  13. public void Main()
  14. {
  15.     if (Comms.HasMessages)
  16.     {
  17.         MyMessage = Comms.Receive();
  18.     }
  19.    
  20.     if (MyMessage != null)
  21.     {
  22.         Echo(MyMessage.Text);
  23.         string[] Data = MyMessage.Text.Split(';');
  24.        
  25.         if (Data[0] == "missile")
  26.         {
  27.             Echo("Sending missile...");
  28.             Vector3D Target = new Vector3D(Double.Parse(Data[1]), Double.Parse(Data[2]), Double.Parse(Data[3]));
  29.             PushTargetToCMS2(Target, "CMD_TARGET_1");
  30.         }
  31.     }
  32. }
  33.  
  34. public string PrintableVector(Vector3D myVector, string Separator = "\n", int RoundTo = 1, bool NoDesignations = false)
  35. {
  36.     if (!NoDesignations)
  37.     {
  38.         return "X: " + Math.Round(myVector.GetDim(0), RoundTo) + Separator +
  39.         "Y: " + Math.Round(myVector.GetDim(1), RoundTo) + Separator +
  40.         "Z: " + Math.Round(myVector.GetDim(2), RoundTo);
  41.     }
  42.     else
  43.     {
  44.         return Math.Round(myVector.GetDim(0), RoundTo) + Separator + Math.Round(myVector.GetDim(1), RoundTo) + Separator + Math.Round(myVector.GetDim(2), RoundTo);
  45.     }
  46. }
  47.  
  48. void PushTargetToCMS2(Vector3D EnemyLoc, string PanelName)
  49. {   // Pushes target coords to Cruise Missile Script Mk2.
  50.     IMyTextPanel screen = ((IMyTextPanel)GridTerminalSystem.GetBlockWithName(PanelName));
  51.     screen.WritePublicTitle("[T:" + EnemyLoc.GetDim(0).ToString() + ":" +
  52.     EnemyLoc.GetDim(1).ToString() + ":" +
  53.     EnemyLoc.GetDim(2).ToString() + "]");
  54. }
  55.  
  56. class JComms
  57. {
  58.     public class JMessage
  59.     {
  60.         public string Sender;
  61.         public string Address;
  62.         public string Text;
  63.         public int Channel;
  64.     }
  65.    
  66.     public int Channel
  67.     {
  68.         get{ return CommAntenna.GetValue<int>("Channel"); }
  69.         set{ CommAntenna.SetValue<int>("Channel", value); }
  70.     }
  71.     string GridName;
  72.     IMyRadioAntenna CommAntenna;
  73.     string EncryptKey;
  74.     public bool HasMessages
  75.     {
  76.         get{
  77.             if (GetMessageCount() > 0)
  78.                 return true;
  79.             return false;
  80.         }
  81.         private set{}
  82.     }
  83.    
  84.     public IMyTerminalBlock Callback
  85.     {
  86.         get{ return CommAntenna.GetValue<IMyTerminalBlock>("Callback"); }
  87.         set{ CommAntenna.SetValue<IMyTerminalBlock>("Callback", value); }
  88.     }
  89.  
  90.     public void Send(string sndMessage, string sndAddress = "bcast")
  91.     {
  92.         if (EncryptKey == "")
  93.             CommAntenna.SetValue<string>("Message", String.Format("[<:txt|{0}|{1}|{2}:>]", GridName, sndAddress, sndMessage));
  94.  
  95.         if (EncryptKey != "")
  96.             CommAntenna.SetValue<string>("Message",
  97.                 String.Format("[<:enc|{0}|{1}|{2}:>]", Encrypt(GridName, EncryptKey), Encrypt(sndAddress, EncryptKey), Encrypt(sndMessage, EncryptKey)));
  98.  
  99.         CommAntenna.ApplyAction("Send");
  100.     }
  101.  
  102.     public JMessage Receive()
  103.     {
  104.         if (CommAntenna.GetValue<int>("IncomingCount") == 0)
  105.             return null;
  106.  
  107.         string RawMessage = CommAntenna.GetValue<string>("ReadNextIncoming");
  108.  
  109.         if (!RawMessage.Contains("[<:") || !RawMessage.Contains(":>]"))
  110.             return null;
  111.  
  112.         RawMessage = RawMessage.Replace("[<:", "");
  113.         RawMessage = RawMessage.Replace(":>]", "");
  114.         string[] Fields = RawMessage.Split('|');
  115.  
  116.         if (Fields[0] == "enc")
  117.         {
  118.             Fields[1] = Decrypt(Fields[1], EncryptKey);
  119.             Fields[2] = Decrypt(Fields[2], EncryptKey);
  120.             Fields[3] = Decrypt(Fields[3], EncryptKey);
  121.         }
  122.  
  123.         if (Fields[2] == GridName || Fields[2] == "bcast")
  124.         {
  125.             JMessage _return = new JMessage();
  126.             _return.Sender = Fields[1];
  127.             _return.Address = Fields[2];
  128.             _return.Text = Fields[3];
  129.             _return.Channel = CommAntenna.GetValue<int>("Channel");
  130.             return _return;
  131.         }
  132.         return null;
  133.     }
  134.  
  135.     public int GetMessageCount()
  136.     {
  137.         return CommAntenna.GetValue<int>("IncomingCount");
  138.     }
  139.  
  140.     public string Encrypt(string text, string key)
  141.     {
  142.         var result = new StringBuilder();
  143.  
  144.         for (int c = 0; c < text.Length; c++)
  145.             result.Append((char)((uint)text[c] ^ (uint)key[c % key.Length]));
  146.  
  147.         return result.ToString();
  148.     }
  149.  
  150.     public string Decrypt(string text, string key)
  151.     {
  152.         return Encrypt(text, key);
  153.     }
  154.  
  155.     public JComms(string MyGridName, IMyRadioAntenna MyAntenna, string MyEncryptKey = "")
  156.     {
  157.         GridName = MyGridName;
  158.         CommAntenna = MyAntenna;
  159.         EncryptKey = MyEncryptKey;
  160.     }
  161. }
Add Comment
Please, Sign In to add comment