private void SerializeRopeData(BitStream stream, bool applyWhenReading, List<MyCubeGrid> gridsGroup = null)
        {
            if (MyRopeComponent.Static == null)
                return;

            if (stream.Writing)
            {
                m_tmpRopes.Clear();
                m_tmpRopeGrids.Clear();

                m_tmpRopeGrids.Add(Entity);
                Debug.Assert(gridsGroup != null);
                if (gridsGroup != null) 
                {
                    foreach (var grid in gridsGroup)
                        m_tmpRopeGrids.Add(grid);
                }

                MyRopeComponent.Static.GetRopesForGrids(m_tmpRopeGrids, m_tmpRopes);

                MyRopeData ropeData;

                stream.WriteUInt16((ushort)m_tmpRopes.Count);
                foreach (var rope in m_tmpRopes)
                {
                    var ropeProxyTarget = MyMultiplayer.Static.ReplicationLayer.GetProxyTarget((IMyEventProxy)rope);
                    NetworkId ropeNetworkId = MyMultiplayer.Static.ReplicationLayer.GetNetworkIdByObject(ropeProxyTarget);
                    stream.WriteNetworkId(ropeNetworkId);

                    //TODO - MyRopeComponent should be rewritten to singleton
                    MyRopeComponent.GetRopeData(rope.EntityId, out ropeData);
                    stream.WriteFloat(ropeData.CurrentRopeLength);
                }

                m_tmpRopes.Clear();
                m_tmpRopeGrids.Clear();
            }
            else
            {
                uint ropesCount = stream.ReadUInt16();
                for (uint i = 0; i < ropesCount; ++i)
                {
                    NetworkId ropeNetworkId = stream.ReadNetworkId();
                    float ropeLength = stream.ReadFloat();

                    MyRopeReplicable replicable = MyMultiplayer.Static.ReplicationLayer.GetObjectByNetworkId(ropeNetworkId) as MyRopeReplicable;
                    MyRope rope = replicable != null ? replicable.Instance : null;

                    if (rope != null && applyWhenReading)
                        MyRopeComponent.Static.SetRopeLengthSynced(rope.EntityId, ropeLength);
                }
            }

        }
        public override void Serialize(BitStream stream, MyClientStateBase forClient, byte packetId, int maxBitPosition)
        {
            base.Serialize(stream, forClient, packetId, maxBitPosition);

            if (stream.Writing)
            {
                stream.WriteHalf(Entity.Physics.LinearVelocity.Length());
                // Head and spine stuff, 36 - 152b (4.5B - 19 B)
                stream.WriteHalf(Entity.HeadLocalXAngle); // 2B
                stream.WriteHalf(Entity.HeadLocalYAngle); // 2B

                // TODO: Spine has only one angle (bending forward backward)
                // Getting EULER angles from Matrix seems good way to get it (z-component)
                stream.WriteQuaternionNormCompressedIdentity(Entity.GetAdditionalRotation(Entity.Definition.SpineBone)); // 1b / 30b
                stream.WriteQuaternionNormCompressedIdentity(Entity.GetAdditionalRotation(Entity.Definition.HeadBone)); // 1b / 30b

                // Movement state, 2B
                stream.WriteUInt16((ushort)Entity.GetCurrentMovementState());
                // Movement flag.
                stream.WriteUInt16((ushort)Entity.MovementFlags);

                // Flags, 6 bits
                bool hasJetpack = Entity.JetpackComp != null;
                stream.WriteBool(hasJetpack ? Entity.JetpackComp.TurnedOn : false);
                stream.WriteBool(hasJetpack ? Entity.JetpackComp.DampenersTurnedOn : false);
                stream.WriteBool(Entity.LightEnabled); // TODO: Remove
                stream.WriteBool(Entity.ZoomMode == MyZoomModeEnum.IronSight);
                stream.WriteBool(Entity.RadioBroadcaster.WantsToBeEnabled); // TODO: Remove
                stream.WriteBool(Entity.TargetFromCamera);

                stream.WriteNormalizedSignedVector3(Entity.MoveIndicator, 8);

                if (MyFakes.CHARACTER_SERVER_SYNC)
                {
                    Vector2 rotation = Entity.RotationIndicator;
                    stream.WriteHalf(rotation.X);
                    stream.WriteHalf(rotation.Y);

                    stream.WriteHalf(Entity.RollIndicator);

                }

            }
            else
            {
                Vector3 move;
                MyCharacterNetState charNetState = ReadCharacterState(stream, out move);


                if (MyFakes.CHARACTER_SERVER_SYNC)
                {
                    charNetState.Rotation.X = stream.ReadHalf();
                    charNetState.Rotation.Y = stream.ReadHalf();
                    charNetState.Roll = stream.ReadHalf();
                }

                if (!IsControlledLocally && !Entity.Closed)
                {
                    Entity.SetStateFromNetwork(ref charNetState);
                    Entity.MoveIndicator = move;
                }             
            }
        }
