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) { 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, this.GetInventory()); block.MoveItemsFromConstructionStockpile(this.GetInventory()); if (block.UseDamageSystem) { MyDamageSystem.Static.RaiseAfterDamageApplied(block, damageInfo); } if (block.IsFullyDismounted) { if (block.FatBlock != null && block.FatBlock.HasInventory) { EmptyBlockInventories(block.FatBlock); } if (block.UseDamageSystem) { MyDamageSystem.Static.RaiseDestroyed(block, damageInfo); } block.SpawnConstructionStockpile(); block.CubeGrid.RazeBlock(block.Min); } } if (targets.Count > 0) { SetBuildingMusic(200); } } 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; }
private bool IsRaycastOK(List <MyPhysics.HitInfo> m_raycastList) { m_raycastHashSet.Clear(); foreach (var rb in m_raycastList) { if (rb.HkHitInfo.Body == null || rb.HkHitInfo.Body.UserObject == null || !(rb.HkHitInfo.Body.UserObject is MyPhysicsBody)) { continue; } if (rb.HkHitInfo.GetHitEntity() is IMyHandheldGunObject <Sandbox.Game.Weapons.MyDeviceBase> ) // ignore player weapons { continue; } m_raycastHashSet.Add(rb.HkHitInfo.GetHitEntity()); } if (m_raycastHashSet.Count > 1) { return(false); } if (m_raycastHashSet.Count == 1 && MySession.Static.ControlledEntity == null) { return(false); } if (m_raycastHashSet.Count == 1 && m_raycastHashSet.FirstElement() != ((MyEntity)MySession.Static.ControlledEntity).GetTopMostParent()) { return(false); } m_raycastHashSet.Clear(); return(true); }
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, 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 bool Disconnect(MyCubeGrid grid, MySlimBlock testBlock = null, bool testDisconnect = false) { ProfilerShort.Begin("Collect+IsInVoxels"); m_largestGroupWithPhysics = default(Group); m_groups.Clear(); m_sortedBlocks.Clear(); m_disconnectHelper.Clear(); foreach (var block in grid.GetBlocks()) { if (block == testBlock) { continue; } m_disconnectHelper.Add(block); } ProfilerShort.End(); ProfilerShort.Begin("GroupBy"); while (m_disconnectHelper.Count > 0) { Group group = default(Group); group.FirstBlockIndex = m_sortedBlocks.Count; AddNeighbours(m_disconnectHelper.FirstElement(), out group.IsValid, testBlock); group.BlockCount = m_sortedBlocks.Count - group.FirstBlockIndex; if (group.IsValid && group.BlockCount > m_largestGroupWithPhysics.BlockCount) { if (m_largestGroupWithPhysics.BlockCount > 0) // Is valid { // order matters, insert in correct place int i = 0; for (i = 0; i < m_groups.Count; i++) { if (m_groups[i].FirstBlockIndex > m_largestGroupWithPhysics.FirstBlockIndex) { m_groups.Insert(i, m_largestGroupWithPhysics); break; } } if (i == m_groups.Count) { m_groups.Add(m_largestGroupWithPhysics); } } m_largestGroupWithPhysics = group; } else { m_groups.Add(group); } } ProfilerShort.End(); ProfilerShort.Begin("RemoveLargestGroup"); m_sortedBlocks.RemoveRange(m_largestGroupWithPhysics.FirstBlockIndex, m_largestGroupWithPhysics.BlockCount); for (int i = 0; i < m_groups.Count; i++) { var g = m_groups[i]; if (g.FirstBlockIndex > m_largestGroupWithPhysics.FirstBlockIndex) { g.FirstBlockIndex -= m_largestGroupWithPhysics.BlockCount; m_groups[i] = g; } } ProfilerShort.End(); ProfilerShort.Begin("CreateSplits"); if (m_groups.Count > 0) { if (testDisconnect) { m_groups.Clear(); m_sortedBlocks.Clear(); m_disconnectHelper.Clear(); ProfilerShort.End(); return(true); } MyCubeGrid.CreateSplits(grid, m_sortedBlocks, m_groups); } else { if (!MySession.Static.Settings.StationVoxelSupport) { if (grid.IsStatic) { grid.TestDynamic = MyCubeGrid.MyTestDynamicReason.GridSplit; } } } ProfilerShort.End(); m_groups.Clear(); m_sortedBlocks.Clear(); m_disconnectHelper.Clear(); 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; }
private static bool StepWeld(ShipyardItem shipyardItem) { HashSet <long> stalledWelders = new HashSet <long>(); var missingComponents = new Dictionary <string, int>(); var random = new Random(); float weldAmount = Server.Instance.Config.WelderSpeedMultiplier * PluginSettings.Instance.WeldMultiplier; float boneAmount = weldAmount * .1f; //shorten this to grid for convenience MyCubeGrid grid = shipyardItem.Grid; if (grid?.Physics == null || grid.Closed) { return(false); } var weldBlocks = new HashSet <MySlimBlock>(); foreach (MySlimBlock block in grid.CubeBlocks.Where(block => !block.IsFullIntegrity || (block.HasDeformation || block.MaxDeformation > 0.0001f))) { weldBlocks.Add(block); } //if we have no blocks to weld, return false so we know we're done if (weldBlocks.Count == 0) { if (shipyardItem.GridProjector == null) { foreach (var block in shipyardItem.Grid.GetFatBlocks()) { MyProjectorBase projector = block as MyProjectorBase; if (projector?.Clipboard.PreviewGrids.Count > 0) { /* * bool canBuild = false; * Wrapper.GameAction( () => canBuild = projector.Clipboard.PreviewGrids[0].CubeBlocks.All(x => projector.CanBuild(x, true) != MyProjectorBase.BuildCheckResult.OK)); * if ( !canBuild) * continue; */ shipyardItem.GridProjector = projector; break; } } } } if (weldBlocks.Count < 8 && shipyardItem.GridProjector?.Clipboard.PreviewGrids.Count > 0) { int neededBlocks = 8 - weldBlocks.Count; //shipyardItem.ProcessBlocks.Clear(); for (int b = 0; b < neededBlocks; b++) { var welder = shipyardItem.Tools[b]; var projector = shipyardItem.GridProjector; var projectedGrid = projector.Clipboard.PreviewGrids[0]; MySlimBlock buildBlock = null; Wrapper.GameAction(() => { //buildBlock = projectedGrid.CubeBlocks.First( x => projector.CanBuild( x, true ) == MyProjectorBase.BuildCheckResult.OK ); foreach (var block in projectedGrid.CubeBlocks) { if (projector.CanBuild(block, true) == MyProjectorBase.BuildCheckResult.OK) { buildBlock = block; break; } } if (buildBlock == null) { return; } var welderInventory = (MyInventory)((IMyShipWelder)welder).GetInventory(0); var components = buildBlock.BlockDefinition.Components; if (components == null || components.Length == 0) { return; } var componentId = components[0].Definition.Id; MyGridConveyorSystem.ItemPullRequest((IMyConveyorEndpointBlock)welder, welderInventory, welder.OwnerId, componentId, 1); if (welderInventory.ContainItems(1, buildBlock.BlockDefinition.Components[0].Definition.Id)) { projector.Build(buildBlock, welder.OwnerId, welder.EntityId); } }); /* * Communication.MessageStruct message = new Communication.MessageStruct() * { * toolId=welder.EntityId, * gridId=buildBlock.CubeGrid.EntityId, * blockPos=buildBlock.Position, * packedColor = Color.LightGreen.PackedValue, * pulse=false * }; * Communication.SendLine( message ); */ //if (buildBlock!=null) //weldBlocks.Add( buildBlock ); } } if (shipyardItem.GridProjector?.Clipboard.PreviewGrids.Count == 0) { shipyardItem.GridProjector = null; return(false); } foreach (IMyCubeBlock welder in shipyardItem.Tools) { if (!shipyardItem.ProcessBlocks.ContainsKey(welder.EntityId)) { MySlimBlock nextBlock = null; var tryCount = 0; while (tryCount < 20) { //limit the number of tries so we don't get stuck in a loop forever tryCount++; //pick a random block. we don't really care if two welders hit the same block, so don't check if (weldBlocks.Count > 1) { nextBlock = weldBlocks.ElementAt(random.Next(0, weldBlocks.Count - 1)); } else { nextBlock = weldBlocks.FirstElement(); } if (nextBlock == null) { continue; } if (!nextBlock.IsFullIntegrity || (nextBlock.HasDeformation || nextBlock.MaxDeformation > 0.0001f)) { break; } } //we weren't able to find a suitable block somehow, so skip this welder for now if (nextBlock == null) { continue; } //we found a block to pair with our welder, add it to the dictionary and carry on with destruction shipyardItem.ProcessBlocks.Add(welder.EntityId, nextBlock); } } if (shipyardItem.ProcessBlocks.Count < 1) { //No more blocks to weld return(false); } Wrapper.GameAction(() => { foreach (IMyCubeBlock welderBlock in shipyardItem.Tools) { var welder = (IMyShipWelder)welderBlock; var welderInventory = (MyInventory)welder.GetInventory(0); MySlimBlock block; shipyardItem.ProcessBlocks.TryGetValue(welderBlock.EntityId, out block); if (block?.CubeGrid?.Physics == null) { continue; } if (!(!block.IsFullIntegrity || (block.HasDeformation || block.MaxDeformation > 0.0001f))) { shipyardItem.ProcessBlocks.Remove(welder.EntityId); continue; } block.GetMissingComponents(missingComponents); foreach (KeyValuePair <string, int> component in missingComponents) { var componentId = new MyDefinitionId(typeof(MyObjectBuilder_Component), component.Key); int amount = Math.Max(component.Value - (int)welderInventory.GetItemAmount(componentId), 0); if (amount == 0) { continue; } if (welder.UseConveyorSystem) { MyGridConveyorSystem.ItemPullRequest((IMyConveyorEndpointBlock)welder, welderInventory, welder.OwnerId, componentId, component.Value); } } block.MoveItemsToConstructionStockpile((MyInventory)welder.GetInventory(0)); block.IncreaseMountLevel(weldAmount, 0, null, boneAmount, true); if (!block.CanContinueBuild((MyInventory)welder.GetInventory(0)) && !block.IsFullIntegrity) { stalledWelders.Add(welder.EntityId); } } }); foreach (var tool in shipyardItem.Tools) { MySlimBlock targetBlock; Communication.MessageStruct message = new Communication.MessageStruct() { toolId = tool.EntityId, gridId = 0, blockPos = new SerializableVector3I(), packedColor = 0 }; if (!shipyardItem.ProcessBlocks.TryGetValue(tool.EntityId, out targetBlock)) { Communication.SendLine(message); continue; } message.gridId = targetBlock.CubeGrid.EntityId; message.blockPos = targetBlock.Position; if (stalledWelders.Contains(tool.EntityId)) { message.packedColor = Color.Purple.PackedValue; message.pulse = true; } else { message.packedColor = Color.DarkCyan.PackedValue; message.pulse = false; } Communication.SendLine(message); } return(true); }
public static IMyTerminalBlock GetShipController(this IMyFunctionalBlock ftl, string tag = "FTL") { IMyTerminalBlock reference = null; var cockpitObjectBuilder = new MyObjectBuilderType(typeof(MyObjectBuilder_Cockpit)); var rcObjectBuilder = new MyObjectBuilderType(typeof(MyObjectBuilder_RemoteControl)); var cockpitlist = ftl.GetGroupedBlocks(cockpitObjectBuilder); var rclist = ftl.GetGroupedBlocks(rcObjectBuilder); cockpitlist.UnionWith(rclist); if (cockpitlist.Count != 0) { // If it's grouped, just find the first non-passenger seat one foreach (var cockpit in cockpitlist) { var definition = MyDefinitionManager.Static.GetCubeBlockDefinition(cockpit.BlockDefinition); if (definition != null && definition is MyShipControllerDefinition && (definition as MyShipControllerDefinition).EnableShipControl) { //if (Globals.Debug) // MyAPIGateway.Utilities.ShowNotification("Found grouped cockpit: " + cockpit.CustomName, 5000); reference = cockpit; // If this is a main cockpit, stop searching for any others and use this one if (((cockpit as IMyCubeBlock).GetObjectBuilderCubeBlock() as MyObjectBuilder_ShipController).IsMainCockpit) { break; } } } return(reference); // If there were any grouped cockpits, never continue looking } var blocks = new List <IMySlimBlock>(); var filterBlocks = new HashSet <IMySlimBlock>(); // Get a list of cockpit/remote control blocks // Might not work with modded cockpits (ftl.CubeGrid as IMyCubeGrid).GetBlocks(blocks, (x) => x.FatBlock != null && (x.FatBlock.BlockDefinition.TypeId == cockpitObjectBuilder || x.FatBlock.BlockDefinition.TypeId == rcObjectBuilder ) ); // Loop through and filter by non-passenger foreach (var block in blocks) { var definition = MyDefinitionManager.Static.GetCubeBlockDefinition(block.FatBlock.BlockDefinition); if (definition != null && definition is MyShipControllerDefinition && (definition as MyShipControllerDefinition).EnableShipControl) { filterBlocks.Add(block); } } //if (Globals.Debug) // MyAPIGateway.Utilities.ShowNotification("Cockpit count: " + filterBlocks.Count); if (filterBlocks.Count == 1) { //if (Globals.Debug) // MyAPIGateway.Utilities.ShowNotification("Found one cockpit: " + filterBlocks[0].FatBlock.DisplayNameText); // If we only found one cockpit, use that reference = filterBlocks.FirstElement().FatBlock as IMyTerminalBlock; } else if (filterBlocks.Count > 1) { foreach (var block in filterBlocks) { if (block.FatBlock.DisplayNameText.Contains(tag)) { if (Globals.Debug) { MyAPIGateway.Utilities.ShowNotification("Found named cockpit: " + block.FatBlock.DisplayNameText); } reference = block.FatBlock as IMyTerminalBlock; break; } else if (((block.FatBlock as IMyCubeBlock).GetObjectBuilderCubeBlock() as MyObjectBuilder_ShipController).IsMainCockpit) { if (Globals.Debug) { MyAPIGateway.Utilities.ShowNotification("Found main control cockpit: " + block.FatBlock.DisplayNameText); } reference = block.FatBlock as IMyTerminalBlock; break; } else if (((block.FatBlock as IMyCubeBlock).GetObjectBuilderCubeBlock() as MyObjectBuilder_ShipController).ControlThrusters) { if (Globals.Debug) { MyAPIGateway.Utilities.ShowNotification("Found thruster control cockpit: " + block.FatBlock.DisplayNameText); } reference = block.FatBlock as IMyTerminalBlock; } } } return(reference); }
public void Disconnect(MyCubeGrid grid) { ProfilerShort.Begin("Collect+IsInVoxels"); m_largestGroupWithPhysics = default(Group); m_groups.Clear(); m_sortedBlocks.Clear(); m_disconnectHelper.Clear(); foreach (var block in grid.GetBlocks()) { m_disconnectHelper.Add(block); } ProfilerShort.End(); ProfilerShort.Begin("GroupBy"); while (m_disconnectHelper.Count > 0) { Group group = default(Group); group.FirstBlockIndex = m_sortedBlocks.Count; AddNeighbours(m_disconnectHelper.FirstElement(), out group.IsValid); group.BlockCount = m_sortedBlocks.Count - group.FirstBlockIndex; if (group.IsValid && group.BlockCount > m_largestGroupWithPhysics.BlockCount) { if (m_largestGroupWithPhysics.BlockCount > 0) // Is valid { // order matters, insert in correct place int i = 0; for (i = 0; i < m_groups.Count; i++) { if (m_groups[i].FirstBlockIndex > m_largestGroupWithPhysics.FirstBlockIndex) { m_groups.Insert(i, m_largestGroupWithPhysics); break; } } if (i == m_groups.Count) { m_groups.Add(m_largestGroupWithPhysics); } } m_largestGroupWithPhysics = group; } else { m_groups.Add(group); } } ProfilerShort.End(); ProfilerShort.Begin("RemoveLargestGroup"); m_sortedBlocks.RemoveRange(m_largestGroupWithPhysics.FirstBlockIndex, m_largestGroupWithPhysics.BlockCount); for (int i = 0; i < m_groups.Count; i++) { var g = m_groups[i]; if (g.FirstBlockIndex > m_largestGroupWithPhysics.FirstBlockIndex) { g.FirstBlockIndex -= m_largestGroupWithPhysics.BlockCount; m_groups[i] = g; } } ProfilerShort.End(); ProfilerShort.Begin("CreateSplits"); if (m_groups.Count > 0) { MyCubeGrid.CreateSplits(grid, m_sortedBlocks, m_groups); } ProfilerShort.End(); m_groups.Clear(); m_sortedBlocks.Clear(); m_disconnectHelper.Clear(); }