Advertisement
Guest User

startup

a guest
Oct 23rd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. p = peripheral.wrap("back")
  2. hasMonitor = peripheral.isPresent("top")
  3. m = nil
  4. if hasMonitor then
  5.     m = peripheral.wrap("top")
  6.     m.setTextScale(1)
  7. end
  8.  
  9. function waitReactor()
  10.     a = p.getEnergyProducedLastTick()
  11.     sleep(2)
  12.     b = p.getEnergyProducedLastTick()
  13.     while (math.abs(1-a/b) > 0.01) do
  14.         a = p.getEnergyProducedLastTick()
  15.         sleep(2)
  16.     end
  17. end
  18.        
  19. function calibrate()
  20.     calibration = {}
  21.     calibration["e"] = {}
  22.     maxEnergy = 0
  23.     maxEff = 0
  24.     for i=1,20 do
  25.         print("Calibrating... " .. i .. "/20")
  26.         l = 100-5*i
  27.         p.setAllControlRodLevels(l)
  28.         waitReactor()      
  29.         e = p.getEnergyProducedLastTick()
  30.         c = p.getFuelConsumedLastTick()
  31.         if (e > maxEnergy) then
  32.             maxEnergy = e
  33.         end
  34.         if (e/c > maxEff) then
  35.             maxEff = e/c
  36.         end
  37.         calibration["e"][i] = e
  38.     end
  39.     print("Calibration completed!")
  40.     print("Max Output: " .. maxEnergy .. " RF/t")
  41.     print("Max Value: " .. maxEff .. " RF/b")
  42.     calibration["maxEnergy"] = maxEnergy
  43.     calibration["maxEff"] = maxEff
  44.     f = fs.open("calibration", "w")
  45.     f.write(textutils.serialize(calibration))
  46.     f.close()
  47. end
  48.  
  49. function getCTL(energy)
  50.     if energy == 0 then
  51.         return 0
  52.     elseif energy >= maxEnergy then
  53.         return 100
  54.     end
  55.     res=100
  56.     for i=0,19 do
  57.         if i == 0 then
  58.             a = 0
  59.         else
  60.             a = calibration.e[i]
  61.         end
  62.         b = calibration.e[i+1]
  63.         if a <= energy and energy <= b then
  64.             slope = 5/(b-a)
  65.             res = slope*(energy-a)+i*5
  66.             break
  67.         elseif b <= energy and energy <= a then
  68.             slope = 5/(a-b)
  69.             res = slope*(energy-b)+i*5
  70.             break
  71.         end
  72.     end
  73.     return math.ceil(res)
  74. end
  75.  
  76. if not fs.exists("calibration") then
  77.     calibrate()
  78. end
  79.  
  80. f = fs.open("calibration", "r")
  81. calibration = textutils.unserialize(f.readAll())
  82. f.close()
  83.  
  84. level = 100
  85. eMax = 10000000
  86. eTargetPct = 66
  87. eTarget = eMax*eTargetPct/100
  88. delay = 30
  89. ticks = delay * 20
  90. p.setActive(true)
  91. maxEff = calibration["maxEff"]
  92. maxEnergy = calibration["maxEnergy"]
  93. p.setAllControlRodLevels(0)
  94. waitReactor()
  95. prod = p.getEnergyProducedLastTick()
  96. if (math.abs(1-prod/calibration.e[20]) > 0.1) then
  97.     print("Reactor needs calibration.")
  98.     calibrate()
  99. end
  100.  
  101. while true do
  102.     p.setAllControlRodLevels(100-level)
  103.     s1 = p.getEnergyStored()
  104.     for i=1,delay do
  105.         sleep(1)
  106.         bPct = math.floor(p.getEnergyStored()*100/eMax)
  107.         if hasMonitor then
  108.             m.setCursorPos(1,1)
  109.             m.write("B " .. bPct .. "% ")
  110.         end
  111.         if (math.abs(bPct-eTargetPct) > 3) then
  112.             ticks = i*20
  113.             break
  114.         end
  115.     end
  116.     s2 = p.getEnergyStored()
  117.     prod = p.getEnergyProducedLastTick()
  118.     net = (s2-s1)/ticks-prod
  119.     eBattPwr = eTarget-p.getEnergyStored()
  120.     eOutPwr = 0-net*ticks
  121.     if (eOutPwr < 0) then
  122.         eOutPwr = 0
  123.     end
  124.     eRequested = eBattPwr + eOutPwr
  125.     if (eRequested < 0) then
  126.         eRequested = 0
  127.     end
  128.     eReqPerTick = eRequested/ticks
  129.     level = getCTL(eReqPerTick)
  130.     if (level > 100) then
  131.         level = 100
  132.     end
  133.     bLevel = math.floor(s2*100/eMax)
  134.     gLevel = math.ceil(-net*100/maxEnergy)
  135.     fuelConsumed = p.getFuelConsumedLastTick()
  136.     eff = math.floor(prod/fuelConsumed)
  137.     if fuelConsumed == 0 then
  138.         eff = 0
  139.     end
  140.     eLevel = math.floor(eff*100/maxEff)
  141.     burnPerHour = math.ceil(fuelConsumed*20*36000)/10
  142.     if (gLevel < 0) then
  143.         gLevel = 0
  144.     end
  145.     print("Battery: " .. bLevel .. "%")
  146.     print("Load: " .. gLevel .. "% (" .. math.ceil(-net) .. " RF/t)")
  147.     print("Generation: " .. level .. "% (" .. math.floor(prod) .. " RF/t)")
  148.     print("Eff: " .. eLevel .. "% (" .. eff .. " RF/b)")
  149.     print("Burn: " .. burnPerHour .. " b/Hr")
  150.     print()
  151.     if hasMonitor then
  152.         m.clear()
  153.         m.setCursorPos(1,1)
  154.         m.write("B " .. bLevel .. "%")
  155.         m.setCursorPos(1,2)
  156.         m.write("L " .. gLevel .. "%")
  157.         m.setCursorPos(1,3)
  158.         m.write("G " .. level .. "%")        
  159.         m.setCursorPos(1,4)
  160.         m.write("E " .. eLevel .. "%")
  161.         m.setCursorPos(1,5)
  162.         m.write("D " .. burnPerHour)
  163.     end
  164.     ticks = delay*20
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement