Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local x,y,z,fx,fz = 0,0,0,1,0
- local map = {[0]={[0]={[0]=true}}}
- local needReturn = false
- local function lt()
- turtle.turnLeft()
- fx,fz = fz,-fx
- end
- local function rt()
- turtle.turnRight()
- fx,fz = -fz,fx
- end
- local function fd(n)
- for i=1,n do
- repeat
- until turtle.forward()
- x = x + fx
- z = z + fz
- end
- end
- local function tset(t,i1,i2,i3,v)
- if not t[i1] then
- t[i1] = {}
- end
- if not t[i1][i2] then
- t[i1][i2] = {}
- end
- t[i1][i2][i3] = v
- end
- local function tget(t,i1,i2,i3)
- if not t[i1] or not t[i1][i2] or t[i1][i2][i3] == nil then
- return nil
- end
- return t[i1][i2][i3]
- end
- --newSpot Helper function
- local function c(toCheck,reached,j,nx,ny,nz)
- if tget(reached,j[1]+nx,j[2]+ny,j[3]+nz) == nil and
- tget(map,j[1]+nx,j[2]+ny,j[3]+nz) ~= false and
- (not needReturn or tget(map,j[1]+nx,j[2]+ny,j[3]+nz)) then
- table.insert(toCheck,{j[1]+nx,j[2]+ny,j[3]+nz})
- tset(reached,j[1]+nx,j[2]+ny,j[3]+nz,{unpack(j)})
- end
- end
- local function getPath(reached,j)
- local path = {{unpack(j)}}
- repeat
- table.insert(path,tget(reached,unpack(path[#path])))
- until path[#path] == true
- table.remove(path) --true
- table.remove(path) --current pos
- return path
- end
- local function newSpot()
- local toCheck = {{x,y,z}}
- local reached = {[x]={[y]={[z]=true}}}
- for i,j in ipairs(toCheck) do
- if tget(map,unpack(j)) == nil and not needReturn then
- return getPath(reached,j)
- end
- if needReturn and j[1] == 0 and
- j[2] == 0 and
- j[3] == 0 then
- return getPath(reached,j)
- end
- if i % 100 == 0 then
- sleep(0)
- end
- c(toCheck,reached,j,0,1,0)
- c(toCheck,reached,j,fx,0,fz)
- c(toCheck,reached,j,-fz,0,fx)
- c(toCheck,reached,j,-fx,0,-fz)
- c(toCheck,reached,j,fz,0,-fx)
- c(toCheck,reached,j,0,-1,0)
- end
- return getPath(reached,{0,0,0})
- end
- local function faceTo(tx,ty,tz)
- if math.abs(x - tx + z - tz) ~= 1 then
- return
- end
- if x + fz == tx and z - fx == tz then
- lt()
- end
- while x + fx ~= tx or z + fz ~= tz do
- rt()
- end
- end
- local function goTo(tx,ty,tz)
- if y < ty then
- repeat until turtle.up()
- y = y + 1
- return
- end
- if y > ty then
- repeat until turtle.down()
- y = y - 1
- return
- end
- faceTo(tx,ty,tz)
- if x + fx == tx and z + fz == tz then
- fd(1)
- end
- end
- local function checkLimit()
- for i=1,16 do
- if turtle.getItemCount(i) == 0 then
- return false
- end
- end
- return true
- end
- local function addToMap(isValid, nx, ny, nz)
- if isValid then
- needReturn = checkLimit()
- end
- tset(map,x + nx, y + ny, z + nz, isValid)
- end
- local goOn = true
- while goOn do
- local path = newSpot()
- for i=#path,2,-1 do
- goTo(unpack(path[i]))
- end
- if #path > 0 and
- (path[1][1] ~= 0 or
- path[1][2] ~= 0 or
- path[1][3] ~= 0) then
- faceTo(unpack(path[1]))
- if turtle.getFuelLevel() < 1000 then
- turtle.refuel()
- end
- if path[1][2] > y then
- addToMap(turtle.digUp(),0,1,0)
- end
- if path[1][2] == y then
- addToMap(turtle.dig(),fx,0,fz)
- end
- if path[1][2] < y then
- addToMap(y > 0 and turtle.digDown(),0,-1,0)
- end
- else
- goTo(0,0,0)
- local a,b = turtle.inspectUp()
- if a and b.name == "minecraft:chest" and needReturn then
- for i=1,16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.dropUp()
- end
- end
- while checkLimit() do
- sleep(10)
- end
- needReturn = false
- else
- faceTo(1,0,0)
- print("Collected the whole Tree")
- goOn = false
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment