Advertisement
rhn

ItemSortStoreTurtleChest

rhn
Apr 2nd, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.20 KB | None | 0 0
  1. ----------------------------Definitions--------------------------
  2. homecoordinate={1,0}--home coordinate of the turtle
  3. fuelcoordinate={2,0}
  4. coordinate={homecoordinate[1],homecoordinate[2]}--sets current coordinate to home coordinate
  5. local rednetsleep=20--rednet timeout in seconds
  6. local rednetsleepcycles=6 --wait X number of rednet timeouts before sorting items despite turtle not full
  7.  
  8.  
  9.  
  10. ---------------------------------Functions-------------------------------
  11. function moveto(ic,jc) --Function for moving to specified chest coordinate
  12.     deltax=ic-coordinate[1] --calculate distance to target from current coordinate
  13.     deltay=jc-coordinate[2]
  14.     --print("Deltas: "..deltax..","..deltay)
  15. -- Move to first coordinate
  16.     if deltay>0 then
  17.         for i=1,deltay do
  18.             while not turtle.forward() do
  19.                 sleep(1)
  20.             end
  21.         end
  22.     elseif deltay<0 then
  23.         for i=1,math.abs(deltay) do
  24.             while not turtle.back() do
  25.                 sleep(1)
  26.             end
  27.         end
  28.     end
  29.     coordinate[2]=jc
  30. -- Move to second coordinate
  31.     if deltax>0 then
  32.         turtle.turnRight()
  33.         for i=1,deltax do
  34.             while not turtle.forward() do
  35.                 sleep(1)
  36.             end
  37.         end
  38.         turtle.turnLeft()
  39.     elseif deltax<0 then
  40.         turtle.turnLeft()
  41.         for i=1,math.abs(deltax) do
  42.             while not turtle.forward() do
  43.                 sleep(1)
  44.             end
  45.         end
  46.         turtle.turnRight()
  47.     end
  48.     coordinate[1]=ic
  49. end
  50.  
  51.  
  52. function resethome()
  53.     for i=1,16 do
  54.         while not turtle.forward() do
  55.             local success,hometest=turtle.inspectDown()
  56.             if success and hometest.name=="enderstorage:ender_storage" then
  57.                 turtle.turnRight()
  58.                 turtle.turnRight()
  59.                 return
  60.             else
  61.                 turtle.turnLeft()
  62.                 break
  63.             end
  64.         end
  65.     end
  66. end
  67. ------------------------Rednet prep-------------------------
  68. rednet.open("right")
  69. rednet.host("ItemSortRandom","random1")
  70.  
  71. ------------------------Peripherals-------------------------
  72. inputChest=peripheral.wrap("bottom")
  73. ------------------------Execution---------------------------
  74. resethome()
  75. for i=1,16 do
  76.     turtle.select(i)
  77.     turtle.dropDown()
  78. end
  79. print("Starting with fuel level: "..turtle.getFuelLevel())
  80.  
  81. while true do --main execution loop
  82.     if turtle.getFuelLevel()<200 then
  83.         moveto(fuelcoordinate[1],fuelcoordinate[2])
  84.         turtle.select(1)
  85.         while turtle.getFuelLevel()<200 do
  86.             turtle.suckDown(64)
  87.             turtle.refuel()
  88.             sleep(1)
  89.         end
  90.         moveto(homecoordinate[1],homecoordinate[2])
  91.     end
  92.  
  93.     --Receive sorting jobs from main computer:
  94.     jobs={}
  95.     jobtimer=0
  96.     while #jobs<16 and jobtimer<rednetsleepcycles do --wait for a full inventory of jobs, or max 5 minutes
  97.         local message=nil
  98.         local senderId, message, protocol = rednet.receive(rednetsleep)
  99.         if message~=nil then --If message was received
  100.             print(textutils.serialize(message))
  101.             for i=1,#message do
  102.                 if #jobs<16 then
  103.                     print(message[i][1].."/"..message[i][2].." x"..message[i][3].." to "..message[i][4]..","..message[i][5].." slot "..message[i][6])
  104.                     --append entry in message to jobs list and add turtle slot number for item:
  105.                     jobs[#jobs+1]={message[i][1],message[i][2],message[i][3],message[i][4],message[i][5],message[i][6],#jobs+1}
  106.                     for j=1,inputChest.size() do --find item for job in input chest
  107.                         inputItem=inputChest.getItemMeta(j)
  108.                         if inputItem~= nil then
  109.                             if inputItem.name == message[i][1] and inputItem.damage == message[i][2] then --check if item matches
  110.                                 inputChest.pushItems("up",j,message[i][3],#jobs) --Insert item into turtle
  111.                                 break
  112.                             end
  113.                         end
  114.                     end
  115.                 end
  116.             end
  117.         else
  118.             jobtimer=jobtimer+1
  119.             print("Rednet receive time-out # "..jobtimer)
  120.         end
  121.     end
  122.     --Sort items to chests
  123.     for i =1,#jobs do
  124.         table.sort(jobs, function(a,b) --sort jobs table according to closest to current coordinate
  125.                     if (a[4]-coordinate[1])^2+(a[5]-coordinate[2])^2 < (b[4]-coordinate[1])^2+(b[5]-coordinate[2])^2 then return true end
  126.                     if (a[4]-coordinate[1])^2+(a[5]-coordinate[2])^2 > (b[4]-coordinate[1])^2+(b[5]-coordinate[2])^2 then return false end
  127.             end)
  128.         moveto(jobs[1][4],jobs[1][5])
  129.         chest=peripheral.wrap("bottom")
  130.         chest.pullItems("up",jobs[1][7],jobs[1][3],jobs[1][6])
  131.         table.remove(jobs,1)
  132.     end
  133.     moveto(homecoordinate[1],homecoordinate[2])
  134.  
  135. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement