Ejemplo n.º 1
0
 public static Boolean TryParse(string str, out QuePos quePos)
 {
     try
     {
         string[] info = str.Split(new string[] { ":" }, StringSplitOptions.None);
         if (info.Length == 2)
         {
             QuePos newQuePos = new QuePos(info[0], info[1]);
             quePos = newQuePos;
             return(true);
         }
         else
         {
             quePos = null;
             return(false);
         }
     }
     catch
     {
         quePos = null;
         return(false);
     }
 }
Ejemplo n.º 2
0
        public Program()
        {
            Runtime.UpdateFrequency = UpdateFrequency.Update100;
            antenna = GridTerminalSystem.GetBlockWithName("Antenna") as IMyRadioAntenna;
            lcd     = GridTerminalSystem.GetBlockWithName("lcd") as IMyTextPanel;
            rc      = GridTerminalSystem.GetBlockWithName("rc spaceport") as IMyRemoteControl;

            connectorGroupDict.Add("default", defaultConnectorList);

            ini.TryParse(Storage);

            int i = 0;

            messageQue.Clear();
            while (ini.ContainsKey("save", "messageQue_" + i))
            {
                string element = ini.Get("save", "messageQue_" + i).ToString();
                messageQue.Add(element);
                i++;
            }

            i = 0;
            argumentQue.Clear();
            while (ini.ContainsKey("save", "argumentQue_" + i))
            {
                string element = ini.Get("save", "argumentQue_" + i).ToString();
                argumentQue.Add(element);
                i++;
            }

            i = 0;
            dockingQue.Clear();
            while (ini.ContainsKey("save", "dockingQue_" + i))
            {
                QuePos  element = new QuePos("ERROR");
                Boolean success = QuePos.TryParse(ini.Get("save", "dockingQue_" + i).ToString(), out element);
                if (success)
                {
                    dockingQue.Add(element);
                }
                //else add to debug panel
                i++;
            }

            i = 0;
            connectorGroupDict.Clear();
            while (ini.ContainsKey("save", "connectorGroupDict_" + i))
            {
                string   str     = ini.Get("save", "connectorGroupDict_" + i).ToString();
                string[] info    = str.Split(new string[] { "|" }, StringSplitOptions.None);
                string   key     = info[0];
                string[] subInfo = info[1].Split(new string[] { ":" }, StringSplitOptions.None);
                foreach (string data in subInfo)
                {
                    try
                    {
                        string[] subData = data.Split(new string[] { ";" }, StringSplitOptions.None);
                        string   customName = subData[0];
                        Boolean  free; Boolean freeSuccess = Boolean.TryParse(subData[1], out free);
                        int      freeCounter; Boolean freeCounterSuccess = Int32.TryParse(subData[2], out freeCounter);
                        string   reservedPassword = subData[3];

                        if (freeSuccess && freeCounterSuccess)
                        {
                            AddConnector(customName, key, free, freeCounter, reservedPassword);
                        }
                        else
                        {
                            //add info to debug panel
                        }
                    }
                    catch
                    {
                        //add info to debug panel
                    }
                }
                i++;
            }

            i = 0;
            connectorGroupPath.Clear();
            while (ini.ContainsKey("save", "connectorGroupPath_" + i))
            {
                string   str     = ini.Get("save", "connectorGroupPath_" + i).ToString();
                string[] info    = str.Split(new string[] { "|" }, StringSplitOptions.None);
                string   key     = info[0];
                string[] subInfo = info[1].Split(new string[] { ";" }, StringSplitOptions.None);
                List <MyWaypointInfo> waypointInfos = new List <MyWaypointInfo>();
                foreach (string data in subInfo)
                {
                    try
                    {
                        MyWaypointInfo newWaypoint = new MyWaypointInfo();
                        Boolean        success     = MyWaypointInfo.TryParse(data, out newWaypoint);
                        if (success)
                        {
                            waypointInfos.Add(newWaypoint);
                        }
                        else
                        {
                            lcd.WritePublicText("\nFAILED DATA (E): " + data, true);
                        }
                    }
                    catch
                    {
                        lcd.WritePublicText("\nFAILED DATA (C): " + data, true);
                    }
                }
                SetGroupPath(key, waypointInfos);
                i++;
            }
        }
