Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- SkunkWorks Logistics
- -- Joint factory control script
- -- Engine variables
- local engineSide = "left" -- Engines on this side - always bundled
- local maxEngines = 8 -- Number of engines connected
- -- Cycler variables
- local cycleSide = "right" -- Transposer output on this side
- local cycleBundle = true -- Bundled cable vs single-conductor transposer line
- local pulseLength = 0.1 -- Pulse "on" time in seconds
- local pulsePeriod = 0.75 -- Pulse cycle length
- local pulseDelay -- This will be set later - the "off" time of each pulse
- local minPeriod = 0.75 -- Shorter than this, transposers may jam
- local maxPeriod = 5.75 -- Nothing should need a longer period than this
- if pulsePeriod < 0.6 then -- Too short a pulse to reliably cycle transposers
- pulsePeriod = 0.6
- end
- local signalsOn = {true, false, false, false} -- Output(s) to trigger - if not cycleBundle then this is meaningless
- -- Display variables
- local monSide = "top" -- Monitor location
- local defColor=colors.lightGray -- Default text color in case of color terminal
- local mon=peripheral.wrap(monSide)
- mon.setTextScale(0.5)
- local maxX, maxY = mon.getSize()
- local graphX = (maxX/2) - 2 -- Each graph side is half the screen, less two pixels
- local graphY = maxY - 3 -- Leave a bit of space for captions
- -- Display functions
- local function writeColor( msg, color ) -- Used to avoid sending color codes to non-color term
- if term.isColor then
- term.setTextColor(color)
- term.write(msg) -- We don't want a linefeed, so don't print()
- term.setTextColor(defColor) -- Set the color back afterwards
- else
- term.write(msg) -- We still don't want a linefeed
- end
- end
- local function writeOnOff( isOn ) -- Given bool isOn, write if it's on or off
- if isOn then -- Signal is on
- writeColor("ON ",colors.lime)
- else
- writeColor("OFF",colors.red)
- end
- end
- local function lineFeed() -- Used to move to a new line on the monitor, or scroll if it's at the bottom. Deprecated.
- local oldx, oldy = mon.getCursorPos()
- if oldy==maxY then
- mon.scroll(1)
- mon.setCursorPos(1,oldy)
- else
- local newy = oldy + 1
- mon.setCursorPos(1, newy)
- end
- end
- local function engineGraph() -- Used to graph the engine power
- term.redirect(mon) -- Send drawing instructions to the monitor
- term.setCursorPos(2, 1)
- term.setTextColor(colors.lightBlue)
- term.write("Engines")
- term.setCursorPos(2, 2)
- term.setTextColor(math.max(engineLevel,colors.white))
- term.write(engineLevel.." / "..maxEngines)
- local engineHeight = math.floor (graphY * engineLevel / maxEngines) -- Take the proportional height out of the graph space, round down
- if engineHeight < 1 then
- engineHeight = 1 -- Graph should be at least one high
- end
- local engineY = math.max(maxY - engineHeight, 3)
- local engineRight = 1 + graphX -- Right-side boundary of the engine graph
- paintutils.drawLine ( 2, 3, 2, maxY, math.max(engineLevel,colors.white) ) -- Start the outline - left side
- paintutils.drawLine ( engineRight, 3, engineRight, maxY, math.max(engineLevel,colors.white) ) -- Right side
- paintutils.drawLine ( 2, 3, engineRight, 3, math.max(engineLevel,colors.white) ) -- Top
- paintutils.drawLine ( 2, maxY, engineRight, maxY, math.max(engineLevel,colors.white) ) -- Bottom
- for i = 2, engineRight do
- paintutils.drawLine ( i, maxY, i, engineY, math.max(engineLevel,colors.white)) -- Fill in the active part of the graph
- end
- term.setTextColor(defColor)
- term.restore() -- Return terminal output to computer
- end
- local function transOutline( xLeft, xRight, yTop, yBottom, color ) -- Used for transposers that are off
- local yText = yTop + 1
- local xText = math.floor((xRight + xLeft) / 2) -1 -- Offset by one for the three characters of OFF
- term.redirect(mon) -- Send output to the monitor
- paintutils.drawLine( xLeft, yTop, xRight, yTop, color ) -- Top
- paintutils.drawLine( xLeft, yBottom, xRight, yBottom, color ) -- Bottom
- paintutils.drawLine( xLeft, yTop, xLeft, yBottom, color ) -- Left
- paintutils.drawLine( xRight, yTop, xRight, yBottom, color ) -- Right
- term.setCursorPos(xText, yText)
- term.setTextColor(colors.red)
- term.setBackgroundColor(colors.black)
- term.write("OFF")
- term.setTextColor(defColor) -- Restore default color, just in case
- term.restore() -- Return terminal output to computer
- end
- local function transSolid ( xLeft, xRight, yTop, yBottom, color ) -- Used for transposers that are running
- local yText = yTop + 1
- local xText = math.floor((xRight + xLeft) / 2) - 2 -- Needs stronger offset
- term.redirect(mon) -- Send output to the monitor
- for i=yTop, yBottom do -- Drawing across for fewer cycles (probably)
- paintutils.drawLine ( xLeft, i, xRight, i, color )
- end
- --[[term.setCursorPos(xLeft, yTop) -- Begin debug print
- term.write(xLeft..", "..yTop)
- term.setCursorPos(xRight - 5, yBottom)
- term.write(xRight..", "..yBottom) -- End debug print ]]
- term.setCursorPos(xText, yText)
- term.setTextColor(colors.lime)
- term.setBackgroundColor(colors.black) -- For contrast
- term.write(" ON ")
- term.setTextColor(defColor) -- Restore default color
- term.setBackgroundColor(colors.black) -- and background
- term.restore() -- Return terminal output to computer
- end
- local function drawGraphs()
- local transRight = maxX - 1
- local transLeft = transRight - graphX
- local transHeight = 3 -- Hardcoded because of formula fail
- local trans4Bot = maxY
- local trans4Top = trans4Bot - transHeight
- local trans3Bot = trans4Top - 2
- local trans3Top = trans3Bot - transHeight
- local trans2Bot = trans3Top - 2
- local trans2Top = trans2Bot - transHeight
- local trans1Bot = trans2Top - 2
- local trans1Top = trans1Bot - transHeight -- Better to keep divisions consistent than to end at y=3 exactly
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(defColor)
- mon.clear()
- engineGraph()
- mon.setCursorPos(transLeft, 1)
- mon.setTextColor(colors.red)
- mon.setBackgroundColor(colors.black)
- mon.write("Pulsers")
- --[[ print("X: "..transLeft..", "..transRight)
- print("Y1:"..trans1Bot..", "..trans1Top)
- print("Y2:"..trans2Bot..", "..trans2Top)
- print("Y3:"..trans3Bot..", "..trans3Top)
- print("Y4:"..trans4Bot..", "..trans4Top)
- print("Max:"..maxX..", "..maxY)
- print("Graph:"..graphX..", "..graphY) ]] -- Debug code above
- if signalsOn[1] then
- transSolid(transLeft, transRight, trans1Top, trans1Bot, colors.white)
- else
- transOutline(transLeft, transRight, trans1Top, trans1Bot, colors.white)
- end
- if signalsOn[2] then
- transSolid(transLeft, transRight, trans2Top, trans2Bot, colors.orange)
- else
- transOutline(transLeft, transRight, trans2Top, trans2Bot, colors.orange)
- end
- if signalsOn[3] then
- transSolid(transLeft, transRight, trans3Top, trans3Bot, colors.magenta)
- else
- transOutline(transLeft, transRight, trans3Top, trans3Bot, colors.magenta)
- end
- if signalsOn[4] then
- transSolid(transLeft, transRight, trans4Top, trans4Bot, colors.lightBlue)
- else
- transOutline(transLeft, transRight, trans4Top, trans4Bot, colors.lightBlue)
- end
- end
- if term.isColor then
- term.setTextColor(defColor) -- Setting default color
- end
- -- Engine control functions
- local function setEngines ( maxEngines )
- numOut = 2^maxEngines - 1
- rs.setBundledOutput(engineSide, numOut)
- end
- engineLevel = 0
- local function enginesUp ()
- if engineLevel < maxEngines then
- engineLevel = engineLevel + 1
- setEngines (engineLevel)
- end -- If engines are already all on, don't increment
- end
- local function enginesDown()
- if engineLevel ~= 0 then -- If not off, decrement
- engineLevel = engineLevel - 1
- setEngines(engineLevel)
- end
- end
- -- Transposer control functions
- local function setSignal() -- Parses the signalsOn table to set appropriate value to signalOut
- local sig = 0 -- Placeholder to accumulate color codes
- for i=1,4 do
- if signalsOn[i] then -- This signal is TRUE and should be included
- sig = sig + ( 2^(i-1) ) -- Tables index from 1, cables from 0
- end -- If the signal is FALSE, don't add it in
- end
- signalOut = sig -- Set signalOut to the total value
- end
- local function toggleSignal (sigAddr) -- Toggles a particular signal and updates the output code appropriately
- if signalsOn[sigAddr] then -- The signal in question is already on
- signalsOn[sigAddr] = false -- Turn it off in the table
- setSignal() -- Update the output code
- else -- The signal is off
- signalsOn[sigAddr] = true -- Turn it on
- setSignal() -- Update the table
- end
- end
- local function pulseLengthSet() -- Used to recalculate pulseDelay when pulsePeriod changes
- pulseDelay = pulsePeriod - pulseLength -- Off time between pulses
- end
- -- Coroutine to be run via KaoShell
- function tickOut () -- Cycler function, applies whether or not using bundles
- while true do
- if cycleBundle then -- Using bundled cable
- rs.setBundledOutput(cycleSide, signalOut)
- sleep(pulseLength)
- rs.setBundledOutput(cycleSide, 0)
- sleep(pulseDelay)
- else -- Using single conductor
- rs.setOutput(cycleSide, true)
- sleep(pulseLength)
- rs.setBundledOutput(cycleSide, false)
- sleep(pulseDelay)
- end
- end
- end
- pulseLengthSet() -- Not currently controlling pulses in the menu, just set it
- setSignal() -- Signal needs to be set from the T/F table before running
- term.clear()
- term.setCursorPos(1,1)
- term.setCursorBlink(false)
- local function showMenu()
- term.clear()
- term.setCursorPos(1,1)
- print("Engine controls engaged\n") -- Lines 1-2
- term.write("Engines active: ") -- Line 3
- writeColor(engineLevel, 2^(engineLevel-1))
- term.write(" out of ")
- writeColor(maxEngines, 2^(maxEngines-1))
- print("") -- Linefeed, end line 3
- print("Transposers active:") -- Line 4
- writeColor("WHITE ",colors.white) -- Line 5
- term.write(": ")
- writeOnOff(signalsOn[1]) -- Print status of white
- print("") -- Linefeed, end line 5
- writeColor("ORANGE ",colors.orange) -- Line 6
- term.write(": ")
- writeOnOff(signalsOn[2]) -- Print status of orange
- print("") -- Linefeed, end line 6
- writeColor("MAGENTA",colors.magenta) -- Line 7
- term.write(": ")
- writeOnOff(signalsOn[3]) -- Print status of magenta
- print("") -- Linefeed, end line 7
- writeColor("LT BLUE",colors.lightBlue) -- Line 8
- term.write(": ")
- writeOnOff(signalsOn[4]) -- Print status of light blue
- print("\n") -- Line feed, end line 8, line 9
- print("Press Q to halt,") -- line 10
- print(" Up arrow or + to increase engine strength,") -- line 11
- print(" Down arrow or - to decrease engine strength,") -- line 12
- print(" Backspace to halt all engines") -- line 13
- print(" Backslash(\\) for full power") -- line 14
- print(" a through d or 1-4 to toggle transposers") -- line 15
- end
- stayInLoop = true
- showMenu()
- cycler = addCo(tickOut) -- Start the cycler ticking. Nonlocal in case of abnormal termination.
- while stayInLoop do
- drawGraphs() -- Update the monitors before waiting for an event
- event, scancode = os.pullEvent("key")
- if scancode == 16 then -- Q received
- term.setCursorPos(1,16)
- term.scroll(2)
- print("\nHalting...")
- engineLevel = 0
- setEngines(0)
- stayInLoop = false -- This should terminate the loop
- elseif scancode == 200 or scancode == 13 or scancode == 78 then -- Up arrow, =/+, Num+
- enginesUp()
- term.setCursorPos(17,3)
- writeColor(engineLevel, 2^(engineLevel-1))
- elseif scancode == 208 or scancode == 12 or scancode == 74 then -- Down arrow, -, Num-
- enginesDown()
- term.setCursorPos(17,3)
- writeColor(engineLevel, 2^(engineLevel-1))
- elseif scancode == 43 then -- Backslash received, max engines
- engineLevel = maxEngines
- setEngines(engineLevel)
- term.setCursorPos(17,3)
- writeColor(engineLevel, 2^(engineLevel-1))
- elseif scancode == 14 then -- Backspace received
- engineLevel = 0
- setEngines(engineLevel)
- term.setCursorPos(17,3)
- writeColor(engineLevel, 2^(engineLevel-1))
- elseif scancode == 2 or scancode == 30 then -- 1 or a received
- toggleSignal(1) -- Toggle channel 1 (white)
- mon.write("WHITE toggled")
- lineFeed()
- term.setCursorPos(10,5)
- writeOnOff(signalsOn[1])
- elseif scancode == 3 or scancode == 48 then -- 2 or b received
- toggleSignal(2) -- Toggle channel 2 (orange)
- mon.write("ORANGE toggled")
- lineFeed()
- term.setCursorPos(10,6)
- writeOnOff(signalsOn[2])
- elseif scancode == 4 or scancode == 46 then -- 3 or c received
- toggleSignal(3) -- Toggle channel 3 (magenta)
- mon.write("MAGENTA toggled")
- lineFeed()
- term.setCursorPos(10,7)
- writeOnOff(signalsOn[3])
- elseif scancode == 5 or scancode == 32 then -- 4 or d received
- toggleSignal(4) -- Toggle channel 4 (light blue)
- mon.write("LT BLUE toggled")
- lineFeed()
- term.setCursorPos(10,8)
- writeOnOff(signalsOn[4])
- end -- Other codes don't matter
- end
- setEngines(0)
- remCo(cycler)
- mon.setTextColor(defColor)
- mon.setBackgroundColor(colors.black)
- -- mon.clear()
- sleep(1)
- mon.setCursorPos(1,1)
- -- mon.write("Offline")
- print("Factory shut down. Type \"factory\" to restart.")
- print("Goodbye...")
- term.setCursorBlink(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement