The3vilM0nk3y

Turbine Reactor Monitor

May 30th, 2015 (edited)
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.74 KB | None | 0 0
  1. tMon = "1"
  2. rMon = "0"
  3. function clean()
  4.   term.setBackgroundColor(colors.black)
  5.   term.clear()
  6.   term.setCursorPos(1,1)
  7. end
  8. function cleanMon()
  9.   m.setBackgroundColor(colors.black)
  10.   m.clear()
  11.   m.setCursorPos(1,1)
  12. end
  13. function center(text,index,y)
  14.   if index == m then
  15.     screen = m
  16.   elseif index == m2 then
  17.     screen = m2
  18.   else
  19.     screen = tWindow[index].window
  20.   end
  21.   w,h = screen.getSize()
  22.   _text = tostring(text)
  23.   sl = string.len(_text)
  24.   screen.setCursorPos(math.floor(w/2 +.5)-math.floor(sl/2 +.5),y)
  25.   screen.write(_text)
  26. end
  27.  
  28. function displayReactor()
  29.   if r.connected() then
  30.   -- Show Reactor Stats
  31.     m2.clear()
  32.     center("| Reactor Monitor |",m2,1)
  33.     center("-------------------",m2,2)
  34.     if r.active() then
  35.       m2.setBackgroundColor(colors.green)
  36.     else
  37.       m2.setBackgroundColor(colors.red)
  38.     end
  39.     center("POWER",m2,4)
  40.     m2.setBackgroundColor(colors.black)
  41.     center("Fuel Temp: " .. math.floor(r.fuelTemperature()),m2,6)
  42.     center("Case Temp: " .. math.floor(r.casingTemperature()),m2,7)
  43.     center("Fuel Amount: ".. math.floor(r.fuelTank().fuel()).."/"..math.floor(r.fuelTank().capacity()),m2,8)
  44.     center("Steam Created: " .. r.coolantTank().transitionedLastTick().." mB/t",m2,9)
  45.     center("Fuel Rate: ".. r.fuelTank().burnedLastTick() .. " mB/t",m2,10)
  46.     center("Fuel Reactivity: "..  math.floor(r.fuelTank().fuelReactivity()).. "%",m2,11)
  47.     center("Total Power Gen: " .. math.floor(totalPowerGen).. " RF/t",m2,12)
  48.  
  49.     sw,sh = m2.getSize()
  50.     loc = 15
  51.     height = 10
  52.     fbl = sw-8
  53.     water = r.coolantTank().coldFluidAmount()
  54.     waterMax = r.coolantTank().capacity()
  55.     waterBlock = waterMax/height
  56.     numWaterBlocks = math.floor(water/waterBlock)
  57.     steamBroke = r.coolantTank().hotFluidAmount()
  58.     steamMax = waterMax
  59.     steam = steamMax - steamBroke
  60.     steamBlock = steamMax/height
  61.     numSteamBlocks = math.floor(steam/steamBlock)
  62.     fuel = r.fuelTank().fuel()
  63.     fuelMax = r.fuelTank().capacity()
  64.     fuelBlock = fuelMax/(fbl-3)
  65.     numFuelBlocks = math.floor(fuel/fuelBlock)
  66.     -- Draw Boxes around Water and Steam Bar
  67.     m2.setBackgroundColor(colors.lightGray)
  68.     for l=1,height do
  69.       m2.setCursorPos(5,loc-l)
  70.       m2.write(" ")
  71.       m2.setCursorPos(8,loc-l)
  72.       m2.write(" ")
  73.       m2.setCursorPos(sw-5,loc-l)
  74.       m2.write(" ")
  75.       m2.setCursorPos(sw-8,loc-l)
  76.       m2.write(" ")
  77.     end
  78.     m2.setCursorPos(5,loc - height - 1)
  79.     m2.write("    ")
  80.     m2.setCursorPos(sw-8,loc - height - 1)
  81.     m2.write("    ")
  82.     m2.setCursorPos(5,loc)
  83.     m2.write("    ")
  84.     m2.setCursorPos(sw-8,loc)
  85.     m2.write("    ")
  86.     --Fill in water level
  87.     m2.setBackgroundColor(colors.blue)
  88.     for l=1,numWaterBlocks do
  89.       m2.setCursorPos(6,loc-l)
  90.       m2.write("  ")
  91.     end
  92.     -- Fill in Steam Level
  93.     m2.setBackgroundColor(colors.gray)
  94.     for l=1,numSteamBlocks do
  95.       m2.setCursorPos(sw-7,loc-l)
  96.       m2.write("  ")
  97.     end
  98.     -- Create Box around Fuel Bar
  99.       -- top outline
  100.     m2.setBackgroundColor(colors.lightGray)
  101.     m2.setCursorPos(5,loc+2)
  102.     for l=1,fbl-1 do
  103.       m2.write(" ")
  104.     end
  105.     -- bottom outline
  106.     m2.setCursorPos(5,loc + 5)
  107.     for l=1,fbl-1 do
  108.       m2.write(" ")
  109.     end
  110.     -- end caps
  111.     for l=1,2 do
  112.       m2.setCursorPos(5,loc+l+2)
  113.       m2.write(" ")
  114.       m2.setCursorPos(sw-5,loc+l+2)
  115.       m2.write(" ")
  116.     end
  117.     -- Fill in Fuel Bars
  118.     m2.setBackgroundColor(colors.yellow)
  119.     for l=1,numFuelBlocks do
  120.       m2.setCursorPos(5+l,loc+3)
  121.       m2.write(" ")
  122.       m2.setCursorPos(5+l,loc+4)
  123.       m2.write(" ")
  124.     end
  125.     m2.setBackgroundColor(colors.black)
  126.     -- Draw Control Rod Buttons
  127.     center("Control Rod Level = " .. controlLevel,m2,sh-1)
  128.     m2.setBackgroundColor(colors.red)
  129.     m2.setCursorPos(sw/2-15,sh)
  130.     m2.write(" -1 ")
  131.     m2.setCursorPos(sw/2-8,sh)
  132.     m2.write(" -10 ")
  133.     m2.setCursorPos((sw/2-1),sh)
  134.     if r.active() then
  135.       m2.setBackgroundColor(colors.green)
  136.     else
  137.       m2.setBackgroundColor(colors.red)
  138.     end
  139.     m2.write("POWER")
  140.     m2.setBackgroundColor(colors.green)
  141.     m2.setCursorPos(sw/2+6,sh)
  142.     m2.write(" +10 ")
  143.     m2.setCursorPos(sw/2+13,sh)
  144.     m2.write(" +1 ")
  145.     m2.setBackgroundColor(colors.black)
  146.   end
  147. end
  148. function checkValue()
  149.   if controlLevel > 100 then
  150.     controlLevel = 100
  151.   elseif controlLevel < 0 then
  152.     controlLevel = 0
  153.   end
  154. end
  155. clean()
  156. peripherals = peripheral.getNames()
  157. t = {}
  158. print("Loading Peripherals..")
  159. for i=1,#peripherals do
  160.   --print(peripherals[i])
  161.   type = peripheral.getType(peripherals[i])
  162.   if type == "BiggerReactors_Turbine" then
  163.     table.insert(t,peripheral.wrap(peripherals[i]))
  164.     print("Adding new turbine to index " .. tostring(#t))
  165.   elseif type ==  "BiggerReactors_Reactor" then
  166.     if not r then
  167.       r = peripheral.wrap(peripherals[i])
  168.       print("Reactor Has Been Connected")
  169.       if r.active() then
  170.         rs.setBundledOutput("bottom",colors.white)
  171.       else
  172.         rs.setBundledOutput("bottom",0)
  173.       end
  174.     end
  175.   end
  176. end
  177. print("Defining Monitors")
  178. -- Turbine Monitor
  179. m = peripheral.wrap("monitor_"..tMon)
  180. -- Reactor Monitor
  181. m2 = peripheral.wrap("monitor_" .. rMon)
  182. m.setTextScale(.5)
  183. m2.setTextScale(.5)
  184. if not r then
  185.   error("No Reactor Found")
  186. end
  187. if not m then
  188.   error("No Monitor Found")
  189. end
  190. if not m2 then
  191.   error("No Secondary Monitor Found")
  192. end
  193. cleanMon()
  194.  
  195. --cpu = term.redirect(m)
  196.  
  197. -- Prepare Windows for Turbines
  198. tWindow = {}
  199. width = 14
  200. height = 5
  201. print("Creating Turbine Windows")
  202. for i=1,#t do
  203.   if i <= 5 then
  204.     y = 4
  205.     xsub = 0
  206.   elseif i <= 10 then
  207.     y = 4 + (1 + height)
  208.     xsub = 5
  209.   elseif i <= 15 then
  210.     y = 4 + (1 + height) * 2
  211.     xsub = 10
  212.   elseif i <= 20 then
  213.     y = 4 + (1 + height) * 3
  214.     xsub = 15
  215.   elseif i <= 25 then
  216.     y = 4 + (1 + height) * 4
  217.     xsub = 20
  218.   else
  219.     y = 4 + (1 + height) * 5
  220.     xsub = 25
  221.   end
  222.   x = 5 + (width * ((i-1) - xsub) ) + ((i-1)-xsub)
  223.   --if i > 4 and i < 9 then
  224.   --  x = x - i
  225.   --end
  226.  
  227.   -- New Method
  228.   tWindow[i] = {
  229.     window = window.create(m,x,y,width,height,true),
  230.     xPos = x,
  231.     yPos = y,
  232.   }
  233.  
  234.   --Old Method
  235.   -- table.insert(tWindow,window.create(m,x,y,17,height,true))
  236.  
  237. end
  238. totalPowerGen = 0
  239. controlLevel = r.getControlRod(0).level()
  240.  
  241. -- Show Turbine Stats
  242. print("Loading Complete.")
  243. while true do
  244.   center("| Turbine Monitor |",m,1)
  245.   center("-------------------",m,2)
  246.   x,y = 1,4
  247.   totalPowerGen = 0
  248.   for i=1,#t do
  249.     if t[i].connected() then
  250.       tWindow[i].window.clear()
  251.       center(i,i,1)
  252.       if t[i].active() then
  253.         tWindow[i].window.setBackgroundColor(colors.green)
  254.       else
  255.         tWindow[i].window.setBackgroundColor(colors.red)
  256.       end
  257.       center("POWER",i,2)
  258.       tWindow[i].window.setBackgroundColor(colors.gray)
  259.       center(math.floor(t[i].battery().stored()).. "RF",i,3)
  260.       center(math.floor(t[i].rotor().RPM()).." RPM",i,4)
  261.       center(math.floor(t[i].battery().producedLastTick()) .. "RF/T",i,5)
  262.       tWindow[i].window.setBackgroundColor(colors.black)
  263.       totalPowerGen = totalPowerGen + t[i].battery().producedLastTick()
  264.     end
  265.   end
  266.   displayReactor()
  267.   timer = os.startTimer(.5)
  268.   while true do
  269.     event,side,x,y = os.pullEvent()
  270.     if event == "timer" then
  271.       break
  272.     elseif event == "monitor_touch" then
  273.       -- check coords for button push
  274.       if side == "monitor_" .. rMon then
  275.         local w,h = m2.getSize()
  276.         powerX = (w/2)-(5/2)
  277.         if x >= powerX and x <= powerX + 5 and y == 4 then
  278.           if r.active() then
  279.             -- disable input of yellorium
  280.             rs.setBundledOutput("bottom",0)
  281.             -- disable reactor
  282.             r.setActive(false)
  283.             -- dump fuel
  284.             repeat
  285.               r.doEjectFuel()
  286.               sleep(1)
  287.               displayReactor()
  288.             until r.fuelTank().fuel() < 1000
  289.           else
  290.             -- enable input of yellorium
  291.             rs.setBundledOutput("bottom",colors.white)
  292.             -- enable reactor
  293.             r.setActive(true)
  294.           end
  295.  
  296.         elseif y == h then
  297.           if x >= w/2-15 and x <= w/2-12 then
  298.             controlLevel = controlLevel - 1
  299.             checkValue()
  300.             r.setAllControlRodLevels(controlLevel)
  301.           elseif x >= w/2-8 and x <= w/2-4 then
  302.             controlLevel = controlLevel - 10
  303.             checkValue()
  304.             r.setAllControlRodLevels(controlLevel)
  305.           elseif x >= w/2+6 and x <= w/2+10 then
  306.             controlLevel = controlLevel + 10
  307.             checkValue()
  308.             r.setAllControlRodLevels(controlLevel)
  309.           elseif x >= w/2+13 and x <= w/2+16 then
  310.             controlLevel = controlLevel + 1
  311.             checkValue()
  312.             r.setAllControlRodLevels(controlLevel)
  313.           elseif x >= w/2-1 and x <= w/2+3 then
  314.             if r.active() then
  315.               r.setActive(false)
  316.             else
  317.               r.setActive(true)
  318.             end
  319.           end
  320.         end
  321.         break
  322.       elseif side == "monitor_" .. tMon then
  323.         print("Got Turbine Screen Click at x:" .. x .. " y: " .. y)
  324.         for i=1,#tWindow do
  325.           local ww,_ = tWindow[i].window.getSize()
  326.           local cOffset = math.floor(ww/2 + .5) - 5
  327.           local winX = tWindow[i].xPos
  328.           local winY = tWindow[i].yPos
  329.           print("Window " .. i .. " x:" .. winX + cOffset .. " y: " .. winY + 1 .. " Click x:" .. x .. " y: " .. y .. " Width:" .. ww .. " Offset:" .. cOffset)
  330.           if x >= winX + cOffset and x <= winX + cOffset + 5 and y == winY + 1 then
  331.             if t[i].getActive() then
  332.               t[i].setActive(false)
  333.             else
  334.               t[i].setActive(true)
  335.             end
  336.             break
  337.           end
  338.         end
  339.       end
  340.     end
  341.   end
  342. end
  343.  
Advertisement
Add Comment
Please, Sign In to add comment