Advertisement
nagoL2015

row.lua

Jul 26th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.18 KB | None | 0 0
  1. modem1 = 6565
  2. modem2 = 6566
  3. modem3 = 6567
  4. modem4 = 6568
  5.  
  6. local item = "" -- Item ID. Do not include minecraft:; this will be added later
  7.  
  8. if fs.exists(".item") and item == "" then
  9.     local f = fs.open(".item","r")
  10.     if f then
  11.         item = f.readLine()
  12.         f.close()
  13.     end
  14. end
  15.  
  16. if turtle.getItemDetail(16) == nil or not (turtle.getItemDetail(16).name == "minecraft:chest" or turtle.getItemDetail(16).name == "minecraft:trapped_chest") then
  17.     error("missing chest/trapped chest on slot 16")
  18. end
  19.  
  20. local count = 0
  21.  
  22. local modems = { peripheral.find("modem") }
  23. if #modems == 0 then error("No modems found") end
  24.  
  25. item = item:lower()
  26.  
  27. local busy = false
  28.  
  29. if item == nil or item == "" then
  30.     os.setComputerLabel("Row : Empty")
  31. else
  32.     os.setComputerLabel("Row : " .. item:sub(1,1):upper() .. item:sub(2))
  33. end
  34.  
  35. if item ~= "" then
  36.     item = "minecraft:"..item
  37.     print(item)
  38. else
  39.     print("Empty")
  40. end
  41.  
  42. local modem = modems[1]
  43.  
  44. modem.open(modem1) --Send Info
  45. modem.open(modem2) --Recount Listener
  46. modem.open(modem3) --Request Listener
  47. modem.open(modem4) --Setup listener
  48.  
  49. while turtle.up() do end
  50.  
  51. local inputReady = true
  52.  
  53. local function selSlot(slot) --Might save time, due to less turtle.select()s?
  54.     if turtle.getSelectedSlot() ~= slot then turtle.select(slot) end
  55. end
  56.  
  57. function detectItemCount()
  58.     inputReady = false
  59.     busy = true
  60.     count = 0
  61.     while turtle.up() do end
  62.    
  63.     for sl = 1,15 do
  64.         if turtle.getItemCount(sl) > 0 then
  65.             turtle.select(sl)
  66.             turtle.dropUp()
  67.         end
  68.     end
  69.    
  70.     while turtle.down() do
  71.         if turtle.getItemDetail(16) == nil or not (turtle.getItemDetail(16).name == "minecraft:chest" or turtle.getItemDetail(16).name == "minecraft:trapped_chest") then
  72.             error("missing chest/trapped chest on slot 16")
  73.         end
  74.         selSlot(16)
  75.         turtle.placeUp()
  76.        
  77.         selSlot(1)
  78.         while turtle.suck() do
  79.             selSlot(1)
  80.             count = count + turtle.getItemCount(1)
  81.             turtle.dropUp()
  82.         end
  83.        
  84.         while turtle.suckUp() do
  85.             selSlot(1)
  86.             turtle.drop()
  87.         end
  88.        
  89.         selSlot(16)
  90.         turtle.digUp()
  91.     end
  92.    
  93.     while turtle.up() do end
  94.    
  95.     inputReady = true
  96.     busy = false
  97.     print("Finished count: "..count)
  98. end
  99.  
  100. function countItms()
  101.     local tuCount = 0
  102.     for s = 1,15 do
  103.         tuCount = tuCount + turtle.getItemCount(s)
  104.     end
  105.     return tuCount
  106. end
  107.  
  108. function modemListener()
  109.     while true do
  110.         local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
  111.        
  112.         if chnl == modem2 then
  113.             print("Detecting item count")
  114.             detectItemCount()
  115.         elseif chnl == modem3 and item ~= "" then
  116.             if type(msg) == "table" and type(msg.name) == "string" and type(msg.count) == "number" then
  117.                 if msg.name == item then
  118.                     if msg.count <= count then
  119.                         inputReady = false
  120.                         busy = true
  121.                        
  122.                         modem.transmit(rChnl,chnl,{type="requestResponse",success=true})
  123.                         print("Request: success")
  124.                         while turtle.down() do
  125.                             if msg.count - countItms() <= 0 then
  126.                                 break
  127.                             end
  128.                             while turtle.suck(msg.count - countItms() >= 64 and 64 or msg.count - countItms()) and countItms() < msg.count do end
  129.                         end
  130.                        
  131.                         while turtle.down() do end
  132.                        
  133.                         count = count - countItms()
  134.                         print("New count: "..count.." (subtracted "..countItms().." )")
  135.                        
  136.                         for slot = 1,15 do
  137.                             if turtle.getItemCount(slot) > 0 then
  138.                                 selSlot(slot)
  139.                                 turtle.dropDown()
  140.                             end
  141.                         end
  142.                        
  143.                         while turtle.up() do end
  144.                        
  145.                         busy = false
  146.                         inputReady = true
  147.                     else
  148.                         modem.transmit(rChnl,chnl,{type="requestResponse",success=false,error="Not enough items"})
  149.                         print("Request: failure (not enough items)")
  150.                     end
  151.                 else
  152.                     print("Request: failure (item does not match)")
  153.                 end
  154.             else
  155.                 modem.transmit(rChnl,chnl,{type="requestResponse",success=false,error="Invalid input"})
  156.                 print("Request: failure (invalid input)")
  157.             end
  158.         elseif chnl == modem4 then
  159.             while turtle.up() do end
  160.             local x,y,z = gps.locate()
  161.             if x ~= nil then
  162.                 modem.transmit(rChnl,chnl,{type="setupResponse",success=true,id=item,x=x,y=y+1,z=z})
  163.                 print("Sent setup: success")
  164.             else
  165.                 modem.transmit(rChnl,chnl,{type="setupResponse",success=false,error="GPS not active"})
  166.                 print("Sent setup: failure")
  167.             end
  168.         end
  169.     end
  170. end
  171.  
  172. function statusUpdater()
  173.     while true do
  174.         local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
  175.        
  176.         if chnl == modem1 then
  177.             local x,y,z = gps.locate()
  178.            
  179.             local inv = {}
  180.            
  181.             for slot = 1,16 do
  182.                 local detail = turtle.getItemDetail(slot)
  183.                 if detail == nil then
  184.                     inv[slot] = {}
  185.                 else
  186.                     inv[slot] = detail
  187.                 end
  188.             end
  189.            
  190.             modem.transmit(rChnl,chnl,{type="infoRowResponse",count=count,item=item,fuel=turtle.getFuelLevel(),inventory=inv,location={x=x,y=y,z=z},busy=busy})
  191.             print("Sent info")
  192.         end
  193.     end
  194. end
  195.  
  196. function testUp()
  197.     while true do
  198.         if inputReady then
  199.             turtle.select(1)
  200.             if turtle.suckUp() then
  201.                 busy = true
  202.                 turtle.select(1)
  203.                 while turtle.suckUp() and turtle.getItemDetail(15) == nil do turtle.select(1) end
  204.                 local scheduleDump = false
  205.                 local loops = 0
  206.                 while true do
  207.                     if loops >= 50 then
  208.                         scheduleDump = true break
  209.                     end
  210.                     local empty = false
  211.                     for slot = 1,15 do
  212.                         if turtle.getItemDetail(slot) ~= nil and turtle.getItemDetail(slot).name == item then
  213.                             empty = true
  214.                         elseif turtle.getItemDetail(slot) ~= nil then --foreign item found; dump through the system.
  215.                             scheduleDump = true
  216.                         end
  217.                     end
  218.                     if empty then
  219.                         turtle.down()
  220.                         for slot = 1,15 do
  221.                             if turtle.getItemDetail(slot) ~= nil and turtle.getItemDetail(slot).name == item then
  222.                                 turtle.select(slot)
  223.                                 local cnt = turtle.getItemCount(slot)
  224.                                 if turtle.drop() and turtle.getItemCount(slot) ~= cnt then count = count + cnt print("New count: "..count.." (added "..cnt.." )") end
  225.                             end
  226.                         end
  227.                     else
  228.                         break
  229.                     end
  230.                     loops = loops + 1
  231.                 end
  232.                 if scheduleDump then
  233.                     while turtle.down() do end
  234.                     for slot = 1,15 do
  235.                         turtle.select(slot)
  236.                         turtle.dropDown()
  237.                     end
  238.                 end
  239.                 while turtle.up() do end
  240.                 busy = false
  241.             end
  242.         end
  243.         sleep(5)
  244.     end
  245. end
  246.  
  247. parallel.waitForAll(modemListener,testUp,statusUpdater,detectItemCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement