Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Blocks Per Second at 1 RPM
- baseBlocksPerSecond = 0.038
- moveWaitTime = 0.45
- -- Used for Additional Redstone I/Os
- redstoneExtension = peripheral.find("redstoneIntegrator")
- -- Current RPM
- rpm = 0
- -- Current Axis Positions
- currentX = 0
- currentY = 0
- -- Size
- xSize = 0
- ySize = 0
- --Possible Axes
- axis = {"x", "y"}
- function resetRedstoneOutputs()
- redstone.setOutput("back", false)
- redstone.setOutput("left", false)
- redstone.setOutput("right", false)
- redstone.setOutput("bottom", false)
- end
- function resetExtension()
- redstoneExtension.setOutput("left", false)
- redstoneExtension.setOutput("back", false)
- redstoneExtension.setOutput("top", false)
- redstoneExtension.setOutput("right", false)
- end
- -- Move a Specific Axis by an Amount
- function moveAxis (selectedAxis, blocks, speed)
- -- Declare Local Variables
- local currentPosition = 0
- local inverted = false
- -- Output Redstone Signal based on Axis
- if selectedAxis == axis[1] then
- redstone.setOutput("left", false)
- currentPosition = currentX
- elseif selectedAxis == axis[2] then
- redstone.setOutput("left", true)
- currentPosition = currentY
- else
- error("Attempted to Move by Non-Existent Axis")
- end
- -- Should Travel be Inverted?
- if blocks < 0 then
- redstone.setOutput("right", true)
- inverted = true
- else
- redstone.setOutput("right", false)
- inverted = false
- end
- -- Calculate Desired Position
- local desiredPosition = currentPosition + blocks
- -- Calculate Travel Time
- local travelTime = math.abs(blocks / speed)
- -- Start Movement
- redstone.setOutput("back", true)
- -- Wait x Seconds, Finishing on arrival.
- os.sleep(travelTime)
- currentPosition = desiredPosition
- -- Stopping Movement and Resetting to Default Redstone Values
- resetRedstoneOutputs()
- -- Returning New Position
- return currentPosition
- end
- function convertToGrid(i, size)
- gridValue = math.floor(i / size) * size
- return gridValue
- end
- function gridToPosition(i, size)
- position = i * size
- return position
- end
- -- Return Miner to Z 0
- function resetZ()
- resetRedstoneOutputs()
- redstone.setOutput("left", true)
- redstone.setOutput("bottom", true)
- redstone.setOutput("back", true)
- os.sleep(moveWaitTime)
- while not redstoneExtension.getInput("back") do
- os.sleep(0.5)
- end
- resetRedstoneOutputs()
- end
- function returnToZero()
- resetZ()
- -- Move to 0 x
- redstone.setOutput("right", true)
- redstone.setOutput("back", true)
- os.sleep(moveWaitTime)
- while not redstoneExtension.getInput("back") do
- os.sleep(0.5)
- if(redstoneExtension.getInput("back")) then
- break
- end
- end
- currentX = 0
- -- Move to 0 y
- redstone.setOutput("right", true)
- redstone.setOutput("back", true)
- redstone.setOutput("left", true)
- os.sleep(moveWaitTime)
- while not redstoneExtension.getInput("back") do
- os.sleep(0.5)
- if(redstoneExtension.getInput("back")) then
- break
- end
- end
- currentY = 0
- end
- function deposit()
- redstoneExtension.setOutput("right", true)
- redstone.setOutput("back", true)
- returnToZero()
- os.sleep(120)
- redstoneExtension.setOutput("right", false)
- redstone.setOutput("back", false)
- end
- -- Dig Down until bedrock, then return to Z 0
- function mine(speed)
- redstone.setOutput("left", true)
- redstone.setOutput("bottom", true)
- redstone.setOutput("right", true)
- redstone.setOutput("back", true)
- os.sleep(0.75)
- while not redstoneExtension.getInput("back") do
- os.sleep(0.5)
- end
- resetZ()
- end
- -- Move to Given Position
- function moveToPosition(x, y)
- resetZ()
- -- Calculate Difference in Positions
- local moveX = x - currentX
- local moveY = y - currentY
- -- Calculate Speed
- local speed = baseBlocksPerSecond * rpm
- -- Move To Desired Position and Update Current Position
- if (moveX ~= 0) then currentX = moveAxis(axis[1], moveX, speed) end
- if (moveY ~= 0) then currentY = moveAxis(axis[2], moveY, speed) end
- end
- function quarry(drillSize, startX, startY)
- io.write("Start X: ")
- local startX = tonumber(read())
- io.write("Start Y: ")
- local startY = tonumber(read())
- for y = startY, ySize, drillSize do
- for x = startX, xSize, drillSize do
- print("X: " .. x)
- print("Y: " .. y)
- moveToPosition(x, y)
- mine()
- os.sleep(0.05)
- end
- redstoneExtension.setOutput("right", true)
- redstone.setOutput("back", true)
- returnToZero()
- os.sleep(120)
- redstoneExtension.setOutput("right", false)
- redstone.setOutput("back", false)
- os.sleep(1)
- end
- end
- -- Get Bounds of XY Movement
- function calibrate()
- local speed = baseBlocksPerSecond * rpm
- returnToZero()
- resetRedstoneOutputs()
- -- Get X Size
- local counter = 0
- redstone.setOutput("back", true)
- os.sleep(moveWaitTime)
- while not redstoneExtension.getInput("back") do
- counter = counter + 1
- end
- currentX = math.floor(((counter * 0.05) * speed) + 0.5)
- xSize = currentX
- -- Get Y Size
- counter = 0
- redstone.setOutput("left", true)
- redstone.setOutput("back", true)
- os.sleep(moveWaitTime)
- while not redstoneExtension.getInput("back") do
- counter = counter + 1
- end
- currentY = math.floor(((counter * 0.05) * speed) + 0.5)
- ySize = currentY
- print(xSize)
- print(ySize)
- moveToPosition(0, 0)
- resetRedstoneOutputs()
- end
- function init()
- if redstoneExtension == nil then error("Failed to find redstoneIntegrator") end
- resetRedstoneOutputs()
- resetExtension()
- term.clear()
- io.write("Current RPM: ")
- rpm = tonumber(read())
- io.write("Calibrate? [Y/N]: ")
- if(read() == "n") then
- io.write("X Size: ")
- xSize = tonumber(read())
- io.write("Y Size: ")
- ySize = tonumber(read())
- returnToZero()
- else
- calibrate()
- end
- io.write("Drill Size: ")
- drillSize = tonumber(read())
- end
- function main()
- print("Options: ")
- print("1. Quarry ")
- print("2. Deposit")
- print("3. Return To Zero")
- io.write(">: ")
- userInput = read()
- if userInput == "1" then
- quarry(drillSize, startX, startY)
- elseif userInput == "2" then
- deposit()
- elseif userInput == "3" then
- returnToZero()
- end
- end
- init()
- main()
Add Comment
Please, Sign In to add comment