Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IMyGridTerminalSystem Term;
- JComms Comms;
- JComms.JMessage MyMessage;
- public Program()
- {
- Term = GridTerminalSystem;
- Comms = new JComms("Missile", (IMyRadioAntenna)Term.GetBlockWithName("Торпеда"));
- Comms.Channel = 16;
- Comms.Callback = Term.GetBlockWithName("Сброс бомбы");
- }
- public void Main()
- {
- if (Comms.HasMessages)
- {
- MyMessage = Comms.Receive();
- }
- if (MyMessage != null)
- {
- Echo(MyMessage.Text);
- string[] Data = MyMessage.Text.Split(';');
- if (Data[0] == "missile")
- {
- Echo("Sending missile...");
- Vector3D Target = new Vector3D(Double.Parse(Data[1]), Double.Parse(Data[2]), Double.Parse(Data[3]));
- PushTargetToCMS2(Target, "CMD_TARGET_1");
- }
- }
- }
- public string PrintableVector(Vector3D myVector, string Separator = "\n", int RoundTo = 1, bool NoDesignations = false)
- {
- if (!NoDesignations)
- {
- return "X: " + Math.Round(myVector.GetDim(0), RoundTo) + Separator +
- "Y: " + Math.Round(myVector.GetDim(1), RoundTo) + Separator +
- "Z: " + Math.Round(myVector.GetDim(2), RoundTo);
- }
- else
- {
- return Math.Round(myVector.GetDim(0), RoundTo) + Separator + Math.Round(myVector.GetDim(1), RoundTo) + Separator + Math.Round(myVector.GetDim(2), RoundTo);
- }
- }
- void PushTargetToCMS2(Vector3D EnemyLoc, string PanelName)
- { // Pushes target coords to Cruise Missile Script Mk2.
- IMyTextPanel screen = ((IMyTextPanel)GridTerminalSystem.GetBlockWithName(PanelName));
- screen.WritePublicTitle("[T:" + EnemyLoc.GetDim(0).ToString() + ":" +
- EnemyLoc.GetDim(1).ToString() + ":" +
- EnemyLoc.GetDim(2).ToString() + "]");
- }
- class JComms
- {
- public class JMessage
- {
- public string Sender;
- public string Address;
- public string Text;
- public int Channel;
- }
- public int Channel
- {
- get{ return CommAntenna.GetValue<int>("Channel"); }
- set{ CommAntenna.SetValue<int>("Channel", value); }
- }
- string GridName;
- IMyRadioAntenna CommAntenna;
- string EncryptKey;
- public bool HasMessages
- {
- get{
- if (GetMessageCount() > 0)
- return true;
- return false;
- }
- private set{}
- }
- public IMyTerminalBlock Callback
- {
- get{ return CommAntenna.GetValue<IMyTerminalBlock>("Callback"); }
- set{ CommAntenna.SetValue<IMyTerminalBlock>("Callback", value); }
- }
- public void Send(string sndMessage, string sndAddress = "bcast")
- {
- if (EncryptKey == "")
- CommAntenna.SetValue<string>("Message", String.Format("[<:txt|{0}|{1}|{2}:>]", GridName, sndAddress, sndMessage));
- if (EncryptKey != "")
- CommAntenna.SetValue<string>("Message",
- String.Format("[<:enc|{0}|{1}|{2}:>]", Encrypt(GridName, EncryptKey), Encrypt(sndAddress, EncryptKey), Encrypt(sndMessage, EncryptKey)));
- CommAntenna.ApplyAction("Send");
- }
- public JMessage Receive()
- {
- if (CommAntenna.GetValue<int>("IncomingCount") == 0)
- return null;
- string RawMessage = CommAntenna.GetValue<string>("ReadNextIncoming");
- if (!RawMessage.Contains("[<:") || !RawMessage.Contains(":>]"))
- return null;
- RawMessage = RawMessage.Replace("[<:", "");
- RawMessage = RawMessage.Replace(":>]", "");
- string[] Fields = RawMessage.Split('|');
- if (Fields[0] == "enc")
- {
- Fields[1] = Decrypt(Fields[1], EncryptKey);
- Fields[2] = Decrypt(Fields[2], EncryptKey);
- Fields[3] = Decrypt(Fields[3], EncryptKey);
- }
- if (Fields[2] == GridName || Fields[2] == "bcast")
- {
- JMessage _return = new JMessage();
- _return.Sender = Fields[1];
- _return.Address = Fields[2];
- _return.Text = Fields[3];
- _return.Channel = CommAntenna.GetValue<int>("Channel");
- return _return;
- }
- return null;
- }
- public int GetMessageCount()
- {
- return CommAntenna.GetValue<int>("IncomingCount");
- }
- public string Encrypt(string text, string key)
- {
- var result = new StringBuilder();
- for (int c = 0; c < text.Length; c++)
- result.Append((char)((uint)text[c] ^ (uint)key[c % key.Length]));
- return result.ToString();
- }
- public string Decrypt(string text, string key)
- {
- return Encrypt(text, key);
- }
- public JComms(string MyGridName, IMyRadioAntenna MyAntenna, string MyEncryptKey = "")
- {
- GridName = MyGridName;
- CommAntenna = MyAntenna;
- EncryptKey = MyEncryptKey;
- }
- }
Add Comment
Please, Sign In to add comment