Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Interface for Applied Energistics
- -- place a Turtle next to a ME Interface,
- -- place a Trash Can next to the ME Interface
- -- all configuration is done in the application
- -- the Turtle can craft items up to a limit
- -- or void items down to a limit
- -- fuzzy void works with vanilla armor and potions etc
- -- ENTER for options, SPACE to stop page, ANY key for next page
- -- released into the public domain
- --
- print("booting...")
- sleep(1)
- print("initializing...")
- --todo
- --testing batch program, display_names, and new crafting loop (also when missing items)
- --add 4color render to header line etc
- --sometimes startup crashes on world load and needs to be restart manually, even afer sleeping
- --a seperate table could be indexed by integer order_num, to accelerate searches and maybe display during loop
- --a crafty turtle could craft compressed cobble or basically anything, if i knew the export direction to turtle
- saveFile = "meautosettings"
- fuzzy_list = {}
- craft_list = {}
- void_list = {}
- cur_crafting_index = 0
- reserve_cpus = 0
- width,height = term.getSize()
- function options()
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("1: Auto-Craft an Item")
- print("2: Void an Item")
- print("3: Fuzzy Void an Item")
- print("4: Remove an Order #")
- print("5: Change Interface Sides")
- print("6: Back to Main Menu")
- print("")
- print("Enter your choice: ")
- choice = tonumber(io.read())
- if choice and choice >= 1 and choice <= 6 then
- break
- end
- end
- if choice == 6 then
- return
- end
- if choice == 1 then
- term.clear()
- term.setCursorPos(1,1)
- print("Place item to be crafted in slot 1")
- print("Press ENTER to continue")
- io.read()
- item_list = {}
- for slot = 1, 16 do
- d = turtle.getItemDetail(1)
- if d then
- table.insert(item_list, d)
- end
- end
- if #item_list == 0 then
- return
- end
- print("Enter amount to auto-craft")
- amt = tonumber(io.read())
- print("Enter batch size to craft (with available memory and materials)")
- batch = tonumber(io.read())
- while true do
- for k,v in pairs(item_list) do
- print("Auto-Craft " .. amt .. " " .. v.name .. ":" .. v.damage .. " with batchsize " .. batch)
- end
- print("Is this correct? (y/n)")
- correct = io.read()
- if correct == "y" or correct == "n" then
- break
- end
- end
- if correct == "n" then
- return
- end
- for k,v in pairs(item_list) do
- longname = v.name .. ":" .. v.damage
- craft_list[longname] = {}
- craft_list[longname].id = v.name
- craft_list[longname].dmg = v.damage
- craft_list[longname].qty = amt
- craft_list[longname].batch = batch
- craft_list[longname].longname = longname
- craft_list[longname].display_name = ""
- end
- return
- end
- if choice == 2 then
- term.clear()
- term.setCursorPos(1,1)
- print("Place item to be voided in slot 1")
- print("Press ENTER to continue")
- io.read()
- item_list = {}
- for slot = 1, 16 do
- d = turtle.getItemDetail(1)
- if d then
- table.insert(item_list, d)
- end
- end
- if #item_list == 0 then
- return
- end
- print("Enter amount to keep before voiding")
- amt = tonumber(io.read())
- while true do
- for k,v in pairs(item_list) do
- print("Void " .. v.name .. ":" .. v.damage .. " when qty reaches " .. amt)
- end
- print("Is this correct? (y/n)")
- correct = io.read()
- if correct == "y" or correct == "n" then
- break
- end
- end
- if correct == "n" then
- return
- end
- for k,v in pairs(item_list) do
- longname = v.name .. ":" .. v.damage
- void_list[longname] = {}
- void_list[longname].id = v.name
- void_list[longname].dmg = v.damage
- void_list[longname].qty = amt
- void_list[longname].longname = longname
- void_list[longname].display_name = ""
- end
- return
- end
- if choice == 3 then
- term.clear()
- term.setCursorPos(1,1)
- print("Place item to be fuzzy voided in slot 1")
- print("Press ENTER to continue")
- io.read()
- item_list = {}
- for slot = 1, 16 do
- d = turtle.getItemDetail(1)
- if d then
- table.insert(item_list, d)
- end
- end
- if #item_list == 0 then
- return
- end
- print("Enter amount to keep before voiding")
- amt = tonumber(io.read())
- while true do
- for k,v in pairs(item_list) do
- print("Fuzzy Void " .. v.name .. " when qty reaches " .. amt)
- end
- print("Is this correct? (y/n)")
- correct = io.read()
- if correct == "y" or correct == "n" then
- break
- end
- end
- if correct == "n" then
- return
- end
- for k,v in pairs(item_list) do
- fuzzy_list[v.name] = {}
- fuzzy_list[v.name].id = v.name
- fuzzy_list[v.name].qty = amt
- fuzzy_list[v.name].display_name = ""
- end
- return
- end
- if choice == 4 then
- term.clear()
- term.setCursorPos(1,1)
- print("Enter Order # to remove")
- ordernum = tonumber(io.read())
- index = 1
- for k,v in pairs(craft_list) do
- if index == ordernum then
- while true do
- print("Remove this Item:")
- print("Auto-Craft " .. v.qty .. " " .. v.id .. ":" .. v.dmg .. " with batchsize " .. v.batch)
- print("Is this correct? (y/n)")
- correct = io.read()
- if correct == "y" or correct == "n" then
- break
- end
- end
- if correct == "n" then
- return
- end
- craft_list[v.longname] = nil
- return
- end
- index = index + 1
- end
- for k,v in pairs(void_list) do
- if index == ordernum then
- while true do
- print("Remove this Item:")
- print("Void " .. v.id .. ":" .. v.dmg .. " when qty reaches " .. v.qty)
- print("Is this correct? (y/n)")
- correct = io.read()
- if correct == "y" or correct == "n" then
- break
- end
- end
- if correct == "n" then
- return
- end
- void_list[v.longname] = nil
- return
- end
- index = index + 1
- end
- for k,v in pairs(fuzzy_list) do
- if index == ordernum then
- while true do
- print("Remove this Item:")
- print("Fuzzy Void " .. v.id .. " when qty reaches " .. v.qty)
- print("Is this correct? (y/n)")
- correct = io.read()
- if correct == "y" or correct == "n" then
- break
- end
- end
- if correct == "n" then
- return
- end
- fuzzy_list[v.id] = nil
- return
- end
- index = index + 1
- end
- print("Order # not found")
- io.read()
- return
- end
- if choice == 5 then
- configOptions()
- return
- end
- end
- function configOptions()
- while true do
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("Enter the side the ME Interface is on, from the turtle")
- print("")
- print("front, back, left, right")
- print("top, or bottom")
- print("")
- w = tostring(io.read())
- if w == "front" or w == "back" or w == "left" or w == "right" or w == "top" or w == "bottom" then
- break
- end
- end
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("Enter the direction from ME Interface to the trashcan")
- print("")
- print("north, south, east, west, up, or down")
- print("")
- e = tostring(io.read())
- if e == "north" or e == "south" or e == "east" or e == "west" or e == "up" or e == "down" then
- break
- end
- end
- i = peripheral.wrap(w)
- c = false
- if i and i.canExport then
- c = i.canExport(e)
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Interface Side: " .. w)
- term.setCursorPos(width - 9, 1)
- if i ~= nil and i.canExport then
- print(" FOUND")
- else
- print("NOT FOUND")
- end
- term.setCursorPos(1,2)
- print("Trash Direction: " .. e)
- term.setCursorPos(width - 9, 2)
- if c == true then
- print(" FOUND")
- else
- print("NOT FOUND")
- end
- print("")
- while true do
- print("Is this correct? (y/n)")
- correct = io.read()
- if correct == "y" or correct == "n" then
- break
- end
- end
- if correct == "y" then
- interface_wrap_side = w
- interface_export_dir = e
- interface = peripheral.wrap(interface_wrap_side)
- break
- end
- end
- end
- print_line = 1
- function printScreen()
- term.clear()
- term.setCursorPos(1,1)
- print("Order# type Item ID:Dmg cur max")
- lines_printed = 0
- lines_to_print = height - 2
- index = 1
- for k,v in pairs(craft_list) do
- if lines_printed < lines_to_print and index == print_line then
- term.setCursorPos(1,2+lines_printed)
- if v.display_name == "" then
- term.write(index .. " craft " .. v.longname)
- else
- term.write(index .. " craft " .. v.display_name)
- end
- right = " " .. v.cur_qty .. " " .. v.qty
- term.setCursorPos(width + 1 - #right, 2+lines_printed)
- term.write(right)
- print_line = print_line + 1
- lines_printed = lines_printed + 1
- end
- index = index + 1
- end
- for k,v in pairs(void_list) do
- if lines_printed < lines_to_print and index == print_line then
- term.setCursorPos(1,2+lines_printed)
- if v.display_name == "" then
- term.write(index .. " void " .. v.longname)
- else
- term.write(index .. " void " .. v.display_name)
- end
- right = " " .. v.cur_qty .. " " .. v.qty
- term.setCursorPos(width + 1 - #right, 2+lines_printed)
- term.write(right)
- print_line = print_line + 1
- lines_printed = lines_printed + 1
- end
- index = index + 1
- end
- for k,v in pairs(fuzzy_list) do
- if lines_printed < lines_to_print and index == print_line then
- term.setCursorPos(1,2+lines_printed)
- if v.display_name == "" then
- term.write(index .. " fuzzy " .. v.id)
- else
- term.write(index .. " fuzzy " .. v.display_name)
- end
- right = " " .. v.cur_qty .. " " .. v.qty
- term.setCursorPos(width + 1 - #right, 2+lines_printed)
- term.write(right)
- print_line = print_line + 1
- lines_printed = lines_printed + 1
- end
- index = index + 1
- end
- if print_line >= index then
- print_line = 1
- end
- term.setCursorPos(1,height)
- term.write("press ENTER for options")
- end
- local function loadSettings()
- linenum = 0
- if fs.exists(saveFile) then
- local file = fs.open(saveFile,"r")
- interface_wrap_side = tostring(file.readLine())
- interface_export_dir = tostring(file.readLine())
- while true do
- input = file.readLine()
- linenum = linenum + 1
- if input == nil then
- break
- end
- if input == "craft" then
- item = {}
- item.id = tostring(file.readLine())
- item.dmg = tonumber(file.readLine())
- item.qty = tonumber(file.readLine())
- item.batch = tonumber(file.readLine())
- item.longname = item.id .. ":" .. item.dmg
- item.display_name = tostring(file.readLine())
- craft_list[item.longname] = item
- linenum = linenum + 4
- elseif input == "void" then
- item = {}
- item.id = tostring(file.readLine())
- item.dmg = tonumber(file.readLine())
- item.qty = tonumber(file.readLine())
- item.longname = item.id .. ":" .. item.dmg
- item.display_name = tostring(file.readLine())
- void_list[item.longname] = item
- linenum = linenum + 3
- elseif input == "fuzzy" then
- item = {}
- item.id = tostring(file.readLine())
- item.qty = tonumber(file.readLine())
- item.display_name = tostring(file.readLine())
- fuzzy_list[item.id] = item
- linenum = linenum + 2
- else
- print("Settings load failed at line " .. linenum)
- sleep(3)
- break
- end
- end
- file.close()
- return true
- end
- return false
- end
- local function saveSettings()
- local file = fs.open(saveFile,"w")
- file.write(tostring(interface_wrap_side).."\n")
- file.write(tostring(interface_export_dir).."\n")
- for k,v in pairs(craft_list) do
- file.write("craft".."\n")
- file.write(tostring(v.id).."\n")
- file.write(tostring(v.dmg).."\n")
- file.write(tostring(v.qty).."\n")
- file.write(tostring(v.batch).."\n")
- file.write(tostring(v.display_name).."\n")
- end
- for k,v in pairs(void_list) do
- file.write("void".."\n")
- file.write(tostring(v.id).."\n")
- file.write(tostring(v.dmg).."\n")
- file.write(tostring(v.qty).."\n")
- file.write(tostring(v.display_name).."\n")
- end
- for k,v in pairs(fuzzy_list) do
- file.write("fuzzy".."\n")
- file.write(tostring(v.id).."\n")
- file.write(tostring(v.qty).."\n")
- file.write(tostring(v.display_name).."\n")
- end
- file.close()
- end
- if not loadSettings() then
- configOptions()
- saveSettings()
- else
- interface = peripheral.wrap(interface_wrap_side)
- if not interface then
- configOptions()
- saveSettings()
- end
- end
- turtle.select(1)
- while true do
- --clear cur amounts in craft_list
- for i,c in pairs(craft_list) do
- c.cur_qty = 0
- c.is_craftable = false
- end
- --clear cur amounts in void_list
- for i,v in pairs(void_list) do
- v.cur_qty = 0
- end
- --clear cur amounts in fuzzy_list
- for i,v in pairs(fuzzy_list) do
- v.cur_qty = 0
- end
- avail = interface.getAvailableItems()
- for k,v in pairs(avail) do
- item = craft_list[v.fingerprint.id .. ":" .. v.fingerprint.dmg]
- if item then
- if item.display_name == "" then
- d = interface.getItemDetail{v.fingerprint)
- if d then
- item.display_name = d.basic().display_name
- end
- end
- item.cur_qty = item.cur_qty + v.size
- if v.is_craftable then
- item.is_craftable = true
- end
- end
- item = void_list[v.fingerprint.id .. ":" .. v.fingerprint.dmg]
- if item then
- if item.display_name == "" then
- d = interface.getItemDetail{v.fingerprint)
- if d then
- item.display_name = d.basic().display_name
- end
- end
- item.cur_qty = item.cur_qty + v.size
- while item.cur_qty - item.qty > 0 do
- qty = math.min(item.cur_qty - item.qty, 64)
- item.cur_qty = item.cur_qty - qty
- interface.exportItem(v.fingerprint, interface_export_dir, qty)
- end
- end
- item = fuzzy_list[v.fingerprint.id]
- if item then
- if item.display_name == "" then
- d = interface.getItemDetail{v.fingerprint)
- if d then
- item.display_name = d.basic().display_name
- end
- end
- item.cur_qty = item.cur_qty + v.size
- while item.cur_qty - item.qty > 0 do
- qty = math.min(item.cur_qty - item.qty, 64)
- item.cur_qty = item.cur_qty - qty
- interface.exportItem(v.fingerprint, interface_export_dir, qty)
- end
- end
- end
- cpus = interface.getCraftingCPUs()
- cpus_available = -reserve_cpus
- for k,v in pairs(cpus) do
- if v.busy == false then
- cpus_available = cpus_available + 1
- end
- end
- --this might count recipes that are uncraftable because lack of resources
- --so it may also decrement avail_cpus tho request is failing
- --testing return of request should help
- if cpus_available > 0 then
- recipe_index = 0
- for i,c in pairs(craft_list) do
- if c.is_craftable and c.qty - c.cur_qty > 0 then
- if cur_crafting_index == recipe_index then
- qty = math.min(c.batch, c.qty - c.cur_qty)
- interface.requestCrafting(c, qty)
- cur_crafting_index = cur_crafting_index + 1
- cpus_available = cpus_available - 1
- if cpus_available <= 0 then
- break
- end
- end
- recipe_index = recipe_index + 1
- end
- end
- if cpus_available > 0 then
- cur_crafting_index = 0
- end
- end
- printScreen()
- mytimer = os.startTimer(4)
- while true do
- local event, key = os.pullEvent()
- if event == "key" then
- if key == 28 then --ENTER
- os.cancelTimer(mytimer)
- options()
- saveSettings()
- break
- elseif key == 57 then --SPACE
- os.cancelTimer(mytimer)
- mytimer = os.startTimer(4)
- else
- os.cancelTimer(mytimer)
- break
- end
- end
- if event == "timer" then
- break
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment