Advertisement
mathiaas

farming

Dec 14th, 2019 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Farming script V2 by Mathias
  2.  
  3. -- Variables
  4. crop = "wheat"
  5. seed = "wheat_seeds"
  6. xpos = 1
  7. ypos = 1
  8. xlock = false
  9. ylock = false
  10.  
  11. -- Functions
  12.  
  13. function organize()
  14.   local inventory = {}
  15.   local slot
  16.   for i = 1,16 do
  17.     if (turtle.getItemDetail(i)) then
  18.       data = turtle.getItemDetail(i)
  19.       data["slot"] = i
  20.       data.name = data.name:gsub("minecraft:","")
  21.       if (data.name == seed) then
  22.         table.insert(inventory,data)
  23.       end
  24.     end
  25.   end
  26.   for key, value in pairs(inventory) do
  27.     if (key ~= 1 and inventory[1].count < 64) then
  28.       turtle.select(inventory[key].slot)
  29.       turtle.transferTo(inventory[1].slot)
  30.       inventory[1].count = inventory[1].count + inventory[key].count
  31.     end
  32.   end
  33. end
  34.  
  35. function depo()
  36.   turtle.turnLeft()
  37.  
  38.   if (turtle.getItemDetail(1)) then
  39.     local data = turtle.getItemDetail(1)
  40.     data.name = data.name:gsub("minecraft:","")
  41.     if (data.name ~= seed) then
  42.       turtle.select(1)
  43.       turtle.drop()
  44.     end
  45.   end
  46.  
  47.   for i = 2,16 do
  48.     if (turtle.getItemDetail(i)) then
  49.       data = turtle.getItemDetail(i)
  50.       data.name = data.name:gsub("minecraft:","")
  51.       turtle.select(i)
  52.       if (data.name == seed) then
  53.         turtle.transferTo(1)
  54.       else
  55.         turtle.drop()
  56.       end
  57.     end
  58.   end
  59.    
  60.   turtle.turnRight()
  61. end
  62.  
  63. function check()
  64.   if (turtle.inspectDown()) then
  65.    local block, type = turtle.inspectDown()
  66.    type.name = type.name:gsub("minecraft:","")
  67.    return type
  68.   else
  69.    local type = {name = "empty",metadata = 0}
  70.    return type
  71.   end
  72.  
  73.   print(block.name)
  74. end
  75.  
  76. function plant()
  77.   turtle.digDown()
  78.   for i = 1,16 do
  79.     if (turtle.getItemDetail(i)) then
  80.       local data = turtle.getItemDetail(i)
  81.       data.name = data.name:gsub("minecraft:","")
  82.       if (data.name == seed) then
  83.         turtle.select(i)
  84.         turtle.placeDown()
  85.         break
  86.       end
  87.     end    
  88.   end
  89. end
  90.  
  91. function checkDown()
  92.   turtle.down()
  93.   result = check()
  94.   turtle.up()
  95.   if (result.name == "dirt") then
  96.     plant()
  97.   elseif (result.name == "water" or result.name == "flowing_water") then
  98.     -- keep moving
  99.   else
  100.     if (xlock == false) then
  101.       xlock = true
  102.     else
  103.       ylock = true
  104.     end
  105.   end  
  106. end
  107.  
  108.  
  109. function decisions()
  110.   result = check()
  111.  
  112.   if (turtle.getFuelLevel() <= 0) then
  113.     while (true) do
  114.       turtle.turnLeft()
  115.     end
  116.   elseif (result.name == crop) then
  117.     if (result.metadata == 7) then
  118.       plant()
  119.     end
  120.   elseif (result.name == "empty") then
  121.     checkDown()    
  122.   else
  123.     print("Confusion while making decisions")
  124.   end
  125. end
  126.  
  127. function declareGrid()
  128.   while (xlock == false) do
  129.     decisions()
  130.     if (xlock == false) then
  131.       turtle.back()
  132.       xpos = xpos + 1
  133.     else
  134.       turtle.forward()
  135.       xpos = xpos - 1      
  136.     end
  137.   end
  138.  
  139.   turtle.turnLeft()
  140.  
  141.   while (ylock == false) do
  142.     decisions()
  143.     if (ylock == false) then
  144.       turtle.back()
  145.       ypos = ypos + 1
  146.     else
  147.       ypos = ypos - 1
  148.       print("Farming a "..ypos.."x"..xpos.." area")
  149.       for i = 1, ypos do
  150.         turtle.forward()
  151.       end
  152.      
  153.       turtle.turnRight()
  154.      
  155.       for i = 2, xpos do
  156.         turtle.forward()
  157.       end
  158.     end
  159.   end
  160. end
  161.  
  162. function turn(direction)
  163.   if (direction % 2 == 0) then
  164.     turtle.turnRight()
  165.     turtle.back()
  166.     turtle.turnRight()
  167.   else
  168.     turtle.turnLeft()
  169.     turtle.back()
  170.     turtle.turnLeft()
  171.   end
  172. end
  173.  
  174. function goHome(_xpos, _ypos)
  175.   if (_ypos % 2 == 0) then
  176.     turtle.turnLeft()
  177.     for i = 2,_xpos do
  178.       turtle.back()
  179.     end
  180.      turtle.turnRight()
  181.   else
  182.     turtle.turnRight()
  183.     for i = 2,_ypos do
  184.       turtle.back()
  185.     end
  186.     turtle.turnRight()
  187.     for i = 2, _xpos do
  188.       turtle.back()
  189.     end
  190.     turtle.turnLeft()
  191.     turtle.turnLeft()
  192.   end
  193. end
  194.  
  195. function xpath(_xpos)
  196.   for i = _xpos, (xpos-1) do
  197.     decisions()
  198.     if (_xpos ~= xpos) then
  199.       turtle.back()
  200.     end
  201.   end
  202.   decisions()
  203. end
  204.  
  205. function pathing()
  206.     for i = 1, ypos do
  207.       xpath(1)
  208.       if (i ~= ypos) then
  209.         turn(i)
  210.       end
  211.     end
  212. end
  213.  
  214. function Main()
  215.   declareGrid()
  216.   depo()
  217.   while (true) do
  218.     pathing()
  219.     goHome(xpos,ypos)
  220.     organize()
  221.     depo()
  222.     sleep(75)
  223.   end
  224. end
  225.  
  226. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement