JustMark

Wheat Farmer

Jul 9th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.48 KB | None | 0 0
  1. local layerCount = nil
  2.  
  3. local currentPosition = {
  4.     x = 0;
  5.     y = 0;
  6.     z = 0;
  7. }
  8.  
  9. local currentDirection = 3
  10.  
  11. local persisted = false
  12.  
  13. function readData(name)
  14.     if fs.exists('/data/'..name) then
  15.         local handle = fs.open('/data/'..name, 'r')
  16.         local stuff = handle.readAll()
  17.         handle.close()
  18.         return textutils.unserialize(stuff)
  19.     end
  20.  
  21.     return
  22. end
  23.  
  24. function writeData(name, data)
  25.     if not fs.exists('/data') then
  26.         fs.makeDir('/data')
  27.     end
  28.  
  29.     local handle = fs.open('/data/'..name, 'w')
  30.     handle.write(textutils.serialize(data))
  31.     handle.close()
  32. end
  33.  
  34. function updatePosition(deltaX, deltaY, deltaZ)
  35.     currentPosition.x = currentPosition.x + deltaX
  36.     currentPosition.y = currentPosition.y + deltaY
  37.     currentPosition.z = currentPosition.z + deltaZ
  38.  
  39.     writeData('position', currentPosition)
  40. end
  41.  
  42. function updateDirection(deltaD)
  43.     currentDirection = (currentDirection + deltaD - 1) % 4 + 1
  44.  
  45.     writeData('direction', currentDirection)
  46. end
  47.  
  48. function updateLayerCount(layers)
  49.     layerCount = layers
  50.  
  51.     writeData('layerCount', layerCount)
  52. end
  53.  
  54. function loadData()
  55.     local savedLayerCount = readData('layerCount')
  56.     local savedPosition = readData('position')
  57.     local savedDirection = readData('direction')
  58.  
  59.     if (savedLayerCount ~= nil) then
  60.         layerCount = savedLayerCount or layerCount
  61.         currentPosition = savedPosition or currentPosition
  62.         currentDirection = savedDirection or currentDirection
  63.  
  64.         persisted = true
  65.     end
  66. end
  67.  
  68. function persist()
  69.     loadData()
  70.  
  71.     if persisted then
  72.         if currentDirection == 1 then
  73.             turnRight()
  74.         elseif currentDirection == 3 then
  75.             turnLeft()
  76.         elseif currentDirection == 4 then
  77.             turnRight()
  78.             turnRight()
  79.         end
  80.  
  81.         if currentPosition.x > 0 then
  82.             moveForward(currentPosition.x)
  83.         elseif currentPosition.x < 0 then
  84.             moveBack(-currentPosition.x)
  85.         end
  86.  
  87.         turnLeft()
  88.  
  89.         if currentPosition.z > 0 then
  90.             moveForward(currentPosition.z)
  91.         elseif currentPosition.z < 0 then
  92.             moveBack(-currentPosition.z)
  93.         end
  94.  
  95.         if currentPosition.y > 0 then
  96.             moveDown(currentPosition.y)
  97.         elseif currentPosition.y < 0 then
  98.             moveUp(-currentPosition.y)
  99.         end
  100.  
  101.         storeItems()
  102.     else
  103.         turnRight()
  104.         turtle.suck(64)
  105.         turtle.suck(16)
  106.         turnLeft()
  107.     end
  108. end
  109.  
  110. function moveForward(n)
  111.     n = n or 1
  112.  
  113.     for i = 1, n do
  114.         while not turtle.forward() do
  115.             term.clear()
  116.             print('turtle is stuck, please remove the block in front.')
  117.             sleep(1)
  118.         end
  119.         term.clear()
  120.  
  121.         local dX = (currentDirection - 3) * ((currentDirection + 1) % 2)
  122.         local dZ = (currentDirection - 2) * (currentDirection % 2)
  123.  
  124.         updatePosition(dX, 0, dZ)
  125.     end
  126. end
  127.  
  128. function moveBack(n)
  129.     n = n or 1
  130.  
  131.     for i = 1, n do
  132.         while not turtle.back() do
  133.             term.clear()
  134.             print('turtle is stuck, please remove the block behind.')
  135.             sleep(1)
  136.         end
  137.         term.clear()
  138.  
  139.         local dX = -(currentDirection - 3) * ((currentDirection + 1) % 2)
  140.         local dZ = -(currentDirection - 2) * (currentDirection % 2)
  141.  
  142.         updatePosition(dX, 0, dZ)
  143.     end
  144. end
  145.  
  146. function moveUp(n)
  147.     n = n or 1
  148.  
  149.     for i = 1, n do
  150.         while not turtle.up() do
  151.             term.clear()
  152.             print('turtle is stuck, please remove the block above.')
  153.             sleep(1)
  154.         end
  155.         term.clear()
  156.  
  157.         updatePosition(0, 1, 0)
  158.     end
  159. end
  160.  
  161. function moveDown(n)
  162.     n = n or 1
  163.  
  164.     for i = 1, n do
  165.         while not turtle.down() do
  166.             term.clear()
  167.             print('turtle is stuck, please remove the block underneath.')
  168.             sleep(1)
  169.         end
  170.         term.clear()
  171.  
  172.         updatePosition(0, -1, 0)
  173.     end
  174. end
  175.  
  176. function turnLeft()
  177.     turtle.turnLeft()
  178.     updateDirection(-1)
  179. end
  180.  
  181. function turnRight()
  182.     turtle.turnRight()
  183.     updateDirection(1)
  184. end
  185.  
  186. function refuel(level, overrideMoveCount)
  187.     local moveCount = nil
  188.  
  189.     if overrideMoveCount~= nil then
  190.         moveCount = overrideMoveCount
  191.     else
  192.         moveCount = 98 + level*6
  193.     end
  194.    
  195.     fuelCount = math.ceil((moveCount - turtle.getFuelLevel()) / 80)
  196.  
  197.     if fuelCount <= 0 then
  198.         return
  199.     end
  200.  
  201.     turnLeft()
  202.     turtle.select(16)
  203.  
  204.     while fuelCount > 0 do
  205.         turtle.suck(fuelCount)
  206.         fuelCount = fuelCount - turtle.getItemCount(16)
  207.         turtle.refuel()
  208.        
  209.         if fuelCount ~= 0 then
  210.             term.clear()
  211.             print('missing '..fuelCount..' coal, please put coal in the chest')
  212.             sleep(1)
  213.         else
  214.             term.clear()
  215.             break
  216.         end
  217.     end
  218.    
  219.     turtle.select(1)
  220.     turnRight()
  221. end
  222.  
  223. function dropByName(name, dropCallback)
  224.     dropCallback = dropCallback or turtle.drop
  225.  
  226.     for i = 1, 16 do
  227.         local data = turtle.getItemDetail(i)
  228.  
  229.         if (data ~= nil) and (data.name == 'minecraft:'..name) then
  230.             turtle.select(i)
  231.             while not dropCallback() do
  232.                 term.clear()
  233.                 print('chest is full, please empty it.')
  234.             end
  235.         end
  236.     end
  237. end
  238.  
  239. function storeItems()
  240.     dropByName('wheat', turtle.dropDown)
  241.  
  242.     turnLeft()
  243.     dropByName('wheat_seeds', turtle.drop)
  244.     turtle.select(1)
  245.     turtle.suck(64)
  246.     turtle.suck(16)
  247.  
  248.     turnLeft()
  249. end
  250.  
  251. function selectSeeds()
  252.     for i = 1, 16 do
  253.         local data = turtle.getItemDetail(i)
  254.  
  255.         if (data ~= nil) and data.name == 'minecraft:wheat_seeds' then
  256.             turtle.select(i)
  257.  
  258.             return
  259.         end
  260.     end
  261. end
  262.  
  263. function replantOne()
  264.     local _, data = turtle.inspectDown()
  265.  
  266.     if (data.state == nil) or (data.state.age == nil) then
  267.         selectSeeds()
  268.         turtle.placeDown()
  269.     elseif data.state.age == 7 then
  270.         turtle.digDown()
  271.         selectSeeds()
  272.         turtle.placeDown()
  273.     end
  274. end
  275.  
  276. function replantLayer(layer)
  277.     refuel(layer)
  278.  
  279.     local y = (layer-1) * 3
  280.    
  281.     moveUp(y)
  282.     moveForward()
  283.  
  284.     for x = 1, 9 do
  285.         for z = 1, 8 do
  286.             replantOne()
  287.             moveForward()
  288.         end
  289.  
  290.         replantOne()
  291.  
  292.         if x ~= 9 then
  293.             if x % 2 == 1 then
  294.                 turnRight()
  295.                 moveForward()
  296.                 turnRight()
  297.             else
  298.                 turnLeft()
  299.                 moveForward()
  300.                 turnLeft()
  301.             end
  302.         end
  303.     end
  304.  
  305.     turnLeft()
  306.     moveForward(8)
  307.     turnLeft()
  308.     moveForward(9)
  309.     moveDown(y)
  310.  
  311.     storeItems()
  312. end
  313.  
  314. function replantAll()
  315.     for i = 1, layerCount do
  316.         replantLayer(i)
  317.     end
  318. end
  319.  
  320. function waitToGrow()
  321.     repeat
  322.         local _, data = turtle.inspectDown()
  323.         sleep(1)
  324.     until (data.state == nil) or (data.state.age == 7) or (data.state.age == nil)
  325. end
  326.  
  327. function watchCrops()
  328.     refuel(nil, 2)
  329.     moveForward()
  330.     waitToGrow()
  331.     moveBack()
  332.     turtle.select(1)
  333. end
  334.  
  335. function main()
  336.     term.clear()
  337.  
  338.     persist()
  339.  
  340.     if not persisted then
  341.         print('Enter layer count: ')
  342.         local input = nil
  343.  
  344.         while true do
  345.             input = tonumber(read())
  346.  
  347.             if (input == nil) or (math.floor(input) ~= input) then
  348.                 print('input should be an integer.')
  349.             else
  350.                 updateLayerCount(input)
  351.                 term.clear()
  352.                 break
  353.             end
  354.         end
  355.     end
  356.    
  357.     while true do
  358.         watchCrops()
  359.         replantAll()
  360.     end
  361. end
  362.  
  363. main()
Add Comment
Please, Sign In to add comment