/// <returns>true iff the entity should be kept</returns> private bool collect_Entities(IMyEntity entity) { if (!(entity is IMyCubeGrid) && !(entity is IMyVoxelMap) && !(entity is IMyFloatingObject)) { return(false); } IMyCubeGrid asGrid = entity as IMyCubeGrid; if (asGrid != null) { if (asGrid == myCubeGrid) { return(false); } if (AttachedGrids.isGridAttached(myCubeGrid, asGrid)) { return(false); } } if (entity.Physics != null && entity.Physics.Mass > 0 && entity.Physics.Mass < 1000) { return(false); } return(true); }
public CanFlyTo(Vector3D destination, Sandbox.ModAPI.IMyCubeGrid gridDest, GridDimensions gridDims, bool isAlternate, bool ignoreAsteroids) { this.myGrid = gridDims.myGrid; this.gridDest = gridDest; this.myAttached = AttachedGrids.getFor(gridDims.myGrid); this.ignoreAsteroids = ignoreAsteroids; myLogger = new Logger(gridDims.myGrid.DisplayName, "CanFlyTo"); collisionSpheres = new Spheres(destination, gridDims, isAlternate); log(myLogger, "got grid dest: " + this.gridDest, ".ctor()", Logger.severity.TRACE); }
public static void doShowoff(IMyCubeBlock showoff, IEnumerator <LastSeen> toDisplay, int toDisplayCount) { LinkedList <Ingame.IMyTextPanel> textPanels = findTextPanel(showoff); if (textPanels == null) { return; } log("building toDisplay", "findTextPanel()", Logger.severity.TRACE); Vector3D myPos = showoff.GetPosition(); List <sortableLastSeen> sortableSeen = new List <sortableLastSeen>(); while (toDisplay.MoveNext()) { IMyCubeGrid grid = toDisplay.Current.Entity as IMyCubeGrid; if (grid == null || AttachedGrids.isGridAttached(grid, showoff.CubeGrid)) { continue; } IMyCubeBlockExtensions.Relations relations = showoff.getRelationsTo(grid, IMyCubeBlockExtensions.Relations.Enemy).mostHostile(); sortableSeen.Add(new sortableLastSeen(myPos, toDisplay.Current, relations)); } sortableSeen.Sort(); int count = 0; StringBuilder displayText = new StringBuilder(); foreach (sortableLastSeen sortable in sortableSeen) { displayText.Append(sortable.ToStringBuilder(count++)); if (count >= 50) { break; } } string displayString = displayText.ToString(); foreach (Ingame.IMyTextPanel panel in textPanels) { log("writing to panel: " + panel.DisplayNameText, "findTextPanel()", Logger.severity.TRACE); panel.WritePublicText(displayString); if (panel.GetPublicTitle() != publicTitle) { panel.WritePublicTitle(publicTitle); panel.AddImageToSelection(radarId); panel.ShowTextureOnScreen(); } } }
/// <summary> /// Find all grids attached to the specified grid, either by piston, rotor, connector or landing gear. /// This will iterate through all attached grids, until all are found. /// </summary> /// <param name="entity"></param> /// <param name="type">Specifies if all attached grids will be found or only grids that are attached either by piston or rotor.</param> /// <returns>A list of all attached grids, including the original.</returns> public static List <IMyCubeGrid> GetAttachedGrids(this IMyEntity entity, AttachedGrids type = AttachedGrids.All) { var cubeGrid = entity as IMyCubeGrid; if (cubeGrid == null) { return(new List <IMyCubeGrid>()); } var results = new List <IMyCubeGrid> { cubeGrid }; GetAttachedGrids(cubeGrid, ref results, type); return(results); }
/// <summary> /// Find all grids attached to the specified grid, either by piston, rotor, connector or landing gear. /// This will iterate through all attached grids, until all are found. /// </summary> /// <param name="entity"></param> /// <param name="type">Specifies if all attached grids will be found or only grids that are attached either by piston or rotor.</param> /// <returns>A list of all attached grids, including the original.</returns> public static List <IMyCubeGrid> GetAttachedGrids(this IMyEntity entity, AttachedGrids type = AttachedGrids.All) { var cubeGrid = entity as IMyCubeGrid; if (cubeGrid == null) { return(new List <IMyCubeGrid>()); } switch (type) { case AttachedGrids.Static: // Should include connections via: Rotors, Pistons, Suspension. return(MyAPIGateway.GridGroups.GetGroup(cubeGrid, GridLinkTypeEnum.Mechanical)); case AttachedGrids.All: default: // Should include connections via: Landing Gear, Connectors, Rotors, Pistons, Suspension. return(MyAPIGateway.GridGroups.GetGroup(cubeGrid, GridLinkTypeEnum.Physical)); } }
private static void GetAttachedGrids(IMyCubeGrid cubeGrid, ref List <IMyCubeGrid> results, AttachedGrids type) { if (cubeGrid == null) { return; } var blocks = new List <IMySlimBlock>(); cubeGrid.GetBlocks(blocks, b => b != null && b.FatBlock != null && !b.FatBlock.BlockDefinition.TypeId.IsNull); foreach (var block in blocks) { //MyAPIGateway.Utilities.ShowMessage("Block", string.Format("{0}", block.FatBlock.BlockDefinition.TypeId)); if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorAdvancedStator) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorStator) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorSuspension) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorBase)) { // The MotorStator which inherits from MotorBase. IMyMotorBase motorBase = block.FatBlock as IMyMotorBase; if (motorBase == null || motorBase.Top == null) { continue; } IMyCubeGrid entityParent = motorBase.TopGrid; if (entityParent == null) { continue; } if (!results.Any(e => e.EntityId == entityParent.EntityId)) { results.Add(entityParent); GetAttachedGrids(entityParent, ref results, type); } } else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorAdvancedRotor) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorRotor) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_RealWheel) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_Wheel)) { // The Rotor Part. IMyMotorRotor motorRotor = block.FatBlock as IMyMotorRotor; IMyCubeGrid entityParent = null; if (motorRotor == null || motorRotor.Base == null) { // Wheels appear to not properly populate the Stator property. IMyCubeBlock altBlock = Support.FindRotorBase(motorRotor.EntityId); if (altBlock == null) { continue; } entityParent = altBlock.CubeGrid; } else { entityParent = motorRotor.Base.CubeGrid; } if (!results.Any(e => e.EntityId == entityParent.EntityId)) { results.Add(entityParent); GetAttachedGrids(entityParent, ref results, type); } } else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_PistonTop)) { // The Piston Top. IMyPistonTop pistonTop = block.FatBlock as IMyPistonTop; if (pistonTop == null || pistonTop.Piston == null) { continue; } IMyCubeGrid entityParent = pistonTop.Piston.CubeGrid; if (!results.Any(e => e.EntityId == entityParent.EntityId)) { results.Add(entityParent); GetAttachedGrids(entityParent, ref results, type); } } else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_ExtendedPistonBase) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_PistonBase)) { IMyPistonBase pistonBase = block.FatBlock as IMyPistonBase; if (pistonBase == null || pistonBase.Top == null) { continue; } IMyCubeGrid entityParent = pistonBase.TopGrid; if (entityParent == null) { continue; } if (!results.Any(e => e.EntityId == entityParent.EntityId)) { results.Add(entityParent); GetAttachedGrids(entityParent, ref results, type); } } else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_ShipConnector) && type == AttachedGrids.All) { var connector = (IMyShipConnector)block.FatBlock; if (connector.Status != Sandbox.ModAPI.Ingame.MyShipConnectorStatus.Connected || connector.OtherConnector == null) { continue; } var otherGrid = (IMyCubeGrid)connector.OtherConnector.CubeGrid; if (!results.Any(e => e.EntityId == otherGrid.EntityId)) { results.Add(otherGrid); GetAttachedGrids(otherGrid, ref results, type); } } // Commented out temporarily, as it isn't been used, but may not work under 1.126. //else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_LandingGear) && type == AttachedGrids.All) //{ // var landingGear = (IMyLandingGear)block.FatBlock; // if (landingGear.IsLocked == false) // continue; // var entity = landingGear.GetAttachedEntity(); // if (entity == null || !(entity is IMyCubeGrid)) // continue; // var otherGrid = (IMyCubeGrid)entity; // if (!results.Any(e => e.EntityId == otherGrid.EntityId)) // { // results.Add(otherGrid); // GetAttachedGrids(otherGrid, ref results, type); // } //} } // Loop through all other grids, find their Landing gear, and figure out if they are attached to <cubeGrid>. var allShips = new HashSet <IMyEntity>(); var checkList = results; // cannot use ref paramter in Lambada expression!?!. MyAPIGateway.Entities.GetEntities(allShips, e => e is IMyCubeGrid && !checkList.Contains(e)); //if (type == AttachedGrids.All) //{ // foreach (IMyCubeGrid ship in allShips) // { // blocks = new List<IMySlimBlock>(); // ship.GetBlocks(blocks, // b => // b != null && b.FatBlock != null && !b.FatBlock.BlockDefinition.TypeId.IsNull && // b.FatBlock is IMyLandingGear); // foreach (var block in blocks) // { // var landingGear = (IMyLandingGear)block.FatBlock; // if (landingGear.IsLocked == false) // continue; // var entity = landingGear.GetAttachedEntity(); // if (entity == null || entity.EntityId != cubeGrid.EntityId) // continue; // if (!results.Any(e => e.EntityId == ship.EntityId)) // { // results.Add(ship); // GetAttachedGrids(ship, ref results, type); // } // } // } //} }
/// <summary> /// Display text either for player or for program /// </summary> private void displayLastSeen() { bool forProgram; string instruction = myCubeBlock.getInstructions(); if (instruction == null) { return; } if (instruction.looseContains(command_forProgram)) { if (sentToProgram && myTextPanel.GetPublicTitle() == publicTitle_forProgram && !string.IsNullOrWhiteSpace(myTextPanel.GetPublicText())) { //myLogger.debugLog("public text is not clear", "displayLastSeen()"); runProgram(); return; } forProgram = true; } else if (instruction.looseContains(command_forPlayer)) { forProgram = false; } else { return; } if (!findAntenna()) { return; } IEnumerator <LastSeen> toDisplay = myAntenna.getLastSeenEnum(); if (forProgram) { myLogger.debugLog("building display list for program", "informPlayer()", Logger.severity.TRACE); } else { myLogger.debugLog("building display list for player", "informPlayer()", Logger.severity.TRACE); } Vector3D myPos = myCubeBlock.GetPosition(); List <sortableLastSeen> sortableSeen = new List <sortableLastSeen>(); while (toDisplay.MoveNext()) { IMyCubeGrid grid = toDisplay.Current.Entity as IMyCubeGrid; if (grid == null || AttachedGrids.isGridAttached(grid, myCubeBlock.CubeGrid)) { continue; } IMyCubeBlockExtensions.Relations relations = myCubeBlock.getRelationsTo(grid, IMyCubeBlockExtensions.Relations.Enemy).mostHostile(); sortableSeen.Add(new sortableLastSeen(myPos, toDisplay.Current, relations)); } sortableSeen.Sort(); int count = 0; StringBuilder displayText = new StringBuilder(); if (forProgram) { foreach (sortableLastSeen sortable in sortableSeen) { displayText.Append(sortable.TextForProgram()); } } else { displayText.Append(timeString); displayText.Append(DateTime.Now.ToLongTimeString()); writeTime = DateTime.Now; displayText.Append('\n'); foreach (sortableLastSeen sortable in sortableSeen) { displayText.Append(sortable.TextForPlayer(count++)); if (count >= 50) { break; } } } string displayString = displayText.ToString(); myLogger.debugLog("writing to panel " + myTextPanel.DisplayNameText, "findTextPanel()", Logger.severity.TRACE); myTextPanel.WritePublicText(displayText.ToString()); // set public title if (forProgram) { if (myTextPanel.GetPublicTitle() != publicTitle_forProgram) { myTextPanel.WritePublicTitle(publicTitle_forProgram); myTextPanel.AddImageToSelection(radarIconId); //myTextPanel.ShowTextureOnScreen(); } runProgram(); } else if (myTextPanel.GetPublicTitle() != publicTitle_forPlayer) { myTextPanel.WritePublicTitle(publicTitle_forPlayer); myTextPanel.AddImageToSelection(radarIconId); //myTextPanel.ShowTextureOnScreen(); } }
private static void GetAttachedGrids(IMyCubeGrid cubeGrid, ref List <IMyCubeGrid> results, AttachedGrids type) { if (cubeGrid == null) { return; } var blocks = new List <IMySlimBlock>(); cubeGrid.GetBlocks(blocks, b => b != null && b.FatBlock != null && !b.FatBlock.BlockDefinition.TypeId.IsNull); foreach (var block in blocks) { //MyAPIGateway.Utilities.ShowMessage("Block", string.Format("{0}", block.FatBlock.BlockDefinition.TypeId)); if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorAdvancedStator) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorStator) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorSuspension) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorBase)) { // The MotorStator which inherits from MotorBase. var motorBase = block.GetObjectBuilder() as MyObjectBuilder_MotorBase; if (motorBase == null || !motorBase.RotorEntityId.HasValue || motorBase.RotorEntityId.Value == 0 || !MyAPIGateway.Entities.EntityExists(motorBase.RotorEntityId.Value)) { continue; } var entityParent = MyAPIGateway.Entities.GetEntityById(motorBase.RotorEntityId.Value).Parent as IMyCubeGrid; if (entityParent == null) { continue; } if (!results.Any(e => e.EntityId == entityParent.EntityId)) { results.Add(entityParent); GetAttachedGrids(entityParent, ref results, type); } } else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorAdvancedRotor) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_MotorRotor) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_RealWheel) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_Wheel)) { // The Rotor Part. var motorCube = Support.FindRotorBase(block.FatBlock.EntityId); if (motorCube == null) { continue; } var entityParent = (IMyCubeGrid)motorCube.Parent; if (!results.Any(e => e.EntityId == entityParent.EntityId)) { results.Add(entityParent); GetAttachedGrids(entityParent, ref results, type); } } else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_PistonTop)) { // The Piston Top. var pistonCube = Support.FindPistonBase(block.FatBlock.EntityId); if (pistonCube == null) { continue; } var entityParent = (IMyCubeGrid)pistonCube.Parent; if (!results.Any(e => e.EntityId == entityParent.EntityId)) { results.Add(entityParent); GetAttachedGrids(entityParent, ref results, type); } } else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_ExtendedPistonBase) || block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_PistonBase)) { var pistonBase = block.GetObjectBuilder() as MyObjectBuilder_PistonBase; if (pistonBase == null || pistonBase.TopBlockId == 0 || !MyAPIGateway.Entities.EntityExists(pistonBase.TopBlockId)) { continue; } var entityParent = MyAPIGateway.Entities.GetEntityById(pistonBase.TopBlockId).Parent as IMyCubeGrid; if (entityParent == null) { continue; } if (!results.Any(e => e.EntityId == entityParent.EntityId)) { results.Add(entityParent); GetAttachedGrids(entityParent, ref results, type); } } else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_ShipConnector) && type == AttachedGrids.All) { // There isn't a non-Ingame interface for IMyShipConnector at this time. var connector = (Sandbox.ModAPI.Ingame.IMyShipConnector)block.FatBlock; if (connector.IsConnected == false || connector.IsLocked == false || connector.OtherConnector == null) { continue; } var otherGrid = (IMyCubeGrid)connector.OtherConnector.CubeGrid; if (!results.Any(e => e.EntityId == otherGrid.EntityId)) { results.Add(otherGrid); GetAttachedGrids(otherGrid, ref results, type); } } else if (block.FatBlock.BlockDefinition.TypeId == typeof(MyObjectBuilder_LandingGear) && type == AttachedGrids.All) { var landingGear = (IMyLandingGear)block.FatBlock; if (landingGear.IsLocked == false) { continue; } var entity = landingGear.GetAttachedEntity(); if (entity == null || !(entity is IMyCubeGrid)) { continue; } var otherGrid = (IMyCubeGrid)entity; if (!results.Any(e => e.EntityId == otherGrid.EntityId)) { results.Add(otherGrid); GetAttachedGrids(otherGrid, ref results, type); } } } // Loop through all other grids, find their Landing gear, and figure out if they are attached to <cubeGrid>. var allShips = new HashSet <IMyEntity>(); var checkList = results; // cannot use ref paramter in Lambada expression!?!. MyAPIGateway.Entities.GetEntities(allShips, e => e is IMyCubeGrid && !checkList.Contains(e)); if (type == AttachedGrids.All) { foreach (IMyCubeGrid ship in allShips) { blocks = new List <IMySlimBlock>(); ship.GetBlocks(blocks, b => b != null && b.FatBlock != null && !b.FatBlock.BlockDefinition.TypeId.IsNull && b.FatBlock is IMyLandingGear); foreach (var block in blocks) { var landingGear = (IMyLandingGear)block.FatBlock; if (landingGear.IsLocked == false) { continue; } var entity = landingGear.GetAttachedEntity(); if (entity == null || entity.EntityId != cubeGrid.EntityId) { continue; } if (!results.Any(e => e.EntityId == ship.EntityId)) { results.Add(ship); GetAttachedGrids(ship, ref results, type); } } } } }
/// <summary> /// If attached to final destination, send message to it. Otherwise sends Message to all attached friendly antennae. /// removes invalids from the list /// </summary> /// <param name="additional">also sends to this collection, without testing</param> public static void sendToAttached(IMyCubeBlock sender, ICollection <Message> toSend) { LinkedList <Message> removeList = new LinkedList <Message>(); foreach (Message mes in toSend) { if (mes.isValid) { if (AttachedGrids.isGridAttached(sender.CubeGrid, mes.DestCubeBlock.CubeGrid)) { // get receiver for block RemoteControl remote; if (RemoteControl.registry.TryGetValue(mes.DestCubeBlock, out remote)) { remote.receive(mes); mes.isValid = false; removeList.AddLast(mes); } else { ProgrammableBlock progBlock; if (ProgrammableBlock.registry.TryGetValue(mes.DestCubeBlock, out progBlock)) { progBlock.receive(mes); mes.isValid = false; removeList.AddLast(mes); } } } } else // not valid { removeList.AddLast(mes); } } foreach (Message mes in removeList) { toSend.Remove(mes); } // to radio antenna foreach (RadioAntenna radioAnt in RadioAntenna.registry) { if (sender.canSendTo(radioAnt.CubeBlock, true)) { foreach (Message mes in toSend) { radioAnt.receive(mes); } } } // to laser antenna foreach (LaserAntenna laserAnt in LaserAntenna.registry) { if (sender.canSendTo(laserAnt.CubeBlock, true)) { foreach (Message mes in toSend) { laserAnt.receive(mes); } } } }