augustclear

GPS Tree Farm

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