Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Automate Powah energizing orb; requires energizing orb below, an
- inventory/AE input/etc above, and a redstone signal that is enabled
- when the orb is empty.
- ]]
- local RUN_EVERY_SEC = 0.5
- local GET_ITEMS_EVERY_ITERS = 4
- local DEBUG_MODE = true
- local Items = {
- IRON = "minecraft:iron_ingot",
- GOLD = "minecraft:gold_ingot",
- EMERALD = "minecraft:emerald",
- DIAMOND = "minecraft:diamond",
- STAR = "minecraft:nether_star",
- RED_BLOCK = "minecraft:redstone_block",
- BLAZING_CRYSTAL_BLOCK = "powah:blazing_crystal_block",
- ENDER_EYE = "minecraft:ender_eye",
- DIELECTRIC_CASING = "powah:dielectric_casing",
- CAPACITOR_BASIC_TINY = "powah:capacitor_basic_tiny",
- BLUE_ICE = "minecraft:blue_ice",
- SNOWBALL = "minecraft:snowball",
- URANINITE_RAW = "powah:uraninite_raw",
- }
- local MAX_SLOT = 16
- -- Add deepslate/stone variants
- local UraniniteVariants = {
- URANINITE_ORE_POOR = "uraninite_ore_poor",
- URANINITE_ORE_DENSE = "uraninite_ore_dense",
- URANINITE_ORE = "uraninite_ore",
- }
- for k, suffix in pairs(UraniniteVariants) do
- Items[k] = "powah:" .. suffix
- Items["DEEPSLATE_" .. k] = "powah:deepslate_" .. suffix
- end
- -- Inputs that aren't just 1 unit of the item, all the ones
- -- that are 1 unit of the item are automatically filled
- local ValidInputs = {
- {
- Items.IRON,
- Items.GOLD,
- },
- {
- Items.STAR,
- Items.RED_BLOCK,
- Items.RED_BLOCK,
- Items.BLAZING_CRYSTAL_BLOCK,
- },
- {
- Items.ENDER_EYE,
- Items.CAPACITOR_BASIC_TINY,
- Items.DIELECTRIC_CASING,
- },
- {
- Items.BLUE_ICE,
- Items.BLUE_ICE,
- },
- }
- local function fillRemainingInputs()
- -- Fill remaining 1 item recipes, as above
- for k, itemId in pairs(Items) do
- local found = false
- for _, recipe in ipairs(ValidInputs) do
- found = includes(recipe, itemId)
- if found then
- break
- end
- end
- if not found then
- ValidInputs[#ValidInputs+1] = {
- itemId
- }
- end
- end
- end
- local function getItemsAndCheck()
- local done = false
- repeat
- local success = turtle.suckUp(64)
- done = not success
- until done
- for slot = 1, MAX_SLOT do
- local data = turtle.getItemDetail(slot)
- if data
- and not includes(Items, data.name) then
- turtle.select(slot)
- turtle.dropUp(64)
- end
- end
- end
- local function nextRecipe()
- local currentRecipe
- local recipeSlots
- for slot = 1, MAX_SLOT do
- local data = turtle.getItemDetail(slot)
- if data then
- if not currentRecipe then
- for _, recipe in ipairs(ValidInputs) do
- if includes(recipe, data.name) then
- -- copy of the recipe, items will be removed as they are done
- currentRecipe = {}
- for _, ingredient in ipairs(recipe) do
- currentRecipe[#currentRecipe+1] = ingredient
- end
- recipeSlots = {}
- print("Detected an item for recipe " .. dump(recipe) .. ", checking if has all...")
- break
- end
- end
- end
- for i = 1, data.count do
- local idx = indexOf(currentRecipe, data.name)
- if idx then
- table.remove(currentRecipe, idx)
- if recipeSlots[slot] then
- recipeSlots[slot] = recipeSlots[slot] + 1
- else
- recipeSlots[slot] = 1
- end
- end
- end
- end
- -- if has recipe and all items for it
- if currentRecipe and #currentRecipe == 0 then
- break
- end
- end
- -- if has recipe and all items for it
- if currentRecipe and #currentRecipe == 0 then
- for slot, amount in pairs(recipeSlots) do
- turtle.select(slot)
- turtle.dropDown(amount)
- end
- print("Done recipe!")
- end
- end
- -- This assumes input that is true when the energizing orb is empty
- -- For example, an inverted comparator out of the orb
- -- for ease of vertical redstone transmission
- local function shouldDoNext()
- for _, side in pairs(redstone.getSides()) do
- if redstone.getInput(side) then
- return true
- end
- end
- return false
- end
- local function main()
- fillRemainingInputs()
- print("Powah automation: starting!")
- local iters = 0
- while true do
- if iters == 0 then
- getItemsAndCheck()
- end
- if shouldDoNext() then
- nextRecipe()
- end
- iters = (iters + 1) % GET_ITEMS_EVERY_ITERS
- ---@diagnostic disable-next-line: undefined-field
- os.sleep(RUN_EVERY_SEC)
- end
- end
- -- library stuff
- function includes(tbl, val)
- for k, v in pairs(tbl) do
- if v == val then
- return true
- end
- end
- return false
- end
- function indexOf(list, val)
- for i, v in ipairs(list) do
- if v == val then
- return i
- end
- end
- end
- -- If the tables have the same items,
- -- with eventually different order
- function matches(tbl1, tbl2)
- local tbl2copy = {}
- for k2, v2 in pairs(tbl2) do
- tbl2copy[k2] = v2
- end
- for k1, v1 in pairs(tbl1) do
- local found = false
- for k2, v2 in pairs(tbl2copy) do
- if v2 == v1 then
- found = true
- tbl2copy[k2] = nil
- break
- end
- end
- if not found then
- return false
- end
- end
- return true
- end
- function dump(o)
- if type(o) == 'table' then
- local s = '{ '
- for k,v in pairs(o) do
- if type(k) ~= 'number' then k = '"'..k..'"' end
- s = s .. '['..k..'] = ' .. dump(v) .. ','
- end
- return s .. '} '
- else
- return tostring(o)
- end
- end
- -- Actually run
- main()
Advertisement
Add Comment
Please, Sign In to add comment