コード例 #1
0
ファイル: ChunkGenerator.cs プロジェクト: svifylabs/dwarfcorp
        public void GenerateWater(VoxelChunk chunk)
        {
            int   waterHeight = (int)(SeaLevel * chunk.SizeY);
            Voxel voxel       = chunk.MakeVoxel(0, 0, 0);

            for (int x = 0; x < chunk.SizeX; x++)
            {
                for (int z = 0; z < chunk.SizeZ; z++)
                {
                    int h;
                    for (int y = 0; y < waterHeight; y++)
                    {
                        h = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z);
                        int index = chunk.Data.IndexAt(x, y, z);
                        voxel.GridPosition = new Vector3(x, y, z);
                        if (voxel.IsEmpty && y >= h)
                        {
                            chunk.Data.Water[index].WaterLevel = 255;
                            chunk.Data.Water[index].Type       = LiquidType.Water;
                        }
                    }


                    Vector2 vec = new Vector2(x + chunk.Origin.X, z + chunk.Origin.Z) / PlayState.WorldScale;
                    if (Overworld.GetWater(Overworld.Map, vec) != Overworld.WaterType.Volcano)
                    {
                        continue;
                    }

                    h = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z);


                    if (h <= 0)
                    {
                        continue;
                    }

                    chunk.Data.Water[chunk.Data.IndexAt(x, h, z)].WaterLevel = 255;
                    chunk.Data.Water[chunk.Data.IndexAt(x, h, z)].Type       = LiquidType.Lava;

                    /*
                     * for (int y = h - 1; y >= 0; y--)
                     * {
                     *  voxel.Chunk = chunk;
                     *  voxel.GridPosition = new Vector3(x, y, z);
                     *  chunk.Data.Water[voxel.Index].Type = LiquidType.None;
                     *  chunk.Data.Water[voxel.Index].WaterLevel = 0;
                     *  voxel.Type = VoxelLibrary.GetVoxelType("Stone");
                     *  voxel.Chunk.NotifyTotalRebuild(!voxel.IsInterior);
                     * }
                     */
                }
            }
        }
コード例 #2
0
        public override void Update(DwarfTime gameTime, ChunkManager chunks, Camera camera)
        {
            UpdateTimer.Update(gameTime);
            if (HasMoved && UpdateTimer.HasTriggered)
            {
                Body p = (Body)Parent;

                VoxelChunk chunk = chunks.ChunkData.GetVoxelChunkAtWorldLocation(p.GlobalTransform.Translation);

                if (chunk != null)
                {
                    Vector3 g = chunk.WorldToGrid(p.GlobalTransform.Translation + Vector3.Down * 0.25f);

                    int h = chunk.GetFilledVoxelGridHeightAt((int)g.X, (int)g.Y, (int)g.Z);

                    if (h != -1)
                    {
                        Vector3 pos = p.GlobalTransform.Translation;
                        pos.Y = h;
                        pos  += VertexNoise.GetNoiseVectorFromRepeatingTexture(pos + Vector3.Down * 0.25f);
                        float  scaleFactor = GlobalScale / (Math.Max((p.GlobalTransform.Translation.Y - h) * 0.25f, 1));
                        Matrix newTrans    = OriginalTransform;
                        newTrans            *= Matrix.CreateScale(scaleFactor);
                        newTrans.Translation = (pos - p.GlobalTransform.Translation) + new Vector3(0.0f, 0.1f, 0.0f);
                        Tint           = new Color(Tint.R, Tint.G, Tint.B, (int)(scaleFactor * 255));
                        LocalTransform = newTrans;
                    }
                }
                UpdateTimer.HasTriggered = false;
            }


            base.Update(gameTime, chunks, camera);
        }
コード例 #3
0
ファイル: ChunkGenerator.cs プロジェクト: svifylabs/dwarfcorp
        public void GenerateCaves(VoxelChunk chunk)
        {
            Vector3 origin     = chunk.Origin;
            int     chunkSizeX = chunk.SizeX;
            int     chunkSizeY = chunk.SizeY;
            int     chunkSizeZ = chunk.SizeZ;

            for (int x = 0; x < chunkSizeX; x++)
            {
                for (int z = 0; z < chunkSizeZ; z++)
                {
                    int h = chunk.GetFilledVoxelGridHeightAt(x, chunkSizeY - 1, z);
                    for (int y = 1; y < chunkSizeY; y++)
                    {
                        if (y >= h - 5)
                        {
                            continue;
                        }

                        float caviness = (float)NoiseGenerator.Noise((float)(x + origin.X) * CaveNoiseScale, (float)(z + origin.Z) * CaveNoiseScale, (float)(y + origin.Y) * CaveNoiseScale);
                        if (!(caviness > 0.9f))
                        {
                            continue;
                        }

                        chunk.Data.Types[chunk.Data.IndexAt(x, y, z)] = 0;
                    }
                }
            }
        }
コード例 #4
0
        public void GenerateAquifers(VoxelChunk chunk)
        {
            Vector3 origin     = chunk.Origin;
            int     chunkSizeX = chunk.SizeX;
            int     chunkSizeY = chunk.SizeY;
            int     chunkSizeZ = chunk.SizeZ;

            for (int x = 0; x < chunkSizeX; x++)
            {
                for (int z = 0; z < chunkSizeZ; z++)
                {
                    int h = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z);
                    for (int i = 0; i < AquiverLevels.Count; i++)
                    {
                        int y = AquiverLevels[i];
                        if (y <= 0 || y >= h)
                        {
                            continue;
                        }

                        double caveNoise = AquiferNoise.GetValue((x + origin.X) * CaveNoiseScale, (y + origin.Y) * 3.0f, (z + origin.Z) * CaveNoiseScale);


                        if (caveNoise > AquiferSize)
                        {
                            chunk.Data.Types[chunk.Data.IndexAt(x, y, z)]            = 0;
                            chunk.Data.Water[chunk.Data.IndexAt(x, y, z)].WaterLevel = 8;
                            chunk.Data.Water[chunk.Data.IndexAt(x, y, z)].Type       = LiquidType.Water;
                        }
                    }
                }
            }
        }
コード例 #5
0
        public void GenerateWater(VoxelChunk chunk)
        {
            int   waterHeight = (int)(SeaLevel * chunk.SizeY);
            Voxel voxel       = chunk.MakeVoxel(0, 0, 0);

            for (int x = 0; x < chunk.SizeX; x++)
            {
                for (int z = 0; z < chunk.SizeZ; z++)
                {
                    int h;
                    for (int y = 0; y <= waterHeight; y++)
                    {
                        h = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z);
                        int index = chunk.Data.IndexAt(x, y, z);
                        voxel.GridPosition = new Vector3(x, y, z);
                        if (voxel.IsEmpty && y >= h)
                        {
                            chunk.Data.Water[index].WaterLevel = 8;
                            chunk.Data.Water[index].Type       = LiquidType.Water;
                        }
                    }


                    Vector2 vec = new Vector2(x + chunk.Origin.X, z + chunk.Origin.Z) / PlayState.WorldScale;
                    if (Overworld.GetWater(Overworld.Map, vec) != Overworld.WaterType.Volcano)
                    {
                        continue;
                    }

                    h = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z);


                    if (h <= 0)
                    {
                        continue;
                    }

                    chunk.Data.Water[chunk.Data.IndexAt(x, h, z)].WaterLevel = 8;
                    chunk.Data.Water[chunk.Data.IndexAt(x, h, z)].Type       = LiquidType.Lava;
                }
            }
        }
コード例 #6
0
ファイル: ChunkData.cs プロジェクト: chrisapril/dwarfcorp
        public float GetFilledVoxelGridHeightAt(float x, float y, float z)
        {
            VoxelChunk chunk = GetVoxelChunkAtWorldLocation(new Vector3(x, y, z));

            if (chunk != null)
            {
                return(chunk.GetFilledVoxelGridHeightAt((int)(x - chunk.Origin.X), (int)(y - chunk.Origin.Y), (int)(z - chunk.Origin.Z)));
            }
            else
            {
                return(-1);
            }
        }
コード例 #7
0
        public void GenerateOres(VoxelChunk chunk, ComponentManager components, ContentManager content,
                                 GraphicsDevice graphics)
        {
            Vector3 origin     = chunk.Origin;
            int     chunkSizeX = chunk.SizeX;
            int     chunkSizeY = chunk.SizeY;
            int     chunkSizeZ = chunk.SizeZ;
            Voxel   v          = chunk.MakeVoxel(0, 0, 0);

            for (int x = 0; x < chunkSizeX; x++)
            {
                for (int z = 0; z < chunkSizeZ; z++)
                {
                    int h = chunk.GetFilledVoxelGridHeightAt(x, chunkSizeY - 1, z);
                    for (int y = 1; y < chunkSizeY; y++)
                    {
                        foreach (
                            KeyValuePair <string, VoxelLibrary.ResourceSpawnRate> spawns in VoxelLibrary.ResourceSpawns)
                        {
                            float s = spawns.Value.VeinSize;
                            float p = spawns.Value.VeinSpawnThreshold;
                            v.GridPosition = new Vector3(x, y, z);
                            if (v.IsEmpty || y >= h - 1 || !(y - h / 2 < spawns.Value.MaximumHeight) ||
                                !(y - h / 2 > spawns.Value.MinimumHeight) ||
                                !(PlayState.Random.NextDouble() <= spawns.Value.Probability) || v.Type.Name != "Stone")
                            {
                                continue;
                            }

                            float caviness = (float)NoiseGenerator.Noise((float)(x + origin.X) * s,
                                                                         (float)(z + origin.Z) * s,
                                                                         (float)(y + origin.Y + h) * s);

                            if (caviness > p)
                            {
                                v.Type = VoxelLibrary.GetVoxelType(spawns.Key);
                            }
                            continue;
                        }
                    }
                }
            }
        }
コード例 #8
0
ファイル: ChunkGenerator.cs プロジェクト: svifylabs/dwarfcorp
        /*
         * public void GenerateOres(VoxelChunk chunk, ComponentManager components, ContentManager content,
         *  GraphicsDevice graphics)
         * {
         *  Vector3 origin = chunk.Origin;
         *  int chunkSizeX = chunk.SizeX;
         *  int chunkSizeY = chunk.SizeY;
         *  int chunkSizeZ = chunk.SizeZ;
         *  Voxel v = chunk.MakeVoxel(0, 0, 0);
         *  for (int x = 0; x < chunkSizeX; x++)
         *  {
         *      for (int z = 0; z < chunkSizeZ; z++)
         *      {
         *          int h = chunk.GetFilledVoxelGridHeightAt(x, chunkSizeY - 1, z);
         *          for (int y = 1; y < chunkSizeY; y++)
         *          {
         *              foreach (
         *                  KeyValuePair<string, VoxelLibrary.ResourceSpawnRate> spawns in VoxelLibrary.ResourceSpawns)
         *              {
         *                  float s = spawns.Value.VeinSize;
         *                  float p = spawns.Value.VeinSpawnThreshold;
         *                  v.GridPosition = new Vector3(x, y, z);
         *                  if (v.IsEmpty || y >= h - 1 || !(y - h/2 < spawns.Value.MaximumHeight) ||
         *                      !(y - h/2 > spawns.Value.MinimumHeight) ||
         *                      !(PlayState.Random.NextDouble() <= spawns.Value.Probability) || v.Type.Name != "Stone")
         *                  {
         *                      continue;
         *                  }
         *
         *                  float caviness = (float) NoiseGenerator.Noise((float) (x + origin.X)*s,
         *                      (float) (z + origin.Z)*s,
         *                      (float) (y + origin.Y + h)*s);
         *
         *                  if (caviness > p)
         *                  {
         *                      v.Type = VoxelLibrary.GetVoxelType(spawns.Key);
         *                  }
         *                  continue;
         *              }
         *          }
         *      }
         *  }
         * }
         */

        public void GenerateFauna(VoxelChunk chunk, ComponentManager components, ContentManager content, GraphicsDevice graphics, FactionLibrary factions)
        {
            int   waterHeight = (int)(SeaLevel * chunk.SizeY);
            Voxel v           = chunk.MakeVoxel(0, 0, 0);

            for (int x = 0; x < chunk.SizeX; x++)
            {
                for (int z = 0; z < chunk.SizeZ; z++)
                {
                    Vector2         vec       = new Vector2(x + chunk.Origin.X, z + chunk.Origin.Z) / PlayState.WorldScale;
                    Overworld.Biome biome     = Overworld.Map[(int)MathFunctions.Clamp(vec.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(vec.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome;
                    BiomeData       biomeData = BiomeLibrary.Biomes[biome];

                    int y = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z);

                    if (!chunk.IsCellValid(x, (int)(y - chunk.Origin.Y), z))
                    {
                        continue;
                    }

                    v.GridPosition = new Vector3(x, y, z);

                    if (chunk.Data.Water[v.Index].WaterLevel != 0 || y <= waterHeight)
                    {
                        continue;
                    }

                    foreach (FaunaData animal in biomeData.Fauna)
                    {
                        if (y <= 0 || !(PlayState.Random.NextDouble() < animal.SpawnProbability))
                        {
                            continue;
                        }


                        EntityFactory.CreateEntity <Body>(animal.Name, chunk.Origin + new Vector3(x, y, z) + Vector3.Up * 1.0f);

                        break;
                    }
                }
            }
        }
コード例 #9
0
ファイル: ChunkGenerator.cs プロジェクト: svifylabs/dwarfcorp
        public void GenerateVegetation(VoxelChunk chunk, ComponentManager components, ContentManager content, GraphicsDevice graphics)
        {
            int   waterHeight = (int)(SeaLevel * chunk.SizeY);
            bool  updated     = false;
            Voxel v           = chunk.MakeVoxel(0, 0, 0);
            Voxel vUnder      = chunk.MakeVoxel(0, 0, 0);

            for (int x = 0; x < chunk.SizeX; x++)
            {
                for (int z = 0; z < chunk.SizeZ; z++)
                {
                    Vector2         vec       = new Vector2(x + chunk.Origin.X, z + chunk.Origin.Z) / PlayState.WorldScale;
                    Overworld.Biome biome     = Overworld.Map[(int)MathFunctions.Clamp(vec.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(vec.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome;
                    BiomeData       biomeData = BiomeLibrary.Biomes[biome];

                    int y = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z);

                    if (!chunk.IsCellValid(x, (int)(y - chunk.Origin.Y), z))
                    {
                        continue;
                    }

                    v.GridPosition = new Vector3(x, y, z);

                    if (!v.IsEmpty || chunk.Data.Water[v.Index].WaterLevel != 0 || y <= waterHeight)
                    {
                        continue;
                    }

                    foreach (VegetationData veg in biomeData.Vegetation)
                    {
                        if (y <= 0)
                        {
                            continue;
                        }

                        if (!MathFunctions.RandEvent(veg.SpawnProbability))
                        {
                            continue;
                        }

                        if (NoiseGenerator.Noise(vec.X / veg.ClumpSize, veg.NoiseOffset, vec.Y / veg.ClumpSize) < veg.ClumpThreshold)
                        {
                            continue;
                        }

                        int yh = chunk.GetFilledVoxelGridHeightAt(x, y, z);

                        if (yh > 0)
                        {
                            vUnder.GridPosition = new Vector3(x, yh - 1, z);
                            if (!vUnder.IsEmpty && vUnder.TypeName == biomeData.GrassVoxel)
                            {
                                vUnder.Type = VoxelLibrary.GetVoxelType(biomeData.SoilVoxel);
                                updated     = true;
                                float offset = veg.VerticalOffset;
                                if (vUnder.RampType != RampType.None)
                                {
                                    offset -= 0.25f;
                                }
                                float treeSize = MathFunctions.Rand() * veg.SizeVariance + veg.MeanSize;
                                EntityFactory.CreateEntity <Body>(veg.Name, chunk.Origin + new Vector3(x, y, z) + new Vector3(0, treeSize * offset, 0), Blackboard.Create("Scale", treeSize));
                            }
                        }

                        break;
                    }
                }
            }

            if (updated)
            {
                chunk.ShouldRebuild = true;
            }
        }
コード例 #10
0
        public void GenerateCaves(VoxelChunk chunk)
        {
            Vector3      origin     = chunk.Origin;
            int          chunkSizeX = chunk.SizeX;
            int          chunkSizeY = chunk.SizeY;
            int          chunkSizeZ = chunk.SizeZ;
            BiomeData    biome      = BiomeLibrary.Biomes[Overworld.Biome.Cave];
            List <Voxel> neighbors  = new List <Voxel>();
            Voxel        vUnder     = chunk.MakeVoxel(0, 0, 0);

            for (int x = 0; x < chunkSizeX; x++)
            {
                for (int z = 0; z < chunkSizeZ; z++)
                {
                    int h = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z);
                    for (int i = 0; i < CaveLevels.Count; i++)
                    {
                        int y = CaveLevels[i];
                        if (y <= 0 || y >= h - 1)
                        {
                            continue;
                        }
                        Vector3 vec       = new Vector3(x, y, z) + chunk.Origin;
                        double  caveNoise = CaveNoise.GetValue((x + origin.X) * CaveNoiseScale * CaveFrequencies[i],
                                                               (y + origin.Y) * CaveNoiseScale * 3.0f, (z + origin.Z) * CaveNoiseScale * CaveFrequencies[i]);

                        double heightnoise = NoiseGenerator.Noise((x + origin.X) * NoiseScale * CaveFrequencies[i],
                                                                  (y + origin.Y) * NoiseScale * 3.0f, (z + origin.Z) * NoiseScale * CaveFrequencies[i]);

                        int caveHeight = Math.Min(Math.Max((int)(heightnoise * 5), 1), 3);

                        if (caveNoise > CaveSize)
                        {
                            bool waterFound = false;
                            for (int dy = 0; dy < caveHeight; dy++)
                            {
                                int index = chunk.Data.IndexAt(x, y - dy, z);
                                chunk.GetNeighborsManhattan(x, y - dy, z, neighbors);

                                if (neighbors.Any(v => v.WaterLevel > 0))
                                {
                                    waterFound = true;
                                }

                                if (waterFound)
                                {
                                    break;
                                }

                                chunk.Data.Types[index] = 0;
                            }

                            if (!waterFound && caveNoise > CaveSize * 1.8f && y - caveHeight > 0)
                            {
                                int indexunder = chunk.Data.IndexAt(x, y - caveHeight, z);
                                chunk.Data.Types[indexunder]      = (byte)VoxelLibrary.GetVoxelType(biome.GrassVoxel).ID;
                                chunk.Data.Health[indexunder]     = (byte)VoxelLibrary.GetVoxelType(biome.GrassVoxel).StartingHealth;
                                chunk.Data.IsExplored[indexunder] = false;
                                foreach (VegetationData veg in biome.Vegetation)
                                {
                                    if (!MathFunctions.RandEvent(veg.SpawnProbability))
                                    {
                                        continue;
                                    }

                                    if (NoiseGenerator.Noise(vec.X / veg.ClumpSize, veg.NoiseOffset, vec.Y / veg.ClumpSize) < veg.ClumpThreshold)
                                    {
                                        continue;
                                    }


                                    vUnder.GridPosition = new Vector3(x, y - 1, z);
                                    if (!vUnder.IsEmpty && vUnder.TypeName == biome.GrassVoxel)
                                    {
                                        vUnder.Type = VoxelLibrary.GetVoxelType(biome.SoilVoxel);
                                        float offset = veg.VerticalOffset;
                                        if (vUnder.RampType != RampType.None)
                                        {
                                            offset -= 0.25f;
                                        }
                                        float         treeSize = MathFunctions.Rand() * veg.SizeVariance + veg.MeanSize;
                                        GameComponent entity   = EntityFactory.CreateEntity <GameComponent>(veg.Name, chunk.Origin + new Vector3(x, y, z) + new Vector3(0, treeSize * offset, 0), Blackboard.Create("Scale", treeSize));
                                        entity.GetRootComponent().SetActiveRecursive(false);
                                        entity.GetRootComponent().SetVisibleRecursive(false);
                                        if (GameSettings.Default.FogofWar)
                                        {
                                            ExploredListener listener = new ExploredListener(
                                                PlayState.ComponentManager, entity, PlayState.ChunkManager, vUnder);
                                        }
                                    }
                                }
                            }

                            foreach (FaunaData animal in biome.Fauna)
                            {
                                if (y <= 0 || !(PlayState.Random.NextDouble() < animal.SpawnProbability))
                                {
                                    continue;
                                }


                                var entity = EntityFactory.CreateEntity <GameComponent>(animal.Name, chunk.Origin + new Vector3(x, y, z) + Vector3.Up * 1.0f);
                                entity.GetRootComponent().SetActiveRecursive(false);
                                entity.GetRootComponent().SetVisibleRecursive(false);

                                if (GameSettings.Default.FogofWar)
                                {
                                    ExploredListener listener = new ExploredListener(PlayState.ComponentManager, entity,
                                                                                     PlayState.ChunkManager, chunk.MakeVoxel(x, y, z));
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
コード例 #11
0
ファイル: BalloonAI.cs プロジェクト: NakedFury/dwarfcorp
        public void Update(DwarfTime gameTime, ChunkManager chunks, Camera camera)
        {
            Vector3 targetVelocity = TargetPosition - Body.GlobalTransform.Translation;

            if (targetVelocity.LengthSquared() > 0.0001f)
            {
                targetVelocity.Normalize();
                targetVelocity *= MaxVelocity;
            }

            Matrix m = Body.LocalTransform;

            m.Translation      += targetVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            Body.LocalTransform = m;

            Body.HasMoved = true;

            switch (State)
            {
            case BalloonState.DeliveringGoods:
                VoxelChunk chunk = chunks.ChunkData.GetVoxelChunkAtWorldLocation(Body.GlobalTransform.Translation);

                if (chunk != null)
                {
                    Vector3 gridPos = chunk.WorldToGrid(Body.GlobalTransform.Translation);
                    float   height  = chunk.GetFilledVoxelGridHeightAt((int)gridPos.X, (int)gridPos.Y, (int)gridPos.Z) + chunk.Origin.Y;
                    TargetPosition = new Vector3(Body.GlobalTransform.Translation.X, height + 5, Body.GlobalTransform.Translation.Z);

                    Vector3 diff = Body.GlobalTransform.Translation - TargetPosition;

                    if (diff.LengthSquared() < 2)
                    {
                        State = BalloonState.Waiting;
                    }
                }
                else
                {
                    State = BalloonState.Leaving;
                }

                break;

            case BalloonState.Leaving:
                TargetPosition = Vector3.UnitY * 100 + Body.GlobalTransform.Translation;

                if (Body.GlobalTransform.Translation.Y > 300)
                {
                    Die();
                }

                break;

            case BalloonState.Waiting:
                TargetPosition = Body.GlobalTransform.Translation;

                if (!shipmentGiven)
                {
                    shipmentGiven = true;
                }
                else
                {
                    State = BalloonState.Leaving;
                }


                break;
            }
        }