public void Test()
        {
            var CustomThreadPool = new CustomThreadPool(2);
            var Results0         = new List <int>();
            var Results1         = new List <int>();
            var CountdownEvent   = new CountdownEvent(2);

            CustomThreadPool.AddTask(0, () =>
            {
                Thread.Sleep(10);
                Results0.Add(0);
            });
            CustomThreadPool.AddTask(0, () =>
            {
                Results0.Add(1);
                CountdownEvent.Signal();
            });
            CustomThreadPool.AddTask(1, () =>
            {
                Results1.Add(0);
                CountdownEvent.Signal();
            });

            CountdownEvent.Wait();
            Thread.Sleep(10);
            Assert.True(CustomThreadPool.GetLoopIterCount(0) <= 2);
            Assert.True(CustomThreadPool.GetLoopIterCount(1) <= 2);
            Assert.Equal("0,1", Results0.ToStringArray());
            Assert.Equal("0", Results1.ToStringArray());
            CustomThreadPool.Stop();
        }
Example #2
0
        private void SecureUpToItem(int maxItem)
        {
            maxItem = Math.Min(maxItem, Length);
            var itemsToRead = maxItem - BufferedItemsCount;

            //Console.WriteLine("SecureUpToItem: {0}, {1}, {2}", MaxItem, BufferedItemsCount, ItemsToRead);

            if (itemsToRead > 0)
            {
                try
                {
                    var dataLength = itemsToRead * StructSize;
                    var data       = new byte[dataLength];

                    /*
                     * Stream.BeginRead(Data, 0, DataLength, (AsyncState) =>
                     * {
                     *  int Readed = Stream.EndRead(AsyncState);
                     *  CachedValues.AddRange(PointerUtils.ByteArrayToArray<TType>(Data));
                     * }, null);
                     */
                    CustomThreadPool.AddTask(0, () =>
                    {
                        var readed = _stream.Read(data, 0, dataLength);
                        _cachedValues.AddRange(PointerUtils.ByteArrayToArray <TType>(data));
                    });
                }
                catch (Exception exception)
                {
                    Console.Error.WriteLine(exception);
                }
            }
        }
    public virtual void Generate(int worldSeed)
    {
        int chunkSeed = Generator.GetPerIslandSeed(this);

        creatingChunkStopwatch = System.Diagnostics.Stopwatch.StartNew();

        random       = new Random(chunkSeed);
        elevations   = new List <float>();
        islandColors = new List <Color>();

        CreatingPoolTask = delegate {
            PoissonDiscSampler sampler = new PoissonDiscSampler(Size, Size, minPointRadius);

            Polygon polygon = new Polygon();

            //Add uniformly-spaced points
            foreach (Vector2 sample in sampler.Samples(chunkSeed))
            {
                polygon.Add(new Vertex((double)sample.x, (double)sample.y));
            }

            //add points at corners so chunk will be always square shaped
            polygon.Add(new Vertex(0, 0));
            polygon.Add(new Vertex(0, Size));
            polygon.Add(new Vertex(Size, 0));
            polygon.Add(new Vertex(Size, Size));

            //Add some randomly sampled points
            for (int i = 0; i < randomPoints - 4; i++)
            {
                polygon.Add(new Vertex(random.Range(0.0f, Size), random.Range(0.0f, Size)));
            }

            TriangleNet.Meshing.ConstraintOptions options = new TriangleNet.Meshing.ConstraintOptions()
            {
                ConformingDelaunay = true
            };
            mesh = (TriangleNet.Mesh)polygon.Triangulate(options);

            // Sample perlin noise to get elevations
            foreach (Vertex vert in mesh.Vertices)
            {
                float height = Generator.GetTerrainHeight((float)vert.x, (float)vert.y, this);
                Color color  = Generator.GetTerrainColor((float)vert.x, (float)vert.y, height, this);

                elevations.Add(height);
                islandColors.Add(color);
            }

            CreateMeshRequest = true;

            //let this be always the last piece of code here
            try {
                bin = new TriangleBin(mesh, Size, Size, minPointRadius * 2.0f);
            } catch (Exception e) {
                Debug.Log("triangulation failed!");
            }
        };
        CustomThreadPool.AddTask(CreatingPoolTask);
    }