Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --==================================================--
- -- user setup:
- -- less fuel than this? EAT UP!!!!
- EAT_FUEL_AT_LT = 80*64*2
- -- min fuel level before return to sender
- MIN_FUEL_RETURN = 20
- -- junk stuff (only partial match required)
- DROP_LIST = {}
- TREE_LIST = {'_log'}
- --==================================================--
- -- lumberjack constants
- FACE_N = 0
- FACE_E = 1
- FACE_S = 2
- FACE_W = 3
- lumberjack = {} -- the turtle
- lumberjack['direction'] = 0
- lumberjack['x'] = 0
- lumberjack['y'] = 0
- lumberjack['z'] = 0
- lumberjack['start_x'], lumberjack['start_y'], lumberjack['start_z'] = lumberjack['x'], lumberjack['y'], lumberjack['z']
- lumberjack['start_direction'] = lumberjack['direction']
- --==================================================--
- function split(str, sep)
- local res = {}
- for x in string.gmatch(str,sep) do
- table.insert(res, x)
- end
- return res
- end
- function partialMatchAny(val, list)
- for index, value in ipairs(list) do
- if string.find(val, value) then
- return true
- end
- end
- return false
- end
- function matchAny(tab, val)
- for index, value in ipairs(tab) do
- if val == value then
- return true
- end
- end
- return false
- end
- --==================================================--
- function lumberjack.checkRefuel(minimum)
- if turtle.getFuelLevel() > minimum then
- return nil
- end
- for i = 1, 16 do
- turtle.select(i)
- -- if it's valid fuel:
- if turtle.refuel(0) then
- for j = 1, turtle.getItemCount(i) do
- turtle.refuel(1) -- do this one by one, some fuels are extreme
- if turtle.getFuelLevel() > minimum then
- return nil
- end
- end
- end
- end
- end
- function lumberjack.updateHz(n)
- n = n or 1
- if lumberjack['direction'] == FACE_N then lumberjack['y'] = lumberjack['y'] - n
- elseif lumberjack['direction'] == FACE_S then lumberjack['y'] = lumberjack['y'] + n
- elseif lumberjack['direction'] == FACE_W then lumberjack['x'] = lumberjack['x'] - n
- elseif lumberjack['direction'] == FACE_E then lumberjack['x'] = lumberjack['x'] + n
- end
- end
- function lumberjack.turnRight(n)
- n = n or 1
- for i=1,n do
- turtle.turnRight()
- lumberjack['direction'] = (lumberjack['direction'] + 1) % 4
- end
- end
- function lumberjack.turnLeft(n)
- n = n or 1
- for i=1,n do
- turtle.turnLeft()
- lumberjack['direction'] = (lumberjack['direction'] - 1) % 4
- end
- end
- function lumberjack.forward(n)
- n = n or 1
- for i=1,n do
- if not(turtle.forward()) then return false end
- lumberjack.updateHz(1)
- end
- return true
- end
- function lumberjack.back(n)
- n = n or 1
- for i=1,n do
- if not turtle.back() then return false end
- lumberjack.updateHz(-1)
- end
- return true
- end
- function lumberjack.up(n)
- n = n or 1
- for i=1,n do
- if not turtle.up() then return false end
- lumberjack['z'] = lumberjack['z'] + 1
- end
- return true
- end
- function lumberjack.down(n)
- n = n or 1
- for i=1,n do
- if not turtle.down() then return false end
- lumberjack['z'] = lumberjack['z'] - 1
- end
- return true
- end
- function lumberjack.digForward(n)
- n = n or 1
- for i=1,n do
- if turtle.getFuelLevel() < MIN_FUEL_RETURN then
- lumberjack.checkRefuel(EAT_FUEL_AT_LT)
- turtle.select(1)
- if (turtle.getFuelLevel() < MIN_FUEL_RETURN) then
- return false
- end
- end
- if (not lumberjack.forward()) then
- -- this needs to be looped in case of airdrops (sand, gravel etc)
- -- also to wait for despawn?
- while turtle.detect() do
- turtle.dig()
- os.sleep(0.1)
- end
- while not lumberjack.forward() do
- turtle.attack()
- end
- end
- end
- return true
- end
- function lumberjack.digDown(n)
- n = n or 1
- for i=1,n do
- if turtle.getFuelLevel() < MIN_FUEL_RETURN then
- lumberjack.checkRefuel(EAT_FUEL_AT_LT)
- turtle.select(1)
- if (turtle.getFuelLevel() < MIN_FUEL_RETURN) then
- return false
- end
- end
- if (not lumberjack.down()) then
- -- this needs to be looped in case of airdrops (sand, gravel etc)
- -- also to wait for despawn?
- while turtle.detectDown() and (turtle.getFuelLevel() > MIN_FUEL_RETURN) do
- turtle.digDown()
- os.sleep(0.1)
- end
- while not lumberjack.down() and (turtle.getFuelLevel() > MIN_FUEL_RETURN) do
- turtle.attackDown()
- end
- end
- end
- return true
- end
- function lumberjack.digUp(n)
- n = n or 1
- for i=1,n do
- if turtle.getFuelLevel() < MIN_FUEL_RETURN then
- lumberjack.checkRefuel(EAT_FUEL_AT_LT)
- turtle.select(1)
- if (turtle.getFuelLevel() < MIN_FUEL_RETURN) then
- return false
- end
- end
- if (not lumberjack.up()) then
- -- this needs to be looped in case of airdrops (sand, gravel etc)
- -- also to wait for despawn?
- while turtle.detectUp() and (turtle.getFuelLevel() > MIN_FUEL_RETURN) do
- turtle.digUp()
- os.sleep(0.1)
- end
- while not lumberjack.up() and (turtle.getFuelLevel() > MIN_FUEL_RETURN) do
- turtle.attackUp()
- end
- end
- end
- return true
- end
- function lumberjack.face(direction)
- -- local diff = math.abs(lumberjack['direction'] - direction)
- while not(lumberjack['direction'] == direction) do
- lumberjack.turnRight()
- end
- end
- function lumberjack.inventoryFull()
- local usedSlots = 0
- for i = 1,16 do
- if turtle.getItemCount(i) > 0 then
- usedSlots = usedSlots + 1
- end
- end
- -- if all slots are used and of 'em is filled, then return `true`
- if usedSlots >= 15 then
- return true
- end
- return false
- end
- function lumberjack.findChest()
- local success, data = false, ''
- for i=0,3 do
- lumberjack.face(i)
- success, data = turtle.inspect()
- if success and string.find(data.name, "chest") then
- return true
- end
- end
- return false
- end
- function lumberjack.depositAll()
- local success, data = false, ''
- local hasChest = false
- for i=0,3 do
- lumberjack.face(i)
- success, data = turtle.inspect()
- if success and string.find(data.name, "chest") then
- hasChest = true
- break
- end
- end
- if (hasChest) then
- for i = 1,16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- return hasChest
- end
- function lumberjack.digTo(x,y,z)
- x = tonumber(x or lumberjack['x'])
- y = tonumber(z or lumberjack['y'])
- z = tonumber(y or lumberjack['z'])
- -- digging x-axis (forward / backward)
- if x < lumberjack['x'] then
- lumberjack.face(FACE_W)
- elseif x > lumberjack['x'] then
- lumberjack.face(FACE_E)
- end
- if not(x == lumberjack['x']) then
- lumberjack.digForward(math.abs(x-lumberjack['x']))
- end
- -- digging y-axis (forward / backward)
- if y < lumberjack['y'] then
- lumberjack.face(FACE_N)
- elseif y > lumberjack['y'] then
- lumberjack.face(FACE_S)
- end
- if not(y == lumberjack['y']) then
- lumberjack.digForward(math.abs(y-lumberjack['y']))
- end
- -- digging up/down
- if z < lumberjack['z'] then
- lumberjack.digDown(math.abs(z-lumberjack['z']))
- elseif z > lumberjack['z'] then
- lumberjack.digUp(math.abs(z-lumberjack['z']))
- end
- end
- function lumberjack.reset(x, y, z)
- x = tonumber(x or lumberjack['start_x'])
- y = tonumber(z or lumberjack['start_y'])
- z = tonumber(y or lumberjack['start_z'])
- lumberjack.digTo(x,y,z)
- end
- -- drop all unwanted ores
- function lumberjack.dropJunk()
- for i = 1,16 do
- if (turtle.getItemCount(i) > 0) and (partialMatchAny(turtle.getItemDetail(i).name, DROP_LIST)) then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- --====| Program | Free reign lumberjack |====--
- -- no fuel? Go back and wait for refuel
- function refuel()
- print('Fuel level is low, waiting for refuel')
- -- wait for insertion
- while (turtle.getFuelLevel() <= MIN_FUEL_RETURN) do
- lumberjack.checkRefuel(EAT_FUEL_AT_LT)
- os.sleep(1)
- end
- end
- function scanForTree360()
- local success, data = false, ''
- print('>>> Scanning for tree')
- for i=0,3 do
- lumberjack.face(i)
- success, data = turtle.inspect()
- if success and (partialMatchAny(data.name, TREE_LIST)) then
- return true
- end
- end
- return false
- end
- function blockedByTree()
- print('>>> Scanning for tree')
- local success, data = turtle.inspect()
- if success and (partialMatchAny(data.name, TREE_LIST)) then
- return true
- end
- return false
- end
- function plantSapling()
- for i = 1,16 do
- if (turtle.getItemCount(i) > 0) and (partialMatchAny(turtle.getItemDetail(i).name, {'_sapling'})) then
- turtle.select(i)
- turtle.place()
- end
- end
- end
- function chopchop()
- turtle.select(1)
- tmp = lumberjack['direction']
- lumberjack.digForward()
- while turtle.detectUp() do
- lumberjack.digUp()
- turtle.suck() -- get any saplings
- os.sleep(0.1)
- end
- -- return down
- lumberjack.face((lumberjack['direction']+2) % 4) --180 degrees
- lumberjack.digForward(1)
- lumberjack.digDown(lumberjack['z'])
- turtle.suck() -- get any saplings
- -- continue
- lumberjack.face(tmp)
- end
- function main()
- math.randomseed(os.time())
- lumberjack.face(math.random(0,3))
- while true do
- if (turtle.getFuelLevel() <= MIN_FUEL_RETURN) then
- refuel()
- end
- local i = 0
- while lumberjack.forward() do
- if (turtle.getFuelLevel() <= MIN_FUEL_RETURN) then
- refuel()
- end
- if i % 2 == 0 then -- we dont have to suck all the time, as we shove the saplings
- turtle.suck() -- get any saplings
- end
- -- randomly change direction
- if math.random(0,10) == 0 then
- break
- end
- i = i + 1
- end
- if blockedByTree() then
- print('Chop chop!')
- chopchop()
- plantSapling()
- else
- print('Setting new direction!')
- lumberjack.face(math.random(0,3))
- end
- if lumberjack.inventoryFull() then
- print('Full inventory')
- while lumberjack.inventoryFull() do
- os.sleep(10)
- end
- end
- end
- end
- -- verify that we start off with a chest near by
- function hasChest()
- if not lumberjack.findChest() then
- print('Failed to recognize chest!')
- lumberjack.face(lumberjack['start_direction'])
- return false
- end
- lumberjack.face(lumberjack['start_direction'])
- return true
- end
- main(area)
Add Comment
Please, Sign In to add comment