Exemplo n.º 1
0
            public static void AnnounceConnect(MyShipConnector masterConnector, MyShipConnector slaveConnector, ref Matrix masterMatrix, ref Matrix slaveMatrix)
            {
                var msg = new ConnectMsg();

                msg.MasterEntityId = masterConnector.EntityId;
                msg.SlaveEntityId = slaveConnector.EntityId;

                msg.MasterForward = masterMatrix.Forward;
                msg.MasterUp = masterMatrix.Up;
                msg.MasterTranslation = masterMatrix.Translation;
                msg.SlaveForward = slaveMatrix.Forward;
                msg.SlaveUp = slaveMatrix.Up;
                msg.SlaveTranslation = slaveMatrix.Translation;

                Sync.Layer.SendMessageToAll(ref msg);
            }
Exemplo n.º 2
0
        private void ConnectInternal(ref Matrix thisMatrix, ref Matrix otherMatrix, MyShipConnector otherConnector, bool constructor)
        {
            Debug.Assert(!m_attachableConveyorEndpoint.AlreadyAttached());
            if (m_attachableConveyorEndpoint.AlreadyAttached()) m_attachableConveyorEndpoint.DetachAll();

            m_attachableConveyorEndpoint.Attach(otherConnector.m_attachableConveyorEndpoint);

            var data = new HkFixedConstraintData();
            data.SetInWorldSpace(ref thisMatrix, ref otherMatrix, ref thisMatrix);
            var newConstraint = new HkConstraint(CubeGrid.Physics.RigidBody, otherConnector.CubeGrid.Physics.RigidBody, data);

            this.Connected = true;
            this.Master = true;
            otherConnector.Connected = true;
            otherConnector.Master = false;
            if (!constructor)
            {
                this.ChangeConstraint(otherConnector, newConstraint);
                otherConnector.ChangeConstraint(this, newConstraint);
            }
            else
            {
                this.SetConstraint(otherConnector, newConstraint);
                otherConnector.SetConstraint(this, newConstraint);
            }

            AddConstraint(newConstraint);

            if (CubeGrid != otherConnector.CubeGrid)
            {
                this.OnConstraintAdded(GridLinkTypeEnum.Logical, otherConnector.CubeGrid);
                this.OnConstraintAdded(GridLinkTypeEnum.Physical, otherConnector.CubeGrid);
            }
        }
Exemplo n.º 3
0
        private void ConnectNosync(ref Matrix localSpaceA, ref Matrix localSpaceB, MyShipConnector otherConnector)
        {
            Debug.Assert(!Connected);

            if (m_constraint != null)
            {
                RemoveConstraint(otherConnector, m_constraint);
            }

            ConnectInternal(ref localSpaceA, ref localSpaceB, otherConnector);
        }
Exemplo n.º 4
0
        private void UnsetConstraint()
        {
            Debug.Assert(InConstraint);

            m_other = null;
            m_constraint = null;

            UpdateEmissivity();
        }
Exemplo n.º 5
0
        private void ConnectOnInit(MyShipConnector otherConnector)
        {
            // This can happen if the other grid failed deserializaing during load, but only after the connector was deserialized
            if (otherConnector.CubeGrid.Physics == null) return;

            Matrix thisMatrix = this.CubeGrid.Physics.RigidBody.GetRigidBodyMatrix();
            Matrix otherMatrix = otherConnector.CubeGrid.Physics.RigidBody.GetRigidBodyMatrix();

            ConnectInternal(ref thisMatrix, ref otherMatrix, otherConnector, constructor: true);
        }
Exemplo n.º 6
0
        private static MyShipConnector TryFindConnectorInGrid(ref BoundingSphereD sphere, MyCubeGrid grid, MyShipConnector thisConnector = null)
        {
            m_tmpBlockSet.Clear();
            grid.GetBlocksInsideSphere(ref sphere, m_tmpBlockSet);

            foreach (var block in m_tmpBlockSet)
            {
                if (block.FatBlock == null || !(block.FatBlock is MyShipConnector)) continue;

                var connector = block.FatBlock as MyShipConnector;
                if (connector.InConstraint) continue;
                if (connector == thisConnector) continue;
                if (!connector.IsWorking) continue;
                if (!connector.FriendlyWithBlock(thisConnector)) continue;

                m_tmpBlockSet.Clear();
                return connector;
            }

            m_tmpBlockSet.Clear();
            return null;
        }
Exemplo n.º 7
0
        private void CreateConstraintInit(MyShipConnector otherConnector)
        {
            var posA = ConstraintPositionInGridSpace();
            var posB = otherConnector.ConstraintPositionInGridSpace();
            var axisA = ConstraintAxisGridSpace();
            var axisB = -otherConnector.ConstraintAxisGridSpace();

            CreateConstraintNosync(otherConnector, ref posA, ref posB, ref axisA, ref axisB);
        }
 protected float GetEffectiveStrength(MyShipConnector otherConnector)
 {
     float strength = 0.0f;
     if (!IsReleasing)
     {
         strength = Math.Min(Strength, otherConnector.Strength);
         if (strength <= MinStrength)
             strength = 0.0000001f; // Must be > 0
     }
     return strength;
 }
        private void UpdateConnectionState()
        {
            if (!IsMaster)
                return;

            var state = m_connectionState.Value;

            // Make sure constraints correctly represents ConnectionState.
            if (state.OtherEntityId == 0) // Detached
            {
                if (InConstraint)
                {
                    Detach(false);
                }
            }
            else if (state.MasterToSlave == null) // Aproaching
            {
                if (Connected || (InConstraint && m_other.EntityId != state.OtherEntityId))
                {
                    // Detach when connected or aproaching something else
                    Detach(false);
                }

                MyShipConnector connector;
                if (!InConstraint && MyEntities.TryGetEntityById<MyShipConnector>(state.OtherEntityId, out connector) && connector.FriendlyWithBlock(this) && connector.Closed == false && connector.MarkedForClose == false)
                {
                    if (Physics != null && connector.Physics != null)
                    {
                        if (Sync.IsServer == false && state.MasterToSlaveGrid.HasValue)
                        {
                            CubeGrid.WorldMatrix = MatrixD.Multiply(state.MasterToSlaveGrid.Value, connector.WorldMatrix);
                        }
                        CreateConstraintNosync(connector);
                        UpdateEmissivity();
                        m_other.UpdateEmissivity();
                    }
                }

            }
            else // Connected
            {
                if (Connected && m_other.EntityId != state.OtherEntityId)
                {
                    Detach(false);
                }

                MyShipConnector connector;
                if (!Connected && MyEntities.TryGetEntityById<MyShipConnector>(state.OtherEntityId, out connector) && connector.FriendlyWithBlock(this))
                {
                    m_other = connector;
                    var masterToSlave = state.MasterToSlave;
                    if (masterToSlave.HasValue && masterToSlave.Value.IsZero)
                    {
                        // Special case when deserializing old saves with connected connector (old saves does not have MasterToSlave transform...it's zero)
                        masterToSlave = null;
                    }

                    if (Sync.IsServer == false && state.MasterToSlaveGrid.HasValue)
                    {
                        this.CubeGrid.WorldMatrix = MatrixD.Multiply(state.MasterToSlaveGrid.Value, connector.WorldMatrix);
                    }
                    Weld(masterToSlave);
                }
            }
        }
        private void RemoveConstraint(MyShipConnector otherConnector, HkConstraint constraint)
        {
            if (this.m_hasConstraint)
            {
                if (CubeGrid.Physics != null)
                    CubeGrid.Physics.RemoveConstraint(constraint);

                m_hasConstraint = false;
            }
            else
            {
                if  (otherConnector.CubeGrid.Physics != null)
                    otherConnector.CubeGrid.Physics.RemoveConstraint(constraint);

                otherConnector.m_hasConstraint = false;
            }
            constraint.Dispose();
        }
        //connectors priority : static grid is always slave to dynamic
        // large grid is always slave to small grid
        // if grid size or physical state is same than higher entityId will be master
        // this is computed only first time when approaching and set only by server
        MyShipConnector GetMaster(MyShipConnector first, MyShipConnector second)
        {
            MyCubeGrid firstGrid = first.CubeGrid;
            MyCubeGrid secondGrid = second.CubeGrid;

            //static grids are always slaves
            if (firstGrid.IsStatic != secondGrid.IsStatic)
            {
                if (firstGrid.IsStatic)
                {
                    return second;
                }

                if (secondGrid.IsStatic)
                {
                    return first;
                }
            }
            else if (firstGrid.GridSize != secondGrid.GridSize)
            {
                if (firstGrid.GridSizeEnum == MyCubeSize.Large)
                {
                    return second;
                }

                if (secondGrid.GridSizeEnum == MyCubeSize.Large)
                {
                    return first;
                }

            }

            return first.EntityId < second.EntityId ? second : first; 
        }
 public MyDebugRenderCompoonentShipConnector(MyShipConnector shipConnector)
     : base(shipConnector)
 {
     m_shipConnector = shipConnector;
 }
Exemplo n.º 13
0
 public ConnectorWrapper( MySlimBlock block )
     : base(block)
 {
     Block = (MyShipConnector)block.FatBlock;
 }
Exemplo n.º 14
0
        private void ConnectInternal(ref Matrix localSpaceA, ref Matrix localSpaceB, MyShipConnector otherConnector)
        {
            Debug.Assert(EntityId > otherConnector.EntityId, "Constraints should be created only master (entity with higher EntityId)");
            Debug.Assert(!m_attachableConveyorEndpoint.AlreadyAttached());
            if (m_attachableConveyorEndpoint.AlreadyAttached()) m_attachableConveyorEndpoint.DetachAll();

            m_attachableConveyorEndpoint.Attach(otherConnector.m_attachableConveyorEndpoint);

            var data = new HkFixedConstraintData();
            data.SetInBodySpace(localSpaceA, localSpaceB, CubeGrid.Physics, otherConnector.CubeGrid.Physics);
            var newConstraint = new HkConstraint(CubeGrid.Physics.RigidBody, otherConnector.CubeGrid.Physics.RigidBody, data);

            this.Connected = true;
            otherConnector.Connected = true;
            this.SetConstraint(otherConnector, newConstraint);
            otherConnector.SetConstraint(this, newConstraint);

            AddConstraint(newConstraint);

            if (CubeGrid != otherConnector.CubeGrid)
            {
                this.OnConstraintAdded(GridLinkTypeEnum.Logical, otherConnector.CubeGrid);
                this.OnConstraintAdded(GridLinkTypeEnum.Physical, otherConnector.CubeGrid);
            }
            CubeGrid.OnPhysicsChanged += CubeGrid_OnPhysicsChanged;
        }
Exemplo n.º 15
0
            public static void RequestDetach(MyShipConnector connector)
            {
                var msg = new DetachMsg();
                msg.MasterEntityId = connector.EntityId;

                Sync.Layer.SendMessageToServer(ref msg, MyTransportMessageEnum.Request);
            }
        private void CreateConstraint(MyShipConnector otherConnector)
        {
            CreateConstraintNosync(otherConnector);
            if (Sync.IsServer)
            {
                MatrixD masterToSlave = CubeGrid.WorldMatrix * MatrixD.Invert(m_other.WorldMatrix);

                m_connectionState.Value = new State() {IsMaster = true, OtherEntityId = otherConnector.EntityId, MasterToSlave = null,MasterToSlaveGrid = masterToSlave};
                otherConnector.m_connectionState.Value = new State() { IsMaster = false, OtherEntityId = EntityId, MasterToSlave = null};
            }
        }
Exemplo n.º 17
0
            public static void AnnounceDetach(MyShipConnector connector)
            {
                var msg = new DetachMsg();
                msg.MasterEntityId = connector.EntityId;

                Sync.Layer.SendMessageToAll(ref msg, MyTransportMessageEnum.Success);
            }
        private void CreateConstraintNosync(MyShipConnector otherConnector)
        {
            Debug.Assert(IsMaster, "Constraints should be created only master (entity with higher EntityId)");

            var posA = ConstraintPositionInGridSpace();
            var posB = otherConnector.ConstraintPositionInGridSpace();
            var axisA = ConstraintAxisGridSpace();
            var axisB = -otherConnector.ConstraintAxisGridSpace();

            var data = new HkHingeConstraintData();
            data.SetInBodySpace(posA, posB, axisA, axisB, CubeGrid.Physics, otherConnector.CubeGrid.Physics);
            var data2 = new HkMalleableConstraintData();
            data2.SetData(data);
            data.ClearHandle();
            data = null;
            data2.Strength = GetEffectiveStrength(otherConnector);

            var newConstraint = new HkConstraint(CubeGrid.Physics.RigidBody, otherConnector.CubeGrid.Physics.RigidBody, data2);
            SetConstraint(otherConnector, newConstraint);
            otherConnector.SetConstraint(this, newConstraint);

            AddConstraint(newConstraint);
        }
Exemplo n.º 19
0
        private void CreateConstraint(MyShipConnector otherConnector)
        {
            var posA = ConstraintPositionInGridSpace();
            var posB = otherConnector.ConstraintPositionInGridSpace();
            var axisA = ConstraintAxisGridSpace();
            var axisB = -otherConnector.ConstraintAxisGridSpace();

            CreateConstraintNosync(otherConnector, ref posA, ref posB, ref axisA, ref axisB);
            MySyncShipConnector.AnnounceApproach(this, otherConnector);
        }
 private void SetConstraint(MyShipConnector other, HkConstraint newConstraint)
 {
     m_other = other;
     m_constraint = newConstraint;
     UpdateEmissivity();
 }
Exemplo n.º 21
0
        private void CreateConstraintNosync(MyShipConnector otherConnector, ref Vector3 posA, ref Vector3 posB, ref Vector3 axisA, ref Vector3 axisB)
        {
            var data = new HkHingeConstraintData();
            data.SetInBodySpace(ref posA, ref posB, ref axisA, ref axisB);
            var data2 = new HkMalleableConstraintData();
            data2.SetData(data);
            data.ClearHandle();
            data = null;
            data2.Strength = 0.0003f;

            var newConstraint = new HkConstraint(CubeGrid.Physics.RigidBody, otherConnector.CubeGrid.Physics.RigidBody, data2);
            this.Master = true;
            otherConnector.Master = false;
            SetConstraint(otherConnector, newConstraint);
            otherConnector.SetConstraint(this, newConstraint);

            AddConstraint(newConstraint);
        }
Exemplo n.º 22
0
            public static void SendChangePropertyMessage(bool newValue, MyShipConnector block, Properties property)
            {
                var msg = new ChangePropertyMsg();
                msg.EntityId = block.EntityId;
                msg.Value = newValue;
                msg.Property = property;

                if (Sync.IsServer)
                {
                    Sync.Layer.SendMessageToAllAndSelf(ref msg, MyTransportMessageEnum.Success);
                }
                else
                {
                    Sync.Layer.SendMessageToServer(ref msg, MyTransportMessageEnum.Request);
                }
            }
Exemplo n.º 23
0
        private void ChangeConstraint(MyShipConnector other, HkConstraint newConstraint)
        {
            Debug.Assert(InConstraint);

            m_other = other;
            m_constraint = newConstraint;

            UpdateEmissivity();
        }
Exemplo n.º 24
0
            public static void AnnounceApproach(MyShipConnector thisConnector, MyShipConnector otherConnector)
            {
                var msg = new ApproachMsg();
                msg.EntityId = thisConnector.EntityId;
                msg.OtherEntityId = otherConnector.EntityId;

                Sync.Layer.SendMessageToAll(msg);
            }
Exemplo n.º 25
0
        private void ConnectNosync(ref Matrix thisMatrix, ref Matrix otherMatrix, MyShipConnector otherConnector)
        {
            Debug.Assert(m_other == otherConnector);
            Debug.Assert(InConstraint);
            Debug.Assert(!Connected);
            Debug.Assert(m_other != null);
            if (m_other != null)
            {
                Debug.Assert(this.m_constraint == m_other.m_constraint);
                Debug.Assert(this == m_other.m_other);
            }

            if (m_constraint != null)
            {
                RemoveConstraint(otherConnector, m_constraint);
            }

            ConnectInternal(ref thisMatrix, ref otherMatrix, otherConnector, constructor: false);
        }
Exemplo n.º 26
0
            public static void RequestConnect(MyShipConnector masterConnector)
            {
                var msg = new RequestConnectMsg();
                msg.MasterEntityId = masterConnector.EntityId;

                Sync.Layer.SendMessageToServer(ref msg);
            }
Exemplo n.º 27
0
 private void RemoveConstraint(MyShipConnector otherConnector, HkConstraint constraint)
 {
     if (this.HasConstraint)
     {
         CubeGrid.Physics.RemoveConstraint(constraint);
         HasConstraint = false;
     }
     else
     {
         otherConnector.CubeGrid.Physics.RemoveConstraint(constraint);
         otherConnector.HasConstraint = false;
     }
     constraint.Dispose();
 }
Exemplo n.º 28
0
 private void CreateConstraint(MyShipConnector otherConnector)
 {
     CreateConstraintNosync(otherConnector);
     if (Sync.IsServer)
     {
         m_connectionState.Value = new State() { OtherEntityId = otherConnector.EntityId, MasterToSlave = null };
         otherConnector.m_connectionState.Value = new State() { OtherEntityId = EntityId, MasterToSlave = null };
     }
 }