red1reaper

CreateQuarryCC

Feb 6th, 2021 (edited)
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local defaults = {
  2.     rows = 16,
  3.     changeRowDelay = 1,
  4.     delayStart = 0
  5. }
  6.  
  7. local pretty = require "cc.pretty"
  8.  
  9. local usage = {
  10.     "if multiple of the same arg are given then the last one will be used - this is redundant don't do it",
  11.     "we will tell you the arg number which caused any errors",
  12.     " -h | prints this usage information",
  13.     " -r <integer/number> | number of rows to mine, defaults to 16",
  14.     " -d <float/number> | number of seconds to wait between row change, defaults to 1 second",
  15.     " -w <float/number> | number of seconds to wait until it starts, if not specified, starts immediately"
  16. }
  17.  
  18. local function printUsage()
  19.     blockInput = true
  20.     print(table.concat(usage, "\n"))
  21.     looping = false
  22. end
  23.  
  24. local function myError(msg, lvl)
  25.   printUsage()
  26.   error(msg, lvl+1)
  27. end
  28.  
  29.  
  30. local writtenStates= {
  31.     ["ropeDown"]="Mining",
  32.     ["ropeUp"]="Pulling the miner up",
  33.     ["nextRow"]="Moving to the next row",
  34.     ["retractStart"]="Retracting",
  35.     ["retractEnd"]="Ending the Program"
  36. }
  37.  
  38. local args = {...}
  39. local maxRows = defaults.rows
  40. local changeRowDelay = defaults.changeRowDelay
  41. local delayStart = defaults.delayStart
  42. local row = 1
  43. local state = "ropeDown"
  44. local looping = true
  45. local firstLoop = true
  46. local blockInput = false
  47.  
  48.  
  49.  
  50. local function updateStatus()
  51.     clear()
  52.     local screen = pretty.concat(
  53.         pretty.text("CQP - Create Quarry Program v0.1", colours.red),
  54.         pretty.text("______________________________________", colours.red),
  55.         pretty.line,
  56.         pretty.group(pretty.concat(pretty.text("Status:    ", colours.orange), pretty.text(writtenStates[state], colours.white))),
  57.         pretty.line,
  58.         pretty.group(pretty.concat(pretty.text("Row:       ", colours.orange), pretty.text(tostring(row) .. "/" .. tostring(maxRows), colours.white))),
  59.         pretty.line,
  60.         pretty.group(pretty.concat(pretty.text("Completed: ", colours.orange), pretty.text(tostring((row/maxRows)*100) .. "%", colours.white)))
  61.     )
  62.     pretty.print(screen)
  63. end
  64.  
  65.  
  66.  
  67.  
  68.  
  69. if #arg > 0 then
  70.     --lower case everything
  71.     for i = 1, #arg do
  72.         if not tonumber(arg[i]) then
  73.             arg[i] = string.lower(arg[i])
  74.         end
  75.     end--]]
  76.     --process arg
  77.     local i = 1
  78.     while i <= #arg do
  79.         local doubleInc = false
  80.         if arg[i] == "-h" then
  81.             looping = false
  82.             printUsage()
  83.             doubleInc = false
  84.         elseif arg[i] == "-r" then --maxRows
  85.             local r = tonumber(arg[i+1])
  86.             if r == nil then
  87.                 myError("arg "..i.." -r arg must be followed by a number", 2)
  88.             elseif not ((r % 1) == 0) then
  89.                 myError("arg "..i.." -r arg must be followed by an integer number, you put a float", 2)
  90.             end
  91.             maxRows = r
  92.             doubleInc = true
  93.         elseif arg[i] == "-d" then --changeRowDelay
  94.             local d = tonumber(arg[i+1])
  95.             if d == nil then
  96.                 myError("arg "..i.." -d arg must be followed by a number", 2)
  97.             end
  98.             changeRowDelay = d
  99.             doubleInc = true
  100.         elseif arg[i] == "-w" then --delayStart
  101.             local w = tonumber(arg[i+1])
  102.             if w == nil then
  103.                 myError("arg "..i.." -w arg must be followed by a number", 2)
  104.             end
  105.             delayStart = w
  106.             doubleInc = true
  107.         else
  108.             myError("arg "..i.." unprocessed arg",2)
  109.         end
  110.       if doubleInc then
  111.         i = i +2
  112.       else
  113.         i = i +1
  114.       end
  115.     end
  116.   end
  117.    
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. local function back(redstate)
  127.     rs.setOutput("back", redstate)
  128. end
  129. local function backSwitch()
  130.     back(not rs.getOutput("back"))
  131. end
  132.  
  133. local function front(redstate)
  134.     rs.setOutput("front", redstate)
  135. end
  136. local function frontSwitch()
  137.     front(not rs.getOutput("front"))
  138. end
  139.  
  140. local function left(redstate)
  141.     rs.setOutput("left", redstate)
  142. end
  143. local function leftSwitch()
  144.     left(not rs.getOutput("left"))
  145. end
  146.  
  147. local function right(redstate)
  148.     rs.setOutput("right", redstate)
  149. end
  150. local function rightSwitch()
  151.     right(not rs.getOutput("right"))
  152. end
  153.  
  154. local function top(redstate)
  155.     rs.setOutput("top", redstate)
  156. end
  157. local function topSwitch()
  158.     top(not rs.getOutput("top"))
  159. end
  160.  
  161. local function bottom(redstate)
  162.     rs.setOutput("bottom", redstate)
  163. end
  164. local function bottomSwitch()
  165.     bottom(not rs.getOutput("bottom"))
  166. end
  167.  
  168.  
  169.  
  170. local function retractEnd()
  171.    blockInput = true
  172.    --print("Reset Ending")
  173.    updateStatus()
  174.    back(false)
  175.    right(false)
  176.    front(false)
  177.    bottom(false)
  178.    top(false)
  179.    left(false)
  180.    looping = false  
  181.    sleep(1)
  182.    blockInput = false
  183. end
  184.    
  185. local function retractStart()
  186.     blockInput = true
  187.     --print("Reset Starting")
  188.     updateStatus()
  189.     --front(true)
  190.     --sleep(4.5)
  191.     back(true)
  192.     right(true)
  193.     sleep(1)
  194.     state = "retractEnd"  
  195.     blockInput = false
  196. end
  197.  
  198. local function ropeDown()
  199.     blockInput = true
  200.     --print("Sending the Drill down on row number " .. tostring(row) .. "/" .. tostring(maxRows))
  201.     updateStatus()
  202.     left(true)
  203.     sleep(1)
  204.     state = "ropeUp"
  205.     blockInput = false
  206. end
  207.  
  208. local function ropeUp()
  209.     blockInput = true
  210.     --print("Retracting the Drill down on row number " .. tostring(row) .. "/" .. tostring(maxRows))
  211.     updateStatus()
  212.     left(false)
  213.     sleep(1)
  214.     if row == maxRows then
  215.         front(true)
  216.         state = "retractStart"
  217.     else
  218.         state = "nextRow"
  219.     end
  220.     blockInput = false
  221. end
  222.  
  223. local function nextRow()
  224.     --print("Moving the Drill from row " .. tostring(row) .. "/" .. tostring(maxRows) .. " to " ..  tostring(row+1) .. "/" .. tostring(maxRows) )
  225.     updateStatus()
  226.     front(true)
  227.     sleep(0.5)
  228.     right(true)
  229.     sleep(changeRowDelay)
  230.     row = row + 1
  231.     updateStatus()
  232.     right(false)
  233.     sleep(1.5)
  234.     front(false)
  235.     state = "ropeDown"
  236. end
  237.  
  238. local states= {
  239.     ["ropeDown"]=ropeDown,
  240.     ["ropeUp"]=ropeUp,
  241.     ["nextRow"]=nextRow,
  242.     ["retractStart"]=retractStart,
  243.     ["retractEnd"]=retractEnd
  244. }
  245. if looping then
  246.     if delayStart > 0 then
  247.         sleep(delayStart)
  248.     end
  249.     while looping do
  250.         if (firstLoop) then
  251.             firstLoop = false
  252.             ropeDown()
  253.         end
  254.         os.pullEvent("redstone")
  255.         if rs.getInput("bottom") and not blockInput then
  256.             states[state]()
  257.         end
  258.     end
  259. end
Advertisement
Add Comment
Please, Sign In to add comment