--[[-- Wireless modem: back Wired modem: front Relay id: 131 Monitor name: monitor_1 MFE name: batbox_1 Self name: computer_4 Controller nickname: controller Relay nickname: relay --]]-- --(( Variables ))-- local running = true local controllerid = os.getComputerID() local controllername = "controller" local relayid = -1 local relayname = "relay" local loading = {"|","/","-","\\"} local subs = { getid = "get id", gotid = "got id", gotidack = "got id, acknowledge", setnuke = "set nuke state", getoutput = "get nuke output", gotoutput = "got nuke output", getnukestate = "get nuke state", gotnukestate = "got nuke state", } local wireless = "back" -- side local cable = "front" -- side local channel = 61234 local refreshrate = .2 -- seconds local modem = peripheral.wrap(wireless) or error("Unable to wrap wireless modem!",0) if not modem.isWireless() then error("Wireless modem is required! ("..wireless.." side)",0) end local wired = peripheral.wrap(cable) or error("Unable to wrap wired modem!",0) if wired.isWireless() then error("Wired modem is required! ("..cable.." side)",0) end -- nuke local nukestate = false -- false=off, true=on local nukeoutput = 0 -- EU/t -- mfe local mfepowerstored = 0 -- EU local mfepowermax = 0 -- EU local mfeoutput = 0 -- EU/t local mfemaxoutput = 0 -- EU local mfeside = "batbox_1" local mfe = peripheral.wrap(mfeside) or error("Unable to wrap storage device!",0) -- monitor local monitorside = "monitor_1" local monitor = peripheral.wrap(monitorside) or error("Unable to wrap monitor display!",0) local monitorw,monitorh = monitor.getSize() -- computer local w,h = term.getSize() --(( Functions ))-- local function setColor(fg,bg,obj) local obj = obj or term if obj.isColor then if obj.isColor() then if type(fg) == "number" then obj.setTextColor(fg) end if type(bg) == "number" then obj.setBackground(bg) end end end end local function sendMsg(from,to,subject,info) info = info or "" local message = { from = from, to = to, subject = subject, info = info, } message = textutils.serialize(message) modem.transmit(channel,channel,message) end local function init() modem.open(channel) local timeout = 10 -- seconds local timeouttimer = os.startTimer(timeout) local refreshtimer = os.startTimer(refreshrate) local refreshcount = 0 local x,y = term.getCursorPos() local relayknows = false repeat local ev,p1,p2,p3,p4,p5 = os.pullEvent() -- Loading message term.setCursorPos(x,y) setColor(colors.yellow) term.write("Initializing ") setColor(colors.orange) term.write(loading[(refreshcount%#loading)+1]) -- Event handling if ev == "modem_message" then -- Message handling local msg = textutils.unserialize(p4) if type(msg) == "table" then if msg.from == relayname and msg.to == controllername then if msg.subject == subs.getid then -- Reply with id sendMsg(controllername,relayname,subs.gotid,controllerid) elseif msg.subject == subs.gotid then if relayid == -1 then -- Save id (into variable) if type(msg.info) == "number" then relayid = msg.info -- Reply with acknowledge confirm sendMsg(controllername,relayname,subs.gotidack) end end elseif msg.subject == subs.gotidack then -- Relay got my id relayknows = true end end end elseif ev == "timer" then -- Timer handling local timer = p1 if timer == refreshtimer then -- Loading count refreshtimer = os.startTimer(refreshrate) refreshcount = refreshcount + 1 -- Ask for id if relayid == -1 then sendMsg(controllername,relayname,subs.getid) end elseif timer == timeouttimer then -- Timeout term.setCursorPos(x,y) setColor(colors.yellow) term.write("Initializing ") setColor(colors.orange) term.write("X") print() setColor(colors.red) error("Operation timed out.",0) end end until relayid ~= -1 and relayknows term.setCursorPos(x,y) setColor(colors.yellow) term.write("Initializing ") setColor(colors.orange) term.write("X") setColor(colors.lime) term.write(" Done!") print() end local function setNuke(state) sendMsg(controllername,relayname,subs.setnuke,state) end local function getNukeOutput() sendMsg(controllername,relayname,subs.getoutput) end local function updateMFE() -- output mfeoutput = mfe.getOutput() mfemaxoutput = mfe.getMaxEnergyOutput() -- storage mfepowerstored = mfe.getStored() mfepowermax = mfe.getCapacity() end local function drawMonitor() -- for debugging, raw info local info = { mfeoutput = tostring(mfeoutput) .. "/" .. tostring(mfemaxoutput), mfepower = tostring(mfepowerstored) .. "/" .. tostring(mfepowermax), nukeoutput = tostring(nukeoutput), } if nukestate then info.nukestate = "ON" else info.nukestate = "OFF" end local count = 0 for index,value in pairs(info) do monitor.setCursorPos(2,1+count) setColor(colors.orange,_,monitor) monitor.write(index) setColor(colors.red,_,monitor) monitor.write(" = ") setColor(colors.yellow,_,monitor) monitor.write(tostring(value)) count = count + 1 end end --(( Main program ))-- init() print("Initiating complete!") local refreshtimer = os.startTimer(refreshrate) local refreshcount = 0 while running do local ev,p1,p2,p3,p4,p5 = os.pullEvent() if ev == "modem_message" then -- Message handling local msg = textutils.unserialize(p4) if type(msg) == "table" then if msg.to == controllername then if msg.from == relayname then -- Receive nuke state if msg.subject == subs.gotnukestate then if type(msg.info) == "boolean" then nukestate = msg.info end end -- Receive nuke output if msg.subject == subs.gotoutput then if type(msg.info) == "number" then nukeoutput = msg.info end end end end end elseif ev == "timer" then if p1 == refreshtimer then refreshtimer = os.startTimer(refreshrate) refreshcount = refreshcount + 1 setNuke(nukestate) getNukeOutput() updateMFE() drawMonitor() end end end --(( EOF ))--