Clem105

MonitorReactor

Feb 22nd, 2022 (edited)
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 KB | None | 0 0
  1. os.loadAPI("viewportAPI")
  2. os.loadAPI("buttonAPI")
  3. os.loadAPI("pbar")
  4.  
  5. p = peripheral.wrap("top")
  6. pbar.SetPeripheral("top")
  7. rSide = "bottom"
  8.  
  9. tresh = 0.9
  10.  
  11.  
  12.  
  13. modem = peripheral.wrap("back")
  14. modem.open(137)
  15.  
  16. redstone.setBundledOutput(rSide, 0)
  17.  
  18. viewport = viewportAPI.new({term = p})
  19.  
  20. flagStorage = true
  21. flagDoor = false
  22. flagReactor = false
  23. flagProgressBar = false
  24.  
  25. buttonReactor = buttonAPI.new({
  26.     x = 5, y = 2, width = 19, height = 5, text = "Reacteur", textColor = colors.white, backgroundColor = colors.red
  27. })
  28.  
  29. viewport:addElement(buttonReactor)
  30.  
  31. buttonDoor = buttonAPI.new({
  32.     x = 5, y = 9, width = 19, height = 5, text = "Porte", textColor = colors.white, backgroundColor = colors.red
  33. })
  34.  
  35. viewport:addElement(buttonDoor)
  36. viewport:redraw()
  37.  
  38. function clickHandlerDoor(element, posX, posY) 
  39.     flagDoor = not flagDoor
  40.     element.backgroundColor = flagDoor and colors.green or colors.red
  41.     bundleHandle()
  42.     return true
  43. end
  44.  
  45. function clickHandlerReactor(element, posX, posY)
  46.     if not flagStorage then
  47.         flagReactor = not flagReactor
  48.         element.backgroundColor = flagReactor and colors.green or colors.red
  49.         bundleHandle()
  50.     end
  51.     return true
  52. end
  53.  
  54. function boolToNum(bool)
  55.     return bool and 1 or 0
  56. end
  57.  
  58. function bundleHandle()
  59.     redstone.setBundledOutput(rSide,boolToNum(flagReactor)*boolToNum(not flagStorage)*colors.white+boolToNum(flagDoor)*colors.red)
  60. end
  61.  
  62. buttonDoor.callback = clickHandlerDoor
  63. buttonReactor.callback = clickHandlerReactor
  64.  
  65. while true do
  66.     event, side, param1, param2, param3 = os.pullEvent()
  67.     if event == "monitor_touch" then
  68.         viewport:handleClick(param1, param2)
  69.         if flagProgressBar then
  70.             pbar.DrawToPeripheral("Energie")
  71.         end
  72.     elseif event == "modem_message" then
  73.         stored = param3[1]
  74.         capacity = param3[2]
  75.         ratio = stored/capacity
  76.         if ratio > tresh then
  77.             flagStorage = true
  78.             buttonReactor.backgroundColor = colors.purple
  79.             viewport:redraw()
  80.             bundleHandle()
  81.         elseif flagStorage then
  82.             flagStorage = false
  83.             buttonReactor.backgroundColor = flagReactor and colors.green or colors.red
  84.             viewport:redraw()
  85.             bundleHandle()
  86.         end
  87.         if not flagProgressBar then
  88.             flagProgressBar = true
  89.             pbar.SetTable("Energie",capacity,stored,5,24,20)
  90.             pbar.DrawToPeripheral("Energie")
  91.         else
  92.             pbar.SetCurValue("Energie",stored)
  93.             pbar.DrawToPeripheral("Energie")
  94.         end
  95.     end
  96. end
Add Comment
Please, Sign In to add comment