Beispiel #1
0
 protected EnumerationWeights()
 {
     foreach (var enumVal in (T[])Enum.GetValues(typeof(T)))
     {
         var newWeight = new WeightedChoice <T>(enumVal, 0d);
         _weightsDictionary.Add(enumVal, newWeight);
         _weights.Add(newWeight);
     }
 }
Beispiel #2
0
 // Update is called once per frame
 void Update()
 {
     nextObstacleTime -= Time.deltaTime;
     if (nextObstacleTime < 0.0f)
     {
         Debug.Assert(minObstacleY < maxObstacleY);
         var delta               = maxObstacleY - minObstacleY;
         var obstaclePositionY   = minObstacleY + Random.value * delta;
         var newObstaclePosition = new Vector3(ObstaclePositionX, obstaclePositionY, transform.position.z);
         var prefab              = WeightedChoice.ChoiceWithFilter <WeightedObstacleController, ObstacleController>(
             weightedObstaclePrefabs,
             item => item.startTime <= Time.time - GameController.Instance.startGameTime);
         var obstacle = Instantiate(prefab, newObstaclePosition, Quaternion.identity);
         nextObstacleTime += obstacleTime;
     }
 }
Beispiel #3
0
    private void GenerateBorderPipes()
    {
        //Sides
        float     precompTotal = WeightedChoice.PrecompTotal(borderChances);
        Furniture prefab;

        //Horizontal sides and corners
        for (int xPos = x; xPos < x + width; xPos++)
        {
            prefab = RandomBorderItem(precompTotal);
            Vector2 position = new Vector3(xPos, y);
            if (prefab != null)
            {
                Rect rect = new Rect(position.x, position.y, borderPipeBreadth, borderPipeBreadth);
                AttemptFurnitureInstantiation(prefab, rect);
            }
            prefab   = RandomBorderItem(precompTotal);
            position = new Vector3(xPos, y + height - 1);
            if (prefab != null)
            {
                Rect rect = new Rect(position.x, position.y, borderPipeBreadth, borderPipeBreadth);
                AttemptFurnitureInstantiation(prefab, rect);
            }
        }
        //Vertical sides
        for (int yPos = y + 1; yPos < y + height - 1; yPos++)
        {
            prefab = RandomBorderItem(precompTotal);
            Vector2 position = new Vector3(x, yPos);
            if (prefab != null)
            {
                Rect rect = new Rect(position.x, position.y, borderPipeBreadth, borderPipeBreadth);
                AttemptFurnitureInstantiation(prefab, rect);
            }
            prefab   = RandomBorderItem(precompTotal);
            position = new Vector3(x + width - 1, yPos);
            if (prefab != null)
            {
                Rect rect = new Rect(position.x, position.y, borderPipeBreadth, borderPipeBreadth);
                AttemptFurnitureInstantiation(prefab, rect);
            }
        }
    }
Beispiel #4
0
    private void InstallRandomPersonality(Ghost ghost)
    {
        int maxAttempts     = 100;
        int currentAttempts = 0;

        while (!ghost.PersonalityIsComplete())
        {
            if (personalityInstallers.Count == 0)
            {
                throw new System.Exception("Ran out of GhostPersonalityInstallers");
            }
            GhostPersonalityInstaller personalityInstaller = WeightedChoice.Choose(personalityInstallers, PIChances);
            personalityInstaller.InstallPersonality(ghost);
            if (maxAttempts <= currentAttempts)
            {
                throw new System.Exception("Installing personality took too long");
            }
            ++currentAttempts;
        }
    }
Beispiel #5
0
    private void SelectTarget()
    {
        var playerObjects = GameObject.FindGameObjectsWithTag(targetTag);

        var entries = playerObjects.ToList().Where(o => o != null && o.activeSelf).ToList();

        if (!entries.Any())
        {
            Destroy(gameObject);
            return;
        }

        _target = WeightedChoice.SelectFromList(
            entries,
            targetGameObject =>
            1 / Vector3.Distance(
                targetGameObject.GetComponent <Transform>().position,
                gameObject.GetComponent <Transform>().position
                )
            );

        _agent.SetDestination(_target.GetComponent <Transform>().position);
    }
Beispiel #6
0
    //Last is two space chance;

    public FurnitureDimensionSet GetRandomOuterGameObject()
    {
        return(WeightedChoice.Choose <FurnitureDimensionSet>(outerChoices, outerChances));
    }
        protected override bool PrivateUpdate(ScenarioFrame frame)
        {
            List <AfterschoolActivity> allChoices = sCareerGetAfterSchoolActivityList.Invoke <List <AfterschoolActivity> >(new object[] { Sim });

            if (allChoices == null)
            {
                allChoices = new List <AfterschoolActivity>();

                foreach (AfterschoolActivityType type in Enum.GetValues(typeof(AfterschoolActivityType)))
                {
                    if ((Sim.Child) && (!AfterschoolActivity.IsChildActivity(type)))
                    {
                        continue;
                    }

                    if ((Sim.Teen) && (!AfterschoolActivity.IsTeenActivity(type)))
                    {
                        continue;
                    }

                    GreyedOutTooltipCallback greyedOutTooltipCallback = null;
                    if (!AfterschoolActivity.MeetsCommonAfterschoolActivityRequirements(Sim.CreatedSim, type, ref greyedOutTooltipCallback))
                    {
                        continue;
                    }

                    allChoices.Add(new AfterschoolActivity(type));
                }
            }

            List <WeightedChoice> choices = new List <WeightedChoice>();

            foreach (AfterschoolActivity activity in allChoices)
            {
                float weight = 0;
                int   count  = 0;

                foreach (SkillNames actualSkill in activity.ActivitySkillNameList)
                {
                    SkillNames skill = actualSkill;
                    switch (skill)
                    {
                    case SkillNames.BassGuitar:
                    case SkillNames.Piano:
                    case SkillNames.Drums:
                        skill = SkillNames.Guitar;
                        break;
                    }

                    if (ScoringLookup.HasScoring(skill.ToString()))
                    {
                        weight += AddScoring(skill.ToString(), Sim);
                    }
                    else
                    {
                        Common.DebugNotify("AfterschoolActivity Missing " + skill.ToString());

                        weight += AddScoring("AfterschoolActivity", Sim);
                    }

                    count++;
                }

                if (weight < 0)
                {
                    continue;
                }

                if (count == 0)
                {
                    count = 1;
                }

                choices.Add(new WeightedChoice(activity.CurrentActivityType, weight / count));
            }

            if (choices.Count == 0)
            {
                IncStat("No Choices");
                return(false);
            }

            WeightedChoice choice = RandomUtil.GetWeightedRandomObjectFromList(choices);

            AfterschoolActivity.AddNewActivity(Sim.CreatedSim, choice.mType);
            return(true);
        }
Beispiel #8
0
 private Furniture RandomBorderFurniture(float precompTotal)
 {
     return(WeightedChoice.Choose(borderOptions, borderChances, precompTotal));
 }
Beispiel #9
0
    public override void GenerateFurniture()
    {
        GenerateLightSwitch();
        float precompTotal = WeightedChoice.PrecompTotal(furnitureChances);

        if (orientation == Orientation.Horizontal)
        {
            for (int xPos = x + 1; xPos < x + width - 1;)
            {
                Furniture prefab;
                if (xPos < x + width - 2)
                {
                    prefab = RandomFurniture(precompTotal);
                }
                else
                {
                    prefab = tileset.smallWineShelf;
                }
                int furnitureBreadth;
                if (prefab == tileset.largeWineShelf)
                {
                    furnitureBreadth = 2;
                }
                else
                {
                    furnitureBreadth = 1;
                }

                InstantiateFurniture(prefab, new Vector2(xPos, y));
                xPos += furnitureBreadth;
            }
            for (int xPos = x + 1; xPos < x + width - 1;)
            {
                Furniture prefab;
                if (xPos < x + width - 2)
                {
                    prefab = RandomFurniture(precompTotal);
                }
                else
                {
                    prefab = tileset.smallWineShelf;
                }
                int furnitureBreadth;
                if (prefab == tileset.largeWineShelf)
                {
                    furnitureBreadth = 2;
                }
                else
                {
                    furnitureBreadth = 1;
                }

                InstantiateFurniture(prefab, new Vector2(xPos, y + height - furnitureBreadth));
                xPos += furnitureBreadth;
            }
        }
        else          //Orientation.Vertical
        {
            for (int yPos = y + 1; yPos < y + height - 1;)
            {
                Furniture prefab;
                if (yPos < y + height - 2)
                {
                    prefab = RandomFurniture(precompTotal);
                }
                else
                {
                    prefab = tileset.smallWineShelf;
                }
                int furnitureBreadth;
                if (prefab == tileset.largeWineShelf)
                {
                    furnitureBreadth = 2;
                }
                else
                {
                    furnitureBreadth = 1;
                }

                InstantiateFurniture(prefab, new Vector2(x, yPos));
                yPos += furnitureBreadth;
            }
            for (int yPos = y + 1; yPos < y + height - 1;)
            {
                Furniture prefab;
                if (yPos < y + height - 2)
                {
                    prefab = RandomFurniture(precompTotal);
                }
                else
                {
                    prefab = tileset.smallWineShelf;
                }
                int furnitureBreadth;
                if (prefab == tileset.largeWineShelf)
                {
                    furnitureBreadth = 2;
                }
                else
                {
                    furnitureBreadth = 1;
                }

                InstantiateFurniture(prefab, new Vector2(x + width - furnitureBreadth, yPos));
                yPos += furnitureBreadth;
            }
        }
    }
