Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local requiredMods = {
- "plethora:sensor",
- "plethora:glasses",
- }
- local hostileEntities = {
- "creeper",
- "zombie",
- "skeleton",
- "spider",
- }
- local RADIUS = 5
- local mods = peripheral.wrap("back")
- -- check mods
- for _, mod in pairs(requiredMods) do
- if not mods.hasModule(mod) then
- error(("Module: %s not found"):format(mod), 0)
- end
- end
- local canvas = mods.canvas()
- function getDistance(entity)
- local x = entity.x
- local y = entity.y
- local z = entity.z
- return math.sqrt(x*x+y*y+z*z)
- end
- function isHostile(entity)
- local isHostile = false
- for _, name in pairs(hostileEntities) do
- if entity.name:lower() == name then
- isHostile = true
- break
- end
- end
- return isHostile
- end
- function getEntities(filter)
- local found = {}
- for _, entity in pairs(mods.sense()) do
- if filter(entity) then table.insert(found, entity) end
- end
- return found
- end
- function drawWarning()
- canvas.clear()
- local text = canvas.addText({x = 5, y = 5}, "")
- text.setScale(2)
- text.setText("WARNING: HOSTILES")
- end
- function drawNoWarning()
- canvas.clear()
- local text = canvas.addText({x = 5, y = 5}, "")
- text.setScale(2)
- text.setText("All set")
- end
- function checkHostiles()
- local nearHostileFilter =
- function(entity)
- return getDistance(entity) <= RADIUS and isHostile(entity)
- end
- local nearHostiles = getEntities(nearHostileFilter)
- if table.getn(nearHostiles) > 0 then
- drawWarning()
- else
- drawNoWarning()
- end
- end
- canvas.clear()
- while true do
- checkHostiles()
- os.sleep(0.5)
- end
Advertisement
Advertisement