Ejemplo n.º 1
0
        public bool TryGet(Vector3I chunkLocation, out LandscapeChunkBuffer buffer)
        {
            lock (_syncLock)
            {
                if (_buffers.TryGetValue(chunkLocation, out buffer) == false)
                {
                    buffer = new LandscapeChunkBuffer()
                    {
                        ChunkLocation = chunkLocation
                    };
                    _buffers.Add(chunkLocation, buffer);
                }

                if (buffer.isReady)
                {
                    return(true);
                }
                if (buffer.isLocked)
                {
                    return(false);
                }
                buffer.isLocked = true;
            }

            GenerateLandscapeBuffer(buffer);
            buffer.isLocked = false;
            return(true);
        }
Ejemplo n.º 2
0
        public void Insert(Vector3I chunkLocation, LandscapeEntity entity)
        {
            LandscapeChunkBuffer buffer;

            lock (_syncLock)
            {
                if (_buffers.TryGetValue(chunkLocation, out buffer) == false)
                {
                    buffer = new LandscapeChunkBuffer()
                    {
                        ChunkLocation = chunkLocation
                    };
                    _buffers.Add(chunkLocation, buffer);
                }
            }

            if (buffer.Entities == null)
            {
                buffer.Entities = new List <LandscapeEntity>();
            }
            buffer.Entities.Add(entity);
        }
Ejemplo n.º 3
0
        private void GenerateLandscapeBuffer(LandscapeChunkBuffer buffer)
        {
            //Get the minimum chunk range that need to be computed to validate current chunk landscape entities generation
            var chunkRange   = new Range3I(buffer.ChunkLocation - _chunkRangeLookUp, new Vector3I(_chunkRangeLookUp.X * 2, 1, _chunkRangeLookUp.Z * 2));
            var chunkBuffers = new List <LandscapeChunkBuffer>(chunkRange.Count);

            foreach (var chunkPosition in chunkRange)
            {
                //Check if all those chunks have been / have their landscape item processed (They could potentially impact this chunk).
                LandscapeChunkBuffer surrendingChunkBuffer;
                bool need2Process = false;
                lock (_syncLock)
                {
                    if (_buffers.TryGetValue(chunkPosition, out surrendingChunkBuffer) == false)
                    {
                        surrendingChunkBuffer = new LandscapeChunkBuffer {
                            ChunkLocation   = chunkPosition,
                            ProcessingState = LandscapeChunkBuffer.LandscapeChunkBufferState.NotProcessed
                        };
                        _buffers.Add(chunkPosition, surrendingChunkBuffer);
                    }

                    if (surrendingChunkBuffer.ProcessingState == LandscapeChunkBuffer.LandscapeChunkBufferState.NotProcessed)
                    {
                        surrendingChunkBuffer.ProcessingState = LandscapeChunkBuffer.LandscapeChunkBufferState.Processing;
                        need2Process = true;
                    }
                }

                chunkBuffers.Add(surrendingChunkBuffer);

                if (need2Process)
                {
                    //Process Landscape
                    FastRandom        chunkNewRnd;
                    Biome             chunkMasterBiome;
                    ChunkColumnInfo[] columnsInfo;
                    byte[]            chunkBytes;
                    Processor.GenerateMacroLandscape(surrendingChunkBuffer.ChunkLocation, out chunkMasterBiome, out chunkBytes, out chunkNewRnd, out columnsInfo);

                    //Process chunk Entity landscape creation
                    Processor.LandscapeEntities.GenerateChunkItems(surrendingChunkBuffer.ChunkLocation, chunkMasterBiome, chunkBytes, columnsInfo, chunkNewRnd);

                    surrendingChunkBuffer.chunkBytesBuffer  = chunkBytes;
                    surrendingChunkBuffer.ColumnsInfoBuffer = columnsInfo;

                    surrendingChunkBuffer.ProcessingState = LandscapeChunkBuffer.LandscapeChunkBufferState.Processed;
                }
            }

            //Here, all chunk have been at least processed, some could still be in Processing state, waiting for all of them to finish.
            while (chunkBuffers.Count(x => x.ProcessingState != LandscapeChunkBuffer.LandscapeChunkBufferState.Processed) > 0)
            {
                //Wait for all lanscape entity from our range to be processed
                Thread.Sleep(1);
            }

            Processor.GenerateMicroLandscape(buffer);

            //Sort the final landscape entity items by their chunk origine.
            if (buffer.Entities != null)
            {
                buffer.Entities = buffer.Entities.OrderBy(x => x.ChunkLocation.GetHashCode()).ToList();
            }

            buffer.isReady = true;
        }