Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local startingPosition = { x = 0, y = 0, z = 0}
- local blocksExplored = {}
- local blockQueue = {}
- local logMessages = {}
- local dirWorld = "north"
- term.write("Enable debug output mode?: ")
- local debugOn = true
- if(io.read("*l") == "false") then
- debugOn = false
- end
- term.write("Enter turtle's x coordinate: ")
- startingPosition.x = io.read("*l")
- term.write("Enter turtle's y coordinate: ")
- startingPosition.y = io.read("*l")
- term.write("Enter turtle's z coordinate: ")
- startingPosition.z = io.read("*l")
- local currentPosition = startingPosition
- local function tableString(tbl)
- local str = "{"
- for k,v in pairs(tbl) do
- str = str .. k .. "=" .. v
- end
- str = str .. "}"
- return str
- end
- local function consumeFuel()
- for i = 1, 16, 1 do
- local fuelDifference = turtle.getFuelLimit() - turtle.getFuelLevel()
- if(fuelDifference < (80 * 64)) then
- turtle.refuel()
- end
- end
- term.write("[CaveEX]: New fuel amount is " .. turtle.getFuelLevel() .. " actions/moves.")
- end
- local function compactPosition()
- return "x: " .. currentPosition.x .. ", y: " .. currentPosition.y .. ", z: " .. currentPosition.z
- end
- local function logMessage(msg)
- logMessages[#logMessages + 1] = tostring(msg)
- if(debugOn) then print(msg) end
- end
- local function updatePosition()
- if(dirWorld == "north") then
- startingPosition.z = startingPosition.z + 1
- elseif(dirWorld == "south") then
- startingPosition.z = startingPosition.z - 1
- elseif(dirWorld == "west") then
- startingPosition.x = startingPosition.x - 1
- elseif(dirWorld == "east") then
- startingPosition.x = startingPosition.x + 1
- end
- end
- local function forward(n)
- for i = 1, n, 1 do
- turtle.forward()
- updatePosition()
- end
- end
- local function noDuplicate(newEntry)
- for i, v in pairs(blocksExplored) do
- if(v == newEntry) then
- return false
- end
- end
- for i, v in pairs(blockQueue) do
- if(v == newEntry) then
- return false
- end
- end
- return true
- end
- local function dirToOffset()
- if(dirWorld == "north") then
- return 0, 1
- elseif(dirWorld == "south") then
- return 0, -1
- elseif(dirWorld == "west") then
- return -1, 0
- elseif(dirWorld == "east") then
- return 1, 0
- end
- end
- local function leftTurn()
- if(dirWorld == "north") then
- dirWorld = "west"
- elseif(dirWorld == "west") then
- dirWorld = "south"
- elseif(dirWorld == "south") then
- dirWorld = "east"
- elseif(dirWorld == "east") then
- dirWorld = "north"
- end
- end
- local function addIfAirFront()
- consumeFuel()
- if(not turtle.detect()) then
- local x0, z0 = dirToOffset()
- local newEntry = { x = currentPosition.x + x0, y = currentPosition.y, z = currentPosition.z + z0 }
- if(noDuplicate(newEntry)) then
- blockQueue[#blockQueue + 1] = newEntry
- logMessage("[CaveEX]: blockQueue += " .. tableString(blockQueue[#blockQueue]))
- end
- else
- local x0, z0 = dirToOffset()
- local success, data = turtle.inspect()
- local newEntry = { x = currentPosition.x + x0, y = currentPosition.y, z = currentPosition.z + z0, name = data.name, meta = data.metadata }
- if(noDuplicate(newEntry)) then
- blocksExplored[#blocksExplored + 1] = newEntry
- logMessage("[CaveEX]: blocksExplored += " .. tableString(blocksExplored[#blocksExplored]))
- end
- end
- turtle.turnLeft()
- leftTurn()
- end
- local function addIfAir()
- if(not turtle.detectDown()) then
- local newEntry = { x = currentPosition.x, y = currentPosition.y - 1, z = currentPosition.z }
- if(noDuplicate(newEntry)) then
- blockQueue[#blockQueue + 1] = newEntry
- logMessage("[CaveEX]: blockQueue += " .. tableString(blockQueue[#blockQueue]))
- end
- else
- local success, data = turtle.inspectDown()
- local newEntry = { x = currentPosition.x, y = currentPosition.y - 1, z = currentPosition.z, name = data.name, meta = data.metadata }
- if(noDuplicate(newEntry)) then
- blocksExplored[#blocksExplored + 1] = newEntry
- logMessage("[CaveEX]: blocksExplored += " .. tableString(blocksExplored[#blocksExplored]))
- end
- end
- if(not turtle.detectUp()) then
- local newEntry = { x = currentPosition.x, y = currentPosition.y + 1, z = currentPosition.z }
- if(noDuplicate(newEntry)) then
- blockQueue[#blockQueue + 1] = newEntry
- logMessage("[CaveEX]: blockQueue += " .. tableString(blockQueue[#blockQueue]))
- end
- else
- local success, data = turtle.inspectUp()
- local newEntry = { x = currentPosition.x, y = currentPosition.y + 1, z = currentPosition.z, name = data.name, meta = data.metadata }
- if(noDuplicate(newEntry)) then
- blocksExplored[#blocksExplored + 1] = newEntry
- logMessage("[CaveEX]: blocksExplored += " .. tableString(blocksExplored[#blocksExplored]))
- end
- end
- addIfAirFront()
- addIfAirFront()
- addIfAirFront()
- addIfAirFront()
- end
- term.write("CaveEX starting from " .. compactPosition())
- consumeFuel()
- -- Populate blockQueue with the blocks around us.
- addIfAir()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement