Lion4ever

GiantTree

Apr 19th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.70 KB | None | 0 0
  1. local x,y,z,fx,fz = 0,0,0,1,0
  2. local map = {[0]={[0]={[0]=true}}}
  3.  
  4. local needReturn = false
  5.  
  6. local function lt()
  7.   turtle.turnLeft()
  8.   fx,fz = fz,-fx
  9. end
  10.  
  11. local function rt()
  12.   turtle.turnRight()
  13.   fx,fz = -fz,fx
  14. end
  15.  
  16. local function fd(n)
  17.   for i=1,n do
  18.     repeat
  19.     until turtle.forward()
  20.     x = x + fx
  21.     z = z + fz
  22.   end
  23. end
  24.  
  25. local function tset(t,i1,i2,i3,v)
  26.  if not t[i1] then
  27.    t[i1] = {}
  28.  end
  29.  if not t[i1][i2] then
  30.    t[i1][i2] = {}
  31.  end
  32.  t[i1][i2][i3] = v
  33. end
  34.  
  35. local function tget(t,i1,i2,i3)
  36.   if not t[i1] or not t[i1][i2] or t[i1][i2][i3] == nil then
  37.     return nil
  38.   end  
  39.   return t[i1][i2][i3]
  40. end
  41.  
  42. --newSpot Helper function
  43. local function c(toCheck,reached,j,nx,ny,nz)
  44.   if tget(reached,j[1]+nx,j[2]+ny,j[3]+nz) == nil and
  45.      tget(map,j[1]+nx,j[2]+ny,j[3]+nz) ~= false and
  46.      (not needReturn or tget(map,j[1]+nx,j[2]+ny,j[3]+nz)) then
  47.     table.insert(toCheck,{j[1]+nx,j[2]+ny,j[3]+nz})
  48.     tset(reached,j[1]+nx,j[2]+ny,j[3]+nz,{unpack(j)})
  49.   end
  50. end
  51.  
  52. local function getPath(reached,j)
  53.   local path = {{unpack(j)}}
  54.   repeat
  55.     table.insert(path,tget(reached,unpack(path[#path])))
  56.   until path[#path] == true
  57.   table.remove(path) --true
  58.   table.remove(path) --current pos
  59.   return path
  60. end
  61.  
  62. local function newSpot()
  63.   local toCheck = {{x,y,z}}
  64.   local reached = {[x]={[y]={[z]=true}}}
  65.   for i,j in ipairs(toCheck) do
  66.     if tget(map,unpack(j)) == nil and not needReturn then
  67.       return getPath(reached,j)
  68.     end
  69.     if needReturn and j[1] == 0 and
  70.                       j[2] == 0 and
  71.                       j[3] == 0 then
  72.       return getPath(reached,j)
  73.     end
  74.     if i % 100 == 0 then
  75.       sleep(0)
  76.     end
  77.     c(toCheck,reached,j,0,1,0)
  78.     c(toCheck,reached,j,fx,0,fz)
  79.     c(toCheck,reached,j,-fz,0,fx)
  80.     c(toCheck,reached,j,-fx,0,-fz)
  81.     c(toCheck,reached,j,fz,0,-fx)
  82.     c(toCheck,reached,j,0,-1,0)
  83.   end
  84.   return getPath(reached,{0,0,0})
  85. end
  86.  
  87. local function faceTo(tx,ty,tz)
  88.   if math.abs(x - tx + z - tz) ~= 1 then
  89.     return
  90.   end
  91.   if x + fz == tx and z - fx == tz then
  92.     lt()
  93.   end
  94.   while x + fx ~= tx or z + fz ~= tz do
  95.     rt()
  96.   end
  97. end
  98.  
  99. local function goTo(tx,ty,tz)
  100.   if y < ty then
  101.     repeat until turtle.up()
  102.     y = y + 1
  103.     return
  104.   end
  105.   if y > ty then
  106.     repeat until turtle.down()
  107.     y = y - 1
  108.     return
  109.   end
  110.   faceTo(tx,ty,tz)
  111.   if x + fx == tx and z + fz == tz then
  112.     fd(1)
  113.   end
  114. end
  115.  
  116. local function checkLimit()
  117.   for i=1,16 do
  118.     if turtle.getItemCount(i) == 0 then
  119.         return false
  120.     end
  121.   end
  122.   return true
  123. end
  124.  
  125. local function addToMap(isValid, nx, ny, nz)
  126.   if isValid then
  127.     needReturn = checkLimit()
  128.   end
  129.   tset(map,x + nx, y + ny, z + nz, isValid)
  130. end
  131. local goOn = true
  132.  
  133. while goOn do
  134.   local path = newSpot()
  135.   for i=#path,2,-1 do
  136.     goTo(unpack(path[i]))
  137.   end
  138.   if #path > 0 and
  139.      (path[1][1] ~= 0 or
  140.      path[1][2] ~= 0 or
  141.      path[1][3] ~= 0) then
  142.     faceTo(unpack(path[1]))
  143.     if turtle.getFuelLevel() < 1000 then
  144.       turtle.refuel()
  145.     end
  146.     if path[1][2] > y then
  147.       addToMap(turtle.digUp(),0,1,0)
  148.     end
  149.     if path[1][2] == y then
  150.       addToMap(turtle.dig(),fx,0,fz)
  151.     end
  152.     if path[1][2] < y then
  153.       addToMap(y > 0  and turtle.digDown(),0,-1,0)
  154.     end
  155.   else
  156.     goTo(0,0,0)
  157.     local a,b = turtle.inspectUp()
  158.     if a and b.name == "minecraft:chest" and needReturn then
  159.       for i=1,16 do
  160.         if turtle.getItemCount(i) > 0 then
  161.           turtle.select(i)
  162.           turtle.dropUp()
  163.         end
  164.       end
  165.       while checkLimit() do
  166.         sleep(10)
  167.       end
  168.       needReturn = false
  169.     else
  170.       faceTo(1,0,0)
  171.       print("Collected the whole Tree")
  172.       goOn = false
  173.     end
  174.   end
  175. end
Advertisement
Add Comment
Please, Sign In to add comment