Flaghacker

BioReactor controller

Feb 26th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.76 KB | None | 0 0
  1. --Credit Flaghacker
  2.  
  3. --Settings
  4. --how many slots the bioreactor has to contain in order to be "filled"
  5. local minSlots = 4
  6. -- the peripheral id for a bioreactor
  7. local bioReactorId = "bioreactor"
  8. --(optionally) the side of the bioreactor
  9. local bioReactorSide
  10.  
  11. --vars
  12. local p
  13.  
  14. local s = 10
  15. local e = 18
  16.  
  17. --util
  18. function printBoolean(boolean)
  19.         if term.isColor() then
  20.                 if boolean then
  21.                         term.setTextColor(colors.lime)
  22.                 else
  23.                         term.setTextColor(colors.red)
  24.                 end
  25.         end
  26.         print(boolean)
  27.         if term.isColor() then
  28.                 term.setTextColor(colors.white)
  29.         end
  30. end
  31.  
  32. --functions
  33. local function wrapPeripheral()
  34.     if bioReactorSide == nil then
  35.         for i, v in ipairs(rs.getSides()) do
  36.             if peripheral.getType(v) == bioReactorId then
  37.                 p = peripheral.wrap(v)
  38.                 bioReactorSide = v;
  39.                 return
  40.             end
  41.         end
  42.     else
  43.         if (peripheral.getType(bioReactorSide)) == bioReactorId then
  44.             p = peripheral.wrap(bioReactorSide)
  45.         else
  46.             error(string.format("The peripheral on side '%s' is not of type '%s', but of type '%s'", tostring(bioReactorSide), tostring(bioReactorId), tostring(peripheral.getType(bioReactorSide))))
  47.         end
  48.     end
  49.    
  50.     error("No bioreactor could be found")
  51. end
  52.  
  53. local function isSlotFilled(slot)
  54.     return not (p.getStackInSlot(slot) == nil)
  55. end
  56.  
  57. local function isReactorFilled()
  58.     local t = 0;
  59.     for i = s, e do
  60.         if (isSlotFilled(i)) then
  61.             t = t + 1
  62.         end
  63.     end
  64.    
  65.     return t >= minSlots
  66. end
  67.  
  68. --main code
  69. wrapPeripheral()
  70.  
  71. while true do
  72.     local a = isReactorFilled()
  73.     rs.setOutput(bioReactorSide, not a)
  74.     rs.setOutput("top", not a)
  75.     term.clear()
  76.     write("Bio reactor enabled: ")
  77.     printBoolean(a)
  78.     sleep(.5)
  79. end
Advertisement
Add Comment
Please, Sign In to add comment