Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------
- -- SINGLE-LAYER SPIRAL DIG
- -- by "OutsideβIn" Snail Method
- -----------------------------------
- -- We'll keep track of a local (x, y) position,
- -- x increasing "forward," y increasing "to the left" if we turn left
- -- We'll also track an orientation integer 0..3:
- -- 0 = facing +x, 1 = +y, 2 = -x, 3 = -y
- local xPos, yPos = 0, 0
- local orient = 0 -- 0 = +x, 1 = +y, 2 = -x, 3 = -y
- -- We'll define lookup tables so when we move forward,
- -- we know how xPos,yPos changes for each orientation.
- local dx = { [0] = 1, [1] = 0, [2] = -1, [3] = 0 }
- local dy = { [0] = 0, [1] = 1, [2] = 0, [3] = -1 }
- -----------------------------------
- -- 1) Prompt for length & width
- -----------------------------------
- print("SINGLE-LAYER SPIRAL DIG")
- print("(No fancy features, just a snail path on one layer)")
- io.write("Enter the room LENGTH (forward): ")
- local length = tonumber(io.read())
- io.write("Enter the room WIDTH (perpendicular): ")
- local width = tonumber(io.read())
- print("OK, digging a spiral across a " .. length .. "x" .. width .. " area...")
- -----------------------------------
- -- 2) Basic digForward function
- -----------------------------------
- local function digForward()
- -- Keep breaking blocks in front until clear
- while turtle.detect() do
- turtle.dig()
- sleep(0.2) -- let gravel/sand settle
- end
- turtle.forward()
- -- Update our local xPos, yPos based on orientation
- xPos = xPos + dx[orient]
- yPos = yPos + dy[orient]
- end
- -----------------------------------
- -- 3) Turn left / right
- -----------------------------------
- local function turnLeft()
- turtle.turnLeft()
- orient = (orient + 1) % 4
- end
- local function turnRight()
- turtle.turnRight()
- orient = (orient + 3) % 4
- end
- -----------------------------------
- -- 4) Move n steps forward, digging as we go
- -----------------------------------
- local function moveSteps(n)
- for i=1, n do
- digForward()
- end
- end
- -----------------------------------
- -- 5) The SPIRAL function (for 1 layer)
- -- This is the classic snail algorithm:
- -- while (length>0 and width>0) do
- -- 1) move (length-1), turnLeft
- -- 2) move (width -1), turnLeft
- -- length--, width--
- -- [repeat again if still length>0 & width>0]
- -----------------------------------
- local function spiralSingleLayer(L, W)
- local horiz = L
- local vert = W
- while (horiz > 0 and vert > 0) do
- -- Pass 1
- moveSteps(horiz - 1)
- turnLeft()
- moveSteps(vert - 1)
- turnLeft()
- horiz = horiz - 1
- vert = vert - 1
- -- if after shrinking, we've run out, break
- if horiz <= 0 or vert <= 0 then
- break
- end
- -- Pass 2
- moveSteps(horiz - 1)
- turnLeft()
- moveSteps(vert - 1)
- turnLeft()
- horiz = horiz - 1
- vert = vert - 1
- end
- end
- -----------------------------------
- -- 6) Run the spiral dig
- -----------------------------------
- spiralSingleLayer(length, width)
- print("Done! The turtle is near the center of that layer.")
- print("xPos="..xPos..", yPos="..yPos.." in local coords, orientation="..orient)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement