Grauly

CC_IEC-OS client v3

May 9th, 2021 (edited)
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.64 KB | None | 0 0
  1. local IECNET = true
  2. -- end of config
  3. local args = {...}
  4.  
  5. --IECNET setup
  6. local gpsEnabled = false
  7. local net = "IECNET"
  8. local host = 0
  9. function iecnetSetup()
  10.     if (IECNET) then
  11.         if (peripheral.isPresent("back")) then
  12.             if (peripheral.getType("back") == "modem") then
  13.                 --peripheral is present, starting IECNET
  14.                 rednet.open("back")
  15.                 host = rednet.lookup(net,"IEC-Host")
  16.                 if (host == nil) then
  17.                     term.write(net.." does not have a Host or Host is Offline, broadcasts will go unheard.")
  18.                     IECNET = false
  19.                 end
  20.                 x,y,z = gps.locate()
  21.                 if (x == nil) then
  22.                     term.write("GPS not available, will not send Location Data")
  23.                 else
  24.                     gpsEnabled = true;
  25.                 end
  26.                 rednet.broadcast("Online",net)
  27.             else
  28.                 term.write("IECNET was enabled, but no modem was found.")
  29.                 IECNET = false
  30.             end
  31.         else
  32.             term.write("IECNET was enabled, but no modem was found.")
  33.             IECNET = false
  34.         end
  35.     end
  36. end
  37.  
  38. function getLocation()
  39.     x,y,z = gps.locate()
  40.     if (x == nil) then
  41.         gpsEnabled = false
  42.         return nil
  43.     else
  44.         if (not gpsEnabled) then
  45.             gpsEnabled = true
  46.         end
  47.         x = math.floor(x+0.5)
  48.         y = math.floor(y+0.5)
  49.         z = math.floor(z+0.5)
  50.         return tostring(x).." "..tostring(y).." "..tostring(z)
  51.     end
  52. end
  53.  
  54. function nextLine()
  55.     x,y = term.getCursorPos()
  56.     term.setCursorPos(1,y+1)
  57. end
  58.  
  59. function head()
  60.     term.setCursorPos(1,1)
  61.     x,y = term.getSize()
  62.     term.write("+")
  63.     for i=1,(x-13)/2,1 do
  64.         term.write("-")
  65.     end
  66.     term.write("[")
  67.     term.setTextColor(colors.yellow)
  68.     term.write("IEC-OS.aic")
  69.     term.setTextColor(colors.white)
  70.     term.write("]")
  71.     for i=1,(x-13)/2,1 do
  72.         term.write("-")
  73.     end
  74.     term.write("+")
  75. end
  76.  
  77. function foot()
  78.     x,y = term.getSize()
  79.     term.setCursorPos(1,y)
  80.     term.write("+")
  81.     for i=1,1,1 do
  82.         term.write("-")
  83.     end
  84.     term.write("[")
  85.     term.setTextColor(colors.yellow)
  86.     term.write("Home")
  87.     term.setTextColor(colors.white)
  88.     term.write("]")
  89.     if (IECNET) then
  90.         for i=1,x-(8+11),1 do
  91.             term.write("-")
  92.         end
  93.         term.write("[")
  94.         term.setTextColor(colors.green)
  95.         term.write("Online")
  96.         term.setTextColor(colors.white)
  97.         term.write("]--+")
  98.     else
  99.         for i=1,x-(8+11),1 do
  100.             term.write("-")
  101.         end
  102.         term.write("[")
  103.         term.setTextColor(colors.red)
  104.         term.write("Offline")
  105.         term.setTextColor(colors.white)
  106.         term.write("]-+")
  107.     end
  108. end
  109.  
  110. function functionals()
  111.     term.clear()
  112.     head()
  113.     foot()
  114.     term.setCursorPos(1,2)
  115. end
  116.  
  117. function startAnimation()
  118.     --TODO
  119. end
  120.  
  121. function divider()
  122.     x,y = term.getSize()
  123.     term.write("+")
  124.     for i=1,(x-2),1 do
  125.         term.write("-")
  126.     end
  127.     term.write("+")
  128.     nextLine()
  129. end
  130.  
  131. function screenHead(label)
  132.     x,y = term.getSize()
  133.     term.write("+-["..label.."]")
  134.     length = 4+ string.len(label)+1
  135.     for i=1,(x-length),1 do
  136.         term.write("-")
  137.     end
  138.     term.write("+")
  139.     nextLine()
  140. end
  141.  
  142. local buttons = {}
  143. --creates and registers a button
  144. function button(label,text)
  145.     screenHead(label)
  146.     x,y = term.getSize()
  147.     term.write("|")
  148.     term.write(text)
  149.     length = 2+ string.len(text)
  150.     for i=1, (x-length),1 do
  151.         term.write(" ")
  152.     end
  153.     term.write("|")
  154.     x,y = term.getCursorPos()
  155.     buttons[y] = label
  156.     nextLine()
  157. end
  158.  
  159. function clearButtons()
  160.     buttons = {}
  161. end
  162.  
  163. function printPercentFilled(startLine,percent)
  164.     term.setCursorPos(4,startLine)
  165.     term.write("Filled to: ")
  166.     if (percent > 30) then
  167.         term.setTextColor(colors.green)
  168.     elseif (percent > 15) then
  169.         term.setTextColor(colors.yellow)
  170.     elseif (percent > 7) then
  171.         term.setTextColor(colors.orange)
  172.     else
  173.         term.setTextColor(colors.red)
  174.     end
  175.     term.setCursorPos(4,startLine+1)
  176.     term.write(percent)
  177.     term.setTextColor(colors.white)
  178.     term.write("%")
  179.     term.setCursorPos(4,startLine+2)
  180. end
  181.  
  182. function printIO(startLine,bankIO,unit)
  183.     term.setCursorPos(4,startLine)
  184.     term.write("I/O:")
  185.     if (bankIO > 0) then
  186.         term.setTextColor(colors.green)
  187.     elseif (bankIO < 0) then
  188.         term.setTextColor(colors.red)
  189.     else
  190.         term.setTextColor(colors.blue)
  191.     end
  192.     term.setCursorPos(4,startLine+1)
  193.     term.write(bankIO)
  194.     term.setTextColor(colors.white)
  195.     term.write(" "..unit)
  196.     term.setCursorPos(4,startLine+3)
  197. end
  198.  
  199. function printETA(startLine,sign,eta,percentFilled)
  200.     term.setCursorPos(4,startLine)
  201.     if (sign == "+") then
  202.         term.write("Full in:")
  203.         term.setTextColor(colors.white)
  204.     elseif (sign == "-") then
  205.         term.write("Empty in:")
  206.         term.setTextColor(colors.red)
  207.     else
  208.         term.write("Full in:")
  209.         term.setCursorPos(4,startLine+1)
  210.         if (percentFilled == 100) then
  211.             eta = "Overfilling!"
  212.         else
  213.             eta = "No I/O"
  214.         return
  215.         end
  216.     end
  217.     term.setCursorPos(4,startLine+1)
  218.     term.write(eta)
  219.     term.setTextColor(colors.white)
  220.     term.setCursorPos(4,startLine+2)
  221. end
  222.  
  223. function printMainBar(startLine,filledPercent)
  224.     x = 2
  225.     term.setCursorPos(x,startLine)
  226.     filledPercent = math.ceil(filledPercent/10)
  227.     for i=1,10-filledPercent,1 do
  228.         x,y = term.getCursorPos()
  229.         term.write("|")
  230.         term.setCursorPos(x,y+1)
  231.     end
  232.     for i=1,filledPercent,1 do
  233.         x,y = term.getCursorPos()
  234.         term.write("#")
  235.         term.setCursorPos(x,y+1)
  236.     end
  237. end
  238.  
  239. function capacitorReadouts(startLine,toPrint)
  240.     if ( toPrint["online"] == true) then
  241.         term.setCursorPos(4,startLine)
  242.         printPercentFilled(startLine,toPrint["fillstat"])
  243.         nextLine()
  244.         x,y = term.getCursorPos()
  245.         printIO(y,toPrint["bankIO"],toPrint["unit"])
  246.         nextLine()
  247.         x,y = term.getCursorPos()
  248.         printETA(y,toPrint["sign"],toPrint["eta"],toPrint["fillstat"])
  249.         printMainBar(startLine,toPrint["fillstat"])
  250.     else
  251.         term.setTextColor(colors.red)
  252.         term.write("Capacitor Offline")
  253.         term.setTextColor(colors.white)
  254.         x,y = term.getCursorPos()
  255.         term.setCursorPos(1,y+10)
  256.     end
  257. end
  258.  
  259. local SCREEN = "Home"
  260. local page = 1 --pagenumber for Energynet screen
  261. local pagenum = 0 --number of pages for Energynet screen
  262.  
  263. function screenChange(newScreen)
  264.     clearButtons()
  265.     if (newScreen == "Turtles") then
  266.         functionals()
  267.         screenHead(SCREEN)
  268.     elseif (newScreen == nil) then
  269.         functionals()
  270.         screenHead("Nil Screen")
  271.     end
  272. end
  273.  
  274. --touch handeling
  275. function touchHandeling(buttonPressed,x,y)
  276.     xMax,yMax = term.getSize()
  277.     if ((y == yMax) and (x > 2) and (x < 8)) then
  278.         --Home button was pressed
  279.         SCREEN = "Home"
  280.         return
  281.     end
  282.     if (SCREEN == "Home" or SCREEN == "Turtles") then
  283.         if (x > 1 and x < xMax) then
  284.             change = buttons[y]
  285.             if (not (change == nil)) then
  286.                 SCREEN = change
  287.                 screenChange(change)
  288.             end
  289.         end
  290.     elseif (SCREEN == "Energy") then
  291.         if ((y == yMax-2) or (y == yMax-3)) then
  292.             if (x == 1) then
  293.                 if (not (page == 1)) then
  294.                     page = page -1
  295.                 end
  296.             elseif (x == xMax) then
  297.                 if (not (page == pagenum)) then
  298.                     page = page +1
  299.                 end
  300.             end
  301.         end
  302.     elseif (SCREEN == "Turtle CMD") then
  303.         if (x > 1 and x < xMax) then
  304.             cmnd = buttons[y]
  305.             if (not (cmnd == nil)) then
  306.                 print("Command: "..cmnd)
  307.                 if (cmnd == "Harvest") then
  308.                     rednet.broadcast("Turtlenet Harvest",net)
  309.                 elseif (cmnd == "Reboot") then
  310.                     rednet.broadcast("Turtlenet Reboot",net)
  311.                 end
  312.             end
  313.         end
  314.     end
  315. end
  316.  
  317. function printSideChangeButtons(c1,c2)
  318.     xO,yO = term.getCursorPos()
  319.     x,y = term.getSize()
  320.     term.setTextColor(c1)
  321.     term.setCursorPos(1,y-3)
  322.     term.write("/")
  323.     term.setCursorPos(1,y-2)
  324.     term.write("\\")
  325.     term.setTextColor(c2)
  326.     term.setCursorPos(x,y-3)
  327.     term.write("\\")
  328.     term.setCursorPos(x,y-2)
  329.     term.write("/")
  330.     term.setCursorPos(xO,yO)
  331.     term.setTextColor(colors.white)
  332. end
  333.  
  334. function sideChangeButtons()
  335.     if (pagenum == 1 or pagenum == 0) then
  336.         printSideChangeButtons(colors.gray,colors.gray)
  337.     elseif (pagenum == 2) then
  338.         if (page == 1) then
  339.             printSideChangeButtons(colors.gray,colors.yellow)
  340.         else
  341.             printSideChangeButtons(colors.yellow,colors.gray)
  342.         end
  343.     else
  344.         if((not (page == 1)) and (not (page == pagenum))) then
  345.             printSideChangeButtons(colors.yellow,colors.yellow)
  346.         else
  347.             if (page == 1) then
  348.                 printSideChangeButtons(colors.gray,colors.yellow)
  349.             else
  350.                 printSideChangeButtons(colors.yellow,colors.gray)
  351.             end
  352.         end
  353.     end
  354. end
  355.  
  356. --rednet handeling
  357. local ticksSinceLastPing = 0
  358. function rednetHandeling(sender,message,prot)
  359.     if (message == "pong" and prot == net) then --online checker
  360.         ticksSinceLastPing = 0
  361.         return
  362.     end
  363.     if (SCREEN == "Home" or SCREEN == "Turtles" or SCREEN == "Turtle CMD") then
  364.        
  365.     elseif (SCREEN == "Turtle Mon") then
  366.         if (type(message) == "table" and prot == net) then --what has been sent is a table
  367.             functionals()
  368.             screenHead(SCREEN)
  369.             if (message["format"] == "turtle") then
  370.                 message = message["message"]
  371.                 for i,line in pairs(message) do
  372.                     screenHead("Turtle: "..i)
  373.                     if (string.find(line,"\n") == nil) then
  374.                         term.write(line)
  375.                         nextLine()
  376.                     else
  377.                         for s in string.gmatch(line,"[^\r\n]+") do
  378.                             term.write(s)
  379.                             nextLine()
  380.                         end
  381.                     end
  382.                 end
  383.             end
  384.             divider()
  385.         end
  386.     elseif (SCREEN == "Energy") then
  387.         if (type(message) == "table" and prot == net) then --what has been sent is a table
  388.             functionals()
  389.             screenHead(SCREEN)
  390.             if (message["format"] == "energy") then
  391.                 message = message["message"]
  392.                 if (not (message == {})) then
  393.                     pages = {} -- list of pages, page numer is saved in higher scope
  394.                     pagenum = 0
  395.                     for k,v in pairs(message) do
  396.                         table.insert(pages,k)
  397.                         pagenum = pagenum +1
  398.                     end
  399.                     toPrint = message[pages[page]]
  400.                     if (not (toPrint == nil)) then
  401.                         if (not (toPrint["label"] == nil)) then
  402.                             screenHead(toPrint["label"])
  403.                             x,y = term.getCursorPos()
  404.                             capacitorReadouts(y,toPrint)
  405.                             x,y = term.getCursorPos()
  406.                             term.setCursorPos(1,y)
  407.                             divider()
  408.                         end
  409.                     end
  410.                     sideChangeButtons()
  411.                 else
  412.                     term.write("No Data.")
  413.                 end
  414.             end
  415.         end
  416.     elseif (SCREEN == "Security") then
  417.         if (type(message) == "table" and prot == net) then
  418.             functionals()
  419.             screenHead(SCREEN)
  420.             if (message["format"] == "energy") then
  421.                 message = message["message"]
  422.                 if (message == "User querry") then
  423.                     term.write("Please Input your User:")
  424.                     input = read()
  425.                     rednet.send(sender,"Securitynet User set:"..input,net)
  426.                 elseif (message == "Pass querry") then
  427.                     term.write("Please Input your User:")
  428.                     input = read()
  429.                     rednet.send(sender,"Securitynet User set:"..input,net)
  430.                 else
  431.  
  432.                 end
  433.             else
  434.                 term.write("No Data.")
  435.             end
  436.         end
  437.     else
  438.         functionals()
  439.         screenHead("No SCREEN")
  440.     end
  441. end
  442.  
  443. function routine()
  444.     functionals()
  445.     if (SCREEN == "Home") then
  446.         button("Turtles","View Turtle Options")
  447.         button("Energy","View Capacitor Status")
  448.         --button("Security","Log on to SECNET")
  449.         divider()
  450.     elseif (SCREEN == "Turtles") then
  451.         screenHead(SCREEN)
  452.         button("Turtle Mon","View Turtle Status")
  453.         button("Turtle CMD","Turtle Commands")
  454.         divider()
  455.     elseif (SCREEN == "Turtle Mon") then
  456.         rednet.broadcast("Turtlenet querry",net)
  457.     elseif (SCREEN == "Turtle CMD") then
  458.         screenHead(SCREEN)
  459.         button("Harvest","Start Harvest")
  460.         button("Reboot","Reboot eligible Turtles")
  461.         divider()
  462.     elseif (SCREEN == "Energy") then
  463.         rednet.broadcast("Energynet querry",net)
  464.     elseif (SCREEN == "Security") then
  465.         rednet.broadcast("Securitynet logon",net)
  466.     else
  467.         functionals()
  468.         screenHead("No SCREEN")
  469.     end
  470.     if (ticksSinceLastPing > 5) then
  471.         IECNET = false
  472.     else
  473.         IECNET = true
  474.     end
  475.     rednet.broadcast("ping",net) --ping the network to see if it is online
  476. end
  477.  
  478. --actual code
  479.  
  480. iecnetSetup()
  481. functionals()
  482.  
  483. local timer = os.startTimer(1)
  484.  
  485. while true do
  486.     event,a1,a2,a3 = os.pullEvent()
  487.     if (event == "timer" and a1 == timer) then
  488.         ticksSinceLastPing = ticksSinceLastPing +1
  489.         routine()
  490.         timer = os.startTimer(1)
  491.     elseif (event == "mouse_click") then
  492.         touchHandeling(a1,a2,a3)
  493.     elseif (event == "rednet_message") then
  494.         rednetHandeling(a1,a2,a3)
  495.     end
  496. end
Add Comment
Please, Sign In to add comment