Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- room
- -- Creates a room with solid walls
- -- Clears the interior
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- version = "room: Rev 3.2"
- usage = "Usage: room <length> <width> <height>"
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- patchSlot = 1
- chestSlot = 15
- torchSlot = 16
- garbageSlot = 5
- torchSpacing = 2
- torchSpot = 5
- nextTurn = "left"
- addTorches = false
- -- Status data for marking a spot to return to after going 'home'
- markX = 0
- markY = 0
- markZ = 0
- markFace = 0
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print("Creates a room with solid walls")
- print("Clears the interior")
- print("Turtle should be pre-fueled")
- print("Slot 1 is for patch material")
- print("Slot 5 is for garbage material")
- print("Slot 15 is for storage chests")
- print("Slot 16 is for torches")
- print(usage)
- return
- end
- if nArgs ~= 3 then
- print(usage)
- return
- end
- length = tonumber(args[1])
- if length == nil then
- print("\"", args[1], "\" is not a valid length")
- return
- end
- if length < 1 then
- print("length must be a positive number greater than zero")
- end
- width = tonumber(args[2])
- if width == nil then
- print("\"", args[2], "\" is not a valid width")
- return
- end
- if width < 1 then
- print("width must be a positive number greater than zero")
- end
- height = tonumber(args[3])
- if height == nil then
- print("\"", args[3], "\" is not a valid height")
- return
- end
- if height < 1 then
- print("height must be a positive number greater than zero")
- end
- targetArea = length * width
- areaCovered = 1;
- function patch()
- if turtle.getItemCount(patchSlot) < 2 then
- gatherPatchMaterial()
- if turtle.getItemCount(patchSlot) < 2 then
- abort = true
- areaCovered = targetArea
- missionMessage = "Aborted: No more wall patch"
- return
- end
- end
- turtle.select(patchSlot)
- turtle.place()
- end
- function patchUp()
- if turtle.getItemCount(patchSlot) < 2 then
- gatherPatchMaterial()
- if turtle.getItemCount(patchSlot) < 2 then
- abort = true
- areaCovered = targetArea
- missionMessage = "Aborted: No more patch material"
- return
- end
- end
- turtle.select(patchSlot)
- turtle.placeUp()
- gatherPatchMaterial();
- end
- function patchDown()
- if turtle.getItemCount(patchSlot) < 2 then
- gatherPatchMaterial()
- if turtle.getItemCount(patchSlot) < 2 then
- abort = true
- areaCovered = targetArea
- missionMessage = "Aborted: No more patch material"
- return
- end
- end
- turtle.select(patchSlot)
- turtle.placeUp()
- gatherPatchMaterial();
- end
- function patchDown()
- turtle.select(patchSlot)
- turtle.placeDown()
- gatherPatchMaterial();
- end
- function gatherPatchMaterial()
- local i
- patchCount = turtle.getItemCount(patchSlot)
- garbageCount = turtle.getItemCount(garbageSlot)
- if patchCount < 3 or garbageCount < 20 then
- -- print("Attempting to refill slot ", patchSlot)
- for i=2,14 do
- turtle.select(i)
- if turtle.compareTo(patchSlot) then
- turtle.transferTo(patchSlot, 64-patchCount)
- -- print("Transferred ", 64 - patchCount, " cobble to slot ", patchSlot)
- end
- if turtle.compareTo(garbageSlot) then
- turtle.transferTo(garbageSlot)
- end
- end
- end
- turtle.select(patchSlot)
- end
- function left()
- if face == 0 then face = 1 turtle.turnLeft() return end
- if face == 1 then face = 2 turtle.turnLeft() return end
- if face == 2 then face = 3 turtle.turnLeft() return end
- if face == 3 then face = 0 turtle.turnLeft() return end
- print("function left\(\): Bad face value: ", face)
- end
- function right()
- if face == 0 then face = 3 turtle.turnRight() return end
- if face == 1 then face = 0 turtle.turnRight() return end
- if face == 2 then face = 1 turtle.turnRight() return end
- if face == 3 then face = 2 turtle.turnRight() return end
- print("function right\(\): Bad face value: ", face)
- end
- function up()
- for i=1,10 do
- if not turtle.up() then
- if turtle.detectUp() then
- digUp()
- else
- turtle.attackUp()
- sleep(2)
- end
- else
- break
- end
- end
- y = y+1
- end
- function down()
- for i=1,10 do
- if not turtle.down() then
- if turtle.detectDown() then
- digDown()
- else
- turtle.attackDown()
- sleep(2)
- end
- else
- break
- end
- end
- y = y-1
- end
- function dig()
- if noMoreEmptySlots() then
- jettisonGarbage()
- goEmptyInventoryToChestAndReturn()
- end
- turtle.dig()
- end
- function digUp()
- if noMoreEmptySlots() then
- -- jettisonGarbage()
- goEmptyInventoryToChestAndReturn()
- end
- turtle.digUp()
- end
- function digDown()
- if noMoreEmptySlots() then
- -- jettisonGarbage()
- goEmptyInventoryToChestAndReturn()
- end
- turtle.digDown()
- end
- function noMoreEmptySlots()
- return (turtle.getItemCount(14) > 0)
- end
- function setFace(f)
- if f == 0 then
- if face == 0 then return end
- if face == 1 then right() return end
- if face == 2 then right() right() return end
- if face == 3 then left() return end
- end
- if f == 1 then
- if face == 0 then left() return end
- if face == 1 then return end
- if face == 2 then right() return end
- if face == 3 then right() right() return end
- end
- if f == 2 then
- if face == 0 then left() left() return end
- if face == 1 then left() return end
- if face == 2 then return end
- if face == 3 then right() return end
- end
- if f == 3 then
- if face == 0 then right() return end
- if face == 1 then left() left() return end
- if face == 2 then left() return end
- if face == 3 then return end
- end
- end
- -- digAboveForward: This function requires that the current level is already cleared
- function DigAboveForward()
- while turtle.detect() do -- This loop added in case of falling sand or whatever
- turtle.dig()
- end
- end
- function forward(keepTrackOfArea)
- if keepTrackOfArea == nil then
- keepTrackOfArea = true
- end
- turtle.select(chestSlot)
- if not turtle.compare() then
- turtle.select(garbageSlot)
- turtle.place() -- in case we're facing an undectable water/lava source block, replace
- end
- turtle.select(patchSlot)
- while turtle.detect() do -- This loop added in case of falling sand or whatever
- dig()
- end
- for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
- if not turtle.forward() then
- if turtle.detect() then
- dig()
- else
- turtle.attack()
- sleep(2)
- end
- else
- break
- end
- end
- if keepTrackOfArea then
- areaCovered = areaCovered + 1
- end
- torchCheck()
- if face == 0 then z = z+1 return end
- if face == 1 then x = x-1 return end
- if face == 2 then z = z-1 return end
- if face == 3 then x = x+1 return end
- end
- function sealRow(distance)
- local count = 0
- turtle.select(patchSlot)
- for count=1,distance do
- if y == height-1 then
- patchUp()
- if height > 1 then
- turtle.digDown()
- end
- -- turtle.placeUp() -- experiment: if solid block is there it will just fail. Maybe help with lava/water?
- end
- if y == 0 then
- patchDown()
- if height > 1 then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- end
- right()
- patch()
- left()
- if count < distance then
- forward()
- end
- end
- end
- function torchCheck()
- if addTorches then
- if y == 1 then
- if (x % torchSpacing) == 0 then
- if (z % torchSpacing) == 0 then
- turtle.select(torchSlot)
- turtle.placeDown()
- end
- end
- end
- end
- turtle.select(patchSlot)
- end
- function clearLevel()
- areaCovered = 0
- setFace(west)
- forward()
- setFace(north)
- forward()
- if y == height-1 then
- turtle.select(patchSlot)
- turtle.placeUp()
- end
- nextTurn = "left"
- areaCovered = 0
- while areaCovered < targetArea do
- if abort then
- break
- end
- for w=1,width-2 do
- if abort then
- -- print("clearLevel: ABORT ABORT ABORT")
- break
- end
- for l=1,length-2 do
- if abort then
- break
- end
- if l < length-2 then
- forward()
- if (y == 0) then
- turtle.placeDown()
- if height > 1 then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- end
- if y == height-1 then
- turtle.select(patchSlot) -- experiment: if solid block is there, it will just fail. Who cares?
- if turtle.getItemCount(patchSlot) < 1 then
- print("clearLevel: Ran out of patch material!")
- end
- turtle.placeUp()
- if height > 1 then
- turtle.digDown()
- end
- end
- end
- end
- if w == width-2 then
- -- print("There's no more to do at this level. Bailing out.")
- levelsCompleted = levelsCompleted + 1
- return
- end
- if w <= width-3 then
- if nextTurn == "left" then
- left()
- forward()
- if y == 0 then
- if height > 1 then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- turtle.placeDown()
- end
- if y == height-1 then
- turtle.select(patchSlot)
- turtle.placeUp()
- if height > 1 then
- turtle.digDown()
- end
- end
- left()
- nextTurn = "right"
- else
- right()
- forward()
- if y == 0 then
- if height > 1 then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- turtle.placeDown()
- end
- if y == height-1 then
- turtle.select(patchSlot)
- turtle.placeUp()
- if height > 1 then
- turtle.digDown()
- end
- end
- right()
- nextTurn = "left"
- end
- else
- break
- end
- end
- end
- levelsCompleted = levelsCompleted + 1
- end
- function bounce()
- local saveY = y
- for i=1,255 do
- if y > 0 then
- turtle.down()
- y = y - 1
- else
- break
- end
- end
- dump()
- while y<saveY do
- turtle.up()
- y = y + 1
- end
- end
- function sealLevel()
- sealRow(length)
- left()
- sealRow(width)
- left()
- sealRow(length)
- left()
- sealRow(width)
- left()
- end
- function sealWalls()
- local i = 0
- for i=1,height do
- sealLevel()
- if y ~= 1 then
- clearLevel()
- end
- if i < height then
- up()
- home()
- end
- end
- end
- function sealCeiling()
- areaCovered = 0
- while areaCovered < targetArea do
- if abort then
- break
- end
- for w=1,width do
- if abort then
- break
- end
- for z=1,length do
- if abort then
- break
- end
- if not turtle.detectUp() then
- patchUp()
- end
- if z < length then
- forward()
- end
- end
- if w < width then
- if nextTurn == "left" then
- left()
- forward()
- left()
- nextTurn = "right"
- else
- right()
- forward()
- right()
- nextTurn = "left"
- end
- else
- return
- end
- end
- end
- end
- function sealFloor()
- areaCovered = 0
- while areaCovered < targetArea do
- if abort then
- break
- end
- for w=1,width do
- if abort then
- break
- end
- for z=1,length do
- if abort then
- break
- end
- if not turtle.detectDown() then
- patchDown()
- end
- while turtle.detectUp() do
- turtle.digUp()
- end
- if z < length then
- forward()
- end
- end
- if w < width then
- if nextTurn == "left" then
- left()
- forward()
- left()
- nextTurn = "right"
- else
- right()
- forward()
- right()
- nextTurn = "left"
- end
- else
- return
- end
- end
- end
- end
- function dump()
- turtle.select(chestSlot)
- if not turtle.compareDown() then
- turtle.digDown()
- turtle.placeDown()
- setFace(west)
- forward()
- turtle.digDown()
- turtle.select(chestSlot)
- turtle.placeDown()
- setFace(east)
- forward()
- setFace(north)
- end
- gatherPatchMaterial()
- for i=2,4 do
- turtle.select(i)
- if not turtle.compareTo(patchSlot) then
- if not turtle.dropDown() then
- print("Dump failed. Chest is full.")
- return
- end
- end
- end
- for i=6,14 do
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- if not turtle.dropDown() then
- print("Dump failed. Chest is full.")
- return
- end
- end
- end
- turtle.select(patchSlot)
- setFace(north)
- end
- function home()
- if x < 0 then
- setFace(east)
- while x < 0 do
- forward()
- end
- else
- if x > 0 then
- setFace(west)
- while x > 0 do
- forward()
- end
- end
- end
- if z < 0 then
- setFace(north)
- while z < 0 do
- forward()
- end
- else
- if z > 0 then
- setFace(south)
- while z > 0 do
- forward()
- end
- end
- end
- setFace(north)
- nextTurn = "left"
- end
- function ceiling()
- while y < height-1 do
- up()
- end
- end
- function floor()
- while y > 0 do
- y = y - 1
- turtle.down()
- end
- end
- workHeight = height
- levelsCompleted = 0
- -- Clear the top level and seal the ceiling
- setFace(east)
- turtle.select(patchSlot)
- for i=1,height-1 do
- up()
- patch()
- end
- setFace(north)
- sealLevel()
- home()
- bounce()
- clearLevel()
- home()
- while y > 0 do
- turtle.down()
- y = y-1
- end
- dump()
- -- Clear the first level and seal the floor
- sealLevel()
- home()
- dump()
- clearLevel()
- home()
- dump()
- setFace(north)
- -- Clear the rest of the levels
- addTorches = true -- torchCheck only adds torches when turtle is at level 1
- workHeight = 1
- while levelsCompleted < height do
- while y < workHeight do
- up()
- end
- sealLevel()
- home()
- bounce()
- clearLevel()
- home()
- while y > 0 do
- turtle.down()
- y = y-1
- end
- dump()
- workHeight = workHeight + 1
- end
- home()
- while y > 0 do
- turtle.down()
- y = y-1
- end
- print(missionMessage)
- print("Fuel level is now ", turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement