Advertisement
Shurhaian

Factory Control Script

Mar 3rd, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.79 KB | None | 0 0
  1. -- SkunkWorks Logistics
  2. -- Joint factory control script
  3.  
  4. -- Engine variables
  5. local engineSide = "left" -- Engines on this side - always bundled
  6. local maxEngines = 8 -- Number of engines connected
  7.  
  8. -- Cycler variables
  9. local cycleSide = "right" -- Transposer output on this side
  10. local cycleBundle = true -- Bundled cable vs single-conductor transposer line
  11. local pulseLength = 0.1 -- Pulse "on" time in seconds
  12. local pulsePeriod = 0.75 -- Pulse cycle length
  13. local pulseDelay -- This will be set later - the "off" time of each pulse
  14. local minPeriod = 0.75 -- Shorter than this, transposers may jam
  15. local maxPeriod = 5.75 -- Nothing should need a longer period than this
  16. if pulsePeriod < 0.6 then -- Too short a pulse to reliably cycle transposers
  17.     pulsePeriod = 0.6
  18. end
  19. local signalsOn = {true, false, false, false} -- Output(s) to trigger - if not cycleBundle then this is meaningless
  20.  
  21. -- Display variables
  22. local monSide = "top" -- Monitor location
  23. local defColor=colors.lightGray -- Default text color in case of color terminal
  24. local mon=peripheral.wrap(monSide)
  25. mon.setTextScale(0.5)
  26. local maxX, maxY = mon.getSize()
  27. local graphX = (maxX/2) - 2 -- Each graph side is half the screen, less two pixels
  28. local graphY = maxY - 3 -- Leave a bit of space for captions
  29.  
  30.  
  31. -- Display functions
  32. local function writeColor( msg, color ) -- Used to avoid sending color codes to non-color term
  33.     if term.isColor then
  34.         term.setTextColor(color)
  35.         term.write(msg) -- We don't want a linefeed, so don't print()
  36.         term.setTextColor(defColor) -- Set the color back afterwards
  37.     else
  38.         term.write(msg) -- We still don't want a linefeed
  39.     end
  40. end
  41.  
  42. local function writeOnOff( isOn ) -- Given bool isOn, write if it's on or off
  43.     if isOn then -- Signal is on
  44.         writeColor("ON ",colors.lime)
  45.     else
  46.         writeColor("OFF",colors.red)
  47.     end
  48. end
  49.    
  50.    
  51. local function lineFeed() -- Used to move to a new line on the monitor, or scroll if it's at the bottom. Deprecated.
  52.     local oldx, oldy = mon.getCursorPos()
  53.     if oldy==maxY then
  54.         mon.scroll(1)
  55.         mon.setCursorPos(1,oldy)
  56.     else
  57.         local newy = oldy + 1
  58.         mon.setCursorPos(1, newy)
  59.     end
  60. end
  61.  
  62. local function engineGraph() -- Used to graph the engine power
  63.     term.redirect(mon) -- Send drawing instructions to the monitor
  64.     term.setCursorPos(2, 1)
  65.     term.setTextColor(colors.lightBlue)
  66.     term.write("Engines")
  67.     term.setCursorPos(2, 2)
  68.     term.setTextColor(math.max(engineLevel,colors.white))
  69.     term.write(engineLevel.." / "..maxEngines)
  70.     local engineHeight = math.floor (graphY * engineLevel / maxEngines) -- Take the proportional height out of the graph space, round down
  71.     if engineHeight < 1 then
  72.         engineHeight = 1 -- Graph should be at least one high
  73.     end
  74.     local engineY = math.max(maxY - engineHeight, 3)
  75.     local engineRight = 1 + graphX -- Right-side boundary of the engine graph
  76.     paintutils.drawLine ( 2, 3, 2, maxY, math.max(engineLevel,colors.white) ) -- Start the outline - left side
  77.     paintutils.drawLine ( engineRight, 3, engineRight, maxY, math.max(engineLevel,colors.white) ) -- Right side
  78.     paintutils.drawLine ( 2, 3, engineRight, 3, math.max(engineLevel,colors.white) ) -- Top
  79.     paintutils.drawLine ( 2, maxY, engineRight, maxY, math.max(engineLevel,colors.white) ) -- Bottom
  80.     for i = 2, engineRight do
  81.         paintutils.drawLine ( i, maxY, i, engineY, math.max(engineLevel,colors.white)) -- Fill in the active part of the graph
  82.     end
  83.     term.setTextColor(defColor)
  84.     term.restore() -- Return terminal output to computer
  85. end
  86.  
  87. local function transOutline( xLeft, xRight, yTop, yBottom, color ) -- Used for transposers that are off
  88.     local yText = yTop + 1
  89.     local xText = math.floor((xRight + xLeft) / 2) -1 -- Offset by one for the three characters of OFF
  90.     term.redirect(mon) -- Send output to the monitor
  91.     paintutils.drawLine( xLeft, yTop, xRight, yTop, color ) -- Top
  92.     paintutils.drawLine( xLeft, yBottom, xRight, yBottom, color ) -- Bottom
  93.     paintutils.drawLine( xLeft, yTop, xLeft, yBottom, color ) -- Left
  94.     paintutils.drawLine( xRight, yTop, xRight, yBottom, color ) -- Right
  95.     term.setCursorPos(xText, yText)
  96.     term.setTextColor(colors.red)
  97.     term.setBackgroundColor(colors.black)
  98.     term.write("OFF")
  99.     term.setTextColor(defColor) -- Restore default color, just in case
  100.     term.restore() -- Return terminal output to computer
  101. end
  102.  
  103. local function transSolid ( xLeft, xRight, yTop, yBottom, color ) -- Used for transposers that are running
  104.     local yText = yTop + 1
  105.     local xText = math.floor((xRight + xLeft) / 2) - 2 -- Needs stronger offset
  106.     term.redirect(mon) -- Send output to the monitor
  107.     for i=yTop, yBottom do -- Drawing across for fewer cycles (probably)
  108.         paintutils.drawLine ( xLeft, i, xRight, i, color )
  109.     end
  110.     --[[term.setCursorPos(xLeft, yTop) -- Begin debug print
  111.     term.write(xLeft..", "..yTop)
  112.     term.setCursorPos(xRight - 5, yBottom)
  113.     term.write(xRight..", "..yBottom) -- End debug print ]]
  114.     term.setCursorPos(xText, yText)
  115.     term.setTextColor(colors.lime)
  116.     term.setBackgroundColor(colors.black) -- For contrast
  117.     term.write(" ON ")
  118.     term.setTextColor(defColor) -- Restore default color
  119.     term.setBackgroundColor(colors.black) -- and background
  120.     term.restore() -- Return terminal output to computer
  121. end
  122.  
  123. local function drawGraphs()
  124.     local transRight = maxX - 1
  125.     local transLeft = transRight - graphX
  126.     local transHeight = 3 -- Hardcoded because of formula fail
  127.     local trans4Bot = maxY
  128.     local trans4Top = trans4Bot - transHeight
  129.     local trans3Bot = trans4Top - 2
  130.     local trans3Top = trans3Bot - transHeight
  131.     local trans2Bot = trans3Top - 2
  132.     local trans2Top = trans2Bot - transHeight
  133.     local trans1Bot = trans2Top - 2
  134.     local trans1Top = trans1Bot - transHeight -- Better to keep divisions consistent than to end at y=3 exactly
  135.     mon.setBackgroundColor(colors.black)
  136.     mon.setTextColor(defColor)
  137.     mon.clear()
  138.     engineGraph()
  139.     mon.setCursorPos(transLeft, 1)
  140.     mon.setTextColor(colors.red)
  141.     mon.setBackgroundColor(colors.black)
  142.     mon.write("Pulsers")
  143.     --[[ print("X: "..transLeft..", "..transRight)
  144.     print("Y1:"..trans1Bot..", "..trans1Top)
  145.     print("Y2:"..trans2Bot..", "..trans2Top)
  146.     print("Y3:"..trans3Bot..", "..trans3Top)
  147.     print("Y4:"..trans4Bot..", "..trans4Top)
  148.     print("Max:"..maxX..", "..maxY)
  149.     print("Graph:"..graphX..", "..graphY) ]] -- Debug code above
  150.     if signalsOn[1] then
  151.         transSolid(transLeft, transRight, trans1Top, trans1Bot, colors.white)
  152.     else
  153.         transOutline(transLeft, transRight, trans1Top, trans1Bot, colors.white)
  154.     end
  155.     if signalsOn[2] then
  156.         transSolid(transLeft, transRight, trans2Top, trans2Bot, colors.orange)
  157.     else
  158.         transOutline(transLeft, transRight, trans2Top, trans2Bot, colors.orange)
  159.     end
  160.     if signalsOn[3] then
  161.         transSolid(transLeft, transRight, trans3Top, trans3Bot, colors.magenta)
  162.     else
  163.         transOutline(transLeft, transRight, trans3Top, trans3Bot, colors.magenta)
  164.     end
  165.     if signalsOn[4] then
  166.         transSolid(transLeft, transRight, trans4Top, trans4Bot, colors.lightBlue)
  167.     else
  168.         transOutline(transLeft, transRight, trans4Top, trans4Bot, colors.lightBlue)
  169.     end
  170. end
  171.  
  172. if term.isColor then
  173.     term.setTextColor(defColor) -- Setting default color
  174. end
  175.  
  176. -- Engine control functions
  177.    
  178. local function setEngines ( maxEngines )
  179.     numOut = 2^maxEngines - 1
  180.     rs.setBundledOutput(engineSide, numOut)
  181. end
  182.  
  183. engineLevel = 0
  184.  
  185. local function enginesUp ()
  186.     if engineLevel < maxEngines then
  187.         engineLevel = engineLevel + 1
  188.         setEngines (engineLevel)
  189.     end -- If engines are already all on, don't increment
  190. end
  191.  
  192. local function enginesDown()
  193.     if engineLevel ~= 0 then -- If not off, decrement
  194.         engineLevel = engineLevel - 1
  195.         setEngines(engineLevel)
  196.     end
  197. end
  198.  
  199. -- Transposer control functions
  200.  
  201. local function setSignal() -- Parses the signalsOn table to set appropriate value to signalOut
  202.     local sig = 0 -- Placeholder to accumulate color codes
  203.     for i=1,4 do
  204.         if signalsOn[i] then -- This signal is TRUE and should be included
  205.             sig = sig + ( 2^(i-1) ) -- Tables index from 1, cables from 0
  206.         end -- If the signal is FALSE, don't add it in
  207.     end
  208.     signalOut = sig -- Set signalOut to the total value
  209. end
  210.  
  211. local function toggleSignal (sigAddr) -- Toggles a particular signal and updates the output code appropriately
  212.     if signalsOn[sigAddr] then -- The signal in question is already on
  213.         signalsOn[sigAddr] = false -- Turn it off in the table
  214.         setSignal() -- Update the output code
  215.     else -- The signal is off
  216.         signalsOn[sigAddr] = true -- Turn it on
  217.         setSignal() -- Update the table
  218.     end
  219. end
  220.  
  221. local function pulseLengthSet() -- Used to recalculate pulseDelay when pulsePeriod changes
  222.     pulseDelay = pulsePeriod - pulseLength -- Off time between pulses
  223. end
  224.  
  225. -- Coroutine to be run via KaoShell
  226. function tickOut () -- Cycler function, applies whether or not using bundles
  227.     while true do
  228.         if cycleBundle then -- Using bundled cable
  229.             rs.setBundledOutput(cycleSide, signalOut)
  230.             sleep(pulseLength)
  231.             rs.setBundledOutput(cycleSide, 0)
  232.             sleep(pulseDelay)
  233.         else -- Using single conductor
  234.             rs.setOutput(cycleSide, true)
  235.             sleep(pulseLength)
  236.             rs.setBundledOutput(cycleSide, false)
  237.             sleep(pulseDelay)
  238.         end
  239.     end
  240. end
  241.  
  242. pulseLengthSet() -- Not currently controlling pulses in the menu, just set it
  243. setSignal() -- Signal needs to be set from the T/F table before running
  244. term.clear()
  245. term.setCursorPos(1,1)
  246. term.setCursorBlink(false)
  247.  
  248. local function showMenu()
  249.     term.clear()
  250.     term.setCursorPos(1,1)
  251.     print("Engine controls engaged\n") -- Lines 1-2
  252.     term.write("Engines active: ") -- Line 3
  253.     writeColor(engineLevel, 2^(engineLevel-1))
  254.     term.write(" out of ")
  255.     writeColor(maxEngines, 2^(maxEngines-1))
  256.     print("") -- Linefeed, end line 3
  257.     print("Transposers active:") -- Line 4
  258.     writeColor("WHITE  ",colors.white) -- Line 5
  259.     term.write(": ")
  260.     writeOnOff(signalsOn[1]) -- Print status of white
  261.     print("") -- Linefeed, end line 5
  262.     writeColor("ORANGE ",colors.orange) -- Line 6
  263.     term.write(": ")
  264.     writeOnOff(signalsOn[2]) -- Print status of orange
  265.     print("") -- Linefeed, end line 6
  266.     writeColor("MAGENTA",colors.magenta) -- Line 7
  267.     term.write(": ")
  268.     writeOnOff(signalsOn[3]) -- Print status of magenta
  269.     print("") -- Linefeed, end line 7
  270.     writeColor("LT BLUE",colors.lightBlue) -- Line 8
  271.     term.write(": ")
  272.     writeOnOff(signalsOn[4]) -- Print status of light blue
  273.     print("\n") -- Line feed, end line 8, line 9
  274.     print("Press Q to halt,") -- line 10
  275.     print("      Up arrow or + to increase engine strength,") -- line 11
  276.     print("      Down arrow or - to decrease engine strength,") -- line 12
  277.     print("      Backspace to halt all engines") -- line 13
  278.     print("      Backslash(\\) for full power") -- line 14
  279.     print("      a through d or 1-4 to toggle transposers") -- line 15
  280. end
  281.  
  282. stayInLoop = true
  283. showMenu()
  284. cycler = addCo(tickOut) -- Start the cycler ticking. Nonlocal in case of abnormal termination.
  285. while stayInLoop do
  286.     drawGraphs() -- Update the monitors before waiting for an event
  287.     event, scancode = os.pullEvent("key")
  288.     if scancode == 16 then -- Q received
  289.         term.setCursorPos(1,16)
  290.         term.scroll(2)
  291.         print("\nHalting...")
  292.         engineLevel = 0
  293.         setEngines(0)
  294.         stayInLoop = false -- This should terminate the loop
  295.     elseif scancode == 200 or scancode == 13 or scancode == 78 then -- Up arrow, =/+, Num+
  296.         enginesUp()
  297.         term.setCursorPos(17,3)
  298.         writeColor(engineLevel, 2^(engineLevel-1))
  299.     elseif scancode == 208 or scancode == 12 or scancode == 74 then -- Down arrow, -, Num-
  300.         enginesDown()
  301.         term.setCursorPos(17,3)
  302.         writeColor(engineLevel, 2^(engineLevel-1))
  303.     elseif scancode == 43 then -- Backslash received, max engines
  304.         engineLevel = maxEngines
  305.         setEngines(engineLevel)
  306.         term.setCursorPos(17,3)
  307.         writeColor(engineLevel, 2^(engineLevel-1))
  308.     elseif scancode == 14 then -- Backspace received
  309.         engineLevel = 0
  310.         setEngines(engineLevel)
  311.         term.setCursorPos(17,3)
  312.         writeColor(engineLevel, 2^(engineLevel-1))
  313.     elseif scancode == 2 or scancode == 30 then -- 1 or a received
  314.         toggleSignal(1) -- Toggle channel 1 (white)
  315.         mon.write("WHITE toggled")
  316.         lineFeed()
  317.         term.setCursorPos(10,5)
  318.         writeOnOff(signalsOn[1])
  319.     elseif scancode == 3 or scancode == 48 then -- 2 or b received
  320.         toggleSignal(2) -- Toggle channel 2 (orange)
  321.         mon.write("ORANGE toggled")
  322.         lineFeed()
  323.         term.setCursorPos(10,6)
  324.         writeOnOff(signalsOn[2])
  325.     elseif scancode == 4 or scancode == 46 then -- 3 or c received
  326.         toggleSignal(3) -- Toggle channel 3 (magenta)
  327.         mon.write("MAGENTA toggled")
  328.         lineFeed()
  329.         term.setCursorPos(10,7)
  330.         writeOnOff(signalsOn[3])
  331.     elseif scancode == 5 or scancode == 32 then -- 4 or d received
  332.         toggleSignal(4) -- Toggle channel 4 (light blue)
  333.         mon.write("LT BLUE toggled")
  334.         lineFeed()
  335.         term.setCursorPos(10,8)
  336.         writeOnOff(signalsOn[4])
  337.     end -- Other codes don't matter
  338. end
  339.  
  340. setEngines(0)
  341. remCo(cycler)
  342. mon.setTextColor(defColor)
  343. mon.setBackgroundColor(colors.black)
  344. -- mon.clear()
  345. sleep(1)
  346. mon.setCursorPos(1,1)
  347. -- mon.write("Offline")
  348. print("Factory shut down. Type \"factory\" to restart.")
  349. print("Goodbye...")
  350. term.setCursorBlink(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement