Advertisement
theguywhojoojes

Creeper warning

Dec 16th, 2023 (edited)
1,128
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | None | 1 0
  1. local requiredMods = {
  2.     "plethora:sensor",
  3.     "plethora:glasses",
  4. }
  5. local hostileEntities = {
  6.     "creeper",
  7.     "zombie",
  8.     "skeleton",
  9.     "spider",
  10. }
  11. local RADIUS = 5
  12. local mods = peripheral.wrap("back")
  13.  
  14. -- check mods
  15. for _, mod in pairs(requiredMods) do
  16.     if not mods.hasModule(mod) then
  17.         error(("Module: %s not found"):format(mod), 0)
  18.     end
  19. end
  20.  
  21. local canvas = mods.canvas()
  22.  
  23. function getDistance(entity)
  24.     local x = entity.x
  25.     local y = entity.y
  26.     local z = entity.z
  27.     return math.sqrt(x*x+y*y+z*z)
  28. end
  29.  
  30. function isHostile(entity)
  31.     local isHostile = false
  32.     for _, name in pairs(hostileEntities) do
  33.         if entity.name:lower() == name then
  34.             isHostile = true
  35.             break
  36.         end
  37.     end
  38.     return isHostile
  39. end
  40.  
  41. function getEntities(filter)
  42.     local found = {}
  43.     for _, entity in pairs(mods.sense()) do
  44.         if filter(entity) then table.insert(found, entity) end
  45.     end
  46.     return found
  47. end
  48.  
  49. function drawWarning()
  50.     canvas.clear()
  51.     local text = canvas.addText({x = 5, y = 5}, "")
  52.     text.setScale(2)
  53.     text.setText("WARNING: HOSTILES")
  54. end
  55.  
  56. function drawNoWarning()
  57.     canvas.clear()
  58.     local text = canvas.addText({x = 5, y = 5}, "")
  59.     text.setScale(2)
  60.     text.setText("All set")
  61. end
  62.  
  63. function checkHostiles()
  64.     local nearHostileFilter =
  65.     function(entity)
  66.             return getDistance(entity) <= RADIUS and isHostile(entity)
  67.     end
  68.  
  69.     local nearHostiles = getEntities(nearHostileFilter)
  70.     if table.getn(nearHostiles) > 0 then
  71.         drawWarning()
  72.     else
  73.         drawNoWarning()
  74.     end
  75. end            
  76.  
  77. canvas.clear()
  78.  
  79. while true do
  80.     checkHostiles()
  81.     os.sleep(0.5)
  82. end
  83.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement