private void EnsureCapacity(int requiredCapacity)
        {
            var capacity = Buffer.Capacity() > 0 ? Buffer.Capacity() : 1;

            if (requiredCapacity > capacity)
            {
                var newCapacity = QuickMath.NextPowerOfTwo(requiredCapacity);
                var newBuffer   = new byte[newCapacity];
                Array.Copy(Buffer.ByteArray(), 0, newBuffer, 0, capacity);
                Buffer.Wrap(newBuffer);
            }
        }
Exemple #2
0
        private void EnsureCapacity(int additionalCapacity)
        {
            var requiredCapacity = position + additionalCapacity;

            if (requiredCapacity < 0)
            {
                var s = string.Format("Insufficient capacity: position=%d additional=%d", position, additionalCapacity);
                throw new InvalidOperationException(s);
            }
            if (requiredCapacity > capacity)
            {
                var newCapacity = QuickMath.NextPowerOfTwo(requiredCapacity);
                var newBuffer   = new byte[newCapacity];
                Array.Copy(protocolBuffer.ByteArray(), 0, newBuffer, 0, capacity);
                capacity = newCapacity;
                protocolBuffer.Wrap(newBuffer);
            }
        }
        private void EnsureCapacity(int additionalCapacity)
        {
            var requiredCapacity = _position + additionalCapacity;

            if (requiredCapacity < 0)
            {
                var s = string.Format("Insufficient capacity: position={0} additional={1}", _position,
                                      additionalCapacity);
                throw new InvalidOperationException(s);
            }
            if (requiredCapacity > _capacity)
            {
                var newCapacity = QuickMath.NextPowerOfTwo(requiredCapacity);
                var newBuffer   = new byte[newCapacity];
                Buffer.BlockCopy(_protocolBuffer.ByteArray(), 0, newBuffer, 0, _capacity);
                _capacity = newCapacity;
                _protocolBuffer.Wrap(newBuffer);
            }
        }
Exemple #4
0
        static void CoffeeTest()
        {
            //MrCoffee lifesaver = new  MrCoffee();
            CoffeeMaker lifesaver = new CoffeeMaker();


            lifesaver.AddGrains(5.0);
            lifesaver.AddWater(12);
            lifesaver.InsertFilter();

            lifesaver.Make();
            QuickMath qm = new QuickMath(5, 6);

            int x = new QuickMath(6, 7).Add();

            x = QuickMaths.add(5, 6);

            //int k = lifesaver.AddGrains(4000000);
        }
 public static ClientMessage CreateForEncode(int initialCapacity)
 {
     initialCapacity = QuickMath.NextPowerOfTwo(initialCapacity);
     return(CreateForEncode(new SafeBuffer(new byte[initialCapacity]), 0));
 }
Exemple #6
0
        /// <summary>Construct a buffer builder with an initial capacity that will be rounded up to the nearest power of 2.</summary>
        /// <param name="initialCapacity">at which the capacity will start.</param>
        private BufferBuilder(int initialCapacity)
        {
            capacity = QuickMath.NextPowerOfTwo(initialCapacity);

            protocolBuffer = new SafeBuffer(new byte[capacity]);
        }
Exemple #7
0
        private void ChunkUpdateThread()
        {
            //Environment.ProcessorCount / 2;
            Stopwatch sw = new Stopwatch();

            while (!CancelationToken.IsCancellationRequested)
            {
                int maxThreads = Options.VideoOptions.ChunkThreads;

                var cameraChunkPos = new ChunkCoordinates(new PlayerLocation(_cameraPosition.X, _cameraPosition.Y,
                                                                             _cameraPosition.Z));
                //SpinWait.SpinUntil(() => Interlocked.Read(ref _threadsRunning) < maxThreads);

                foreach (var data in _chunkData.ToArray().Where(x =>
                                                                QuickMath.Abs(cameraChunkPos.DistanceTo(x.Key)) > Options.VideoOptions.RenderDistance))
                {
                    data.Value?.Dispose();
                    _chunkData.TryRemove(data.Key, out _);
                }

                //var cameraChunkPos = new ChunkCoordinates(new PlayerLocation(camera.Position.X, camera.Position.Y,
                //     camera.Position.Z));

                var renderedChunks = Chunks.ToArray().Where(x =>
                {
                    if (Math.Abs(x.Key.DistanceTo(cameraChunkPos)) > Options.VideoOptions.RenderDistance)
                    {
                        return(false);
                    }

                    var chunkPos = new Vector3(x.Key.X * ChunkColumn.ChunkWidth, 0, x.Key.Z * ChunkColumn.ChunkDepth);
                    return(_cameraBoundingFrustum.Intersects(new Microsoft.Xna.Framework.BoundingBox(chunkPos,
                                                                                                     chunkPos + new Vector3(ChunkColumn.ChunkWidth, 256 /*16 * ((x.Value.GetHeighest() >> 4) + 1)*/,
                                                                                                                            ChunkColumn.ChunkDepth))));
                }).ToArray();

                foreach (var c in renderedChunks)
                {
                    if (_chunkData.TryGetValue(c.Key, out var data))
                    {
                        if (_renderedChunks.TryAdd(data))
                        {
                        }
                    }
                }

                foreach (var c in _renderedChunks.ToArray())
                {
                    if (c == null || c.Coordinates == default || !renderedChunks.Any(x => x.Key.Equals(c.Coordinates)))
                    {
                        _renderedChunks.Remove(c);
                    }
                }

                if (Interlocked.Read(ref _threadsRunning) >= maxThreads)
                {
                    continue;
                }


                bool nonInView = false;
                try
                {
                    if (HighestPriority.TryDequeue(out var coords))
                    {
                        if (Math.Abs(cameraChunkPos.DistanceTo(coords)) > Options.VideoOptions.RenderDistance)
                        {
                            if (!Enqueued.Contains(coords))
                            {
                                Enqueued.Add(coords);
                            }
                        }
                        else
                        {
                            Enqueued.Remove(coords);
                            Schedule(coords, WorkItemPriority.Highest);
                        }

                        //Enqueued.Remove(coords);
                    }
                    else if (Enqueued.Count > 0)
                    {
                        //var cc = new ChunkCoordinates(CameraPosition);

                        var where = Enqueued.Where(x => IsWithinView(x, _cameraBoundingFrustum)).ToArray();
                        if (where.Length > 0)
                        {
                            coords = where.MinBy(x => Math.Abs(x.DistanceTo(new ChunkCoordinates(_cameraPosition))));
                        }
                        else
                        {
                            coords = Enqueued.MinBy(x => Math.Abs(x.DistanceTo(new ChunkCoordinates(_cameraPosition))));
                        }

                        if (Math.Abs(cameraChunkPos.DistanceTo(coords)) <= Options.VideoOptions.RenderDistance)
                        {
                            if (!_workItems.ContainsKey(coords))
                            {
                                Schedule(coords, WorkItemPriority.AboveNormal);
                            }
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                    break;
                }
            }

            if (!CancelationToken.IsCancellationRequested)
            {
                Log.Warn($"Chunk update loop has unexpectedly ended!");
            }

            //TaskScheduler.Dispose();
        }