Grauly

CC_IEC-OS client v2

Apr 13th, 2021 (edited)
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.91 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("IECNET 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 = floor(x+0.5)
  48.         y = floor(y+0.5)
  49.         z = 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. end
  129.  
  130. function screenHead(label)
  131.     x,y = term.getSize()
  132.     term.write("+-["..label.."]")
  133.     length = 4+ string.len(label)+1
  134.     for i=1,(x-length),1 do
  135.         term.write("-")
  136.     end
  137.     term.write("+")
  138.     nextLine()
  139. end
  140.  
  141. local buttons = {}
  142. --creates and registers a button
  143. function button(label,text)
  144.     screenHead(label)
  145.     x,y = term.getSize()
  146.     term.write("|")
  147.     term.write(text)
  148.     length = 2+ string.len(text)
  149.     for i=1, (x-length),1 do
  150.         term.write(" ")
  151.     end
  152.     term.write("|")
  153.     x,y = term.getCursorPos()
  154.     buttons[y] = label
  155.     nextLine()
  156. end
  157.  
  158. local SCREEN = "Home"
  159.  
  160. --touch handeling
  161. function touchHandeling(button,x,y)
  162.     xMax,yMax = term.getSize()
  163.     if ((y == yMax) and (x > 2) and (x < 8)) then
  164.         --Home button was pressed
  165.         SCREEN = "Home"
  166.         return
  167.     end
  168.     if (SCREEN == "Home") then
  169.         if (x > 1 and x < xMax) then
  170.             change = buttons[y]
  171.             if (not (change == nil)) then
  172.                 SCREEN = change
  173.                 if (change == "Turtles") then
  174.                     functionals()
  175.                     screenHead(SCREEN)
  176.                 end
  177.             end
  178.         end
  179.     end
  180. end
  181.  
  182. --rednet handeling
  183. local ticksSinceLastPing = 0
  184. function rednetHandeling(sender,message,prot)
  185.     if (message == "pong" and prot == net) then --online checker
  186.         ticksSinceLastPing = 0
  187.         return
  188.     end
  189.     if (SCREEN == "Turtles") then
  190.         if (type(message) == "table" and prot == net) then
  191.             functionals()
  192.             screenHead(SCREEN)
  193.             --what has been sent is a table
  194.             for i,line in pairs(message) do
  195.                 screenHead("Turtle: "..i)
  196.                 term.write(line)
  197.                 nextLine()
  198.             end
  199.         end
  200.         divider()
  201.     end
  202. end
  203.  
  204. function routine()
  205.     functionals()
  206.     if (SCREEN == "Home") then
  207.         button("Turtles","View Turtle Status")
  208.     elseif (SCREEN == "Turtles") then
  209.         rednet.broadcast("Turtlenet querry",net)
  210.     else
  211.         functionals()
  212.         screenHead("No SCREEN")
  213.     end
  214.     if (ticksSinceLastPing > 5) then
  215.         IECNET = false
  216.     else
  217.         IECNET = true
  218.     end
  219.     rednet.broadcast("ping",net) --ping the network to see if it is online
  220. end
  221.  
  222. --actual code
  223.  
  224. iecnetSetup()
  225. functionals()
  226.  
  227. local timer = os.startTimer(1)
  228.  
  229. while true do
  230.     event,a1,a2,a3 = os.pullEvent()
  231.     if (event == "timer" and a1 == timer) then
  232.         ticksSinceLastPing = ticksSinceLastPing +1
  233.         routine()
  234.         timer = os.startTimer(1)
  235.     elseif (event == "mouse_click") then
  236.         touchHandeling(a1,a2,a3)
  237.     elseif (event == "rednet_message") then
  238.         rednetHandeling(a1,a2,a3)
  239.     end
  240. end
Add Comment
Please, Sign In to add comment