Neon1432

quarrycontroller

Nov 7th, 2025 (edited)
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | None | 0 0
  1. ---Loads a locally saved table and returns it
  2. ---@param path string a valid filepath
  3. ---@return table params or empty table
  4. local function loadParams(path)
  5.     local file = fs.open(path, "r")
  6.     if file ~= nil then
  7.         local data = file.readAll()
  8.         file.close()
  9.         return textutils.unserialize(data)
  10.     else
  11.         return {}
  12.     end
  13. end
  14.  
  15. ---Saves the table at the defined path
  16. ---@param path string a valid filepath
  17. ---@param table table paramtable to save
  18. local function saveParams(path, table)
  19.     local file = fs.open(path, "w")
  20.     file.write(textutils.serialize(table))
  21.     file.close()
  22. end
  23.  
  24. local STEPSIZE = 5
  25. local FIELDSIZE = 7
  26. local SECONDSWAITINGONDRILL = 260
  27. local PARAMPATH = "files/quarry"
  28.  
  29. local params = loadParams(PARAMPATH)
  30.  
  31. local function setRedstoneOutput(side, active)
  32.     if active == nil or active then
  33.         redstone.setOutput(side, true)
  34.     else
  35.         redstone.setOutput(side, false)
  36.     end
  37. end
  38.  
  39. local function stopRotation()
  40.     setRedstoneOutput("right", true)
  41.     setRedstoneOutput("top", true)
  42. end
  43.  
  44. local function slowerSpeed(active)
  45.     setRedstoneOutput("top", active)
  46.     setRedstoneOutput("right", not active)
  47. end
  48.  
  49. local function reverseRotation(active)
  50.     setRedstoneOutput("bottom", active)
  51. end
  52.  
  53. local function activateFirstCarriage(active)
  54.     setRedstoneOutput("left", active)
  55. end
  56.  
  57. local function activateSecondCarriage(active)
  58.     stopRotation()
  59.     setRedstoneOutput("back", active)
  60.     sleep(0.5)
  61. end
  62.  
  63. local function writeText(text)
  64.     term.clear()
  65.     term.setCursorPos(1, 1)
  66.     print(text)
  67. end
  68.  
  69. local function skipCurrent()
  70.     params = loadParams(PARAMPATH)
  71.     if params.skip ~= nil then
  72.         if params.skip == true then
  73.             params.skip = false
  74.             reverseRotation(false)
  75.             saveParams(PARAMPATH, params)
  76.             writeText("skipping current")
  77.             return true
  78.         end
  79.     end
  80.     return false
  81. end
  82.  
  83. local function skipRow()
  84.     params = loadParams(PARAMPATH)
  85.     if params.skiprow ~= nil then
  86.         if params.skiprow == true then
  87.             params.skiprow = false
  88.             saveParams(PARAMPATH, params)
  89.             writeText("skipping row")
  90.             return true
  91.         end
  92.     end
  93.     return false
  94. end
  95.  
  96. local function drill()
  97.     activateFirstCarriage()
  98.     activateSecondCarriage()
  99.     reverseRotation()
  100.     slowerSpeed(false)
  101.     if not skipCurrent() then
  102.         for i = 1, SECONDSWAITINGONDRILL, 1 do
  103.             sleep(1)
  104.             writeText(tostring(i) .. " seconds passed from " .. tostring(SECONDSWAITINGONDRILL))
  105.             if skipCurrent() then
  106.                 break
  107.             end
  108.         end
  109.         reverseRotation(false)
  110.         for i = 1, math.ceil(SECONDSWAITINGONDRILL / 15), 1 do
  111.             sleep(1)
  112.         end
  113.     end
  114. end
  115.  
  116. local function moveFirstCarriage()
  117.     activateFirstCarriage(false)
  118.     activateSecondCarriage(false)
  119.     slowerSpeed(true)
  120.     reverseRotation(true)
  121.     for i = 1, STEPSIZE, 1 do
  122.         sleep(0.85)
  123.     end
  124. end
  125.  
  126. local function callbackFirstCarriage()
  127.     activateFirstCarriage(false)
  128.     activateSecondCarriage(false)
  129.     slowerSpeed(false)
  130.     sleep(0.5)
  131.     reverseRotation(false)
  132.     for i = 1, FIELDSIZE, 1 do
  133.         sleep(1)
  134.     end
  135. end
  136.  
  137. local function moveSecondCarriage()
  138.     activateFirstCarriage(true)
  139.     activateSecondCarriage(false)
  140.     slowerSpeed(true)
  141.     sleep(0.5)
  142.     reverseRotation(true)
  143.     for i = 1, STEPSIZE, 1 do
  144.         sleep(0.85)
  145.     end
  146. end
  147.  
  148. local function callbackSecondCarriage()
  149.     activateFirstCarriage(true)
  150.     activateSecondCarriage(false)
  151.     slowerSpeed(false)
  152.     sleep(0.5)
  153.     reverseRotation(false)
  154.     for i = 1, FIELDSIZE, 1 do
  155.         sleep(1)
  156.     end
  157. end
  158.  
  159. local function executeAll()
  160.     callbackFirstCarriage()
  161.     for rows = 1, FIELDSIZE, 1 do
  162.         for columns = 1, FIELDSIZE, 1 do
  163.             while params.stop do
  164.                 stopRotation()
  165.                 sleep(1)
  166.                 writeText("waiting to proceed")
  167.                 params = loadParams(PARAMPATH)
  168.             end
  169.             if skipRow() then
  170.                 break
  171.             end
  172.             drill()
  173.             moveSecondCarriage()
  174.         end
  175.         callbackSecondCarriage()
  176.         moveFirstCarriage()
  177.     end
  178. end
  179.  
  180. local function test()
  181.     callbackFirstCarriage()
  182.     moveSecondCarriage()
  183.     callbackSecondCarriage()
  184.     moveFirstCarriage()
  185. end
  186.  
  187. executeAll()
  188. -- test()
  189.  
Advertisement
Add Comment
Please, Sign In to add comment