shinevision

Reactor Lua

Nov 28th, 2020 (edited)
573
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. print("ReactorOs\n")
  4. local reactor = peripheral.wrap("back") --Wrap dat ass
  5. local buffer = reactor.getEnergyStored() --Put currect buffer amount in var.
  6. local controlRodsAmount = reactor.getNumberOfControlRods() --Get number of control rods. Can later be used to loop over each and set percentages.
  7. local BUFFER_MAX = 10000000 --Max internal buffer.
  8.  
  9. local keepAtBuffer = 8000000
  10. local lastBufferAmount = buffer
  11.  
  12. local currentRodLevel = 50 --Set controll rods at 50% for start and do math stuffz later to figure out how much % it should be to power ze shittz.
  13. local changeSpeed = 10
  14.  
  15. local updateDelay = 0.1 --How many seconds of delay there should be each loop. (os.sleep(ThisIsTheVariable))
  16.  
  17. local allowedChange = 128 --Amount of change acceptable between the desired output and the actual buffer. This prevents it going up and down every second like a sine wave lol.
  18.  
  19. print("ReactorOS fully loaded.")
  20.  
  21. function LiftRods() --Made this functions because it needs to check if its above 100% each time.
  22.     if (currentRodLevel - changeSpeed) >= 0 then
  23.         currentRodLevel = (currentRodLevel - changeSpeed)
  24.     else
  25.         currentRodLevel = 0
  26.     end
  27. end
  28.  
  29. function DropRods()
  30.     if (currentRodLevel + changeSpeed)  <= 100 then --Cant go -1%... 0% is max...
  31.         currentRodLevel = (currentRodLevel + changeSpeed)
  32.     else
  33.         currentRodLevel = 100
  34.     end
  35. end
  36.  
  37. function Reactor()
  38.     local buffer = reactor.getEnergyStored()
  39.     print("buffer: " .. buffer .. "\nchangeSpeed: " .. changeSpeed .. "\nChange: " .. math.abs(buffer - keepAtBuffer) .. "\ncurrentRodLevel: " .. currentRodLevel .. "\n\n")
  40.     if math.abs(buffer - keepAtBuffer) > 10000 then
  41.         --changeSpeed = 10
  42.     else
  43.         --changeSpeed = 1
  44.     end
  45.  
  46.     if(buffer + allowedChange) == keepAtBuffer or (buffer - allowedChange) == keepAtBuffer then
  47.         return
  48.     end
  49.  
  50.     if(buffer < keepAtBuffer and (buffer + allowedChange) < keepAtBuffer) then --Reactor is below min perc...
  51.         print("Lifting rods by " .. changeSpeed .. " step(s).")
  52.         LiftRods()
  53.         reactor.setAllControlRodLevels(currentRodLevel)
  54.  
  55.     else if (buffer > keepAtBuffer and (buffer - allowedChange) > keepAtBuffer) then
  56.         print("dropping rods by " .. changeSpeed .. " step(s).")
  57.         DropRods()
  58.         reactor.setAllControlRodLevels(currentRodLevel)
  59.     end
  60.     end
  61.     os.sleep(updateDelay) --There is no reason to be SPEEEEEED.
  62.     --Yield bios.
  63.     os.queueEvent("fakeEvent");
  64.     os.pullEvent();
  65. end
  66.  
  67. local doOnce = true
  68. function SimpleReactor()
  69.    
  70.     local allowedOffset = 10
  71.     buffer = reactor.getEnergyStored()
  72.  
  73.     local updateDelay = 0.1 -- 0.1 means update 10 times a second. 1 means every second.
  74.    
  75.     if doOnce then
  76.         reactor.setAllControlRodLevels(0) --Put this outside loop instead of this dumb shit.
  77.         doOnce = false
  78.     end
  79.  
  80.     reactor.setActive(buffer < keepAtBuffer)
  81.     os.sleep(updateDelay)
  82. end
  83.  
  84. print("Starting SimpleReactor().")
  85. while(true) do
  86.     SimpleReactor()
  87.  
  88.     --Yield bios.
  89.     os.queueEvent("fakeEvent");
  90.     os.pullEvent();
  91. end
  92. -- pastebin run HdLq4jDC
RAW Paste Data