Advertisement
Rolcam

Computercraft - Sensor test program

Jul 11th, 2025 (edited)
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --This is just a test prorgam so I can get familiar with various computercraft peripherals. Please excuse the mess.
  2.  
  3. -- load the API
  4. os.loadAPI("ocs/apis/sensor")
  5.  
  6.  
  7.  
  8. -- Change the peripheral names accordingly
  9. -- Prox = sensor (requires a proximity card) | mon = monitor | glass = terminal glass bridge | printer = printer
  10. prox = sensor.wrap("top")
  11. mon = peripheral.wrap("left")
  12. glass = peripheral.wrap("right")
  13. -- Set this to 0 to disable printing
  14. printReadout = 1
  15. printer = peripheral.wrap("bottom")
  16.  
  17. -- get the targets
  18. local targets = prox.getTargets()
  19.  
  20. --Redirects output to monitor
  21. term.redirect(mon)
  22. term.clear()
  23. term.setCursorPos(1,1)
  24. term.setTextColor(colors.yellow)
  25. print("Sensor Readout - Entities:")
  26. print()
  27.  
  28. glass.clear()
  29. --List
  30. glass.addBox(348,1,180,200,0xFFFFFF, 0.2)
  31. glass.addText(350,2,"Radar Readout:", 0xF2B233)
  32. glassY = 12
  33. -- Page tracker for printer
  34. pgCt = 1
  35.  
  36. term.setTextColor(colors.white)
  37. if printReadout == 1 then
  38.     printer.newPage()
  39.     printer.setPageTitle("Radar Printout Page " .. pgCt)
  40.     -- Y value tracker for printer - will start a new page once it gets to the bottom
  41.     printY = 1
  42.  
  43.     -- Updates the cursor position on the page = for printer
  44.     printer.setCursorPos(1,printY)
  45. end
  46. -- Checks if the page is full. If so, it starts a new page
  47. function printCheck ()
  48.     if printY > 21 then
  49.         -- Ends the page
  50.         printer.endPage()
  51.         -- Adds one to the page counter
  52.         pgCt = pgCt + 1
  53.         -- Starts a new page
  54.         printer.newPage()
  55.         printer.setPageTitle("Radar Printout Page " .. pgCt)
  56.         -- Resets printer Y position to 1
  57.         printY = 1
  58.         printer.setCursorPos(1,printY)
  59.     end
  60. end
  61.  
  62. -- loop through targets from sensor
  63. for name, basicDetails in pairs(targets) do
  64.  
  65.     -- get more details about them
  66.   local moreDetails = prox.getTargetDetails(name)
  67.     -- Changes the text color to yellow if it lists a player. Otherwise, it's white
  68.   if moreDetails.IsPlayer == true then
  69.         term.setTextColor(colors.yellow)
  70.         print("Found player: "..name)
  71.         if printReadout == 1 then
  72.             printer.setCursorPos(1,printY)
  73.             printer.write("Found player: "..name)
  74.             printY = printY + 1
  75.             printCheck()
  76.         end
  77.   else
  78.         term.setTextColor(colors.white)
  79.         print("Found entity: "..name)
  80.         if printReadout == 1 then
  81.             printer.write("Found entity: "..name)
  82.             printY = printY + 1
  83.             printCheck()
  84.         end
  85.   end
  86.    
  87.    
  88.   -- print their health
  89.   print("Health: "..moreDetails.Health)
  90.   if printReadout == 1 then
  91.     printer.setCursorPos(1,printY)
  92.     printer.write("Health: "..moreDetails.Health)
  93.     printY = printY + 1
  94.     printCheck()
  95.   end
  96.   term.setTextColor(colors.white)
  97.   print("------")
  98.   if printReadout == 1 then
  99.     printer.setCursorPos(1,printY)
  100.     printer.write("------")
  101.     printY = printY + 1
  102.     printCheck()
  103.   end
  104. end
  105.  
  106. if printReadout == 1 then
  107.     printer.endPage()
  108. end
  109. term.restore()
  110.  
  111. function glassCheck()
  112.     if glassY > 200 then
  113.         sleep(5)
  114.         glass.clear()
  115.         glass.addBox(348,1,180,200,0xFFFFFF, 0.2)
  116.         glass.addText(350,2,"Radar Readout:", 0xF2B233)
  117.         glassY = 12
  118.     end
  119. end
  120.  
  121. -- loop through targets from sensor
  122. for name, basicDetails in pairs(targets) do
  123.    
  124.      -- get more details about them
  125.   local moreDetails = prox.getTargetDetails(name)
  126.     -- Changes the text color to yellow if it lists a player. Otherwise, it's white
  127. if moreDetails.IsPlayer == nil then  
  128.         glass.addText(350,glassY,"Found ??????: "..name, 0xF2B233)
  129.         glassY = glassY + 10
  130.         glassCheck()
  131. elseif moreDetails.IsPlayer == true then
  132.         glass.addText(350,glassY,"Found player: "..name, 0xF2B233)
  133.         glassY = glassY + 10
  134.         glassCheck()
  135.   else
  136.         glass.addText(350,glassY,"Found entity: "..name, 0x000000)
  137.         glassY = glassY + 10
  138.         glassCheck()
  139.   end
  140.    
  141.   -- print their health
  142.   glass.addText(350,glassY,"Health: "..moreDetails.Health, 0x000000)
  143.   glassY = glassY + 10
  144.   glassCheck()
  145.   glass.addText(350,glassY,"------", 0x000000)
  146.   glassY = glassY + 10
  147.   glassCheck()
  148. end
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement