Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --APIS
- --[[ Computercraft UI API by ThePH]]
- -- functions comonly used, suing targets (being a monitor or term)
- function cur(target, x, y) target.setCursorPos(x, y) end
- function color(target,fg,bg) if fg > -1 then target.setTextColor(fg) end if bg > -1 then target.setBackgroundColor(bg) end end
- function drawPixel(target, x, y, nColour) if nColour then term.setBackgroundColor( nColour ) end cur(target, x, y) v.write(' ') end
- --Function to check if item is contained in tbl and return en key or 0 if not in tbl
- function inTable(tbl, item)
- if type(item) == 'table' then
- for i, v in pairs(item) do
- for key,value in pairs(tbl) do
- if value == v then return key end
- end
- end
- elseif type(item) == 'string' then
- for key,value in pairs(tbl) do
- if value == item then return key end
- end
- end
- return 0
- end
- -- Reset the the UI list to make it fit the 'format'
- function reset(list)
- list.ui = {}
- list.shape = {}
- return list
- end
- -- Function to create and edit the lists
- -- Adding a UI element
- function addUI(uiList, keyName, x, y, text, fg, bg, action, pages, output, enabled, visible)
- if enabled == nil then enabled = true end
- if visible == nil then visible = true end
- uiList.ui[keyName] = {x, y, text, fg, bg, action, pages, output, enabled, visible}
- return uiList
- end
- -- Adding a shape (p3 is not used yet, but its here just in case)
- function addShape(uiList, keyName, shape, x, y, p1, p2, p3, color, pages, output, enabled)
- if enabled == nil then enabled = true end
- uiList.shape[keyName] = {shape, x, y, p1, p2, p3, color, pages, output, enabled}
- return uiList
- end
- -- Editing a UI element
- function editUI(uiList, key, id, value)
- local ids = {'x','y','text','fg','bg','action','pages','output', 'enabled', 'visible'}
- local index = inTable(ids, id)
- uiList.ui[key][index] = value
- return uiList
- end
- -- Draws the UI and shapes (shapes before)
- function draw(uiList, pageList, dFG, dBG)
- for index, value in pairs(uiList.shape) do
- local shape, x, y, p1, p2, p3, color, pagesIn, output, enabled = unpack(value)
- local pageOk = false
- if inTable(pagesIn, pageList) > 0 then
- pageOk = true
- end
- if pageOk and enabled then
- if shape == 'line' then
- drawLine(x, y, p1, p2, output, color)
- elseif shape == 'outline' then
- drawOutline(x, y, p1, p2, output, color)
- elseif shape == 'box' then
- drawBox(x, y, p1, p2, output, color)
- end
- end
- end
- for index, value in pairs(uiList.ui) do
- local x, y, text, fg, bg, action, pagesIn, output, enabled, visible = unpack(value)
- local pageOk = false
- if inTable(pagesIn, pageList) > 0 then
- pageOk = true
- end
- if pageOk and visible and enabled then
- for i,v in pairs(output) do
- color(v, dFG, dBG)
- cur(v, x, y)
- color(v, fg, bg)
- v.write(text)
- end
- end
- end
- end
- -- Function to draw something not used for interaction (captions etc)
- function drawSingle(x, y, text, fg, bg, output)
- for i, v in pairs(output) do
- cur(v, x, y)
- color(v, fg, bg)
- v.write(text)
- end
- end
- -- Checks mouse function (using the ui list, x, y pages and (optional, the targets))
- -- returns the action, the key in the ui list and the position on the string
- function mouse(uiList, mx, my, pageList, target)
- for index, value in pairs(uiList.ui) do
- local x, y, text, fg, bg, action, pagesIn, output, enabled, visible = unpack(value)
- pageOk = false
- if inTable(pagesIn, pageList) > 0 then pageOk = true end
- if (not(target) or inTable(output, target) > 0) and not(action == 'nil') and pageOk and enabled then
- for i, v in pairs(pagesIn) do
- if inTable(pageList, v) then
- if my == y then
- if mx >= x and mx <= (x + text:len() - 1) then
- return action, index, (mx - x + 1)
- end
- end
- break
- end
- end
- end
- end
- return nil, nil, nil
- end
- -- function the draw an empty box on target
- function drawOutline(x1,y1, x2, y2, target, c)
- drawLine(x1,y1, x1, y2, target, c)
- drawLine(x1,y2, x2, y2, target, c)
- drawLine(x2,y1, x2, y2, target, c)
- drawLine(x1,y1, x2, y1, target, c)
- end
- -- function from paintutils ported for using targets
- local function drawPixelInternal( xPos, yPos, target )
- target.setCursorPos(xPos, yPos)
- target.write(" ")
- end
- -- function to draw line using targets
- function drawLine(startX, startY, endX, endY, target, nColour)
- for i, v in pairs(target) do
- line( startX, startY, endX, endY, v, nColour )
- end
- end
- -- function to draw a filled box on target
- function drawBox(startX, startY, endX, endY, target, nColour)
- if startY <= endY then s = 1 else s= -1 end
- for index=startY, endY, s do
- drawLine( startX, index, endX, index, target, nColour )
- end
- end
- -- function from paintutils ported for using targets
- function line( startX, startY, endX, endY, v, nColour )
- if nColour then
- v.setBackgroundColor( nColour )
- end
- startX = math.floor(startX)
- startY = math.floor(startY)
- endX = math.floor(endX)
- endY = math.floor(endY)
- if startX == endX and startY == endY then
- drawPixelInternal( startX, startY, v )
- return
- end
- local minX = math.min( startX, endX )
- if minX == startX then
- minY = startY
- maxX = endX
- maxY = endY
- else
- minY = endY
- maxX = startX
- maxY = startY
- end
- local xDiff = maxX - minX
- local yDiff = maxY - minY
- if xDiff > math.abs(yDiff) then
- local y = minY
- local dy = yDiff / xDiff
- for x=minX,maxX do
- drawPixelInternal( x, math.floor( y + 0.5 ), v )
- y = y + dy
- end
- else
- local x = minX
- local dx = xDiff / yDiff
- if maxY >= minY then
- for y=minY,maxY do
- drawPixelInternal( math.floor( x + 0.5 ), y, v )
- x = x + dx
- end
- else
- for y=minY,maxY,-1 do
- drawPixelInternal( math.floor( x + 0.5 ), y, v )
- x = x - dx
- end
- end
- end
- end
- --START PROGRAM HERE
- tConfig = {}
- pMonitor = {}
- function fGrabPheripherals()
- if tConfig["is-receiver"] then
- local p = peripheral.find("monitor", function(name, object) object.side = name return true end)
- if p == nil then
- error("Could not find a monitor!")
- return false
- end
- pMonitor = peripheral.wrap(p.side)
- end
- local p = peripheral.find("modem", function(name, object) object.side = name return true end)
- if p == nil then
- error("Could not find a modem")
- return false
- end
- rednet.open(p.side)
- return true
- end
- --Data types (ae2, steam-turbines, reactor, power-storage[GENERIC THIS WILL CHANGE TO A SPECIFIC TYPE])
- --Receiver Side (Monitoring)
- function fListenLoop()
- while true do
- fListenLoopUpdate()
- end
- end
- function fListenLoopUpdate()
- tEvent = {rednet.receive(tConfig["protocol-id"])}
- tPacket = textutils.unserialize(tEvent[2])
- if tPacket == nil then
- print("Received invalid packet data")
- print("Invalid message from (" .. tEvent[1] .. "): " .. tPacket[2])
- return
- end
- local tData = tPacket[tConfig["data-type"]]
- if tData == nil then
- print("Received invalid data type")
- print("Data typed received from (" .. tEvent[1] .. "): " .. tEvent[2])
- return
- end
- print("Data received: " .. tEvent[2])
- --Render data
- end
- --Sender Side (Data sending)
- function fSendPacket(dataType, statusContent)
- local tToBeSent = {[dataType]=statusContent}
- rednet.broadcast(textutils.serialize(tToBeSent),tConfig["protocol-id"])
- end
- function fSendLoop()
- while true do
- if tConfig["data-type"] == "ae2" then
- ae = peripheral.wrap("right")
- tSend = {["power"]=ae.getDemandedEnergy()}
- fSendPacket("ae2",tSend)
- end
- sleep(tConfig["update-time"])
- end
- end
- --
- function fGetConfig()
- local tFile = fs.open("config","r")
- local sReturn = textutils.unserialize(tFile.readAll())
- tFile.close()
- return sReturn
- end
- if not fs.exists("config") then
- print("Starting setup routine!")
- local sReceiver = ""
- repeat
- print("Is this a sender or a reciever? [s/r]")
- sReceiver = read()
- until(sReceiver == "s" or sReceiver == "r")
- print("What is the data type?")
- local sType = read()
- print("What is the network ID?")
- local sID = read()
- local sDelay = ""
- repeat
- print("What should the time be between updates? (inseconds)")
- sDelay = read()
- until(tonumber(sDelay) ~= nil)
- local tWriteConfig = {
- ["is-receiver"] = (sReceiver == "r"), --receiver (Monitoring), node (Sends data)
- ["data-type"] = sType,
- ["protocol-id"] = sID,
- ["update-time"] = tonumber(sDelay)
- }
- local tFile = fs.open("config","w")
- tFile.write(textutils.serialize(tWriteConfig))
- tFile.close()
- end
- tConfig = fGetConfig()
- --Starts respective loop
- function onStart()
- if not fGrabPheripherals() then
- error("Invalid peripheral. Terminating")
- return
- end
- if tConfig["is-receiver"] then
- print("Starting loop as receiver")
- print("Data type: " .. tConfig["data-type"])
- print("Protocol id: " .. tConfig["protocol-id"])
- --print("Update interval: " .. tConfig["update-time"])
- fListenLoop()
- else
- print("Starting loop as sender")
- print("Data type: " .. tConfig["data-type"])
- print("Protocol id: " .. tConfig["protocol-id"])
- print("Update interval: " .. tConfig["update-time"])
- fSendLoop()
- end
- end
- onStart()
- if fGrabPheripherals() then
- else
- end
Add Comment
Please, Sign In to add comment