Example #1
0
        private void PullTransparencyCacheFromAdjacentBlock(ChunkSubspacePosition position,
                                                            int xOffset, int yOffset, int zOffset)
        {
            ChunkSubspacePosition checkPosition;

            checkPosition    = position;
            checkPosition.x += xOffset;
            checkPosition.y += yOffset;
            checkPosition.z += zOffset;

            if (checkPosition.x >= 0 && checkPosition.x < SIZE &&
                checkPosition.y >= 0 && checkPosition.y < SIZE &&
                checkPosition.z >= 0 && checkPosition.z < SIZE)
            {
                SetAdjacentBlockTransparentFlag(position, xOffset, yOffset, zOffset,
                                                GetBlock(checkPosition).IsTransparent());
            }
            else
            {
                BlockSpacePosition blockPosition = checkPosition.GetBlockSpacePosition(this);
                Chunk otherChunk = ChunkRepository.GetChunkAtPosition(blockPosition);

                if (otherChunk != null)
                {
                    ChunkSubspacePosition otherChunkPosition = blockPosition.GetChunkSubspacePosition(otherChunk);

                    SetAdjacentBlockTransparentFlag(position, xOffset, yOffset, zOffset,
                                                    otherChunk.GetBlock(otherChunkPosition).IsTransparent());
                }
            }
        }
Example #2
0
 private void PutInChunkProcessingList()
 {
     if (!isInChunkProcessingList)
     {
         ChunkRepository.AddToProcessingChunkList(this);
     }
 }
Example #3
0
        private void PushTransparencyCacheToAdjacentBlock(ChunkSubspacePosition position, int xOffset, int yOffset, int zOffset,
                                                          bool transparent)
        {
            position.x += xOffset;
            position.y += yOffset;
            position.z += zOffset;

            if (position.x >= 0 && position.x < SIZE &&
                position.y >= 0 && position.y < SIZE &&
                position.z >= 0 && position.z < SIZE)
            {
                SetAdjacentBlockTransparentFlag(position, -xOffset, -yOffset, -zOffset, transparent);
            }
            else
            {
                BlockSpacePosition blockPosition = position.GetBlockSpacePosition(this);
                Chunk otherChunk = ChunkRepository.GetChunkAtPosition(blockPosition);

                if (otherChunk != null)
                {
                    ChunkSubspacePosition otherChunkPosition = blockPosition.GetChunkSubspacePosition(otherChunk);

                    otherChunk.SetAdjacentBlockTransparentFlag(otherChunkPosition,
                                                               -xOffset, -yOffset, -zOffset, transparent);
                }
            }
        }
Example #4
0
        private List <Chunk> LockNearbyChunkModels()
        {
            List <Chunk> lockedChunks = new List <Chunk>();
            int          maxOffset    = (int)Mathf.Ceil(Configuration.MAX_MODEL_RADIUS / (float)SIZE);
            int          maxXIttr     = worldPosition.x + maxOffset;
            int          maxYIttr     = worldPosition.y + maxOffset;
            int          maxZIttr     = worldPosition.z + maxOffset;

            for (int xIttr = worldPosition.x - maxOffset; xIttr <= maxXIttr; xIttr++)
            {
                for (int yIttr = worldPosition.y - maxOffset; yIttr <= maxYIttr; yIttr++)
                {
                    for (int zIttr = worldPosition.z - maxOffset; zIttr <= maxZIttr; zIttr++)
                    {
                        ChunkSpacePosition position;
                        position.x = xIttr;
                        position.y = yIttr;
                        position.z = zIttr;
                        Chunk chunk = ChunkRepository.GetChunkAtPosition(position);
                        if (chunk != null)
                        {
                            Monitor.Enter(chunk.generatingModelsLock);
                            lockedChunks.Add(chunk);
                        }
                    }
                }
            }

            return(lockedChunks);
        }
Example #5
0
        private void ApplyNearbyModels()
        {
            int maxOffset = (int)Mathf.Ceil(Configuration.MAX_MODEL_RADIUS / (float)SIZE);
            int maxXIttr  = worldPosition.x + maxOffset;
            int maxYIttr  = worldPosition.y + maxOffset;
            int maxZIttr  = worldPosition.z + maxOffset;

            for (int xIttr = worldPosition.x - maxOffset; xIttr <= maxXIttr; xIttr++)
            {
                for (int yIttr = worldPosition.y - maxOffset; yIttr <= maxYIttr; yIttr++)
                {
                    for (int zIttr = worldPosition.z - maxOffset; zIttr <= maxZIttr; zIttr++)
                    {
                        ChunkSpacePosition position;
                        position.x = xIttr;
                        position.y = yIttr;
                        position.z = zIttr;
                        Chunk chunk = ChunkRepository.GetChunkAtPosition(position);
                        if (chunk != null)
                        {
                            foreach (Model model in chunk.IterateModels())
                            {
                                model.template.ApplyToChunk(model.position, this);
                            }
                        }
                    }
                }
            }
        }
Example #6
0
 public ChunkAgentOptions(ChunkRepository repository)
 {
     if (repository == null)
     {
         throw new ArgumentNullException("repository");
     }
     this.repository = repository;
 }
Example #7
0
        void OnApplicationQuit()
        {
            AsyncService.GetCPUMediator().Shutdown();

            UnityEngine.Debug.Log("Saving all loaded chunks...");
            ChunkRepository.SaveAllLoadedChunks();
            FileRepository.Shutdown();
            UnityEngine.Debug.Log("Done.");
        }
Example #8
0
        private Chunk GetAdjacentChunk(int xOffset, int yOffset, int zOffset)
        {
            ChunkSpacePosition position = worldPosition;

            position.x += xOffset;
            position.y += yOffset;
            position.z += zOffset;
            return(ChunkRepository.GetChunkAtPosition(position));
        }
Example #9
0
        static void OpenLevelDBRepository(string[] commandParts)
        {
            string          nick  = commandParts [2];
            string          path  = commandParts [3];
            ChunkRepository LRepo = new ChunkRepository(path);

            lock (repositories)
                repositories.Add(nick, LRepo);
        }
Example #10
0
 void OnGUI()
 {
     if (showDebugMenu)
     {
         GUI.Box(uiThreadQueueSizeRect, "Thread queue size: " + AsyncService.ThreadQueueSize().ToString());
         GUI.Box(uiProcessingListSizeRect, "Processing chunk queue size: " + ChunkRepository.GetProcessingChunkListSize().ToString());
         GUI.Box(uiTotalChunkCountRect, "Chunk pool size: " + GeneratorService.TotalChunkCount().ToString());
         GUI.Box(uiUnloadChunkCountRect, "Queued chunk unloads: " + GeneratorService.UnloadChunksListCount().ToString());
     }
 }
Example #11
0
        public async void CreateChunks()
        {
            var repo         = new ChunkRepository();
            var originPoints = new[] { TilePosition.Zero, new TilePosition(0, 0, 1) };
            var command      = new CreateChunksCommand(originPoints);

            var chunk = await repo.AddChunks(command, CancellationToken.None);

            Assert.IsAssignableFrom <IEnumerable <Chunk> >(chunk);
        }
Example #12
0
        void Start()
        {
            if (player == null)
            {
                throw new MissingComponentException("Missing Player component");
            }
            if (blockParticle == null)
            {
                throw new MissingComponentException("Missing Block Particle component");
            }
            if (chunkMeshPrefab == null)
            {
                throw new MissingComponentException("Missing Chunk Mesh component");
            }
            if (solidBlockMaterial == null)
            {
                throw new MissingComponentException("Missing Solid Block Material component");
            }
            if (transparentBlockMaterial == null)
            {
                throw new MissingComponentException("Missing Transparent Block Material component");
            }
            if (waterBlockMaterial == null)
            {
                throw new MissingComponentException("Missing Water Block Material component");
            }

            Vector3 playerStartPosition;

            playerStartPosition.x = 64.5f;
            playerStartPosition.y = 148;
            playerStartPosition.z = 64.5f;
            Instantiate(player, playerStartPosition, Quaternion.identity);

            instance = this;
            BlockDefinition.InitializeAllTypes();
            AsyncService.Initialize();
            ChunkRepository.Initialize();
            CollisionService.Initialize();
            GeneratorService.Initialize();
            RenderService.Initialize();

            SetSeed(UnityEngine.Random.Range(Int32.MinValue + 1, Int32.MaxValue - 1));

            fogDistance = 0.0f;

            AsyncService.Load();

            // Remove the next line if you want your game to stop processing when it loses focus
            Application.runInBackground = true;
        }
Example #13
0
        public ChunkAgent(ServiceAgentMode serviceAgentMode, ObjectBusSession objectBusSession, Action flush, ChunkRepository repository)
            : base(serviceAgentMode, objectBusSession, flush, false)
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }
            this.repository = repository;
            objectBusSession.RegisterType(typeof(TopLevelChunksRequestMessage), RequestTopLevelChunkDeltaMessageReceived);
//			objectBusSession.RegisterType (typeof(BD2.Chunk.Daemon.Common.PollChunksRequestMessage), );
//			objectBusSession.RegisterType (typeof(BD2.Chunk.Daemon.Common.PollNewChunksRequestMessage), );
//			objectBusSession.RegisterType (typeof(BD2.Chunk.Daemon.Common.PushChunksRequestMessage), );
            //this.repository.Enumerate ();
        }
Example #14
0
 public void ClearAll()
 {
     lock (padlock) {
         modificationList.Clear();
         lock (adjacentTransparencyModificationList) {
             adjacentTransparencyModificationList.Clear();
         }
         SetLoadState(ChunkLoadState.LoadingFromDisk);
         ClearMeshObject();
         lights.Clear();
         models.Clear();
         ChunkRepository.RemoveFromProcessingChunkList(this);
         unload = false;
     }
 }
Example #15
0
        /// <summary>
        /// Get a single face for the mesh setting.
        /// </summary>
        /// <param name='centerPosition'>
        /// Where the center of the block resides.
        /// </param>
        /// <param name='side'>
        /// Which side of the cube to add.
        /// </param>
        /// <param name='block'>
        /// The block that the face belongs to.
        /// </param>
        /// <param name='blockAbove'>
        /// Whether there is a block above the one being represented by the face.
        /// </param>
        /// <param name='blockBelow'>
        /// Whether there is a block below the one being represented by the face.
        /// </param>
        /// <param name='hasSunlight'>
        /// Whether sunlight is reaching the block.
        /// </param>
        private void AddFace(Vector3 centerPosition, CubeSide side, Block block, bool blockAbove, bool blockBelow,
                             List <BlockLight> lightList,
                             bool shadeTopLeft, bool shadeTopRight, bool shadeBottomLeft, bool shadeBottomRight)
        {
            Vector3 vertice;
            Vector3 normalDirection = Vector3.zero;

            Color lightColor;
            ChunkSubspacePosition subspaceSamplePosition;

            subspaceSamplePosition.x = (int)centerPosition.x;
            subspaceSamplePosition.y = (int)centerPosition.y;
            subspaceSamplePosition.z = (int)centerPosition.z;
            BlockSpacePosition worldSamplePosition =
                subspaceSamplePosition.GetBlockSpacePosition(associatedChunkMeshCluster.chunk);

            lightColor = RenderService.SampleLight(lightList, worldSamplePosition, side);

            if (side == CubeSide.Top)
            {
                normalDirection = Vector3.up;

                // 0, 1, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.Bottom)
            {
                normalDirection = Vector3.down;

                // 1, 0, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 0, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 0, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 0, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.North)
            {
                normalDirection = Vector3.back;

                // 0, 0, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 0, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.South)
            {
                normalDirection = Vector3.forward;

                // 1, 0, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 0, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.West)
            {
                normalDirection = Vector3.left;

                // 0, 0, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 0, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.East)
            {
                normalDirection = Vector3.right;

                // 1, 0, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 0, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);
            }

            trianglesList.Add((int)(meshArraysIndex * 4 + 0));
            trianglesList.Add((int)(meshArraysIndex * 4 + 1));
            trianglesList.Add((int)(meshArraysIndex * 4 + 2));
            trianglesList.Add((int)(meshArraysIndex * 4 + 1));
            trianglesList.Add((int)(meshArraysIndex * 4 + 3));
            trianglesList.Add((int)(meshArraysIndex * 4 + 2));


            // Determine whether this block is exposed directly to sunlight (nothing at all above it)
            byte sunlightValue = 255;
            BlockSpacePosition checkPosition = worldSamplePosition;

            while (checkPosition.y < Configuration.HEIGHT)
            {
                checkPosition.y++;
                Block checkBlock = ChunkRepository.GetBlockAtPosition(checkPosition);
                if (checkBlock.IsNotTransparent() && checkBlock.IsActive())
                {
                    sunlightValue = 0;
                    break;
                }
            }

            Vector2 textureCoordinates    = block.GetTextureCoordinates(side, blockAbove, blockBelow, sunlightValue);
            Vector2 overallTextureSize    = block.GetOverallTextureSize();
            Vector2 individualTextureSize = block.GetIndividualTextureSize();

            Vector2 lowerUVs, upperUVs;

            lowerUVs.x = textureCoordinates.x / overallTextureSize.x;
            lowerUVs.y = 1.0f - textureCoordinates.y / overallTextureSize.y;
            upperUVs.x = (textureCoordinates.x + individualTextureSize.x) / overallTextureSize.x;
            upperUVs.y = 1.0f - (textureCoordinates.y + individualTextureSize.y) / overallTextureSize.y;

            Vector2 uv;

            uv.x = lowerUVs.x;
            uv.y = upperUVs.y;
            uvList.Add(uv);
            uv.x = lowerUVs.x;
            uv.y = lowerUVs.y;
            uvList.Add(uv);
            uv.x = upperUVs.x;
            uv.y = upperUVs.y;
            uvList.Add(uv);
            uv.x = upperUVs.x;
            uv.y = lowerUVs.y;
            uvList.Add(uv);

            normalsList.Add(normalDirection);
            normalsList.Add(normalDirection);
            normalsList.Add(normalDirection);
            normalsList.Add(normalDirection);

            Color32 color;

            color.a = 255;
            color.r = (byte)(lightColor.r * 255);
            color.g = (byte)(lightColor.g * 255);
            color.b = (byte)(lightColor.b * 255);

            Color32 shadedColor;

            if (shadeTopLeft || shadeTopRight || shadeBottomLeft || shadeBottomRight)
            {
                HSBColor hsbShadedColor = HSBColor.FromColor(lightColor);
                hsbShadedColor.b = Mathf.Max(hsbShadedColor.b - 0.37f, 0.063f);
                shadedColor      = hsbShadedColor.ToColor();
            }
            else
            {
                shadedColor = color;
            }

            colorList.Add(shadeBottomLeft ? shadedColor : color);
            colorList.Add(shadeTopLeft ? shadedColor : color);
            colorList.Add(shadeBottomRight ? shadedColor : color);
            colorList.Add(shadeTopRight ? shadedColor : color);

            meshArraysIndex++;
        }
Example #16
0
        void Update()
        {
            if (AsyncService.Loading())
            {
                return;
            }

            AsyncService.StartFrameTimer();


            if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_GENERATE_NEW_CHUNKS_DEADLINE))
            {
                Chunk newChunk = GeneratorService.GenerateNewChunk();
                if (newChunk != null)
                {
                    ChunkRepository.AddToProcessingChunkList(newChunk);
                }
            }

            int i = 0;

            while (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_START_WORK_DEADLINE) &&
                   i < ChunkRepository.GetProcessingChunkListSize())
            {
                Chunk chunk = ChunkRepository.GetProcessingChunk(i);
                if (chunk == null)
                {
                    continue;
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_FLUSH_MODIFICATIONS_DEADLINE))
                {
                    chunk.FlushModifications();
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_FINISH_MESH_DEADLINE))
                {
                    RenderService.FinishMeshGeneration(chunk);
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_GENERATE_MESH_DEADLINE) &&
                    AsyncService.ThreadQueueSize() < Configuration.PERFORMANCE_MAX_THREAD_QUEUE_SIZE)
                {
                    RenderService.GenerateMeshes(chunk); // Threaded
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_MARK_CHUNKS_FOR_MESH_UPDATE_DEADLINE))
                {
                    RenderService.MarkSurroundingChunksForMeshUpdate(chunk);
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_GENERATE_BLOCKS_DEADLINE) &&
                    AsyncService.ThreadQueueSize() < Configuration.PERFORMANCE_MAX_THREAD_QUEUE_SIZE)
                {
                    chunk.GenerateBlocks(GetSeed()); // Threaded
                }

                if (chunk.IsFinishedProcessing())
                {
                    ChunkRepository.RemoveFromProcessingChunkList(chunk);
                }

                i++;
            }

            GeneratorService.UnloadDeadChunks();
            RenderService.CullChunks();

            int numberOfChunks = ChunkRepository.NumberOfChunks();

            for (int chunkIndex = 0; chunkIndex < numberOfChunks; chunkIndex++)
            {
                Chunk chunk = ChunkRepository.GetChunkAtIndex(chunkIndex);
                if (chunk.IsFinishedProcessing())
                {
                    Vector3 position;
                    position.x = chunk.WorldPosition().x *Chunk.SIZE;
                    position.y = chunk.WorldPosition().y *Chunk.SIZE;
                    position.z = chunk.WorldPosition().z *Chunk.SIZE;

                    float distance = Vector3.Distance(position, Camera.main.transform.position);
                    if (distance > fogDistance)
                    {
                        fogDistance = distance;
                    }
                }
            }
            SendMessage("UpdateFogDistance", fogDistance, SendMessageOptions.DontRequireReceiver);

            GeneratorService.CleanupOldChunks();
            AsyncService.RePrioritizeCPUMediatorWork();
            ChunkRepository.RePrioritizeSortProcessingChunkList();
            ChunkRepository.FlushProcessingChunkListModifications();

            if (Input.GetKeyDown(KeyCode.F12))
            {
                showDebugMenu = !showDebugMenu;
            }

            if (Input.GetKeyDown(KeyCode.F5))
            {
                SaveScreenshot();
            }

            if (Input.GetKeyDown(KeyCode.F9))
            {
                ChunkRepository.DumpProcessingChunkListDebugData();
            }

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Application.Quit();
            }
        }
Example #17
0
 public static ServiceAgent CreateAgent(ServiceAgentMode serviceAgentMode, ObjectBusSession objectBusSession, Action flush, ChunkRepository repository)
 {
     if (repository == null)
     {
         throw new ArgumentNullException("repository");
     }
     return(new ChunkAgent(serviceAgentMode, objectBusSession, flush, repository));
 }
Example #18
0
        void GenerateBlocksThread(object chunkInstance)
        {
            Chunk chunk = chunkInstance as Chunk;

            ChunkSubspacePosition position;
            BlockSpacePosition    checkPosition;

            for (position.x = 0; position.x < SIZE; position.x++)
            {
                checkPosition.x = worldPosition.x * SIZE + position.x;
                for (position.z = 0; position.z < SIZE; position.z++)
                {
                    checkPosition.z = worldPosition.z * SIZE + position.z;
                    isShorelineCache[position.x, position.z] = true;

                    for (position.y = worldPosition.y * SIZE + SIZE; position.y < Configuration.HEIGHT; position.y++)
                    {
                        checkPosition.y = position.y;
                        Block checkBlock = ChunkRepository.GetBlockAtPosition(checkPosition);
                        if (checkBlock.IsActive() && checkBlock.IsNotTransparent())
                        {
                            isShorelineCache[position.x, position.z] = false;
                            break;
                        }
                    }
                }
            }

            BlockType[,,] blockTypes = worldGenerator.GenerateBlocks(chunk);

            for (position.x = 0; position.x < SIZE; position.x++)
            {
                for (position.y = 0; position.y < SIZE; position.y++)
                {
                    for (position.z = 0; position.z < SIZE; position.z++)
                    {
                        BlockDefinition blockDefinition = BlockDefinition.DefinitionOfType(blockTypes[position.x, position.y, position.z]);
                        chunk.SetBlock(position, blockDefinition, false);

                        if (blockDefinition.IsActive() && blockDefinition.IsNotTransparent() &&
                            blockDefinition.GetBlockType() != BlockType.Sand)
                        {
                            isShorelineCache[position.x, position.z] = false;
                        }

                        if (blockDefinition.IsLightEmitter())
                        {
                            BlockLight light;
                            light.chunk           = this;
                            light.chunkPosition   = position;
                            light.blockDefinition = blockDefinition;
                            lock (chunk.padlock) {
                                chunk.lights.Add(light);
                            }
                        }
                    }
                }
            }

            // Generate and apply the models from this and all nearby chunks
            List <Model> generatedModels = worldGenerator.GenerateModels(chunk);

            lock (generatingModelsLock) {
                for (int i = 0; i < generatedModels.Count; i++)
                {
                    AddModel(generatedModels[i]);
                }
            }

            List <Chunk> lockedChunks = LockNearbyChunkModels();

            ApplyModels();
            UnlockChunkModels(lockedChunks);

            // Cleanup pass
            for (position.x = 0; position.x < SIZE; position.x++)
            {
                for (position.z = 0; position.z < SIZE; position.z++)
                {
                    for (position.y = SIZE - 1; position.y >= 0; position.y--)
                    {
                        chunk.GetBlock(position);

                        // If the block is water, make sure it's surrounded on the bottom and sides
                        if (chunk.GetBlock(position).IsWater())
                        {
                            for (int i = 0; i < 5; i++)
                            {
                                ChunkSubspacePosition solidCheckPosition = position;
                                if (i == 0)
                                {
                                    solidCheckPosition.y -= 1;
                                }
                                else if (i == 1)
                                {
                                    solidCheckPosition.x -= 1;
                                }
                                else if (i == 2)
                                {
                                    solidCheckPosition.x += 1;
                                }
                                else if (i == 3)
                                {
                                    solidCheckPosition.z -= 1;
                                }
                                else if (i == 4)
                                {
                                    solidCheckPosition.z += 1;
                                }

                                if (solidCheckPosition.x >= 0 && solidCheckPosition.x < SIZE &&
                                    solidCheckPosition.y >= 0 && solidCheckPosition.y < SIZE &&
                                    solidCheckPosition.z >= 0 && solidCheckPosition.z < SIZE)
                                {
                                    if (chunk.GetBlock(solidCheckPosition).IsNotActive())
                                    {
                                        chunk.SetBlock(solidCheckPosition, BlockType.Stone, false);
                                    }
                                }
                                else
                                {
                                    BlockSpacePosition checkWorldPosition = solidCheckPosition.GetBlockSpacePosition(chunk);
                                    if (ChunkRepository.GetBlockAtPosition(checkWorldPosition).IsNotActive())
                                    {
                                        ChunkRepository.SetBlockAtPosition(checkWorldPosition, BlockType.Stone, false);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            LoadTransparencyCache();

            chunk.SetLoadState(ChunkLoadState.BlockGenerationComplete);
        }