From 284e4c4df3398b40f2038946339d508cffbf1b90 Mon Sep 17 00:00:00 2001 From: GHXX Date: Sun, 4 Oct 2026 20:54:13 +0200 Subject: [PATCH] conditional crafting and more recipes --- genericAutocraft.lua | 157 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 148 insertions(+), 9 deletions(-) diff --git a/genericAutocraft.lua b/genericAutocraft.lua index a5be934..3931eaa 100644 --- a/genericAutocraft.lua +++ b/genericAutocraft.lua @@ -10,13 +10,38 @@ local kernel = require("kernel") local crafter = agc("digitalCrafter") local interface = agc("adapter") +local k = 1000 + +local currentCraftCondition = nil local recipesToAutocraft = {} local function addRecipeDirect(recipe) assert(type(recipe) == "table", "recipe was not table but "..type(recipe)) assert(recipe.ingredients, "key ingredients was not contained in recipe") assert(recipe.result, "key result was not contained in recipe") assert(recipe.resultCount, "key resultCount was not contained in recipe") - table.insert(recipesToAutocraft, recipe) + table.insert(recipesToAutocraft, { + recipe=recipe, + condition=currentCraftCondition + }) +end + +local function withCraftCondition(condition, innerBlock) + assert(type(condition) == "function", "condition must be func") + assert(type(innerBlock) == "function", "innerBlock must be func") + currentCraftCondition = condition + innerBlock() + currentCraftCondition = nil +end + +local function pred_itemGreaterThan(itemName, amount) + return function() + return interface:getCount(itemName) > amount + end +end +local function pred_itemLessThan(itemName, amount) + return function() + return interface:getCount(itemName) < amount + end end local function addSingleRecipeOfItem(name) -- ensures that there is exactly one recipe to use @@ -26,6 +51,76 @@ local function addSingleRecipeOfItem(name) -- ensures that there is exactly one addRecipeDirect(recipe) end +local function addSingleRecipeForItemOfIngredients(target, ingredients) -- ensures that there is exactly one recipe to use + local recipesForItem = crafter:searchRecipesWithResult(target) + local filteredRecipes = {} + for index, recipe in ipairs(recipesForItem) do + local recipeIngredients = recipe.ingredients + local currentRecipeIsMatch = true + local referencedFilterIngredientIdxs = {} + for i = 1, 9 do -- make sure all listed ingredients are mentioned in 'ingredients' + if recipeIngredients[i] ~= nil then + local possibleIngredientOptions = table.concat(recipeIngredients[i],";") + local anyIngredientOptionIsListedForEachSlot = false + for j = 1, #ingredients do + if possibleIngredientOptions:find(ingredients[j], 0, true) then + --recipeIngredients[i] = ingredients[j] + referencedFilterIngredientIdxs[j] = true + anyIngredientOptionIsListedForEachSlot = true + break + end + end + + if not anyIngredientOptionIsListedForEachSlot then + currentRecipeIsMatch = false + end + end + end + + if currentRecipeIsMatch then + -- make sure there are no listed ingredients that are not mentioned in 'ingredients' + for j = 1, #ingredients do + if not referencedFilterIngredientIdxs[j] then + currentRecipeIsMatch = false + end + end + end + + if currentRecipeIsMatch then + table.insert(filteredRecipes, recipe) + end + end + + assert(#filteredRecipes == 1, ("somehow there was not exactly one recipe for %s from ingredients %s"):format(target, table.concat(ingredients, " "))) + local recipe = filteredRecipes[1] + for i = 1, #recipe.ingredients do + print("ingredient: ",recipe.ingredients[i][1]) + end + addRecipeDirect(recipe) +end + +local function addSingleRecipeWithExactlyNIngredients(name, ingredientCount, justTakeTheFirstRecipeIfTheresMultiple) -- ensures that there is exactly one recipe to use + local recipesForItem = crafter:searchRecipesWithResult(name) + local filtered = {} + for _, recipe in ipairs(recipesForItem) do + local recipeIngredientCount = 0 + for i = 1, 9 do + if recipe.ingredients[i] then + recipeIngredientCount = recipeIngredientCount + 1 + end + end + if recipeIngredientCount == ingredientCount then + table.insert(filtered, recipe) + end + end + if justTakeTheFirstRecipeIfTheresMultiple and #filtered > 1 then + filtered = {filtered[1]} + end + assert(#filtered == 1, "somehow there was not exactly one recipe for "..tostring(name)) + local recipe = filtered[1] + addRecipeDirect(recipe) +end + local function addSingleRecipeForAllCsv(csv, prefixOrNil) for match in csv:gsub("%s",""):gmatch("[^,]+") do @@ -39,8 +134,45 @@ local function addSingleRecipeThatUsesItem(itemToConsume) local recipe = recipesForItem[1] addRecipeDirect(recipe) end -addSingleRecipeThatUsesItem("mysticalagriculture:sulfur_essence") +addSingleRecipeThatUsesItem("mysticalagriculture:sulfur_essence") addSingleRecipeThatUsesItem("mysticalagriculture:draconium_essence") +addSingleRecipeThatUsesItem("mysticalagriculture:neutronium_essence") +addSingleRecipeThatUsesItem("mysticalagriculture:awakened_draconium_essence") +addSingleRecipeWithExactlyNIngredients("draconicevolution:awakened_draconium_ingot", 9) +addSingleRecipeWithExactlyNIngredients("avaritia:neutronium_ingot", 9) +addSingleRecipeWithExactlyNIngredients("minecraft:copper_ingot", 9, true) +addSingleRecipeWithExactlyNIngredients("minecraft:amethyst_shard", 8) + +withCraftCondition(pred_itemGreaterThan("minecraft:gold_nugget",10*k), function() + addSingleRecipeWithExactlyNIngredients("minecraft:gold_ingot", 9) +end) + +withCraftCondition(pred_itemLessThan("minecraft:oak_planks",256), function() + addSingleRecipeWithExactlyNIngredients("minecraft:oak_planks", 1) +end) + +for resourceName in ("copper,iron,lapis,redstone,diamond,emerald,amethyst,netherite"):gmatch("[^,]+") do + local resourceItem = resourceName + if resourceName == "copper" or resourceName == "iron" or resourceName == "gold" or resourceName == "netherite" then + resourceItem = resourceName .. "_ingot" + elseif resourceName == "lapis" then + resourceItem = resourceName .. "_lazuli" + elseif resourceName == "amethyst" then + resourceItem = resourceName .. "_shard" + end + + local minimumCount = 50*k + + if resourceName == "amethyst" or resourceName == "netherite" then + minimumCount = 1 * k + elseif resourceName == "diamond" or resourceName == "emerald" then + minimumCount = 10 * k + end + withCraftCondition(pred_itemGreaterThan(("minecraft:%s"):format(resourceItem),minimumCount), function() addSingleRecipeOfItem(("minecraft:%s_block"):format(resourceName)) end) +end + + + -- to be prefixed with nuclearcraft:fuel_ local possibleFuelsRaw = --"leu-235,len-236,hen-236,hecf-249,hecm-245,mix-241,mix-239,lep-241,hep-241,heu-233,lecm-245,leu-233,lecf-249,hecm-243,lep-239,hep-239,lecm-243,heu-235,lea-242,tbu,hea_242,lecf_251,heb_248,leb_248" @@ -59,6 +191,10 @@ addSingleRecipeForAllCsv([[zinc_block]], "create:raw_") addSingleRecipeForAllCsv([[aluminum_block,platinum_block,iridium_block]], "alltheores:raw_") addSingleRecipeForAllCsv([[tin_block,lead_block,silver_block,nickel_block]], "thermal:raw_") +withCraftCondition(pred_itemGreaterThan("minecraft:netherite_scrap",512), function() + addSingleRecipeForItemOfIngredients("minecraft:netherite_ingot", {"minecraft:netherite_scrap", "minecraft:gold"}) +end) + print(("%s Recipes loaded."):format(#recipesToAutocraft)) local function safeCraft(...) @@ -122,7 +258,7 @@ local function tryCraftRecipeAsOftenAsPossible(recipe) error(("We somehow dont have enough to craft %s. Is our math wrong?"):format(resultItemName)) else print(("Time to craft %dx %s."):format(recipeCraftCount, resultItemName)) - --crafter:dumpInventoryUp() + crafter:dumpInventory() for itemName, count in pairs(ingredientsStacked) do local remainingCount = recipeCraftCount * count while remainingCount > 0 do @@ -142,14 +278,17 @@ while true do print("New iteration started.") skipSleep = false - for _, recipe in ipairs(recipesToAutocraft) do + for _, packedRecipe in ipairs(recipesToAutocraft) do + local recipe = packedRecipe.recipe local maxCraft = (recipe.result:find("fuel") and 1 or 10) -- craft 10 stacks at once to speed things up, unless this is a fuel recipe for _ = 1, maxCraft do - local success = tryCraftRecipeAsOftenAsPossible(recipe) - if success then - skipSleep = true - else - break + if packedRecipe.condition == nil or packedRecipe.condition() then + local success = tryCraftRecipeAsOftenAsPossible(packedRecipe.recipe) + if success then + skipSleep = true + else + break + end end end end