Advertisement
Billiammurton

Reactor Control 1.1

May 1st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.85 KB | None | 0 0
  1. diagCode = 0
  2. --PERIPHERALS
  3. if diagCode == 1 then print("loading peripherals...") end
  4. local modem = peripheral.wrap("back")
  5. local stats = peripheral.wrap("right")
  6. local lights = peripheral.wrap("top")
  7. local cap = peripheral.wrap("tile_blockcapacitorbank_name_0")
  8. local reactor = peripheral.wrap("BigReactors-Reactor_2")
  9. if diagCode == 1 then print("done") end
  10.  
  11. --VARIABLES
  12. if diagCode == 1 then print("loading varibales") end
  13.  
  14. --USER DEFINED
  15. numOfCaps = 10
  16. fuelCutoffLimit = 2000
  17. capUpperLimit = .9
  18. capLowerLimit = .75
  19. includeReactor = true
  20.  
  21. if diagCode == 1 then print("done") end
  22.  
  23. --UTILITIES
  24. if diagCode == 1 then print("loading utilities") end
  25.  
  26. function refresh()
  27.   if diagCode == 2 then print("refreshing") end
  28.   connected = false
  29.   activeBool = reactor.getActive()
  30.   fuelInNuke = reactor.getFuelAmount()
  31.   wasteInNuke = reactor.getWasteAmount()
  32.   oldEnergyInCap = cap.getEnergyStored()*numOfCaps
  33.   sleep(.05)
  34.   energyInCap = cap.getEnergyStored()*numOfCaps
  35.   netEnergyCap = energyInCap-oldEnergyInCap
  36.   oldEnergyInNuke = reactor.getEnergyStored()
  37.   sleep(.05)
  38.   energyInNuke = reactor.getEnergyStored()
  39.   netEnergyNuke = energyInNuke-oldEnergyInNuke
  40.   fuelTemp = reactor.getFuelTemperature()
  41.   casingTemp = reactor.getCasingTemperature()
  42.   fuelConsumed = reactor.getFuelConsumedLastTick()
  43.   powerProduced = reactor.getEnergyProducedLastTick()
  44. end
  45.  
  46. function colorWrite(colorVar, text, x, y)
  47.   if x ~= nil then
  48.     stats.setCursorPos(x,y)
  49.     stats.clearLine()
  50.   end
  51.   stats.setTextColor(colorVar)
  52.   stats.write(tostring(text))
  53. end
  54.  
  55. function writeLine(x,y,label,content,colorVar)
  56.   colorWrite(colors.white, label..": ", x, y)
  57.   colorWrite(colorVar, content)
  58. end
  59.  
  60. function percent(part,whole)
  61.   local per = part/whole*100
  62.   local colorVar
  63.   if per < 20 then
  64.     colorVar = colors.red
  65.   elseif per < 40 then
  66.     colorVar = colors.orange
  67.   elseif per < 60 then
  68.     colorVar = colors.yellow
  69.   elseif per < 80 then
  70.     colorVar = colors.lime
  71.   elseif per < 100 then
  72.     colorVar = colors.green
  73.   elseif per == 100 then
  74.     colorVar = colors.cyan
  75.   end
  76.   return colorVar
  77. end
  78.  
  79. function displayPer(x,y,label,cont,max,append,invert)
  80.   if invert then
  81.     colorVar = percent(max-cont,max)
  82.   else
  83.     colorVar = percent(cont, max)
  84.   end
  85.   writeLine(x,y,label, math.floor(cont), colorVar)
  86.   stats.setTextColor(colors.cyan)
  87.   stats.write(" "..append.." ("..tostring(math.floor(cont/max*100)).."%)")
  88. end
  89.  
  90. if diagCode == 1 then print("done") end
  91.  
  92. --Statistics
  93. if diagCode == 1 then print("loading statistics") end
  94.  
  95. function active(x,y)
  96.   if diagCode == 3 then print("running active: "..tostring(activeBool)) end
  97.   if activeBool then
  98.     colorVar = colors.green
  99.   else
  100.     colorVar = colors.red
  101.   end
  102.   writeLine(x,y,"Active", tostring(activeBool) ,colorVar)
  103. end
  104.  
  105. function nukeEnergy(x,y)
  106.   if diagCode == 3 then print("running nukeEnergy: "..tostring(energyInNuke)) end  
  107.   displayPer(x,y,"RF in Reactor", energyInNuke, 10000000, "RF")
  108. end
  109.  
  110. function capEnergy(x,y)
  111.   if diagCode == 3 then print("running capEnergy: "..tostring(energyInCap)) end
  112.   displayPer(x,y,"RF in Capacitor", energyInCap, maxCapEnergy, "RF")
  113. end
  114.  
  115. function fuelTemperature(x,y)
  116.   if diagCode == 3 then print("running fuelTemperature: "..tostring(fuelTemp)) end
  117.   displayPer(x,y,"Fuel Temp", fuelTemp, 2000, "C", true)
  118. end
  119.  
  120. function caseTemperature(x,y)
  121.   if diagCode == 3 then print("running caseTemperature: "..tostring(caseTemp)) end
  122.   displayPer(x,y,"Casing Temp", casingTemp, 2000,"C", true)  
  123. end
  124.  
  125. function fuelWaste(x,y)
  126.   if diagCode == 3 then print("running fuelWaste: "..tostring(fuelInNuke)..","..tostring(wasteInNuke)) end
  127.   writeLine(x,y,"Fuel",fuelInNuke,colors.yellow)
  128.   colorWrite(colors.cyan, " mB")
  129.   colorWrite(colors.white, " Waste: ")
  130.   colorWrite(colors.blue, wasteInNuke)
  131.   colorWrite(colors.cyan, " mB")
  132. end
  133.  
  134. function efficiency(x,y)
  135.   if diagCode == 3 then print("running efficiency: "..tostring(math.floor(powerProduced/fuelConsumed))) end
  136.   colorWrite(colors.white, "Efficiency: ",x,y)
  137.   colorWrite(colors.cyan, tostring(math.floor(powerProduced/fuelConsumed)).." RF/mB")
  138. end
  139.  
  140. function powerProducedLast(x,y)
  141.   if diagCode == 3 then print("running powerProducedLast: "..tostring(powerProduced)) end
  142.   colorWrite(colors.white, "Power production: ",x,y)
  143.   colorWrite(colors.cyan, string.format("%.5d",powerProduced).." RF/Tick")
  144. end
  145.  
  146. function netPower(x,y)
  147.   if diagCode == 3 then print("running netPower"..tostring(netEnergyCap+netEnergyNuke)) end
  148.   colorWrite(colors.white, "Net energy change: ",x,y)
  149.   colorWrite(colors.cyan, string.format("%.5d",netEnergyCap+netEnergyNuke).." RF/Tick")
  150. end
  151.  
  152. if diagCode == 1 then print("done") end
  153.  
  154. --MAIN LOOP
  155. if diagCode == 1 then print("loading final functions") end
  156.  
  157. --The monitor is 36 across
  158.  
  159. function draw()
  160.   stats.clear()
  161.   if diagCode == 2 then print("drawing") end
  162.   active(1,1)
  163.   nukeEnergy(1,2)
  164.   capEnergy(1,3)
  165.   fuelTemperature(1,4)
  166.   caseTemperature(1,5)
  167.   fuelWaste(1,6)
  168.   efficiency(1,7)
  169.   powerProducedLast(1,8)
  170.   netPower(1,9)
  171. end
  172.  
  173. function check()
  174.   mx = maxCapEnergy
  175.   e = energyInCap
  176.   ul = capUpperLimit
  177.   ll = capLowerLimit
  178.   if diagCode == 2 then print("checking") end
  179.   if includeReactor then
  180.     mx = mx+10000000
  181.     e = e+energyInNuke
  182.   end
  183.   if e/mx > ul then
  184.     reactor.setActive(false)
  185.   elseif e/mx < ll and fuelInNuke > fuelCutoffLimit then
  186.     reactor.setActive(true)
  187.   end
  188.   if fuelInNuke < fuelCutoffLimit then
  189.     reactor.setActive(false)
  190.   end
  191. end
  192.  
  193. stats.setTextScale(0.5)
  194.  
  195. --ONLY DEFINED ONCE
  196. connected = false
  197. maxFuel = reactor.getFuelAmountMax()
  198. maxCapEnergy = cap.getMaxEnergyStored()*numOfCaps
  199. maxNukeEnergy = 10000000
  200.  
  201. if diagCode == 1 then print("done") end
  202.  
  203. if diagCode == 1 then print("entering main loop") end
  204.  
  205. while true do
  206.   if diagCode == 2 then print("iterating") end
  207.   refresh()
  208.   draw()
  209.   check()
  210. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement