Advertisement
Forecaster

infoProgram

Feb 12th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.40 KB | None | 0 0
  1. local mon = peripheral.wrap("left")
  2. local monwidth, monheight = mon.getSize()
  3.  
  4. local failsafeDefault = 20
  5. local failsafe = failsafeDefault
  6. local screenDebug = 0
  7.  
  8. buttonTable = {}
  9.  
  10. mon.setBackgroundColor(colors.black)
  11. mon.clear()
  12.  
  13. if screenDebug ~= 0 then
  14.   --This will make the background a checkerboard. For debugging button positions.
  15.   for a = 1, monheight do
  16.     for b = 1, monwidth do
  17.       if a%2 == 0 then
  18.         if b%2 ~= 0 then
  19.           mon.setBackgroundColor(colors.black)
  20.         else
  21.           mon.setBackgroundColor(colors.white)
  22.         end
  23.       else
  24.         if b%2 ~= 0 then
  25.           mon.setBackgroundColor(colors.white)
  26.         else
  27.           mon.setBackgroundColor(colors.black)
  28.         end
  29.       end
  30.       mon.setCursorPos(b,a)
  31.       mon.write(" ")
  32.     end
  33.   end
  34. end
  35.  
  36. function newButton(name, xmin, xmax, ymin, ymax, offfunct, onfunct, offcolor, oncolor, offlabel, onlabel, center, hidden, overwrite)
  37.   buttonTable[name] = {}
  38.   buttonTable[name]["xmin"] = xmin
  39.   buttonTable[name]["xmax"] = xmax
  40.   buttonTable[name]["ymin"] = ymin
  41.   buttonTable[name]["ymax"] = ymax
  42.   buttonTable[name]["offfunct"] = offfunct
  43.   buttonTable[name]["onfunct"] = onfunct
  44.   buttonTable[name]["offcolor"] = offcolor
  45.   buttonTable[name]["oncolor"] = oncolor
  46.   buttonTable[name]["offlabel"] = offlabel
  47.   buttonTable[name]["onlabel"] = onlabel
  48.   buttonTable[name]["center"] = center
  49.   buttonTable[name]["hidden"] = hidden
  50.   buttonTable[name]["overwrite"] = overwrite
  51.  
  52.   buttonTable[name]["width"] = (xmax - xmin)
  53.   buttonTable[name]["height"] = (ymax - ymin)
  54.   buttonTable[name]["state"] = false
  55.   buttonTable[name]["monmargin"] = (math.floor((monwidth - buttonTable[name]["width"]) / 2) - 1)
  56.   buttonTable[name]["offlength"] = string.len(offlabel)
  57.   buttonTable[name]["offmargin"] = math.floor((buttonTable[name]["width"] - buttonTable[name]["offlength"]) / 2)
  58.   buttonTable[name]["onlength"] = string.len(onlabel)
  59.   buttonTable[name]["onmargin"] = math.floor((buttonTable[name]["width"] - buttonTable[name]["onlength"]) / 2)
  60.  
  61.   drawButton(name, buttonTable[name]["state"], false)
  62. end
  63.  
  64. function drawButton(name, state, overwrite)
  65.   --Clear button location
  66.   if (buttonTable[name]["overwrite"] == true) or (overwrite == true) then
  67.     for y = 0,(buttonTable[name]["height"]) do
  68.       for x = 0,(buttonTable[name]["width"]) do
  69.         if buttonTable[name]["center"] == true then
  70.           mon.setCursorPos((buttonTable[name]["xmin"] + x + buttonTable[name]["monmargin"]),(buttonTable[name]["ymin"] + y))
  71.           mon.write(" ")
  72.         else
  73.           mon.setCursorPos((buttonTable[name]["xmin"] + x),(buttonTable[name]["ymin"] + y))
  74.           mon.write(" ")
  75.         end
  76.       end
  77.     end
  78.   end
  79.  
  80.   if buttonTable[name]["hidden"] == false then
  81.     --Set button background
  82.     if state == false then
  83.       mon.setBackgroundColor(buttonTable[name]["offcolor"])
  84.     else
  85.       mon.setBackgroundColor(buttonTable[name]["oncolor"])
  86.     end
  87.    
  88.     --Set currentLabel
  89.     if state == true then
  90.       currentLabel = buttonTable[name]["onlabel"]
  91.     else
  92.       currentLabel = buttonTable[name]["offlabel"]
  93.     end
  94.    
  95.     labelLen = string.len(currentLabel)
  96.    
  97.     textVertMargin = math.ceil(buttonTable[name]["height"] / 2)
  98.     textHoriMargin = math.ceil((buttonTable[name]["width"] - labelLen) / 2)
  99.  
  100.  
  101.     --Draw button
  102.     for y = 0,(buttonTable[name]["height"]) do
  103.       for x = 0,(buttonTable[name]["width"]) do
  104.         if buttonTable[name]["center"] == true then
  105.           mon.setCursorPos((buttonTable[name]["xmin"] + x + buttonTable[name]["monmargin"]),(buttonTable[name]["ymin"] + y))
  106.           mon.write(" ")
  107.         else
  108.           mon.setCursorPos((buttonTable[name]["xmin"] + x),(buttonTable[name]["ymin"] + y))
  109.           mon.write(" ")
  110.         end
  111.       end
  112.     end
  113.    
  114.     --Draw button text
  115.     for y = 0,(buttonTable[name]["height"]) do
  116.       for x = 0,(buttonTable[name]["width"]) do
  117.         if buttonTable[name]["center"] == true then
  118.           mon.setCursorPos((buttonTable[name]["xmin"] + x + buttonTable[name]["monmargin"]),(buttonTable[name]["ymin"] + y))
  119.           if (y == textVertMargin) and (x == textHoriMargin) then
  120.             mon.write(currentLabel)
  121.           end
  122.         else
  123.           mon.setCursorPos((buttonTable[name]["xmin"] + x),(buttonTable[name]["ymin"] + y))
  124.           if (y == textVertMargin) and (x == textHoriMargin) then
  125.             mon.write(currentLabel)
  126.           end
  127.         end
  128.       end
  129.     end
  130.    
  131.     --Reset background color
  132.     mon.setBackgroundColor(colors.black)
  133.   else
  134.     print("Skipped hidden button: " .. name)
  135.   end
  136. end
  137.  
  138. function toggleButton(name)
  139.   if buttonTable[name]["state"] == false then
  140.     buttonTable[name]["state"] = true
  141.     drawButton(name, true, false)
  142.   else
  143.     buttonTable[name]["state"] = false
  144.     drawButton(name, false, false)
  145.   end
  146. end
  147.  
  148. function toggleHidden(name)
  149.   if buttonTable[name]["hidden"] == false then
  150.     buttonTable[name]["hidden"] = true
  151.     drawButton(name, false, true)
  152.   else
  153.     buttonTable[name]["hidden"] = false
  154.     drawButton(name, false, true)
  155.   end
  156. end
  157.  
  158. --User functions:
  159.  
  160. function enableReactor(name)
  161.   if rs.testBundledInput("bottom",colors.brown) == false then
  162.     print("Toggling memory cell to on")
  163.     rs.setBundledOutput("bottom",colors.lime)
  164.     sleep(0.1)
  165.     rs.setBundledOutput("bottom",0)
  166.   end
  167.   rs.setBundledOutput("bottom",colors.white)
  168. end
  169.  
  170. function disableReactor(name)
  171.   if rs.testBundledInput("bottom",colors.brown) == true then
  172.     print("Toggling memory cell to off")
  173.     rs.setBundledOutput("bottom",colors.lime)
  174.     sleep(0.1)
  175.     rs.setBundledOutput("bottom",0)
  176.   end
  177.   rs.setBundledOutput("bottom", 0)
  178. end
  179.  
  180. function failsafeReset()
  181.   rs.setBundledOutput("bottom",0)
  182.   toggleHidden("reset")
  183.   toggleHidden("main")
  184. end
  185.  
  186. --User functions end.
  187.  
  188. --Startup code:
  189.  
  190. --newButton(str name, int xmin, int xmax, int ymin, int ymax, function or "none", function or "none",
  191. --          str offcolor, str oncolor, str offlabel, str onlabel, bool centered, bool hidden, bool overwrite)
  192.  
  193. newButton("main", 2, 18, 2, 4, disableReactor, enableReactor, 16384, 8192, "Offline", "Online", true, false, true)
  194. newButton("reset", 2, 18, 2, 4, "none", failsafeReset, 8, 16384, "Reset", "Reset", true, true, false)
  195. newButton("hold", 2, 18, 6, 8, "none", "none", 16, 16, "Hold", "Hold", true, true, false)
  196. newButton("cool", 2, 18, 6, 8, "none", "none", 8, 16384, "Cooldown", "Cooldown", true, true, false)
  197.  
  198. print("Reactor control online!")
  199.  
  200. if rs.testBundledInput("bottom",colors.brown) == true then
  201.   print("Memory cell states reactor should be online! Turning on reactor!")
  202.   rs.setBundledOutput("bottom",colors.white)
  203.   toggleButton("main")
  204. else
  205.   print("Memory cell states reactor should be offline! Ensuring reactor is offline!")
  206.   rs.setBundledOutput("bottom",0)
  207. end
  208.  
  209. --Startup code end.
  210.  
  211. while true do
  212.   if failsafe ~= 0 then
  213.     failsafe = failsafe - 1
  214.     term.setCursorPos(1,17)
  215.     term.clearLine()
  216.     print(failsafe)
  217.    
  218.     os.startTimer(5)
  219.     local event, p1, p2, p3, p4 = os.pullEvent()
  220.    
  221.     if event == "monitor_touch" then
  222.       print("Touch event triggered")
  223.       for name,value in pairs(buttonTable) do
  224.         if buttonTable[name]["hidden"] == false then
  225.           if buttonTable[name]["center"] == false then
  226.             if p2 >= buttonTable[name]["xmin"] then
  227.               if p2 <= buttonTable[name]["xmax"] then
  228.                 if p3 >= buttonTable[name]["ymin"] then
  229.                   if p3 <= buttonTable[name]["ymax"] then
  230.                     if buttonTable[name]["state"] == true then
  231.                       if buttonTable[name]["offfunct"] ~= "none" then
  232.                         buttonTable[name]["offfunct"](name)
  233.                       end
  234.                     elseif buttonTable[name]["state"] == false then
  235.                       if buttonTable[name]["onfunct"] ~= "none" then
  236.                         buttonTable[name]["onfunct"](name)
  237.                       end
  238.                     end
  239.                     toggleButton(name)
  240.                   end
  241.                 end
  242.               end
  243.             end
  244.           else
  245.             if p2 >= (buttonTable[name]["xmin"] + buttonTable[name]["monmargin"]) then
  246.               if p2 <= (buttonTable[name]["xmax"] + buttonTable[name]["monmargin"]) then
  247.                 if p3 >= buttonTable[name]["ymin"] then
  248.                   if p3 <= buttonTable[name]["ymax"] then
  249.                     if buttonTable[name]["state"] == true then
  250.                       if buttonTable[name]["offfunct"] ~= "none" then
  251.                         buttonTable[name]["offfunct"](name)
  252.                       end
  253.                     elseif buttonTable[name]["state"] == false then
  254.                       if buttonTable[name]["onfunct"] ~= "none" then
  255.                         buttonTable[name]["onfunct"](name)
  256.                       end
  257.                     end
  258.                     toggleButton(name)
  259.                   end
  260.                 end
  261.               end
  262.             end
  263.           end
  264.         end
  265.       end
  266.     elseif event == "redstone" then
  267.       if rs.testBundledInput("bottom", colors.blue) == true then
  268.         print("Energy storage full! Halting reactor!")
  269.         rs.setBundledOutput("bottom",0)
  270.         toggleHidden("hold")
  271.        
  272.         while rs.testBundledInput("bottom", colors.blue) == true do
  273.           sleep(5)
  274.         end
  275.        
  276.         toggleHidden("hold")
  277.         rs.setBundledOutput("bottom",colors.white)
  278.       elseif rs.getInput("top") == true then
  279.         if rs.testBundledInput("bottom", colors.brown) then
  280.           print("Failsafe triggered! 5 Minute shutdown")
  281.           disableReactor()
  282.           toggleHidden("cool")
  283.           term.scroll(1)
  284.           for count = 1, 500 do
  285.             term.setCursorPos(1,18)
  286.             term.clearLine()
  287.             print(500 - count)
  288.             sleep(1)
  289.           end
  290.           print("Resuming reactor run!")
  291.           enableReactor()
  292.           toggleHidden("cool")
  293.          
  294.         end
  295.       end
  296.     elseif event == "lan_message" then
  297.       if p4 == "reset" then
  298.         failsafe = failsafeDefault
  299.       end
  300.     end
  301.  
  302.   else
  303.     print("Failsafe triggered due to contact with reactor monitor lost!")
  304.     print("Toggling memory cell to off")
  305.     rs.setBundledOutput("bottom",colors.lime)
  306.     sleep(0.1)
  307.     print("Shutting down reactor!")
  308.     rs.setBundledOutput("bottom",colors.yellow)
  309.     error("Failsafe!")
  310.   end
  311. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement