SHARE
TWEET

driveworker

xmann110 Feb 14th, 2020 (edited) 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function get_empty_slot()
  2.     found_empty = false
  3.     for i=1, 16 do
  4.         if turtle.getItemCount() == 0 then
  5.             found_empty = true
  6.             break
  7.         end
  8.     end
  9.     return found_empty
  10. end
  11.  
  12. function starts_with(str, start)
  13.    return str:sub(1, #start) == start
  14. end
  15.  
  16. function ends_with(str, ending)
  17.    return ending == "" or str:sub(-#ending) == ending
  18. end
  19.  
  20. function update_index()
  21.     foreign_items = 0
  22.     for i=1, 16 do
  23.         turtle.select(i)
  24.         item = turtle.getItemDetail()
  25.         if item then
  26.             if item.name == top_item then
  27.                 turtle.dropUp()
  28.             else
  29.                 if item.name == bottom_item then
  30.                     turtle.dropUp()
  31.                 else
  32.                     foreign_items = foreign_items + item.count
  33.                 end
  34.             end
  35.         end
  36.     end
  37.  
  38.     if foreign_items > 0 then
  39.         print("WARNING: " .. tostring(foreign_items) .. " FOREIGN ITEMS DETECTED")
  40.     end
  41.  
  42.     if not get_empty_slot() then
  43.         print("WARNING: FULL INVENTORY")
  44.     else
  45.         top_item = nil
  46.         bottom_item = nil
  47.         if turtle.suckUp(1) then
  48.             top_item = turtle.getItemDetail().name
  49.             turtle.dropUp(1)
  50.         end
  51.         if turtle.suckDown(1) then
  52.             bottom_item = turtle.getItemDetail().name
  53.             turtle.dropDown(1)
  54.         end
  55.     end
  56.  
  57.     print("Top container: " .. tostring(top_item))
  58.     print("Bottom container: " .. tostring(bottom_item))
  59. end
  60.  
  61. rednet.open("left")
  62.  
  63. top_item = nil
  64. bottom_item = nil
  65.  
  66. last = nil
  67. first = nil
  68.  
  69. last = not turtle.detect()
  70.  
  71. a, b = turtle.inspectDown()
  72. first = b.name == "minecraft:obsidian"
  73.  
  74. if not first then
  75.     update_index()
  76. end
  77.  
  78. neighbor = nil
  79. if not last then
  80.     neighbor = peripheral.wrap("front")
  81. end
  82.  
  83. if first then
  84.     while true do
  85.         term.write(">>> ")
  86.         command = read()
  87.         if command == "index" then
  88.             rednet.send(neighbor.getID(), {}, "index")
  89.             sender, message, protocol = rednet.receive()
  90.             print("There are " .. tostring(table.getn(message)) .. " nodes connected to the network")
  91.         end
  92.         if command == "reboot" then
  93.             rednet.send(neighbor.getID(), ".", "reboot")
  94.             os.reboot()
  95.         end
  96.         if command == "print" then
  97.             term.write("Enter message: ")
  98.             rednet.send(neighbor.getID(), read(), "print")
  99.         end
  100.         if command == "get" then
  101.             term.write("Enter item name: ")
  102.             item = read()
  103.             term.write("How many: ")
  104.             count = read()
  105.             if count == "" then
  106.                 count = "64"
  107.             end
  108.             rednet.send(neighbor.getID(), {item, count}, "get")
  109.             sender, message, protocol = rednet.receive()
  110.             if message == true then
  111.                 turtle.suck()
  112.                 print("Sucessfully retrieved item")
  113.             else
  114.                 print("Failed to find item")
  115.             end
  116.         end
  117.         if command == "store" then
  118.             turtle.select(1)
  119.             item = turtle.getItemDetail()
  120.             if item then
  121.             else
  122.                 print("Nothing to store")
  123.             end
  124.         end
  125.     end
  126. else
  127.     while true do
  128.         sender, message, protocol = rednet.receive()
  129.         print(protocol .. ": " .. tostring(message))
  130.         if protocol == "index" then
  131.             modified_message = message
  132.             table.insert(modified_message, os.getComputerID())
  133.             if last then
  134.                 rednet.send(sender, modified_message, "index")
  135.             else
  136.                 rednet.send(neighbor.getID(), modified_message, "index")
  137.                 neighbor_reply_id, index_reply, index_protocol = rednet.receive(1)
  138.                 if neighbor_reply_id then
  139.                     rednet.send(sender, index_reply, "index")
  140.                 else
  141.                     print("No response from neighbor")
  142.                     rednet.send(sender, "Unresponsive neighbor", "error")
  143.                 end
  144.             end
  145.         end
  146.         if protocol == "get" then
  147.             found = false
  148.             if not (top_item == nil) and ends_with(top_item, message[1]) then
  149.                 turtle.suckUp(tonumber(message[2]))
  150.                 found = true
  151.             end
  152.             if not (bottom_item == nil) and ends_with(bottom_item, message[1]) then
  153.                 turtle.suckDown(tonumber(message[2]))
  154.                 found = true
  155.             end
  156.             if found then
  157.                 rednet.send(sender, true, "get")
  158.                 while turtle.getItemCount() > 0 do
  159.                     sleep(0.1)
  160.                 end
  161.                 update_index()
  162.             else
  163.                 if last then
  164.                     rednet.send(sender, false, "get")
  165.                 else
  166.                     rednet.send(neighbor.getID(), message, "get")
  167.                     neighbor_reply_id, get_reply, get_protocol = rednet.receive(1)
  168.                     if neighbor_reply_id then
  169.                         if get_reply == true then
  170.                             turtle.suck(64)
  171.                         end
  172.                         rednet.send(sender, get_reply, "get")
  173.                     else
  174.                         print("No response from neighbor")
  175.                         rednet.send(sender, "Unresponsive neighbor", "error")
  176.                     end
  177.                 end
  178.             end
  179.         end
  180.         if protocol == "reboot" then
  181.             if not last then
  182.                 rednet.send(neighbor.getID(), ".", "reboot")
  183.             end
  184.             os.reboot()
  185.         end
  186.         if protocol == "print" then
  187.             if not last then
  188.                 rednet.send(neighbor.getID(), message, "print")
  189.             end
  190.             print("MESSAGE: " .. message)
  191.         end
  192.     end
  193. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top