Skip to content

Commit

Permalink
First draft recipescan optimize subrecipes
Browse files Browse the repository at this point in the history
  • Loading branch information
derfloh205 committed Feb 6, 2024
1 parent f0f1c1f commit 2e737e9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Data/Classes/PriceData.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function CraftSim.PriceData:Update()
wipe(self.qualityPriceList)
wipe(self.expectedCostsByQuality)

local useSubRecipes = CraftSimOptions.costOptimizationAutomaticSubRecipeOptimization
local useSubRecipes = self.recipeData.subRecipeCostsEnabled

print("Update PriceData", false, true)
print("using subrecipes: " .. tostring(useSubRecipes))
Expand Down
11 changes: 8 additions & 3 deletions Data/Classes/RecipeData.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function CraftSim.RecipeData:new(recipeID, isRecraft, isWorkOrder, crafterData)
--- might be not necessary
---@type CraftSim.RecipeData[]
self.optimizedSubRecipes = {}
self.subRecipeCostsEnabled = false

if not recipeID then
return -- e.g. when deserializing
Expand Down Expand Up @@ -249,6 +250,10 @@ function CraftSim.RecipeData:new(recipeID, isRecraft, isWorkOrder, crafterData)
end
end

function CraftSim.RecipeData:SetSubRecipeCostsUsage(enabled)
self.subRecipeCostsEnabled = enabled
end

---@param reagentList CraftSim.ReagentListItem[]
function CraftSim.RecipeData:SetReagents(reagentList)
-- go through required reagents and set quantity accordingly
Expand Down Expand Up @@ -467,9 +472,9 @@ function CraftSim.RecipeData:Copy()
copy.resultData = self.resultData:Copy(copy) -- Is this needed or covered by constructor?
-- copy spec data or already handled in constructor?
copy.orderData = self.orderData
copy.crafter = self.crafter
copy.crafterClass = self.crafterClass
copy.crafterRealm = self.crafterRealm
copy.crafterData = self.crafterData
copy.subRecipeCostsEnabled = self.subRecipeCostsEnabled
copy.optimizedSubRecipes = self.optimizedSubRecipes

copy:Update()
return copy
Expand Down
4 changes: 3 additions & 1 deletion Main/CraftSim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ CraftSimOptions = CraftSimOptions or {
recipeScanFilteredExpansions = nil,
recipeScanAltProfessions = nil,
recipeScanImportAllProfessions = false,
recipeScanOptimizeSubRecipes = false,

-- profit calc
customMulticraftConstant = CraftSim.CONST.MULTICRAFT_CONSTANT,
Expand Down Expand Up @@ -682,7 +683,8 @@ function CraftSim.MAIN:TriggerModulesByRecipeType()
end

-- subrecipe optimization
if CraftSimOptions.costOptimizationAutomaticSubRecipeOptimization then
recipeData:SetSubRecipeCostsUsage(CraftSimOptions.costOptimizationAutomaticSubRecipeOptimization)
if recipeData.subRecipeCostsEnabled then
CraftSim.UTIL:StartProfiling("OptimizeSubRecipes")
recipeData:OptimizeSubRecipes()
CraftSim.UTIL:StopProfiling("OptimizeSubRecipes")
Expand Down
2 changes: 1 addition & 1 deletion Modules/CostDetails/Frames.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function CraftSim.COST_DETAILS:UpdateDisplay(recipeData, exportMode)
costDetailsFrame = CraftSim.COST_DETAILS.frame
end

local considerSubRecipes = CraftSimOptions.costOptimizationAutomaticSubRecipeOptimization
local considerSubRecipes = recipeData.subRecipeCostsEnabled

costDetailsFrame.content.craftingCostsValue:SetText(CraftSim.GUTIL:FormatMoney(recipeData.priceData.craftingCosts))

Expand Down
10 changes: 5 additions & 5 deletions Modules/ReagentOptimization/ReagentOptimization.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function CraftSim.REAGENT_OPTIMIZATION:IsCurrentAllocation(recipeData, bestResul
return recipeData.reagentData:EqualsQualityReagents(bestResult.reagents)
end

function CraftSim.REAGENT_OPTIMIZATION:CreateCrumbs(ksItem)
function CraftSim.REAGENT_OPTIMIZATION:CreateCrumbs(ksItem, useSubRecipeCosts)
local inf = math.huge

local j, k, a, b, c, n, w
Expand All @@ -241,11 +241,11 @@ function CraftSim.REAGENT_OPTIMIZATION:CreateCrumbs(ksItem)
end

local q3ItemPrice = CraftSim.PRICEDATA:GetMinBuyoutByItemID(ksItem.reagent.items[3].item:GetItemID(), true, false,
CraftSimOptions.costOptimizationAutomaticSubRecipeOptimization)
useSubRecipeCosts)
local q2ItemPrice = CraftSim.PRICEDATA:GetMinBuyoutByItemID(ksItem.reagent.items[2].item:GetItemID(), true, false,
CraftSimOptions.costOptimizationAutomaticSubRecipeOptimization)
useSubRecipeCosts)
local q1ItemPrice = CraftSim.PRICEDATA:GetMinBuyoutByItemID(ksItem.reagent.items[1].item:GetItemID(), true, false,
CraftSimOptions.costOptimizationAutomaticSubRecipeOptimization)
useSubRecipeCosts)

--print("start crumb creation: " .. ksItem.name)
for k = 0, n, 1 do
Expand Down Expand Up @@ -341,7 +341,7 @@ function CraftSim.REAGENT_OPTIMIZATION:OptimizeReagentAllocation(recipeData)
--print("mWeight[index] / weightGCD -> " .. mWeight[index] .. " / " .. weightGCD .. " = " .. mWeight[index] / weightGCD)

-- fill crumbs
CraftSim.REAGENT_OPTIMIZATION:CreateCrumbs(ksItem)
CraftSim.REAGENT_OPTIMIZATION:CreateCrumbs(ksItem, recipeData.subRecipeCostsEnabled)
ksItems[index] = ksItem
end
CraftSim.UTIL:StopProfiling("KnapsackKsItemCreation")
Expand Down
14 changes: 12 additions & 2 deletions Modules/RecipeScan/Frames.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local CraftSim = select(2, ...)
local GGUI = CraftSim.GGUI
local GUTIL = CraftSim.GUTIL
local L = CraftSim.UTIL:GetLocalizer()
local f = CraftSim.UTIL:GetFormatter()

---@class CraftSim.RECIPE_SCAN
CraftSim.RECIPE_SCAN = CraftSim.RECIPE_SCAN
Expand Down Expand Up @@ -697,6 +698,15 @@ function CraftSim.RECIPE_SCAN.FRAMES:InitScanOptionsTab(scanOptionsTab)
clickCallback = function(_, checked) CraftSimOptions.recipeScanUseInsight = checked end
}

content.optimizeSubRecipes = GGUI.Checkbox {
parent = content, anchorParent = content.useInsightCB.frame, anchorA = "TOP", anchorB = "BOTTOM", offsetY = checkBoxSpacingY,
label = "Optimize Sub Recipes",
tooltip = "If enabled, " .. f.l("CraftSim") .. " also optimizes crafts of cached reagent recipes of scanned recipes and uses their\n" ..
f.bb("expected costs") .. " to calculate the crafting costs for the final product.\n\n" .. f.r("Warning: This might reduce scanning performance"),
initialValue = CraftSimOptions.recipeScanOptimizeSubRecipes,
clickCallback = function(_, checked) CraftSimOptions.recipeScanOptimizeSubRecipes = checked end
}

content.expansionSelector = GGUI.CheckboxSelector {
savedVariablesTable = CraftSimOptions.recipeScanFilteredExpansions,
initialItems = GUTIL:Sort(GUTIL:Map(CraftSim.CONST.EXPANSION_IDS,
Expand All @@ -717,9 +727,9 @@ function CraftSim.RECIPE_SCAN.FRAMES:InitScanOptionsTab(scanOptionsTab)

},
buttonOptions = {
parent = content, anchorParent = content.useInsightCB.frame,
parent = content, anchorParent = content.optimizeSubRecipes.frame,
anchorA = "TOPLEFT", anchorB = "BOTTOMLEFT", offsetY = checkBoxSpacingY,
label = L(CraftSim.CONST.TEXT.RECIPE_SCAN_EXPANSION_FILTER_BUTTON), offsetX = 20,
label = L(CraftSim.CONST.TEXT.RECIPE_SCAN_EXPANSION_FILTER_BUTTON), offsetX = 25,
adjustWidth = true, sizeX = 20,
},
}
Expand Down
8 changes: 8 additions & 0 deletions Modules/RecipeScan/RecipeScan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ function CraftSim.RECIPE_SCAN:StartScan(row)
return
end

-- if optimizing subrecipes
if CraftSimOptions.recipeScanOptimizeSubRecipes then
printS("Optimizing SubRecipes..")
recipeData:SetSubRecipeCostsUsage(true)
recipeData:OptimizeSubRecipes()
printS("optimizedRecipes: " .. tostring(#recipeData.optimizedSubRecipes))
end

--optimize top gear first cause optimized reagents might change depending on the gear
if CraftSimOptions.recipeScanOptimizeProfessionTools then
printS("Optimizing Gear...")
Expand Down

0 comments on commit 2e737e9

Please sign in to comment.