Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ms = require("movescript")
- local geo = peripheral.find("geoScanner")
- local MS_SETTINGS = {
- debug=false,
- destructive=true
- }
- local function isLog(data)
- if not data then return false end
- local tags = data.tags
- return tags["minecraft:logs"] == true
- end
- local function getNearbyLogs(radius)
- local cooldown = geo.getOperationCooldown("scanBlocks")
- if cooldown > 0 then
- cooldown = cooldown / 1000
- print("Sleeping for "..cooldown.." seconds...")
- os.sleep(cooldown)
- end
- local logs = {}
- radius = radius or 16
- for _,b in pairs(geo.scan(radius)) do
- for _,t in pairs(b.tags) do
- if t == "minecraft:block/minecraft:logs" then
- table.insert(
- logs,
- {x=b.x,y=b.y,z=b.z}
- )
- break
- end
- end
- end
- return logs
- end
- local function getTreeLocations(logs)
- local locations = {}
- for _,log in pairs(logs) do
- local exists = false
- for _,loc in pairs(locations) do
- if loc.x == log.x and loc.z == log.z then
- exists = true
- break
- end
- end
- if not exists then
- table.insert(locations,{x=log.x,z=log.z})
- end
- end
- return locations
- end
- local function getHighestLog(logs)
- if #logs == 0 then return nil end
- local maxY = -1000
- for _,log in pairs(logs) do
- if log.y > maxY then
- maxY = log.y
- end
- end
- return maxY
- end
- local function moveAboveHighestTree(logs, loc, r)
- local maxY = getHighestLog(logs)
- if maxY == nil then return end
- while maxY >= r do -- The highest log is at the top of scan height, so go up and keep scanning.
- ms.run((maxY+1).."U", MS_SETTINGS)
- loc.y = loc.y + (maxY+1)
- logs = getNearbyLogs(r)
- maxY = getHighestLog(logs)
- end
- if maxY > 0 then
- ms.run((maxY+1).."U", MS_SETTINGS)
- loc.y = loc.y + (maxY+1)
- elseif maxY < 0 then
- ms.run((maxY-1).."D", MS_SETTINGS)
- loc.y = loc.y - (maxY-1)
- end
- end
- local function moveTo(target, loc)
- print("Going to x="..target.x..", z="..target.z)
- print("At x="..loc.x..", y="..loc.y..", z="..loc.z)
- -- Navigate horizontally.
- -- Assume we start North.
- local dx = target.x - loc.x
- local dz = target.z - loc.z
- if dz < 0 then
- print("Moving on -Z axis...")
- ms.run(math.abs(dz).."F", MS_SETTINGS)
- loc.z = loc.z + dz
- elseif dz > 0 then
- print("Moving on +Z axis...")
- ms.run("2R"..dz.."F2R", MS_SETTINGS)
- loc.z = loc.z + dz
- end
- if dx < 0 then
- print("Moving on -X axis...")
- ms.run("L"..math.abs(dx).."FR", MS_SETTINGS)
- loc.x = loc.x + dx
- elseif dx > 0 then
- print("Moving on +X axis...")
- ms.run("R"..dx.."FL", MS_SETTINGS)
- loc.x = loc.x + dx
- end
- end
- local function chopDownTree()
- local yDif = 0
- local success,data = turtle.inspectDown()
- while not success or data.tags["minecraft:logs"] or data.tags["minecraft:leaves"] do
- ms.run("DgdD", MS_SETTINGS)
- yDif = yDif + 1
- success,data = turtle.inspectDown()
- end
- while yDif > 0 do
- ms.run("U", MS_SETTINGS)
- yDif = yDif - 1
- end
- end
- local function chopArea(r)
- r = r or 16
- -- Assume we're facing North.
- local logs = getNearbyLogs(r)
- print("Found "..#logs.." log blocks.")
- local trees = getTreeLocations(logs)
- print("For "..#trees.." trees.")
- local loc = {x=0,y=0,z=0,dir="N"}
- print("Moving above the highest tree...")
- moveAboveHighestTree(logs, loc, r)
- for _,tree in pairs(trees) do
- print("Going to tree ".._)
- moveTo(tree, loc)
- chopDownTree()
- end
- moveTo({x=0,z=0}, loc)
- ms.run(loc.y.."D", MS_SETTINGS)
- end
- chopArea(16)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement