Advertisement
Guest User

farm

a guest
Jul 22nd, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. -- Left chest: Seed
  2. -- Back chest: Product
  3. -- Right chest: Fuel
  4.  
  5. -- Constants
  6. local seedSlot = 15
  7. local chestSlot = 16
  8. local NORTH = 1
  9. local EAST = 2
  10. local SOUTH = 3
  11. local WEST = 4
  12.  
  13. -- States
  14. local loop = true
  15. local power = turtle.getFuelLevel()
  16. local selected = 1 -- Top left
  17. turtle.select(selected)
  18. local facing = NORTH
  19. local position = {}
  20. position.x = 1
  21. position.y = 1
  22.  
  23. -- Turn right, incrementing facing
  24. local function TurnRight()
  25.   facing = facing + 1
  26.   if (facing > 4) then
  27.     facing = 1
  28.   end
  29.   turtle.turnRight()
  30. end
  31.  
  32. -- Turn left, decrementing facing
  33. local function TurnLeft()
  34.   facing = facing - 1
  35.   if (facing < 1) then
  36.     facing = 4
  37.   end
  38.   turtle.turnRight()
  39. end
  40.  
  41. -- Move forward, increment/decrement facing coordinate position
  42. local function MoveForward()
  43.   if (facing == NORTH) then
  44.     position.y = position.y + 1
  45.   elseif (facing == EAST) then
  46.     position.x = position.x + 1
  47.   elseif (facing == SOUTH) then
  48.     position.y = position.y - 1
  49.   else
  50.     position.x = position.x - 1
  51.   end
  52.   turtle.forward()
  53. end
  54.  
  55. -- Find the direction towards the first farm plot
  56. local function Initialize()
  57.   local facingChest = true
  58.   turtle.select(chestSlot)
  59.   facingChest = turtle.compare()
  60.   while (facingChest) do
  61.     turtle.turnRight()
  62.     turtle.select(chestSlot)
  63.     facingChest = turtle.compare()
  64.   end
  65.   facing = 1
  66. end
  67.  
  68. -- Plant a crop, harvesting any existing plant and plowing
  69. local function PlantCrop()
  70.   turtle.digDown()
  71. end
  72.  
  73. local function Farm(x, y)
  74.   --Initialize()
  75.   MoveForward()
  76.  
  77.   while (loop) do
  78.     print(string.format(
  79.       "CURRENT POS (%s, %s)",
  80.       position.x, position.y))
  81.      
  82.     if (position.y < y) then
  83.       MoveForward()
  84.     elseif (position.x < x) then
  85.       TurnRight()
  86.       TurnRight()
  87.       while (position.x > 1) do
  88.         MoveForward()
  89.       end
  90.       TurnLeft()
  91.       MoveForward()
  92.       TurnLeft()
  93.     else
  94.       loop = false
  95.     end
  96.   end
  97. end
  98.  
  99. Farm(3, 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement