Advertisement
ImmoralKoala

CC_Create_Quarry

Jan 28th, 2022 (edited)
584
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.44 KB | None
  1. -- Blocks Per Second at 1 RPM
  2. baseBlocksPerSecond = 0.0369
  3. moveWaitTime = 0.45
  4.  
  5. -- Used for Additional Redstone I/Os
  6. redstoneExtension = peripheral.find("redstoneIntegrator")
  7.  
  8. -- Current RPM
  9. rpm = 0
  10.  
  11. -- Current Axis Positions
  12. currentX = 0
  13. currentY = 0
  14.  
  15. -- Size
  16. xSize = 0
  17. ySize = 0
  18.  
  19. --Possible Axes
  20. axis = {"x", "y"}
  21.  
  22. -- Linear Interpolation
  23. function lerp(a,b,t) return a * (1-t) + b * t end
  24.  
  25. function resetRedstoneOutputs()
  26.     redstone.setOutput("back", false)
  27.     redstone.setOutput("left", false)
  28.     redstone.setOutput("right", false)
  29.     redstone.setOutput("bottom", false)
  30. end
  31.  
  32. function resetExtension()
  33.     redstoneExtension.setOutput("left", false)
  34.     redstoneExtension.setOutput("back", false)
  35.     redstoneExtension.setOutput("top", false)
  36.     redstoneExtension.setOutput("right", false)
  37. end
  38.  
  39. -- Move a Specific Axis by an Amount
  40. function moveAxis (selectedAxis, blocks, speed)
  41.     -- Declare Local Variables
  42.     local currentPosition = 0
  43.     local inverted = false
  44.  
  45.     -- Output Redstone Signal based on Axis
  46.     if selectedAxis == axis[1] then
  47.         redstone.setOutput("left", false)
  48.         currentPosition = currentX
  49.     elseif selectedAxis == axis[2] then
  50.         redstone.setOutput("left", true)
  51.         currentPosition = currentY
  52.     else
  53.         error("Attempted to Move by Non-Existent Axis")
  54.     end
  55.  
  56.     -- Should Travel be Inverted?
  57.     if blocks < 0 then
  58.         redstone.setOutput("right", true)
  59.         inverted = true
  60.     else
  61.         redstone.setOutput("right", false)
  62.         inverted = false
  63.     end
  64.  
  65.     -- Calculate Desired Position
  66.     local desiredPosition = currentPosition + blocks
  67.  
  68.     -- Calculate Travel Time
  69.     local travelTime = math.abs(blocks / speed)
  70.  
  71.     -- Start Movement
  72.     redstone.setOutput("back", true)
  73.  
  74.     -- Wait x Seconds, Finishing on arrival.
  75.     os.sleep(travelTime)
  76.  
  77.     currentPosition = desiredPosition
  78.  
  79.     -- Stopping Movement and Resetting to Default Redstone Values
  80.     resetRedstoneOutputs()
  81.    
  82.     -- Returning New Position
  83.     return currentPosition
  84.  
  85. end
  86.  
  87. -- Convert Coordinate to Grid Position
  88. function convertToGrid(i, size)
  89.     gridValue = math.floor((i / size) + 0.5) * size
  90.    
  91.     return gridValue
  92. end
  93.  
  94. function gridToPosition(i, size)
  95.     position = i * size
  96.     return position
  97. end
  98.  
  99. -- Return Miner to Z 0
  100. function resetZ()
  101.     resetRedstoneOutputs()
  102.  
  103.     redstone.setOutput("left", true)
  104.     redstone.setOutput("bottom", true)
  105.     redstone.setOutput("back", true)
  106.     os.sleep(moveWaitTime)
  107.     while not redstoneExtension.getInput("back") do
  108.         os.sleep(0.5)
  109.     end
  110.  
  111.     resetRedstoneOutputs()
  112. end
  113.  
  114. function returnToZero()
  115.     resetZ()
  116.  
  117.     -- Move to 0 x
  118.     redstone.setOutput("right", true)
  119.     redstone.setOutput("back", true)
  120.     os.sleep(moveWaitTime)
  121.     while not redstoneExtension.getInput("back") do
  122.         os.sleep(0.5)
  123.         if(redstoneExtension.getInput("back")) then
  124.             redstone.setOutput("back", false)
  125.             break
  126.         end
  127.     end
  128.     currentX = 0
  129.  
  130.     -- Move to 0 y
  131.     redstone.setOutput("right", true)
  132.     redstone.setOutput("back", true)
  133.     redstone.setOutput("left", true)
  134.     os.sleep(moveWaitTime)
  135.     while not redstoneExtension.getInput("back") do
  136.         os.sleep(0.5)
  137.         if(redstoneExtension.getInput("back")) then
  138.             redstone.setOutput("back", false)
  139.             break
  140.         end
  141.     end
  142.     currentY = 0
  143.    
  144. end
  145.  
  146. -- Dig Down until bedrock, then return to Z 0
  147. function mine(speed)
  148.     redstone.setOutput("left", true)
  149.     redstone.setOutput("bottom", true)
  150.     redstone.setOutput("right", true)
  151.     redstone.setOutput("back", true)
  152.  
  153.     os.sleep(0.75)
  154.     while not redstoneExtension.getInput("back") do
  155.         os.sleep(0.5)
  156.     end
  157.  
  158.     resetZ()
  159. end
  160.  
  161. -- Move to Given Position
  162. function moveToPosition(x, y)
  163.     resetZ()
  164.     -- Calculate Difference in Positions
  165.     local moveX = x - currentX
  166.     local moveY = y - currentY
  167.  
  168.     -- Calculate Speed
  169.     local speed = baseBlocksPerSecond * rpm
  170.  
  171.     -- Move To Desired Position and Update Current Position
  172.     if (moveX ~= 0) then currentX = moveAxis(axis[1], moveX, speed) end
  173.     if (moveY ~= 0) then currentY = moveAxis(axis[2], moveY, speed) end
  174. end
  175.  
  176. function quarry(drillSize, startX, startY)
  177.     for y = startY, ySize, drillSize do
  178.         for x = startX, xSize, drillSize do
  179.             print("X: " .. x)
  180.             print("Y: " .. y)
  181.             moveToPosition(x, y)
  182.             mine()
  183.             os.sleep(0.05)
  184.         end
  185.  
  186.         redstoneExtension.setOutput("right", true)
  187.         redstone.setOutput("back", true)
  188.         moveToPosition(0, 0)
  189.         os.sleep(120)
  190.         redstoneExtension.setOutput("right", false)
  191.         redstone.setOutput("back", false)
  192.         os.sleep(1)
  193.     end
  194. end
  195.  
  196. -- Get Bounds of XY Movement
  197. function calibrate()
  198.     local speed = baseBlocksPerSecond * rpm
  199.  
  200.     returnToZero()
  201.  
  202.     resetRedstoneOutputs()
  203.  
  204.     -- Get X Size
  205.     local counter = 0
  206.     redstone.setOutput("back", true)
  207.     os.sleep(moveWaitTime)
  208.     while not redstoneExtension.getInput("back") do
  209.         counter = counter + 1
  210.     end
  211.  
  212.     currentX = math.floor(((counter * 0.05) * speed) + 0.5)
  213.     xSize = currentX
  214.  
  215.     -- Get Y Size
  216.     counter = 0
  217.     redstone.setOutput("left", true)
  218.     redstone.setOutput("back", true)
  219.     os.sleep(moveWaitTime)
  220.     while not redstoneExtension.getInput("back") do
  221.         counter = counter + 1
  222.     end
  223.  
  224.     currentY = math.floor(((counter * 0.05) * speed) + 0.5)
  225.     ySize = currentY
  226.  
  227.     print(xSize)
  228.     print(ySize)
  229.  
  230.     moveToPosition(0, 0)
  231.  
  232.     resetRedstoneOutputs()
  233. end
  234.  
  235. -- Initialisation
  236. function init()
  237.     if redstoneExtension == nil then error("Failed to find redstoneIntegrator") end
  238.  
  239.     resetRedstoneOutputs()
  240.     resetExtension()
  241.  
  242.     term.clear()
  243.     io.write("Current RPM: ")
  244.     rpm = tonumber(read())
  245.     io.write("Calibrate? [Y/N]: ")
  246.     if(read() == "n") then
  247.         io.write("X Size: ")
  248.         xSize = tonumber(read())
  249.         io.write("Y Size: ")
  250.         xSize = tonumber(read())
  251.         returnToZero()
  252.     else
  253.         calibrate()
  254.     end
  255. end
  256.  
  257. init()
  258.  
  259. -- Move To Pos Interaction
  260. while true do
  261.     term.clear()
  262.     io.write("Drill Size: ")
  263.     local drillSize = tonumber(read())
  264.     io.write("Start X: ")
  265.     local startX = tonumber(read())
  266.     io.write("Start Y: ")
  267.     local startY = tonumber(read())
  268.     quarry(drillSize, startX, startY)
  269. end
Advertisement
RAW Paste Data Copied
Advertisement