Advertisement
Shaka01

smartStockpile_receiver

Feb 26th, 2023 (edited)
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.58 KB | None | 0 0
  1. --Receiver
  2.  
  3.  
  4. -- Define the inventory to move items to
  5. local storage = ""
  6.  
  7. -- Load target inventory from file
  8. if fs.exists("targetInv") then
  9.     local file = fs.open("targetInv", "r")
  10.     storage = file.readAll()
  11.     file.close()
  12. end
  13.  
  14. -- Prompt user to select target inventory if not set
  15. if storage == "" then
  16.     -- Find and display available inventories
  17.     local modem = nil
  18.     for i, side in ipairs(peripheral.getNames()) do
  19.         local device = peripheral.getType(side)
  20.         if device == "modem" then
  21.             modem = peripheral.wrap(side)
  22.             break
  23.         end
  24.     end
  25.  
  26.     if modem then
  27.         local options = modem.getNamesRemote()
  28.         term.clear()
  29.         term.setCursorPos(1, 1)
  30.         term.setTextColor(colors.black)
  31.         term.setBackgroundColor(colors.yellow)
  32.         print("Select a target inventory:\n")
  33.         term.setBackgroundColor(colors.black)
  34.         term.setTextColor(colors.white)
  35.         for i, option in ipairs(options) do
  36.             print(i .. ". " .. option)
  37.         end
  38.        
  39.         local choice = tonumber(read())
  40.         if choice and choice >= 1 and choice <= #options then
  41.             storage = options[choice]
  42.             local file = fs.open("targetInv", "w")
  43.             file.write(storage)
  44.             file.close()
  45.             term.clear()
  46.             term.setTextColor(colors.green)
  47.             term.setCursorPos(1, 1)
  48.             term.write("Set ")
  49.             term.setTextColor(colors.yellow)
  50.             term.write(storage)
  51.             term.setTextColor(colors.green)
  52.             print(" as target inventory.")
  53.             term.setTextColor(colors.gray)
  54.             print("\n\nPress any key to continue..")
  55.             event, key = os.pullEvent("key")
  56.             term.clear()
  57.         else
  58.             print("Invalid choice.")
  59.             return
  60.         end
  61.     else
  62.         print("No modem detected.")
  63.         return
  64.     end
  65. end
  66.  
  67. --------------------------------------------
  68. -- Initialize
  69. local toInventory = peripheral.wrap(storage)
  70. local itemToTransfer = 0
  71. local amountToTransfer = 0
  72. fromInventories = {}
  73. modem = nil
  74. modem2 = nil
  75.  
  76.  
  77. -- Connect peripherals
  78. local sides = {"left", "right", "top", "bottom", "front", "back"}
  79. for i, side in ipairs(sides) do
  80.   local device = peripheral.getType(side)
  81.   if device == "modem" then
  82.     rednet.open(side)
  83.     if modem == nil then
  84.     modem = peripheral.wrap(side)
  85.     else
  86.     modem2 = peripheral.wrap(side)
  87.     end
  88.     -- print("Opened rednet on " .. side)
  89.   elseif device then
  90.     local wrapper = peripheral.wrap(side)
  91.     -- print("Wrapped " .. device .. " on " .. side)
  92.   end
  93. end
  94.  
  95.  
  96. function checkWireless(modemName)
  97.     for i, side in ipairs(sides) do
  98.         if modemName ~= nil then
  99.             if modemName.isWireless() then
  100.                 return true
  101.             else
  102.                 return false
  103.             end
  104.         else
  105.             return "noModem"
  106.         end
  107.     end
  108. end
  109.  
  110. if checkWireless(modem) == "noModem" or checkWireless(modem2) == "noModem" then
  111.     term.clear()
  112.     term.setCursorPos(1,1)
  113.     term.setTextColor(colors.orange)
  114.     print("Missing modems!\n\nNeed one wired and one wireless modem.")
  115.     event, key = os.pullEvent(key)
  116.     term.setTextColor(colors.red)
  117.     print("Rebooting.")
  118.     sleep(3)
  119.     os.reboot()
  120. end
  121.  
  122.  
  123. if checkWireless(modem) == false and checkWireless(modem2) == false then
  124.     term.clear()
  125.     term.setCursorPos(1,1)
  126.     term.setTextColor(colors.orange)
  127.     print("No >wireless< modem connected!\n\nConnect one and press any key.")
  128.     event, key = os.pullEvent(key)
  129.     term.setTextColor(colors.red)
  130.     print("Rebooting.")
  131.     sleep(3)
  132.     os.reboot()
  133. elseif checkWireless(modem) == true and checkWireless(modem2) == true then
  134.     term.clear()
  135.     term.setCursorPos(1,1)
  136.     term.setTextColor(colors.orange)
  137.     print("No >wired< modem connected!\n\nConnect one and press any key.")
  138.     event, key = os.pullEvent(key)
  139.     term.setTextColor(colors.red)
  140.     print("Rebooting.")
  141.     sleep(3)
  142.     os.reboot()
  143. end
  144.  
  145.  
  146.  
  147. function scanAndSendInventories(fromInventories)
  148.   local scannedItems = {}
  149.   local sentItems = {}
  150.   for i = 1, #fromInventories do
  151.     local inventory = peripheral.wrap(fromInventories[i])
  152.     for slot, item in pairs(inventory.list()) do
  153.       if not scannedItems[item.name] then
  154.         scannedItems[item.name] = item.count
  155.       else
  156.         scannedItems[item.name] = scannedItems[item.name] + item.count
  157.       end
  158.     end
  159.   end
  160.  
  161.   for itemName, itemCount in pairs(scannedItems) do
  162.     if not sentItems[itemName] then
  163.       sendRednetMessage(itemName, "stockpileupdate")
  164.       sentItems[itemName] = true
  165.     end
  166.   end
  167. end
  168.  
  169. -- Function to send rednet message
  170. function sendRednetMessage(itemName, protocoll)
  171. rednet.broadcast("saveItem " .. itemName, protocoll)
  172. end
  173.  
  174. function moveItems()
  175.   -- Loop through the list of inventories
  176.   for i, fromName in ipairs(fromInventories) do
  177.     local fromInventory = peripheral.wrap(fromName)
  178.  
  179.     -- Get a list of all items in the inventory
  180.     local items = fromInventory.list()
  181.  
  182.     -- Loop through the items in the inventory
  183.     for slot, item in pairs(items) do
  184.       -- Check if the item is the specified item to transfer
  185.       if item.name == itemToTransfer then
  186.         local transferred = fromInventory.pushItems(peripheral.getName(toInventory), slot, amountToTransfer)
  187.         amountToTransfer = amountToTransfer - transferred
  188.         if amountToTransfer <= 0 then
  189.           return
  190.         end
  191.       end
  192.     end
  193.   end
  194. end
  195.  
  196. function split(s, delimiter)
  197.     result = {};
  198.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  199.         table.insert(result, match);
  200.     end
  201.     return result;
  202. end
  203.  
  204. function receive()
  205.     senderID, message, protocol = rednet.receive()
  206.     split(message, " ")
  207.     itemToTransfer = tostring(result[1])
  208.     amountToTransfer = tonumber(result[2])
  209. end
  210.  
  211. function wholething()
  212.     receive()
  213.     if protocol == "stockpile" then
  214.     term.setTextColor(colors.white)
  215.     print(itemToTransfer, ": ",amountToTransfer)
  216.     moveItems()
  217.     end
  218. end
  219.  
  220. function updateAvailableItems()
  221.     term.clear()
  222.     term.setCursorPos(1,1)
  223.     print("Scan all attached inventories and send them to my brain?\n")
  224.     term.setTextColor(colors.red)
  225.     print("Hit Enter to confirm.")
  226.     local event, key = os.pullEvent("key")
  227.     if key == keys.enter then
  228.         scanAndSendInventories(fromInventories)
  229.         term.clear()
  230.         term.setCursorPos(1,1)
  231.         term.setTextColor(colors.lime)
  232.         print("Items transmitted!")
  233.         sleep(2)
  234.         os.reboot()
  235. end    
  236. end
  237.  
  238. function retransmitItems()
  239.     event, key = os.pullEvent("key")
  240.     if key == keys.space then
  241.         updateAvailableItems() 
  242.     end
  243. end
  244.  
  245.    
  246.    
  247. --initialize inventories
  248. if checkWireless(modem) == false then
  249. newInventories = modem.getNamesRemote()
  250. else
  251. newInventories = modem2.getNamesRemote()
  252. end
  253.  
  254. for _, inventoryName in ipairs(newInventories) do
  255.     if inventoryName ~= storage then
  256.             -- print(inventoryName)
  257.       table.insert(fromInventories, inventoryName)
  258.     end
  259. end
  260.  
  261. ---loopy doo
  262. while true do
  263. parallel.waitForAny(retransmitItems, wholething)
  264. end
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement