Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local IECNET = true
- -- end of config
- local args = {...}
- --IECNET setup
- local gpsEnabled = false
- local net = "IECNET"
- local host = 0
- function iecnetSetup()
- if (IECNET) then
- if (peripheral.isPresent("back")) then
- if (peripheral.getType("back") == "modem") then
- --peripheral is present, starting IECNET
- rednet.open("back")
- host = rednet.lookup(net,"IEC-Host")
- if (host == nil) then
- term.write(net.." does not have a Host or Host is Offline, broadcasts will go unheard.")
- IECNET = false
- end
- x,y,z = gps.locate()
- if (x == nil) then
- term.write("GPS not available, will not send Location Data")
- else
- gpsEnabled = true;
- end
- rednet.broadcast("Online",net)
- else
- term.write("IECNET was enabled, but no modem was found.")
- IECNET = false
- end
- else
- term.write("IECNET was enabled, but no modem was found.")
- IECNET = false
- end
- end
- end
- function getLocation()
- x,y,z = gps.locate()
- if (x == nil) then
- gpsEnabled = false
- return nil
- else
- if (not gpsEnabled) then
- gpsEnabled = true
- end
- x = math.floor(x+0.5)
- y = math.floor(y+0.5)
- z = math.floor(z+0.5)
- return tostring(x).." "..tostring(y).." "..tostring(z)
- end
- end
- function nextLine()
- x,y = term.getCursorPos()
- term.setCursorPos(1,y+1)
- end
- function head()
- term.setCursorPos(1,1)
- x,y = term.getSize()
- term.write("+")
- for i=1,(x-13)/2,1 do
- term.write("-")
- end
- term.write("[")
- term.setTextColor(colors.yellow)
- term.write("IEC-OS.aic")
- term.setTextColor(colors.white)
- term.write("]")
- for i=1,(x-13)/2,1 do
- term.write("-")
- end
- term.write("+")
- end
- function foot()
- x,y = term.getSize()
- term.setCursorPos(1,y)
- term.write("+")
- for i=1,1,1 do
- term.write("-")
- end
- term.write("[")
- term.setTextColor(colors.yellow)
- term.write("Home")
- term.setTextColor(colors.white)
- term.write("]")
- if (IECNET) then
- for i=1,x-(8+11),1 do
- term.write("-")
- end
- term.write("[")
- term.setTextColor(colors.green)
- term.write("Online")
- term.setTextColor(colors.white)
- term.write("]--+")
- else
- for i=1,x-(8+11),1 do
- term.write("-")
- end
- term.write("[")
- term.setTextColor(colors.red)
- term.write("Offline")
- term.setTextColor(colors.white)
- term.write("]-+")
- end
- end
- function functionals()
- term.clear()
- head()
- foot()
- term.setCursorPos(1,2)
- end
- function startAnimation()
- --TODO
- end
- function divider()
- x,y = term.getSize()
- term.write("+")
- for i=1,(x-2),1 do
- term.write("-")
- end
- term.write("+")
- nextLine()
- end
- function screenHead(label)
- x,y = term.getSize()
- term.write("+-["..label.."]")
- length = 4+ string.len(label)+1
- for i=1,(x-length),1 do
- term.write("-")
- end
- term.write("+")
- nextLine()
- end
- local buttons = {}
- --creates and registers a button
- function button(label,text)
- screenHead(label)
- x,y = term.getSize()
- term.write("|")
- term.write(text)
- length = 2+ string.len(text)
- for i=1, (x-length),1 do
- term.write(" ")
- end
- term.write("|")
- x,y = term.getCursorPos()
- buttons[y] = label
- nextLine()
- end
- function clearButtons()
- buttons = {}
- end
- function printPercentFilled(startLine,percent)
- term.setCursorPos(4,startLine)
- term.write("Filled to: ")
- if (percent > 30) then
- term.setTextColor(colors.green)
- elseif (percent > 15) then
- term.setTextColor(colors.yellow)
- elseif (percent > 7) then
- term.setTextColor(colors.orange)
- else
- term.setTextColor(colors.red)
- end
- term.setCursorPos(4,startLine+1)
- term.write(percent)
- term.setTextColor(colors.white)
- term.write("%")
- term.setCursorPos(4,startLine+2)
- end
- function printIO(startLine,bankIO,unit)
- term.setCursorPos(4,startLine)
- term.write("I/O:")
- if (bankIO > 0) then
- term.setTextColor(colors.green)
- elseif (bankIO < 0) then
- term.setTextColor(colors.red)
- else
- term.setTextColor(colors.blue)
- end
- term.setCursorPos(4,startLine+1)
- term.write(bankIO)
- term.setTextColor(colors.white)
- term.write(" "..unit)
- term.setCursorPos(4,startLine+3)
- end
- function printETA(startLine,sign,eta,percentFilled)
- term.setCursorPos(4,startLine)
- if (sign == "+") then
- term.write("Full in:")
- term.setTextColor(colors.white)
- elseif (sign == "-") then
- term.write("Empty in:")
- term.setTextColor(colors.red)
- else
- term.write("Full in:")
- term.setCursorPos(4,startLine+1)
- if (percentFilled == 100) then
- eta = "Overfilling!"
- else
- eta = "No I/O"
- return
- end
- end
- term.setCursorPos(4,startLine+1)
- term.write(eta)
- term.setTextColor(colors.white)
- term.setCursorPos(4,startLine+2)
- end
- function printMainBar(startLine,filledPercent)
- x = 2
- term.setCursorPos(x,startLine)
- filledPercent = math.ceil(filledPercent/10)
- for i=1,10-filledPercent,1 do
- x,y = term.getCursorPos()
- term.write("|")
- term.setCursorPos(x,y+1)
- end
- for i=1,filledPercent,1 do
- x,y = term.getCursorPos()
- term.write("#")
- term.setCursorPos(x,y+1)
- end
- end
- function capacitorReadouts(startLine,toPrint)
- if ( toPrint["online"] == true) then
- term.setCursorPos(4,startLine)
- printPercentFilled(startLine,toPrint["fillstat"])
- nextLine()
- x,y = term.getCursorPos()
- printIO(y,toPrint["bankIO"],toPrint["unit"])
- nextLine()
- x,y = term.getCursorPos()
- printETA(y,toPrint["sign"],toPrint["eta"],toPrint["fillstat"])
- printMainBar(startLine,toPrint["fillstat"])
- else
- term.setTextColor(colors.red)
- term.write("Capacitor Offline")
- term.setTextColor(colors.white)
- x,y = term.getCursorPos()
- term.setCursorPos(1,y+10)
- end
- end
- local SCREEN = "Home"
- local page = 1 --pagenumber for Energynet screen
- local pagenum = 0 --number of pages for Energynet screen
- local currentlyLoggedIn = false
- function screenChange(newScreen)
- clearButtons()
- if (newScreen == "Turtles") then
- functionals()
- screenHead(SCREEN)
- elseif (newScreen == nil) then
- functionals()
- screenHead("Nil Screen")
- end
- end
- local turretControlButtons = {}
- function printTurretControl(label,states)
- local inactiveColor = colors.gray
- local activeColor = colors.green
- x,y = term.getSize()
- term.write("|"..label)
- for i=1,x-2-string.len(label)-3-3,1 do
- term.write(" ")
- end
- term.write("|")
- term.setBackgroundColor(inactiveColor)
- if (states["n"] == true) then
- term.setBackgroundColor(activeColor)
- end
- term.write(" ")
- term.setBackgroundColor(colors.black)
- term.write("|")
- term.setBackgroundColor(inactiveColor)
- if (states["m"] == true) then
- term.setBackgroundColor(activeColor)
- end
- term.write(" ")
- term.setBackgroundColor(colors.black)
- term.write("|")
- term.setBackgroundColor(inactiveColor)
- if (states["p"] == true) then
- term.setBackgroundColor(activeColor)
- end
- term.write(" ")
- term.setBackgroundColor(colors.black)
- term.write("|")
- x,y = term.getCursorPos()
- turretControlButtons[y] = label
- nextLine()
- end
- local lastTurretnetState = {}
- --touch handeling
- function touchHandeling(buttonPressed,x,y)
- xMax,yMax = term.getSize()
- if ((y == yMax) and (x > 2) and (x < 8)) then
- --Home button was pressed
- SCREEN = "Home"
- return
- end
- if (SCREEN == "Home" or SCREEN == "Turtles") then
- if (x > 1 and x < xMax) then
- change = buttons[y]
- if (not (change == nil)) then
- SCREEN = change
- screenChange(change)
- end
- end
- elseif (SCREEN == "Energy") then
- if ((y == yMax-2) or (y == yMax-3)) then
- if (x == 1) then
- if (not (page == 1)) then
- page = page -1
- end
- elseif (x == xMax) then
- if (not (page == pagenum)) then
- page = page +1
- end
- end
- end
- elseif (SCREEN == "Turtle CMD") then
- if (x > 1 and x < xMax) then
- cmnd = buttons[y]
- if (not (cmnd == nil)) then
- term.write("Command: "..cmnd)
- if (cmnd == "Harvest") then
- rednet.broadcast("Turtlenet Harvest",net)
- elseif (cmnd == "Reboot") then
- rednet.broadcast("Turtlenet Reboot",net)
- end
- end
- end
- elseif (SCREEN == "Turretnet") then
- if (y == yMax-1) then
- if (x > xMax-11 and x < xMax-4) then
- rednet.send(host,"Turretnet reboot",net)
- end
- end
- if (not (turretControlButtons[y] == nil)) then
- --"+n+m+p+"
- returnMessage = {}
- returnMessage["format"] = "Securitynet"
- message = {}
- message["action"] = "control"
- message["label"] = turretControlButtons[y]
- if (x == xMax-1) then
- --player
- message["p"] = not lastTurretnetState[turretControlButtons[y]]["p"]
- elseif (x == xMax-3) then
- --mobs
- message["m"] = not lastTurretnetState[turretControlButtons[y]]["m"]
- elseif (x == xMax-5) then
- --neutrals
- message["n"] = not lastTurretnetState[turretControlButtons[y]]["n"]
- end
- returnMessage["message"] = message
- rednet.send(host,returnMessage,net)
- end
- end
- end
- function printSideChangeButtons(c1,c2)
- xO,yO = term.getCursorPos()
- x,y = term.getSize()
- term.setTextColor(c1)
- term.setCursorPos(1,y-3)
- term.write("/")
- term.setCursorPos(1,y-2)
- term.write("\\")
- term.setTextColor(c2)
- term.setCursorPos(x,y-3)
- term.write("\\")
- term.setCursorPos(x,y-2)
- term.write("/")
- term.setCursorPos(xO,yO)
- term.setTextColor(colors.white)
- end
- function sideChangeButtons()
- if (pagenum == 1 or pagenum == 0) then
- printSideChangeButtons(colors.gray,colors.gray)
- elseif (pagenum == 2) then
- if (page == 1) then
- printSideChangeButtons(colors.gray,colors.yellow)
- else
- printSideChangeButtons(colors.yellow,colors.gray)
- end
- else
- if((not (page == 1)) and (not (page == pagenum))) then
- printSideChangeButtons(colors.yellow,colors.yellow)
- else
- if (page == 1) then
- printSideChangeButtons(colors.gray,colors.yellow)
- else
- printSideChangeButtons(colors.yellow,colors.gray)
- end
- end
- end
- end
- function turretnetRebootButton()
- x,y = term.getSize()
- term.setCursorPos(1,y-1)
- term.write("+")
- for i=1,x-4-2-6,1 do
- term.write("-")
- end
- term.write("[")
- term.setTextColor(colors.red)
- term.write("Reboot")
- term.setTextColor(colors.white)
- term.write("]--+")
- end
- --rednet handeling
- local ticksSinceLastPing = 0
- function rednetHandeling(sender,message,prot)
- if (message == "pong" and prot == net) then --online checker
- ticksSinceLastPing = 0
- host = sender
- return
- elseif (message == "Logout" and prot == net and sender == host) then
- SCREEN = "Home"
- currentlyLoggedIn = false
- end
- if (SCREEN == "Home" or SCREEN == "Turtles" or SCREEN == "Turtle CMD" or SCREEN == "Security") then
- elseif (SCREEN == "Turtle Mon") then
- if (type(message) == "table" and prot == net) then --what has been sent is a table
- functionals()
- screenHead(SCREEN)
- if (message["format"] == "turtle") then
- message = message["message"]
- for i,line in pairs(message) do
- screenHead("Turtle: "..i)
- if (string.find(line,"\n") == nil) then
- term.write(line)
- nextLine()
- else
- for s in string.gmatch(line,"[^\r\n]+") do
- term.write(s)
- nextLine()
- end
- end
- end
- end
- divider()
- end
- elseif (SCREEN == "Energy") then
- if (type(message) == "table" and prot == net) then --what has been sent is a table
- functionals()
- screenHead(SCREEN)
- if (message["format"] == "energy") then
- message = message["message"]
- if (not (message == {})) then
- pages = {} -- list of pages, page numer is saved in higher scope
- pagenum = 0
- for k,v in pairs(message) do
- table.insert(pages,k)
- pagenum = pagenum +1
- end
- toPrint = message[pages[page]]
- if (not (toPrint == nil)) then
- if (not (toPrint["label"] == nil)) then
- screenHead(toPrint["label"])
- x,y = term.getCursorPos()
- capacitorReadouts(y,toPrint)
- x,y = term.getCursorPos()
- term.setCursorPos(1,y)
- divider()
- end
- end
- sideChangeButtons()
- else
- term.write("No Data.")
- end
- end
- end
- elseif (SCREEN == "Please Wait") then
- if(type(message) == "table" and prot == net) then
- if(message["format"] == "Securitynet-Login") then
- if(message["message"] == "Denied") then
- functionals()
- screenHead(SCREEN)
- term.setTextColor(colors.red)
- term.write("Access Denied")
- term.setTextColor(colors.white)
- SCREEN = "Home"
- elseif(message["message"] == "Success") then
- functionals()
- screenHead(SCREEN)
- term.setTextColor(colors.green)
- term.write("Access Granted")
- term.setTextColor(colors.white)
- SCREEN = "Turretnet"
- currentlyLoggedIn = true
- end
- end
- end
- elseif (SCREEN == "Turretnet") then
- if(type(message) == "table" and prot == net) then
- x,y = term.getSize()
- functionals()
- screenHead(SCREEN)
- term.write("+")
- for i=1,x-9,1 do
- term.write("-")
- end
- term.write("+n+m+p+")
- nextLine()
- if(message["format"] == "Securitynet-Data") then
- message = message["message"]
- lastTurretnetState = message
- turretControlButtons = {}
- for k,v in pairs(message) do
- printTurretControl(k,v)
- end
- end
- divider()
- turretnetRebootButton()
- end
- else
- functionals()
- screenHead("No SCREEN")
- end
- end
- function routine()
- functionals()
- if (SCREEN == "Home") then
- button("Turtles","View Turtle Options")
- button("Energy","View Capacitor Status")
- button("Security","Log on to IECNET")
- divider()
- elseif (SCREEN == "Turtles") then
- screenHead(SCREEN)
- button("Turtle Mon","View Turtle Status")
- button("Turtle CMD","Turtle Commands")
- divider()
- elseif (SCREEN == "Turtle Mon") then
- rednet.broadcast("Turtlenet querry",net)
- elseif (SCREEN == "Turtle CMD") then
- screenHead(SCREEN)
- button("Harvest","Start Harvest")
- button("Reboot","Reboot eligible Turtles")
- divider()
- elseif (SCREEN == "Energy") then
- rednet.broadcast("Energynet querry",net)
- elseif (SCREEN == "Security") then
- screenHead(SCREEN)
- if (currentlyLoggedIn) then
- SCREEN = "Turretnet"
- return
- end
- term.write("User:")
- nextLine()
- term.setTextColor(colors.yellow)
- term.write("> ")
- term.setTextColor(colors.white)
- user = read()
- term.write("Password:")
- nextLine()
- term.setTextColor(colors.yellow)
- term.write("> ")
- term.setTextColor(colors.white)
- pass = read()
- host = rednet.lookup(net)
- if (host == nil) then
- functionals()
- screenHead(SCREEN)
- term.setTextColor(colors.red)
- term.write("IECNET Unreachable.")
- else
- passMessage = {}
- passMessage["format"] = "Securitynet"
- message = {}
- message["action"] = "login"
- message["user"] = user
- message["pass"] = pass
- passMessage["message"] = message
- rednet.send(host,passMessage,net)
- end
- SCREEN = "Please Wait"
- elseif (SCREEN == "Turretnet") then
- rednet.send(host,"Turretnet querry",net)
- else
- functionals()
- screenHead("No SCREEN")
- end
- if (ticksSinceLastPing > 5) then
- IECNET = false
- else
- IECNET = true
- end
- rednet.broadcast("ping",net) --ping the network to see if it is online
- end
- --actual code
- iecnetSetup()
- functionals()
- local timer = os.startTimer(1)
- while true do
- event,a1,a2,a3 = os.pullEvent()
- if (event == "timer" and a1 == timer) then
- ticksSinceLastPing = ticksSinceLastPing +1
- routine()
- timer = os.startTimer(1)
- elseif (event == "mouse_click") then
- touchHandeling(a1,a2,a3)
- elseif (event == "rednet_message") then
- rednetHandeling(a1,a2,a3)
- end
- end
Add Comment
Please, Sign In to add comment