private bool AddDerivedRecipes()
        {
            var changed = false;

            foreach (var kv in MyDefinitionManager.Static.GetBlueprintDefinitions())
            {
                // Try 1:  Map inputs onto outputs
                if (kv.Prerequisites.All(x => _props.ContainsKey(x.Id)) &&
                    kv.Results.All(x => !_props.ContainsKey(x.Id)))
                {
                    float totalInputMass = kv.Prerequisites.Sum(x =>
                                                                (float)x.Amount * MyDefinitionManager.Static.GetPhysicalItemDefinition(x.Id).Mass);
                    MaterialProperties combinedProps =
                        new MaterialProperties(default(MyDefinitionId), 0, 0, 0, 0, 0, 0, 0);
                    foreach (var p in kv.Prerequisites)
                    {
                        var mass = (float)p.Amount * MyDefinitionManager.Static.GetPhysicalItemDefinition(p.Id).Mass;
                        var prop = _props[p.Id];
                        combinedProps =
                            MaterialProperties.LinearCombination(default(MyDefinitionId), combinedProps, 1, prop, mass);
                    }

                    combinedProps = combinedProps.Clone(default(MyDefinitionId), 1 / totalInputMass);

                    foreach (var r in kv.Results)
                    {
                        _props[r.Id] = combinedProps.Clone(r.Id);
                    }
                    changed = true;
                }

                // Try 2:  Outputs onto inputs
                if (kv.Results.All(x => _props.ContainsKey(x.Id)) &&
                    kv.Prerequisites.All(x => !_props.ContainsKey(x.Id)))
                {
                    float totalOutputMass = kv.Results.Sum(x =>
                                                           (float)x.Amount * MyDefinitionManager.Static.GetPhysicalItemDefinition(x.Id).Mass);
                    MaterialProperties combinedProps =
                        new MaterialProperties(default(MyDefinitionId), 0, 0, 0, 0, 0, 0, 0);
                    foreach (var p in kv.Results)
                    {
                        var mass = (float)p.Amount * MyDefinitionManager.Static.GetPhysicalItemDefinition(p.Id).Mass;
                        var prop = _props[p.Id];
                        combinedProps =
                            MaterialProperties.LinearCombination(default(MyDefinitionId), combinedProps, 1, prop, mass);
                    }

                    combinedProps = combinedProps.Clone(default(MyDefinitionId), 1 / totalOutputMass);

                    foreach (var r in kv.Prerequisites)
                    {
                        _props[r.Id] = combinedProps.Clone(r.Id);
                    }
                    changed = true;
                }
            }

            return(changed);
        }
        public MaterialProperties PropertiesOf(MyDefinitionId id, MaterialProperties defaultTemplate = null)
        {
            MaterialProperties res;

            if (!_props.TryGetValue(id, out res))
            {
                _props[id] = res = (defaultTemplate ?? IronMaterial).Clone(id);
            }
            return(res);
        }
예제 #3
0
 public static MaterialProperties LinearCombination(MyDefinitionId @new, MaterialProperties a, float av,
                                                    MaterialProperties b, float bv)
 {
     return(new MaterialProperties(@new,
                                   a.DensitySolid * av + b.DensitySolid * bv,
                                   a.ThermalConductivity * av + b.ThermalConductivity * bv,
                                   a.MeltingPoint * av + b.MeltingPoint * bv,
                                   a.BoilingPoint * av + b.BoilingPoint * bv,
                                   a.SpecificHeat * av + b.SpecificHeat * bv,
                                   a.EnthalpyOfFusion * av + b.EnthalpyOfFusion * bv,
                                   a.EnthalpyOfVaporization * av + b.EnthalpyOfVaporization * bv));
 }