Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Args
- local args = { ... }
- local slaveNum = tonumber(args[1]) or 1
- local redstoneOutput = args[2] or "left"
- local avgRFT = tonumber(args[3]) or 10000
- local channel = tonumber(args[4]) or 40
- -- Libraries
- local setup = require("/lua/lib/setupUtils")
- local monUtils = require("/lua/lib/monitorUtils")
- local write = monUtils.write
- local drawBox = monUtils.drawBox
- local render = require("/lua/lib/render")
- local state = require("/lua/lib/stateHandler")
- -- Peripherals
- local wrappedPers = setup.getPers({
- "monitor", "modem"
- })
- local monitor = setup.setupMonitor(
- wrappedPers.monitor[1], 0.5
- )
- local modem = wrappedPers.modem[1]
- local speaker = peripheral.find("speaker")
- -- Setup
- local stateData = state.getState("enderControlSlave")
- local defaultData = {
- state = "off", num = slaveNum,
- auto = "off", avgRFT = avgRFT
- }
- local slaveData = stateData or defaultData
- local buttons = {}
- -- Windows
- local winHeader = setup.setupWindow(
- monitor, 1, 1, monitor.x, 6
- )
- local winMain = setup.setupWindow(
- monitor, 1, 7, monitor.x, (monitor.y - (6 + 4))
- )
- local winMainControl = setup.setupWindow(
- winMain, (winMain.x - 13), 1,
- 14, winMain.y
- )
- local winFooter = setup.setupWindow(
- monitor, 1, (monitor.y - 3), monitor.x, 4
- )
- -- Actions
- local actions = {}
- actions.on = function()
- rs.setAnalogOutput(redstoneOutput, 15)
- if(speaker) then
- speaker.playSound(
- "minecraft:block.lever.click",
- 1, 1
- )
- end
- slaveData.state = "on"
- drawHeader()
- drawFooter()
- updateRender()
- end
- actions.off = function()
- rs.setAnalogOutput(redstoneOutput, 0)
- if(speaker) then
- speaker.playSound(
- "minecraft:block.lever.click",
- 1, 0.8
- )
- end
- slaveData.state = "off"
- drawHeader()
- drawFooter()
- updateRender()
- end
- actions.toggle = function()
- if(slaveData.state == "off") then
- actions.on()
- else
- actions.off()
- end
- end
- actions.toggleAuto = function()
- if(slaveData.auto == "off") then
- slaveData.auto = "on"
- else
- slaveData.auto = "off"
- end
- updateRender()
- end
- -- Main
- function start()
- print("# Program Started")
- modem.open(channel)
- drawHeader()
- drawFooter()
- drawMain()
- updateRender()
- await()
- end
- function await()
- while(true) do
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- local isTouch = (event == "monitor_touch")
- local isModemMessage = (event == "modem_message")
- if(isTouch) then
- local x = p2 - (winMain.x - winMainControl.x) - 1
- local y = p3 - winHeader.y
- if(x > 0) then
- for i, button in pairs(buttons) do
- if(y == button.y) then
- actions[button.action]()
- modem.transmit(channel, channel,
- {type = "updatePending"}
- )
- updateState()
- end
- end
- end
- elseif(isModemMessage) then
- local body = p4
- if(body.type == "modify") then
- local isSelf = (body.slaveNum == slaveNum)
- if(isSelf) then
- local change = body.change
- actions[change.action](change)
- modem.transmit(channel, channel,
- {type = "updatePending"}
- )
- updateState()
- end
- elseif(body.type == "poll") then
- modem.transmit(channel, channel, {
- type = "pollRes",
- slaveData = slaveData
- })
- end
- end
- end
- end
- function updateRender()
- buttons = render.ender({{
- output = winMain,
- controlOutput = winMainControl,
- data = slaveData
- }})
- end
- function updateState()
- state.updateState("enderControlSlave", slaveData)
- end
- function drawHeader()
- winHeader.bg = colors.blue
- winHeader.setBackgroundColor(winHeader.bg)
- drawBox(winHeader,
- 1, 1, winHeader.x, winHeader.y,
- true
- )
- drawBox(winHeader,
- 1, winHeader.y, winHeader.x, winHeader.y,
- true, colors.white
- )
- write(winHeader,
- "Endergenic Resonation Control",
- 0, 2, "centre"
- )
- write(winHeader,
- "Generator Unit: " .. slaveNum,
- 0, 4, "centre"
- )
- end
- function drawFooter()
- winFooter.bg = colors.blue
- winFooter.setBackgroundColor(winFooter.bg)
- drawBox(winFooter,
- 1, 1, winFooter.x, winFooter.y,
- true
- )
- drawBox(winFooter,
- 1, 1, winFooter.x, 1,
- true, colors.white
- )
- end
- function drawMain()
- winMain.bg = colors.purple
- winMain.setBackgroundColor(winMain.bg)
- drawBox(winMain,
- 1, 1, winMain.x, winMain.y,
- true
- )
- end
- setup.utilsWrapper(start, modem, channel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement