Exemplo n.º 1
0
        //private static ConcurrentQueue<ChunkColumn> Finished = new ConcurrentQueue<ChunkColumn>();
        static void Main(string[] args)
        {
            WorldGen = new OverworldGeneratorV2();
            Game     = new TestGame(WorldGen, Radius, Resolution);
            //  gen.ApplyBlocks = true;

            bool done = false;
            //  ChunkColumn[] generatedChunks = new ChunkColumn[chunks * chunks];
            ConcurrentQueue <ChunkCoordinates> chunkGeneratorQueue = new ConcurrentQueue <ChunkCoordinates>();

            long average = 0;
            long min     = long.MaxValue;
            long max     = long.MinValue;

            int chunskGenerated = 0;

            Thread[] threads = new Thread[Environment.ProcessorCount / 2];
            for (int t = 0; t < threads.Length; t++)
            {
                threads[t] = new Thread(() =>
                {
                    Stopwatch timing = new Stopwatch();
                    while (true)
                    {
                        if (chunkGeneratorQueue.TryDequeue(out var coords))
                        {
                            timing.Restart();

                            // ChunkColumn column = WorldGen.GenerateChunkColumn(coords);
                            NoiseData nd = new NoiseData();
                            nd.X         = coords.X;
                            nd.Z         = coords.Z;
                            nd.Chunk     = WorldGen.GenerateChunkColumn(coords);

                            /*   for (int x = 0; x < 16; x++)
                             *  {
                             *      for (int z = 0; z < 16; z++)
                             *      {
                             *          var index = NoiseMap.GetIndex(x, z);
                             *          var temp = WorldGen.TemperatureNoise.GetValue((coords.X * 16) + x, (coords.Z * 16) + z) + 1f;
                             *          var rain = MathF.Abs(WorldGen.RainfallNoise.GetValue((coords.X * 16) + x, (coords.Z * 16) + z));
                             *
                             *          nd.Temperature[index] = temp;
                             *          nd.Humidity[index] = rain;
                             *      }
                             *  }
                             */
                            lock (Game.Lock)
                            {
                                Game.Chunks.Add(nd);
                            }

                            // generatedChunks[(coords.X * chunks) + coords.Z] = column;
                            // Finished.Enqueue(column);
                            chunskGenerated++;

                            timing.Stop();

                            average += timing.ElapsedMilliseconds;
                            if (timing.ElapsedMilliseconds < min)
                            {
                                min = timing.ElapsedMilliseconds;
                            }

                            if (timing.ElapsedMilliseconds > max)
                            {
                                max = timing.ElapsedMilliseconds;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                });
            }

            // threads[0] = new Thread(() => { GenerateBiomeMap(chunks); });

            for (int z = 0; z < Radius; z += Resolution)
            {
                for (int x = 0; x < Radius; x += Resolution)
                {
                    chunkGeneratorQueue.Enqueue(new ChunkCoordinates(x, z));
                }
            }

            Stopwatch timer = Stopwatch.StartNew();

            foreach (var thread in threads)
            {
                thread.Start();
            }


            Game.Run();

            timer.Stop();

            Console.Clear();

            Console.WriteLine($"Generating {Radius * Radius} chunks took: {timer.Elapsed}");
            //Console.WriteLine($"Min Height: {gen.MinHeight} Max Height: {gen.MaxHeight}");
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int chunks = 64;
            int width  = chunks * 16;
            int height = chunks * 16;

            OverworldGeneratorV2 gen = new OverworldGeneratorV2();

            gen.ApplyBlocks = true;

            bool done = false;
            //  ChunkColumn[] generatedChunks = new ChunkColumn[chunks * chunks];
            ConcurrentQueue <ChunkCoordinates> chunkGeneratorQueue = new ConcurrentQueue <ChunkCoordinates>();

            long average = 0;
            long min     = long.MaxValue;
            long max     = long.MinValue;

            int chunskGenerated = 0;

            Thread[] threads = new Thread[Environment.ProcessorCount - 1];
            for (int t = 1; t < threads.Length; t++)
            {
                threads[t] = new Thread(() =>
                {
                    Stopwatch timing = new Stopwatch();
                    while (true)
                    {
                        if (chunkGeneratorQueue.TryDequeue(out var coords))
                        {
                            timing.Restart();

                            ChunkColumn column = gen.GenerateChunkColumn(coords);
                            // generatedChunks[(coords.X * chunks) + coords.Z] = column;
                            Finished.Enqueue(column);
                            chunskGenerated++;

                            timing.Stop();

                            average += timing.ElapsedMilliseconds;
                            if (timing.ElapsedMilliseconds < min)
                            {
                                min = timing.ElapsedMilliseconds;
                            }

                            if (timing.ElapsedMilliseconds > max)
                            {
                                max = timing.ElapsedMilliseconds;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                });
            }

            threads[0] = new Thread(() => { GenerateBiomeMap(chunks); });

            for (int x = 0; x < chunks; x++)
            {
                for (int z = 0; z < chunks; z++)
                {
                    chunkGeneratorQueue.Enqueue(new ChunkCoordinates(x, z));
                }
            }

            Stopwatch timer = Stopwatch.StartNew();

            foreach (var thread in threads)
            {
                thread.Start();
            }

            int threadsAlive = 0;

            do
            {
                threadsAlive = threads.Count(x => x.IsAlive);

                Console.Clear();

                Console.WriteLine($"Threads: {threadsAlive} Queued: {chunkGeneratorQueue.Count} Generated: {chunskGenerated} Avg: {average / Math.Max(1, chunskGenerated)}ms Min: {min}ms Max: {max}ms");
                Console.WriteLine($"Processed: {Imaged} Remaining: {Finished.Count}");

                Thread.Sleep(100);
            } while (threadsAlive > 0);

            timer.Stop();

            Console.Clear();

            Console.WriteLine($"Generating {chunks * chunks} chunks took: {timer.Elapsed}");
            Console.WriteLine($"Min Height: {gen.MinHeight} Max Height: {gen.MaxHeight}");
        }