Advertisement
mandydax

Tree Farm

Feb 7th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local lastTurn = 1 -- direction of last turn (0=left, 1=right)
  2. local size = 11 -- square size of farm
  3. local woodSlot = 16 -- slot comparison wood is kept
  4. local maxStack = 4 -- maximum number of items to keep in slots
  5. local sleepTimer = 300 -- time between farmings in seconds
  6. local x,y,z = gps.locate(5) -- turtle position on startup
  7. local hx,hy,hz = 306, 49, 130 -- turtle home position
  8. local debugMessages = true -- print debug messages
  9. local startTimer = 10 -- seconds to wait on startup before running
  10.  
  11. --debug print function
  12. function db(mesg)
  13.   if debugMessages then
  14.     print(mesg)
  15.   end
  16. end
  17.  
  18. --check where home log is and then face south
  19. function checkFacing()
  20.   db("Checking facing")
  21.   turtle.select(woodSlot)
  22.   while not turtle.compare() do
  23.     left()
  24.   end
  25.   db("Home log found")
  26.   left()
  27. end
  28.  
  29. function goHome()
  30.   db("Going Home")
  31.   x,y,z = gps.locate(5)
  32.   db("hxyz="..hx..","..hy..","..hz)
  33.   db(" xyz="..x..","..y..","..z)
  34.   forward()
  35.   nx,ny,nz = gps.locate(5)
  36.   db("nxyz="..nx..","..ny..","..nz)
  37.   --orient to west
  38.   if nx > x then
  39.     db("Looking east")
  40.     left()
  41.     left()
  42.   elseif nz > z then
  43.     db("Looking south")
  44.     right()
  45.   elseif nz < z then
  46.     db("Looking north")
  47.     left()
  48.   end
  49.   --go to home y level
  50.   db("Going up")
  51.   for i=1,hy-ny do
  52.     up()
  53.   end
  54.   --go to home x position
  55.   for i=1,nx-hx do
  56.     forward()
  57.   end
  58.   right() --orient to north
  59.   --go to home z position
  60.   for i=1,nz-hz do
  61.     forward()
  62.   end
  63.   --verify is home
  64.   checkFacing()
  65. end
  66.  
  67. function upperLayer()
  68.   db("Running upperLayer")
  69.   for i=1,size do
  70.     for j=1,size-1 do
  71.       forward()
  72.       dig()
  73.     end
  74.     if i < size then
  75.       gimbal()
  76.     end
  77.   end
  78.   left()
  79.   left()
  80.   nextLayer()
  81. end
  82.  
  83. function bottomLayer()
  84.   db("Running bottomLayer")
  85.   for i=1,size do
  86.     for j=1,size-1 do
  87.       forward()
  88.       turtle.digUp()
  89.       if turtle.compareDown() then
  90.         plantSapling()
  91.       end
  92.     end
  93.     if i < size then
  94.       gimbal()
  95.     end
  96.   end
  97.   dropExtras()
  98.   --[[for i=1,6 do
  99.     up()
  100.   end
  101.   right()
  102.   for i=1,size-1 do
  103.     forward()
  104.     right()
  105.     forward()
  106.     left()
  107.   end
  108.   left()
  109.   lastTurn = 1 ]]
  110.   goHome()
  111. end
  112.  
  113. -- turns the proper direction for next row
  114. function gimbal()
  115.   --db("Gimbaling")
  116.   if lastTurn == 0 then
  117.     right()
  118.     forward()
  119.     dig()
  120.     right()
  121.   else
  122.     left()
  123.     forward()
  124.     dig()
  125.     left()
  126.   end
  127.   lastTurn = math.abs(lastTurn-1)
  128.   --db("Set lastTurn to "..lastTurn)
  129. end
  130.  
  131. -- breaks the block below and places a random sapling
  132. function plantSapling()
  133.   db("Planting sapling")
  134.   turtle.digDown()
  135.   turtle.select(math.random(1,15))
  136.   while turtle.placeDown() == false do
  137.     turtle.select(math.random(1,15))
  138.   end
  139.   turtle.select(woodSlot)
  140. end
  141.  
  142. -- drops excess inventory
  143. function dropExtras()
  144.   db("dropping excess inventory")
  145.   for i=1,16 do
  146.     turtle.select(i)
  147.     while turtle.getItemCount(i) > maxStack do
  148.       turtle.dropDown(math.ceil(maxStack/4))
  149.     end
  150.   end
  151. end
  152.  
  153. function nextLayer()
  154.   db("Going down to next layer")
  155.   for i=1,3 do
  156.     down()
  157.   end
  158. end
  159.  
  160. function forward()
  161.   turtle.dig()
  162.   turtle.forward()
  163. end
  164.  
  165. function down()
  166.   turtle.digDown()
  167.   turtle.down()
  168. end
  169.  
  170. function up()
  171.   turtle.digUp()
  172.   turtle.up()
  173. end
  174.  
  175. function dig()
  176.   turtle.digUp()
  177.   turtle.digDown()
  178. end
  179.  
  180. function right()
  181.   turtle.turnRight()
  182. end
  183.  
  184. function left()
  185.   turtle.turnLeft()
  186. end
  187.  
  188. -- main program
  189. db("Waiting "..startTimer.." seconds to start")
  190. for i=startTimer,1,-1 do
  191.   sleep(1)
  192.   db(i)
  193. end
  194. -- checks if in home position then checks facing or goes home
  195. if x==hx and y==hy and z==hz then
  196.   checkFacing()
  197. else
  198.   goHome()
  199. end
  200. while true do
  201.   print("Fuel level: "..turtle.getFuelLevel())
  202.   turtle.select(woodSlot)
  203.   for i=1,2 do
  204.     upperLayer()
  205.   end
  206.   bottomLayer()
  207.   db("Sleeping for "..sleepTimer.."s")
  208.   for i=math.ceil(sleepTimer/30),1,-1 do
  209.     db(i*30)
  210.     sleep(30)
  211.   end
  212. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement