private static IMyPistonTop FindTop(IMyPistonBase block)
        {
            if (block.CubeGrid == null)
            {
                return(null);
            }

            MatrixD topMatrix;

            if (!GetTopMatrix(block, block.CubeGrid.GridSizeEnum, out topMatrix))
            {
                return(null);
            }

            Vector3D        pos  = topMatrix.Translation;
            BoundingSphereD area = new BoundingSphereD(pos, 1);

            foreach (IMyEntity e in MyAPIGateway.Entities.GetTopMostEntitiesInSphere(ref area))
            {
                IMyCubeGrid g = e as IMyCubeGrid;
                if (g != null)
                {
                    foreach (IMySlimBlock slim in g.GetBlocksInsideSphere(ref area))
                    {
                        IMyPistonTop top = slim.FatBlock as IMyPistonTop;
                        if (top != null && (top.Base == null || top.Base.Top == null) && TrySetDir(top, block, out top))
                        {
                            return(top);
                        }
                    }
                }
            }
            return(null);
        }
 public static void Attach(IMyPistonBase block)
 {
     if (!block.IsAttached)
     {
         IMyPistonTop top = FindTop(block);
         if (top != null)
         {
             block.Attach(top, true);
         }
         else
         {
             block.Attach();
         }
         MyAPIGateway.Utilities.InvokeOnGameThread(() => FinalizeAttach(block));
     }
 }
        private static bool TrySetDir(IMyPistonTop top, IMyPistonBase piston, out IMyPistonTop result)
        {
            result = top;
            if (((MyCubeGrid)top.CubeGrid).BlocksCount == 1)
            {
                // Top is the only block on the grid, the piston can safely set its orientation.
                return(true);
            }

            // Get directions that the top should have.
            Matrix m = WorldToLocal(piston.WorldMatrix, top.CubeGrid.WorldMatrix);

            Base6Directions.Direction forward = Base6Directions.GetClosestDirection(m.Forward);
            Base6Directions.Direction up      = Base6Directions.GetClosestDirection(m.Up);

            if (top.Orientation.Up != up)
            {
                // Top must be facing up.
                result = null;
                return(false);
            }

            if (top.Orientation.Forward == forward)
            {
                // Top is already in the correct orientation.
                return(true);
            }

            // Replace the top with one that is in the correct orientation.
            MyObjectBuilder_PistonTop ob = (MyObjectBuilder_PistonTop)top.GetObjectBuilderCubeBlock(true);

            ob.BlockOrientation = new SerializableBlockOrientation(forward, up);
            top.CubeGrid.RemoveBlock(top.SlimBlock);
            ((MyCubeGrid)top.CubeGrid).SendRemovedBlocks();
            ob.EntityId = 0;
            result      = top.CubeGrid.AddBlock(ob, false)?.FatBlock as IMyPistonTop;
            if (result != null)
            {
                ob.EntityId = result.EntityId;
                new AddBlockPacket(top.CubeGrid, ob).SendToOthers();
                return(true);
            }
            return(false);
        }
        private static void OnSmallTopCreated(IMyPistonBase block, List <IMyCubeGrid> temp)
        {
            IMyCubeGrid topGrid = temp.FirstOrDefault();

            if (topGrid == null)
            {
                return;
            }

            IMyPistonTop top = topGrid.GetCubeBlock(Vector3I.Zero)?.FatBlock as IMyPistonTop;

            if (top == null)
            {
                MyAPIGateway.Entities.MarkForClose(topGrid);
            }
            else
            {
                block.Attach(top, true);
                MyAPIGateway.Utilities.InvokeOnGameThread(() => FinalizeAttach(block));
            }
        }
Exemple #5
0
        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);
            //            }
            //        }
            //    }
            //}
        }
        private static void GetAttachedGrids(IMyCubeGrid cubeGrid, ref List <IMyCubeGrid> results)
        {
            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);
                    }
                }
                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;
                    if (motorRotor == null || motorRotor.Base == null)
                    {
                        continue;
                    }

                    IMyCubeGrid entityParent = motorRotor.Base.CubeGrid;
                    if (!results.Any(e => e.EntityId == entityParent.EntityId))
                    {
                        results.Add(entityParent);
                        GetAttachedGrids(entityParent, ref results);
                    }
                }
                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);
                    }
                }
                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);
                    }
                }
            }
        }
        private static bool GetAttachedGridsLoopBlocks(IMySlimBlock slim) // should always return false!
        {
            IMyCubeBlock block = slim.FatBlock;

            if (block == null)
            {
                return(false);
            }

            //if(Constants.CLEANUP_CONNECTOR_CONNECTED)
            //{
            //	IMyShipConnector connector = block as IMyShipConnector;

            //	if(connector != null)
            //	{
            //		IMyCubeGrid otherGrid = connector.OtherConnector?.CubeGrid;

            //		if(otherGrid != null && !grids.Contains(otherGrid))
            //		{
            //			grids.Add(otherGrid);
            //			RecursiveGetAttachedGrids(otherGrid);
            //		}

            //		return false;
            //	}
            //}

            IMyMotorStator rotorBase = block as IMyMotorStator;

            if (rotorBase != null)
            {
                IMyCubeGrid otherGrid = rotorBase.TopGrid;

                if (otherGrid == null || Grids.Contains(otherGrid))
                {
                    return(false);
                }
                Grids.Add(otherGrid);
                RecursiveGetAttachedGrids(otherGrid);

                return(false);
            }

            IMyMotorRotor rotorTop = block as IMyMotorRotor;

            if (rotorTop != null)
            {
                IMyCubeGrid otherGrid = rotorTop.Base?.CubeGrid;

                if (otherGrid == null || Grids.Contains(otherGrid))
                {
                    return(false);
                }
                Grids.Add(otherGrid);
                RecursiveGetAttachedGrids(otherGrid);

                return(false);
            }

            IMyPistonBase pistonBase = block as IMyPistonBase;

            if (pistonBase != null)
            {
                IMyCubeGrid otherGrid = pistonBase.TopGrid;

                if (otherGrid == null || Grids.Contains(otherGrid))
                {
                    return(false);
                }
                Grids.Add(otherGrid);
                RecursiveGetAttachedGrids(otherGrid);

                return(false);
            }

            IMyPistonTop pistonTop = block as IMyPistonTop;

            if (pistonTop == null)
            {
                return(false);
            }
            {
                IMyCubeGrid otherGrid = pistonTop.Piston?.CubeGrid;
                if (otherGrid == null || Grids.Contains(otherGrid))
                {
                    return(false);
                }
                Grids.Add(otherGrid);
                RecursiveGetAttachedGrids(otherGrid);
                return(false);
            }
        }
        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.Rotor == null)
                    {
                        continue;
                    }

                    IMyCubeGrid entityParent = motorBase.RotorGrid;
                    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;
                    if (motorRotor == null || motorRotor.Stator == null)
                    {
                        continue;
                    }

                    IMyCubeGrid entityParent = motorRotor.Stator.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.IsConnected == false || connector.IsLocked == false || connector.OtherConnector == null)
                    {
                        continue;
                    }

                    var otherGrid = 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);
                        }
                    }
                }
            }
        }