rhn

ItemSortStoreTurtle

rhn
Mar 24th, 2020
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. sides={"north","south","west","east"}
  2. --Choose which row turtle occupies
  3. if (fs.exists("choices") == false) then --Check if this is first time run, no saved choices on file
  4.     while true do
  5.             print("Which row?")
  6.  
  7.             local answer = read()
  8.             if tonumber(answer)==nill then
  9.                 print("-Wrong answer, input not a number-")
  10.             else
  11.                 local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  12.                     row=answerv
  13.                     print("Row ", row, " chosen")
  14.                     break
  15.             end
  16.     end
  17.  
  18.     while true do --Asks for side for attached inventory
  19.             print("Which direction is the back of the turtle facing?")
  20.             for i=1, #sides do
  21.                 print(i..". "..sides[i])
  22.             end
  23.             local answer = read()
  24.             if tonumber(answer)==nill then
  25.                 print("-Wrong answer, input not a number-")
  26.             else
  27.                 local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  28.                 if (0 < answerv and answerv <= 4) then
  29.                     side1=answerv
  30.                     --print("Turtle facing: ", sides[side1])
  31.                     break
  32.                 else
  33.                     print("-Wrong answer, input does not match number of sides-")
  34.                 end
  35.             end
  36.     end
  37.  
  38.  
  39. local file = fs.open("choices","w")
  40. file.writeLine("row="..tostring(row))
  41. file.writeLine("side1="..tostring(side1))
  42. file.close()
  43. end
  44. os.loadAPI("choices") -- Reads choices from file
  45. local row = choices.row
  46. local side1 = choices.side1
  47.  
  48. ---------------Functions-------------------
  49. function loader(jobs)
  50. local slotcounter=0
  51. inputChest=peripheral.wrap("front")
  52.     for i=1,#jobs do
  53.         for j=1,inputChest.size() do
  54.             inputItem=inputChest.getItemMeta(j)
  55.             if inputItem~= nil then
  56.                 --print(inputItem.name)
  57.                 --print(jobs[i][1])
  58.                 if inputItem.name == jobs[i][1] and inputItem.damage == jobs[i][2] then
  59.                     inputChest.pushItems(sides[side1],j,64)
  60.                     slotcounter=slotcounter+1
  61.                 end
  62.             end
  63.             if slotcounter==16 then
  64.                 return
  65.             end
  66.         end
  67.     end
  68. end
  69.  
  70. function empty(jobs)
  71.     local pos=0
  72.     for i=1,#jobs do
  73.         if jobs[i][3]-pos>0 then
  74.             for j=1,jobs[i][3]-pos do
  75.                 while not turtle.up() do
  76.                     sleep(1)
  77.                 end
  78.                 pos=pos+1
  79.  
  80.             end
  81.             for j=1,16 do
  82.                 turtle.select(j)
  83.                 turtle.drop()
  84.             end
  85.         elseif jobs[i][3]-pos<0 then
  86.             for j=1,pos-jobs[i][3] do
  87.                 while not turtle.down() do
  88.                     sleep(1)
  89.                 end
  90.                 pos=pos-1
  91.             end
  92.             for j=1,16 do
  93.                 turtle.select(j)
  94.                 turtle.drop()
  95.             end
  96.         end
  97.     end
  98.     for j=1,pos do
  99.         while not turtle.down() do
  100.             sleep(1)
  101.         end
  102.     end
  103.     for i=1,16 do
  104.         if turtle.getItemCount(i)>0 then
  105.                 turtle.select(i)
  106.                 turtle.dropDown()
  107.         end
  108.     end
  109. end
  110.  
  111. local function refuel()
  112. if turtle.getFuelLevel() < 200 then
  113.     turtle.down()
  114.     turtle.select(1)
  115.     while turtle.getFuelLevel() < 200 do
  116.         turtle.suckDown(64)
  117.         if turtle.refuel() then
  118.             print("Refueled to "..turtle.getFuelLevel())
  119.         else
  120.             print("Refuel failed, no fuel!")
  121.             print("Add fuel and hold down X to continue")
  122.             repeat
  123.             event,key= os.pullEvent("key")
  124.             sleep(0.5)
  125.             until key==keys.x
  126.         end
  127.     end
  128.     turtle.up()
  129. end
  130. end
  131.  
  132. local function home()
  133.     local home=false
  134.     while home==false do
  135.         if not turtle.down() then
  136.             home=true
  137.             print("home found")
  138.         end
  139.     end
  140. end
  141.  
  142. ---------------Execute-------------------
  143. -- Initialize rednet
  144. rednet.open("right")
  145. rednet.host("ItemSortCrate","row"..row)
  146. sleep(3)
  147. home()
  148.  
  149. --messageformat: {{itemname,damage,height},.....}
  150.  
  151. while true do
  152.     local senderId, message, protocol = rednet.receive()
  153.     --print(message[1][1]..", "..message[1][2].." , "..message[1][3])
  154.     loader(message)
  155.     empty(message)
  156.     refuel()
  157. end
Add Comment
Please, Sign In to add comment