Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public override bool Invoke(ulong steamId, long playerId, string messageText)
        {
            if (messageText.StartsWith("/rotateroid ", StringComparison.InvariantCultureIgnoreCase))
            {
                var match = Regex.Match(messageText, @"/rotateroid\s{1,}(?<Key>.+){1,}\s{1,}(?<X>[+-]?((\d+(\.\d*)?)|(\.\d+)))\s{1,}(?<Y>[+-]?((\d+(\.\d*)?)|(\.\d+)))\s{1,}(?<Z>[+-]?((\d+(\.\d*)?)|(\.\d+)))", RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    var rotateVector = new Vector3(
                        double.Parse(match.Groups["X"].Value, CultureInfo.InvariantCulture),
                        double.Parse(match.Groups["Y"].Value, CultureInfo.InvariantCulture),
                        double.Parse(match.Groups["Z"].Value, CultureInfo.InvariantCulture));
                    var searchName = match.Groups["Key"].Value;

                    var          currentAsteroidList = new List <IMyVoxelBase>();
                    IMyVoxelBase originalAsteroid    = null;
                    MyAPIGateway.Session.VoxelMaps.GetInstances(currentAsteroidList, v => v.StorageName.Equals(searchName, StringComparison.InvariantCultureIgnoreCase));
                    if (currentAsteroidList.Count == 1)
                    {
                        originalAsteroid = currentAsteroidList[0];
                    }
                    else
                    {
                        MyAPIGateway.Session.VoxelMaps.GetInstances(currentAsteroidList, v => v.StorageName.IndexOf(searchName, StringComparison.InvariantCultureIgnoreCase) >= 0);

                        if (currentAsteroidList.Count == 1)
                        {
                            originalAsteroid = currentAsteroidList[0];
                        }
                    }

                    List <IMyVoxelBase> asteroidCache = CommandAsteroidsList.GetAsteroidCache(steamId);
                    int index;
                    if (searchName.Substring(0, 1) == "#" && Int32.TryParse(searchName.Substring(1), out index) && index > 0 && index <= asteroidCache.Count)
                    {
                        originalAsteroid = asteroidCache[index - 1];
                    }

                    if (originalAsteroid == null)
                    {
                        MyAPIGateway.Utilities.SendMessage(steamId, "Cannot find asteroid", string.Format("'{0}'", searchName));
                        return(true);
                    }

                    var quaternion = Quaternion.CreateFromYawPitchRoll(rotateVector.X / (180 / MathHelper.Pi), rotateVector.Y / (180 / MathHelper.Pi), rotateVector.Z / (180 / MathHelper.Pi));
                    var oldStorage = originalAsteroid.Storage;

                    var oldCache = new MyStorageData();
                    oldCache.Resize(oldStorage.Size);
                    oldStorage.ReadRange(oldCache, MyStorageDataTypeFlags.ContentAndMaterial, 0, Vector3I.Zero, oldStorage.Size - 1);

                    var transSize   = Vector3I.Transform(oldStorage.Size, quaternion);
                    var newSize     = Vector3I.Abs(transSize);
                    var newName     = Support.CreateUniqueStorageName(originalAsteroid.StorageName);
                    var newVoxelMap = Support.CreateNewAsteroid(newName, newSize, originalAsteroid.PositionLeftBottomCorner);

                    var cache = new MyStorageData();
                    var min   = Vector3I.Zero;
                    var max   = newSize - 1;
                    cache.Resize(min, max);

                    Vector3I p;
                    for (p.Z = 0; p.Z < oldStorage.Size.Z; ++p.Z)
                    {
                        for (p.Y = 0; p.Y < oldStorage.Size.Y; ++p.Y)
                        {
                            for (p.X = 0; p.X < oldStorage.Size.X; ++p.X)
                            {
                                var content  = oldCache.Content(ref p);
                                var material = oldCache.Material(ref p);

                                var newP = Vector3I.Transform(p, quaternion);
                                // readjust the points, as rotation occurs arround 0,0,0.
                                newP.X = newP.X < 0 ? newP.X - transSize.X - 1 : newP.X;
                                newP.Y = newP.Y < 0 ? newP.Y - transSize.Y - 1 : newP.Y;
                                newP.Z = newP.Z < 0 ? newP.Z - transSize.Z - 1 : newP.Z;

                                cache.Content(ref newP, content);
                                cache.Material(ref newP, material);
                            }
                        }
                    }

                    newVoxelMap.Storage.WriteRange(cache, MyStorageDataTypeFlags.ContentAndMaterial, min, max);
                    MyAPIGateway.Entities.RemoveEntity((IMyEntity)originalAsteroid);

                    // Invalidate the cache, to force user to select again to prevent possible corruption by using an old cache.
                    asteroidCache.Clear();
                    return(true);
                }
            }

            return(false);
        }