Advertisement
AssortedBrunoz

ME system

Sep 23rd, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. -- ME SYSTEM BY NUNOTO
  2.  
  3. term.clear()
  4. local controller = peripheral.wrap("left")
  5. local output = peripheral.wrap("right")
  6. local item_queue = {}
  7.  
  8. local function reset()
  9.     term.clear()
  10.     term.setCursorPos(1,1)
  11. end
  12.  
  13. local function list()
  14.     return controller.list()
  15.     -- RETURNS A TABLE WITH SLOT OBJECTS ({count = 64, name = "..."})
  16. end
  17.  
  18. local function special_unpack(s,e,tr)
  19.     local t = {}
  20.     for i = s, e, 1 do
  21.         table.insert(t, tr[i])
  22.     end
  23.     return t
  24. end
  25.  
  26. local function split(str, point)
  27.     return string.sub(str,1, string.find(str,point)-1), string.sub(str, string.find(str,point)+1)
  28. end
  29. local function is_in_table(haystack,needle)
  30.     for _, v in pairs(haystack) do
  31.         if v == needle then
  32.             return true
  33.         end
  34.     end
  35.     return false
  36. end
  37.  
  38. local function get_slot(name)
  39.     local l = list()
  40.    
  41.     for i, v in pairs(l) do
  42.         if name == v.name then
  43.             return i
  44.         end
  45.     end
  46. end
  47.  
  48. local function open_page(page)
  49.     reset()
  50.     local l = list()
  51.     local section = special_unpack((page+1),page+16,l)
  52.     for slot, item in pairs(section) do
  53.         if item ~= nil then
  54.             local source, name = split(item.name, ":")
  55.             write(item.count.."x "..name:gsub("_", " ").."\n")
  56.         end
  57.     end
  58.    
  59.     term.setCursorPos(1,18)
  60.    
  61.     write("<      |      >")
  62.    
  63.     local event, button, x, y = os.pullEvent("mouse_click")
  64.    
  65.     print(x,y)
  66.    
  67.     os.pullEvent("redstone")
  68. end
  69.  
  70. local function pull(slot, amount)
  71.     controller.pushItems(peripheral.getName(output),slot,amount)
  72. end
  73.  
  74. local function push()
  75.     for i = 1, #output.list(), 1 do
  76.         output.pushItems(peripheral.getName(controller),i)
  77.     end
  78. end
  79.  
  80. function levenshtein(str1, str2)
  81.  
  82.     local matrix = {}
  83.    
  84.     for i = 0, #str1 do
  85.         matrix[i] = {[0] = i}
  86.     end
  87.     for j = 0, #str2 do
  88.         matrix[0][j] = j
  89.     end
  90.    
  91.     for i = 1, #str1 do
  92.         for j = 1, #str2 do
  93.             local cost = (str1:sub(i, i) == str2:sub(j, j) and 0 or 1)
  94.             matrix[i][j] = math.min(matrix[i-1][j] + 1, matrix[i][j-1] + 1, matrix[i-1][j-1] + cost)
  95.         end
  96.     end
  97.    
  98.     return matrix[#str1][#str2]
  99.    
  100. end
  101.  
  102. function closest_match(target, strings)
  103.     local closest, distance = nil, math.huge
  104.    
  105.     for index, str in pairs(strings) do
  106.         if str.name ~= nil then
  107.             local d = levenshtein(target, str.name)
  108.             if d < distance then
  109.                 closest, distance = str.name, d
  110.             end
  111.         end
  112.     end
  113.    
  114.     return closest
  115. end
  116.  
  117. local function flush()
  118.     for i, v in pairs(list()) do
  119.         if v.count <= 64 then
  120.             pull(i, 64)
  121.         end
  122.     end
  123.     reset()
  124.     write("ITEMS FLUSHED.")
  125.     os.sleep(5)
  126. end
  127.  
  128. local function deposit()
  129.     push()
  130.     reset()
  131.     write("ITEMS SUCCESSFULLY DEPOSITED.")
  132.     os.sleep(5)
  133. end
  134.  
  135. local function withdraw()
  136.     reset()
  137.     write("INSERT ITEM NAME:\nITEM: ")
  138.     local search = string.lower(read()):gsub(" ", "_")
  139.     local found = closest_match(search,list())
  140.     local slot = get_slot(found)
  141.     if found == nil then
  142.         write("\nITEM NOT FOUND")
  143.         os.sleep(2)
  144.         withdraw()
  145.         return
  146.     end
  147.     reset()
  148.     write("INSERT AMOUNT:\n")
  149.     local mod, item_ = split(found, ":")
  150.     write("ITEM: "..item_.."\nAMOUNT: ")
  151.     local amount = read()
  152.     amount = tonumber(amount)
  153.     pull(slot, amount)
  154. end
  155.  
  156. while true do
  157.     reset()
  158.     write("NSP CC INVENTORY SYSTEM\nO: ORDER ITEM\nI: INPUT ITEM\nF: FLUSH ITEMS\n")
  159.  
  160.     local key = read()
  161.  
  162.     --if key == keys.l then
  163.     --    open_page(1)
  164.     --end
  165.     if key == "o" then
  166.         withdraw()
  167.     elseif key == "i" then
  168.         deposit()
  169.     elseif key == "f" then
  170.         flush()
  171.     end
  172. end
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement