Advertisement
chippydip

CC TreeFarm

Apr 19th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local width = tonumber(args[1]) or 12
  4. local height = tonumber(args[2]) or width
  5.  
  6. local start = turtle.loc
  7.  
  8. local function makeItem(name, metadata)
  9.   return { name=name, metadata=metadata }
  10. end
  11.  
  12. local treeType = 0 -- Oak
  13.  
  14. local trunk = makeItem("minecraft:log", treeType)
  15. local sapling = makeItem("minecraft:sapling", treeType)
  16. local leaves = makeItem("minecraft:leaves", 4 + treeType)
  17.  
  18. local function isMatch(a, b)
  19.   return a.name == b.name and a.metadata == b.metadata
  20. end
  21.  
  22. local function chopAndPlantOne()
  23.   -- Done if this is outside the planting area
  24.   local x = turtle.loc.x - start.x + 1
  25.   local z = turtle.loc.z - start.z + 1
  26.   if x <= 2 or x + 2 > width or z <= 2 or z + 2 > height then
  27.     return
  28.   end
  29.  
  30.   -- Done if there is already a sapling planted
  31.   local _, down = turtle.inspectDown()
  32.   if isMatch(down, sapling) then
  33.     return
  34.   end
  35.  
  36.   -- Not a sapling, so dig it out
  37.   turtle.digDown()
  38.  
  39.   -- Replant if we have extra saplings
  40.   if turtle.getItemCount(1) > 1 then
  41.     turtle.select(1)
  42.     turtle.placeDown()
  43.   end
  44. end
  45.  
  46. local function chopAndPlant(n)
  47.   for i=1,n do
  48.     turtle.digUp()
  49.     chopAndPlantOne()
  50.     turtle.suckDown()
  51.     turtle.dig()
  52.     turtle.forward()
  53.   end
  54. end
  55.  
  56. local function chopAll(n)
  57.   for i=1,n do
  58.     turtle.digUp()
  59.     turtle.digDown()
  60.     turtle.dig()
  61.     turtle.forward()
  62.   end
  63. end
  64.  
  65. local layerCount = 0
  66.  
  67. local function chopLayer()
  68.   local turnParity = layerCount + width;
  69.  
  70.   local layer = (turtle.loc.y - 1 - start.y)/3
  71.   if layer > 0 then
  72.     layerCount = layerCount + 1
  73.   else
  74.     layerCount = 0
  75.   end
  76.  
  77.   -- Chop and plant on the first layer, otherwise just shop
  78.   local chop = layer == 0 and chopAndPlant or chopAll
  79.  
  80.   for i=1,width do
  81.     local turn = (i + turnParity) % 2 == 0 and turtle.lt or turtle.rt
  82.     if i > 1 then
  83.       turn()
  84.       chop(1)
  85.       turn()
  86.     end
  87.     chop(height-1)
  88.   end
  89.  
  90.   -- About face
  91.   turtle.rt()
  92.   turtle.rt()
  93. end
  94.  
  95. local function chopUp(n)
  96.   for i=1,n do
  97.     turtle.digUp()
  98.     turtle.up()
  99.   end
  100. end
  101.  
  102. local function chopDown(n)
  103.   for i=1,n do
  104.     turtle.digDown()
  105.     turtle.down()
  106.   end
  107. end
  108.  
  109. ----------
  110. -- Main --
  111. ----------
  112.  
  113. local requiredFuel = width*height*2 + 6
  114.  
  115. chopUp(1)
  116.  
  117. while true do
  118.   -- TODO: inventory stuff
  119.  
  120.   -- Check fuel
  121.   if turtle.getFuelLevel() < requiredFuel then
  122.     print "Not enough fuel"
  123.     -- TODO: refuel
  124.     break
  125.   end
  126.  
  127.   chopUp(3)
  128.   chopLayer()
  129.  
  130.   chopDown(3)
  131.   chopLayer()
  132.  
  133.   -- stop after one pass for now
  134.   break
  135. end
  136.  
  137. chopDown(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement