Advertisement
jille_Jr

CC: Treefarm - farm.lua

May 29th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.10 KB | None | 0 0
  1.  
  2. local farmWidth -- number
  3. local farmLength -- number
  4. local farmSpacing -- number, blocks between each tree
  5.  
  6. local farmField -- table
  7.  
  8. local farmXOffset -- number
  9. local farmYOffset -- number
  10. local farmZOffset -- number
  11.  
  12. -- gets all global variables
  13. function init( global )
  14.     setfenv(1,global)
  15. end
  16.  
  17. function distance( x1,z1,x2,z2 )
  18.     local dx = x1-x2
  19.     local dz = z1-z2
  20.     return math.sqrt(dx^2+dz^2)
  21. end
  22.  
  23. function findNearestSpot( x,z )
  24.     local nearX,nearZ = x,z
  25.     local nearestDistance = math.huge
  26.     local turtleX,_,turtleZ = gps.getPosition()
  27.     local direction
  28.     -- direction is from the nearest position towards the x,z
  29.  
  30.     for xOffset = -1,1 do
  31.         for zOffset = -1,1 do
  32.             if (xOffset==0 or zOffset==0) and zOffset~=xOffset then
  33.  
  34.                 -- distance from the turtle to the tree
  35.                 local tmpDistance = distance(x+xOffset,z+zOffset,turtleX,turtleZ)
  36.                 if tmpDistance < nearestDistance then
  37.                     nearX = x + xOffset
  38.                     nearZ = z + zOffset
  39.                     nearestDistance = tmpDistance
  40.  
  41.                     if xOffset == 1 then
  42.                         direction = gps.west
  43.                     elseif xOffset == -1 then
  44.                         direction = gps.east
  45.                     elseif zOffset == 1 then
  46.                         direction = gps.north
  47.                     elseif zOffset == -1 then
  48.                         direction = gps.south
  49.                     end
  50.                 end
  51.                
  52.             end
  53.         end
  54.     end
  55.  
  56.     return nearX,nearZ,direction
  57. end
  58.  
  59. function findNearestCorner( x1,z1,x2,z2 )
  60.     local nearX,nearZ
  61.     local nearestDistance = math.huge
  62.     local turtleX,_,turtleZ = gps.getPosition()
  63.  
  64.     for x = x1,x2,x2-x1 do
  65.         for z = z1,z2,z2-z1 do
  66.            
  67.             -- distance from the turtle to the corner
  68.             local tmpDistance = distance(x,z,turtleX,turtleZ)
  69.             if tmpDistance < nearestDistance then
  70.                 nearX = x
  71.                 nearZ = z
  72.                 nearestDistance = tmpDistance
  73.             end
  74.  
  75.         end
  76.     end
  77.  
  78.     return nearX,nearZ
  79. end
  80.  
  81. -- will dig til it reaches the surface
  82. -- instead of comparing log
  83. -- more consistant and easier with a big farm
  84. -- This does move the turtle 1 block forward
  85. function chop( clearDirt,x,z )
  86.     if not x or not z then
  87.         x,_,z = gps.getPosition(1,gps.absPos.dir)
  88.     end
  89.     gps.moveForward(1,killMobs,true)
  90.     if turtle.detectUp() then
  91.         -- tree in front
  92.        
  93.         -- begin chop down the tree
  94.         -- keep count on the trees height
  95.         local height = 0
  96.         while turtle.detectUp() do
  97.             gps.moveUp(1,killMobs,true)
  98.             height = height + 1
  99.         end
  100.  
  101.         -- get back down to the "ground"
  102.         gps.moveDown(height,killMobs,true)
  103.     else
  104.         -- no tree in front
  105.     end
  106.     if clearDirt then
  107.         dig.down(killMobs,true)
  108.     end
  109. end
  110.  
  111. function chopAll( clearDirt )
  112.     if type(farmWidth) == "number"
  113.     and type(farmLength) == "number"
  114.     and type(farmSpacing) == "number"
  115.     and type(farmXOffset) == "number"
  116.     and type(farmYOffset) == "number"
  117.     and type(farmZOffset) == "number"
  118.     then
  119.  
  120.         -- find nearest corner of farm
  121.        
  122.         local homeX,homeY,homeZ = gps.getHome()
  123.  
  124.         local corner1x = homeX + farmXOffset
  125.         local corner1z = homeZ + farmZOffset
  126.         local corner2x = homeX + farmXOffset + (farmWidth-mult(farmWidth)) * farmSpacing
  127.         local corner2z = homeZ + farmZOffset + (farmLength-mult(farmLength)) * farmSpacing
  128.  
  129.         -- loop through the trees
  130.         local startX,startZ = findNearestCorner( corner1x,corner1z,corner2x,corner2z )
  131.         local stopX = corner1x; if stopX==startX then stopX = corner2x end
  132.         local stopZ = corner1z; if stopZ==startZ then stopZ = corner2z end
  133.  
  134.         for x = startX,stopX,farmSpacing*mult(stopX-startX) do
  135.             -- to alternate so you zig zag through the farm to save fuel and time
  136.             local iteration = (x-startX+farmSpacing)/farmSpacing
  137.             local A,B = startZ,stopZ
  138.             if iteration%2==0 then A,B = stopZ,startZ end
  139.             --print("i="..iteration.." start at "..A.." stop at "..B)
  140.             for z = A,B,farmSpacing*mult(B-A) do
  141.                 --write(z.." ")
  142.                 local treeX,treeZ,dir = findNearestSpot(x,z)
  143.                 gps.goto(treeX,homeY+farmYOffset,treeZ,dir)
  144.                 chop(clearDirt)
  145.             end
  146.             --print()
  147.         end
  148.     end
  149. end
  150.  
  151. -- requires dirt
  152. function replant()
  153.     if type(farmWidth) == "number"
  154.     and type(farmLength) == "number"
  155.     and type(farmSpacing) == "number"
  156.     and type(farmXOffset) == "number"
  157.     and type(farmYOffset) == "number"
  158.     and type(farmZOffset) == "number"
  159.     then
  160.  
  161.         -- obtain materials
  162.         item.followStoragePath()
  163.         item.dropoff()
  164.         item.getItems({item.types.dirt,item.types.sapling},math.abs(farmWidth*farmLength),true)
  165.         item.followStoragePath(true)
  166.  
  167.         -- find nearest corner of farm
  168.        
  169.         local homeX,homeY,homeZ = gps.getHome()
  170.  
  171.         local corner1x = homeX + farmXOffset
  172.         local corner1z = homeZ + farmZOffset
  173.         local corner2x = homeX + farmXOffset + (farmWidth-mult(farmWidth)) * farmSpacing
  174.         local corner2z = homeZ + farmZOffset + (farmLength-mult(farmLength)) * farmSpacing
  175.  
  176.         -- loop through the trees
  177.         local startX,startZ = findNearestCorner( corner1x,corner1z,corner2x,corner2z )
  178.         local stopX = corner1x; if stopX==startX then stopX = corner2x end
  179.         local stopZ = corner1z; if stopZ==startZ then stopZ = corner2z end
  180.  
  181.         for x = startX,stopX,farmSpacing*mult(stopX-startX) do
  182.             -- to alternate so you zig zag through the farm to save fuel and time
  183.             local iteration = (x-startX+farmSpacing)/farmSpacing
  184.             local A,B = startZ,stopZ
  185.             if iteration%2==0 then A,B = stopZ,startZ end
  186.             --print("i="..iteration.." start at "..A.." stop at "..B)
  187.             for z = A,B,farmSpacing*mult(B-A) do
  188.  
  189.                 -- select dirt
  190.                 local atStorage = false
  191.                 while not item.select(item.types.dirt) do
  192.                     if not atStorage then item.followStoragePath() atStorage=true end
  193.                     item.getItem(item.types.dirt,_,false)
  194.                 end
  195.                 if atStorage then item.followStoragePath(true) end
  196.  
  197.                 gps.goto(x,homeY+farmYOffset,z)
  198.                 turtle.placeDown()
  199.  
  200.                 -- select sapling
  201.                 local atStorage = false
  202.                 while not item.select(item.types.sapling) do
  203.                     if not atStorage then item.followStoragePath() atStorage=true end
  204.                     item.getItem(item.types.sapling,_,false)
  205.                 end
  206.                 if atStorage then item.followStoragePath(true) end
  207.  
  208.                 gps.goto(x,homeY+farmYOffset+1,z)
  209.                 turtle.placeDown()
  210.             end
  211.         end
  212.     end
  213.  
  214.     item.followStoragePath()
  215.     item.dropoff()
  216.     gps.goto(gps.getHome())
  217. end
  218.  
  219. function setOffset( xOffset,yOffset,zOffset )
  220.     farmXOffset = xOffset or farmXOffset
  221.     farmYOffset = yOffset or farmYOffset
  222.     farmZOffset = zOffset or farmZOffset
  223. end
  224.  
  225. function setSize( width,length,spacing )
  226.     farmWidth = width or farmWidth
  227.     farmLength = length or farmLength
  228.     farmSpacing = spacing or farmSpacing
  229. end
  230.  
  231. -- sets the field forwards to the right
  232. -- z = forward
  233. -- x = right
  234. -- state = (boolean) true/false depends if its grown/harvested
  235. function updateField( state )
  236.     if type(farmWidth) == "number"
  237.     and type(farmLength) == "number"
  238.     and type(farmSpacing) == "number"
  239.     and type(farmZOffset) == "number"
  240.     and type(farmXOffset) == "number"
  241.     then
  242.         -- reset the field
  243.         farmField = {}
  244.  
  245.         local totalWidth = (farmWidth-1) * farmSpacing + 1
  246.         local totalLength = (farmLength-1) * farmSpacing + 1
  247.  
  248.         -- loop through the width and length
  249.         for x = 1,totalWidth,farmSpacing do
  250.             for z = 1,totalLength,farmSpacing do
  251.                 -- make sure the column exists
  252.                 if not farmField[x] then farmField[x] = {} end
  253.  
  254.                 farmField[x][z] = farmField[x][z] or (state or false)
  255.             end
  256.         end
  257.  
  258.         return true
  259.     else
  260.         return false
  261.     end
  262. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement