Advertisement
kinderrs

Cotton Turtle Farmer

Nov 21st, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. local xPos, zPos = 0,0
  2. local xDir, zDir = 0,1
  3. local depth = 0
  4.  
  5. local function turnLeft()
  6.     turtle.turnLeft()
  7.     xDir, zDir = -zDir, xDir
  8. end
  9.  
  10. local function turnRight()
  11.     turtle.turnRight()
  12.     xDir, zDir = zDir, -xDir
  13. end
  14.  
  15. local function tryForwards()
  16.  
  17. -- TODO:  check fuel levels
  18.  
  19.    while not turtle.forward() do
  20.       if turtle.detect() then
  21.          --blocked and we're a pacifist so wait
  22.          sleep(0.5)
  23.       end
  24.    end
  25.  
  26.    xPos = xPos + xDir
  27.    zPos = zPos + zDir
  28.    return true
  29. end
  30.  
  31. function goTo( x, y, z, xd, zd )
  32.     while depth > y do
  33.         if turtle.up() then
  34.             depth = depth - 1
  35.         elseif turtle.digUp() or turtle.attackUp() then
  36.             collect()
  37.         else
  38.             sleep( 0.5 )
  39.         end
  40.     end
  41.  
  42.     if xPos > x then
  43.         while xDir ~= -1 do
  44.             turnLeft()
  45.         end
  46.         while xPos > x do
  47.             if turtle.forward() then
  48.                 xPos = xPos - 1
  49.             elseif turtle.dig() or turtle.attack() then
  50.                 collect()
  51.             else
  52.                 sleep( 0.5 )
  53.             end
  54.         end
  55.     elseif xPos < x then
  56.         while xDir ~= 1 do
  57.             turnLeft()
  58.         end
  59.         while xPos < x do
  60.             if turtle.forward() then
  61.                 xPos = xPos + 1
  62.             elseif turtle.dig() or turtle.attack() then
  63.                 collect()
  64.             else
  65.                 sleep( 0.5 )
  66.             end
  67.         end
  68.     end
  69.    
  70.     if zPos > z then
  71.         while zDir ~= -1 do
  72.             turnLeft()
  73.         end
  74.         while zPos > z do
  75.             if turtle.forward() then
  76.                 zPos = zPos - 1
  77.             elseif turtle.dig() or turtle.attack() then
  78.                 collect()
  79.             else
  80.                 sleep( 0.5 )
  81.             end
  82.         end
  83.     elseif zPos < z then
  84.         while zDir ~= 1 do
  85.             turnLeft()
  86.         end
  87.         while zPos < z do
  88.             if turtle.forward() then
  89.                 zPos = zPos + 1
  90.             elseif turtle.dig() or turtle.attack() then
  91.                 collect()
  92.             else
  93.                 sleep( 0.5 )
  94.             end
  95.         end
  96.     end
  97.    
  98.     while depth < y do
  99.         if turtle.down() then
  100.             depth = depth + 1
  101.         elseif turtle.digDown() or turtle.attackDown() then
  102.             collect()
  103.         else
  104.             sleep( 0.5 )
  105.         end
  106.     end
  107.    
  108.     while zDir ~= zd or xDir ~= xd do
  109.         turnLeft()
  110.     end
  111. end
  112.  
  113. function findSeeds()
  114.   for n=1,16 do
  115.     local slotData = turtle.getItemDetail(n)
  116.     if slotData then
  117.        if (slotData.name == "Natura:barley.seed") then
  118.           turtle.select(n)
  119.        end
  120.     end
  121.   end
  122. end
  123.  
  124. function inspectCrop()
  125.    if turtle.detectDown() then
  126.      local success, data = turtle.inspectDown()
  127.      if success then
  128.        if ((data.name == "Natura:N Crops") and (data.metadata == 8)) then
  129.           turtle.digDown()
  130.           findSeeds()
  131.           turtle.placeDown()
  132.        end
  133.      end
  134.    end
  135. end
  136.  
  137. function advanceRow()
  138.   if xPos % 2 == 0 then
  139.     turnRight()
  140.     tryForwards()
  141.     turnRight()
  142.   else
  143.     turnLeft()
  144.     tryForwards()
  145.     turnLeft()
  146.   end
  147. end
  148.  
  149. function walkRow()
  150.   for n=1,6 do
  151.     tryForwards()
  152.     inspectCrop()
  153.   end
  154. end
  155.  
  156. function turnAround()
  157.   turnLeft()
  158.   turnLeft()
  159. end
  160.  
  161. if turtle.up() then
  162.   depth = depth - 1
  163. end
  164.  
  165.  
  166. -- currently this only works on my test farm ;-)
  167. for i=1,8 do
  168.   walkRow()
  169.   advanceRow()
  170.   print("My position is: X"..xPos.." Z:"..zPos)
  171. end
  172. goTo(0,0,0,0,-1)
  173. turnAround()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement