Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- lay_perp_line.lua
- -- Place blocks perpendicular to travel while moving forward.
- -- Usage:
- -- lay_perp_line <down|left|right> [steps]
- -- Examples:
- -- lay_perp_line down -- place below each step until blocked
- -- lay_perp_line left 25 -- 25 steps, place to the left each step
- local args = {...}
- local DIR = args[1]
- local STEPS = tonumber(args[2] or "0") -- 0 = run until blocked
- local function die(msg) print(msg) error(msg, 0) end
- if not DIR or not ({down=true,left=true,right=true})[DIR] then
- die("Usage: lay_perp_line <down|left|right> [steps]")
- end
- -- Select a slot with blocks; keep current slot if it still has items
- local function ensureBlockSelected()
- if turtle.getItemCount() > 0 then return true end
- for i=1,16 do
- if turtle.getItemCount(i) > 0 then turtle.select(i); return true end
- end
- return false
- end
- local function placeDownIfAir()
- if turtle.detectDown() then return true end
- if not ensureBlockSelected() then return false end
- return turtle.placeDown()
- end
- local function placeLeftIfAir()
- turtle.turnLeft()
- local ok = true
- if not turtle.detect() then
- ok = ensureBlockSelected() and turtle.place() or false
- end
- turtle.turnRight()
- return ok
- end
- local function placeRightIfAir()
- turtle.turnRight()
- local ok = true
- if not turtle.detect() then
- ok = ensureBlockSelected() and turtle.place() or false
- end
- turtle.turnLeft()
- return ok
- end
- local function placePerp()
- if DIR == "down" then return placeDownIfAir()
- elseif DIR == "left" then return placeLeftIfAir()
- else return placeRightIfAir() end
- end
- local stepsDone = 0
- while true do
- -- Place in the requested perpendicular direction if there is air there.
- if not placePerp() then
- if not ensureBlockSelected() then print("Out of blocks."); break end
- -- If placement failed for another reason, continue. It might already be filled.
- end
- -- Stop if something is immediately ahead.
- if turtle.detect() then
- print("Blocked ahead. Stopping.")
- break
- end
- -- Move forward one block.
- if not turtle.forward() then
- print("Movement failed. Stopping.")
- break
- end
- stepsDone = stepsDone + 1
- if STEPS > 0 and stepsDone >= STEPS then break end
- end
- print(("Done. Steps: %d"):format(stepsDone))
Advertisement
Add Comment
Please, Sign In to add comment