Exemple #1
0
        public override bool Invoke(ulong steamId, long playerId, string messageText)
        {
            if (messageText.Equals("/repair", StringComparison.InvariantCultureIgnoreCase))
            {
                var entity = Support.FindLookAtEntity(MyAPIGateway.Session.ControlledObject, true, false, false, false, false, false);
                if (entity != null)
                {
                    var shipEntity = entity as IMyCubeGrid;
                    if (shipEntity != null)
                    {
                        RepairShip(steamId, entity);
                        return(true);
                    }
                }

                MyAPIGateway.Utilities.SendMessage(steamId, "repair", "No ship targeted.");
                return(true);
            }

            var match = Regex.Match(messageText, @"/repair\s{1,}(?<Key>.+)", RegexOptions.IgnoreCase);

            if (match.Success)
            {
                var shipName = match.Groups["Key"].Value;

                var currentShipList = new HashSet <IMyEntity>();
                MyAPIGateway.Entities.GetEntities(currentShipList, e => e is IMyCubeGrid && e.DisplayName.Equals(shipName, StringComparison.InvariantCultureIgnoreCase));

                if (currentShipList.Count == 1)
                {
                    RepairShip(steamId, currentShipList.First());
                    return(true);
                }
                else if (currentShipList.Count == 0)
                {
                    int index;
                    List <IMyEntity> shipCache = CommandListShips.GetShipCache(steamId);
                    if (shipName.Substring(0, 1) == "#" && Int32.TryParse(shipName.Substring(1), out index) && index > 0 && index <= shipCache.Count && shipCache[index - 1] != null)
                    {
                        RepairShip(steamId, shipCache[index - 1]);
                        shipCache[index - 1] = null;
                        return(true);
                    }
                }
                else if (currentShipList.Count > 1)
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "repair", "{0} Ships match that name.", currentShipList.Count);
                    return(true);
                }

                MyAPIGateway.Utilities.SendMessage(steamId, "repair", "Ship name not found.");
                return(true);
            }

            return(false);
        }
Exemple #2
0
        public override bool Invoke(ulong steamId, long playerId, string messageText)
        {
            var match = Regex.Match(messageText, @"/(elistships|extendedlistships|listships2)(\s{1,}(?<Key>.+)|)", RegexOptions.IgnoreCase);

            if (match.Success)
            {
                string shipName = match.Groups["Key"].Value;

                var currentShipList = Support.FindShipsByName(shipName);

                IMyPlayer player   = MyAPIGateway.Players.GetPlayer(steamId);
                Vector3D  position = Vector3D.Zero;
                if (player != null)
                {
                    position = player.Controller.ControlledEntity.Entity.GetPosition();
                }

                List <IMyEntity> shipCache = CommandListShips.GetShipCache(steamId);
                shipCache.Clear();

                var description = new StringBuilder();
                var prefix      = string.Format("Count: {0}", currentShipList.Count);
                var index       = 1;
                foreach (var ship in currentShipList.OrderBy(s => s.DisplayName))
                {
                    shipCache.Add(ship);
                    var pos      = ship.WorldAABB.Center;
                    var distance = Math.Sqrt((position - pos).LengthSquared());
                    description.AppendFormat("#{0} {1}\r\n  ({2:N}|{3:N}|{4:N}) {5:N}m\r\n", index++, ship.DisplayName, pos.X, pos.Y, pos.Z, distance);
                }

                MyAPIGateway.Utilities.SendMissionScreen(steamId, "List Ships", prefix, " ", description.ToString(), null, "OK");
                return(true);
            }

            return(false);
        }
Exemple #3
0
        public override bool Invoke(ulong steamId, long playerId, string messageText)
        {
            Action <Vector3D> saveTeleportBack = delegate(Vector3D position)
            {
                //save teleport in history
                CommandTeleportBack.SaveTeleportInHistory(playerId, position);
            };

            Action emptySourceMsg = delegate()
            {
                MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "Source entity no longer exists.");
            };

            Action emptyTargetMsg = delegate()
            {
                MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "Target entity no longer exists.");
            };

            Action noSafeLocationMsg = delegate()
            {
                MyAPIGateway.Utilities.SendMessage(steamId, "Failed", "Could not find safe location to transport to.");
            };

            var match = Regex.Match(messageText, teleportPattern, RegexOptions.IgnoreCase);

            if (match.Success)
            {
                var safely     = match.Groups["command"].Value.Equals("/tp", StringComparison.InvariantCultureIgnoreCase);
                var ident1     = !string.IsNullOrEmpty(match.Groups["ID1"].Value);
                var ident2     = !string.IsNullOrEmpty(match.Groups["ID2"].Value);
                var ship1      = !string.IsNullOrEmpty(match.Groups["Ship1"].Value);
                var ship2      = !string.IsNullOrEmpty(match.Groups["Ship2"].Value);
                var character1 = !string.IsNullOrEmpty(match.Groups["Character1"].Value);
                var character2 = !string.IsNullOrEmpty(match.Groups["Character2"].Value);
                var asteroid1  = !string.IsNullOrEmpty(match.Groups["Asteroid1"].Value);
                var asteroid2  = !string.IsNullOrEmpty(match.Groups["Asteroid2"].Value);
                var planet1    = !string.IsNullOrEmpty(match.Groups["Planet1"].Value);
                var planet2    = !string.IsNullOrEmpty(match.Groups["Planet2"].Value);
                var pos1       = !string.IsNullOrEmpty(match.Groups["X1"].Value);
                var pos2       = !string.IsNullOrEmpty(match.Groups["X2"].Value);
                var word1      = !string.IsNullOrEmpty(match.Groups["Quote1"].Value) || !string.IsNullOrEmpty(match.Groups["Word1"].Value);
                var word2      = !string.IsNullOrEmpty(match.Groups["Quote2"].Value) || !string.IsNullOrEmpty(match.Groups["Word2"].Value);
                var gps1       = !string.IsNullOrEmpty(match.Groups["GX1"].Value);
                var gps2       = !string.IsNullOrEmpty(match.Groups["GX2"].Value);
                //var currentPosition = MyAPIGateway.Session.Player.Controller.ControlledEntity.Entity.GetPosition();
                int index;
                List <IMyIdentity> identityCache = CommandPlayerStatus.GetIdentityCache(steamId);
                List <IMyEntity>   shipCache     = CommandListShips.GetShipCache(steamId);

                List <IMyVoxelBase> planetCache   = CommandPlanetsList.GetPlanetCache(steamId);
                List <IMyVoxelBase> asteroidCache = CommandAsteroidsList.GetAsteroidCache(steamId);

                IMyEntity taggedEntity = MessageTaggedEntityStore.GetEntity(playerId);

                #region Teleport You to target.

                if (!ident2 && !ship2 && !character2 && !asteroid2 && !planet2 && !word2 && !pos2 && !gps2)
                {
                    // move this player or the ship this player is in.
                    IMyPlayer player = MyAPIGateway.Players.GetPlayer(steamId);

                    if (ident1)
                    {
                        if (taggedEntity == null)
                        {
                            MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "No item identified yet.");
                            return(true);
                        }

                        if (taggedEntity is IMyIdentity)
                        {
                            Support.MoveTo(player, ((IMyIdentity)taggedEntity).Player(), safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                            return(true);
                        }

                        if (taggedEntity is IMyCubeGrid)
                        {
                            Support.MoveTo(player, taggedEntity, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }

                        if (taggedEntity is IMyCubeBlock)
                        {
                            Support.MoveTo(player, taggedEntity.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }

                        if (taggedEntity is IMyVoxelBase)
                        {
                            Support.MoveTo(player, taggedEntity, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }

                        MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "Item not teleportable to currently.");
                        return(true);
                    }
                    if (pos1)
                    {
                        var position = new Vector3D(
                            double.Parse(match.Groups["X1"].Value, CultureInfo.InvariantCulture),
                            double.Parse(match.Groups["Y1"].Value, CultureInfo.InvariantCulture),
                            double.Parse(match.Groups["Z1"].Value, CultureInfo.InvariantCulture));

                        Support.MoveTo(player, position, safely, saveTeleportBack, noSafeLocationMsg);
                        return(true);
                    }
                    if (gps1)
                    {
                        var position = new Vector3D(
                            double.Parse(match.Groups["GX1"].Value, CultureInfo.InvariantCulture),
                            double.Parse(match.Groups["GY1"].Value, CultureInfo.InvariantCulture),
                            double.Parse(match.Groups["GZ1"].Value, CultureInfo.InvariantCulture));

                        Support.MoveTo(player, position, safely, saveTeleportBack, noSafeLocationMsg);
                        return(true);
                    }
                    if (character1)
                    {
                        if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                        {
                            IMyIdentity selectedPlayer = identityCache[index - 1];
                            Support.MoveTo(player, selectedPlayer.Player(), safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                            return(true);
                        }
                    }
                    if (ship1)
                    {
                        if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                        {
                            var currentShipList = new HashSet <IMyEntity> {
                                shipCache[index - 1]
                            };
                            var targetShip = currentShipList.FirstElement();
                            Support.MoveTo(player, targetShip, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (asteroid1)
                    {
                        if (Int32.TryParse(match.Groups["Asteroid1"].Value.Substring(1), out index) && index > 0 && index <= asteroidCache.Count)
                        {
                            var currentAsteroidList = new HashSet <IMyEntity> {
                                asteroidCache[index - 1]
                            };
                            var asteroid = (IMyVoxelBase)currentAsteroidList.FirstElement();
                            Support.MoveTo(player, asteroid, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (planet1)
                    {
                        if (Int32.TryParse(match.Groups["Planet1"].Value.Substring(1), out index) && index > 0 && index <= planetCache.Count)
                        {
                            var currentPlanetList = new HashSet <IMyEntity> {
                                planetCache[index - 1]
                            };
                            var planet = (IMyVoxelBase)currentPlanetList.FirstElement();
                            Support.MoveTo(player, planet, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (word1)
                    {
                        var entityName = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value;

                        IMyPlayer foundPlayer;
                        IMyEntity foundEntity;
                        IMyGps    foundGps;
                        if (Support.FindEntitiesNamed(steamId, playerId, entityName, true, true, true, true, true, out foundPlayer, out foundEntity, out foundGps))
                        {
                            if (foundPlayer != null)
                            {
                                Support.MoveTo(player, foundPlayer, safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                            }
                            if (foundEntity != null)
                            {
                                Support.MoveTo(player, foundEntity, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundGps != null)
                            {
                                Support.MoveTo(player, foundGps.Coords, safely, saveTeleportBack, noSafeLocationMsg);
                            }
                        }
                        return(true); // FindEntitiesNamed should have displayed a message.
                    }

                    MyAPIGateway.Utilities.SendMessage(steamId, "Error", "Could not find specified object");
                    return(true);
                }

                #endregion

                #region Teleport entity to position.

                if (pos2)
                {
                    var position2 = new Vector3D(
                        double.Parse(match.Groups["X2"].Value, CultureInfo.InvariantCulture),
                        double.Parse(match.Groups["Y2"].Value, CultureInfo.InvariantCulture),
                        double.Parse(match.Groups["Z2"].Value, CultureInfo.InvariantCulture));

                    if (character1)
                    {
                        if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                        {
                            IMyIdentity selectedPlayer = identityCache[index - 1];
                            Support.MoveTo(selectedPlayer.Player(), position2, safely, saveTeleportBack, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (ship1)
                    {
                        if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                        {
                            var currentShipList = new HashSet <IMyEntity> {
                                shipCache[index - 1]
                            };
                            var sourceShip = currentShipList.FirstElement();
                            Support.MoveTo(sourceShip, position2, safely, saveTeleportBack, emptySourceMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (word1)
                    {
                        var entityName1 = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value;

                        IMyPlayer foundPlayer;
                        IMyEntity foundEntity;
                        IMyGps    foundGps;
                        if (Support.FindEntitiesNamed(steamId, playerId, entityName1, true, true, false, false, false, out foundPlayer, out foundEntity, out foundGps))
                        {
                            if (foundPlayer != null)
                            {
                                Support.MoveTo(foundPlayer, position2, safely, saveTeleportBack, noSafeLocationMsg);
                            }
                            if (foundEntity != null)
                            {
                                Support.MoveTo(foundEntity, position2, safely, saveTeleportBack, emptySourceMsg, noSafeLocationMsg);
                            }
                        }
                        return(true); // FindEntitiesNamed should have displayed a message.
                    }
                }

                #endregion

                #region Teleport entity to gps

                if (gps2)
                {
                    var position2 = new Vector3D(
                        double.Parse(match.Groups["GX2"].Value, CultureInfo.InvariantCulture),
                        double.Parse(match.Groups["GY2"].Value, CultureInfo.InvariantCulture),
                        double.Parse(match.Groups["GZ2"].Value, CultureInfo.InvariantCulture));

                    if (character1)
                    {
                        if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                        {
                            IMyIdentity selectedPlayer = identityCache[index - 1];
                            Support.MoveTo(selectedPlayer.Player(), position2, safely, saveTeleportBack, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (ship1)
                    {
                        if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                        {
                            var currentShipList = new HashSet <IMyEntity> {
                                shipCache[index - 1]
                            };
                            var sourceShip = currentShipList.FirstElement();
                            Support.MoveTo(sourceShip, position2, safely, saveTeleportBack, emptySourceMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (word1)
                    {
                        var entityName1 = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value;

                        IMyPlayer foundPlayer;
                        IMyEntity foundEntity;
                        IMyGps    foundGps;
                        if (Support.FindEntitiesNamed(steamId, playerId, entityName1, true, true, false, false, false, out foundPlayer, out foundEntity, out foundGps))
                        {
                            if (foundPlayer != null)
                            {
                                Support.MoveTo(foundPlayer, position2, safely, saveTeleportBack, noSafeLocationMsg);
                            }
                            if (foundEntity != null)
                            {
                                Support.MoveTo(foundEntity, position2, safely, saveTeleportBack, emptySourceMsg, noSafeLocationMsg);
                            }
                        }
                        return(true); // FindEntitiesNamed should have displayed a message.
                    }
                }

                #endregion

                #region Teleport entity to shipgrid.

                if (ship2)
                {
                    IMyEntity targetShip = null;
                    if (Int32.TryParse(match.Groups["Ship2"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                    {
                        var currentShipList = new HashSet <IMyEntity> {
                            shipCache[index - 1]
                        };
                        targetShip = currentShipList.FirstElement();
                    }
                    if (targetShip == null)
                    {
                        MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "Target ship not found.");
                        return(true);
                    }

                    if (character1)
                    {
                        if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                        {
                            IMyIdentity selectedPlayer = identityCache[index - 1];
                            Support.MoveTo(selectedPlayer.Player(), targetShip, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (ship1)
                    {
                        if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                        {
                            var currentShipList = new HashSet <IMyEntity> {
                                shipCache[index - 1]
                            };
                            var sourceShip = currentShipList.FirstElement();
                            Support.MoveTo(sourceShip, targetShip, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (word1)
                    {
                        var entityName1 = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value;

                        IMyPlayer foundPlayer;
                        IMyEntity foundEntity;
                        IMyGps    foundGps;
                        if (Support.FindEntitiesNamed(steamId, playerId, entityName1, true, true, false, false, false, out foundPlayer, out foundEntity, out foundGps))
                        {
                            if (foundPlayer != null)
                            {
                                Support.MoveTo(foundPlayer, targetShip, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundEntity != null)
                            {
                                Support.MoveTo(foundEntity, targetShip, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                        }
                        return(true); // FindEntitiesNamed should have displayed a message.
                    }
                }

                #endregion

                #region Teleport entity to player.

                if (character2)
                {
                    IMyIdentity targetPlayer = null;
                    if (Int32.TryParse(match.Groups["Character2"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                    {
                        targetPlayer = identityCache[index - 1];
                    }
                    if (targetPlayer == null)
                    {
                        MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "Target player not found.");
                        return(true);
                    }

                    if (character1)
                    {
                        if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                        {
                            IMyIdentity selectedPlayer = identityCache[index - 1];
                            Support.MoveTo(selectedPlayer.Player(), targetPlayer.Player(), safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                            return(true);
                        }
                    }
                    if (ship1)
                    {
                        if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                        {
                            var currentShipList = new HashSet <IMyEntity> {
                                shipCache[index - 1]
                            };
                            var sourceShip = currentShipList.FirstElement();
                            Support.MoveTo(sourceShip, targetPlayer.Player(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (word1)
                    {
                        var entityName1 = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value;

                        IMyPlayer foundPlayer;
                        IMyEntity foundEntity;
                        IMyGps    foundGps;
                        if (Support.FindEntitiesNamed(steamId, playerId, entityName1, true, true, false, false, false, out foundPlayer, out foundEntity, out foundGps))
                        {
                            if (foundPlayer != null)
                            {
                                Support.MoveTo(foundPlayer, targetPlayer.Player(), safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                            }
                            if (foundEntity != null)
                            {
                                Support.MoveTo(foundEntity, targetPlayer.Player(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                        }
                        return(true); // FindEntitiesNamed should have displayed a message.
                    }
                }

                #endregion

                #region Teleport entity to named entity.

                if (word2)
                {
                    var entityName2 = string.IsNullOrEmpty(match.Groups["Quote2"].Value) ? match.Groups["Word2"].Value : match.Groups["Quote2"].Value;

                    if (character1)
                    {
                        if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                        {
                            IMyIdentity selectedPlayer = identityCache[index - 1];

                            IMyPlayer foundPlayer;
                            IMyEntity foundEntity;
                            IMyGps    foundGps;
                            if (Support.FindEntitiesNamed(steamId, playerId, entityName2, true, true, true, true, true, out foundPlayer, out foundEntity, out foundGps))
                            {
                                if (foundPlayer != null)
                                {
                                    Support.MoveTo(selectedPlayer.Player(), foundPlayer, safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                                }
                                if (foundEntity != null)
                                {
                                    Support.MoveTo(selectedPlayer.Player(), foundEntity, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                                }
                                if (foundGps != null)
                                {
                                    Support.MoveTo(selectedPlayer.Player(), foundGps.Coords, safely, saveTeleportBack, noSafeLocationMsg);
                                }
                            }
                            return(true); // FindEntitiesNamed should have displayed a message.
                        }
                    }
                    if (ship1)
                    {
                        if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                        {
                            var currentShipList = new HashSet <IMyEntity> {
                                shipCache[index - 1]
                            };
                            var selectedShip = currentShipList.FirstElement();

                            IMyPlayer foundPlayer;
                            IMyEntity foundEntity;
                            IMyGps    foundGps;
                            if (Support.FindEntitiesNamed(steamId, playerId, entityName2, true, true, true, true, true, out foundPlayer, out foundEntity, out foundGps))
                            {
                                if (foundPlayer != null)
                                {
                                    Support.MoveTo(selectedShip, foundPlayer, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                                }
                                if (foundEntity != null)
                                {
                                    Support.MoveTo(selectedShip, foundEntity, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                                }
                                if (foundGps != null)
                                {
                                    Support.MoveTo(selectedShip, foundGps.Coords, safely, saveTeleportBack, emptySourceMsg, noSafeLocationMsg);
                                }
                            }
                            return(true); // FindEntitiesNamed should have displayed a message.
                        }
                    }
                    if (word1)
                    {
                        var entityName1 = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value;

                        IMyPlayer foundPlayer1;
                        IMyEntity foundEntity1;
                        IMyGps    foundGps1;
                        IMyPlayer foundPlayer2;
                        IMyEntity foundEntity2;
                        IMyGps    foundGps2;

                        var result1 = Support.FindEntitiesNamed(steamId, playerId, entityName1, true, true, false, false, false, out foundPlayer1, out foundEntity1, out foundGps1);
                        var result2 = Support.FindEntitiesNamed(steamId, playerId, entityName2, true, true, true, true, true, out foundPlayer2, out foundEntity2, out foundGps2);

                        if (result1 && result2)
                        {
                            if (foundPlayer1 != null && foundPlayer2 != null)
                            {
                                Support.MoveTo(foundPlayer1, foundPlayer2, safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                            }
                            if (foundPlayer1 != null && foundEntity2 != null)
                            {
                                Support.MoveTo(foundPlayer1, foundEntity2, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundPlayer1 != null && foundGps2 != null)
                            {
                                Support.MoveTo(foundPlayer1, foundGps2.Coords, safely, saveTeleportBack, noSafeLocationMsg);
                            }
                            if (foundEntity1 != null && foundPlayer2 != null)
                            {
                                Support.MoveTo(foundEntity1, foundPlayer2, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundEntity1 != null && foundEntity2 != null)
                            {
                                Support.MoveTo(foundEntity1, foundEntity2, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundEntity1 != null && foundGps2 != null)
                            {
                                Support.MoveTo(foundEntity1, foundGps2.Coords, safely, saveTeleportBack, emptySourceMsg, noSafeLocationMsg);
                            }
                        }
                        if (!result1)
                        {
                            MyAPIGateway.Utilities.SendMessage(steamId, "Failed", "Could not find '{0}'", entityName1);
                        }
                        if (!result2)
                        {
                            MyAPIGateway.Utilities.SendMessage(steamId, "Failed", "Could not find '{0}'", entityName2);
                        }
                        return(true); // FindEntitiesNamed should have displayed a message.
                    }
                }

                #endregion

                #region Teleport entity to ID

                if (ident1 && ident2)
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "cannot teleport item to itself.");
                    return(true);
                }

                if (ident2)
                {
                    if (taggedEntity == null)
                    {
                        MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "No item identified yet.");
                        return(true);
                    }

                    if (character1)
                    {
                        if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                        {
                            IMyIdentity selectedPlayer = identityCache[index - 1];
                            if (taggedEntity is IMyIdentity)
                            {
                                Support.MoveTo(selectedPlayer.Player(), ((IMyIdentity)taggedEntity).Player(), safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                            }
                            if (taggedEntity is IMyCubeGrid)
                            {
                                Support.MoveTo(selectedPlayer.Player(), taggedEntity, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (taggedEntity is IMyCubeBlock)
                            {
                                Support.MoveTo(selectedPlayer.Player(), taggedEntity.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (taggedEntity is IMyVoxelBase)
                            {
                                Support.MoveTo(selectedPlayer.Player(), taggedEntity, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            return(true);
                        }
                    }
                    if (ship1)
                    {
                        if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                        {
                            var currentShipList = new HashSet <IMyEntity> {
                                shipCache[index - 1]
                            };
                            var sourceShip = currentShipList.FirstElement();
                            if (taggedEntity is IMyIdentity)
                            {
                                Support.MoveTo(sourceShip, ((IMyIdentity)taggedEntity).Player(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (taggedEntity is IMyCubeGrid)
                            {
                                Support.MoveTo(sourceShip, taggedEntity, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (taggedEntity is IMyCubeBlock)
                            {
                                Support.MoveTo(sourceShip, taggedEntity.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (taggedEntity is IMyVoxelBase)
                            {
                                Support.MoveTo(sourceShip, taggedEntity, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            return(true);
                        }
                    }
                    if (word1)
                    {
                        var entityName1 = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value;

                        IMyPlayer foundPlayer;
                        IMyEntity foundEntity;
                        IMyGps    foundGps;
                        if (Support.FindEntitiesNamed(steamId, playerId, entityName1, true, true, false, false, false, out foundPlayer, out foundEntity, out foundGps))
                        {
                            if (foundPlayer != null && taggedEntity is IMyIdentity)
                            {
                                Support.MoveTo(foundPlayer, ((IMyIdentity)taggedEntity).Player(), safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                            }
                            if (foundPlayer != null && taggedEntity is IMyCubeGrid)
                            {
                                Support.MoveTo(foundPlayer, taggedEntity, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundPlayer != null && taggedEntity is IMyCubeBlock)
                            {
                                Support.MoveTo(foundPlayer, taggedEntity.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundPlayer != null && taggedEntity is IMyVoxelBase)
                            {
                                Support.MoveTo(foundPlayer, taggedEntity, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundEntity != null && taggedEntity is IMyIdentity)
                            {
                                Support.MoveTo(foundEntity, ((IMyIdentity)taggedEntity).Player(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundEntity != null && taggedEntity is IMyCubeGrid)
                            {
                                Support.MoveTo(foundEntity, taggedEntity, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundEntity != null && taggedEntity is IMyCubeBlock)
                            {
                                Support.MoveTo(foundEntity, taggedEntity.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundEntity != null && taggedEntity is IMyVoxelBase)
                            {
                                Support.MoveTo(foundEntity, taggedEntity, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                        }
                        return(true); // FindEntitiesNamed should have displayed a message.
                    }
                }

                #endregion

                #region Teleport entity to asteroid.

                if (asteroid2)
                {
                    IMyEntity targetAsteroid = null;
                    if (Int32.TryParse(match.Groups["Asteroid2"].Value.Substring(1), out index) && index > 0 && index <= asteroidCache.Count)
                    {
                        var currentAsteroidList = new HashSet <IMyEntity> {
                            asteroidCache[index - 1]
                        };
                        targetAsteroid = currentAsteroidList.FirstElement();
                    }
                    if (targetAsteroid == null)
                    {
                        MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "Target asteroid not found.");
                        return(true);
                    }

                    if (character1)
                    {
                        if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                        {
                            IMyIdentity selectedPlayer = identityCache[index - 1];
                            Support.MoveTo(selectedPlayer.Player(), targetAsteroid, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (ship1)
                    {
                        if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                        {
                            var currentShipList = new HashSet <IMyEntity> {
                                shipCache[index - 1]
                            };
                            var sourceShip = currentShipList.FirstElement();
                            Support.MoveTo(sourceShip, targetAsteroid, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (word1)
                    {
                        var entityName1 = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value;

                        IMyPlayer foundPlayer;
                        IMyEntity foundEntity;
                        IMyGps    foundGps;
                        if (Support.FindEntitiesNamed(steamId, playerId, entityName1, true, true, false, false, false, out foundPlayer, out foundEntity, out foundGps))
                        {
                            if (foundPlayer != null)
                            {
                                Support.MoveTo(foundPlayer, targetAsteroid, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundEntity != null)
                            {
                                Support.MoveTo(foundEntity, targetAsteroid, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                        }
                        return(true); // FindEntitiesNamed should have displayed a message.
                    }
                }

                #endregion

                #region Teleport entity to planet.

                if (planet2)
                {
                    IMyEntity targetPlanet = null;
                    if (Int32.TryParse(match.Groups["Planet2"].Value.Substring(1), out index) && index > 0 && index <= planetCache.Count)
                    {
                        var currentPlanetList = new HashSet <IMyEntity> {
                            planetCache[index - 1]
                        };
                        targetPlanet = currentPlanetList.FirstElement();
                    }
                    if (targetPlanet == null)
                    {
                        MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "Target planet not found.");
                        return(true);
                    }

                    if (character1)
                    {
                        if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= identityCache.Count)
                        {
                            IMyIdentity selectedPlayer = identityCache[index - 1];
                            Support.MoveTo(selectedPlayer.Player(), targetPlanet, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (ship1)
                    {
                        if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                        {
                            var currentShipList = new HashSet <IMyEntity> {
                                shipCache[index - 1]
                            };
                            var sourceShip = currentShipList.FirstElement();
                            Support.MoveTo(sourceShip, targetPlanet, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            return(true);
                        }
                    }
                    if (word1)
                    {
                        var entityName1 = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value;

                        IMyPlayer foundPlayer;
                        IMyEntity foundEntity;
                        IMyGps    foundGps;
                        if (Support.FindEntitiesNamed(steamId, playerId, entityName1, true, true, false, false, false, out foundPlayer, out foundEntity, out foundGps))
                        {
                            if (foundPlayer != null)
                            {
                                Support.MoveTo(foundPlayer, targetPlanet, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                            if (foundEntity != null)
                            {
                                Support.MoveTo(foundEntity, targetPlanet, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                            }
                        }
                        return(true); // FindEntitiesNamed should have displayed a message.
                    }
                }

                #endregion

                // more move logic goes here. If we can figure out if we need more. When we need it.

                MyAPIGateway.Utilities.SendMessage(steamId, "Incomplete", "This function of teleport is not complete.");
                return(true);
            }

            match = Regex.Match(messageText, teleportPattern2, RegexOptions.IgnoreCase);
            if (match.Success)
            {
                var safely     = match.Groups["command"].Value.Equals("/tp", StringComparison.InvariantCultureIgnoreCase);
                var entityName = match.Groups["Word1"].Value;

                IMyPlayer player = MyAPIGateway.Players.GetPlayer(steamId);
                IMyPlayer foundPlayer;
                IMyEntity foundEntity;
                IMyGps    foundGps;
                if (Support.FindEntitiesNamed(steamId, playerId, entityName, true, true, true, true, true, out foundPlayer, out foundEntity, out foundGps))
                {
                    if (foundPlayer != null)
                    {
                        Support.MoveTo(player, foundPlayer, safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg, steamId);
                    }
                    if (foundEntity != null)
                    {
                        Support.MoveTo(player, foundEntity, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg);
                    }
                    if (foundGps != null)
                    {
                        Support.MoveTo(player, foundGps.Coords, safely, saveTeleportBack, noSafeLocationMsg);
                    }
                }
                return(true); // FindEntitiesNamed should have displayed a message.
            }

            return(false);
        }
        public override bool Invoke(ulong steamId, long playerId, string messageText)
        {
            var match = Regex.Match(messageText, @"/tps\s{1,}(?<Key>.+)", RegexOptions.IgnoreCase);

            if (match.Success)
            {
                var shipName = match.Groups["Key"].Value;

                var currentShipList = new HashSet <IMyEntity>();
                MyAPIGateway.Entities.GetEntities(currentShipList, e => e is IMyCubeGrid && e.DisplayName.Equals(shipName, StringComparison.InvariantCultureIgnoreCase));

                if (currentShipList.Count == 0)
                {
                    int index;
                    List <IMyEntity> shipCache = CommandListShips.GetShipCache(steamId);
                    if (shipName.Substring(0, 1) == "#" && Int32.TryParse(shipName.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                    {
                        currentShipList = new HashSet <IMyEntity> {
                            shipCache[index - 1]
                        };
                    }
                }

                var ship = currentShipList.FirstElement();

                if (ship == null)
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "Ship name", "'{0}' not found", shipName);
                    return(true);
                }

                if (ship.Closed)
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "Ship", "no longer exists");
                    return(true);
                }

                Action <Vector3D> saveTeleportBack = delegate(Vector3D position)
                {
                    // save teleport in history
                    CommandTeleportBack.SaveTeleportInHistory(playerId, position);
                };

                Action emptySourceMsg = delegate()
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "Source player no longer exists.");
                };

                Action emptyTargetMsg = delegate()
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "Teleport failed", "Target ship no longer exists.");
                };

                Action noSafeLocationMsg = delegate()
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "Failed", "Could not find safe location to transport to.");
                };

                IMyPlayer player = MyAPIGateway.Players.GetPlayer(steamId);
                return(Support.MoveTo(player, ship, true,
                                      saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg));
            }

            return(false);
        }