Esempio n. 1
0
        public Program()
        {
            rotor = (IMyMotorBase)GridTerminalSystem.GetBlockWithName("Advanced Rotor");

            pistons = GridTerminalSystem.GetBlockGroupWithName("Crane Pisonts");
            pistons.GetBlocksOfType <IMyPistonBase>(cranePistons);
            horizontal1 = (IMyPistonBase)GridTerminalSystem.GetBlockWithName("Piston Crane Horizontal 1");
            horizontal2 = (IMyPistonBase)GridTerminalSystem.GetBlockWithName("Piston Crane Horizontal 2");
            horizontal3 = (IMyPistonBase)GridTerminalSystem.GetBlockWithName("Piston Crane Horizontal 3");

            vertical1 = (IMyPistonBase)GridTerminalSystem.GetBlockWithName("Piston Crane Vertical 1");
            vertical2 = (IMyPistonBase)GridTerminalSystem.GetBlockWithName("Piston Crane Vertical 2");
            vertical3 = (IMyPistonBase)GridTerminalSystem.GetBlockWithName("Piston Crane Vertical 3");

            Echo("Status");
            Echo("-------------------------------------------");
            Echo("Rotor: " + (rotor == null ? "Not Available" : "Ok"));
            Echo("Crane Pistons: " + (pistons == null ? "Not Available" : "Ok") + " / " + (cranePistons == null ? "Not Available" : "Ok"));
            Echo(" - Horizontal 1: " + (horizontal1 == null ? "Not Available" : "Ok"));
            Echo(" - Horizontal 2: " + (horizontal2 == null ? "Not Available" : "Ok"));
            Echo(" - Horizontal 3: " + (horizontal3 == null ? "Not Available" : "Ok"));
            Echo(" - Vertical 1: " + (vertical1 == null ? "Not Available" : "Ok"));
            Echo(" - Vertical 2: " + (vertical2 == null ? "Not Available" : "Ok"));
            Echo(" - Vertical 3: " + (vertical3 == null ? "Not Available" : "Ok"));
        }
Esempio n. 2
0
            protected override IMyCubeBlock GetPartner()
            {
                IMyMotorBase block = (IMyMotorBase)myBlock;

                if (block.IsAttached)
                {
                    return(block.Top);
                }
                return(null);
            }
Esempio n. 3
0
        public Program()
        {
            rotersR = new List <IMyTerminalBlock>();
            rotersL = new List <IMyTerminalBlock>();


            gunner  = GridTerminalSystem.GetBlockWithName("Azimuth Open Cockpit No Oxygen") as IMyCockpit;
            cockpit = GridTerminalSystem.GetBlockWithName("Azimuth Passenger Seat") as IMyCockpit;
            roter1  = GridTerminalSystem.GetBlockWithName("Rotor Turret") as IMyMotorBase;
            roter2  = GridTerminalSystem.GetBlockWithName("Two-ended Motor") as IMyMotorBase;
            GridTerminalSystem.GetBlockGroupWithName("RotersR").GetBlocks(rotersR);
            GridTerminalSystem.GetBlockGroupWithName("RotersL").GetBlocks(rotersL);
            text = GridTerminalSystem.GetBlockWithName("Text panel") as IMyTextPanel;
            Runtime.UpdateFrequency = UpdateFrequency.Update1;
        }
 public void Main(string argument, UpdateType updateSource)
 {
     if (_solarPanel == null)
     {
         _solarPanel = (IMySolarPanel)GridTerminalSystem.GetBlockWithName("太阳能板-基准");
     }
     if (_motorRotorBottom == null)
     {
         _motorRotorBottom = (IMyMotorBase)GridTerminalSystem.GetBlockWithName("太阳能转子");
     }
     if (_solarPanel.MaxOutput < upSolarPanelPanelMaxOutput)
     {
         _motorRotorBottom.ApplyAction("Reverse");
     }
     upSolarPanelPanelMaxOutput = _solarPanel.MaxOutput;
 }
            private bool control(IMyMotorBase rotorBase, MyContext context)
            {
                IMyMotorStator rotor    = rotorBase as IMyMotorStator;
                float          distance = angleDistance_rads(rotor.Angle, _angle_rads);

                rotor.Enabled = true;
                if (Math.Abs(distance) < _threshold_rads)
                {
                    rotor.RotorLock         = true;
                    rotor.TargetVelocityRPM = 0;
                    return(false);
                }
                else
                {
                    rotor.RotorLock         = false;
                    rotor.TargetVelocityRPM = distance > 0 ? -_speed_rpm : _speed_rpm;
                    return(true);
                }
            }
            private bool control(MyContext context, IMyMotorBase rotorBase)
            {
                IMyMotorStator rotor    = rotorBase as IMyMotorStator;
                float          distance = angleDistance_rads(rotor.Angle, 0);

                rotor.Enabled = true;
                if (Math.Abs(distance) < DIST_THRESHOLD_RADS)
                {
                    rotor.RotorLock         = true;
                    rotor.TargetVelocityRPM = 0;
                    return(false);
                }
                else
                {
                    rotor.RotorLock         = false;
                    rotor.TargetVelocityRPM = distance > 0 ? -0.5f : 0.5f;
                    return(true);
                }
            }
Esempio n. 7
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 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);
                        }
                    }
                }
            }
        }