Esempio n. 1
0
        public ServerChunkGenerator(
            IChunkSizePolicy chunkSizePolicy, 
            IAssetManagerProvider assetManagerProvider, 
            IGeneratorResolver generatorResolver,
            IEdgePointCalculator edgePointCalculator,
            IChunkConverter chunkConverter,
            IChunkCompressor chunkCompressor)
        {
            this.m_ChunkSizePolicy = chunkSizePolicy;
            this.m_EdgePointCalculator = edgePointCalculator;
            this.m_AssetManager = assetManagerProvider.GetAssetManager();
            this.m_ChunkCompressor = chunkCompressor;
            this.m_ChunkConverter = chunkConverter;

            this.m_GeneratorPipeline = new ThreadedTaskPipeline<ChunkGenerationRequest>();
            this.m_ConverterPipeline = new ThreadedTaskPipeline<ChunkConversionRequest>(false);
            this.m_CompressorPipeline = new ThreadedTaskPipeline<ChunkCompressionRequest>(false);

            this.m_Generator = generatorResolver.GetGeneratorForGame();
            this.m_Generator.SetSeed(10000);

            new Thread(this.GeneratorRun)
            {
                IsBackground = true,
                Priority = ThreadPriority.Highest
            }.Start();

            new Thread(this.ConverterRun)
            {
                IsBackground = true,
                Priority = ThreadPriority.Highest
            }.Start();

            new Thread(this.CompressorRun)
            {
                IsBackground = true,
                Priority = ThreadPriority.Highest
            }.Start();
        }
Esempio n. 2
0
        public PregenerateWorld(
            IAssetManagerProvider assetManagerProvider,
            I2DRenderUtilities twodRenderUtilites,
            IChunkSizePolicy chunkSizePolicy,
            IGenerationPlanner generationPlanner,
            IGeneratorResolver generatorResolver,
            ILevel level)
        {
            this.m_AssetManager = assetManagerProvider.GetAssetManager();
            this.m_2DRenderUtilities = twodRenderUtilites;
            this.m_ChunkSizePolicy = chunkSizePolicy;
            this.m_GenerationPlanner = generationPlanner;

            this.m_DefaultFont = this.m_AssetManager.Get<FontAsset>("font.Default");
            this.m_Entities = new List<IEntity>();
            this.m_Status = "Generating world...";
            this.m_Level = level;

            // Create plan and execute in a seperate thread.
            var t = new Thread(() =>
            {
                var generator = generatorResolver.GetGeneratorForGame();
                generator.SetSeed(10000);
                var request = this.m_GenerationPlanner.CreateRequest(generator);

                this.m_Level.ScanChunks();
                for (var x = -1; x <= 1; x++)
                for (var y = -1; y <= 1; y++)
                for (var z = -1; z <= 1; z++)
                {
                    if (this.m_Level.HasChunk(
                        x * this.m_ChunkSizePolicy.ChunkCellWidth * this.m_ChunkSizePolicy.CellVoxelWidth,
                        y * this.m_ChunkSizePolicy.ChunkCellHeight * this.m_ChunkSizePolicy.CellVoxelHeight,
                        z * this.m_ChunkSizePolicy.ChunkCellDepth * this.m_ChunkSizePolicy.CellVoxelDepth))
                        continue;
                    request.AddRegion(
                        x * this.m_ChunkSizePolicy.ChunkCellWidth,
                        z * this.m_ChunkSizePolicy.ChunkCellDepth,
                        y * this.m_ChunkSizePolicy.ChunkCellHeight,
                        this.m_ChunkSizePolicy.ChunkCellWidth,
                        this.m_ChunkSizePolicy.ChunkCellDepth,
                        this.m_ChunkSizePolicy.ChunkCellHeight);
                }

                for (var x = -10; x <= 10; x++)
                for (var z = -10; z <= 10; z++)
                {
                    if (this.m_Level.HasChunk(
                        x * this.m_ChunkSizePolicy.ChunkCellWidth * this.m_ChunkSizePolicy.CellVoxelWidth,
                        0,
                        z * this.m_ChunkSizePolicy.ChunkCellDepth * this.m_ChunkSizePolicy.CellVoxelDepth))
                        continue;
                    request.AddRegion(
                        x * this.m_ChunkSizePolicy.ChunkCellWidth,
                        z * this.m_ChunkSizePolicy.ChunkCellDepth,
                        0,
                        this.m_ChunkSizePolicy.ChunkCellWidth,
                        this.m_ChunkSizePolicy.ChunkCellDepth,
                        1);
                }

                request.Progress += (sender, e) => this.m_Status = "Generating world... " + e.Progress + "%";
                request.RegionComplete += (sender, e) =>
                {
                    var cells = new Cell[this.m_ChunkSizePolicy.ChunkCellWidth,
                        this.m_ChunkSizePolicy.ChunkCellHeight,
                        this.m_ChunkSizePolicy.ChunkCellDepth];
                    for (var x = 0; x < e.Region.GeneratedData.GetLength(0); x++)
                    for (var y = 0; y < e.Region.GeneratedData.GetLength(1); y++)
                    for (var z = 0; z < e.Region.GeneratedData.GetLength(2); z++)
                        cells[x, y, z] = e.Region.GeneratedData[x, y, z];
                    this.m_Level.SaveChunk(
                        e.Region.X * this.m_ChunkSizePolicy.CellVoxelWidth,
                        e.Region.Y * this.m_ChunkSizePolicy.CellVoxelHeight,
                        e.Region.Z * this.m_ChunkSizePolicy.CellVoxelDepth,
                        cells);
                };
                this.m_GenerationPlanner.Execute(request);
            });
            t.IsBackground = true;
            t.Start();
        }