Bunny_bt

Sensor

Apr 12th, 2022 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.41 KB | None | 0 0
  1. --[[    TABLES
  2. targets[Position offset from sensor]
  3. obj[RawName, Damagevalue, Name, Position, Slots, InventoryPercentFull, TotalSpace, ItemCount]
  4. ]]--
  5. local mySensor = peripheral.find('sensor')
  6. local modem = peripheral.find('modem')
  7. local monitor = peripheral.find('monitor')
  8. local targets = mySensor.getTargets()
  9. local dataset = {}
  10. local reactor_pos
  11. local reactor_detail
  12. local isUpdated = true
  13. local chnl = 7200
  14. local chnl_re = 7201
  15.  
  16. if monitor ~= nil then
  17.     term.redirect(monitor)
  18.     monitor.setTextScale(0.5)
  19. end
  20.  
  21. function FindReactor()
  22.     print('Finding Reactor...')
  23.     for k, v in pairs(targets) do
  24.         local obj = mySensor.getTargetDetails(k)
  25.         if string.find(obj.Name, 'Reactor') then
  26.             print('Reactor Found: ' .. obj.Name)
  27.             dataset['Reactor_Data'] = obj
  28.             local reactor_posTbl = dataset.Reactor_Data.Position
  29.             reactor_pos = (tostring(reactor_posTbl.X) .. ',' .. tostring(reactor_posTbl.Y) .. ',' .. tostring(reactor_posTbl.Z))
  30.             return dataset
  31.         end
  32.     end
  33.     if dataset.Reactor_Data == nil then print('No reactor found.') end
  34. end
  35.  
  36. function UpdateData()
  37.     print('Finding Fuel Rod...')
  38.     os.sleep(1)
  39.     dataset['Reactor_Data'].Slots = mySensor.getTargetDetails(reactor_pos).Slots
  40.     for k, v in pairs(dataset.Reactor_Data.Slots) do
  41.         if string.find(v.Name, 'Fuel Rod') then
  42.             print('Fuel Rod Found: ' .. v.Name)
  43.             dataset['Rod_Data'] = v
  44.             print('Rod data assigned')
  45.             return dataset
  46.         end
  47.     end
  48.     if dataset.Rod_Data == nil then print('No rod found.') end
  49. end
  50.  
  51. function PushData()
  52.     local timer = os.startTimer(30)
  53.     print('Wating for PullEvent...')
  54.     local evt = {os.pullEvent()}
  55.     local isReactor_Powered = redstone.testBundledInput('back', colors.lightBlue)
  56.     dataset['Reactor_State'] = isReactor_Powered
  57.  
  58.     if evt[1] == 'peripheral_detach' then
  59.         isUpdated = true
  60.         modem.transmit(chnl_re, chnl, dataset)
  61.  
  62.     elseif evt[1] == 'timer' then
  63.         isUpdated = true
  64.         modem.transmit(chnl_re, chnl, dataset)
  65.  
  66.     else isUpdated = false end
  67.  
  68.     print(evt[1])
  69.     os.cancelTimer(timer)
  70. end
  71.  
  72. FindReactor()
  73.  
  74. while true do
  75.     while isUpdated do
  76.         UpdateData()
  77.         isUpdated = false
  78.     end
  79.  
  80.     PushData()
  81.     if monitor == nil then
  82.         term.setCursorPos(1,1)
  83.         term.clear()
  84.     end
  85. end
Add Comment
Please, Sign In to add comment