Advertisement
horsemeat

ComputerCraft Power Management Application

Dec 26th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 38.75 KB | None | 0 0
  1. --Computer Craft Rekia's Reactor Craft Power Manager
  2. --By Snipy67
  3. --V1.5
  4. --which is built ontop of
  5. --Computer Craft Master Application for redstone interfacing
  6. --By snipy67
  7. --V1.1
  8. --You will need the Node Application at http://pastebin.com/3sGZ4dB3
  9. --to setup the nodes you need to download the node application and make sure you have 1 computer for each entry in "NODES" below
  10. --I recomend you try it first with redstone lamps
  11. --You will need the Router Application in server mode running on the network http://pastebin.com/BuRcCc2n
  12. MONITOR_SIDE = "top"
  13. NODES = {"gen1","gen2","gasturbine","tobatfromgen","tobatfromturbine","4bat","straightout","frombat","fromgas"}
  14. NODES_NAME = {"Generator\n1","Generator\n2","Gas\nTurbine","Generator\nTo\nBattery","Turbine\nTo\nBattery", "Batteries","Straight\nOut From\nGenerator","Out From\nBattery","Out From\nTurbine"}
  15. BUTTON_SIZE_X = 8
  16. BUTTON_SIZE_Y = 4
  17. nodeState = {}
  18. nodePort = {}
  19. nodeSendPort = {}
  20. nodeButtons = {}
  21.  
  22. conPort = {}
  23.  
  24. buttons = {}
  25. buttonsPressed = {}
  26. buttonsPosition = {}
  27. buttonsFunc = {}
  28. drawLinesData = {}
  29. --Computer Craft Router Application
  30. --By snipy67
  31. --V1.0
  32. MASTER_PORT = 1
  33. BROADCAST_PORT = 2
  34.  
  35. --Layer 1
  36. lowLevelListeners = {}
  37. routerComputerId = 0
  38.  
  39. function detectModem()
  40.     return peripheral.find("modem")
  41. end
  42. function lowLevelMessageSplit(message)
  43.     local tab = {}
  44.     local current = ""
  45.     for i = 1, #message, 1 do
  46.         if message:sub(i,i) == ':' then
  47.             table.insert(tab,current)
  48.             current = ""
  49.         else
  50.             current = current .. message:sub(i,i)
  51.         end
  52.     end
  53.     if #current > 0 then
  54.         table.insert(tab,current)
  55.     end
  56.     return tab
  57. end
  58.  
  59. function lowLevelListenerCheckForMethod(modem,port,tab,verbose)
  60.     for k, v in pairs(lowLevelListeners) do
  61.         if v.message == tab[1] then
  62.             if verbose then
  63.                 print("function " .. v.message .. "found")
  64.             end
  65.             if v.func ~= nil then
  66.                 local t = {}
  67.                 t.message = tab[1]
  68.                 t.senderId = tab[2]
  69.                 t.reciverId = tab[3]
  70.                 t.data = tab[4]
  71.                 v.func(t)
  72.                 if t.data ~= nil then
  73.                     local responseText = tab[1] .. "~response:" .. os.getComputerID() .. ":".. tab[2] .. ":".. t.data;
  74.                     modem.transmit(port,port,responseText)
  75.                 else
  76.                     local responseText = tab[1] .. "~response:" .. os.getComputerID() .. ":".. tab[2]
  77.                     modem.transmit(port,port,responseText)
  78.                 end
  79.             else
  80.                 local responseText = tab[1] .. "~response:" .. os.getComputerID() .. ":".. tab[2]
  81.                 modem.transmit(port,port,responseText)
  82.             end
  83.             return true
  84.         else
  85.             if verbose then
  86.                 print("v.message:" .. v.message .. " != tab[1]:"..tab[1])
  87.             end
  88.         end
  89.     end
  90.     return false
  91. end
  92.  
  93. function lowLevelListener(modem,port,verbose)
  94.     while true do
  95.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()--listens for a actions
  96.         if event == "modem_message" then--gets the message
  97.             if senderChannel == port then
  98.                 if verbose then
  99.                     print("message recived: " .. message)
  100.                 end
  101.                 local tab = lowLevelMessageSplit(message)
  102.                 if #tab > 2 then
  103.                     if verbose then
  104.                         print("There is more then 2 arguments in the message")
  105.                     end
  106.                     if tonumber(tab[3]) == os.getComputerID() then
  107.                         if verbose then
  108.                             print("id matches")
  109.                         end
  110.                         lowLevelListenerCheckForMethod(modem,port,tab,verbose)
  111.                     elseif tonumber(tab[3]) == -1 then
  112.                         if verbose then
  113.                             print("broadcast")
  114.                         end
  115.                         lowLevelListenerCheckForMethod(modem,port,tab,verbose)
  116.                     end
  117.                 elseif #tab == 2 then
  118.                     lowLevelListenerCheckForMethod(modem,port,tab,verbose)
  119.                 end
  120.             end
  121.         end
  122.     end
  123. end
  124.  
  125. function lowLevelRequest(modem,request,port,maxTimeOut,timeBetweenResend,numberOfResendsBeforeTimeOut,id,data,verbose)
  126.     local str = ""
  127.     if id ~= nil and id ~= -1 then
  128.         str = request .. ":" .. os.getComputerID() .. ":" .. id -- adds the computer id and the id of the reciver
  129.     else
  130.         str = request .. ":" .. os.getComputerID() .. ":" .. -1-- adds the computer id to the requests
  131.     end
  132.    
  133.     if data ~= nil then
  134.         str = str .. ":" .. data
  135.     end
  136.    
  137.     modem.transmit(port,port,str);--transmits the request
  138.     local responseText = request .. "~response"--creates text on how a response will look like
  139.     local count = 0--count is to keep track of how many times the request timed out
  140.     local timer = -1
  141.     if timeBetweenResend > 0 and numberOfResendsBeforeTimeOut > 0 then
  142.         timer = os.startTimer(timeBetweenResend) -- to keep track of the number of seconds before a time out
  143.     end
  144.     local atLeastOne = false--this determines if atleast one computer recived the request
  145.     local timeOutTimer
  146.     if maxTimeOut > 0 then
  147.         timeOutTimer = os.startTimer(maxTimeOut)--this timer is the number of seconds it should wait for return requests
  148.     end
  149.     local all = {} -- this is all the data that will be returned
  150.     all.package = {}--this is where the data from each request is stored
  151.     while true do
  152.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()--listens for a actions
  153.         if event == "timer" then--check if the timer is up
  154.             local timerId = modemSide; --modemSide is the timerid when timer event is called (prevents confusion)
  155.             if timerId == timer then--checks if the timer is the timer that handels resends
  156.                 if atLeastOne == false then--checks if there are no records recovered
  157.                     if count ~= numberOfResendsBeforeTimeOut then--check to make sure the count is not at the timeout stage
  158.                         modem.transmit(port,port,str);--transmits the request again
  159.                         count = count + 1 -- adds one to the #of time outs
  160.                         timer = os.startTimer(timeBetweenResend)--resets the timer
  161.                         if verbose then
  162.                             print("Message Transmission failed resending...")
  163.                         end
  164.                     else
  165.                         if verbose then
  166.                             print("Message Transmission failed")
  167.                         end
  168.                         all.sucess = false
  169.                         return all
  170.                     end
  171.                 end
  172.             elseif timerId == timeOutTimer then--this is the maximum amount of time it was going to wait and not it's going to return the data
  173.                 if atLeastone == false then
  174.                     all.sucess = false
  175.                 else
  176.                     all.sucess = true
  177.                 end
  178.                 return all
  179.             end
  180.         elseif event == "modem_message" then--gets the message
  181.             if senderChannel == port then
  182.                 local tab = lowLevelMessageSplit(message)
  183.                 if verbose then
  184.                     print("Modem message: " .. message)
  185.                 end
  186.                 if tab[1] == responseText then
  187.                     if tonumber(tab[3]) == os.getComputerID() then
  188.                         local p = {}
  189.                         p.sucess = true
  190.                         p.tab = tab
  191.                         atLeastOne = true
  192.                         table.insert(all.package,p)
  193.                         if verbose then
  194.                             print("This computer: " .. tab[3] .. ". Request response recived from " .. tab[2])
  195.                         end
  196.                         if timeOutTimer == -1 then
  197.                             all.sucess = true
  198.                             return all;
  199.                         end
  200.                     end
  201.                 end
  202.             end
  203.         end
  204.     end
  205. end
  206.  
  207. MAX_TIME_OUT = 0.8
  208. TIME_BETWEEN_RESENDS = 0.2
  209. NUMBER_OF_RESENDS = 4
  210. VERBOSE = false
  211.  
  212. --easy to use methods
  213. --> the low level lowLevelListener returns true if atleast 1 tramission has been valid and in this case only 1 transmision
  214. function basicListener(modem)
  215.     return lowLevelListener(modem,BROADCAST_PORT,VERBOSE)--this will never finish, it will listen forever
  216. end
  217.  
  218. -->Low level request return
  219. --.sucess
  220. --.package[instance].sucess = true
  221. --.package[instance].tab = tab
  222. function basicBroadcast(modem,request)
  223.    
  224.     return lowLevelRequest(modem,request,BROADCAST_PORT,MAX_TIME_OUT,TIME_BETWEEN_RESENDS,NUMBER_OF_RESENDS,nil,nil,VERBOSE)--A broadcast request which will conenct to everyone
  225. end
  226.  
  227. function basicRequest(modem,request,computer)
  228.     return lowLevelRequest(modem,request,BROADCAST_PORT,MAX_TIME_OUT,TIME_BETWEEN_RESENDS,NUMBER_OF_RESENDS,computer,nil,VERBOSE)-- a message for a certian computer
  229. end
  230.  
  231. function basicBroadcastWithData(modem,request,data)
  232.     return lowLevelRequest(modem,request,BROADCAST_PORT,MAX_TIME_OUT,TIME_BETWEEN_RESENDS,NUMBER_OF_RESENDS,nil,data,VERBOSE)-- this a a broadcast with data
  233. end
  234.  
  235. function basicRequestWithData(modem,request,computer,data)
  236.     return lowLevelRequest(modem,request,BROADCAST_PORT,MAX_TIME_OUT,TIME_BETWEEN_RESENDS,NUMBER_OF_RESENDS,computer,data,VERBOSE) -- this is a message for a certian computer with data
  237. end
  238.  
  239. function addActionListener(message,func)
  240.     --function parameters required (data)
  241.     --.message -- the message that was transmitted
  242.     --.senderId -- the sender of the message
  243.     --.reciverId -- the reciver of the message
  244.     --.data -- the data that might have been passed along
  245.     local tab = {}
  246.     tab.message = message
  247.     tab.func = func
  248.     table.insert(lowLevelListeners,tab)
  249.     return #lowLevelListeners
  250. end
  251.  
  252. function removeActionListener(index)
  253.     table.remove(lowLevelListeners,index)
  254. end
  255. --End Of Layer 1
  256.  
  257. --Routeing layer
  258. portsTaken = {}
  259. computerId = {}
  260.  
  261. function openBroadcastNode(modem)
  262.     portsTaken[BROADCAST_PORT] = true
  263.     modem.open(BROADCAST_PORT)
  264. end
  265.  
  266. function portInit(modem)
  267.     for i = 1, 128, 1 do
  268.         portsTaken[i] = false
  269.         if modem.isOpen(i) then
  270.             modem.close(i)
  271.         end
  272.     end
  273. end
  274.  
  275. function findEmptyPort(modem)
  276.     for i = 1,128, 1 do
  277.         if portsTaken[i] == false then
  278.             return i
  279.         end
  280.     end
  281.     return -1
  282. end
  283.  
  284. function routerServer(modem)
  285.     portInit(modem)
  286.     openBroadcastNode(modem)
  287.     local requestRoute = function(data)
  288.         local port = findEmptyPort(modem)
  289.         computerId[port] = data.senderId
  290.         portsTaken[port] = true
  291.         data.data = tostring(port)
  292.         print("Port Found: " .. port)
  293.     end
  294.     addActionListener("addport",requestRoute)
  295.     local deleteRoute = function(data)
  296.         local port = tonumber(data.data)
  297.         if port ~= nil or port ~= -1 then
  298.             if data.senderId == computerId[port] then
  299.                 computerId[data.senderId] = -1
  300.                 portsTaken[port] = false
  301.                 data.data = "true"
  302.                 print("Port Deleted: " .. port)
  303.             else
  304.                 data.data = "false"
  305.             end
  306.         end    
  307.     end
  308.     addActionListener("removeport",deleteRoute)
  309.     addActionListener("ping",nil)
  310.     basicListener(modem)
  311. end
  312.  
  313. function deletePort(modem,port)
  314.     local d = basicBroadcastWithData(modem,"removeport",tostring(port))
  315.     for k, v in pairs(d.package) do
  316.         if #v.tab > 3 then
  317.             local state = v.tab[4]
  318.             if state ~=nil then
  319.                 if state == "true" then
  320.                     return true
  321.                 else
  322.                     return false
  323.                 end
  324.             end
  325.         end
  326.     end
  327.     print("router error")
  328.     return false
  329. end
  330.  
  331. function getRouterPort(modem)
  332.     local d = basicRequest(modem,"addport",routerComputerId)
  333.     for k, v in pairs(d.package) do
  334.         if #v.tab > 3 then
  335.             local p = v.tab[4]
  336.             if p ~= nil and p ~= "" then
  337.                 return tonumber(p)
  338.             end
  339.         end
  340.     end
  341.     print("router error")
  342.     return -1
  343. end
  344.  
  345. function routerInit(modem)
  346.     portInit(modem)
  347.     openBroadcastNode(modem)
  348.     local d = basicBroadcast(modem,"ping")
  349.     if d.sucess then
  350.         if #d.package > 0 then
  351.             local id = -1
  352.             for k, v in pairs(d.package) do
  353.                 if id == -1 or id == tonumber(v.tab[2]) then
  354.                     id = tonumber(v.tab[2])
  355.                     if id ~= -1 and id < 128 then
  356.                         routerComputerId = id
  357.                         return true
  358.                     end
  359.                 else
  360.                     print("Too many routers")
  361.                     return false
  362.                 end
  363.             end
  364.             print("unknown router Error")
  365.             return false
  366.         elseif #d.package == 0 then
  367.             print("No Routers detected")
  368.             return false
  369.         else
  370.             return false
  371.         end
  372.     else
  373.         print("Router not found")
  374.         return false
  375.     end
  376. end
  377. --End of routeing layer
  378. --begining of master layer core
  379. function splitText(text)
  380.     local textSplit = {}
  381.     local current = ""
  382.     for i = 1, #text, 1 do
  383.         local currentCharecter = text:sub(i,i)
  384.         if currentCharecter == '\n' then
  385.             table.insert(textSplit,current)
  386.             current = ""
  387.            
  388.         else
  389.             current = current .. currentCharecter
  390.         end
  391.     end
  392.     if current ~= ""  then
  393.         table.insert(textSplit,current)
  394.         current = ""
  395.     end
  396.     return textSplit
  397. end
  398.  
  399. function writeCenter(monitor,text,ypos)
  400.     local xSize, ySize = monitor.getSize()
  401.     monitor.setCursorPos((xSize / 2) - ((#text / 2) - 1),ypos)
  402.     monitor.write(text)
  403. end
  404.  
  405. function draw(monitor)
  406.     monitor.clear()
  407.     monitor.setBackgroundColor(colors.lightGray)
  408.     monitor.setTextColor(colors.lightBlue)
  409.     monitor.setTextScale(1)
  410.     drawButtons(monitor)
  411.     drawLines(monitor)
  412.     --writeCenter(monitor,"Electrial Transformer Controller",1)
  413. end
  414.  
  415. function drawButtons(monitor)
  416.     for k, v in pairs(buttons) do
  417.         drawButton(monitor,v)
  418.     end
  419. end
  420.  
  421. function drawButton(monitor,button)
  422.     local originalColor = monitor.getBackgroundColor()
  423.     if button["state"] == true then --sets the color based on it's state
  424.         monitor.setBackgroundColor(button["color2"])
  425.     else
  426.         monitor.setBackgroundColor(button["color1"])
  427.     end
  428.    
  429.     local xMiddle = math.floor(button["xSize"] / 2)--calculates the middle horizontally
  430.     local yMiddle = math.floor(button["ySize"] / 2)--calculates the middle vertically
  431.    
  432.     local text = splitText(button["text"])--this searchs for linebreaks and then seperates them into seperate tables
  433.     local textSizeHalfNotRounded = (#text / 2)
  434.     local textSizeHalf = math.floor(textSizeHalfNotRounded)
  435.    
  436.     local yExit = 0
  437.    
  438.     if (textSizeHalf == textSizeHalfNotRounded) then
  439.         yExit = (yMiddle + (textSizeHalf - 1)) + button["y"] --calculates where the y exit point is
  440.     else
  441.         yExit = (yMiddle + textSizeHalf) + button["y"]
  442.     end
  443.    
  444.     local yEntry = (yMiddle - textSizeHalf) + button["y"]--calculates where the y entry point is
  445.    
  446.     local xMax = button["x"] + button["xSize"]
  447.     local yMax = button["y"] + button["ySize"]
  448.    
  449.     for i = button["y"],yMax,1 do -- loops from the top to the bottom of the label
  450.         monitor.setCursorPos(button["x"],i) -- sets the position of the cursor
  451.         if i >= yEntry and i <= yExit then--checks to see if the y is at the line where the text goes
  452.             local textIndex = (i - yEntry) + 1--calculates what index text is at based on the yPos
  453.             local xEntry = button["x"] + (xMiddle - ((#text[textIndex] / 2)))--Finds the xEntry point based off the y
  454.             for j = button["x"],xMax,1 do -- loops from side to side of the label
  455.                 if j >= xEntry and j < xEntry + #text[textIndex] then-- portCheck if the charecters are within x entry
  456.                     local index = (j - xEntry) + 1--gets the index of the current charecter
  457.                     local textOut = text[textIndex]:sub(index,index)--prints out the charecter
  458.                     monitor.write(textOut)
  459.                 else
  460.                     monitor.write(" ")
  461.                 end
  462.             end
  463.         else
  464.             for j = button["x"],xMax,1 do -- loops from side to side of the label
  465.                 monitor.write(" ")--writes blank
  466.             end
  467.         end
  468.     end
  469.     monitor.setBackgroundColor(originalColor)
  470. end
  471.  
  472. function addButton(text,x,y,xSize,ySize,color1,color2,func)
  473.     local button = {}
  474.     button["text"] = text
  475.     button["state"] = false
  476.     button["x"] = x
  477.     button["y"] = y
  478.     button["xSize"] = xSize
  479.     button["ySize"] = ySize
  480.     button["color1"] = color1
  481.     button["color2"] = color2
  482.     button["func"] = func
  483.     table.insert(buttons,button)
  484.     return #buttons
  485. end
  486.  
  487. function removeButton(index)
  488.     table.remove(buttons,index)
  489. end
  490.  
  491. function removeAllButtons()
  492.     if #buttons ~= 0 then
  493.         buttons = {}
  494.     end
  495. end
  496.  
  497. function GetNumberOfDigits(number)
  498.     if number > 0 then
  499.         return math.log10(number) + 1
  500.     else
  501.         return 1
  502.     end
  503. end
  504.  
  505. function allNodesPresent()
  506.     for k, v in pairs(NODES) do
  507.         if nodeState[v] == nil or nodeState[v] == false then
  508.             return false
  509.         end
  510.     end
  511.     return true
  512. end
  513.  
  514. function checkIfNode(message)
  515.     for k, v in pairs(NODES) do
  516.         if message == v then
  517.             return true
  518.         end
  519.     end
  520.     return false
  521. end
  522.  
  523. function isNodePort(channel)
  524.     for k, v in pairs(nodePort) do
  525.         if channel == tonumber(v) then
  526.             return k
  527.         end
  528.     end
  529.     return -1
  530. end
  531.  
  532. function listen(monitor,modem)
  533.     while true do
  534.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  535.         if event == "modem_message" then
  536.             if senderChannel == 1 then
  537.                 if message == "~connect~" then
  538.                     print("Connection request recived")
  539.                     local port = getRouterPort(modem)
  540.                     if port ~= -1 then
  541.                         modem.open(port)
  542.                         conPort[port] = replyChannel
  543.                         print("connection established. returned response port: " .. port)
  544.                         modem.transmit(replyChannel,port,"~connected~")
  545.                     else
  546.                         print("a device could not connect to the server, can't find valid port")
  547.                         modem.transmit(replyChannel,1,"~connection-error~")
  548.                     end
  549.                 end
  550.             else
  551.                 if conPort[senderChannel] ~= nil then
  552.                     local index = isNodePort(senderChannel)
  553.                     --local index = conPort[senderChannel]
  554.                     if index ~= -1 then
  555.                         if message == "~close-connection" then
  556.                             print("closeing connection")
  557.                             closeConnection(index,monitor,modem,false)
  558.                         end
  559.                     else
  560.                         if checkIfNode(message) == true then
  561.                             print("Node Connecting")
  562.                             if nodeState[message] == nil or nodeState[message] == false then
  563.                                 nodeState[message] = true
  564.                                 nodePort[message] = senderChannel
  565.                                 nodeSendPort[message] = replyChannel
  566.                                 buttons[nodeButtons[message]]["state"] = true
  567.                                 modem.transmit(replyChannel,senderChannel,"~NodeAccepted~")
  568.                                 print("Node Accepted")
  569.                                 draw(monitor)
  570.                                 if allNodesPresent() then
  571.                                     addActiveButtons(monitor)
  572.                                     activateAlreadyPressedButtons(monitor,modem)
  573.                                     onAllNodesPresent()
  574.                                 end
  575.                                 draw(monitor)
  576.                             else
  577.                                 print("Node Not Accepted")
  578.                                 modem.transmit(replyChannel,senderChannel,"~NodeNotAccepted~")
  579.                             end
  580.                         else
  581.                             modem.transmit(replyChannel,senderChannel,"~NodeNotAccepted~")
  582.                         end
  583.                     end
  584.                 end
  585.             end
  586.         elseif event == "monitor_touch" then
  587.             local x = senderChannel
  588.             local y = replyChannel
  589.            
  590.             for k, v in pairs(buttons) do
  591.                 local xMax = v["x"] + v["xSize"]
  592.                 local yMax = v["y"] + v["ySize"]
  593.                 if x >= v["x"] and x <= xMax and y >= v["y"] and y <= yMax then
  594.                     v["func"](monitor,modem,v)
  595.                     break
  596.                 end
  597.             end
  598.         elseif event == "key" then
  599.             local key = modemSide
  600.             local held = senderChannel
  601.             if key == 31 then
  602.                
  603.                 print("closeing connections")
  604.                
  605.                 closeConnectionAll(monitor,modem,true)
  606.                 print("closeing program")
  607.                
  608.                 break
  609.             end
  610.         end
  611.     end
  612. end
  613.  
  614. function transmitMessage(messageForTransmission,index,monitor,modem)
  615.     if nodeState[index] == true then
  616.         local timer = os.startTimer(1)
  617.         local loop = 0     
  618.         modem.transmit(nodeSendPort[index],nodePort[index],messageForTransmission)
  619.         while true do
  620.             local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  621.            
  622.             if event == "timer" then
  623.                 if modemSide == timer then
  624.                     print("Connection failed, retransmitting")
  625.                     if loop >= 5 then
  626.                         print("Connection failed, assumeing node is shutdown, shuting down system...")
  627.                         closeConnection(index,monitor,modem,false)
  628.                         return false
  629.                     else
  630.                         modem.transmit(nodeSendPort[index],nodePort[index],messageForTransmission)
  631.                         timer = os.startTimer(0.5)
  632.                         loop = loop + 1
  633.                     end
  634.                 end
  635.             elseif event == "modem_message" then
  636.                 if senderChannel == nodePort[index] and replyChannel == nodeSendPort[index] then
  637.                     if message == "~Operation-Complete~" then
  638.                         break
  639.                     elseif message == "~close-connection~" then
  640.                         closeConnection(index,monitor,modem,false)
  641.                         return false
  642.                     end
  643.                 end
  644.             end
  645.         end
  646.     end
  647.     return true
  648. end
  649.  
  650. function closeConnection(index,monitor,modem,transmit)
  651.     local nodesPresent = allNodesPresent()
  652.     if transmit then
  653.         transmitMessage("~close-connection~",index,monitor,modem)
  654.     end
  655.     nodeState[index] = false
  656.     modem.close(nodePort[index])
  657.     buttons[nodeButtons[index]]["state"] = false
  658.     if nodesPresent then
  659.         transmitMessageToAll("redstone-off",monitor,modem)
  660.         saveState()
  661.         onAllNodesNotPresent()
  662.         addNodes(monitor)
  663.     end
  664.     deletePort(modem,conPort[index])
  665.     conPort[index] = -1
  666.     draw(monitor)
  667. end
  668.  
  669. function closeConnectionAll(monitor,modem,transmit)
  670.     for k, v in pairs(NODES) do
  671.         if nodeState[v] == true then
  672.             closeConnection(v,monitor,modem,transmit)
  673.         end
  674.     end
  675. end
  676.  
  677. function transmitMessageToAll(message,monitor,modem)
  678.     for k, v in pairs(NODES) do
  679.         if nodeState[v] then
  680.             local state = transmitMessage(message,v,monitor,modem)
  681.             if message ~= "~close-connection~" and state == false then
  682.                 break
  683.             end
  684.         end
  685.     end
  686. end
  687. function activateAlreadyPressedButtons(monitor,modem)
  688.     for k, v in pairs(NODES) do
  689.         loadButtonState(monitor,modem,v,buttonsPressed[v])
  690.     end
  691. end
  692. function addActiveButtons(modem)
  693.     nodeButtons = {}
  694.     removeAllButtons()
  695.     for k, v in pairs(NODES) do
  696.         nodeButtons[v] = addButton(NODES_NAME[k] .. "\noff",buttonsPosition[v].x,buttonsPosition[v].y,BUTTON_SIZE_X,BUTTON_SIZE_Y,colors.red,colors.green,buttonsFunc[v])
  697.     end
  698. end
  699.  
  700. function nodeFunctions()
  701.     local nodeFunc = {}
  702.     for k, v in pairs(NODES) do
  703.         nodeFunc[v] = function(monitor,modem,button)
  704.         end
  705.     end
  706.     return nodeFunc
  707. end
  708.  
  709.  
  710. function SetConnectedNodesColor()
  711.     for k, v in pairs(NODES) do
  712.         if nodeState[v] ~= nil then
  713.             buttons[nodeButtons[v]]["state"] = nodeState[v]
  714.         end    
  715.     end
  716. end
  717. function addNodes(monitor)
  718.     nodeButtons = {}
  719.     local nodeFunc = nodeFunctions()
  720.     removeAllButtons()
  721.     for k, v in pairs(NODES) do
  722.         nodeButtons[v] = addButton(NODES_NAME[k],buttonsPosition[v].x,buttonsPosition[v].y,BUTTON_SIZE_X,BUTTON_SIZE_Y,colors.red,colors.green,nodeFunc[v])
  723.     end
  724.     SetConnectedNodesColor()
  725. end
  726. function calculateButtonPositions(monitor)
  727.     local buttonsPos = {}
  728.     local xPos = 2
  729.     local yPos = 9
  730.     for k, v in pairs(NODES) do
  731.         local buttonPos = {}
  732.         buttonPos.x = xPos
  733.         buttonPos.y = yPos
  734.         buttonsPos[v] = buttonPos
  735.         if (xPos + 10) < monitor.getSize() then
  736.             xPos = xPos + 10
  737.         else
  738.             xPos = 2
  739.             yPos = yPos + 6
  740.         end
  741.     end
  742.     buttonsPosition = buttonsPos
  743. end
  744. function placeButtonManually(index,x,y)
  745.     local buttonPos = {}
  746.     buttonPos.x = x
  747.     buttonPos.y = y
  748.     buttonsPosition[index] = buttonPos
  749. end
  750. function drawLine(monitor,x,y,xSize,ySize,color)
  751.     local originalColor = monitor.getBackgroundColor()
  752.     monitor.setBackgroundColor(color)
  753.     local xMax = x + xSize
  754.     local yMax = y + ySize
  755.  
  756.     for i = y,yMax,1 do
  757.         monitor.setCursorPos(x,i)
  758.         for j = x,xMax,1 do
  759.             monitor.write(" ")
  760.         end
  761.     end
  762.     monitor.setBackgroundColor(originalColor)
  763. end
  764. function drawLines(monitor)
  765.     --this is where you can place any lines you want drawn
  766.     for k, v in pairs(drawLinesData) do
  767.         drawLine(monitor,v.x,v.y,v.xSize,v.ySize,v.color)
  768.     end
  769. end
  770. function addLine(index,x,y,xSize,ySize,color)
  771.     local item = {}
  772.     item.x = x
  773.     item.y = y
  774.     item.xSize = xSize
  775.     item.ySize = ySize
  776.     item.color = color
  777.     drawLinesData[index] = item
  778. end
  779. function loadButtonState(monitor,modem,index,state)
  780.     if state == true then
  781.         local button = buttons[nodeButtons[index]]
  782.         button["state"] = true
  783.         local text = splitText(button["text"])
  784.         button["text"] = ""
  785.         for i = 1,#text - 1 do
  786.             button["text"] = button["text"] .. text[i] .. "\n"
  787.         end
  788.         button["text"] = button["text"] .. "on"
  789.         transmitMessage("redstone-on",index,monitor,modem)
  790.     end
  791.     draw(monitor)
  792. end
  793. function addButtonActionFunction(index,func)
  794.     buttonsFunc[index] = function(monitor,modem,button)
  795.         if button["state"] == false then
  796.             button["state"] = true
  797.             buttonsPressed[index] = true
  798.             local text = splitText(button["text"])
  799.             button["text"] = ""
  800.             for i = 1,#text - 1 do
  801.                 button["text"] = button["text"] .. text[i] .. "\n"
  802.             end
  803.             button["text"] = button["text"] .. "on"
  804.             transmitMessage("redstone-on",index,monitor,modem)
  805.             if func ~= nil then
  806.                 func(monitor,modem,button,true)
  807.             end
  808.             saveState()
  809.         else
  810.             button["state"] = false
  811.             buttonsPressed[index] = false
  812.             local text = splitText(button["text"])
  813.             button["text"] = ""
  814.             for i = 1, #text - 1 do
  815.                 button["text"] = button["text"] .. text[i] .. "\n"
  816.             end
  817.             button["text"] = button["text"] .. "off"
  818.             transmitMessage("redstone-off",index,monitor,modem)
  819.             if func ~= nil then
  820.                 func(monitor,modem,button,false)
  821.             end
  822.             saveState()
  823.         end
  824.         draw(monitor)
  825.     end
  826. end
  827. function generateDefaultButtonActions()
  828.     for k, v in pairs(NODES) do
  829.         addButtonActionFunction(v,nil)
  830.     end
  831. end
  832. function loadState()
  833.     local f = io.open("latest_state.txt", "r")
  834.     local map = {}
  835.     if f then
  836.         local s = f:read("*a")
  837.         f:close()
  838.         local k = ""
  839.         local v = ""
  840.         local state = false
  841.         local failed = false
  842.         for i = 1, #s, 1 do
  843.             if s:sub(i,i) == ':' then
  844.                 if state == false then
  845.                     state = true
  846.                 else
  847.                     print("io exception, too many :")
  848.                     failed = true
  849.                     break
  850.                 end
  851.             elseif s:sub(i,i) == ',' then
  852.                 if state == true then
  853.                     state = false
  854.                     if v == "true" then
  855.                         map[k] = true
  856.                     elseif v == "false" then
  857.                         map[k] = false
  858.                     else
  859.                         print("result is not true or false")
  860.                         failed = true
  861.                         break
  862.                     end
  863.                     k = ""
  864.                     v = ""
  865.                 else
  866.                     print("io exception, no key")
  867.                     failed = true
  868.                     break
  869.                 end
  870.             else
  871.                 if state then
  872.                     v = v .. s:sub(i,i)
  873.                 else
  874.                     k = k .. s:sub(i,i)
  875.                 end
  876.             end
  877.         end
  878.  
  879.         if failed == false then
  880.             if #map == #NODES then
  881.                 for k, v in pairs(NODES) do
  882.                     if map[v] == nil then
  883.                         failed = true
  884.                         break
  885.                     end
  886.                 end
  887.             else
  888.                 failed = true
  889.             end
  890.         end
  891.     end
  892.     return map
  893. end
  894.  
  895. function saveState()
  896.     local file = io.open("latest_state.txt" , "w")
  897.    
  898.     for k, v in pairs(buttonsPressed) do
  899.         file:write(k .. ":" .. tostring(v) .. ",")
  900.     end
  901.    
  902.     file:flush()
  903.     file:close()
  904. end
  905.  
  906. --end of Master Layers core
  907. --begining of Master Layers config layer
  908.  
  909. function onAllNodesPresent()
  910.     --allows you to config what happends when all nodes become present
  911.     drawCorespondingLines()
  912. end
  913.  
  914. function onAllNodesNotPresent()
  915.     --allows you to config what happends when one node leaves when all nodes were present
  916.     resetCorespondingLineColors()
  917. end
  918.  
  919. function setupButtonActionsManually()
  920.     --if you want to customize what happends when you select a button
  921.     --Example:addButtonActionFunction(index,func<(monitor,modem,button,newState)>)
  922.     addButtonActionFunction("gen1",function(monitor,modem,button,newState)
  923.         if newState then
  924.             if buttons[nodeButtons["gen2"]]["state"] == false then
  925.                 buttons[nodeButtons["gen2"]]["func"](monitor,modem,buttons[nodeButtons["gen2"]])
  926.             end
  927.         else
  928.             if buttons[nodeButtons["gen2"]]["state"] then
  929.                 buttons[nodeButtons["gen2"]]["func"](monitor,modem,buttons[nodeButtons["gen2"]])
  930.             end
  931.         end
  932.         resetCorespondingLineColors()
  933.         drawCorespondingLines()
  934.     end)
  935.     addButtonActionFunction("gen2",function(monitor,modem,button,newState)
  936.         if newState then
  937.             if buttons[nodeButtons["gen1"]]["state"] == false then
  938.                 buttons[nodeButtons["gen1"]]["func"](monitor,modem,buttons[nodeButtons["gen1"]])
  939.             end
  940.         else
  941.             if buttons[nodeButtons["gen1"]]["state"] then
  942.                 buttons[nodeButtons["gen1"]]["func"](monitor,modem,buttons[nodeButtons["gen1"]])
  943.             end
  944.         end
  945.         resetCorespondingLineColors()
  946.         drawCorespondingLines()
  947.     end)
  948.     addButtonActionFunction("gasturbine",function(monitor,modem,button,newState)
  949.         resetCorespondingLineColors()
  950.         drawCorespondingLines()
  951.     end)
  952.     addButtonActionFunction("tobatfromgen",function(monitor,modem,button,newState)
  953.         if newState then
  954.             if buttons[nodeButtons["tobatfromturbine"]]["state"] then
  955.                 buttons[nodeButtons["tobatfromturbine"]]["func"](monitor,modem,buttons[nodeButtons["tobatfromturbine"]])
  956.             end
  957.         end
  958.         resetCorespondingLineColors()
  959.         drawCorespondingLines()
  960.     end)
  961.     addButtonActionFunction("tobatfromturbine",function(monitor,modem,button,newState)
  962.         if newState then
  963.             if buttons[nodeButtons["tobatfromgen"]]["state"] then
  964.                 buttons[nodeButtons["tobatfromgen"]]["func"](monitor,modem,buttons[nodeButtons["tobatfromgen"]])
  965.             end
  966.         end
  967.         resetCorespondingLineColors()
  968.         drawCorespondingLines()
  969.     end)
  970.     addButtonActionFunction("4bat",function(monitor,modem,button,newState)
  971.         resetCorespondingLineColors()
  972.         drawCorespondingLines()
  973.     end)
  974.     addButtonActionFunction("straightout",function(monitor,modem,button,newState)
  975.         if newState then
  976.             if buttons[nodeButtons["frombat"]]["state"] then
  977.                 buttons[nodeButtons["frombat"]]["func"](monitor,modem,buttons[nodeButtons["frombat"]])
  978.             end
  979.             if buttons[nodeButtons["fromgas"]]["state"] then
  980.                 buttons[nodeButtons["fromgas"]]["func"](monitor,modem,buttons[nodeButtons["fromgas"]])
  981.             end
  982.         end
  983.         resetCorespondingLineColors()
  984.         drawCorespondingLines()
  985.     end)
  986.     addButtonActionFunction("frombat",function(monitor,modem,button,newState)
  987.         if newState then
  988.             if buttons[nodeButtons["straightout"]]["state"] then
  989.                 buttons[nodeButtons["straightout"]]["func"](monitor,modem,buttons[nodeButtons["straightout"]])
  990.             end
  991.             if buttons[nodeButtons["fromgas"]]["state"] then
  992.                 buttons[nodeButtons["fromgas"]]["func"](monitor,modem,buttons[nodeButtons["fromgas"]])
  993.             end
  994.         end
  995.         resetCorespondingLineColors()
  996.         drawCorespondingLines()
  997.     end)
  998.     addButtonActionFunction("fromgas",function(monitor,modem,button,newState)
  999.         if newState then
  1000.             if buttons[nodeButtons["frombat"]]["state"] then
  1001.                 buttons[nodeButtons["frombat"]]["func"](monitor,modem,buttons[nodeButtons["frombat"]])
  1002.             end
  1003.             if buttons[nodeButtons["straightout"]]["state"] then
  1004.                 buttons[nodeButtons["straightout"]]["func"](monitor,modem,buttons[nodeButtons["straightout"]])
  1005.             end
  1006.         end
  1007.         resetCorespondingLineColors()
  1008.         drawCorespondingLines()
  1009.     end)
  1010. end
  1011. function resetCorespondingLineColors()
  1012.     for k, v in pairs(drawLinesData) do
  1013.         v.color = colors.red
  1014.     end
  1015. end
  1016.  
  1017. --changes the colors of the lines depending what is switch on or off
  1018. function drawCorespondingLines()
  1019.     --drawLinesData[index]
  1020.     --buttonsPressed[index]
  1021.     if buttonsPressed["gen1"] and buttonsPressed["gen2"] then
  1022.         if buttonsPressed["straightout"] then
  1023.             drawLinesData["gen1out"].color = colors.green
  1024.             drawLinesData["gen1intersection"].color = colors.green
  1025.             drawLinesData["gentostraightout"].color = colors.green
  1026.             drawLinesData["gen2out"].color = colors.green
  1027.             drawLinesData["genconnection"].color = colors.green
  1028.             drawLinesData["gen2intersection"].color = colors.green
  1029.         end
  1030.         if buttonsPressed["tobatfromgen"] then
  1031.             drawLinesData["gen1out"].color = colors.green
  1032.             drawLinesData["gen1intersection"].color = colors.green
  1033.             drawLinesData["genconnection"].color = colors.green
  1034.             drawLinesData["gen2intersection"].color = colors.green
  1035.             drawLinesData["gentobattery"].color = colors.green
  1036.             drawLinesData["gen2out"].color = colors.green
  1037.         end
  1038.     elseif buttonsPressed["gen1"] then
  1039.         if buttonsPressed["straightout"] then
  1040.             drawLinesData["gen1out"].color = colors.green
  1041.             drawLinesData["gen1intersection"].color = colors.green
  1042.             drawLinesData["gentostraightout"].color = colors.green
  1043.         end
  1044.         if buttonsPressed["tobatfromgen"] then
  1045.             drawLinesData["gen1out"].color = colors.green
  1046.             drawLinesData["gen1intersection"].color = colors.green
  1047.             drawLinesData["genconnection"].color = colors.green
  1048.             drawLinesData["gen2intersection"].color = colors.green
  1049.             drawLinesData["gentobattery"].color = colors.green
  1050.         end
  1051.     elseif buttonsPressed["gen2"] then
  1052.         if buttonsPressed["straightout"] then
  1053.             drawLinesData["gen2out"].color = colors.green
  1054.             drawLinesData["gen2intersection"].color = colors.green
  1055.             drawLinesData["genconnection"].color = colors.green
  1056.             drawLinesData["gen1intersection"].color = colors.green
  1057.             drawLinesData["gentostraightout"].color = colors.green
  1058.         end
  1059.         if buttonsPressed["tobatfromgen"] then
  1060.             drawLinesData["gen2out"].color = colors.green
  1061.             drawLinesData["gen2intersection"].color = colors.green
  1062.             drawLinesData["gentobattery"].color = colors.green
  1063.         end
  1064.     end
  1065.    
  1066.     if buttonsPressed["gasturbine"] then
  1067.         if buttonsPressed["fromgas"] then
  1068.             drawLinesData["turbineoutline1"].color = colors.green
  1069.             drawLinesData["turbinestraightout1"].color = colors.green
  1070.             drawLinesData["turbinestraightout2"].color = colors.green
  1071.             drawLinesData["turbinestraightout3"].color = colors.green
  1072.             drawLinesData["turbinestraightout4"].color = colors.green
  1073.             drawLinesData["turbinestraightout5"].color = colors.green
  1074.         end
  1075.         if buttonsPressed["tobatfromturbine"] then
  1076.             drawLinesData["turbineoutline1"].color = colors.green
  1077.             drawLinesData["turbineoutlinetobat2"].color = colors.green
  1078.             drawLinesData["turbineoutlinetobat3"].color = colors.green
  1079.         end
  1080.     end
  1081.    
  1082.     if buttonsPressed["tobatfromgen"] then
  1083.         if buttonsPressed["gen1"] or buttonsPressed["gen2"] then
  1084.             drawLinesData["genToBat1"].color = colors.green
  1085.             drawLinesData["genToBat2"].color = colors.green
  1086.             drawLinesData["genToBat3"].color = colors.green
  1087.         end
  1088.     end
  1089.    
  1090.     if buttonsPressed["tobatfromturbine"] then
  1091.         if buttonsPressed["gasturbine"] then
  1092.             drawLinesData["turbineToBat1"].color = colors.green
  1093.             drawLinesData["turbineToBat2"].color = colors.green
  1094.             drawLinesData["turbineToBat3"].color = colors.green
  1095.         end
  1096.     end
  1097.    
  1098.     if buttonsPressed["4bat"] then
  1099.         if buttonsPressed["frombat"] then
  1100.             drawLinesData["bat1"].color = colors.green
  1101.         end
  1102.     end
  1103.    
  1104.     if buttonsPressed["straightout"] then
  1105.         if buttonsPressed["gen1"] or buttonsPressed["gen2"] then
  1106.             drawLinesData["outline"].color = colors.green
  1107.             drawLinesData["outlineconnection"].color = colors.green
  1108.             drawLinesData["lineout"].color = colors.green
  1109.            
  1110.         end
  1111.     end
  1112.    
  1113.     if buttonsPressed["frombat"] then
  1114.         if buttonsPressed["4bat"] then
  1115.             drawLinesData["batline"].color = colors.green
  1116.             drawLinesData["lineout"].color = colors.green
  1117.         end
  1118.     end
  1119.    
  1120.     if buttonsPressed["fromgas"] then
  1121.         if buttonsPressed["gasturbine"] then
  1122.             drawLinesData["gasline"].color = colors.green
  1123.             drawLinesData["gaslineconnection"].color = colors.green
  1124.             drawLinesData["lineout"].color = colors.green
  1125.         end
  1126.     end
  1127. end
  1128. function setupLines()
  1129.     --this is a mess, taking positions from anywhere I could reference them gg understanding it
  1130.     local gen1StartPosX = buttonsPosition["gen1"].x + BUTTON_SIZE_X
  1131.     local gen1StartPosY = buttonsPosition["gen1"].y + (BUTTON_SIZE_Y / 2)
  1132.     local gen2StartPosX = buttonsPosition["gen2"].x + BUTTON_SIZE_X
  1133.     local gen2StartPosY = buttonsPosition["gen2"].y + (BUTTON_SIZE_Y / 2)
  1134.    
  1135.     local genStraightOutPosX = buttonsPosition["straightout"].x
  1136.    
  1137.     --generator lines
  1138.     addLine("gen1out",gen1StartPosX + 1,gen1StartPosY,0,0,colors.red)
  1139.     addLine("gen1intersection",gen1StartPosX + 2,gen1StartPosY,0,0,colors.red)
  1140.     addLine("gen2out",gen2StartPosX + 1,gen2StartPosY,0,0,colors.red)
  1141.     addLine("gen2intersection",gen2StartPosX + 2,gen2StartPosY,0,0,colors.red)
  1142.     addLine("genconnection",gen1StartPosX + 2,gen1StartPosY + 1,0,3,colors.red)
  1143.     addLine("gentobattery",gen1StartPosX + 3, gen1StartPosY + 5,2,0,colors.red)
  1144.     addLine("gentostraightout",gen1StartPosX + 3,gen1StartPosY,genStraightOutPosX - gen1StartPosX - 4,0,colors.red)
  1145.    
  1146.     --gas turbine lines
  1147.     local turbineStartPosX = buttonsPosition["gasturbine"].x + BUTTON_SIZE_X
  1148.     local turbineStartPosY = buttonsPosition["gasturbine"].y + (BUTTON_SIZE_Y / 2)
  1149.     local toBatFromTurbinePosY = buttonsPosition["tobatfromturbine"].y + (BUTTON_SIZE_Y / 2)
  1150.     local toBatFromTurbinePosX = buttonsPosition["tobatfromturbine"].x + BUTTON_SIZE_X
  1151.     local straightOutFromTurbinePosY = buttonsPosition["fromgas"].y + (BUTTON_SIZE_Y / 2)
  1152.     local straightOutFromTurbinePosX = buttonsPosition["fromgas"].x
  1153.    
  1154.     addLine("turbineoutline1",turbineStartPosX + 1,turbineStartPosY,1,0,colors.red)
  1155.     addLine("turbineoutlinetobat2",turbineStartPosX + 2,turbineStartPosY - 2,0,2,colors.red)
  1156.     addLine("turbineoutlinetobat3",turbineStartPosX + 2,toBatFromTurbinePosY,3,0,colors.red)
  1157.     addLine("turbinestraightout1",turbineStartPosX + 2,straightOutFromTurbinePosY,1,0,colors.red)
  1158.     addLine("turbinestraightout2",turbineStartPosX + 3,straightOutFromTurbinePosY + 1,0,0,colors.red)
  1159.     addLine("turbinestraightout3",turbineStartPosX + 3,straightOutFromTurbinePosY + 2,toBatFromTurbinePosX - (turbineStartPosX + 3) + 2,0,colors.red)
  1160.     addLine("turbinestraightout4",toBatFromTurbinePosX + 2,straightOutFromTurbinePosY,0,1,colors.red)
  1161.     addLine("turbinestraightout5",toBatFromTurbinePosX + 3,straightOutFromTurbinePosY,straightOutFromTurbinePosX - toBatFromTurbinePosX - 4,0,colors.red)
  1162.    
  1163.     --battery lines
  1164.     local genToBatPosX = buttonsPosition["tobatfromgen"].x + BUTTON_SIZE_X
  1165.     local genToBatPosY = buttonsPosition["tobatfromgen"].y + (BUTTON_SIZE_Y / 2)
  1166.     local turbineToBatPosX = buttonsPosition["tobatfromturbine"].x + BUTTON_SIZE_X
  1167.     local turbineToBatPosY = buttonsPosition["tobatfromturbine"].y + (BUTTON_SIZE_Y / 2)
  1168.     local batPosX = buttonsPosition["4bat"].x + BUTTON_SIZE_X
  1169.     local batPosY = buttonsPosition["4bat"].y + (BUTTON_SIZE_Y / 2)
  1170.     local batOutPosX = buttonsPosition["frombat"].x
  1171.     local batOutPosY = buttonsPosition["frombat"].y + (BUTTON_SIZE_Y / 2)
  1172.    
  1173.     addLine("genToBat1",genToBatPosX + 1,genToBatPosY - 1,1,0,colors.red)
  1174.     addLine("genToBat2",genToBatPosX + 2,genToBatPosY,0,0,colors.red)
  1175.     addLine("genToBat3",genToBatPosX + 2,genToBatPosY + 1,(batPosX - BUTTON_SIZE_X) - turbineToBatPosX - 3,0,colors.red)
  1176.    
  1177.     addLine("turbineToBat1",turbineToBatPosX + 1,turbineToBatPosY + 1,1,0,colors.red)
  1178.     addLine("turbineToBat2",turbineToBatPosX + 2,turbineToBatPosY -2,0,3,colors.red)
  1179.     addLine("turbineToBat3",turbineToBatPosX + 2,turbineToBatPosY -2,(batPosX - BUTTON_SIZE_X) - turbineToBatPosX - 3,0,colors.red)
  1180.    
  1181.     addLine("bat1",batPosX + 1,batPosY,batOutPosX - batPosX - 2,0,colors.red)
  1182.    
  1183.     --all outputs
  1184.     local outLinePosX = buttonsPosition["straightout"].x + BUTTON_SIZE_X
  1185.     local outLinePosY = buttonsPosition["straightout"].y + (BUTTON_SIZE_Y / 2)
  1186.     local batLineOutPosX = buttonsPosition["frombat"].x + BUTTON_SIZE_X
  1187.     local batLineOutPosY = buttonsPosition["frombat"].y + (BUTTON_SIZE_Y / 2)
  1188.     local gasLineOutPosX = buttonsPosition["fromgas"].x + BUTTON_SIZE_X
  1189.     local gasLineOutPosY = buttonsPosition["fromgas"].y + (BUTTON_SIZE_Y / 2)
  1190.    
  1191.     addLine("outline",outLinePosX + 1,outLinePosY,0,0,colors.red)
  1192.     addLine("outlineconnection",outLinePosX + 2,outLinePosY,0,batLineOutPosY - outLinePosY - 1,colors.red)
  1193.     addLine("batline",batLineOutPosX + 1,batLineOutPosY,0,0,colors.red)
  1194.     addLine("gasline",gasLineOutPosX + 1,gasLineOutPosY,0,0,colors.red)
  1195.     addLine("lineout",batLineOutPosX + 2,batLineOutPosY,1,0,colors.red)
  1196.     addLine("gaslineconnection",batLineOutPosX + 2,batLineOutPosY + 1,0,gasLineOutPosY - batLineOutPosY - 1,colors.red)
  1197. end
  1198.  
  1199. --NODES = {"gen1","gen2","gasturbine","tobatfromgen","tobatfromturbine","4bat","straightout","frombat","fromgas"}
  1200.  
  1201. function placeButtonsManually()
  1202. --if you want to setup a custom button configuration you can do it here
  1203. --example: placeButtonManually("NAME_OF_NODE",x,y)
  1204.     placeButtonManually("gen1",2,2)
  1205.     placeButtonManually("gen2",2,7)
  1206.     placeButtonManually("gasturbine",2,14)
  1207.     placeButtonManually("tobatfromgen",16,6)
  1208.     placeButtonManually("tobatfromturbine",16,11)
  1209.     placeButtonManually("4bat",28,8)
  1210.     placeButtonManually("straightout",39,2)
  1211.     placeButtonManually("frombat",39,8)
  1212.     placeButtonManually("fromgas",39,14)
  1213. end
  1214.  
  1215.  
  1216. function drawMonitor()
  1217.     local monitor = peripheral.wrap(MONITOR_SIDE)
  1218.    
  1219.     --setup
  1220.     --generateDefaultButtonActions() -- this will generate the default actions which turns then green to red
  1221.     --calculateButtonPositions(monitor) -- this will place the buttons automaticly based on the monitor
  1222.     setupButtonActionsManually()
  1223.     placeButtonsManually()
  1224.     setupLines()
  1225.    
  1226.     addNodes(monitor)
  1227.    
  1228.     draw(monitor)
  1229.     --test(monitor)
  1230.     return monitor
  1231. end
  1232. --end of master Layer config
  1233. function runMaster()
  1234.     local modem = detectModem()
  1235.     routerInit(modem)
  1236.     modem.open(1)
  1237.     local monitor = drawMonitor()
  1238.     buttonsPressed = loadState()
  1239.     listen(monitor,modem)
  1240. end
  1241.  
  1242. runMaster()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement