TrovadorUrbano

[MC 1.6.4][CC 1.58][OpenP 0.3.3] IC2 Reactor Monitor

May 1st, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. local fuelAlert = {}
  2. local z = textutils.serialize
  3. local Reactors, Sides
  4. --local monitor = peripheral.wrap("left")
  5. --monitor.setTextScale(1)
  6.  
  7. --[[mon = getPeripheral("monitor")
  8. --print(z(monitor))
  9. local term_back = term
  10. if mon then
  11.     term = mon
  12. end]]
  13. local monx, mony = term.getSize()
  14.  
  15. -- Helper API for MiscPeripherals integrated programs
  16.  
  17. -- Gets all peripherals of the specified type
  18. function getPeripherals(typ, amount)
  19.   peripherals = {}
  20.   sides = {}
  21.   if amount == nil then amount = 1000000 end
  22.  
  23.   count = 0
  24.   for _, side in pairs(rs.getSides()) do
  25.     peripheralType = peripheral.getType(side)
  26.     if peripheralType == typ then
  27.       -- Directly attached
  28.       table.insert(peripherals, peripheral.wrap(side))
  29.       table.insert(sides, side)
  30.       count = count + 1
  31.       if count >= amount then
  32.         return peripherals, sides
  33.       end
  34.     elseif peripheralType == "modem" and not peripheral.call(side, "isWireless") then
  35.       -- Look for remote peripherals
  36.       modem = peripheral.wrap(side)
  37.       for _, name in pairs(modem.getNamesRemote()) do
  38.         if modem.getTypeRemote(name) == typ then
  39.           -- Found one
  40.           table.insert(peripherals, wrapRemote(modem, name))
  41.           table.insert(sides, side..":"..name)
  42.           count = count + 1
  43.           if count >= amount then
  44.             return peripherals, sides
  45.           end
  46.         end
  47.       end
  48.     end
  49.   end
  50.  
  51.   return peripherals, sides
  52. end
  53.  
  54. function getPeripheral(typ)
  55.   resultPeripheral, resultSide = getPeripherals(typ, 1)
  56.   return resultPeripheral[1], resultSide[1]
  57. end
  58. -- Modified version of peripheral.wrap for remote peripherals
  59. function wrapRemote(modem, name)
  60.   result = {}
  61.     methods = modem.getMethodsRemote(name)
  62.   for _, method in pairs(methods) do
  63.     result[method] = function(...)
  64.       return modem.callRemote(name, method, ...)
  65.     end
  66.   end
  67.   return result
  68. end
  69.  
  70. --- MiscPeripherals apis I DONT OWN THOSE CODES
  71.  
  72. -- Custom function to draw a status bar on the screen
  73. function drawStatusBar(title,x,y,cval,maxval,color1,color2,size,offset)
  74.     if size==nil then
  75.         size = 10
  76.     end
  77.     term.setCursorPos(x,y)
  78.     term.setBackgroundColor(colors.black)
  79. --  print(cval)
  80. --  sleep(5)
  81.     local s = 0
  82.     if offset then
  83.         s = offset - string.len(title)
  84.         if s < 0 then s = 0 end
  85.     end
  86.     term.write(title)
  87.     for i=1,s do term.write(" ") end
  88.     local fill = math.floor(cval*size/maxval)
  89.    
  90.     term.setBackgroundColor(color1)
  91.     for i=1,fill do
  92.         term.write(" ")
  93.     end
  94.     term.setBackgroundColor(color2)
  95.     for i=1,(size-fill) do
  96.         term.write(" ")
  97.     end
  98.     term.setBackgroundColor(colors.black)
  99.     -- Salto de Linea ##########
  100. end
  101.  
  102. function printScr(n,reactor,side)
  103.     fuel = 0
  104.     items = reactor.getAllStacks()
  105.     nItems = 0
  106.     for islot,i in pairs(items) do
  107.         nItems = nItems+1
  108.         term.setCursorPos(1,mony-5)
  109.         if string.find(i.name,"Fuel Rod") then
  110.             fuel = fuel+ (10000-i.dmg)
  111.             if i.dmg >= 9700 then
  112.                 table.insert(fuelAlert,{item=i,reactor=n,slot=islot})
  113.             end
  114.         end
  115.     end
  116.     fuel = fuel/nItems
  117.     term.clear()
  118.     term.setCursorPos(1,1)
  119.     term.write("Reactor #"..n.." on "..side.." (items: "..nItems..")")
  120.     drawStatusBar("Heat",2,3,reactor.getHeat(),reactor.getMaxHeat(),colors.red,colors.green,30,12)
  121.     drawStatusBar("Fuel (avg)",2,5,fuel,10000,colors.green,colors.red,30,12)
  122.     -- Salto de Linea ##########
  123.     for N,i in pairs(fuelAlert) do
  124.         term.setCursorPos(1,mony-1)
  125.         term.setTextColor(colors.red)
  126.         term.write(i.item.name.." [slot("..i.slot..")] on Reactor #"..i.reactor.." is low.")
  127.         term.setTextColor(colors.yellow)
  128.         fuelAlert = {}
  129.     end
  130. end
  131.  
  132. ---------------------------------
  133. ---------------------------------
  134. ---------------------------------
  135.  
  136. term.clear()
  137. term.setCursorPos(1,1)
  138. term.setTextColor(colors.yellow)
  139.  
  140. term.write("Starting...")
  141. --sleep(1)
  142. os.startTimer(0.1)
  143.  
  144. local fuel = 0
  145. local counter = 1
  146. local static = false
  147. local Reactors,Sides
  148. --drawSquare(indicator((a.dmg-10000)*(-1)),"R")
  149. while true do
  150.     event,key,a = os.pullEvent()   
  151.     if event == "timer" then
  152.         Reactors, Sides = getPeripherals("nuclear_reactor")
  153.         printScr(counter,Reactors[counter],Sides[counter])
  154.         if not static then
  155.             counter = counter+1
  156.         end
  157.         if counter > #Reactors then
  158.             counter = 1
  159.         end
  160.         os.startTimer(1)
  161.         --[[for n,reactor in pairs(Reactors) do
  162.             printScr(n,reactor,Sides[n])
  163.         end--]]
  164.     --elseif event == "terminate" then
  165.         --term = term_back
  166.     elseif event == "mouse_click" then
  167.         if not static then
  168.             static = true
  169.             if counter == 1 then
  170.                 counter = #Reactors
  171.             elseif counter==#Reactors then
  172.                 counter = counter-1
  173.             end        
  174.         else
  175.             static = false
  176.         end
  177.     end
  178.     --[[elseif event == "peripheral" then
  179.         mon = getPeripheral("monitor")
  180.         term.setCursorPos(1,1)
  181.         term.write("redirecting to monitor")
  182.         if mon then
  183.             term = mon
  184.         end
  185.     end]]
  186. end
Advertisement
Add Comment
Please, Sign In to add comment