Guest User

farming

a guest
Oct 21st, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. local x=0
  2. local y=0
  3. local dir=0
  4. local field=0
  5. local homeX=0
  6. local homeY=0
  7. local stop=false
  8.  
  9. --Fields (H for home):
  10. --  1       3
  11. --      H
  12. --  2       4
  13. -- Only 1 and 2 are done for now
  14.  
  15. local function harvest()
  16.     if turtle.detectDown() then
  17.         turtle.digDown()
  18.     end
  19. end
  20.  
  21. local function plant()
  22.     if not turtle.detectDown() and turtle.getItemCount(1)>1 then
  23.         turtle.select(1)
  24.         turtle.placeDown()
  25.     end
  26. end
  27.  
  28. local function turn(direction)
  29.     if direction == "left" then
  30.         turtle.turnLeft()
  31.         dir = (dir-1)%4
  32.     elseif direction == "right" then
  33.         turtle.turnRight()
  34.         dir = (dir+1)%4
  35.     end
  36. end
  37.  
  38. local function forward()
  39.     if dir == 0 then
  40.         if turtle.forward() then
  41.             x = x+1
  42.             homeX = homeX+1
  43.         end
  44.     elseif dir == 2 then
  45.         if turtle.forward() then
  46.             x = x-1
  47.             homeX = homeX-1
  48.         end
  49.     elseif dir == 1 then
  50.         if turtle.forward() then
  51.             y = y+1
  52.             homeY = homeY+1
  53.         end
  54.     elseif dir == 3 then
  55.         if turtle.forward() then
  56.             y = y-1
  57.             homeY = homeY-1
  58.         end
  59.     end
  60. end
  61.  
  62. local function setDir(newDir)
  63.     while not (newDir == dir) do
  64.         turn("right")
  65.     end
  66. end
  67.  
  68. local function move()
  69.     if x==0 and y~=0 then
  70.         turn("left")
  71.     elseif x==8 and y~=8 then
  72.         turn("right")
  73.     elseif x==8 and y==8 then
  74.         return false
  75.     end
  76.     forward()
  77.     return true
  78. end
  79.  
  80. local function returnHome()
  81.     if homeX > 0 then
  82.         setDir(2)
  83.         while homeX ~=0 do
  84.             forward()
  85.         end
  86.     elseif homeX < 0 then
  87.         setDir(0)
  88.         while homeX ~=0 do
  89.             forward()
  90.         end
  91.     end
  92.    
  93.     if homeY > 0 then
  94.         setDir(3)
  95.         while homeY ~=0 do
  96.             forward()
  97.         end
  98.     elseif homeY < 0 then
  99.         setDir(1)
  100.         while homeY ~=0 do
  101.             forward()
  102.         end
  103.     end
  104.     setDir(0)
  105.     print("Returned home")
  106. end
  107.  
  108. local function gotoField()
  109.     if homeX ~=0 or homeY ~=0 or dir~=0 then
  110.         returnHome()
  111.     end
  112.     if field == 1 then
  113.         turn("right")
  114.         forward()
  115.        
  116.         turn("left")
  117.         forward()
  118.     elseif field == 2 then
  119.         turn("left")
  120.         for i=1, 9 do
  121.             forward()
  122.         end
  123.        
  124.         turn("right")
  125.             forward()
  126.     end
  127.     -- Reset x and y
  128.     x=0
  129.     y=0
  130.     write("Starting harvesting field ")
  131.     write(field)
  132.     print("...")
  133. end
  134.  
  135. local function manageInventory()
  136.     local fuelCount
  137.     for i=3,15 do
  138.         turtle.select(i)
  139.         turtle.dropDown()
  140.     end
  141.     turtle.select(16)
  142.     if turtle.getFuelLevel() < 100 and turtle.suckUp() then
  143.         fuelCount = turtle.getItemCount()
  144.         turtle.refuel(fuelCunt-1)
  145.     end
  146. end
  147.  
  148. local function harvestLoop(fieldNum)
  149.     field = fieldNum
  150.     stop=false
  151.     turtle.select(1)
  152.     gotoField()
  153.     while not stop do
  154.         harvest()
  155.         plant()
  156.         if not move() then
  157.             stop = true
  158.         end
  159.     end
  160.     returnHome()
  161.     manageInventory()
  162. end
  163.  
  164. local function mainLoop()
  165.     harvestLoop(1)
  166.     harvestLoop(2)
  167. end
  168.  
  169. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment