/// <summary> /// Gets the amount of raw resources (ores) required to produce the given blueprint. /// </summary> /// <param name="id">definition ID</param> /// <returns></returns> public double GetRawResourcesFor(MyDefinitionId id) { double required; if (m_rawResourcesFor.TryGetValue(id, out required)) { return(required); } required = 0; m_rawResourcesFor[id] = required; MyCubeBlockDefinition cubeDef = MyDefinitionManager.Static.GetCubeBlockDefinition(id); if (cubeDef != null) { foreach (var kv in cubeDef.Components) { required += kv.Count * GetRawResourcesFor(kv.Definition.Id); } m_rawResourcesFor[id] = required; return(required); } IndexedBlueprint index = GetTopLevelProducing(id); if (index != null) { foreach (var kv in index.Ingredients) { if (kv.Key.TypeId == typeof(MyObjectBuilder_Ore) || kv.Key.SubtypeName.IndexOf("ore", StringComparison.OrdinalIgnoreCase) >= 0) { required += (double)kv.Value; } } m_rawResourcesFor[id] = required; return(required); } return(0); }
private void Load() { foreach (var t in m_index) { t.Clear(); } var needsElaboration = new Queue <IndexedBlueprint>(); // Load base blueprints. foreach (var blueprint in MyDefinitionManager.Static.GetBlueprintDefinitions()) { if (blueprint is MyCompositeBlueprintDefinition) { continue; } foreach (var result in blueprint.Results) { if (result.Amount > 0) { var bp = new IndexedBlueprint(blueprint, result.Id, result.Amount, false); bp.Blueprints[blueprint] = 1.0 / (double)result.Amount; needsElaboration.Enqueue(bp); GetSafe(result.Id, BlueprintType.OriginalProducer).Add(bp); GetSafe(result.Id, BlueprintType.AllProducers).Add(bp); } } foreach (var src in blueprint.Prerequisites) { if (src.Amount > 0) { var bp = new IndexedBlueprint(blueprint, src.Id, src.Amount, true); bp.Blueprints[blueprint] = 1.0 / (double)src.Amount; GetSafe(src.Id, BlueprintType.Consumer).Add(bp); } } } while (needsElaboration.Count > 0) { var bp = needsElaboration.Dequeue(); var elaborate = bp.Ingredients.Keys.Any(m_index[(int)BlueprintType.AllProducers].ContainsKey); if (!elaborate) { GetSafe(bp.Result, BlueprintType.TopLevelProducer).Add(bp); continue; } var recipe = new Dictionary <MyDefinitionId, MyFixedPoint>(MyDefinitionId.Comparer); var bps = new Dictionary <MyBlueprintDefinitionBase, double>(bp.Blueprints); foreach (var kv in bp.Ingredients) { List <IndexedBlueprint> bpChild; if (!m_index[(int)BlueprintType.AllProducers].TryGetValue(kv.Key, out bpChild)) { recipe.AddValue(kv.Key, kv.Value); continue; } if (bpChild == null || bpChild.Count <= 0) { continue; } // Something better than the last one? var bpSrc = bpChild[bpChild.Count - 1]; foreach (var x in bpSrc.Blueprints) { bps.AddValue(x.Key, x.Value * (double)kv.Value); } foreach (var kvC in bpSrc.Ingredients) { recipe.AddValue(kvC.Key, kvC.Value * kv.Value); } } var bpOut = new IndexedBlueprint(bp.Result, recipe); foreach (var kv in bps) { bpOut.Blueprints[kv.Key] = kv.Value; } needsElaboration.Enqueue(bpOut); GetSafe(bp.Result, BlueprintType.AllProducers).Add(bpOut); } }