ImmoralKoala

CC_Create_QuarryController

Jan 30th, 2022 (edited)
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None
  1. -- Blocks Per Second at 1 RPM
  2. baseBlocksPerSecond = 0.038
  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.  
  23. function resetRedstoneOutputs()
  24. redstone.setOutput("back", false)
  25. redstone.setOutput("left", false)
  26. redstone.setOutput("right", false)
  27. redstone.setOutput("bottom", false)
  28. end
  29.  
  30.  
  31. function resetExtension()
  32. redstoneExtension.setOutput("left", false)
  33. redstoneExtension.setOutput("back", false)
  34. redstoneExtension.setOutput("top", false)
  35. redstoneExtension.setOutput("right", false)
  36. end
  37.  
  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.  
  88. function convertToGrid(i, size)
  89. gridValue = math.floor(i / size) * size
  90.  
  91. return gridValue
  92. end
  93.  
  94.  
  95. function gridToPosition(i, size)
  96. position = i * size
  97. return position
  98. end
  99.  
  100.  
  101. -- Return Miner to Z 0
  102. function resetZ()
  103. resetRedstoneOutputs()
  104.  
  105. redstone.setOutput("left", true)
  106. redstone.setOutput("bottom", true)
  107. redstone.setOutput("back", true)
  108. os.sleep(moveWaitTime)
  109. while not redstoneExtension.getInput("back") do
  110. os.sleep(0.5)
  111. end
  112.  
  113. resetRedstoneOutputs()
  114. end
  115.  
  116.  
  117. function returnToZero()
  118. resetZ()
  119.  
  120. -- Move to 0 x
  121. redstone.setOutput("right", true)
  122. redstone.setOutput("back", true)
  123. os.sleep(moveWaitTime)
  124. while not redstoneExtension.getInput("back") do
  125. os.sleep(0.5)
  126. if(redstoneExtension.getInput("back")) then
  127. break
  128. end
  129. end
  130. currentX = 0
  131.  
  132. -- Move to 0 y
  133. redstone.setOutput("right", true)
  134. redstone.setOutput("back", true)
  135. redstone.setOutput("left", true)
  136. os.sleep(moveWaitTime)
  137. while not redstoneExtension.getInput("back") do
  138. os.sleep(0.5)
  139. if(redstoneExtension.getInput("back")) then
  140. break
  141. end
  142. end
  143. currentY = 0
  144.  
  145. end
  146.  
  147. function deposit()
  148. redstoneExtension.setOutput("right", true)
  149. redstone.setOutput("back", true)
  150. returnToZero()
  151. os.sleep(120)
  152. redstoneExtension.setOutput("right", false)
  153. redstone.setOutput("back", false)
  154. end
  155.  
  156.  
  157. -- Dig Down until bedrock, then return to Z 0
  158. function mine(speed)
  159. redstone.setOutput("left", true)
  160. redstone.setOutput("bottom", true)
  161. redstone.setOutput("right", true)
  162. redstone.setOutput("back", true)
  163.  
  164. os.sleep(0.75)
  165. while not redstoneExtension.getInput("back") do
  166. os.sleep(0.5)
  167. end
  168.  
  169. resetZ()
  170. end
  171.  
  172.  
  173. -- Move to Given Position
  174. function moveToPosition(x, y)
  175. resetZ()
  176. -- Calculate Difference in Positions
  177. local moveX = x - currentX
  178. local moveY = y - currentY
  179.  
  180. -- Calculate Speed
  181. local speed = baseBlocksPerSecond * rpm
  182.  
  183. -- Move To Desired Position and Update Current Position
  184. if (moveX ~= 0) then currentX = moveAxis(axis[1], moveX, speed) end
  185. if (moveY ~= 0) then currentY = moveAxis(axis[2], moveY, speed) end
  186. end
  187.  
  188.  
  189. function quarry(drillSize, startX, startY)
  190. io.write("Start X: ")
  191. local startX = tonumber(read())
  192. io.write("Start Y: ")
  193. local startY = tonumber(read())
  194.  
  195. for y = startY, ySize, drillSize do
  196. for x = startX, xSize, drillSize do
  197. print("X: " .. x)
  198. print("Y: " .. y)
  199. moveToPosition(x, y)
  200. mine()
  201. os.sleep(0.05)
  202. end
  203.  
  204. redstoneExtension.setOutput("right", true)
  205. redstone.setOutput("back", true)
  206. returnToZero()
  207. os.sleep(120)
  208. redstoneExtension.setOutput("right", false)
  209. redstone.setOutput("back", false)
  210. os.sleep(1)
  211. end
  212. end
  213.  
  214.  
  215. -- Get Bounds of XY Movement
  216. function calibrate()
  217. local speed = baseBlocksPerSecond * rpm
  218.  
  219. returnToZero()
  220.  
  221. resetRedstoneOutputs()
  222.  
  223. -- Get X Size
  224. local counter = 0
  225. redstone.setOutput("back", true)
  226. os.sleep(moveWaitTime)
  227. while not redstoneExtension.getInput("back") do
  228. counter = counter + 1
  229. end
  230.  
  231. currentX = math.floor(((counter * 0.05) * speed) + 0.5)
  232. xSize = currentX
  233.  
  234. -- Get Y Size
  235. counter = 0
  236. redstone.setOutput("left", true)
  237. redstone.setOutput("back", true)
  238. os.sleep(moveWaitTime)
  239. while not redstoneExtension.getInput("back") do
  240. counter = counter + 1
  241. end
  242.  
  243. currentY = math.floor(((counter * 0.05) * speed) + 0.5)
  244. ySize = currentY
  245.  
  246. print(xSize)
  247. print(ySize)
  248.  
  249. moveToPosition(0, 0)
  250.  
  251. resetRedstoneOutputs()
  252. end
  253.  
  254.  
  255. function init()
  256. if redstoneExtension == nil then error("Failed to find redstoneIntegrator") end
  257.  
  258. resetRedstoneOutputs()
  259. resetExtension()
  260.  
  261. term.clear()
  262. io.write("Current RPM: ")
  263. rpm = tonumber(read())
  264. io.write("Calibrate? [Y/N]: ")
  265. if(read() == "n") then
  266. io.write("X Size: ")
  267. xSize = tonumber(read())
  268. io.write("Y Size: ")
  269. ySize = tonumber(read())
  270. returnToZero()
  271. else
  272. calibrate()
  273. end
  274.  
  275. io.write("Drill Size: ")
  276. drillSize = tonumber(read())
  277. end
  278.  
  279.  
  280. function main()
  281. print("Options: ")
  282. print("1. Quarry ")
  283. print("2. Deposit")
  284. print("3. Return To Zero")
  285. io.write(">: ")
  286. userInput = read()
  287. if userInput == "1" then
  288. quarry(drillSize, startX, startY)
  289. elseif userInput == "2" then
  290. deposit()
  291. elseif userInput == "3" then
  292. returnToZero()
  293. end
  294.  
  295. end
  296.  
  297.  
  298. init()
  299. main()
RAW Paste Data Copied