Beispiel #1
0
        public static Vector3 GetRandomNoiseVector(Vector3 position)
        {
            float x = VertexNoiseX.Noise((float)position.X * NoiseScale, (float)position.Y * NoiseScale, (float)position.Z * NoiseScale) * NoiseMagnitude - NoiseMagnitude / 2.0f;
            float y = VertexNoiseY.Noise((float)position.X * NoiseScale, (float)position.Y * NoiseScale, (float)position.Z * NoiseScale) * NoiseMagnitude - NoiseMagnitude / 2.0f;
            float z = VertexNoiseZ.Noise((float)position.X * NoiseScale, (float)position.Y * NoiseScale, (float)position.Z * NoiseScale) * NoiseMagnitude - NoiseMagnitude / 2.0f;

            float gx = GlobalVertexNoiseX.Noise((float)position.X * GlobalNoiseScale, (float)position.Y * GlobalNoiseScale, (float)position.Z * GlobalNoiseScale) * GlobalNoiseMagnitude - GlobalNoiseMagnitude / 2.0f;
            float gy = GlobalVertexNoiseY.Noise((float)position.X * GlobalNoiseScale, (float)position.Y * GlobalNoiseScale, (float)position.Z * GlobalNoiseScale) * GlobalNoiseMagnitude - GlobalNoiseMagnitude / 2.0f;
            float gz = GlobalVertexNoiseZ.Noise((float)position.X * GlobalNoiseScale, (float)position.Y * GlobalNoiseScale, (float)position.Z * GlobalNoiseScale) * GlobalNoiseMagnitude - GlobalNoiseMagnitude / 2.0f;

            return(new Vector3((float)x + (float)gx, (float)y + (float)gy, (float)z + (float)gz));
        }
Beispiel #2
0
        public static void Distort(int width, int height, float distortAmount, float distortScale, ScalarFieldType fieldType)
        {
            float[,] buffer = new float[width, height];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    buffer[x, y] = GetValue(Map, new Vector2(x, y) + new Vector2((XDistort.Noise(x * distortScale, y * distortScale, 0) * 2.0f - 1.0f) * distortAmount,
                                                                                 (YDistort.Noise(x * distortScale, y * distortScale, 0) * 2.0f - 1.0f) * distortAmount), fieldType);
                }
            }

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Map[x, y].SetValue(fieldType, buffer[x, y]);
                }
            }
        }
Beispiel #3
0
        public void RebuildMoteLayer(int LocalY)
        {
#if DEBUG
            if (LocalY < 0 || LocalY >= VoxelConstants.ChunkSizeY)
            {
                throw new InvalidOperationException();
            }
#endif

            var moteList = new List <NewInstanceData>();

            // Enumerate voxels.
            for (var x = 0; x < VoxelConstants.ChunkSizeX; ++x)
            {
                for (var z = 0; z < VoxelConstants.ChunkSizeZ; ++z)
                {
                    var v = VoxelHandle.UnsafeCreateLocalHandle(this, new LocalVoxelCoordinate(x, LocalY, z));
                    if (!v.IsValid)
                    {
                        continue;
                    }

                    // Don't generate in empty voxels.
                    if (v.IsEmpty)
                    {
                        continue;
                    }

                    if (!v.IsExplored)
                    {
                        continue;
                    }

                    // Don't generate motes if above is not empty
                    var voxelAbove = VoxelHelpers.GetVoxelAbove(v);
                    if (voxelAbove.IsValid && (!voxelAbove.IsEmpty || voxelAbove.LiquidLevel != 0))
                    {
                        continue;
                    }

                    // Find biome type.
                    var biomeData = Manager.World.Overworld.Map.GetBiomeAt(v.WorldPosition, Manager.World.Overworld.InstanceSettings.Origin);

                    // Don't generate if not on grass type.
                    if (v.GrassType == 0 || Library.GetGrassType(v.GrassType).Name != biomeData.GrassDecal)
                    {
                        continue;
                    }

                    // Biomes can contain multiple types of mote.
                    foreach (var moteDetail in biomeData.Motes)
                    {
                        // Lower mote if voxel is ramped.
                        float vOffset = 0.0f;
                        if (v.RampType != RampType.None)
                        {
                            vOffset = -0.5f;
                        }

                        var   vPos  = v.WorldPosition * moteDetail.RegionScale;
                        float value = MoteNoise.Noise(vPos.X, vPos.Y, vPos.Z);

                        if (!(Math.Abs(value) > moteDetail.SpawnThreshold))
                        {
                            continue;
                        }

                        float s = MoteScaleNoise.Noise(vPos.X, vPos.Y, vPos.Z) * moteDetail.MoteScale;

                        var smallNoise = ClampVector(VertexNoise.GetRandomNoiseVector(vPos * 20.0f) * 20.0f, 0.4f);
                        smallNoise.Y = 0.0f;

                        var mote = GenerateGrassMote(
                            Manager.World,
                            v.WorldPosition + new Vector3(0.5f, 1.0f + s * 0.5f + vOffset, 0.5f) + smallNoise,
                            new Color(v.Sunlight ? 255 : 0, 128, 0),
                            s,
                            moteDetail);

                        moteList.Add(mote);
                    }
                }
            }

            MoteRecords[LocalY] = moteList;
        }
        public static void GenerateCaveVegetation(VoxelChunk chunk, int x, int y, int z, int caveHeight, BiomeData biome, Vector3 vec, WorldManager world, Perlin NoiseGenerator)
        {
            var vUnder   = new VoxelHandle(chunk, new LocalVoxelCoordinate(x, y - 1, z));
            var wayUnder = new VoxelHandle(chunk, new LocalVoxelCoordinate(x, y - caveHeight, z));

            wayUnder.RawSetType(VoxelLibrary.GetVoxelType(biome.SoilLayer.VoxelType));

            var grassType = GrassLibrary.GetGrassType(biome.GrassDecal);

            if (grassType != null)
            {
                wayUnder.RawSetGrass(grassType.ID);
            }

            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;
                }

                if (!vUnder.IsEmpty && vUnder.Type.Name == biome.SoilLayer.VoxelType)
                {
                    vUnder.RawSetType(VoxelLibrary.GetVoxelType(biome.SoilLayer.VoxelType));
                    vUnder.RawSetGrass(0);
                    float treeSize = MathFunctions.Rand() * veg.SizeVariance + veg.MeanSize;

                    WorldManager.DoLazy(() =>
                    {
                        if (!GameSettings.Default.FogofWar)
                        {
                            GameComponent entity = EntityFactory.CreateEntity <GameComponent>(veg.Name,
                                                                                              vUnder.WorldPosition + new Vector3(0.5f, 1.0f, 0.5f),
                                                                                              Blackboard.Create("Scale", treeSize));
                        }
                        else
                        {
                            world.ComponentManager.RootComponent.AddChild(new ExploredListener(
                                                                              world.ComponentManager, vUnder)
                            {
                                EntityToSpawn  = veg.Name,
                                SpawnLocation  = vUnder.WorldPosition + new Vector3(0.5f, 1.0f, 0.5f),
                                BlackboardData = Blackboard.Create("Scale", treeSize)
                            });
                        }
                    });
                }
            }

            float spawnLikelihood = (world.InitialEmbark.Difficulty + 0.1f);

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

                FaunaData animal1 = animal;
                WorldManager.DoLazy(() =>
                {
                    if (!GameSettings.Default.FogofWar)
                    {
                        var entity = EntityFactory.CreateEntity <GameComponent>(animal1.Name,
                                                                                wayUnder.WorldPosition + Vector3.Up * 1.5f);
                    }
                    else
                    {
                        world.ComponentManager.RootComponent.AddChild(new ExploredListener
                                                                          (world.ComponentManager, new VoxelHandle(chunk, wayUnder.Coordinate.GetLocalVoxelCoordinate()))
                        {
                            EntityToSpawn = animal1.Name,
                            SpawnLocation = wayUnder.WorldPosition + Vector3.Up * 1.5f
                        });
                    }
                });
                break;
            }
        }
Beispiel #5
0
        public void RebuildMoteLayer(int Y)
        {
            BoundingBox box = GetBoundingBox();

            box.Min.Y = Y;
            box.Max.Y = Y + 1;
            //Drawer3D.DrawBox(box, Color.Red, 0.1f, true);
            // Destroy old motes.
            if (MoteRecords[Y] != null)
            {
                foreach (var record in MoteRecords[Y])
                {
                    DestroyGrassMote(this.Manager.World, record.Type, record);
                }
                MoteRecords[Y].Clear();
            }
            else
            {
                MoteRecords[Y] = new List <NewInstanceData>();
            }

            // Enumerate voxels.
            for (var x = 0; x < VoxelConstants.ChunkSizeX; ++x)
            {
                for (var z = 0; z < VoxelConstants.ChunkSizeZ; ++z)
                {
                    // Don't generate motes if above is not empty
                    if (Y < VoxelConstants.ChunkSizeY - 1)
                    {
                        var voxelAbove = new VoxelHandle(this, new LocalVoxelCoordinate(x, Y + 1, z));
                        if (!voxelAbove.IsEmpty || voxelAbove.WaterCell.WaterLevel != 0)
                        {
                            continue;
                        }
                    }

                    var v = new VoxelHandle(this, new LocalVoxelCoordinate(x, Y, z));

                    // Don't generate in empty voxels.
                    if (v.IsEmpty)
                    {
                        continue;
                    }

                    if (!v.IsExplored)
                    {
                        continue;
                    }

                    // Find biome type.
                    var biome = Overworld.Map[
                        (int)MathFunctions.Clamp(v.WorldPosition.X / Manager.World.WorldScale, 0, Overworld.Map.GetLength(0) - 1),
                        (int)MathFunctions.Clamp(v.WorldPosition.Z / Manager.World.WorldScale, 0, Overworld.Map.GetLength(1) - 1)]
                                .Biome;

                    var biomeData = BiomeLibrary.Biomes[biome];

                    // Don't generate if not on grass type.
                    if (v.GrassType == 0 || GrassLibrary.GetGrassType(v.GrassType).Name != biomeData.GrassDecal)
                    {
                        continue;
                    }

                    // Biomes can contain multiple types of mote.
                    foreach (var moteDetail in biomeData.Motes)
                    {
                        // Lower mote if voxel is ramped.
                        float vOffset = 0.0f;
                        if (v.RampType != RampType.None)
                        {
                            vOffset = -0.5f;
                        }

                        var   vPos  = v.WorldPosition * moteDetail.RegionScale;
                        float value = MoteNoise.Noise(vPos.X, vPos.Y, vPos.Z);

                        if (!(Math.Abs(value) > moteDetail.SpawnThreshold))
                        {
                            continue;
                        }

                        float s = MoteScaleNoise.Noise(vPos.X, vPos.Y, vPos.Z) * moteDetail.MoteScale;

                        var smallNoise = ClampVector(VertexNoise.GetRandomNoiseVector(vPos * 20.0f) * 20.0f, 0.4f);
                        smallNoise.Y = 0.0f;

                        var mote = GenerateGrassMote(
                            Manager.World,
                            v.WorldPosition + new Vector3(0.5f, 1.0f + s * 0.5f + vOffset, 0.5f) + smallNoise,
                            new Color(v.SunColor, 128, 0),
                            s,
                            moteDetail.Name);

                        MoteRecords[Y].Add(mote);
                    }
                }
            }
        }