Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should not prefer inaccessible recipes: Treat as disabled #258

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Yafc.Model/Analysis/Analysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public static float ApproximateFlow(this FactorioObject recipe, bool atCurrentMi
return CostAnalysis.Get(atCurrentMilestones).flow[recipe];
}

public static float ProductCost(this Recipe recipe, bool atCurrentMilestones = false) {
return CostAnalysis.Get(atCurrentMilestones).recipeProductCost[recipe];
}

public static float RecipeWaste(this Recipe recipe, bool atCurrentMilestones = false) {
return CostAnalysis.Get(atCurrentMilestones).recipeWastePercentage[recipe];
}
Expand Down
7 changes: 0 additions & 7 deletions Yafc.Model/Analysis/CostAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public static CostAnalysis Get(bool atCurrentMilestones) {

public Mapping<FactorioObject, float> cost;
public Mapping<Recipe, float> recipeCost;
public Mapping<RecipeOrTechnology, float> recipeProductCost;
public Mapping<FactorioObject, float> flow;
public Mapping<Recipe, float> recipeWastePercentage;
public Goods[]? importantItems;
Expand Down Expand Up @@ -103,7 +102,6 @@ public override void Compute(Project project, ErrorCollector warnings) {
}

var export = Database.objects.CreateMapping<float>();
var recipeProductionCost = Database.recipesAndTechnologies.CreateMapping<float>();
recipeCost = Database.recipes.CreateMapping<float>();
flow = Database.objects.CreateMapping<float>();
var lastVariable = Database.goods.CreateMapping<Variable>();
Expand Down Expand Up @@ -300,10 +298,6 @@ public override void Compute(Project project, ErrorCollector warnings) {
{
export[o] += export[ingredient.goods] * ingredient.amount;
}

foreach (var product in recipe.products) {
recipeProductionCost[recipe] += product.amount * export[product.goods];
}
}
else if (o is Entity entity) {
float minimal = float.PositiveInfinity;
Expand All @@ -316,7 +310,6 @@ public override void Compute(Project project, ErrorCollector warnings) {
}
}
cost = export;
recipeProductCost = recipeProductionCost;

recipeWastePercentage = Database.recipes.CreateMapping<float>();
if (result is Solver.ResultStatus.OPTIMAL or Solver.ResultStatus.FEASIBLE) {
Expand Down
217 changes: 137 additions & 80 deletions Yafc.Model/Model/ProductionTable.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Yafc.Model/Model/ProductionTableContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public IEnumerable<RecipeRowProduct> Products {
internal float fuelUsagePerSecond => (float)(parameters.fuelUsagePerSecondPerRecipe * recipesPerSecond);
public UsedModule usedModules => parameters.modules;
public WarningFlags warningFlags => parameters.warningFlags;
public bool FindLink(Goods goods, [MaybeNullWhen(false)] out ProductionLink link) {
public bool FindLink(Goods? goods, [MaybeNullWhen(false)] out ProductionLink link) {
return linkRoot.FindLink(goods, out link);
}

Expand Down
10 changes: 3 additions & 7 deletions Yafc.Model/Model/RecipeParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ public struct UsedModule {
public int beaconCount;
}

internal class RecipeParameters(float recipeTime, float fuelUsagePerSecondPerBuilding, float productivity, WarningFlags warningFlags, ModuleEffects activeEffects, UsedModule modules) {
internal sealed record RecipeParameters(float recipeTime, float fuelUsagePerSecondPerBuilding, float productivity, WarningFlags warningFlags, ModuleEffects activeEffects, UsedModule modules) {
public const float MIN_RECIPE_TIME = 1f / 60;
public float recipeTime { get; } = recipeTime;
public float fuelUsagePerSecondPerBuilding { get; } = fuelUsagePerSecondPerBuilding;
public float productivity { get; } = productivity;
public WarningFlags warningFlags { get; internal set; } = warningFlags;
public ModuleEffects activeEffects { get; } = activeEffects;
public UsedModule modules { get; } = modules;

public static RecipeParameters Empty = new(0, 0, 0, 0, default, default);

Expand Down Expand Up @@ -182,5 +176,7 @@ public static RecipeParameters CalculateParameters(RecipeRow row) {

return new RecipeParameters(recipeTime, fuelUsagePerSecondPerBuilding, productivity, warningFlags, activeEffects, modules);
}

internal RecipeParameters WithFlag(WarningFlags additionalFlag) => this with { warningFlags = warningFlags | additionalFlag };
}
}
5 changes: 4 additions & 1 deletion Yafc.UI/Core/ExceptionScreen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using SDL2;
using Serilog;

Expand Down Expand Up @@ -33,7 +34,7 @@ protected internal override void Close() {
exists = false;
}

protected override void BuildContents(ImGui gui) {
protected override Task BuildContents(ImGui gui) {
gui.BuildText(ex.GetType().Name, Font.header);
gui.BuildText(ex.Message, new TextBlockDisplayStyle(Font.subheader, true));
gui.BuildText(ex.StackTrace, TextBlockDisplayStyle.WrappedText);
Expand All @@ -50,6 +51,8 @@ protected override void BuildContents(ImGui gui) {
_ = SDL.SDL_SetClipboardText(ex.Message + "\n\n" + ex.StackTrace);
}
}

return Task.CompletedTask;
}
}
}
7 changes: 4 additions & 3 deletions Yafc.UI/Core/Window.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Numerics;
using System.Threading.Tasks;
using SDL2;
using Serilog;

Expand Down Expand Up @@ -201,12 +202,12 @@ public void ShowDropDown(ImGui targetGui, Rect target, GuiBuilder builder, Paddi
ShowDropDown(simpleDropDown);
}

private void Build(ImGui gui) {
private async void Build(ImGui gui) {
if (closed) {
return;
}

BuildContents(gui);
await BuildContents(gui);
if (dropDown != null) {
dropDown.Build(gui);
if (!dropDown.active) {
Expand All @@ -222,7 +223,7 @@ private void Build(ImGui gui) {
}
}

protected abstract void BuildContents(ImGui gui);
protected abstract Task BuildContents(ImGui gui);
public virtual void Dispose() => rootGui.Dispose();

internal ImGui.DragOverlay GetDragOverlay() => draggingOverlay ??= new ImGui.DragOverlay();
Expand Down
7 changes: 4 additions & 3 deletions Yafc.UI/Core/WindowMain.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using SDL2;
using Serilog;

Expand Down Expand Up @@ -35,12 +36,12 @@ protected void Create(string title, int display, float initialWidth, float initi
base.Create();
}

protected override void BuildContents(ImGui gui) {
BuildContent(gui);
protected override async Task BuildContents(ImGui gui) {
await BuildContent(gui);
gui.SetContextRect(new Rect(default, size));
}

protected abstract void BuildContent(ImGui gui);
protected abstract Task BuildContent(ImGui gui);

protected override void OnRepaint() {
rootGui.Rebuild();
Expand Down
7 changes: 5 additions & 2 deletions Yafc/Windows/AboutScreen.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Yafc.UI;
using System.Threading.Tasks;
using Yafc.UI;

namespace Yafc {
public class AboutScreen : WindowUtility {
public const string Github = "https://github.com/have-fun-was-taken/yafc-ce";

public AboutScreen(Window parent) : base(ImGuiUtils.DefaultScreenPadding) => Create("About YAFC-CE", 50, parent);

protected override void BuildContents(ImGui gui) {
protected override Task BuildContents(ImGui gui) {
gui.allocator = RectAllocator.Center;
gui.BuildText("Yet Another Factorio Calculator", new TextBlockDisplayStyle(Font.header, Alignment: RectAlignment.Middle));
gui.BuildText("(Community Edition)", TextBlockDisplayStyle.Centered);
Expand Down Expand Up @@ -66,6 +67,8 @@ protected override void BuildContents(ImGui gui) {
gui.allocator = RectAllocator.Center;
gui.BuildText("Factorio name, content and materials are trademarks and copyrights of Wube Software");
BuildLink(gui, "https://factorio.com/");

return Task.CompletedTask;
}

private void BuildLink(ImGui gui, string url, string? text = null) {
Expand Down
5 changes: 4 additions & 1 deletion Yafc/Windows/FilesystemScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using SDL2;
using Yafc.UI;

Expand Down Expand Up @@ -42,7 +43,7 @@ public FilesystemScreen(string? header, string description, string button, strin
previousFocus = InputSystem.Instance.SetDefaultKeyboardFocus(this);
}

protected override void BuildContents(ImGui gui) {
protected override Task BuildContents(ImGui gui) {
gui.BuildText(description, TextBlockDisplayStyle.WrappedText);
if (gui.BuildTextInput(location, out string newLocation, null)) {
if (Directory.Exists(newLocation)) {
Expand All @@ -62,6 +63,8 @@ protected override void BuildContents(ImGui gui) {
}
}
}

return Task.CompletedTask;
}

private void BuildSelectButton(ImGui gui) {
Expand Down
14 changes: 9 additions & 5 deletions Yafc/Windows/MainScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,27 @@ private void SetProject(Project project) {
_ = InputSystem.Instance.SetDefaultKeyboardFocus(this);
}

private void ProjectSettingsChanged(bool visualOnly) {
private async void ProjectSettingsChanged(bool visualOnly) {
if (visualOnly) {
return;
}

if (topScreen == null) {
ReRunAnalysis();
await ReRunAnalysis();
}
else {
analysisUpdatePending = true;
}
}

private void ReRunAnalysis() {
private async Task ReRunAnalysis() {
analysisUpdatePending = false;
ErrorCollector collector = new ErrorCollector();
Analysis.ProcessAnalyses(this, project, collector);
project.pages.ForEach(p => p.SetToRecalculate());
Task primary = (activePage?.RunSolveJob() ?? Task.CompletedTask);
await (secondaryPage?.RunSolveJob() ?? Task.CompletedTask);
await primary;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could go with Task.WaitAll here to wait for both jobs. That way if the second one fails, you get thrown out of it instantly.

rootGui.MarkEverythingForRebuild();
if (collector.severity > ErrorSeverity.None) {
ErrorListPanel.Show(collector);
Expand Down Expand Up @@ -205,7 +209,7 @@ public void RebuildProjectView() {
secondaryPageView?.bodyContent.MarkEverythingForRebuild();
}

protected override void BuildContent(ImGui gui) {
protected override async Task BuildContent(ImGui gui) {
if (pseudoScreens.Count > 0) {
var top = pseudoScreens[0];
if (gui.isBuilding) {
Expand All @@ -224,7 +228,7 @@ protected override void BuildContent(ImGui gui) {
_ = InputSystem.Instance.SetDefaultKeyboardFocus(this);
topScreen = null;
if (analysisUpdatePending) {
ReRunAnalysis();
await ReRunAnalysis();
}
}
BuildTabBar(gui);
Expand Down
5 changes: 4 additions & 1 deletion Yafc/Windows/WelcomeScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using SDL2;
using Serilog;
using Yafc.Model;
Expand Down Expand Up @@ -97,7 +98,7 @@ private void BuildError(ImGui gui) {
gui.DrawRectangle(gui.lastRect, SchemeColor.Error);
}

protected override void BuildContents(ImGui gui) {
protected override Task BuildContents(ImGui gui) {
gui.spacing = 1.5f;
gui.BuildText("Yet Another Factorio Calculator", new TextBlockDisplayStyle(Font.header, Alignment: RectAlignment.Middle));
if (loading) {
Expand Down Expand Up @@ -185,6 +186,8 @@ protected override void BuildContents(ImGui gui) {
}
}
}

return Task.CompletedTask;
}

private void ProjectErrorMoreInfo(ImGui gui) {
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Date:
the launch-settings from the most-recently opened project.
Bugfixes:
- Several fixes to the legacy summary page, including a regression in 0.8.1.
- Milestone-locked and inaccessible recipes will be avoided when multiple recipes are available.
Internal changes:
- Add .git-blame-ignore revs. It doesn't work with Visual Studio, but it might be useful in CLI or other IDEs.
- Add the ability for tests to load lua and run tests that need parts of a Yafc project.
Expand Down