Beispiel #10
0
 private Furniture RandomFurniture(float precompTotal)
 {
     return(WeightedChoice.Choose(furnitureOptions, furnitureChances, precompTotal));
 }
Beispiel #11
0
 public Furniture RandomPantryItem(float precompTotal)
 {
     return(WeightedChoice.Choose(pantryOptions, pantryChances, precompTotal));
 }
Beispiel #12
0
    public void GeneratePantryFurniture()
    {
        //Corners
        float     precompTotal = WeightedChoice.PrecompTotal(pantryCornerChances);
        Furniture prefab       = RandomPantryItem(precompTotal);

        if (prefab != null)
        {
            InstantiateFurniture(prefab, new Vector2(pX, pY));
        }
        prefab = RandomPantryItem(precompTotal);
        if (prefab != null)
        {
            InstantiateFurniture(prefab, new Vector2(pX + pWidth - 1, pY));
        }
        prefab = RandomPantryItem(precompTotal);
        if (prefab != null)
        {
            InstantiateFurniture(prefab, new Vector2(pX, pY + pHeight - 1));
        }
        prefab = RandomPantryItem(precompTotal);
        if (prefab != null)
        {
            InstantiateFurniture(prefab, new Vector2(pX + pWidth - 1, pY + pHeight - 1));
        }

        //Sides
        precompTotal = WeightedChoice.PrecompTotal(pantryChances);
        //Horizontal sides
        for (int xPos = pX + 1; xPos < pX + pWidth - 1; xPos++)
        {
            prefab = RandomPantryItem(precompTotal);
            Vector2 position = new Vector3(xPos, pY);
            if (prefab != null && position != pantryDoorPosition)
            {
                InstantiateFurniture(prefab, position);
            }
            prefab   = RandomPantryItem(precompTotal);
            position = new Vector3(xPos, pY + pHeight - 1);
            if (prefab != null && position != pantryDoorPosition)
            {
                InstantiateFurniture(prefab, position);
            }
        }
        //Vertical sides
        for (int yPos = pY + 1; yPos < pY + pHeight - 1; yPos++)
        {
            prefab = RandomPantryItem(precompTotal);
            Vector2 position = new Vector3(pX, yPos);
            if (prefab != null && position != pantryDoorPosition)
            {
                InstantiateFurniture(prefab, position);
            }
            prefab   = RandomPantryItem(precompTotal);
            position = new Vector3(pX + pWidth - 1, yPos);
            if (prefab != null && position != pantryDoorPosition)
            {
                InstantiateFurniture(prefab, position);
            }
        }
    }
Beispiel #13
0
        public ProceduralConstructionSeed(ProceduralFactionSeed faction, Vector4D locationDepth, Quaternion?orientation, long seed,
                                          int?populationOverride = null)
        {
            Seed     = seed;
            Location = locationDepth.XYZ();
            Faction  = faction;
            var tmpRandom = new Random((int)seed);

            Population = populationOverride ?? (int)MyMath.Clamp((float)(Math.Round(5 * tmpRandom.NextExponential() * locationDepth.W)), 1, 100);
            var sqrtPopulation = Math.Sqrt(Population);

            var choice = new WeightedChoice <ProceduralStationSpeciality>();

            foreach (var kv in Faction.Specialities)
            {
                foreach (var kt in kv.Key.StationSpecialities)
                {
                    choice.Add(kt.Key, kv.Value * kt.Value);
                }
            }
            Speciality = choice.Choose(tmpRandom.NextDouble(), WeightedChoice <ProceduralStationSpeciality> .WeightedNormalization.ClampToZero);
            m_exports  = new Dictionary <MyDefinitionId, TradeRequirements>(MyDefinitionId.Comparer);
            m_imports  = new Dictionary <MyDefinitionId, TradeRequirements>(MyDefinitionId.Comparer);
            List <MyDefinitionBase> rchoice = null;
            var specialExport = tmpRandom.NextDouble() <= Speciality.SpecializationChance;

            if (specialExport)
            {
                rchoice = new List <MyDefinitionBase>();
            }
            foreach (var kv in MyDefinitionManager.Static.GetAllDefinitions())
            {
                if (Speciality.CanExport(kv))
                {
                    m_exports.Add(kv.Id, default(TradeRequirements));
                    rchoice?.Add(kv);
                }
                if (Speciality.CanImport(kv))
                {
                    m_imports.Add(kv.Id, default(TradeRequirements));
                }
            }
            SpecialityExport = null;
            if (specialExport)
            {
                // ReSharper disable once PossibleNullReferenceException
                SpecialityExport = rchoice[tmpRandom.Next(0, rchoice.Count)].Id;
                m_exports.Clear();
                m_exports.Add(SpecialityExport.Value, default(TradeRequirements));
            }

            var nameBuilder = new StringBuilder();

            nameBuilder.Append(Faction.FounderName).Append("'s ");
            if (SpecialityExport.HasValue)
            {
                nameBuilder.Append(MyDefinitionManager.Static.GetDefinition(SpecialityExport.Value).DisplayNameText).Append(" ");
            }
            else if (Speciality.GeneralizedPrefixes != null && Speciality.GeneralizedPrefixes.Length > 0)
            {
                nameBuilder.Append(tmpRandom.NextUniformChoice(Speciality.GeneralizedPrefixes)).Append(" ");
            }
            nameBuilder.Append(tmpRandom.NextUniformChoice(Speciality.Suffixes));
            Name = nameBuilder.ToString();

            // Compute hotlisted import amounts
            {
                var keys = m_imports.Keys.ToList();
                foreach (var kv in keys)
                {
                    var baseMult = Population * tmpRandom.NextExponential() / keys.Count;
                    var k        = MyDefinitionManager.Static.GetDefinition(kv);
                    var pi       = k as MyPhysicalItemDefinition;
                    if (k is MyComponentDefinition)
                    {
                        baseMult *= 100 / pi.Mass;
                    }
                    else if (k.Id.TypeId == typeof(MyObjectBuilder_Ore))
                    {
                        baseMult *= 10000 / (1 + Math.Sqrt(OreUtilities.GetRarity(k.Id)));
                    }
                    else if (k.Id.TypeId == typeof(MyObjectBuilder_Ingot))
                    {
                        baseMult *= 5000 * OreUtilities.GetOutputRatio(k.Id);
                    }
                    else if (k.Id.TypeId == typeof(MyObjectBuilder_GasProperties))
                    {
                        baseMult *= 250000; // 1/10th a large tank
                    }
                    else if (pi != null)
                    {
                        baseMult *= 10 / pi.Mass;
                    }

                    m_imports[kv] = new TradeRequirements(baseMult, Math.Sqrt(baseMult));
                }
            }

            // Compute exported amounts
            {
                var keys = m_exports.Keys.ToList();
                foreach (var kv in keys)
                {
                    var baseMult = Population * Population * tmpRandom.NextExponential() / keys.Count;
                    var k        = MyDefinitionManager.Static.GetDefinition(kv);
                    var pi       = k as MyPhysicalItemDefinition;
                    if (k is MyComponentDefinition)
                    {
                        baseMult *= 100 / pi.Mass;
                    }
                    else if (k.Id.TypeId == typeof(MyObjectBuilder_Ore))
                    {
                        baseMult *= 10000 / (1 + Math.Sqrt(OreUtilities.GetRarity(k.Id)));
                    }
                    else if (k.Id.TypeId == typeof(MyObjectBuilder_Ingot))
                    {
                        baseMult *= 5000 * OreUtilities.GetOutputRatio(k.Id);
                    }
                    else if (k.Id.TypeId == typeof(MyObjectBuilder_GasProperties))
                    {
                        baseMult *= 250000; // 1/10th a large tank
                    }
                    m_exports[kv] = new TradeRequirements(baseMult, Math.Sqrt(baseMult));
                }
            }

            // Using exports, compute imports
            {
                foreach (var kv in m_exports)
                {
                    var producer = BlueprintIndex.Instance.GetAllProducing(kv.Key, true).ToList();
                    foreach (var blueprint in producer)
                    {
                        foreach (var ingredient in blueprint.Ingredients)
                        {
                            var aStorage    = kv.Value.Storage * (double)ingredient.Value / producer.Count;
                            var aThroughput = kv.Value.Throughput * (double)ingredient.Value / producer.Count;
                            TradeRequirements current;
                            m_imports[ingredient.Key] = m_imports.TryGetValue(ingredient.Key, out current) ?
                                                        new TradeRequirements(current.Storage + aStorage, current.Throughput + aThroughput) :
                                                        new TradeRequirements(aStorage, aThroughput);
                        }
                    }
                }
            }

            m_localStorage = new Dictionary <MyDefinitionId, TradeRequirements>(MyDefinitionId.Comparer)
            {
                // ~0.5MWH / person stored, ~1MW / person produced
                [MyResourceDistributorComponent.ElectricityId] = new TradeRequirements(Population * 0.5 * tmpRandom.NextNormal(1, 0.1), Population * tmpRandom.NextNormal(1, 0.1), 10, 10),
                // one large O2 tank / 10 person stored (~2 days), 1 O2/sec/person produced
                [MyResourceDistributorComponent.OxygenId] = new TradeRequirements(Population * 1e4 * tmpRandom.NextNormal(1, 0.1), Population * 1 * tmpRandom.NextNormal(1, 0.1), 10, 10),
                // I don't even know how I'd guess this
                [MyResourceDistributorComponent.HydrogenId] = new TradeRequirements(Population * 1e4 * tmpRandom.NextExponential(), tmpRandom.NextExponential() * sqrtPopulation)
            };

            // Compute mass & volume of storage
            ComputeStorageVolumeMass(out StorageVolume, out StorageMass);

            // ReSharper disable once UseObjectOrCollectionInitializer
            m_blockCountRequirements = new Dictionary <SupportedBlockTypes, BlockRequirement>(SupportedBlockTypesEquality.Instance);
            // living quarters.
            var addlLiving = Faction.AttributeWeight(ProceduralFactionSpeciality.Housing);

            m_blockCountRequirements[SupportedBlockTypes.CryoChamber]    = Population + Math.Max(0, (int)Math.Round(sqrtPopulation * (tmpRandom.NextNormal(1, 2) + addlLiving)));
            m_blockCountRequirements[SupportedBlockTypes.MedicalRoom]    = new BlockRequirement(Math.Max(1, (int)Math.Round(sqrtPopulation * (tmpRandom.NextNormal(0.5, 0.5) + Math.Max(addlLiving, Faction.AttributeWeight(ProceduralFactionSpeciality.Military))))), 1e3);
            m_blockCountRequirements[SupportedBlockTypes.ShipController] = Math.Max(1, (int)Math.Round(Population * tmpRandom.NextNormal(0.5, 0.5)));
            // how "defensive" this group is
            m_blockCountRequirements[SupportedBlockTypes.Weapon] = Math.Max(0, (int)Math.Round(Population * tmpRandom.NextNormal(2, 2) * Faction.AttributeWeight(ProceduralFactionSpeciality.Military)));
            // ship repair?
            m_blockCountRequirements[SupportedBlockTypes.ShipConstruction] = Math.Max(0, (int)Math.Round(Population * tmpRandom.NextNormal(4, 3) * Faction.AttributeWeight(ProceduralFactionSpeciality.Repair)));
            // docking?
            m_blockCountRequirements[SupportedBlockTypes.Docking] = Math.Max(1, (int)Math.Round(sqrtPopulation * tmpRandom.NextNormal(1, 1) * Faction.AttributeWeight(ProceduralFactionSpeciality.Housing)));
            // comms?
            m_blockCountRequirements[SupportedBlockTypes.Communications] = new BlockRequirement(Math.Max(1, (int)Math.Round(sqrtPopulation * MyMath.Clamp((float)tmpRandom.NextNormal(), 0, 1))), 1e6);

            Orientation = tmpRandom.NextQuaternion();
            // Branched, since we want to consume a quaternion from the random.
            if (orientation != null)
            {
                Orientation = orientation.Value;
            }
        }