Пример #3
0
        public override void SerializePhysics(BitStream stream, MyNetworkClient sender, bool highOrientationCompression = false)
        {
            // Usually 59 B

            // Base stuff (position, orientation, velocities)
            base.SerializePhysics(stream, sender, true); // 50.5 B

            if (MyFakes.CHARACTER_SERVER_SYNC)
            {
                // TODO: Serialize move and rotate when necesary
            }

            if (stream.Writing)
            {
                // Head and spine stuff, 36 - 152b (4.5B - 19 B)
                stream.WriteHalf(Entity.HeadLocalXAngle); // 2B
                stream.WriteHalf(Entity.HeadLocalYAngle); // 2B

                // TODO: Spine has only one angle (bending forward backward)
                stream.WriteQuaternionNormCompressedIdentity(Entity.GetAdditionalRotation(Entity.Definition.SpineBone)); // 1b / 30b
                stream.WriteQuaternionNormCompressedIdentity(Entity.GetAdditionalRotation(Entity.Definition.HeadBone)); // 1b / 30b
                stream.WriteQuaternionNormCompressedIdentity(Entity.GetAdditionalRotation(Entity.Definition.LeftForearmBone)); // 1b / 30b
                stream.WriteQuaternionNormCompressedIdentity(Entity.GetAdditionalRotation(Entity.Definition.LeftUpperarmBone)); // 1b / 30b

                // Movement state, 2B
                stream.WriteUInt16((ushort)Entity.GetCurrentMovementState());

                // Flags, 6 bits
                stream.WriteBool(Entity.JetpackComp != null);
                if (Entity.JetpackComp != null)
                {
                    stream.WriteBool(Entity.JetpackComp.TurnedOn);
                    stream.WriteBool(Entity.JetpackComp.DampenersTurnedOn);
                }

                stream.WriteBool(Entity.LightEnabled); // TODO: Remove
                stream.WriteBool(Entity.ZoomMode == MyZoomModeEnum.IronSight);
                stream.WriteBool(Entity.RadioBroadcaster.WantsToBeEnabled); // TODO: Remove
                stream.WriteBool(Entity.TargetFromCamera);

                if (MyFakes.CHARACTER_SERVER_SYNC)
                {
                    Vector3 temp = Entity.MoveIndicator;
                    stream.WriteHalf(temp.X);
                    stream.WriteHalf(temp.Y);
                    stream.WriteHalf(temp.Z);

                    Vector2 rotation = Entity.RotationIndicator;
                    stream.WriteHalf(rotation.X);
                    stream.WriteHalf(rotation.Y);

                    stream.WriteHalf(Entity.Roll);

                }
            }
            else
            {
                // Head and spine stuff
                float headX = stream.ReadHalf();
                float headY = stream.ReadHalf();
                Quaternion spine = stream.ReadQuaternionNormCompressedIdentity();
                Quaternion head = stream.ReadQuaternionNormCompressedIdentity();
                Quaternion hand = stream.ReadQuaternionNormCompressedIdentity();
                Quaternion upperArm = stream.ReadQuaternionNormCompressedIdentity();
                var handler0 = HeadOrSpineChanged;
                if (handler0 != null) handler0(headX, headY, spine, head, hand, upperArm);

                // Movement state
                MyCharacterMovementEnum movementState = (MyCharacterMovementEnum)stream.ReadUInt16();
                var handler = MovementStateChanged;
                if (handler != null && Entity.GetCurrentMovementState() != movementState) handler(movementState);

                // Flags
                bool hasJetpack = stream.ReadBool();
                bool jetpack = false;
                bool dampeners = false;

                if (hasJetpack)
                {
                    jetpack = stream.ReadBool();
                    dampeners = stream.ReadBool();
                }
                bool lights = stream.ReadBool(); // TODO: Remove
                bool ironsight = stream.ReadBool();
                bool broadcast = stream.ReadBool(); // TODO: Remove
                bool targetFromCamera = stream.ReadBool();

                if (MyFakes.CHARACTER_SERVER_SYNC)
                {
                    Vector3 temp = Vector3.Zero;
                    temp.X = stream.ReadHalf();
                    temp.Y = stream.ReadHalf();
                    temp.Z = stream.ReadHalf();

                    Entity.MoveIndicator = temp;

                    Vector2 rotation = Vector2.Zero;
                    rotation.X = stream.ReadHalf();
                    rotation.Y = stream.ReadHalf();

                    Entity.RotationIndicator = rotation;

                    Entity.Roll = stream.ReadHalf();
                }

                var handler2 = FlagsChanged;
                if (handler2 != null) handler2(jetpack, dampeners, lights, ironsight, broadcast, targetFromCamera);
            }
        }
        public override bool Serialize(BitStream stream, EndpointId forClient,uint timeStamp, byte packetId, int maxBitPosition)
        {
            base.Serialize(stream, forClient,timeStamp, packetId, maxBitPosition);

            if (stream.Writing)
            {
                // Head and spine stuff, 36 - 152b (4.5B - 19 B)
                stream.WriteHalf(Entity.HeadLocalXAngle); // 2B
                stream.WriteHalf(Entity.HeadLocalYAngle); // 2B

                // TODO: Spine has only one angle (bending forward backward)
                // Getting EULER angles from Matrix seems good way to get it (z-component)
                stream.WriteQuaternionNormCompressedIdentity(Entity.GetAdditionalRotation(Entity.Definition.SpineBone)); // 1b / 30b
                stream.WriteQuaternionNormCompressedIdentity(Entity.GetAdditionalRotation(Entity.Definition.HeadBone)); // 1b / 30b

                // Movement state, 2B
                stream.WriteUInt16((ushort)Entity.GetCurrentMovementState());
                // Movement flag.
                stream.WriteUInt16((ushort)Entity.MovementFlags);

                // Flags, 6 bits
                bool hasJetpack = Entity.JetpackComp != null;
                stream.WriteBool(hasJetpack ? Entity.JetpackComp.TurnedOn : false);
                stream.WriteBool(hasJetpack ? Entity.JetpackComp.DampenersTurnedOn : false);
                stream.WriteBool(Entity.LightEnabled); // TODO: Remove
                stream.WriteBool(Entity.ZoomMode == MyZoomModeEnum.IronSight);
                stream.WriteBool(Entity.RadioBroadcaster.WantsToBeEnabled); // TODO: Remove
                stream.WriteBool(Entity.TargetFromCamera);

                stream.WriteNormalizedSignedVector3(Entity.MoveIndicator, 8);

                float speed = Entity.Physics.CharacterProxy != null ? Entity.Physics.CharacterProxy.Speed : 0.0f;
                stream.WriteFloat(speed);

                stream.WriteFloat(Entity.RotationIndicator.X);
                stream.WriteFloat(Entity.RotationIndicator.Y);
                stream.WriteFloat(Entity.RollIndicator);

            }
            else
            {
                Vector3 move;
                MyCharacterNetState charNetState = ReadCharacterState(stream);

                if (!IsControlledLocally && !Entity.Closed)
                {
                   Entity.SetStateFromNetwork(ref charNetState);
                }
            }
            return true;
        }