Advertisement
augustclear

BigTreeFarm

May 30th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.67 KB | None | 0 0
  1. local success = false
  2. local data = nil
  3.  
  4. local x = 0
  5. local y = 0
  6. local z = 0
  7. local facing = 0
  8.  
  9. local dropx = 2
  10. local dropy = 9
  11. local dropz = 254
  12.  
  13. local pathx = 7
  14.  
  15. local refuelx = 3
  16. local refuely = 10
  17. local refuelz = 251
  18.  
  19. local height = 0;
  20.  
  21. ---------------------------------------
  22. -- BASIC FUNCTIONS FOR TURTLE CONTROL -
  23. ---------------------------------------
  24.  
  25. local function gf(n)
  26.   if n==nil then
  27.     n=1
  28.   end
  29.   for i=1,n,1 do
  30.     turtle.forward()
  31.   end
  32. end
  33. local function gb(n)
  34.   if n==nil then
  35.     n=1
  36.   end
  37.   for i=1,n,1 do
  38.     turtle.back()
  39.   end
  40. end
  41. local function gu(n)
  42.   if n==nil then
  43.     n=1
  44.   end
  45.   for i=1,n,1 do
  46.     turtle.up()
  47.   end
  48. end
  49. local function gd(n)
  50.   if n==nil then
  51.     n=1
  52.   end
  53.   for i=1,n,1 do
  54.     turtle.down()
  55.   end
  56. end
  57. local function tl()
  58.   turtle.turnLeft()
  59.   if facing == 1 then
  60.     facing = 4
  61.   else
  62.     facing = facing - 1
  63.   end
  64. end
  65. local function tr()
  66.   turtle.turnRight()
  67.   if facing == 4 then
  68.     facing = 1
  69.   else
  70.     facing = facing + 1
  71.   end
  72. end
  73. local function pf()  turtle.place()       end
  74. local function pu()  turtle.placeUp()     end
  75. local function pd()  turtle.placeDown()   end
  76. local function df()  return turtle.dig()  end
  77. local function du()  turtle.digUp()       end
  78. local function dd()  turtle.digDown()     end
  79. local function sf()  turtle.suck()        end
  80. local function su(n)
  81.   if n==nil then
  82.     while turtle.suckUp() do end
  83.   else
  84.     for i=1,n do
  85.       turtle.suckUp()
  86.     end
  87.   end
  88. end
  89. local function sd(n)
  90.   if n==nil then
  91.     while turtle.suckDown() do end
  92.   else
  93.     for i=1,n do
  94.       turtle.suckDown()
  95.     end
  96.   end
  97. end
  98. local function Df()  turtle.drop()       end
  99. local function Du()  turtle.dropUp()     end
  100. local function Dd(n)
  101.   if n==nil then n=64 end
  102.   turtle.dropDown(n)
  103. end
  104. local function ss(s) turtle.select(s)    end
  105. local function cf() turtle.compare()     end
  106. local function cu() turtle.compareUp()   end
  107.  
  108. ---------------------------------------
  109. --          Building Blocks          --
  110. ---------------------------------------
  111.  
  112. local function lightUp()
  113.     redstone.setOutput("back", true)
  114.     redstone.setOutput("right", true)
  115.     redstone.setOutput("left", true)
  116.     redstone.setOutput("top", true)
  117.     redstone.setOutput("bottom", true)
  118. end
  119.  
  120. local function locate()
  121.   x,y,z = gps.locate(5)
  122.   if not x then
  123.     print("Failed to get my location!")
  124.   else
  125.     print("I am at (" .. x .. ", " .. y .. ", " .. z .. ")")
  126.   end
  127. end
  128.  
  129. local function refuel()
  130.   if turtle.getFuelLevel()<80 then
  131.     ss(fuelSlot)
  132.     turtle.refuel(1)
  133.   end
  134. end
  135.  
  136. local function changeFacing(goal)
  137.   while facing ~= goal do
  138.     tl()
  139.   end
  140. end
  141.  
  142. local function calibrate()
  143.   locate()
  144.   local tempx = x
  145.   local tempy = y
  146.   gf()
  147.   locate()
  148.   if (x - tempx) == 1 then
  149.     facing = 3
  150.   elseif (x - tempx) == -1 then
  151.     facing = 1
  152.   else
  153.     if (y - tempy) == 1 then
  154.       facing = 2
  155.     else
  156.       facing = 4
  157.     end
  158.   end
  159.   gb()
  160.   changeFacing(1)
  161. end
  162.  
  163. local function mx(n)
  164.   if n > 0 then
  165.     changeFacing(3)
  166.   elseif n < 0 then
  167.     changeFacing(1)
  168.     n = n*-1
  169.   end
  170.   for i=1,n do
  171.     if turtle.detect() then
  172.       df()
  173.     end
  174.     gf()
  175.   end
  176. end
  177.  
  178. local function my(n)
  179.   if n > 0 then
  180.     changeFacing(2)
  181.   elseif n < 0 then
  182.     changeFacing(4)
  183.     n = n*-1
  184.   end
  185.   for i=1,n do
  186.     if turtle.detect() then
  187.       df()
  188.     end
  189.     gf()
  190.   end
  191. end
  192.  
  193. local function mz(n)
  194.   if n > 0 then
  195.     for i=1,n do
  196.       if turtle.detectUp() then
  197.         du()
  198.       end
  199.       gu()
  200.     end
  201.   elseif n < 0 then
  202.     for i=1,-n do
  203.       if turtle.detectDown() then
  204.         dd()
  205.       end
  206.       gu()
  207.     end
  208.   end
  209. end
  210.  
  211. local function dropOff()
  212.   locate()
  213.   mx(-math.abs(pathx-x))
  214.   if y > dropy then
  215.     my(-math.abs(dropy-y))
  216.   elseif y < dropy then
  217.     my(math.abs(dropy-y))
  218.   end
  219.   mz(dropz-z)
  220.   locate()
  221.   mx(math.abs(dropx-x))
  222.   locate()
  223.   changeFacing(1)
  224. end
  225.  
  226. local function refuel()
  227.   if turtle.getFuelLevel()<500 then
  228.     print("Fuck")
  229.     return
  230.   end
  231. end
  232.  
  233. local function checkFull()
  234.   --TODO:Drop off when full
  235. end
  236.  
  237. local function checkSaps()
  238.   --TODO:Go back for saplings
  239. end
  240.  
  241. local function tempReload()
  242.   --TODO: Write a reload function
  243. end
  244.  
  245. local function calibrate2()
  246.   success, data = turtle.inspect()
  247.   if success then
  248.     if data.name == "minecraft:wood" then
  249.       chopTree()
  250.     end
  251.   end
  252.   calibrate()
  253. end
  254.  
  255. local function findLog()
  256.     repeat
  257.         tl()
  258.         success, data = turtle.inspect()
  259.     until data.name == "minecraft:log"
  260. end
  261.  
  262. local function chop2x2()
  263.     data = nil
  264.     repeat
  265.         height = height - 1
  266.         dd()
  267.         gd()
  268.         findLog()
  269.         df()
  270.         findLog()
  271.         df()
  272.     until height == 0
  273.     gu()
  274. end
  275.  
  276. local function chopTree()
  277.     success, data = turtle.inspect()
  278.     if data.name == "minecraft:log" then
  279.         df()
  280.         gf()
  281.         while success do
  282.             success, data = turtle.inspectUp()
  283.             if data.name == "minecraft:log" then
  284.                 du()
  285.                 gu()
  286.                 height = height + 1
  287.             else
  288.                 break
  289.             end
  290.         end
  291.         success, data = turtle.inspect()
  292.         if data.name == "minecraft:log" then
  293.             df()
  294.             gf()
  295.             du()
  296.             findLog()
  297.             df()
  298.             gf()
  299.             du()
  300.             findLog()
  301.             df()
  302.             gf()
  303.             du()
  304.             gb()
  305.         else
  306.             gd()
  307.             height = height - 1
  308.             success, data = turtle.inspect()
  309.             if data.name == "minecraft:log" then
  310.                 df()
  311.                 gf()
  312.                 findLog()
  313.                 df()
  314.                 gf()
  315.                 findLog()
  316.                 df()
  317.             end
  318.         end
  319.         if data.name == "minecraft:log" then
  320.             chop2x2()
  321.         else
  322.             repeat
  323.                 gd()
  324.                 height = height - 1
  325.             until height == 0
  326.         end
  327.     else
  328.         print("No tree found")
  329.     end
  330. end
  331.  
  332. ---------------------------------------
  333. ---------         MAIN        ---------
  334. ---------------------------------------
  335.  
  336. calibrate()
  337.  
  338. dropOff()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement