protected override bool Activate(HashSet<MySlimBlock> targets) { m_otherGrid = null; if (targets.Count > 0) { m_otherGrid = targets.FirstElement().CubeGrid; } if (Sync.IsServer) { float coefficient = (MyShipGrinderConstants.GRINDER_COOLDOWN_IN_MILISECONDS * 0.001f) / targets.Count; foreach (var block in targets) { m_otherGrid = block.CubeGrid; block.DecreaseMountLevel(MySession.Static.GrinderSpeedMultiplier * MyShipGrinderConstants.GRINDER_AMOUNT_PER_SECOND * coefficient, Inventory); block.MoveItemsFromConstructionStockpile(Inventory); if (block.IsFullyDismounted) { if (block.FatBlock is IMyInventoryOwner) EmptyBlockInventories(block.FatBlock as IMyInventoryOwner); block.SpawnConstructionStockpile(); block.CubeGrid.RazeBlock(block.Min); } } } m_wantsToShake = targets.Count != 0; return targets.Count != 0; }
protected override bool Activate(HashSet<MySlimBlock> targets) { m_otherGrid = null; if (targets.Count > 0) { m_otherGrid = targets.FirstElement().CubeGrid; } if (Sync.IsServer) { float coefficient = (MyShipGrinderConstants.GRINDER_COOLDOWN_IN_MILISECONDS * 0.001f) / targets.Count; foreach (var block in targets) { if ((MySession.Static.IsScenario || MySession.Static.Settings.ScenarioEditMode) && !block.CubeGrid.BlocksDestructionEnabled) continue; m_otherGrid = block.CubeGrid; float damage = MySession.Static.GrinderSpeedMultiplier * MyShipGrinderConstants.GRINDER_AMOUNT_PER_SECOND * coefficient; MyDamageInformation damageInfo = new MyDamageInformation(false, damage, MyDamageType.Grind, EntityId); if (block.UseDamageSystem) MyDamageSystem.Static.RaiseBeforeDamageApplied(block, ref damageInfo); block.DecreaseMountLevel(damageInfo.Amount, Inventory); block.MoveItemsFromConstructionStockpile(Inventory); if (block.UseDamageSystem) MyDamageSystem.Static.RaiseAfterDamageApplied(block, damageInfo); if (block.IsFullyDismounted) { if (block.FatBlock is IMyInventoryOwner) EmptyBlockInventories(block.FatBlock as IMyInventoryOwner); if(block.UseDamageSystem) MyDamageSystem.Static.RaiseDestroyed(block, damageInfo); block.SpawnConstructionStockpile(); block.CubeGrid.RazeBlock(block.Min); } } } m_wantsToShake = targets.Count != 0; return targets.Count != 0; }
public override bool Invoke(ulong steamId, long playerId, string messageText) { 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; #region Teleport You to target. if (!ident2 && !ship2 && !character2 && !asteroid2 && !planet2 && !word2 && !pos2 && !gps2) { // move this player or the ship this player is in. var player = MyAPIGateway.Session.Player; if (ident1) { if (CommandIdentify.IdentifyCache == null) { MyAPIGateway.Utilities.ShowMessage("Teleport failed", "No item identified yet."); return true; } if (CommandIdentify.IdentifyCache is IMyIdentity) { Support.MoveTo(steamId, player, ((IMyIdentity)CommandIdentify.IdentifyCache).Player(), safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); return true; } if (CommandIdentify.IdentifyCache is IMyCubeGrid) { Support.MoveTo(player, CommandIdentify.IdentifyCache, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); return true; } if (CommandIdentify.IdentifyCache is IMyCubeBlock) { Support.MoveTo(player, CommandIdentify.IdentifyCache.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); return true; } if (CommandIdentify.IdentifyCache is IMyVoxelBase) { Support.MoveTo(player, CommandIdentify.IdentifyCache, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg); return true; } MyAPIGateway.Utilities.ShowMessage("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 <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.IdentityCache[index - 1]; Support.MoveTo(steamId, player, selectedPlayer.Player(), safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); return true; } } if (ship1) { if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.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 <= CommandAsteroidsList.AsteroidCache.Count) { var currentAsteroidList = new HashSet<IMyEntity> { CommandAsteroidsList.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 <= CommandPlanetsList.PlanetCache.Count) { var currentPlanetList = new HashSet<IMyEntity> { CommandPlanetsList.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(entityName, true, true, true, true, true, out foundPlayer, out foundEntity, out foundGps)) { if (foundPlayer != null) Support.MoveTo(steamId, player, foundPlayer, safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); 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.ShowMessage("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 <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.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 <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.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(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 <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.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 <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.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(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 <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.ShipCache[index - 1] }; targetShip = currentShipList.FirstElement(); } if (targetShip == null) { MyAPIGateway.Utilities.ShowMessage("Teleport failed", "Target ship not found."); return true; } if (character1) { if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.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 <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.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(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 <= CommandPlayerStatus.IdentityCache.Count) { targetPlayer = CommandPlayerStatus.IdentityCache[index - 1]; } if (targetPlayer == null) { MyAPIGateway.Utilities.ShowMessage("Teleport failed", "Target player not found."); return true; } if (character1) { if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.IdentityCache[index - 1]; Support.MoveTo(steamId, selectedPlayer.Player(), targetPlayer.Player(), safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); return true; } } if (ship1) { if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.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(entityName1, true, true, false, false, false, out foundPlayer, out foundEntity, out foundGps)) { if (foundPlayer != null) Support.MoveTo(steamId, foundPlayer, targetPlayer.Player(), safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); 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 <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.IdentityCache[index - 1]; IMyPlayer foundPlayer; IMyEntity foundEntity; IMyGps foundGps; if (Support.FindEntitiesNamed(entityName2, true, true, true, true, true, out foundPlayer, out foundEntity, out foundGps)) { if (foundPlayer != null) Support.MoveTo(steamId, selectedPlayer.Player(), foundPlayer, safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); 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 <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.ShipCache[index - 1] }; var selectedShip = currentShipList.FirstElement(); IMyPlayer foundPlayer; IMyEntity foundEntity; IMyGps foundGps; if (Support.FindEntitiesNamed(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(entityName1, true, true, false, false, false, out foundPlayer1, out foundEntity1, out foundGps1); var result2 = Support.FindEntitiesNamed(entityName2, true, true, true, true, true, out foundPlayer2, out foundEntity2, out foundGps2); if (result1 && result2) { if (foundPlayer1 != null && foundPlayer2 != null) Support.MoveTo(steamId, foundPlayer1, foundPlayer2, safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); 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.ShowMessage("Failed", "Could not find '{0}'", entityName1); if (!result2) MyAPIGateway.Utilities.ShowMessage("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.ShowMessage("Teleport failed", "cannot teleport item to itself."); return true; } if (ident2) { if (CommandIdentify.IdentifyCache == null) { MyAPIGateway.Utilities.ShowMessage("Teleport failed", "No item identified yet."); return true; } if (character1) { if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.IdentityCache[index - 1]; if (CommandIdentify.IdentifyCache is IMyIdentity) Support.MoveTo(steamId, selectedPlayer.Player(), ((IMyIdentity)CommandIdentify.IdentifyCache).Player(), safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (CommandIdentify.IdentifyCache is IMyCubeGrid) Support.MoveTo(selectedPlayer.Player(), CommandIdentify.IdentifyCache, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (CommandIdentify.IdentifyCache is IMyCubeBlock) Support.MoveTo(selectedPlayer.Player(), CommandIdentify.IdentifyCache.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (CommandIdentify.IdentifyCache is IMyVoxelBase) Support.MoveTo(selectedPlayer.Player(), CommandIdentify.IdentifyCache, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg); return true; } } if (ship1) { if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.ShipCache[index - 1] }; var sourceShip = currentShipList.FirstElement(); if (CommandIdentify.IdentifyCache is IMyIdentity) Support.MoveTo(sourceShip, ((IMyIdentity)CommandIdentify.IdentifyCache).Player(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (CommandIdentify.IdentifyCache is IMyCubeGrid) Support.MoveTo(sourceShip, CommandIdentify.IdentifyCache, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (CommandIdentify.IdentifyCache is IMyCubeBlock) Support.MoveTo(sourceShip, CommandIdentify.IdentifyCache.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (CommandIdentify.IdentifyCache is IMyVoxelBase) Support.MoveTo(sourceShip, CommandIdentify.IdentifyCache, 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(entityName1, true, true, false, false, false, out foundPlayer, out foundEntity, out foundGps)) { if (foundPlayer != null && CommandIdentify.IdentifyCache is IMyIdentity) Support.MoveTo(steamId, foundPlayer, ((IMyIdentity)CommandIdentify.IdentifyCache).Player(), safely, false, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (foundPlayer != null && CommandIdentify.IdentifyCache is IMyCubeGrid) Support.MoveTo(foundPlayer, CommandIdentify.IdentifyCache, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (foundPlayer != null && CommandIdentify.IdentifyCache is IMyCubeBlock) Support.MoveTo(foundPlayer, CommandIdentify.IdentifyCache.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (foundPlayer != null && CommandIdentify.IdentifyCache is IMyVoxelBase) Support.MoveTo(foundPlayer, CommandIdentify.IdentifyCache, safely, saveTeleportBack, noSafeLocationMsg, emptyTargetMsg, noSafeLocationMsg); if (foundEntity != null && CommandIdentify.IdentifyCache is IMyIdentity) Support.MoveTo(foundEntity, ((IMyIdentity)CommandIdentify.IdentifyCache).Player(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (foundEntity != null && CommandIdentify.IdentifyCache is IMyCubeGrid) Support.MoveTo(foundEntity, CommandIdentify.IdentifyCache, safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (foundEntity != null && CommandIdentify.IdentifyCache is IMyCubeBlock) Support.MoveTo(foundEntity, CommandIdentify.IdentifyCache.GetTopMostParent(), safely, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); if (foundEntity != null && CommandIdentify.IdentifyCache is IMyVoxelBase) Support.MoveTo(foundEntity, CommandIdentify.IdentifyCache, 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 <= CommandAsteroidsList.AsteroidCache.Count) { var currentAsteroidList = new HashSet<IMyEntity> { CommandAsteroidsList.AsteroidCache[index - 1] }; targetAsteroid = currentAsteroidList.FirstElement(); } if (targetAsteroid == null) { MyAPIGateway.Utilities.ShowMessage("Teleport failed", "Target asteroid not found."); return true; } if (character1) { if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.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 <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.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(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 <= CommandPlanetsList.PlanetCache.Count) { var currentPlanetList = new HashSet<IMyEntity> { CommandPlanetsList.PlanetCache[index - 1] }; targetPlanet = currentPlanetList.FirstElement(); } if (targetPlanet == null) { MyAPIGateway.Utilities.ShowMessage("Teleport failed", "Target planet not found."); return true; } if (character1) { if (Int32.TryParse(match.Groups["Character1"].Value.Substring(1), out index) && index > 0 && index <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.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 <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.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(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.ShowMessage("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; var player = MyAPIGateway.Session.Player; IMyPlayer foundPlayer; IMyEntity foundEntity; IMyGps foundGps; if (Support.FindEntitiesNamed(entityName, true, true, true, true, true, out foundPlayer, out foundEntity, out foundGps)) { if (foundPlayer != null) Support.MoveTo(steamId, player, foundPlayer, safely, true, saveTeleportBack, emptySourceMsg, emptyTargetMsg, noSafeLocationMsg); 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 static bool FindEntitiesNamed(string entityName, bool findPlayers, bool findShips, bool findAsteroids, bool findPlanets, bool findGps, out IMyPlayer player, out IMyEntity entity, out IMyGps gps) { #region Find Exact name match first. var playerList = new List<IMyPlayer>(); var shipList = new HashSet<IMyEntity>(); var asteroidList = new List<IMyVoxelBase>(); var planetList = new List<IMyVoxelBase>(); var gpsList = new List<IMyGps>(); if (findPlayers) MyAPIGateway.Players.GetPlayers(playerList, p => entityName == null || p.DisplayName.Equals(entityName, StringComparison.InvariantCultureIgnoreCase)); if (findShips) shipList = FindShipsByName(entityName); if (findAsteroids) MyAPIGateway.Session.VoxelMaps.GetInstances(asteroidList, v => v is IMyVoxelMap && (entityName == null || v.StorageName.Equals(entityName, StringComparison.InvariantCultureIgnoreCase))); if (findPlanets) MyAPIGateway.Session.VoxelMaps.GetInstances(planetList, v => v is Sandbox.Game.Entities.MyPlanet && (entityName == null || v.StorageName.Equals(entityName, StringComparison.InvariantCultureIgnoreCase))); if (findGps) gpsList = MyAPIGateway.Session.GPS.GetGpsList(MyAPIGateway.Session.Player.IdentityId).Where(g => entityName == null || g.Name.Equals(entityName, StringComparison.InvariantCultureIgnoreCase)).ToList(); // identify a unique ship or player by the name. if (playerList.Count > 1 || shipList.Count > 1 || asteroidList.Count > 1 || planetList.Count > 1 || gpsList.Count > 1) { // TODO: too many entities. hotlist or sublist? string msg = "Too many entries with that name."; if (findPlayers) msg += string.Format(" Players: {0}", playerList.Count); if (findShips) msg += string.Format(" Ships: {0}", shipList.Count); if (findAsteroids) msg += string.Format(" Asteroids: {0}", asteroidList.Count); if (findPlayers) msg += string.Format(" Planets: {0}", planetList.Count); if (findGps) msg += string.Format(" Gps: {0}", gpsList.Count); MyAPIGateway.Utilities.ShowMessage("Cannot match", msg); player = null; entity = null; gps = null; return false; } if (playerList.Count == 1 && shipList.Count == 0 && asteroidList.Count == 0 && planetList.Count == 0 && gpsList.Count == 0) { player = playerList[0]; entity = null; gps = null; return true; } if (playerList.Count == 0 && shipList.Count == 1 && asteroidList.Count == 0 && planetList.Count == 0 && gpsList.Count == 0) { player = null; entity = shipList.FirstElement(); gps = null; return true; } if (playerList.Count == 0 && shipList.Count == 0 && asteroidList.Count == 1 && planetList.Count == 0 && gpsList.Count == 0) { player = null; entity = asteroidList[0]; gps = null; return true; } if (playerList.Count == 0 && shipList.Count == 0 && asteroidList.Count == 0 && planetList.Count == 1 && gpsList.Count == 0) { player = null; entity = planetList[0]; gps = null; return true; } if (playerList.Count == 0 && shipList.Count == 0 && asteroidList.Count == 0 && planetList.Count == 0 && gpsList.Count == 1) { player = null; entity = null; gps = gpsList[0]; return true; } #endregion #region find partial name matches. playerList.Clear(); shipList.Clear(); asteroidList.Clear(); planetList.Clear(); gpsList.Clear(); if (findPlayers) MyAPIGateway.Players.GetPlayers(playerList, p => entityName == null || p.DisplayName.IndexOf(entityName, StringComparison.InvariantCultureIgnoreCase) >= 0); if (findShips) shipList = FindShipsByName(entityName, true, false); if (findAsteroids) MyAPIGateway.Session.VoxelMaps.GetInstances(asteroidList, v => v is IMyVoxelMap && (entityName == null || v.StorageName.IndexOf(entityName, StringComparison.InvariantCultureIgnoreCase) >= 0)); if (findPlanets) MyAPIGateway.Session.VoxelMaps.GetInstances(planetList, v => v is Sandbox.Game.Entities.MyPlanet && (entityName == null || v.StorageName.IndexOf(entityName, StringComparison.InvariantCultureIgnoreCase) >= 0)); if (findGps) gpsList = MyAPIGateway.Session.GPS.GetGpsList(MyAPIGateway.Session.Player.IdentityId).Where(g => entityName == null || g.Name.IndexOf(entityName, StringComparison.InvariantCultureIgnoreCase) >= 0).ToList(); // identify a unique ship or player by the name. if (playerList.Count > 1 || shipList.Count > 1 || asteroidList.Count > 1 || planetList.Count > 1 || gpsList.Count > 1) { // TODO: too many entities. hotlist or sublist? string msg = "Too many entries with that name."; if (findPlayers) msg += string.Format(" Players: {0}", playerList.Count); if (findShips) msg += string.Format(" Ships: {0}", shipList.Count); if (findAsteroids) msg += string.Format(" Asteroids: {0}", asteroidList.Count); if (findPlayers) msg += string.Format(" Planets: {0}", planetList.Count); if (findGps) msg += string.Format(" Gps: {0}", gpsList.Count); MyAPIGateway.Utilities.ShowMessage("Cannot match", msg); player = null; entity = null; gps = null; return false; } if (playerList.Count == 1 && shipList.Count == 0 && asteroidList.Count == 0 && planetList.Count == 0 && gpsList.Count == 0) { player = playerList[0]; entity = null; gps = null; return true; } if (playerList.Count == 0 && shipList.Count == 1 && asteroidList.Count == 0 && planetList.Count == 0 && gpsList.Count == 0) { player = null; entity = shipList.FirstElement(); gps = null; return true; } if (playerList.Count == 0 && shipList.Count == 0 && asteroidList.Count == 1 && planetList.Count == 0 && gpsList.Count == 0) { player = null; entity = asteroidList[0]; gps = null; return true; } if (playerList.Count == 0 && shipList.Count == 0 && asteroidList.Count == 0 && planetList.Count == 1 && gpsList.Count == 0) { player = null; entity = planetList[0]; gps = null; return true; } if (playerList.Count == 0 && shipList.Count == 0 && asteroidList.Count == 0 && planetList.Count == 0 && gpsList.Count == 1) { player = null; entity = null; gps = gpsList[0]; return true; } #endregion // In reality, this is equivilant to: // if (playerList.Count == 0 && shipList.Count == 0 && asteroidList.Count == 0 && planetList.Count == 0 && gpsList.Count == 0) MyAPIGateway.Utilities.ShowMessage("Error", "Could not find specified object"); player = null; entity = null; gps = null; return false; }
public override bool Invoke(string messageText) { var match = Regex.Match(messageText, teleportPattern, RegexOptions.IgnoreCase); if (match.Success) { var ship1 = !string.IsNullOrEmpty(match.Groups["Ship1"].Value); var ship2 = !string.IsNullOrEmpty(match.Groups["Ship2"].Value); var player1 = !string.IsNullOrEmpty(match.Groups["Player1"].Value); var player2 = !string.IsNullOrEmpty(match.Groups["Player2"].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(); if (!ship2 && !player2 && !pos2 && !word2) { // move this player or the ship this player is in. var player = MyAPIGateway.Session.Player; 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)); MovePlayerPilotToPosition(player, position); CommandTeleportBack.SaveTeleportInHistory(currentPosition); 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)); MovePlayerPilotToPosition(player, position); CommandTeleportBack.SaveTeleportInHistory(currentPosition); return true; } if (player1) { int index; if (Int32.TryParse(match.Groups["Player1"].Value.Substring(1), out index) && index > 0 && index <= CommandPlayerStatus.IdentityCache.Count) { IMyIdentity selectedPlayer = CommandPlayerStatus.IdentityCache[index - 1]; MovePlayerPilotToPlayer(player, selectedPlayer, true); CommandTeleportBack.SaveTeleportInHistory(currentPosition); return true; } } if (ship1) { int index; if (Int32.TryParse(match.Groups["Ship1"].Value.Substring(1), out index) && index > 0 && index <= CommandListShips.ShipCache.Count) { var currentShipList = new HashSet<IMyEntity> { CommandListShips.ShipCache[index - 1] }; var ship = currentShipList.FirstElement(); MovePlayerPilotToShip(player, ship); CommandTeleportBack.SaveTeleportInHistory(currentPosition); return true; } } if (word1) { var entityName = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value; var players = new List<IMyPlayer>(); MyAPIGateway.Players.GetPlayers(players, p => p.DisplayName.Equals(entityName, StringComparison.InvariantCultureIgnoreCase)); var currentShipList = Support.FindShipsByName(entityName); // identify a unique ship or player by the name. if (players.Count == 0 && currentShipList.Count == 0) { MyAPIGateway.Utilities.ShowMessage("Teleport failed", "Cannot find the player or ship of name '{0}'", entityName); return true; } if (players.Count == 0 && currentShipList.Count == 1) { MovePlayerPilotToShip(player, currentShipList.FirstElement()); CommandTeleportBack.SaveTeleportInHistory(currentPosition); return true; } if (players.Count == 1 && currentShipList.Count == 0) { MovePlayerPilotToPlayer(player, players[0], true); CommandTeleportBack.SaveTeleportInHistory(currentPosition); return true; } // TODO: too many entities. hotlist or sublist? MyAPIGateway.Utilities.ShowMessage("Players", "{0}", players.Count); MyAPIGateway.Utilities.ShowMessage("Ships", "{0}", currentShipList.Count); //var findPlayer = players.FirstOrDefault(); return true; } } var entityName1 = string.IsNullOrEmpty(match.Groups["Quote1"].Value) ? match.Groups["Word1"].Value : match.Groups["Quote1"].Value; var entityName2 = string.IsNullOrEmpty(match.Groups["Quote2"].Value) ? match.Groups["Word2"].Value : match.Groups["Quote2"].Value; 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)); // TODO: generic the methods. :( if (pos2) { if (player1) { } if (ship1) { } if (word1) { } } if (ship2) { if (player1) { } if (ship1) { } if (word1) { } } if (player2) { if (player1) { } if (ship1) { } if (word1) { } } if (word2) { if (player1) { } if (ship1) { } if (word1) { } } // ... move logic. return true; } return false; }