Skip to content

Commit

Permalink
feat: Add preferences for "change-recipe-productivity" technology unl…
Browse files Browse the repository at this point in the history
…ocks (#334)

I was playing over the weekend and needed this to plan out my factory,
based on the discussion in #331 this would be option 3
I had to move the "done" button to the top of the screen to avoid it
overlapping with the levels, and I couldn't quite figure out how to make
a scrollable box

I'm happy to split the PR into the parsing/calculating change vs the UI
change if you think there's a nicer UI to be had :)

<details><summary>Screenshots</summary>
<p>

Preferences screen:
(I have some WIP commits that fix the icon rendering too, I'll clean
them up soon)

![image](https://github.com/user-attachments/assets/ec4e7295-c7c6-4295-abab-db90390db1ad)


No tech unlocked:

![image](https://github.com/user-attachments/assets/c3190395-1ff3-4c98-8305-55702ae64cad)

Level 10 plastic (+100% productivity) unlocked:

![image](https://github.com/user-attachments/assets/38c75200-f59c-4011-8e19-7e1cb037ae82)

</p>
</details>
  • Loading branch information
shpaass authored Oct 31, 2024
2 parents fbf8f31 + 22df67f commit bb37a39
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Yafc.Model/Data/DataClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public class Recipe : RecipeOrTechnology {
public AllowedEffects allowedEffects { get; internal set; }
public string[]? allowedModuleCategories { get; internal set; }
public Technology[] technologyUnlock { get; internal set; } = [];
public Dictionary<Technology, float> technologyProductivity { get; internal set; } = [];
public bool HasIngredientVariants() {
foreach (var ingredient in ingredients) {
if (ingredient.variants != null) {
Expand Down Expand Up @@ -522,7 +523,8 @@ public class EntityContainer : Entity {
public class Technology : RecipeOrTechnology { // Technology is very similar to recipe
public float count { get; internal set; } // TODO support formula count
public Technology[] prerequisites { get; internal set; } = [];
public Recipe[] unlockRecipes { get; internal set; } = [];
public List<Recipe> unlockRecipes { get; internal set; } = [];
public Dictionary<Recipe, float> changeRecipeProductivity { get; internal set; } = [];
internal override FactorioObjectSortOrder sortingOrder => FactorioObjectSortOrder.Technologies;
public override string type => "Technology";

Expand Down
1 change: 1 addition & 0 deletions Yafc.Model/Model/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public class ProjectSettings(Project project) : ModelObject<Project>(project) {
public float miningProductivity { get; set; }
public float researchSpeedBonus { get; set; }
public float researchProductivity { get; set; }
public Dictionary<Technology, int> productivityTechnologyLevels { get; } = [];
public int reactorSizeX { get; set; } = 2;
public int reactorSizeY { get; set; } = 2;
public float PollutionCostModifier { get; set; } = 0;
Expand Down
11 changes: 11 additions & 0 deletions Yafc.Model/Model/RecipeParameters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Yafc.Model;
Expand Down Expand Up @@ -125,6 +126,16 @@ public static RecipeParameters CalculateParameters(RecipeRow row) {
else if (recipe is Technology) {
productivity += Project.current.settings.researchProductivity;
}
else if (recipe is Recipe actualRecipe) {
Dictionary<Technology, int> levels = Project.current.settings.productivityTechnologyLevels;
foreach ((Technology productivityTechnology, float changePerLevel) in actualRecipe.technologyProductivity) {
if (!levels.TryGetValue(productivityTechnology, out int productivityTechLevel)) {
continue;
}

productivity += changePerLevel * productivityTechLevel;
}
}

if (entity is EntityReactor reactor && reactor.reactorNeighborBonus > 0f) {
productivity += reactor.reactorNeighborBonus * Project.current.settings.GetReactorBonusMultiplier();
Expand Down
29 changes: 26 additions & 3 deletions Yafc.Parser/Data/FactorioDataDeserializer_RecipeAndTechnology.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,32 @@ private void LoadTechnologyData(Technology technology, LuaTable table, ErrorColl
}

if (table.Get("effects", out LuaTable? modifiers)) {
technology.unlockRecipes = modifiers.ArrayElements<LuaTable>()
.Select(x => x.Get("type", out string? type) && type == "unlock-recipe" && GetRef<Recipe>(x, "recipe", out var recipe) ? recipe : null).WhereNotNull()
.ToArray();
foreach (LuaTable modifier in modifiers.ArrayElements<LuaTable>()) {
switch (modifier.Get("type", "")) {
case "unlock-recipe": {
if (!GetRef<Recipe>(modifier, "recipe", out var recipe)) {
continue;
}

technology.unlockRecipes.Add(recipe);

break;
}

case "change-recipe-productivity": {
if (!GetRef<Recipe>(modifier, "recipe", out var recipe)) {
continue;
}

float change = modifier.Get("change", 0f);

technology.changeRecipeProductivity.Add(recipe, change);
recipe.technologyProductivity.Add(technology, change);

break;
}
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Yafc.UI/ImGui/ImGuiLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void StartNextAllocatePass(bool alsoDraw) {

public void Dispose() {
gui.enableDrawing = initialDrawState;
gui.state.top = maximumBottom;
gui.state.top = Math.Max(gui.state.top, maximumBottom);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Yafc/Widgets/ObjectTooltip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ private void BuildTechnology(Technology technology, ImGui gui) {
}
}

if (technology.unlockRecipes.Length > 0) {
if (technology.unlockRecipes.Count > 0) {
BuildSubHeader(gui, "Unlocks recipes");
using (gui.EnterGroup(contentPadding)) {
BuildIconRow(gui, technology.unlockRecipes, 2);
Expand Down
16 changes: 16 additions & 0 deletions Yafc/Windows/PreferencesScreen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Yafc.Model;
using Yafc.UI;

Expand Down Expand Up @@ -72,6 +74,20 @@ private static void DrawProgression(ImGui gui) {
Project.current.settings.RecordUndo().researchProductivity = amount.Value;
}
}

IEnumerable<Technology> productivityTech = Database.technologies.all
.Where(x => x.changeRecipeProductivity.Count != 0)
.OrderBy(x => x.locName);
foreach (var tech in productivityTech) {
using (gui.EnterRow()) {
gui.BuildFactorioObjectButton(tech, ButtonDisplayStyle.Default);
gui.BuildText($"{tech.locName} Level: ", topOffset: 0.5f);
int currentLevel = Project.current.settings.productivityTechnologyLevels.GetValueOrDefault(tech, 0);
if (gui.BuildIntegerInput(currentLevel, out int newLevel) && newLevel >= 0) {
Project.current.settings.RecordUndo().productivityTechnologyLevels[tech] = newLevel;
}
}
}
}

private static void DrawGeneral(ImGui gui) {
Expand Down

0 comments on commit bb37a39

Please sign in to comment.