OreSeur-

Test 4

Mar 8th, 2021 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 KB | None | 0 0
  1. --This code is designed to leave the reactor on all the times and maintain
  2. --power levels by adjusting the control rod insertion
  3.  
  4. --return a integar ignoring any remainder
  5. function DIV(a,b)
  6.     return (a - (a % b)) / b
  7. end
  8.  
  9. --the total size of the buffer
  10. local TOTAL_MAX = 10000000
  11. --the amount of power we want to maintain in the buffer
  12. local TARGET_AMOUNT = 5000000
  13. --the default insertion of control rods
  14. local controlLevel = 50
  15. --Buffer zone size
  16. local BUFFER_SIZE = 1000000
  17.  
  18. --connect to reactor
  19. local reactor = peripheral.wrap("back")
  20.  
  21. --confirm connection
  22. if(not reactor.getConnected) then
  23.     print("Not connected to Reactor")
  24. end
  25.  
  26. --turn the reactor on
  27. if(not reactor.getActive()) then
  28.     reactor.setActive(true)
  29.     print("Starting Reactor")
  30. end
  31.  
  32. --move control rod to defaul level
  33. reactor.setAllControlRodLevels(controlLevel)
  34.  
  35.  
  36. while(true) do
  37.    
  38.    
  39.     --find the current amount of energy in the buffer
  40.     local prevEnergy = reactor.getEnergyStored()
  41.    
  42.     sleep(3)
  43.    
  44.     local currentEnergy = reactor.getEnergyStored()
  45.    
  46.    
  47.     if (currentEnergy > prevEnergy ) then
  48.         local direction = 2
  49.         local distance = (currentEnergy - prevEnergy)
  50.         print("Greater than")
  51.     elseif (currentEnergy < prevEnergy ) then
  52.         local direction = 1
  53.         local distance = (prevEnergy - currentEnergy)
  54.         print("Less than")
  55.     elseif (prevEnergy == currentEnergy ) then
  56.         local direction = 0
  57.         local distance = 0
  58.     end
  59.    
  60.     print("prevEnergy: ", currentEnergy)
  61.     print("currentEnergy:  ", currentEnergy)
  62.     print("Direction: ", direction)
  63.     print("Distance:  ", distance)
  64.    
  65.     --if above middle buffer
  66.     if (currentEnergy > TARGET_AMOUNT + (BUFFER_SIZE / 2) ) then
  67.         --if in top buffer
  68.         if currentEnergy > (TOTAL_MAX - BUFFER_SIZE) then
  69.             controlLevel = controlLevel + 50
  70.         --if moving in wrong direction
  71.         elseif direction == (2) then
  72.             controlLevel = controlLevel + DIV(distance, 100000)
  73.         end
  74.        
  75.     --if below middle buffer
  76.     elseif (currentEnergy < TARGET_AMOUNT - (BUFFER_SIZE / 2) ) then
  77.         --if in bottom buffer
  78.         if currentEnergy < BUFFER_SIZE then
  79.             controlLevel = controlLevel - 50
  80.         --if moving in wrong direction
  81.         elseif direction == (1) then
  82.             controlLevel = controlLevel - DIV(distance, 100000)
  83.         end
  84.        
  85.     end
  86.    
  87.     if (controlLevel < 0) then
  88.         controlLevel = 0
  89.     elseif (controlLevel > 100) then
  90.         controlLevel = 100
  91.     end
  92.    
  93.     reactor.setAllControlRodLevels(controlLevel)
  94.     print("Control Level: %d", controlLevel)
  95.    
  96. end
Add Comment
Please, Sign In to add comment