Files
DSP_Mods/UXAssist/Production/ProcessEvaluator.cs
T
soarqin f0773ffb9b fix(production): correct blueprint power and planner edge cases
Read the Advanced Mining Machine speed stored with station settings in prebuild
and blueprint captures, and count charging power nodes such as Satellite
Substations at their idle draw. Skip recipes that consume their own product when
choosing an item's default recipe, so Refined Oil uses Plasma Refining instead
of Reforming Refine.

Run each planner recipe in one proliferation mode and report conflicting
explicit modes, keep material flows for recipe types the catalog does not know,
and attribute receiver warnings to their recipe. Compare simplex ratio ties
relatively and drive out near-zero artificials without amplifying residuals.
Bound the steady-state reduction so lossless cycles cannot stall blueprint
descriptions, and choose power units after rounding.

Remove redundant code: share the numeric and flow helpers, and drop the unused
IsExcessIntermediate threshold, legacy constructor and Capture overloads, the
multi-field updater, and the unused proliferator source table.
2026-10-25 22:01:05 +08:00

123 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using static UXAssist.Production.ProductionMath;
namespace UXAssist.Production;
public sealed class ProcessEvaluator
{
private readonly IProductionProcessAdapter[] _adapters;
public ProcessEvaluator(IEnumerable<IProductionProcessAdapter> adapters = null)
{
_adapters = (adapters ?? Array.Empty<IProductionProcessAdapter>())
.Concat(new IProductionProcessAdapter[]
{
new FractionationProcessAdapter(),
new AccumulatorExchangeAdapter(),
new RenewableGenerationAdapter(),
new MiningProcessAdapter(),
new FuelGenerationAdapter(),
new RayReceiverAdapter(),
new LaunchProcessAdapter(),
new GasCollectionAdapter(),
new ResearchProcessAdapter()
}).ToArray();
}
public bool TryEvaluate(ProductionCatalog catalog, ProductionRecipe recipe, ProductionBuilding building,
ProliferationMode mode, int level, IReadOnlyDictionary<string, double> operatingParameters,
out ProductionProcess process, out ProductionDiagnostic diagnostic)
{
process = null;
diagnostic = null;
if (catalog == null)
{
diagnostic = new ProductionDiagnostic(ProductionDiagnosticCode.DataNotReady, "Production data is not loaded.");
return false;
}
foreach (var adapter in _adapters)
{
if (adapter.CanEvaluate(recipe, building))
return adapter.TryEvaluate(catalog, recipe, building, mode, level, operatingParameters,
out process, out diagnostic);
}
if (recipe == null)
{
diagnostic = new ProductionDiagnostic(ProductionDiagnosticCode.UnsupportedProcess,
"No process adapter supports the selected building without a recipe.",
buildingItemId: building?.ItemId ?? 0);
return false;
}
// Recipe types unknown to the catalog, such as ones added by other mods, keep their material flows only
// while no building is selected.
if (!IsOrdinary(recipe.Category) && (recipe.Category != ProductionRecipeCategory.None || building != null))
{
diagnostic = new ProductionDiagnostic(ProductionDiagnosticCode.UnsupportedProcess,
"The recipe requires a special-process adapter.", recipeId: recipe.Id,
buildingItemId: building?.ItemId ?? 0);
return false;
}
if (building != null && (building.Category != recipe.Category || !FinitePositive(building.SpeedFactor) ||
!FiniteNonnegative(building.WorkingPowerWatts)))
{
diagnostic = new ProductionDiagnostic(ProductionDiagnosticCode.IncompatibleBuilding,
"The selected building cannot execute this recipe.", recipeId: recipe.Id,
buildingItemId: building.ItemId);
return false;
}
if (recipe.TimeTicks <= 0 || !ValidFlows(recipe.Inputs) || !ValidFlows(recipe.Outputs) ||
!Enum.IsDefined(typeof(ProliferationMode), mode))
{
diagnostic = new ProductionDiagnostic(ProductionDiagnosticCode.InvalidRequest,
"The recipe or proliferation mode is invalid.", recipeId: recipe.Id);
return false;
}
var speedBonus = 0.0;
var extraBonus = 0.0;
var powerMultiplier = 1.0;
if (mode != ProliferationMode.None)
{
if (recipe.Inputs.Count == 0 || level < 1 ||
!catalog.TryGetProliferation(level, out speedBonus, out extraBonus,
out powerMultiplier) ||
mode == ProliferationMode.ExtraProducts && !recipe.Productive)
{
diagnostic = new ProductionDiagnostic(ProductionDiagnosticCode.UnsupportedProliferation,
"The selected proliferation mode or level is unavailable for this recipe.", recipeId: recipe.Id);
return false;
}
}
var outputMultiplier = mode == ProliferationMode.ExtraProducts ? 1 + extraBonus : 1;
var cycleMultiplier = mode == ProliferationMode.Speedup ? 1 + speedBonus : 1;
var cycles = building == null ? 0 :
ProductionUnits.TicksPerMinute * building.SpeedFactor / recipe.TimeTicks * cycleMultiplier;
var power = building == null ? 0 : building.WorkingPowerWatts * powerMultiplier;
process = new ProductionProcess(recipe.Id, building?.ItemId ?? 0, mode,
mode == ProliferationMode.None ? 0 : level, recipe.Inputs,
recipe.Outputs.Select(output => new KeyValuePair<int, double>(output.Key, output.Value * outputMultiplier)),
cycles, power, building != null);
return true;
}
private static bool IsOrdinary(ProductionRecipeCategory category)
{
return category == ProductionRecipeCategory.Assemble || category == ProductionRecipeCategory.Smelt ||
category == ProductionRecipeCategory.Chemical || category == ProductionRecipeCategory.Refine ||
category == ProductionRecipeCategory.Particle || category == ProductionRecipeCategory.Research;
}
private static bool ValidFlows(IReadOnlyDictionary<int, double> flows)
{
return flows.All(flow => flow.Key > 0 && FinitePositive(flow.Value));
}
}