Advertisement
JereTheJuggler

bigReactorController.lua

Nov 26th, 2021
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.52 KB | None | 0 0
  1. shell.run("bg dispLogo")
  2. sleep(5)
  3.  
  4. mon = peripheral.find("monitor")
  5. reactor = peripheral.find("BigReactors-Reactor")
  6.  
  7. mon.setBackgroundColor(colors.black)
  8. mon.clear()
  9. mon.setCursorPos(1,1)
  10.  
  11. autoEject = false
  12. autoEjectBounds = {0,0,0,0}
  13. onOffBounds = {0,0,0,0}
  14. ejectBounds = {0,0,0,0}
  15.  
  16. x,y = -5,-5
  17. width,height = 1,1
  18. rodHeight = reactor.getControlRodLevel(1)
  19.  
  20. function isInBounds (xPos,yPos,bounds)
  21.   if xPos >= bounds[1] and
  22.      yPos >= bounds[2] and
  23.      xPos <= bounds[3] and
  24.      yPos <= bounds[4] then
  25.     return true
  26.   else
  27.     return false
  28.   end
  29. end
  30.  
  31. function writeCentered (text,line,xMin,xMax)
  32.   if xMin == nil then
  33.     xMin = 0
  34.   end
  35.   if xMax == nil then
  36.     xMax = width
  37.   end
  38.   deltaX = xMax-xMin
  39.   mon.setCursorPos(xMin+math.floor((deltaX/2)-(string.len(text)/2))+1,line)
  40.   mon.write(text)
  41. end
  42.  
  43. function writeRightJustified (text,line,gap)
  44.   if gap == nil then
  45.     gap = 0
  46.   end
  47.   mon.setCursorPos(width-string.len(text)+1-gap,line)
  48.   mon.write(text)
  49. end
  50.  
  51. function fillRect (xMin,yMin,dx,dy)
  52.   line = ""
  53.   for cursor=1,dx do
  54.     line = line.." "
  55.   end
  56.   for cursor=1,dy do
  57.     mon.setCursorPos(xMin,yMin+cursor-1)
  58.     mon.write(line)
  59.   end
  60.   return {xMin,yMin,xMin+dx-1,yMin+dy-1}
  61. end
  62.  
  63. function bgc (color)
  64.   mon.setBackgroundColor(color)
  65. end
  66.  
  67. function tc (color)
  68.   mon.setTextColor(color)
  69. end
  70.  
  71. function writeLeft (text,line,gap)
  72.   if gap == nil then
  73.     gap = 0
  74.   end
  75.   mon.setCursorPos(1+gap,line)
  76.   mon.write(text)
  77. end
  78.  
  79. function getClick ()
  80.   while true do
  81.     event,arg1,xPos,yPos = os.pullEvent()
  82.     if event == "monitor_touch" then
  83.       return xPos,yPos
  84.     elseif event == "key" and arg1 == keys.x then
  85.       return -1,-1
  86.     end
  87.   end
  88. end
  89.  
  90. function drawGui ()
  91.   rodHeight = reactor.getControlRodLevel(1)
  92.   width,height = mon.getSize()
  93.   bgc(colors.black)
  94.   mon.clear()
  95.   bgc(colors.white)
  96.   fillRect(1,1,width-7,2)
  97.   tc(colors.black)
  98.   writeRightJustified(" Fuel ",1)
  99.   writeRightJustified(" Rods ",2)
  100.   writeCentered("Reactor Information",1,1,width-7)
  101.   bgc(colors.pink)
  102.   tc(colors.white)
  103.   writeRightJustified("-1 ",height-1,3)
  104.   bgc(colors.red)
  105.   writeRightJustified("-10",height-1)
  106.   bgc(colors.lime)
  107.   writeRightJustified("+1 ",height,3)
  108.   bgc(colors.green)
  109.   writeRightJustified("+10",height)
  110.   if reactor.getFuelAmount()+reactor.getWasteAmount() ~= reactor.getFuelAmountMax() then
  111.     bgc(colors.lightGray)
  112.     for yPos=4,height-4 do
  113.       writeRightJustified("    ",yPos,1)
  114.     end
  115.   end
  116.   bgc(colors.yellow)
  117.   if reactor.getFuelAmount() > 0 then
  118.     yPos = 4
  119.     startY = 4
  120.     while (yPos-startY)/(height-8) <= (reactor.getFuelAmount()+reactor.getWasteAmount())/reactor.getFuelAmountMax() do
  121.       writeRightJustified("    ",height-yPos,1)
  122.       yPos = yPos + 1
  123.     end
  124.   end
  125.   bgc(colors.cyan)
  126.   if reactor.getWasteAmount() > 1000 then
  127.     yPos = 4
  128.     startY = 4
  129.     while (yPos-startY)/(height-8) <= reactor.getWasteAmount()/reactor.getFuelAmountMax() do
  130.       writeRightJustified("    ",height-yPos,1)
  131.       yPos = yPos + 1
  132.     end
  133.   end
  134.   yPos = height-4
  135.   bgc(colors.black)
  136.   writeRightJustified(tostring(rodHeight).."%",height-2,1)
  137.   bgc(colors.gray)
  138.   startY = 4
  139.   yPos = 4
  140.   if rodHeight > 0 then
  141.     while (yPos-startY)/(height-8) <= rodHeight/100 do
  142.       writeRightJustified("  ",yPos,2)
  143.       yPos = yPos+1
  144.     end
  145.   end
  146.   bgc(colors.black)
  147.   tc(colors.white)
  148.   for yPos=1,height do
  149.     mon.setCursorPos(width-6,yPos)
  150.     mon.write("|")
  151.   end
  152.   --display reactor stats--
  153.   if reactor.getActive() then
  154.     tc(colors.red)
  155.     writeLeft("Energy",4,1)
  156.     tc(colors.white)
  157.     writeLeft("Production: ",5,3)
  158.     writeRightJustified(tostring(math.floor(
  159.       reactor.getEnergyProducedLastTick())).." rf/t",
  160.       5,8)
  161.     writeLeft("Stored:",6,3)
  162.     writeRightJustified(tostring(math.floor(
  163.       reactor.getEnergyStored())).." rf",
  164.       6,8)
  165.     tc(colors.red)
  166.     writeLeft("Fuel Amount",8,1)
  167.     tc(colors.white)
  168.     writeLeft("Capacity:",9,3)
  169.     writeRightJustified(tostring(reactor.getFuelAmountMax()).." mb",
  170.       9,8)
  171.     writeLeft("Fuel:",10,3)
  172.     writeRightJustified(tostring(reactor.getFuelAmount()).." mb",
  173.       10,8)
  174.     writeLeft("Waste:",11,3)
  175.     writeRightJustified(tostring(reactor.getWasteAmount()).." mb",
  176.       11,8)
  177.     tc(colors.red)
  178.     writeLeft("Efficiency",13,1)
  179.     tc(colors.white)
  180.     writeLeft("Core Heat:",14,3)
  181.     writeRightJustified(tostring(math.floor(
  182.       reactor.getFuelTemperature())).." C",
  183.       14,8)
  184.     writeLeft("Casing Heat:",15,3)
  185.     writeRightJustified(tostring(math.floor(
  186.       reactor.getCasingTemperature())).." C",
  187.       15,8)
  188.     writeLeft("Fuel Use:",16,3)
  189.     writeRightJustified(tostring(math.floor(1000*
  190.       reactor.getFuelConsumedLastTick())/1000).." mb/t",
  191.       16,8)
  192.     writeLeft("Reactivity:",17,3)
  193.     writeRightJustified(tostring(math.floor((
  194.       reactor.getFuelReactivity()))).."%",
  195.       17,8)
  196.   end
  197.   --bottom buttons--
  198.   writeLeft("POWER!",height-4,1)
  199.   onOff = " "
  200.   if reactor.getActive() then
  201.     bgc(colors.green)
  202.     onOff = "ON"
  203.   else
  204.     bgc(colors.red)
  205.     onOff = "OFF"
  206.   end
  207.   onOffBounds = fillRect(2,height-3,6,3)
  208.   writeCentered(onOff,onOffBounds[2]+1,onOffBounds[1],onOffBounds[3])
  209.   bgc(colors.black)
  210.   writeLeft("Auto-Eject",height-4,onOffBounds[3]+1)
  211.   if autoEject then
  212.     bgc(colors.green)
  213.     onOff = "ON"
  214.   else
  215.     bgc(colors.red)
  216.     onOff = "OFF"
  217.   end
  218.   autoEjectBounds = fillRect(onOffBounds[3]+2,height-3,10,3)
  219.   writeCentered(onOff,autoEjectBounds[2]+1,autoEjectBounds[1],autoEjectBounds[3])
  220.   if not autoEject then
  221.     bgc(colors.black)
  222.     writeLeft("Eject Now",height-4,autoEjectBounds[3]+1)
  223.     bgc(colors.red)
  224.     ejectBounds = fillRect(autoEjectBounds[3]+2,height-3,9,3)
  225.   end
  226. end
  227.  
  228. function main ()
  229.   x,y = -5,-5
  230.   while true do
  231.     drawGui()
  232.     sleep(.25)
  233.     if x ~= -5 then
  234.       if x > width or y > height then
  235.         x = -5
  236.         y = -5
  237.       elseif x == -1 then
  238.         return
  239.       elseif isInBounds(x,y,autoEjectBounds) then
  240.         if autoEject then
  241.           autoEject = false
  242.         else
  243.           autoEject = true
  244.         end
  245.       elseif not autoEject and isInBounds(x,y,ejectBounds) then
  246.         reactor.doEjectWaste()
  247.       elseif isInBounds(x,y,onOffBounds) then
  248.         if reactor.getActive() then
  249.           reactor.setActive(false)
  250.         else
  251.           reactor.setActive(true)
  252.         end
  253.       else
  254.         if x >= width-5 and y >= height-1 then
  255.           if y == height-1 and rodHeight > 0 then
  256.             if x >= width-2 then
  257.               rodHeight = rodHeight - 10
  258.               if rodHeight < 0 then
  259.                 rodHeight = 0
  260.               end
  261.             else
  262.               rodHeight = rodHeight - 1
  263.             end
  264.             reactor.setAllControlRodLevels(rodHeight)
  265.           elseif y == height and rodHeight < 100 then
  266.             if x >= width-2 then
  267.               rodHeight = rodHeight + 10
  268.               if rodHeight > 100 then
  269.                 rodHeight = 100
  270.               end
  271.             else
  272.               rodHeight = rodHeight + 1
  273.             end
  274.             reactor.setAllControlRodLevels(rodHeight)
  275.           end
  276.         end
  277.       end
  278.       x,y = -5,-5
  279.     end
  280.     if autoEject then
  281.       reactor.doEjectWaste()
  282.     end
  283.   end
  284. end
  285.  
  286. function getInput ()
  287.   while true do
  288.     x,y = getClick()
  289.     if x == -1 then
  290.       return
  291.     end  
  292.   end
  293. end
  294.  
  295. term.clear()
  296. term.setCursorPos(1,1)
  297. term.write("press x to terminate program")
  298. parallel.waitForAll(main,getInput)
  299.  
  300. term.clear()
  301. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement