Alakazard12

AltProto_Reactor

Mar 11th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.52 KB | None | 0 0
  1. local reactor = peripheral.find("BigReactors-Reactor")
  2.  
  3. local maximum = 100000000 -- subtracted from max
  4. local minimum = 500000000 -- subtracted from max
  5.  
  6. local storage_id = RECIPIENT_ALL
  7.  
  8. local last_req = true
  9.  
  10. CreateTimer(function()
  11.     if reactor then
  12.         if last_req then
  13.             local active = reactor.getActive()
  14.  
  15.             if active then
  16.                 reactor.setActive(false)
  17.                 Log("Disabled reactor")
  18.             end
  19.         end
  20.  
  21.         last_req = true
  22.         SendMessage("REQ_EN", storage_id, nil)
  23.     end
  24. end, 3, true)
  25.  
  26.  
  27.  
  28. OnEvent(function(event, side)
  29.     if not reactor and peripheral.getType(side) == "BigReactors-Reactor" then
  30.         reactor = peripheral.wrap(side)
  31.         Log("Reactor attached")
  32.     end
  33. end, "peripheral")
  34.  
  35.  
  36.  
  37. OnMessage(function(id, recipient, sender, data)
  38.     if reactor then
  39.         local active = reactor.getActive()
  40.         if data.max - data.stored <= maximum then
  41.             if active then
  42.                 reactor.setActive(false)
  43.                 Log("Disabled reactor")
  44.             end
  45.         elseif data.max - data.stored >= minimum then
  46.             if not active then
  47.                 reactor.setActive(true)
  48.                 Log("Enabled reactor")
  49.             end
  50.         end
  51.     end
  52.  
  53.     last_req = false
  54. end, "RES_EN")
  55.  
  56.  
  57.  
  58. OnMessage(function(id, recipient, sender, data)
  59.     if reactor then
  60.         reactor.setActive(not not data)
  61.         if data then
  62.             Log("Enabled reactor")
  63.         else
  64.             Log("Disabled reactor")
  65.         end
  66.     end
  67.  
  68.     last_req = false
  69. end, "REACT_SET")
  70.  
  71.  
  72. OnEvent(function(event, side)
  73.     if reactor and peripheral.getType(side) == "BigReactors-Reactor" then
  74.         reactor = nil
  75.         Log("Reactor detached")
  76.     end
  77. end, "peripheral_detach")
Add Comment
Please, Sign In to add comment