public override void OnGUI(Rect i_Position, SerializedProperty i_Property, GUIContent i_Label)
        {
            EditorGUI.BeginProperty(i_Position, i_Label, i_Property);

            EditorGUI.BeginChangeCheck();

            SerializedProperty serializedValueProperty = i_Property.FindPropertyRelative("_serializedValue");
            string             value = serializedValueProperty.stringValue;

            FP fpValue = FP.Zero;

            if (value != "")
            {
                fpValue = FP.FromRaw(long.Parse(value));
            }

            fpValue = EditorGUI.FloatField(i_Position, i_Label, (float)fpValue);

            if (EditorGUI.EndChangeCheck())
            {
                serializedValueProperty.stringValue = fpValue.RawValue.ToString();
            }

            EditorGUI.EndProperty();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns 2 raised to the specified power.
        /// Provides at least 6 decimals of accuracy.
        /// </summary>
        internal static FP Pow2(FP x)
        {
            if (x.RawValue == 0)
            {
                return(FP.One);
            }

            // Avoid negative arguments by exploiting that exp(-x) = 1/exp(x).
            bool neg = x.RawValue < 0;

            if (neg)
            {
                x = -x;
            }

            if (x == FP.One)
            {
                return(neg ? FP.One / (FP)2 : (FP)2);
            }
            if (x >= FP.Log2Max)
            {
                return(neg ? FP.One / FP.MaxValue : FP.MaxValue);
            }
            if (x <= FP.Log2Min)
            {
                return(neg ? FP.MaxValue : FP.Zero);
            }

            /* The algorithm is based on the power series for exp(x):
             * http://en.wikipedia.org/wiki/Exponential_function#Formal_definition
             *
             * From term n, we get term n+1 by multiplying with x/n.
             * When the sum term drops to zero, we can stop summing.
             */

            int integerPart = (int)Floor(x);

            // Take fractional part of exponent
            x = FP.FromRaw(x.RawValue & 0x00000000FFFFFFFF);

            var result = FP.One;
            var term   = FP.One;
            int i      = 1;

            while (term.RawValue != 0)
            {
                term    = FP.FastMul(FP.FastMul(x, term), FP.Ln2) / (FP)i;
                result += term;
                i++;
            }

            result = FP.FromRaw(result.RawValue << integerPart);
            if (neg)
            {
                result = FP.One / result;
            }

            return(result);
        }
Exemplo n.º 3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            var rawProp = property.FindPropertyRelative("_serializedValue");

            FP fpValue = FP.FromRaw(rawProp.longValue);

            fpValue = EditorGUI.FloatField(position, label, (float)fpValue);

            rawProp.longValue = fpValue.RawValue;

            EditorGUI.EndProperty();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns the base-2 logarithm of a specified number.
        /// Provides at least 9 decimals of accuracy.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The argument was non-positive
        /// </exception>
        internal static FP Log2(FP x)
        {
            if (x.RawValue <= 0)
            {
                throw new ArgumentOutOfRangeException("Non-positive value passed to Ln", "x");
            }

            // This implementation is based on Clay. S. Turner's fast binary logarithm
            // algorithm (C. S. Turner,  "A Fast Binary Logarithm Algorithm", IEEE Signal
            //     Processing Mag., pp. 124,140, Sep. 2010.)

            long b = 1U << (FP.FRACTIONAL_PLACES - 1);
            long y = 0;

            long rawX = x.RawValue;

            while (rawX < FP.ONE)
            {
                rawX <<= 1;
                y     -= FP.ONE;
            }

            while (rawX >= (FP.ONE << 1))
            {
                rawX >>= 1;
                y     += FP.ONE;
            }

            var z = FP.FromRaw(rawX);

            for (int i = 0; i < FP.FRACTIONAL_PLACES; i++)
            {
                z = FP.FastMul(z, z);
                if (z.RawValue >= (FP.ONE << 1))
                {
                    z  = FP.FromRaw(z.RawValue >> 1);
                    y += b;
                }
                b >>= 1;
            }

            return(FP.FromRaw(y));
        }
Exemplo n.º 5
0
        public override void Deserialize(byte[] data, ref int offset)
        {
            byte numberOfActions = data[offset++];

            for (int i = 0; i < numberOfActions; i++)
            {
                byte key  = data[offset++];
                byte type = data[offset++];

                switch (type)
                {
                case (byte)Types.Integer:
                    int intValue = BitConverter.ToInt32(data, offset);
                    AddInt(key, intValue);
                    offset += sizeof(int);
                    break;

                case (byte)Types.Byte:
                    byte byteValue = data[offset++];
                    AddByte(key, byteValue);
                    break;

                case (byte)Types.ByteArray:
                    int byteArrayLen = BitConverter.ToInt32(data, offset);
                    offset += sizeof(int);

                    byte[] bArray = new byte[byteArrayLen];
                    for (int indexArray = 0; indexArray < byteArrayLen; indexArray++)
                    {
                        bArray[indexArray] = data[offset + indexArray];
                    }

                    offset += byteArrayLen;

                    AddByteArray(key, bArray);
                    break;

                case (byte)Types.String:
                    int strlen = BitConverter.ToInt32(data, offset);
                    offset += sizeof(int);
                    string stringValue = System.Text.Encoding.ASCII.GetString(data, offset, strlen);
                    offset += strlen;
                    AddString(key, stringValue);
                    break;

                case (byte)Types.FP:
                    FP fpValue = FP.FromRaw(BitConverter.ToInt64(data, offset));
                    AddFP(key, fpValue);
                    offset += sizeof(long);
                    break;

                case (byte)Types.TSVector:
                    FP fpValueX = FP.FromRaw(BitConverter.ToInt64(data, offset));
                    offset += sizeof(long);

                    FP fpValueY = FP.FromRaw(BitConverter.ToInt64(data, offset));
                    offset += sizeof(long);

                    FP fpValueZ = FP.FromRaw(BitConverter.ToInt64(data, offset));
                    offset += sizeof(long);

                    AddTSVector(key, new TSVector(fpValueX, fpValueY, fpValueZ));
                    break;

                case (byte)Types.TSVector2:
                    FP fpValueX2 = FP.FromRaw(BitConverter.ToInt64(data, offset));
                    offset += sizeof(long);

                    FP fpValueY2 = FP.FromRaw(BitConverter.ToInt64(data, offset));
                    offset += sizeof(long);

                    AddTSVector2(key, new TSVector2(fpValueX2, fpValueY2));
                    break;

                default:
                    //Not Implemented
                    break;
                }
            }
        }
Exemplo n.º 6
0
        public static SyncedData[] Decode(byte[] data)
        {
            List <SyncedData> list = new List <SyncedData>();
            int i   = 0;
            int num = BitConverter.ToInt32(data, i);

            i += 4;
            byte ownerID = data[i++];
            byte b       = data[i++];
            bool flag    = data[i++] == 1;
            int  num2    = num;

            while (i < data.Length)
            {
                SyncedData syncedData = new SyncedData(ownerID, num2--);
                byte       b2         = data[i++];
                for (int j = 0; j < (int)b2; j++)
                {
                    byte key = data[i++];
                    switch (data[i++])
                    {
                    case 0:
                    {
                        byte value = data[i++];
                        syncedData.inputData.AddByte(key, value);
                        break;
                    }

                    case 1:
                    {
                        byte   b3      = data[i++];
                        string @string = Encoding.ASCII.GetString(data, i, (int)b3);
                        i += (int)b3;
                        syncedData.inputData.AddString(key, @string);
                        break;
                    }

                    case 2:
                    {
                        int value2 = BitConverter.ToInt32(data, i);
                        syncedData.inputData.AddInt(key, value2);
                        i += 4;
                        break;
                    }

                    case 3:
                    {
                        FP value3 = FP.FromRaw(BitConverter.ToInt64(data, i));
                        syncedData.inputData.AddFP(key, value3);
                        i += 8;
                        break;
                    }

                    case 4:
                    {
                        FP x = FP.FromRaw(BitConverter.ToInt64(data, i));
                        i += 8;
                        FP y = FP.FromRaw(BitConverter.ToInt64(data, i));
                        i += 8;
                        FP z = FP.FromRaw(BitConverter.ToInt64(data, i));
                        i += 8;
                        syncedData.inputData.AddTSVector(key, new TSVector(x, y, z));
                        break;
                    }
                    }
                }
                list.Add(syncedData);
            }
            bool flag2 = list.Count > 0;

            if (flag2)
            {
                list[0].dropPlayer       = flag;
                list[0].dropFromPlayerId = b;
            }
            return(list.ToArray());
        }