Пример #1
0
        public ProceduralConstructionSeed(ProceduralFactionSeed faction, Vector3D location, Ob_ProceduralConstructionSeed ob)
        {
            Faction                  = faction;
            Location                 = location;
            Name                     = ob.Name;
            Orientation              = ob.Orientation;
            Population               = ob.Population;
            Seed                     = ob.Seed;
            Speciality               = ob.Speciality;
            SpecialityExport         = ob.SpecialityExport;
            m_blockCountRequirements = new Dictionary <SupportedBlockTypes, BlockRequirement>();
            foreach (var k in ob.BlockCountRequirements)
            {
                m_blockCountRequirements[k.Type] = new BlockRequirement(k.Count, k.ErrorMultiplier);
            }
            m_imports      = TradeReqToDictionary(ob.Imports);
            m_exports      = TradeReqToDictionary(ob.Exports);
            m_localStorage = TradeReqToDictionary(ob.Local);

            ComputeStorageVolumeMass(out StorageVolume, out StorageMass);
        }
Пример #2
0
 private ProceduralFactionSpeciality Add(ProceduralStationSpeciality s, float w)
 {
     StationSpecialities.Add(new KeyValuePair <ProceduralStationSpeciality, float>(s, w));
     return(this);
 }
Пример #3
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;
            }
        }