Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Start of my quarry 'script'.
- -- Learning Lua so this may be rough.
- -- Idea is to just mine out a Z * X size cube until the turtle hits bottom.
- --[[ things to worry about:
- -- Fuel levels
- -- Inventory levels
- -- distance from home
- ]]--
- local __update = false -- Making use of an autoupdater script I found, by ensuring this script is always up to date, and only updates upon request instead of whenever I run it. (May actually end up making something akin to apt, yum, etc.)
- local __version = 0.01
- -- Some of these are future proof definitions. Small program, and small inefficiencies like extra memory shouldn't hog server resources _too_ much. If I start killing the server I can adjust.
- local __home = 0
- local __returnhome = false
- local currentX = 0
- local currentY = 0
- local currentZ = 0
- local __paused = false
- local __direction = "north"
- local function turnWest()
- if __direction == "north" then
- turtle.turnLeft()
- __direction = "west"
- end
- if __direction == "east" then
- turtle.turnRight()
- turtle.turnRight()
- __direction = "west"
- end
- if __direction == "south" then
- turtle.turnRight()
- __direction = "west"
- end
- end
- local function turnSouth()
- if __direction == "north" then
- turtle.turnRight()
- turtle.turnRight()
- elseif __direction == "east" then
- turtle.turnRight()
- elseif __direction == "west" then
- turtle.turnLeft()
- end
- end
- local function distanceToHome() -- Simple distance calculation.
- return currentX + currentY + currentZ
- end
- local function safeFuelLevel(currentX, currentY, currentZ)
- local safeFuelLevel = true
- local safetyCushion = 10
- if turtle.getFuelLevel() <= (distanceToHome() + safetyCushion) then
- safeFuelLevel = false
- print("Debug: fuel level safety trigger flagged.")
- end
- return safeFuelLevel
- end
- local function tryDropItems()
- for i = 2, 16 do
- turtle.select(i)
- if not turtle.dropUp() then
- -- If we can't drop, we're not at an inventory
- return false
- end
- end
- turtle.select(1)
- return true
- end
- local function returnHome()
- while currentX > 0 do
- if __direction ~= "west" then
- turtle.turnRight()
- end
- turtle.forward()
- end
- while currentY > 0 do
- if __direction ~= "south" then
- turnSouth()
- end
- turtle.forward()
- end
- while currentZ > 0 do
- turtle.up()
- end
- turnSouth()
- if tryDropItems() ~= true then
- print("Danger Will Robinson! We don't have an inventory!")
- end
- end
- local function inventoryNotFull()
- if turtle.getItemCount(16) > 0 then
- print("Debug: inventory level safety trigger flagged.")
- return false
- else return true
- end
- end
- local function fuelAndInventorySafe()
- print("Debug: fuelAndInventorySafe function called.")
- print("Debug: inventoryNotFull() status: " .. tostring(inventoryNotFull()))
- print("Debug: safeFuelLevel() status: " .. tostring(safeFuelLevel(currentX, currentY, currentZ)))
- if inventoryNotFull() ~= true then
- __returnhome = true
- returnHome()
- end
- if safeFuelLevel(currentX, currentY, currentZ) ~= true then
- __returnhome = true
- returnHome()
- end
- return true
- end
- local function quarry(width, length) --width = x, length = y
- if width == nil then
- width = 0
- end
- if length == nil then
- length = 0
- end
- print("Debug: Sanity Checks")
- local inventoryStatus = inventoryNotFull()
- local fuelStatus = safeFuelLevel(currentX, currentY, currentZ)
- print("Debug: Inventory status: " .. tostring(inventoryStatus))
- print("Debug: Fuel status: " .. tostring(fuelStatus))
- print("Debug: width: " .. tostring(width))
- print("Debug: length: " .. tostring(length))
- print("Debug: currentX: " .. tostring(currentX))
- print("Debug: currentY: " .. tostring(currentY))
- print("Debug: currentZ: " .. tostring(currentZ))
- local reverseY = false
- local reverseX = false
- --[ loop flow
- -- 1. Check if something is in front
- -- 1a. if something is in front. Mine it
- -- 2. Move forward, increment currentY
- -- 3. Repeat 1 and 2 until currentY equals length
- -- 4. turn right if currentX % 2 = 0, else turn left
- -- 4a. repeat step one and 2
- -- 4b. repeat step 4 & 4a
- -- 5. repeat steps 1-4 until currentX equals width
- -- 6. Detect down
- -- 6a. If something is down, mine it
- -- 6b. Move down
- -- 6c. move 180 degrees.
- -- 7. Repeat.
- -- ]
- --
- -- Home logic flow
- -- Fuel? -> Home Distance? -> Full inventory?
- while __returnhome ~= true do
- -- Check for fuel and inventory here?
- -- Sanity check
- print("Debug: __returnhome loop call")
- while currentX < width and currentX >= 0 do
- -- Sanity check
- print("Debug: width loop call")
- -- Fuel and Inventory check.
- --
- if not fuelAndInventorySafe() then
- print("Debug: __returnhome flag triggered in width loop")
- __returnhome = true
- break
- end
- --Debug insanity, while loop is not being entered even though it has the _exact_ same logic as the previous one.
- print("Debug: Y condition check:")
- print("currentY < length: " .. tostring(currentY < length))
- print("currentY >= 0: " .. tostring(currentY >= 0))
- print("Combined condition: " .. tostring(currentY < length and currentY >= 0))
- while currentY < length and currentY >= 0 do
- -- Sanity Debug check
- print("Debug: length loop call")
- -- Fuel and Inventory check.
- --
- if not fuelAndInventorySafe() then
- print("Debug: __returnhome flag triggered in length loop")
- __returnhome = true
- returnHome()
- break
- end
- local success, data = turtle.inspect()
- if success then
- if data.name == "minecraft:bedrock" then
- __returnhome = true
- returnHome()
- break
- end
- turtle.dig()
- turtle.forward()
- else turtle.forward()
- end
- if reverseY == false then
- currentY = currentY + 1
- else currentY = currentY - 1
- end
- end
- print("Debug: Preparing turn. Current direction: " .. __direction)
- if currentX % 2 == 0 then
- print("Debug: Turning right")
- turtle.turnRight()
- __direction = "east"
- else turtle.turnLeft()
- print("Debug: Turning left")
- __direction = "west"
- end
- local success, data = turtle.inspect()
- if success then
- if data.name == "minecraft:bedrock" then
- __returnhome = true
- returnHome()
- break
- end
- turtle.dig()
- turtle.forward()
- else turtle.forward()
- end
- print("Debug: Preparing second turn. Current direction: " .. __direction)
- if currentX % 2 == 0 then
- print("Debug: Turning right part 2")
- turtle.turnRight()
- __direction = "south"
- else turtle.turnLeft()
- print("Debug: Turning left part 2")
- __direction = "north"
- end
- if reverseX == false then
- currentX = currentX + 1
- else currentX = currentX - 1
- end
- if reverseY == false then
- reverseY = true
- else reverseY = false
- end
- local success, data = turtle.inspect()
- if success then
- if data.name == "minecraft:bedrock" then
- __returnhome = true
- returnHome()
- break
- end
- turtle.dig()
- turtle.forward()
- else turtle.forward()
- end
- end
- local success, data = turtle.inspectDown()
- if success then
- if data.name == "minecraft:bedrock" then
- __returnhome = true
- returnHome()
- break
- end
- turtle.digDown()
- turtle.down()
- else turtle.down()
- end
- currentZ = currentZ + 1
- turtle.turnRight()
- turtle.turnRight()
- if __direction == "north" then
- __direction = "south"
- else __direction = "north"
- end
- if reverseX == false then
- reverseX = true
- else reverseX = false
- end
- if reverseY == false then
- reverseY = true
- else reverseY = false
- end
- end
- end
- quarry(16, 16)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement