Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --digTools v1.0
- --by nupanick
- digWait = .5 --for digging through gravel or sand
- --digRectRoom
- --a function to help a computercraft turtle clear out rectangular rooms, something I seem to be doing way too much of.
- --the x, y, and z coordinates are relative, with the turtle's starting location at the origin, +x being right, +y being up, and +z being forward.
- --the method used is a recursive fill (like with the good old paint bucket) because there may be some blocks the turtle is unable to clear.
- --todo: automatic refueling
- --todo: remove redundant spinning, long return trip?
- --x's, y's, and z's: two points defining the corners of the box to be dug out.
- --fillslot: the slot used to fill in empty spaces at the edges of the box (to prevent monsters from wandering in from exposed caves).
- --fuelslot: the slot where the turtle will look for spare fuel if it runs low.
- function digRectRoom(x1, x2, y1, y2, z1, z2, fuelslot, fillslot, safeblocks)
- fill = fillslot
- fuel = fuelslot
- whitelist = safeblocks
- --blocks is a 3D array storing the status of the blocks to be dug.
- --0 is the default, 1 means we've dug there successfully, -1 means that cell is undiggable for some reason.
- blocks = {}
- for x = x1, x2 do
- blocks[x] = {}
- for y = y1, y2 do
- blocks[x][y] = {}
- for z = z1, z2 do
- blocks[x][y][z] = 0
- end
- end
- end --array initialization
- x = 0 --coordinates relative to the turtle. Note that the turtle is facing the +z direction.
- y = 0
- z = 0
- recursiveDig()
- end --digRectRoom
- function recursiveDig()
- --recursively dig on all six adjacent blocks
- -- y+
- if blocks[x][y+1] == nil then
- turtle.select(fill)
- turtle.placeUp()
- elseif blocks[x][y+1][z] == 0 then
- success = dig(1)
- if success then
- blocks[x][y+1][z] = 1
- turtle.up()
- y = y + 1
- recursiveDig()
- dig(-1)
- turtle.down()
- y = y - 1
- else
- blocks[x][y+1][z] = -1
- end
- end
- -- y-
- if blocks[x][y-1] == nil then
- turtle.select(fill)
- turtle.placeDown()
- elseif blocks[x][y-1][z] == 0 then
- success = dig(-1)
- if success then
- blocks[x][y-1][z] = 1
- turtle.down()
- y = y - 1
- recursiveDig()
- dig(1)
- turtle.up()
- y = y + 1
- else
- blocks[x][y-1][z] = -1
- end
- end
- -- x+
- if blocks[x+1] == nil then
- turtle.turnRight()
- turtle.select(fill)
- turtle.place()
- turtle.turnLeft()
- elseif blocks[x+1][y][z] == 0 then
- turtle.turnRight()
- success = dig(0)
- if success then
- blocks[x+1][y][z] = 1
- turtle.forward()
- x = x + 1
- turtle.turnLeft()
- recursiveDig()
- turtle.turnLeft()
- dig(0)
- turtle.forward()
- x = x - 1
- turtle.turnRight()
- else
- blocks[x+1][y][z] = -1
- turtle.turnLeft()
- end
- end
- -- x-
- if blocks[x-1] == nil then
- turtle.turnLeft()
- turtle.select(fill)
- turtle.place()
- turtle.turnRight()
- elseif blocks[x-1][y][z] == 0 then
- turtle.turnLeft()
- success = dig(0)
- if success then
- blocks[x-1][y][z] = 1
- turtle.forward()
- x = x - 1
- turtle.turnRight()
- recursiveDig()
- turtle.turnRight()
- dig(0)
- turtle.forward()
- x = x + 1
- turtle.turnLeft()
- else
- blocks[x-1][y][z] = -1
- turtle.turnRight()
- end
- end
- -- z+
- if blocks[x][y][z+1] == nil then
- turtle.select(fill)
- turtle.place()
- elseif blocks[x][y][z+1] == 0 then
- success = dig(0)
- if success then
- blocks[x][y][z+1] = 1
- turtle.forward()
- z = z + 1
- recursiveDig()
- turtle.turnLeft()
- turtle.turnLeft()
- dig(0)
- turtle.forward()
- turtle.turnRight()
- turtle.turnRight()
- z = z - 1
- else
- blocks[x][y][z+1] = -1
- end
- end
- -- z-
- if blocks[x][y][z-1] == nil then
- turtle.select(fill)
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.place()
- turtle.turnRight()
- turtle.turnRight()
- elseif blocks[x][y][z-1] == 0 then
- turtle.turnLeft()
- turtle.turnLeft()
- success = dig(0)
- if success then
- blocks[x][y][z-1] = 1
- turtle.forward()
- z = z - 1
- turtle.turnRight()
- turtle.turnRight()
- recursiveDig()
- dig(0)
- turtle.forward()
- z = z + 1
- else
- blocks[x][y][z-1] = -1
- turtle.turnRight()
- turtle.turnRight()
- end
- end
- end
- --helper method: dig until clear or unable to dig
- --dir: 0 = forward, 1 = up, -1 = down.
- --returns: true if the space is now empty, -1 if it is blocked.
- function dig(dir)
- digFunc = turtle.dig
- detectFunc = turtle.detect
- compareFunc = turtle.compare
- if dir == 1 then
- digFunc = turtle.digUp
- detectFunc = turtle.detectUp
- compareFunc = turtle.compareUp
- elseif dir == -1 then
- digFunc = turtle.digDown
- detectFunc = turtle.detectDown
- compareFunc = turtle.compareDown
- end
- if not detectFunc() then return true end
- whitelisted = false
- for i,v in pairs(whitelist) do
- turtle.select(v)
- if compareFunc() then whitelisted = true end
- end
- if not whitelisted then return false end
- digging = true
- blocked = detectFunc()
- while digging and blocked do
- digging = digFunc()
- sleep(digWait)
- blocked = detectFunc()
- end
- return not blocked
- end
Advertisement
Add Comment
Please, Sign In to add comment