public MyCsgShapePlanet(MyRandom random, Vector3 translation, ref MyCsgShapePlanetShapeAttributes shapeAttributes, ref MyCsgShapePlanetHillAttributes hillAttributes, ref MyCsgShapePlanetHillAttributes canyonAttributes, float deviationFrequency = 0, float detailFrequency = 0)
        {
            m_translation = translation;
            m_random = random;
            m_shapeAttributes = shapeAttributes;
            m_hillAttributes = hillAttributes;
            m_canyonAttributes = canyonAttributes;

            m_canyonBlendTreshold = m_canyonAttributes.Treshold + m_canyonAttributes.BlendTreshold;
            m_hillBlendTreshold = m_hillAttributes.Treshold - m_hillAttributes.BlendTreshold;

            m_shapeAttributes.Radius = (shapeAttributes.Radius/2.0f) * (1 - shapeAttributes.DeviationScale * m_hillAttributes.SizeRatio);
            m_halfDeviation = (shapeAttributes.Radius / 2.0f) * shapeAttributes.DeviationScale;

            m_deviationFrequency = deviationFrequency;
            m_detailFrequency    = detailFrequency;

            m_hillHalfDeviation = m_halfDeviation * m_hillAttributes.SizeRatio;
            m_canyonHalfDeviation = m_halfDeviation * m_canyonAttributes.SizeRatio;
          
            m_enableModulation = true;

            m_hillModule = new MyCompositeNoise(hillAttributes.NumNoises, hillAttributes.Frequency / m_shapeAttributes.Radius, random);

            m_canyonModule = new MyCompositeNoise(canyonAttributes.NumNoises, canyonAttributes.Frequency / m_shapeAttributes.Radius, random);

            m_normalModule = new MySimplexFast(seed: random.Next(), frequency: shapeAttributes.NormalNoiseFrequency / m_shapeAttributes.Radius);

            ComputeDerivedProperties();
        }
        public MyCompositeNoise(int numNoises,float startFrequency,MyRandom random)
        {
            m_numNoises = numNoises;
            m_noises = new IMyModule[m_numNoises];
            m_amplitudeScales = new float[m_numNoises];
            m_normalizationFactor = 2.0f - 1.0f / (float)Math.Pow(2, numNoises - 1);

            float frequency = startFrequency;
            for (int i = 0; i < m_numNoises; ++i)
            {
                m_amplitudeScales[i] = 1.0f / (float)Math.Pow(2.0f, i);
                m_noises[i] = new MySimplexFast(seed: random.Next(), frequency: frequency);
                frequency *= 2.01f;
            }

        }
Esempio n. 3
0
        public void InitializeRandomTexture(int? seed = null)
        {
            MyRandom random;
            if (seed.HasValue)
                random = new MyRandom(seed.Value);
            else
                random = new MyRandom();

            int randowTexRes = 1024;
            Vector4[] randomValues = new Vector4[randowTexRes * randowTexRes * 4];
            for (uint i = 0, ctr = 0; i < randowTexRes * randowTexRes; i++)
            {
                randomValues[ctr++] = new Vector4(random.NextFloat() * 2.0f - 1.0f,
                    random.NextFloat() * 2.0f - 1.0f,
                    random.NextFloat() * 2.0f - 1.0f,
                    random.NextFloat() * 2.0f - 1.0f);
            }
            CreateR32G32B32A32_Float((MyGeneratedTexture)RandomTex, "RandomTex", new Vector2I(randowTexRes, randowTexRes), randomValues);
        }
Esempio n. 4
0
 public StateToken(MyRandom random, int newSeed)
 {
     m_random = random;
     random.GetState(out m_state);
     random.SetSeed(newSeed);
 }
Esempio n. 5
0
 public StateToken(MyRandom random)
 {
     m_random = random;
     random.GetState(out m_state);
 }
Esempio n. 6
0
        public static void InitializeConstantBuffer(int? randomSeed = null)
        {
            MyRandom random;
            if (randomSeed.HasValue)
                random = new MyRandom(randomSeed.Value);
            else
                random = new MyRandom();

            const int JITTERSIZE = 4 * 4;
            var jitters = new Vector4[JITTERSIZE];
            for (int i = 0; i < JITTERSIZE; i++)
            {
                float angle = 2.0f * (float)Math.PI * random.NextFloat() / NUM_DIRECTIONS;
                jitters[i].X = (float)Math.Cos(angle);
                jitters[i].Y = (float)Math.Sin(angle);
                jitters[i].Z = random.NextFloat();
                jitters[i].W = random.NextFloat();
            }
            PerPassConstantBuffer data;
            for (uint sliceIndex = 0; sliceIndex < NUM_SLICES; ++sliceIndex)
            {
                data.Offset.X = (float)(sliceIndex % 4) + 0.5f;
                data.Offset.Y = (float)(sliceIndex / 4) + 0.5f;
                data.Jitter = jitters[sliceIndex];
                data.SliceIndexFloat = (float)sliceIndex;
                data.SliceIndexInt = sliceIndex;

                var buffer = m_perPassCBs[sliceIndex];
                if (buffer == null)
                {
                    buffer = MyManagers.Buffers.CreateConstantBuffer("MyHBAO::passCB " + sliceIndex, PERPASSCONSTANTBUFFERSIZE, usage: ResourceUsage.Dynamic);
                    m_perPassCBs[sliceIndex] = buffer;
                }

                var mapping = MyMapping.MapDiscard(buffer);
                mapping.WriteAndPosition(ref data);
                mapping.Unmap();
            }
        }
Esempio n. 7
0
 public MyAIComponent()
 {
     Static = this;
     BotFactory = Activator.CreateInstance(MyPerGameSettings.BotFactoryType) as MyBotFactoryBase;
     Random = MyFakes.DEBUG_AVOID_RANDOM_AI ? new MyRandom(0): new MyRandom();
 }
 private static Vector3 CreateRandomPointOnBox(MyRandom self, float boxSize)
 {
     Vector3 result = Vector3.Zero;
     switch (self.Next() & 6)
     {// each side of a box
         case 0: return new Vector3(0f, self.NextFloat(), self.NextFloat());
         case 1: return new Vector3(1f, self.NextFloat(), self.NextFloat());
         case 2: return new Vector3(self.NextFloat(), 0f, self.NextFloat());
         case 3: return new Vector3(self.NextFloat(), 1f, self.NextFloat());
         case 4: return new Vector3(self.NextFloat(), self.NextFloat(), 0f);
         case 5: return new Vector3(self.NextFloat(), self.NextFloat(), 1f);
     }
     result *= boxSize;
     return result;
 }
        private Vector3D GetRandomDirection(MyRandom random)
        {
            double phi = random.NextDouble() * 2.0 * Math.PI;
            double z = random.NextDouble() * 2.0 - 1.0;
            double root = Math.Sqrt(1.0 - z * z);

            return new Vector3D(root * Math.Cos(phi), root * Math.Sin(phi), z);
        }
Esempio n. 10
0
 public MyAIComponent()
 {
     Static = this;
     BotFactory = Activator.CreateInstance(MyPerGameSettings.BotFactoryType) as IMyBotFactory;
     Random = new MyRandom();
 }
Esempio n. 11
0
 public StateToken(MyRandom random, int newSeed)
 {
     m_random = random;
     random.GetState(out m_state);
     random.SetSeed(newSeed);
 }
Esempio n. 12
0
 public StateToken(MyRandom random)
 {
     m_random = random;
     random.GetState(out m_state);
 }
Esempio n. 13
0
        public override void LoadData()
        {
            base.LoadData();

            m_updateCounter = 0;

            m_envItems = new Dictionary<long, MyEnvironmentItems>(10);
            m_forestAreas = new List<Area>(100);
            m_highLevelBoxes = new List<BoundingBoxD>();
            m_tmpItemInfos = new List<ItemInfo>(500);
            m_tmpAreas = new List<Area>();
            m_tmpAreas2 = new List<Area>();
            m_checkedSectors = new HashSet<Vector3I>();
            m_checkQueue = new Queue<long>();
            m_initialForestLocations = new Queue<Vector3D>();
            m_tmpSectors = new List<Vector3I>();

            m_aabbTree = new MyDynamicAABBTreeD(Vector3D.Zero);

            m_random = new MyRandom();

            // MW:TODO growing items on allowed materials
            m_allowedMaterials = new HashSet<MyStringHash>();

            m_loadPhase = true;
            m_findValidForestPhase = false;

            MyEntities.OnEntityAdd += MyEntities_OnEntityAdd;
            MyEntities.OnEntityRemove += MyEntities_OnEntityRemove;
            Static = this;
        }
 public MyPanCakeFieldDesityFunction(MyRandom random, double frequency)
 {
     noise = new MySimplexFast(random.Next(), frequency);
 }
        private void GenerateObject(MyProceduralCell cell, MyObjectSeed objectSeed, ref int index, MyRandom random, IMyModule densityFunctionFilled, IMyModule densityFunctionRemoved)
        {
            cell.AddObject(objectSeed);

            IMyAsteroidFieldDensityFunction func = objectSeed.UserData as IMyAsteroidFieldDensityFunction;
            if (func != null)
            {
                ChildrenAddDensityFunctionRemoved(func);
            }

            switch (objectSeed.Type)
            {
                case MyObjectSeedType.Moon:
                    break;
                case MyObjectSeedType.Planet:
                    m_tmpClusterBoxes.Add(objectSeed.BoundingVolume);

                    for (int i = 0; i < MOONS_MAX; ++i)
                    {
                        var direction = GetRandomDirection(random);
                        var size = MathHelper.Lerp(MOON_SIZE_MIN, MOON_SIZE_MAX, random.NextDouble());
                        var distance = MathHelper.Lerp(MOON_DISTANCE_MIN, MOON_DISTANCE_MAX, random.NextDouble());
                        var position = objectSeed.BoundingVolume.Center + direction * (size + objectSeed.BoundingVolume.HalfExtents.Length() * 2 + distance);

                        ProfilerShort.Begin("GetValue");
                        var value = densityFunctionFilled.GetValue(position.X, position.Y, position.Z);
                        ProfilerShort.End();

                        if (value < MOON_DENSITY) // -1..+1
                        {
                            var clusterObjectSeed = new MyObjectSeed(cell, position, size);
                            clusterObjectSeed.Seed = random.Next();
                            clusterObjectSeed.Type = MyObjectSeedType.Moon;
                            clusterObjectSeed.Index = index++;
                            clusterObjectSeed.UserData = new MySphereDensityFunction(position, OBJECT_SEED_RADIUS, OBJECT_SEED_RADIUS);

                            bool overlaps = false;
                            foreach (var box in m_tmpClusterBoxes)
                            {
                                if (overlaps |= clusterObjectSeed.BoundingVolume.Intersects(box))
                                {
                                    break;
                                }
                            }

                            if (!overlaps)
                            {
                                m_tmpClusterBoxes.Add(clusterObjectSeed.BoundingVolume);
                                GenerateObject(cell, clusterObjectSeed, ref index, random, densityFunctionFilled, densityFunctionRemoved);
                            }
                        }
                    }
                    m_tmpClusterBoxes.Clear();
                    break;
                case MyObjectSeedType.Empty:
                    break;
                default:
                    throw new InvalidBranchException();
                    break;
            }
        }
        private static MyCsgShapePlanetHillAttributes FillValues(MyStructureParams input, MyRandom random)
        {
            MyCsgShapePlanetHillAttributes outputValues = new MyCsgShapePlanetHillAttributes();

            outputValues.BlendTreshold = random.NextFloat(input.BlendSize.Min, input.BlendSize.Max);
            outputValues.Treshold = random.NextFloat(input.Treshold.Min, input.Treshold.Max);
            outputValues.Frequency = random.NextFloat(input.Frequency.Min, input.Frequency.Max);
            outputValues.SizeRatio = random.NextFloat(input.SizeRatio.Min, input.SizeRatio.Max);
            outputValues.NumNoises = random.Next((int)input.NumNoises.Min, (int)input.NumNoises.Max);

            return outputValues;
        }
        private void GenerateObject(MyProceduralCell cell, MyObjectSeed objectSeed, ref int index, MyRandom random, IMyModule densityFunction)
        {
            cell.AddObject(objectSeed);
            switch (objectSeed.Type)
            {
                case MyObjectSeedType.Asteroid:
                    m_asteroidSeedCount++;
                    break;
                case MyObjectSeedType.AsteroidCluster:
                    objectSeed.Type = MyObjectSeedType.Asteroid;
                    m_asteroidSeedCount++;

                    m_tmpClusterBoxes.Add(objectSeed.BoundingVolume);

                    for (int j = 0; j < OBJECT_MAX_IN_CLUSTER; ++j)
                    {
                        var direction = GetRandomDirection(random);
                        var size = GetClusterObjectSize(random.NextDouble());
                        var distance = MathHelper.Lerp(OBJECT_MIN_DISTANCE_CLUSTER, OBJECT_MAX_DISTANCE_CLUSTER, random.NextDouble());
                        var clusterObjectPosition = objectSeed.BoundingVolume.Center + direction * (size + objectSeed.BoundingVolume.HalfExtents.Length() * 2 + distance);

                        ProfilerShort.Begin("GetValue");
                        var value = densityFunction.GetValue(clusterObjectPosition.X, clusterObjectPosition.Y, clusterObjectPosition.Z);
                        ProfilerShort.End();

                        if (value < OBJECT_DENSITY_CLUSTER) // -1..+1
                        {
                            var clusterObjectSeed = new MyObjectSeed(cell, clusterObjectPosition, size);
                            clusterObjectSeed.Seed = random.Next();
                            clusterObjectSeed.Index = index++;
                            clusterObjectSeed.Type = GetClusterSeedType(random.NextDouble());

                            bool overlaps = false;
                            foreach (var box in m_tmpClusterBoxes)
                            {
                                if (overlaps |= clusterObjectSeed.BoundingVolume.Intersects(box))
                                {
                                    break;
                                }
                            }

                            if (!overlaps)
                            {
                                m_tmpClusterBoxes.Add(clusterObjectSeed.BoundingVolume);
                                GenerateObject(cell, clusterObjectSeed, ref index, random, densityFunction);
                            }
                        }
                    }
                    m_tmpClusterBoxes.Clear();
                    break;
                case MyObjectSeedType.EncounterAlone:
                case MyObjectSeedType.EncounterSingle:
                case MyObjectSeedType.EncounterMulti:
                    m_encounterSeedCount++;
                    break;
                default:
                    throw new InvalidBranchException();
                    break;
            }
        }
        private static MyMaterialLayer CreatePoleLayer(MyRandom random, MyPoleParams poleParams, float startHeight, float outerRadius, ref int layerOffset)
        {
            if (m_materialsByOreType.ContainsKey("Ice") == false)
            {
                return null;
            }

            MyMaterialLayer poleLayer = null;
            float poleProbability = random.NextFloat(0, 1);

            if (poleParams != null && poleProbability < poleParams.Probability)
            {
                layerOffset++;

                poleLayer = new MyMaterialLayer();
                poleLayer.StartHeight = startHeight;
                poleLayer.EndHeight = outerRadius;
                poleLayer.MaterialDefinition = m_materialsByOreType["Ice"][random.Next() % m_materialsByOreType["Ice"].Count];
                poleLayer.HeightEndDeviation = 0;
                poleLayer.HeightStartDeviation = 0;
                poleLayer.StartAngle = random.NextFloat(poleParams.Angle.Min, poleParams.Angle.Max);
                poleLayer.EndAngle = 1.0f;
                poleLayer.AngleStartDeviation = random.NextFloat(poleParams.AngleDeviation.Min, poleParams.AngleDeviation.Max);
            }
            return poleLayer;
        }
 private static Vector3 CreateRandomPointInBox(MyRandom self, float boxSize)
 {
     return new Vector3(
         self.NextFloat() * boxSize,
         self.NextFloat() * boxSize,
         self.NextFloat() * boxSize);
 }
        private static MyMaterialLayer[] CreateMaterialLayers(MyPlanetDefinition planetDefinition, bool isHostile, MyRandom random, float averagePlanetRadius, float hillHalfDeviation, float canyonHalfDeviation, ref float outerRadius, ref float innerRadius)
        {
            int numLayers = random.Next((int)planetDefinition.NumLayers.Min, (int)planetDefinition.NumLayers.Max);

            float startHeight = averagePlanetRadius - canyonHalfDeviation;
            outerRadius = averagePlanetRadius + hillHalfDeviation;
            innerRadius = averagePlanetRadius - canyonHalfDeviation;

            int layerOffset = 0;

            MyMaterialLayer southPoleLayer = CreatePoleLayer(random, planetDefinition.SouthPole, startHeight, outerRadius, ref layerOffset);
            MyMaterialLayer northPoleLayer = CreatePoleLayer(random, planetDefinition.NorthPole, startHeight, outerRadius, ref layerOffset);


            MyMaterialLayer[] materialLayers = new MyMaterialLayer[numLayers + layerOffset];

            float endAngle = 1;
            float startAngle = -1;
            int currentLayer = 0;

            if (southPoleLayer != null)
            {
                materialLayers[currentLayer] = southPoleLayer;
                endAngle = southPoleLayer.StartAngle;
                currentLayer++;
            }

            if (northPoleLayer != null)
            {
                materialLayers[currentLayer] = northPoleLayer;
                northPoleLayer.EndAngle = northPoleLayer.StartAngle;
                northPoleLayer.StartAngle = -1.0f;
                northPoleLayer.AngleEndDeviation = northPoleLayer.AngleStartDeviation;
                northPoleLayer.AngleStartDeviation = 0.0f;
                startAngle = northPoleLayer.EndAngle;
            }

            float step = (outerRadius - innerRadius) / materialLayers.Length;


            float organicHeightEnd = random.NextFloat(planetDefinition.OrganicHeightEnd.Min, planetDefinition.OrganicHeightEnd.Max);
            float metalsHeightEnd = random.NextFloat(planetDefinition.MetalsHeightEndHostile.Min, planetDefinition.MetalsHeightEndHostile.Max);
            float floraMaterialSpawnProbability = random.NextFloat(planetDefinition.FloraMaterialSpawnProbability.Min, planetDefinition.FloraMaterialSpawnProbability.Max);
            float metalsSpawnProbability = random.NextFloat(planetDefinition.MetalsSpawnProbability.Min, planetDefinition.MetalsSpawnProbability.Max);
            float metalsSpawnValue = random.NextFloat(0, 1);

            for (int i = layerOffset; i < materialLayers.Length; ++i)
            {
                float layerHeight = random.NextFloat(0, step);
                materialLayers[i] = new MyMaterialLayer();
                materialLayers[i].StartHeight = startHeight;
                materialLayers[i].EndHeight = startHeight + layerHeight;
                materialLayers[i].StartAngle = startAngle;
                materialLayers[i].EndAngle = endAngle;
                materialLayers[i].HeightStartDeviation = random.NextFloat(0, 100.0f / (float)(i + 1));
                materialLayers[i].AngleStartDeviation = 0;
                materialLayers[i].HeightEndDeviation = random.NextFloat(0, 100.0f / (float)(i + 1));
                materialLayers[i].AngleEndDeviation = 0;

                MyVoxelMaterialDefinition materialDefinition = null;

                if (m_materialsByOreType.ContainsKey("Stone") == true)
                {
                    materialDefinition = m_materialsByOreType["Stone"][random.Next() % m_materialsByOreType["Stone"].Count];
                }

                if (planetDefinition.HasAtmosphere && isHostile == false)
                {
                    if ((outerRadius - startHeight) > ((outerRadius - innerRadius) * (1 - organicHeightEnd)))
                    {
                        float value = random.NextFloat(0, 1);
                        if (value > floraMaterialSpawnProbability)
                        {
                            materialDefinition = m_organicMaterials[random.Next() % m_organicMaterials.Count];
                        }
                        else
                        {
                            materialDefinition = m_spawningMaterials[random.Next() % m_spawningMaterials.Count];
                        }
                    }
                }
                else
                {
                    if (metalsSpawnValue < metalsSpawnProbability)
                    {
                        if ((outerRadius - startHeight) > ((outerRadius - innerRadius) * (1 - metalsHeightEnd)))
                        {
                            MyOreProbability probablity = GetOre(random.NextFloat(0, 1));
                            if (probablity != null)
                            {
                                materialLayers[i].EndHeight = materialLayers[i].StartHeight - 1;
                                materialLayers[i].HeightStartDeviation *= probablity.Probability;
                                materialLayers[i].HeightEndDeviation *= probablity.Probability;

                                materialDefinition = m_materialsByOreType[probablity.OreName][random.Next() % m_materialsByOreType[probablity.OreName].Count];
                            }
                        }
                    }
                }

                materialLayers[i].MaterialDefinition = materialDefinition;
                startHeight += layerHeight;
            }
            return materialLayers;
        }
 private static Quaternion CreateRandomRotation(MyRandom self)
 {
     Quaternion q = new Quaternion(
         self.NextFloat() * 2f - 1f,
         self.NextFloat() * 2f - 1f,
         self.NextFloat() * 2f - 1f,
         self.NextFloat() * 2f - 1f);
     q.Normalize();
     return q;
 }
 public MyInfiniteDensityFunction(MyRandom random, double frequency)
 {
     noise = new MySimplexFast(random.Next(), frequency);
 }