Hydrotronics

Farmer ComputerCraft

Nov 18th, 2020 (edited)
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | None | 0 0
  1. _G["xPos"] = 0
  2. _G["yPos"] = 0
  3. _G["dir"] = 0
  4. _G["xSize"] = 0
  5. _G["ySize"] = 0
  6. _G["requiredFuel"] = 0
  7. _G["farmTime"] = 0
  8.  
  9. args = {...}
  10.  
  11. if args[0] == nil then
  12.     xSize = 9
  13.     ySize = 9
  14.     farmTime = 60
  15. elseif args[0] ~= nil then
  16.     if #args > 1 then
  17.         xSize = tonumber(args[1])
  18.         ySize = tonumber(args[2])
  19.         farmTime = tonumber(args[3])
  20.         if farmTime == nil then
  21.             farmTime = 60
  22.         end
  23.         if xSize == nil or ySize == nil then
  24.             print("Usage: "..shell.getRunningProgram().." [<xSize> <ySize>] [secondsToWait]")
  25.             return
  26.         end
  27.     else
  28.         farmTime = tonumber(args[1])
  29.         if farmTime == nil then
  30.             print("Usage: "..shell.getRunningProgram().." [secondsToWait]")
  31.         end
  32.     end
  33. end
  34.  
  35. requiredFuel = xSize * ySize + xSize + ySize
  36.  
  37. function moveForward()
  38.     while not turtle.forward() do
  39.         sleep(1)
  40.     end
  41.     if dir == 0 then
  42.         yPos = yPos + 1
  43.     elseif dir == 1 then
  44.         xPos = xPos + 1
  45.     elseif dir == 2 then
  46.         yPos = yPos - 1
  47.     elseif dir == 3 then
  48.         xPos = xPos - 1
  49.     end
  50. end
  51.  
  52. function moveBack()
  53.     while not turtle.back() do
  54.         sleep(1)
  55.     end
  56.     if dir == 0 then
  57.         yPos = yPos - 1
  58.     elseif dir == 1 then
  59.         xPos = xPos - 1
  60.     elseif dir == 2 then
  61.         yPos = yPos + 1
  62.     elseif dir == 3 then
  63.         xPos = xPos + 1
  64.     end
  65. end
  66.  
  67. function turnLeft()
  68.     turtle.turnLeft()
  69.     dir = (dir + 1) % 4
  70. end
  71. function turnRight()
  72.     turtle.turnRight()
  73.     dir = (dir - 1) % 4
  74. end
  75. function faceDirection(n)
  76.     while dir ~= n do
  77.         if dir < n then
  78.             if math.abs(dir - n) < 2 then
  79.                 turnLeft()
  80.             else
  81.                 turnRight()
  82.             end
  83.         else
  84.             if math.abs(dir - n) < 2 then
  85.                 turnRight()
  86.             else
  87.                 turnLeft()
  88.             end
  89.         end
  90.     end
  91. end
  92. function returnHome()
  93.     faceDirection(2)
  94.     for i = 1,yPos do
  95.         turtle.forward()
  96.     end
  97.     faceDirection(3)
  98.     for i = 1,xPos do
  99.         turtle.forward()
  100.     end
  101.     faceDirection(0)
  102.     xPos = 0
  103.     yPos = 0
  104. end
  105.  
  106. function dumpItems()
  107.     for i = 1,16 do
  108.         if turtle.getItemCount(i) > 0 then
  109.             if string.find(turtle.getItemDetail(i).name, "charcoal") == nil then
  110.                 turtle.select(i)
  111.                 turtle.dropDown()
  112.             end
  113.         end
  114.     end
  115. end
  116. function placeCrop()
  117.     for i = 1,16 do
  118.         if turtle.getItemCount(i) > 0 then
  119.             if string.find(turtle.getItemDetail(i).name,"seed") then
  120.                 turtle.select(i)
  121.                 turtle.placeDown()
  122.                 break
  123.             end
  124.         end
  125.     end
  126. end
  127. function harvestCrop()
  128.     found, block = turtle.inspectDown()
  129.     if found then
  130.         if block["state"]["age"] then
  131.             if block["state"]["age"] == 7 then
  132.                 turtle.digDown()
  133.                 placeCrop()
  134.             end
  135.         end
  136.     end
  137. end
  138. function awaitFuel()
  139.     rs.setOutput("top", true)
  140.     for i = 1,16 do
  141.         turtle.select(i)
  142.         turtle.refuel()
  143.         if turtle.getFuelLevel() >= requiredFuel then
  144.             break
  145.         end
  146.     end
  147.     rs.setOutput("top", false)
  148. end
  149.  
  150. while true do
  151.     if turtle.getFuelLevel() >= requiredFuel then
  152.         moveForward()
  153.         harvestCrop()
  154.         for x = 1,9 do
  155.             for y = 1,8 do
  156.                 moveForward()
  157.                 harvestCrop()
  158.             end
  159.             if x ~= 9 then
  160.                 faceDirection(1)
  161.                 moveForward()
  162.                 harvestCrop()
  163.                 if x % 2 == 0 then
  164.                     faceDirection(0)
  165.                 else
  166.                     faceDirection(2)
  167.                 end
  168.             end
  169.         end
  170.         returnHome()
  171.    
  172.         parallel.waitForAll(
  173.             function()
  174.                 sleep(farmTime)
  175.             end,
  176.             function()
  177.                 dumpItems()
  178.             end)
  179.         else
  180.         awaitFuel()
  181.     end
  182. end
Advertisement
Add Comment
Please, Sign In to add comment