Exemple #1
0
        public bool BoundingBoxIntersectsWorld(BoundingBox box)
        {
            HashSet<VoxelChunk> chunksIntersecting = new HashSet<VoxelChunk>();
            GetChunksIntersecting(box, chunksIntersecting);

            return chunksIntersecting.Count > 0 || GeneratedChunks.Any(chunk => chunk.GetBoundingBox().Intersects(box));
        }
Exemple #2
0
        public void GenerateInitialChunks(OrbitCamera camera, Point3 origin, ref string message)
        {
            float origBuildRadius = GenerateDistance;

            GenerateDistance = origBuildRadius * 2.0f;

            int i     = 0;
            int iters = WorldSize.X * WorldSize.Y * WorldSize.Z;

            for (int dx = origin.X - WorldSize.X / 2 + 1; dx < origin.X + WorldSize.X / 2; dx++)
            {
                for (int dy = origin.Y - WorldSize.Y / 2; dy <= origin.Y + WorldSize.Y / 2; dy++)
                {
                    for (int dz = origin.Z - WorldSize.Z / 2 + 1; dz < origin.Z + WorldSize.Z / 2; dz++)
                    {
                        message = "Generating : " + (i + 1) + "/" + iters;
                        i++;

                        Point3 box = new Point3(dx, dy, dz);

                        if (!ChunkData.ChunkMap.ContainsKey(box))
                        {
                            Vector3    worldPos = new Vector3(box.X * ChunkData.ChunkSizeX, box.Y * ChunkData.ChunkSizeY, box.Z * ChunkData.ChunkSizeZ);
                            VoxelChunk chunk    = ChunkGen.GenerateChunk(worldPos, (int)ChunkData.ChunkSizeX, (int)ChunkData.ChunkSizeY, (int)ChunkData.ChunkSizeZ, Components, Content, Graphics);
                            chunk.ShouldRebuild             = true;
                            chunk.ShouldRecalculateLighting = true;
                            chunk.IsVisible = true;
                            chunk.ResetSunlight(0);
                            GeneratedChunks.Enqueue(chunk);
                            foreach (VoxelChunk chunk2 in GeneratedChunks)
                            {
                                if (!ChunkData.ChunkMap.ContainsKey(chunk2.ID))
                                {
                                    ChunkData.AddChunk(chunk2);
                                    RecalculateBounds();
                                }
                            }
                        }
                    }
                }
            }

            UpdateRebuildList();
            GenerateDistance = origBuildRadius;

            while (GeneratedChunks.Count > 0)
            {
                VoxelChunk gen = null;
                if (!GeneratedChunks.TryDequeue(out gen))
                {
                    break;
                }
            }

            ChunkData.ChunkManager.CreateGraphics(ref message, ChunkData);
        }
Exemple #3
0
        public void GenerateThread()
        {
            EventWaitHandle[] waitHandles =
            {
                NeedsGenerationEvent,
                Program.ShutdownEvent
            };

#if CREATE_CRASH_LOGS
            try
#endif
            {
                while (!ExitThreads)
                {
                    EventWaitHandle wh = Datastructures.WaitFor(waitHandles);

                    //GeneratorLock.WaitOne();

                    if (!PauseThreads && ToGenerate != null && ToGenerate.Count > 0)
                    {
                        Point3 box = ToGenerate[0];

                        if (!ChunkData.ChunkMap.ContainsKey(box))
                        {
                            Vector3 worldPos = new Vector3(box.X * ChunkData.ChunkSizeX, box.Y * ChunkData.ChunkSizeY, box.Z * ChunkData.ChunkSizeZ);
                            VoxelChunk chunk = ChunkGen.GenerateChunk(worldPos, (int)ChunkData.ChunkSizeX, (int)ChunkData.ChunkSizeY, (int)ChunkData.ChunkSizeZ, Components, Content, Graphics);
                            Drawer3D.DrawBox(chunk.GetBoundingBox(), Color.Red, 0.1f);
                            chunk.ShouldRebuild = true;
                            chunk.ShouldRecalculateLighting = true;
                            GeneratedChunks.Enqueue(chunk);
                        }

                        ToGenerate.Remove(box);
                    }


                    //GeneratorLock.ReleaseMutex();
                    if (wh == Program.ShutdownEvent)
                    {
                        break;
                    }
                }
            }
#if CREATE_CRASH_LOGS
            catch (Exception exception)
            {
                ProgramData.WriteExceptionLog(exception);
            }
#endif

           
        }
Exemple #4
0
        public void Load(Chunk item, Transform parent)
        {
            if (!GeneratedChunks.ContainsID(item.ID) || LoadedChunks.Contains(item))
            {
                return;
            }

            LoadedChunks.Add(item);

            var chunkObj = new GameObject("Chunk:" + item.IDStr);

            item.Object      = GameObject.Instantiate(chunkObj, parent);
            item.Object.name = item.Object.name.Substring(0, item.Object.name.Length - 7);

            if (GameSystem.TileSelected && item.Contains(GameSystem.SelectedTile))
            {
                GameObject selector = VisualContainer.CreateObject("ui:selector", GameSystem.SelectedTile, 1.1f, item.Object.transform);
                item.Objects.Add(selector);
            }

            foreach (var block in item.Blocks)
            {
                if (!item.Contains(block.Location))
                {
                    Debug.Log(string.Format("Attempting to load Block {0},{1} into Chunk {2} has failed.", block.Location.X.ToString(), block.Location.Y.ToString(), item.IDStr));
                }

                GameObject obj = VisualContainer.CreateObject(block.Tile.Type.IdName, block.Location, 0, item.Object.transform);
                item.Objects.Add(obj);

                if (block.Overlay.Type != ObjectID.ENV.VOID)
                {
                    GameObject overlayObj = VisualContainer.CreateObject(block.Overlay.Type.IdName, block.Location, 0.1f, item.Object.transform);
                    item.Objects.Add(overlayObj);
                }
            }

            GameObject.Destroy(chunkObj);
        }
Exemple #5
0
        public void Update(DwarfTime gameTime, Camera camera, GraphicsDevice g)
        {
            UpdateRenderList(camera);

            if (waterUpdateTimer.Update(gameTime))
            {
                WaterUpdateEvent.Set();
            }

            UpdateRebuildList();

            generateChunksTimer.Update(gameTime);
            if (generateChunksTimer.HasTriggered)
            {
                if (ToGenerate.Count > 0)
                {
                    NeedsGenerationEvent.Set();
                    ChunkData.RecomputeNeighbors();
                }

                foreach (VoxelChunk chunk in GeneratedChunks)
                {
                    if (!ChunkData.ChunkMap.ContainsKey(chunk.ID))
                    {
                        ChunkData.AddChunk(chunk);
                        ChunkGen.GenerateVegetation(chunk, Components, Content, Graphics);
                        ChunkGen.GenerateFauna(chunk, Components, Content, Graphics, PlayState.ComponentManager.Factions);
                        List <VoxelChunk> adjacents = ChunkData.GetAdjacentChunks(chunk);
                        foreach (VoxelChunk c in adjacents)
                        {
                            c.ShouldRecalculateLighting = true;
                            c.ShouldRebuild             = true;
                        }
                        RecalculateBounds();
                    }
                }

                while (GeneratedChunks.Count > 0)
                {
                    VoxelChunk gen = null;
                    if (!GeneratedChunks.TryDequeue(out gen))
                    {
                        break;
                    }
                }
            }


            visibilityChunksTimer.Update(gameTime);
            if (visibilityChunksTimer.HasTriggered)
            {
                visibleSet.Clear();
                GetChunksIntersecting(camera.GetFrustrum(), visibleSet);
                //RemoveDistantBlocks(camera);
            }


            foreach (VoxelChunk chunk in ChunkData.ChunkMap.Values)
            {
                chunk.Update(gameTime);
            }

            Water.Splash(gameTime);
            Water.HandleTransfers(gameTime);

            HashSet <VoxelChunk> affectedChunks = new HashSet <VoxelChunk>();

            foreach (Voxel voxel in KilledVoxels)
            {
                affectedChunks.Add(voxel.Chunk);
                voxel.Chunk.NotifyDestroyed(new Point3(voxel.GridPosition));
                if (!voxel.IsInterior)
                {
                    foreach (KeyValuePair <Point3, VoxelChunk> n in voxel.Chunk.Neighbors)
                    {
                        affectedChunks.Add(n.Value);
                    }
                }
            }

            if (GameSettings.Default.FogofWar)
            {
                ChunkData.Reveal(KilledVoxels);
            }

            lock (RebuildList)
            {
                foreach (VoxelChunk affected in affectedChunks)
                {
                    affected.NotifyTotalRebuild(false);
                }
            }
            KilledVoxels.Clear();
        }
Exemple #6
0
        public void GenerateInitialChunks(Point3 origin, Action <String> SetLoadingMessage)
        {
            float origBuildRadius = GenerateDistance;

            GenerateDistance = origBuildRadius * 2.0f;

            int i     = 0;
            int iters = WorldSize.X * WorldSize.Y * WorldSize.Z;

            for (int dx = origin.X - WorldSize.X / 2 + 1; dx < origin.X + WorldSize.X / 2; dx++)
            {
                for (int dy = origin.Y - WorldSize.Y / 2; dy <= origin.Y + WorldSize.Y / 2; dy++)
                {
                    for (int dz = origin.Z - WorldSize.Z / 2 + 1; dz < origin.Z + WorldSize.Z / 2; dz++)
                    {
                        SetLoadingMessage("Generating : " + (i + 1) + "/" + iters);
                        i++;

                        Point3 box = new Point3(dx, dy, dz);

                        if (!ChunkData.ChunkMap.ContainsKey(box))
                        {
                            Vector3    worldPos = new Vector3(box.X * ChunkData.ChunkSizeX, box.Y * ChunkData.ChunkSizeY, box.Z * ChunkData.ChunkSizeZ);
                            VoxelChunk chunk    = ChunkGen.GenerateChunk(worldPos, (int)ChunkData.ChunkSizeX, (int)ChunkData.ChunkSizeY, (int)ChunkData.ChunkSizeZ, Components, Content, Graphics);
                            chunk.ShouldRebuild             = true;
                            chunk.ShouldRecalculateLighting = true;
                            chunk.IsVisible = true;
                            chunk.ResetSunlight(0);
                            GeneratedChunks.Enqueue(chunk);
                            foreach (VoxelChunk chunk2 in GeneratedChunks)
                            {
                                if (!ChunkData.ChunkMap.ContainsKey(chunk2.ID))
                                {
                                    ChunkData.AddChunk(chunk2);
                                    RecalculateBounds();
                                }
                            }
                        }
                    }
                }
            }
            RecalculateBounds();
            chunkData.RecomputeNeighbors();
            SetLoadingMessage("Generating Ores...");

            GenerateOres();

            SetLoadingMessage("Fog of war...");
            // We are going to force fog of war to be on for the first reveal then reset it back to it's previous setting after.
            // This is a pseudo hack to stop worlds created with Fog of War off then looking awful if it is turned back on.
            bool fogOfWar = GameSettings.Default.FogofWar;

            GameSettings.Default.FogofWar = true;
            ChunkData.Reveal(GeneratedChunks.First().MakeVoxel(0, (int)ChunkData.ChunkSizeY - 1, 0));
            GameSettings.Default.FogofWar = fogOfWar;

            UpdateRebuildList();
            GenerateDistance = origBuildRadius;

            while (GeneratedChunks.Count > 0)
            {
                VoxelChunk gen = null;
                if (!GeneratedChunks.TryDequeue(out gen))
                {
                    break;
                }
            }

            ChunkData.ChunkManager.CreateGraphics(SetLoadingMessage, ChunkData);
        }
Exemple #7
0
 public int GeneratedCount() => GeneratedChunks.Count();
Exemple #8
0
 public void Generate(Chunk item)
 {
     item.Generate();
     GeneratedChunks.Add(item);
 }