Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print ("How long should the room be?")
- local length = tonumber(io.read())
- print ("How wide should the room be?")
- local width = tonumber(io.read())
- print ("How high should the room be?")
- local height = tonumber(io.read())
- xPos = 0 -- Initalise positions. Positive x = forward from starting direction
- yPos = 0 -- Positive y = right from starting position
- zPos = 0 -- Positive z = up from starting position
- dir = 1 -- initalize direction 1 = ahead, 2 = right, 3 = back, 4 = left, relative to inital position
- xMods = {1,0,-1,0} -- index xMods[dir] indicates how x will change on a turtle.forward()
- yMods = {0,1,0,-1} -- index xMods[dir] indicates how x will change on a turtle.forward()
- fuelThreshold = 10 -- below what fuel level will the turtle refuel itself?
- local function setHeight(targetZ) -- moves the turtle to the target height, bashing any obstructions clear
- while zPos < targetZ do
- if turtle.detectUp() then
- turtle.digUp()
- end
- if turtle.up() then
- zPos = zPos + 1
- end
- end
- while zPos > targetZ do
- if turtle.detectDown() then
- turtle.digDown()
- end
- if turtle.down() then
- zPos = zPos - 1
- end
- end
- end
- local function right() -- turns right once
- turtle.turnRight()
- dir = dir + 1
- if dir > 4 then --makes sure dir is in the 1-4 range
- dir = dir - 4
- end
- end
- local function left() -- turns left once
- turtle.turnLeft()
- dir = dir - 1
- if dir < 1 then --makes sure dir is in the 1-4 range
- dir = dir + 4
- end
- end
- local function turnTo(targetDir) -- turns right untl facing the right way (TODO: Find a way of making it turn the shortest way whish isn't just one long list of special cases)
- while targetDir ~= dir do
- right()
- end
- end
- local function checkFuel() -- ensure the turtle as enough fuel
- if turtle.getFuelLevel ~= "unlimited" -- check that turtles even need fuel
- then
- if turtle.getFuelLevel() < fuelThreshold -- if fuel is below the threshold...
- then
- i = 1
- needFuel = true
- while needFuel do
- turtle.select(i) -- then step through the slots
- if turtle.refuel(1) -- and try to use 1 fuel
- then
- if turtle.getFuelLevel() > fuelThreshold -- if fueled, then done
- then
- needFuel = false
- end
- else
- if i < 16
- then
- i = i + 1
- else
- print("No fuel left!")
- sleep(1)
- needFuel = false
- end
- end
- end
- end
- end
- end
- local function forward(distance) -- Simple go forward, smash anything in the way. (TODO: add a simple evasion routine)
- local i = 0
- while i < distance do
- if turtle.detect() then -- is there anything in the way? Smash it then
- turtle.dig()
- end
- checkFuel()
- if turtle.forward() then -- go forward, count if you actually move
- i = i + 1
- xPos = xPos + xMods[dir]
- yPos = yPos + yMods[dir]
- end
- end
- -- print("At end of forward command, I am at ("..xPos..","..yPos..","..zPos..")") -- decomment this to see where the turtle thinks it is
- end
- local function navigateTo(targetX,targetY,targetZ) -- navigates to a relative coordinate, smashing everything it it's way. USE WITH CAUTION
- -- print("Moving to (" .. targetX ..","..targetY..","..targetZ..")") -- decomment this to see where the turtle thinks it is going
- if xPos < targetX then
- turnTo(1)
- forward(targetX - xPos)
- end
- if xPos > targetX then
- turnTo(3)
- forward(xPos - targetX)
- end
- if yPos < targetY then
- turnTo(2)
- forward(targetY - yPos)
- end
- if yPos > targetY then
- turnTo(4)
- forward(yPos - targetY)
- end
- setHeight(targetZ)
- end
- for z=0,(height-1),3 do
- if z < (height -1) then
- zLevel = z+1
- digDown = true
- if z < (height - 2)
- then
- digUp = true
- else
- digUp = false
- end
- else
- zLevel = z
- digUp = false
- digDown = false
- end
- upTheRow = true
- for y = 0, (width - 1) do
- if upTheRow
- then
- startX = 0
- endX = (length -1)
- modX = 1
- upTheRow = false
- else
- startX = (length -1)
- endX = 0
- modX = -1
- upTheRow = true
- end
- for x = startX, endX, modX do
- navigateTo(x,y,zLevel)
- if digUp then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- if digDown then
- while turtle.detectDown() do
- turtle.digDown()
- end
- end
- end
- end
- end
- navigateTo(0,0,0)
Advertisement
Add Comment
Please, Sign In to add comment