Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ME SYSTEM BY NUNOTO
- term.clear()
- local controller = peripheral.wrap("bottom")
- local output = peripheral.wrap("right")
- local item_queue = {}
- local function reset()
- term.clear()
- term.setCursorPos(1,1)
- end
- local function list()
- local l = controller.list() --A TABLE WITH SLOT OBJECTS ({count = 64, name = "..."})
- --RETURNS A OPTIMIZED LIST
- --[[
- {
- {
- count = (total count of all drawers),
- displayName = "Something"
- name = "mod:something_lame"
- slots = [1,2,5,3] (slots that drawers contain the item)
- }
- }
- ]]
- local optimized = {}
- for slot, slot_obj in pairs(l) do
- local details = controller.getItemDetail(slot)
- --[[
- count = _,
- displayName = _,
- itemGroups = _,
- maxCount = _,
- name = _
- ]]
- local found = false
- for i_slot, i_slot_obj in pairs(optimized) do
- if i_slot_obj.name == slot_obj.name then
- found = true
- i_slot_obj.count = slot_obj.count + details.count
- table.insert(i_slot_obj.slots, slot)
- end
- end
- if not found then
- table.insert(optimized,{
- count = details.count,
- displayName = details.displayName,
- name = details.name,
- slots = {slot}
- })
- end
- end
- return optimized
- end
- print("LOADING INVENTORY.... \nPLEASE WAIT....")
- local inventory = list()
- local function special_unpack(s,e,tr)
- local t = {}
- for i = s, e, 1 do
- table.insert(t, tr[i])
- end
- return t
- end
- local function split(str, point)
- return string.sub(str,1, string.find(str,point)-1), string.sub(str, string.find(str,point)+1)
- end
- local function pull(obj, amount)
- local a = 0
- local curr = 1
- while a ~= amount do
- a = a + controller.pushItems(peripheral.getName(output),obj.slots[curr],amount - a)
- curr = curr + 1
- end
- end
- local function push(obj, amount)
- for i = 1, #output.list(), 1 do
- output.pushItems(peripheral.getName(controller),i)
- end
- end
- local function flush()
- for i, v in pairs(controller.list()) do
- if v.count <= 64 then
- controller.pushItems(peripheral.getName(output),i)
- end
- end
- reset()
- write("ITEMS FLUSHED.")
- os.sleep(5)
- end
- local function deposit()
- push()
- reset()
- write("ITEMS SUCCESSFULLY DEPOSITED.")
- os.sleep(5)
- end
- local function fracture(str)
- local f = {}
- for i in string.gmatch(str, "%a") do
- table.insert( f,i )
- end
- return f
- end
- local function closest_search(str)
- local rating = {}
- local fractured_target = fracture(string.lower(str))
- for j, obj in pairs(inventory) do
- local t = {slot = j, rating = 0}
- for i, v in pairs(fracture(string.lower(obj.displayName))) do
- if v == fractured_target[i] then
- t.rating = t.rating + 1
- else
- t.rating = t.rating - 1
- end
- end
- if t.rating > 0 then
- table.insert( rating,t )
- end
- end
- table.sort(rating, function(a,b)
- return a.rating > b.rating
- end)
- if next(rating) == nil then
- return nil
- end
- return inventory[rating[1].slot]
- end
- local function inclusive_search(str)
- local matches = {}
- for slot, obj in pairs(inventory) do
- if string.find(string.lower(obj.displayName), string.lower(str)) then
- table.insert( matches, obj )
- end
- end
- return matches
- end
- local function exact_search(str)
- for opt_slot, slot_obj in pairs(inventory) do
- if string.lower(slot_obj.displayName) == str then
- return slot_obj
- end
- end
- end
- local function withdraw(starting_mode)
- reset()
- term.setCursorPos(1,1)
- local mode = starting_mode
- local modes = {"CLOSEST","INCLUDES","EXACT"}
- write("INSERT ITEM NAME:\nCHANGE SEARCH MODE WITH <- or ->\nCURRENT MODE: "..modes[mode].."\nITEM: ")
- local function text_input()
- local search = string.lower(read())
- local obj = nil
- if modes[mode] == "EXACT" then
- obj = exact_search(search)
- elseif modes[mode] == "INCLUDES" then
- obj = inclusive_search(search)
- elseif modes[mode] == "CLOSEST" then
- obj = closest_search(search)
- end
- if obj == nil then
- write("ITEM NOT FOUND.")
- os.sleep(5)
- withdraw(mode)
- return
- elseif obj.name ~= nil then
- --single answer
- term.setCursorPos(1,4)
- term.clearLine()
- write("ITEM FOUND: "..obj.displayName.."\nIS THIS THE CORRECT ITEM? (y/n)\n")
- local a = read()
- if a ~= "y" then
- return
- end
- else
- if next(obj) == nil then
- write("NO ITEMS FOUND")
- os.sleep(2)
- withdraw(mode)
- return
- end
- if #obj == 9 then
- write("SEARCH TOO BROAD, PLEASE BE MORE PRECISE.")
- os.sleep(2)
- withdraw(mode)
- return
- end
- term.setCursorPos(1,1)
- term.clearLine()
- write("SELECT ONE OF THE FOLLOWING ITEMS:")
- term.setCursorPos(1,4)
- --multiple options
- for i, _obj in pairs(obj) do
- write((i)..". ".._obj.displayName.."\n")
- end
- local chosen = tonumber(read())
- obj = obj[chosen]
- for i = 5, 14, 1 do
- term.setCursorPos(1,i)
- term.clearLine()
- end
- end
- term.setCursorPos(1,1)
- term.clearLine()
- write("INSERT DESIRED AMOUNT:")
- term.setCursorPos(1,5)
- term.clearLine()
- term.setCursorPos(1,4)
- term.clearLine()
- write("ITEM: "..obj.displayName.."\nAVAILABLE AMOUNT: "..obj.count.."\nAMOUNT: ")
- local amount = tonumber(read())
- pull(obj, amount)
- end
- local function mode_switch()
- while true do
- local event, key = os.pullEvent("key")
- local p = #modes[mode]
- if key == keys.right then
- if mode + 1 > #modes then
- mode = 1
- else
- mode = mode + 1
- end
- elseif key == keys.left then
- if mode - 1 == 0 then
- mode = #modes
- else
- mode = mode - 1
- end
- end
- local x,y = term.getCursorPos()
- term.setCursorPos(1,3)
- term.clearLine()
- write("CURRENT MODE: "..modes[mode])
- term.setCursorPos(x,y)
- end
- end
- parallel.waitForAny(text_input, mode_switch)
- end
- while true do
- reset()
- write("NSP CC INVENTORY SYSTEM\nO: ORDER ITEM\nI: INPUT ITEM\nF: FLUSH ITEMS\n")
- local key = read()
- --if key == keys.l then
- -- open_page(1)
- --end
- if key == "o" then
- withdraw(1)
- elseif key == "i" then
- deposit()
- elseif key == "f" then
- flush()
- end
- inventory = list()
- end
Add Comment
Please, Sign In to add comment