Exemple #1
0
        public ClientPlayerSnapshot(SnapshotSystem snapshotSystem, NetConnection otherConnection,
                                    bool dontAllocateId = false)
            : base(snapshotSystem, otherConnection, dontAllocateId)
        {
            x        = AddPrimitiveField <float>();
            y        = AddPrimitiveField <float>();
            z        = AddPrimitiveField <float>();
            camYaw   = AddPrimitiveField <float>();
            camPitch = AddPrimitiveField <float>();

            actionFlag   = AddPrimitiveField <ByteFlag>();
            movementFlag = AddPrimitiveField <ByteFlag>();

            selectedItem = AddPrimitiveField <byte>();

            colorR = AddPrimitiveField <byte>();
            colorG = AddPrimitiveField <byte>();
            colorB = AddPrimitiveField <byte>();

            bulletSnapshot = AddCustomField(BulletSnapshot = new ClientBulletSnapshot());

            // TODO: Figure out why we can't compress ByteFlag's.
            // They won't always send the latest version.
            actionFlag.NeverCompress   = true;
            movementFlag.NeverCompress = true;

            Setup();

            // This snapshot updates so much that we really don't need
            // to store many delta snapshots.
            EnableDeltaCompression(4);
        }
Exemple #2
0
        public NetworkPlayerSnapshot(SnapshotSystem snapshotSystem, NetConnection otherConnection, NetworkPlayer netPlayer,
                                     bool dontAllocateId = false, bool dontAwait = false)
            : base(snapshotSystem, otherConnection, dontAllocateId, dontAwait)
        {
            NetPlayer = netPlayer;

            teamField         = AddPrimitiveField <byte>((byte)Team.None);
            scoreField        = AddPrimitiveField <short>();
            pingField         = AddPrimitiveField <ushort>();
            characterIdField  = AddPrimitiveField <ushort>();
            hasCharacterField = AddPrimitiveField <bool>();

            Setup();
            EnableDeltaCompression(4);
        }
        /*
         *  Array
         *  byte index
         *  byte type
         *  object data...
         *
         *  NestedSnapshot
         *  byte snapshotType
         *  byte type
         *  object data...
         *
         *  NestedSnapshot in Array
         *  byte index
         *  byte NestedSnapshot
         *  byte type
         *  object data...
         */

        public virtual void Serialize(NetBuffer buffer, Snapshot prev)
        {
            if (prev != null && GetType() != prev.GetType())
            {
                throw new InvalidOperationException("Cannot get delta diff of two different snapshot types.");
            }

            // Get delta diff
            List <SnapshotField> deltaFields = new List <SnapshotField>();

            for (int i = 0; i < fields.Count; i++)
            {
                object a = fields[i];

                if (prev != null && !forceSendFields.Contains(i))
                {
                    if (prev.fields.Count > i)
                    {
                        object b = prev.fields[i];
                        if (!a.Equals(b))
                        {
                            deltaFields.Add(new SnapshotField((byte)i, GetFieldType(a.GetType()), a));
                        }
                    }
                    else
                    {
                        deltaFields.Add(new SnapshotField((byte)i, GetFieldType(a.GetType()), a));
                    }
                }
                else
                {
                    deltaFields.Add(new SnapshotField((byte)i, GetFieldType(a.GetType()), a));
                }
            }

            forceSendFields.Clear();

            // Write delta diff
            buffer.Write((ushort)deltaFields.Count);
            for (int i = 0; i < deltaFields.Count; i++)
            {
                SnapshotField f = deltaFields[i];
                buffer.Write(f.Id);
                buffer.Write((byte)f.Type);
                buffer.WriteDynamic(f.Value);
            }
        }
        public PlayerSnapshot(SnapshotSystem snapshotSystem, NetConnection otherConnection, bool isOwner, ushort id,
                              bool dontAllocateId = false)
            : base(snapshotSystem, otherConnection, dontAllocateId)
        {
            initId  = id;
            IsOwner = isOwner;

            netId = AddPrimitiveField(id);

            selectedItem = AddPrimitiveField <byte>();

            if (isOwner)
            {
                currentMag = AddPrimitiveField <byte>();
                storedAmmo = AddPrimitiveField <ushort>();

                health = AddPrimitiveField <float>();

                numBlocks   = AddPrimitiveField <ushort>();
                numGrenades = AddPrimitiveField <byte>();
                numMelons   = AddPrimitiveField <byte>();

                AddCustomField(HitFeedbackSnapshot = new HitFeedbackSnapshot());

                hitEnemy = (Trigger)AddTrigger().Value;
            }
            else
            {
                x = AddPrimitiveField <float>();
                y = AddPrimitiveField <float>();
                z = AddPrimitiveField <float>();

                stateFlag = AddPrimitiveField <ByteFlag>();
                stateFlag.NeverCompress = true;

                movementFlag = AddPrimitiveField <ByteFlag>();
                movementFlag.NeverCompress = true;

                camYaw   = AddPrimitiveField <float>();
                camPitch = AddPrimitiveField <float>();

                timesShot = (Trigger)AddTrigger().Value;
            }

            Setup();
            EnableDeltaCompression(32);
        }
Exemple #5
0
        public WorldSnapshot(SnapshotSystem snapshotSystem, NetConnection otherConnection,
                             bool dontAllocateId = false)
            : base(snapshotSystem, otherConnection, dontAllocateId)
        {
            playerFields = new Dictionary <ushort, DynamicSnapshotField>();

            AddCustomField(TerrainSnapshot           = new TerrainDeltaSnapshot());
            AddNestedField(NetworkPlayerListSnapshot = new NetworkPlayerListSnapshot(snapshotSystem,
                                                                                     otherConnection, dontAllocateId));
            AddNestedField(NetEntityListSnapshot = new NetEntityListSnapshot(snapshotSystem,
                                                                             otherConnection, dontAllocateId));

            time = AddPrimitiveField <float>();
            ag_max_cl_tickrate     = AddPrimitiveField <ushort>();
            ag_force_cl_await_snap = AddPrimitiveField <bool>();

            Setup();
            EnableDeltaCompression(16);
        }
Exemple #6
0
        bool SerializeField(SnapshotField field, NetBuffer buffer, DeltaSnapshot prev)
        {
            // Write field type
            buffer.Write((byte)field.Type);

            // Write the field data
            if (field.Type == SnapshotFieldType.Primitive)
            {
                // Write primitive
                buffer.Write((byte)field.PrimitiveType);
                buffer.WriteDynamic(field.Value);
            }
            else if (field.Type == SnapshotFieldType.Trigger)
            {
                Trigger t = (Trigger)field.Value;

                // Write trigger
                buffer.Write(t.Iterations);
                // Reset trigger
                t.Iterations = 0;
            }
            else if (field.Type == SnapshotFieldType.Snapshot)
            {
                // Write nested snapshot
                Snapshot ns = (Snapshot)field.Value;
                ns.Serialize(buffer);
            }
            else if (field.Type == SnapshotFieldType.Custom)
            {
                // Write custom snapshot
                CustomSnapshot cs = (CustomSnapshot)field.Value;
                cs.Serialize(buffer);
            }

            return(true);
        }
Exemple #7
0
        void ReadField(NetBuffer buffer, SnapshotField field)
        {
            // Read field type
            SnapshotFieldType fieldType = (SnapshotFieldType)buffer.ReadByte();

            // Read field data
            if (fieldType == SnapshotFieldType.Primitive)
            {
                // Read primitive
                SnapshotPrimitiveType primType = (SnapshotPrimitiveType)buffer.ReadByte();
                object v = ReadPrimitive(buffer, primType);

                if (field != null)
                {
                    field.Value = v;
                }
            }
            else if (fieldType == SnapshotFieldType.Trigger)
            {
                // Read trigger
                byte it = buffer.ReadByte();

                if (field != null)
                {
                    Trigger t = (Trigger)field.Value;
                    t.Iterations = it;
                }
            }
            else if (fieldType == SnapshotFieldType.Snapshot)
            {
                // Read nested snapshot
                Snapshot ns;
                if (field != null)
                {
                    ns = (Snapshot)field.Value;
                }
                else
                {
                    // Make temp snapshot to just read ns
                    ns = new TempSnapshot(snapshotSystem);
                }

                ns.Deserialize(buffer);
            }
            else if (fieldType == SnapshotFieldType.Custom)
            {
                // Read custom snapshot
                // Read snapshot size
                ushort bufferSize = buffer.ReadUInt16();

                if (field != null)
                {
                    // Read custom snapshot
                    CustomSnapshot cs = (CustomSnapshot)field.Value;
                    cs.Deserialize(buffer);
                }
                else
                {
                    // Skip buffer
                    buffer.Position += bufferSize;
                }
            }
        }