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

feat: Add preferences for "change-recipe-productivity" technology unlocks #334

Merged
merged 7 commits into from
Oct 31, 2024
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
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