Ejemplo n.º 3
0
        public void Main(string argument, UpdateType updateSource)
        {
            if (rc.GetShipSpeed() < 1)
            {
                if (!String.IsNullOrEmpty(argument))
                {
                    if (argument.Substring(argument.Length - 1, 1) == ",")
                    {
                        argument = argument.Remove(argument.LastIndexOf(','), 1);
                    }
                    string[] info = argument.Split(new string[] { "," }, StringSplitOptions.None);
                    if (info[0].ToLower() == "add")
                    {
                        if (info.Length == 2)
                        {
                            if (!string.IsNullOrEmpty(info[1]))
                            {
                                AddConnector(info[1]);
                            }
                            else
                            {
                                WriteError(argument);
                            }
                        }
                        else if (info.Length == 3)
                        {
                            if (!string.IsNullOrEmpty(info[1]) && !string.IsNullOrEmpty(info[2]))
                            {
                                AddConnector(info[1], info[2]);
                            }
                            else
                            {
                                WriteError(argument);
                            }
                        }
                        else
                        {
                            WriteError(argument);
                        }
                    }

                    else if (info[0].ToLower() == "addbytag")
                    {
                        if (info.Length == 2)
                        {
                            AddConnectedByTag(info[1]);
                        }
                        else if (info.Length == 3)
                        {
                            AddConnectedByTag(info[1], info[2]);
                        }
                        else
                        {
                            WriteError(argument);
                        }
                    }

                    else if (info[0].ToLower() == "remove")
                    {
                        if (info.Length == 2)
                        {
                            RemoveConnector(info[1]);
                        }
                        else
                        {
                            WriteError(argument);
                        }
                    }

                    else if (info[0].ToLower() == "removegroup")
                    {
                        try {
                            RemoveGroup(info[1]);
                            Echo("Removed group: " + info[1]);
                        }
                        catch {
                            WriteError(argument);
                        }
                    }
                    else if (info[0].ToLower() == "free")
                    {
                        if (info.Length == 2)
                        {
                            FreeConnector(info[1]);
                        }
                        else
                        {
                            WriteError(argument);
                        }
                    }

                    else if (info[0].ToLower() == "showallgroups")
                    {
                        ShowAllGroups();
                        Echo("Check designated LCD");
                    }

                    else if (info[0].ToLower() == "requestdock") //0 command, 1 password, 2 group (if exist), 3 override
                    {
                        Echo("Docking info requested with length: " + info.Length);
                        lcd.WritePublicText("\nDocking Arg:" + argument, true);
                        string password = info[1];

                        Boolean duplicate = false;
                        Boolean leaving   = false;

                        if (info.Length != 4)
                        {
                            foreach (QuePos request in dockingQue)
                            {
                                if (request.password == password)
                                {
                                    duplicate = true;
                                }
                                else
                                if (info[3] == "leaving")
                                {
                                    leaving = true;
                                }
                            }
                        }

                        lcd.WritePublicText("\nDuplicate: " + duplicate, true);

                        if (!duplicate)
                        {
                            Connector connector = null;
                            Boolean   good      = true;

                            if (info.Length == 2)
                            {
                                connector = GetFreeConnector(password, leaving);
                            }
                            else if (info.Length > 2)
                            {
                                connector = GetFreeConnector(info[2], password, leaving);
                            }
                            else
                            {
                                WriteError(argument);
                            } good = false;

                            Boolean sent = antenna.TransmitMessage(password + ",received," + nameOfSpaceport);
                            if (!sent)
                            {
                                messageQue.Add(password + ",received," + nameOfSpaceport);
                            }
                            Echo((connector == null) + "");

                            if (connector != null || leaving)
                            {
                                if (leaving)
                                {
                                    foreach (List <Connector> list in connectorGroupDict.Values)
                                    {
                                        connector = list.First();
                                        break;
                                    }
                                }

                                lcd.WritePublicText("\nFound Connector", true);
                                connector.reservedPassword = password;
                                Echo("Re?");
                                Boolean hasPath = false; List <MyWaypointInfo> path = new List <MyWaypointInfo>();

                                string groupName = "";

                                if (info.Length > 2)
                                {
                                    groupName = info[2];
                                    hasPath   = connectorGroupPath.ContainsKey(info[2]);
                                    if (hasPath)
                                    {
                                        path = connectorGroupPath[info[2]];
                                    }
                                }
                                else if (info.Length == 2)
                                {
                                    groupName = "default";
                                    hasPath   = connectorGroupPath.ContainsKey("default");
                                    if (hasPath)
                                    {
                                        path = connectorGroupPath["default"];
                                    }
                                }

                                Echo("hasPath:" + hasPath);
                                if (!hasPath)
                                {
                                    SendDockingInfo(connector, password);
                                }
                                else
                                {
                                    if (pathFreeDict.ContainsKey(groupName))
                                    {
                                        if (pathFreeDict[groupName])
                                        {
                                            pathFreeDict[groupName] = false;
                                            SendDockingInfo(connector, password, TranslateToWorldCoords(path, rc));
                                            foreach (MyWaypointInfo waypoint in TranslateToWorldCoords(path, rc))
                                            {
                                                lcd.WritePublicText("\nName: " + waypoint.Name + " Coord: " + waypoint.Coords, true);
                                            }
                                        }
                                        else
                                        {
                                            sent = antenna.TransmitMessage(password + ",waitque");
                                            if (!sent)
                                            {
                                                messageQue.Add(password + ",waitque");
                                            }

                                            QuePos newQue = new QuePos(null, null);
                                            if (info.Length == 2)
                                            {
                                                newQue = new QuePos(password, "default");
                                            }
                                            if (info.Length == 3)
                                            {
                                                newQue = new QuePos(password, info[2]);
                                            }

                                            if (newQue.password != null && newQue.group != null)
                                            {
                                                dockingQue.Add(newQue);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        WriteError("pathFreeDict KEY WAS NOT FOUND");
                                    }
                                }
                                Echo("A");
                            }
                            else if (good)
                            {
                                lcd.WritePublicText("\nDidn't find a free connector", true);
                                sent = antenna.TransmitMessage(password + ",waitque");
                                if (!sent)
                                {
                                    messageQue.Add(password + ",waitque");
                                }

                                QuePos newQue = new QuePos(null, null);
                                if (info.Length == 2)
                                {
                                    newQue = new QuePos(password, "default");
                                }
                                if (info.Length == 3)
                                {
                                    newQue = new QuePos(password, info[2]);
                                }

                                if (newQue.password != null && newQue.group != null)
                                {
                                    dockingQue.Add(newQue);
                                }
                            }
                            else
                            {
                                sent = antenna.TransmitMessage(password + ",fail," + nameOfSpaceport);
                                if (!sent)
                                {
                                    messageQue.Add(password + ",fail," + nameOfSpaceport);
                                }
                            }
                        }
                        else
                        {
                            Boolean sent = antenna.TransmitMessage(password + "reject");
                            if (!sent)
                            {
                                messageQue.Add(password + "reject");
                            }
                        }
                    }

                    else if (info[0].ToLower() == "freepath")
                    {
                        if (info.Length == 1)
                        {
                            pathFreeDict["default"] = true;
                        }
                        else if (info.Length == 2)
                        {
                            pathFreeDict[info[1]] = true;
                        }
                        else
                        {
                            WriteError(argument);
                        }

                        lcd.WritePublicText("freepath arg: " + argument);
                    }

                    else if (info[0].ToLower() == "canceldock")
                    {
                        if (info.Length == 2)
                        {
                            string        password   = info[1];
                            List <QuePos> removeList = new List <QuePos>();
                            foreach (QuePos que in dockingQue)
                            {
                                if (que.password == password)
                                {
                                    removeList.Add(que);
                                }
                            }

                            foreach (QuePos removeQue in removeList)
                            {
                                dockingQue.Remove(removeQue);
                            }

                            foreach (List <Connector> list in connectorGroupDict.Values)
                            {
                                foreach (Connector connector in list)
                                {
                                    if (connector.reservedPassword == password)
                                    {
                                        connector.free             = true;
                                        connector.freeCounter      = 0;
                                        connector.reservedPassword = "";
                                    }
                                }
                            }
                        }
                    }

                    else if (info[0].ToLower() == "setpath") //0 command, 1 groupname, 2+ waypoints
                    {
                        if (info.Length > 2)
                        {
                            Echo("info length: " + info.Length);
                            string   group   = info[1];
                            string[] sublist = new string[info.Length - 2];
                            Echo("sub length: " + info.Length);
                            for (int i = 0; i < sublist.Length; i++)
                            {
                                sublist[i] = info[i + 2];
                            }

                            foreach (string str in sublist)
                            {
                                if (!String.IsNullOrEmpty(str))
                                {
                                    Echo(str);
                                    lcd.WritePublicText("\nstr: " + str, true);
                                }
                                else
                                {
                                    Echo("EMPTY STR ERROR");
                                }
                            }

                            lcd.WritePublicText("\nRC coords" + rc.GetPosition() + "\n", true);

                            List <MyWaypointInfo> waypointInfos = StringArraytoWaypoint(sublist);

                            lcd.WritePublicText("\n", true);

                            try
                            {
                                foreach (MyWaypointInfo waypoint in waypointInfos)
                                {
                                    lcd.WritePublicText("\nName: " + waypoint.Name + " Coord: " + waypoint.Coords, true);
                                }

                                if (waypointInfos != null)
                                {
                                    List <MyWaypointInfo> relativeWaypointInfos = TranslateToRelativeCoords(waypointInfos, rc);
                                    SetGroupPath(group, relativeWaypointInfos);
                                    Echo("Path set for: " + group);
                                }
                                else
                                {
                                    WriteError(argument);
                                }
                            }
                            catch
                            {
                                WriteError(argument);
                            }
                        }
                        else
                        {
                            WriteError(argument);
                        }
                    }

                    else if (info[0].ToLower() == "cleardata")
                    {
                        Storage = "";
                        connectorGroupDict.Clear();
                        connectorGroupPath.Clear();
                        pathFreeDict.Clear();
                        messageQue.Clear();
                        dockingQue.Clear();
                        argumentQue.Clear();

                        Echo("DATA CLEARED");
                    }

                    else if (info[0].ToLower() == "debug")
                    {
                        Debug();
                    }
                }

                if (updateSource != UpdateType.Trigger)
                {
                    CheckQue();
                    CheckMessageQue();
                }

                if (messageQue.Count > 0)
                {
                    Echo("First Que: " + messageQue.First());
                }
                else
                {
                    Echo("No messages in que");
                    SendAntennaPos();
                }

                UpdateFreeConnectors();
            }
            else if (updateSource == UpdateType.Antenna)
            {
                argumentQue.Add(argument);
            }
            else
            {
                Echo("SHIP MOVING. SPACEPORT OFFLINE.");
            }
        }