Advertisement
AssortedBrunoz

test

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