Skip to content

Commit

Permalink
Add a checkbox that uses net production instead of gross production f…
Browse files Browse the repository at this point in the history
…or determining which recipes to display.

For example, with the box checked, Kovarex enrichment will display as producing only U-235, and as consuming only U-238.
  • Loading branch information
DaleStan committed May 22, 2024
1 parent 4e31251 commit 10abaa5
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CommandLineToolExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void Main(string[] args) {
// Empty project path loads default project (with one empty page).
// Project is irrelevant if you just need data, but you need it to perform sheet calculations
// Set to not render any icons
project = FactorioDataSource.Parse(factorioPath, "", "", false, new ConsoleProgressReport(), errorCollector, "en", false);
project = FactorioDataSource.Parse(factorioPath, "", "", false, false, new ConsoleProgressReport(), errorCollector, "en", false);
}
catch (Exception ex) {
// Critical errors that make project un-loadable will be thrown as exceptions
Expand Down
1 change: 1 addition & 0 deletions Yafc.Model/Data/DataUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static Bits GetMilestoneOrder(FactorioId id) {
public static string dataPath { get; internal set; }
public static string modsPath { get; internal set; }
public static bool expensiveRecipes { get; internal set; }
public static bool netProduction { get; internal set; }
public static string[] allMods { get; internal set; }
public static Icon NoFuelIcon { get; internal set; }
public static Icon WarningIcon { get; internal set; }
Expand Down
4 changes: 2 additions & 2 deletions Yafc.Parser/Data/FactorioDataDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static void AddTemperatureToFluidIcon(Fluid fluid) {
];
}

public Project LoadData(string projectPath, LuaTable data, LuaTable prototypes, IProgress<(string, string)> progress, ErrorCollector errorCollector, bool renderIcons) {
public Project LoadData(string projectPath, LuaTable data, LuaTable prototypes, bool netProduction, IProgress<(string, string)> progress, ErrorCollector errorCollector, bool renderIcons) {
progress.Report(("Loading", "Loading items"));
raw = (LuaTable)data["raw"];
foreach (object prototypeName in ((LuaTable)prototypes["item"]).ObjectElements.Keys) {
Expand Down Expand Up @@ -127,7 +127,7 @@ Func<Item, bool> AllowedModulesFilter(Recipe key) {
var iconRenderTask = renderIcons ? Task.Run(RenderIcons) : Task.CompletedTask;
UpdateRecipeIngredientFluids();
UpdateRecipeCatalysts();
CalculateMaps();
CalculateMaps(netProduction);
ExportBuiltData();
progress.Report(("Post-processing", "Calculating dependencies"));
Dependencies.Calculate();
Expand Down
15 changes: 10 additions & 5 deletions Yafc.Parser/Data/FactorioDataDeserializer_Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private bool IsBarrelingRecipe(Recipe barreling, Recipe unbarreling) {
return true;
}

private void CalculateMaps() {
private void CalculateMaps(bool netProduction) {
DataBucket<Goods, Recipe> itemUsages = new DataBucket<Goods, Recipe>();
DataBucket<Goods, Recipe> itemProduction = new DataBucket<Goods, Recipe>();
DataBucket<Goods, FactorioObject> miscSources = new DataBucket<Goods, FactorioObject>();
Expand All @@ -221,16 +221,21 @@ private void CalculateMaps() {
case Recipe recipe:
allRecipes.Add(recipe);
foreach (var product in recipe.products) {
if (product.amount > 0) {
float inputAmount = netProduction ? (recipe.ingredients.FirstOrDefault(i => i.goods == product.goods)?.amount ?? 0) : 0;
float outputAmount = ((IFactorioObjectWrapper)product).amount;
if (outputAmount > inputAmount)
itemProduction.Add(product.goods, recipe);
}
}

foreach (var ingredient in recipe.ingredients) {
if (ingredient.variants == null) {
float inputAmount = ingredient.amount;
IFactorioObjectWrapper outputData = recipe.products.FirstOrDefault(p => p.goods == ingredient.goods || p.goods == ingredient.variants?[0]);
float outputAmount = netProduction ? (outputData?.amount ?? 0) : 0;

if (ingredient.variants == null && inputAmount > outputAmount) {
itemUsages.Add(ingredient.goods, recipe);
}
else {
else if (ingredient.variants != null) {
ingredient.goods = ingredient.variants[0];
foreach (var variant in ingredient.variants) {
itemUsages.Add(variant, recipe);
Expand Down
5 changes: 3 additions & 2 deletions Yafc.Parser/FactorioDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static void FindMods(string directory, IProgress<(string, string)> progr
}
}

public static Project Parse(string factorioPath, string modPath, string projectPath, bool expensive, IProgress<(string, string)> progress, ErrorCollector errorCollector, string locale, bool renderIcons = true) {
public static Project Parse(string factorioPath, string modPath, string projectPath, bool expensive, bool netProduction, IProgress<(string, string)> progress, ErrorCollector errorCollector, string locale, bool renderIcons = true) {
LuaContext dataContext = null;
try {
currentLoadingMod = null;
Expand Down Expand Up @@ -257,6 +257,7 @@ public static Project Parse(string factorioPath, string modPath, string projectP
DataUtils.dataPath = factorioPath;
DataUtils.modsPath = modPath;
DataUtils.expensiveRecipes = expensive;
DataUtils.netProduction = netProduction;


dataContext = new LuaContext();
Expand All @@ -283,7 +284,7 @@ public static Project Parse(string factorioPath, string modPath, string projectP
currentLoadingMod = null;

FactorioDataDeserializer deserializer = new FactorioDataDeserializer(expensive, factorioVersion ?? defaultFactorioVersion);
var project = deserializer.LoadData(projectPath, dataContext.data, dataContext.defines["prototypes"] as LuaTable, progress, errorCollector, renderIcons);
var project = deserializer.LoadData(projectPath, dataContext.data, dataContext.defines["prototypes"] as LuaTable, netProduction, progress, errorCollector, renderIcons);
Console.WriteLine("Completed!");
progress.Report(("Completed!", ""));
return project;
Expand Down
5 changes: 3 additions & 2 deletions Yafc/Utils/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void Save() {
public string language { get; set; } = "en";
public string overrideFont { get; set; }

public void AddProject(string path, string dataPath, string modsPath, bool expensiveRecipes) {
public void AddProject(string path, string dataPath, string modsPath, bool expensiveRecipes, bool netProduction) {
recentProjects = recentProjects.Where(x => string.Compare(path, x.path, StringComparison.InvariantCultureIgnoreCase) != 0)
.Prepend(new ProjectDefinition { path = path, modsPath = modsPath, dataPath = dataPath, expensive = expensiveRecipes }).ToArray();
.Prepend(new ProjectDefinition { path = path, modsPath = modsPath, dataPath = dataPath, expensive = expensiveRecipes, netProduction = netProduction }).ToArray();
Save();
}
}
Expand All @@ -58,5 +58,6 @@ public class ProjectDefinition {
public string dataPath { get; set; }
public string modsPath { get; set; }
public bool expensive { get; set; }
public bool netProduction { get; set; }
}
}
2 changes: 1 addition & 1 deletion Yafc/Windows/MainScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ private async Task<bool> SaveProjectAs() {
FilesystemScreen.Mode.SelectOrCreateFile, "project", this, null, "yafc");
if (path != null) {
project.Save(path);
Preferences.Instance.AddProject(path, DataUtils.dataPath, DataUtils.modsPath, DataUtils.expensiveRecipes);
Preferences.Instance.AddProject(path, DataUtils.dataPath, DataUtils.modsPath, DataUtils.expensiveRecipes, DataUtils.netProduction);
return true;
}

Expand Down
11 changes: 9 additions & 2 deletions Yafc/Windows/WelcomeScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class WelcomeScreen : WindowUtility, IProgress<(string, string)> {
private string currentLoad1, currentLoad2;
private string path = "", dataPath = "", modsPath = "";
private bool expensive;
private bool netProduction;
private string createText;
private bool canCreate;
private readonly ScrollArea errorScroll;
Expand Down Expand Up @@ -137,6 +138,10 @@ protected override void BuildContents(ImGui gui) {
gui.BuildText("In-game objects language:");
}

using (gui.EnterRow()) {
gui.BuildCheckBox("Use net production/consumption when analyzing recipes", netProduction, out netProduction);
}

using (gui.EnterRow()) {
if (Preferences.Instance.recentProjects.Length > 1) {
if (gui.BuildButton("Recent projects", SchemeColor.Grey)) {
Expand Down Expand Up @@ -281,12 +286,14 @@ private void BuildPathSelect(ImGui gui, ref string path, string description, str
private void SetProject(ProjectDefinition project) {
if (project != null) {
expensive = project.expensive;
netProduction = project.netProduction;
modsPath = project.modsPath ?? "";
path = project.path ?? "";
dataPath = project.dataPath ?? "";
}
else {
expensive = false;
netProduction = false;
modsPath = "";
path = "";
dataPath = "";
Expand All @@ -305,7 +312,7 @@ private void SetProject(ProjectDefinition project) {
private async void LoadProject() {
try {
var (dataPath, modsPath, projectPath, expensiveRecipes) = (this.dataPath, this.modsPath, path, expensive);
Preferences.Instance.AddProject(projectPath, dataPath, modsPath, expensiveRecipes);
Preferences.Instance.AddProject(projectPath, dataPath, modsPath, expensiveRecipes, netProduction);
Preferences.Instance.Save();
tip = tips.Length > 0 ? tips[DataUtils.random.Next(tips.Length)] : "";

Expand All @@ -314,7 +321,7 @@ private async void LoadProject() {

await Ui.ExitMainThread();
ErrorCollector collector = new ErrorCollector();
var project = FactorioDataSource.Parse(dataPath, modsPath, projectPath, expensiveRecipes, this, collector, Preferences.Instance.language);
var project = FactorioDataSource.Parse(dataPath, modsPath, projectPath, expensiveRecipes, netProduction, this, collector, Preferences.Instance.language);
await Ui.EnterMainThread();
Console.WriteLine("Opening main screen");
_ = new MainScreen(displayIndex, project);
Expand Down
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Date: soon
- Add the option to specify a number of belts of production, and to specify per-second/minute/hour
production regardless of the current display setting.
- When searching in the page list, allow searching in page contents as well as in page names.
- Allow the user to select whether catalysts should be considered produced and consumed by the recipes that
use them. (e.g. Does coal liquefaction consume heavy oil?)
Changes:
- Add a help message and proper handling for command line arguments
- Removed default pollution cost from calculation. Added a setting to customize pollution cost.
Expand Down

0 comments on commit 10abaa5

Please sign in to comment.