--[[-- message form = { from = sender name, to = reciver name, subject = operation, info = additional information, } --]]-- --(( Variables ))-- local running = true local controllerid = -1 local controllername = "controller" local relayid = os.getComputerID() local relayname = "relay" local wireless = "bottom" -- side local channel = 61234 local refreshrate = .2 -- seconds 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 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 -- computer local w,h = term.getSize() -- nuke local nukeside = "top" local nuke = peripheral.wrap(nukeside) or error("Unable to wrap nuclear reactor!",0) local nukestate = false local nukeoutput = 0 -- EU/t --(( Functions ))-- local function setColor(fg,bg) if term.isColor then if term.isColor() then if type(fg) == "number" then term.setTextColor(fg) end if type(bg) == "number" then term.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 controllerknows = 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 == controllername and msg.to == relayname then if msg.subject == subs.getid then -- Reply with id sendMsg(relayname,controllername,subs.gotid,relayid) elseif msg.subject == subs.gotid then if controllerid == -1 then -- Save id (into variable) if type(msg.info) == "number" then controllerid = msg.info -- Reply with acknowledge confirm sendMsg(relayname,controllername,subs.gotidack) end end elseif msg.subject == subs.gotidack then -- Controller got my id controllerknows = 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 controllerid == -1 then sendMsg(relayname,controllername,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 controllerid ~= -1 and controllerknows 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 updateNuke() -- Nuke on/off rs.setOutput(nukeside,nukestate) -- Nuke output nukeoutput = nuke.getOutput() end --(( Main program ))-- init() setColor(colors.yellow) 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 == relayname then if msg.from == controllername then -- Get nuke state if msg.subject == subs.getnukestate then sendMsg(relayname,controllername,subs.gotnukestate,nukestate) end -- Set nuke state if msg.subject == subs.setnuke then if type(msg.info) == "boolean" then nukestate = msg.info end end -- Get nuke output if msg.subject == subs.getoutput then sendMsg(relayname,controllername,subs.gotoutput,nukeoutput) end end end end elseif ev == "timer" then if p1 == refreshtimer then refreshtimer = os.startTimer(refreshrate) refreshcount = refreshcount + 1 updateNuke() end end end --(( EOF ))--