OC Sensors
Sensor addon for the OpenComputers modification, inspired by the OpenCCSensors development. Provides a specialized sensor block that enables computers to analyze surrounding objects.
Functional Capabilities
scan(x:number, y:number, z:number, [side:number]) — performs scanning of a block at specified coordinates relative to the sensor
search([name:string=""], [meta:number=-1], [section:string=""], [range:number=]) — executes search for blocks based on given parameters within specified radius
searchEntities(x1:number, y1:number, z1:number, x2:number, y2:number, z3:number) — detects entities in the designated area relative to the sensor device
Practical Usage Examples
Energy Capacity Monitoring
local component = require('component')
local sensor = component.sensor
local totalCapacity = 0
local totalStored = 0
print("Searching for energy blocks")
local positions = sensor.search("", -1, "energy")
print("Scanning " .. #positions .. " blocks")
for ,pos in ipairs(positions) do
local info = sensor.scan(pos.x, pos.y, pos.z)
print(" " .. info.block.label .. " @ " .. pos.x .. "," .. pos.y .. "," .. pos.z)
totalStored = totalStored + info.data.energy.energyStored
totalCapacity = totalCapacity + info.data.energy.maxEnergyStored
end
print("")
print("Energy summary:")
print(" Total capacity: " .. totalCapacity);
print(" Total stored: " .. totalStored);
Generator Operation Automation
local component = require("component")
local sides = require("sides")
local sensor = component.sensor
local redstone = component.redstone
local redstoneSide = sides.back
-- One-time generator search
local generators = sensor.search("", -1, "extrautils2")
while(true) do
-- Checking readiness of all devices
local missing = 0
for ,pos in ipairs(generators) do
local info = sensor.scan(pos.x, pos.y, pos.z)
local xu = info.data.extrautils2
-- Is the generator active?
local alreadyRunning = info.data.extrautils2.processTime > 0
-- Sufficient items?
local enoughItems = info.data.items.n == 0 or info.data.items.n == #info.data.items
-- Sufficient fluid?
local enoughFluid = true
if(info.data.fluid.n > 0) then
for ,tank in ipairs(info.data.fluid) do
if(tank.contents == nil or tank.contents.amount == 0) then
enoughFluid = false
end
end
end
if(alreadyRunning or (enoughItems and enoughFluid)) then
--print(info.block.label .. ": " .. serialization.serialize(info.data))
print("Ready: " .. info.block.label)
else
print("Missing: " .. info.block.label)
-- Instant shutdown of other generators
redstone.setOutput(redstoneSide, 0)
missing = missing + 1
end
end
-- System response
if(missing == 0) then
print("--> All generators ready")
redstone.setOutput(redstoneSide, 15)
else
print("--> " .. missing .. " generators not ready")
end
os.sleep(1)
end
Fluid Analysis from Special Cows
local component = require('component')
local serialization = require('serialization')
local sensor = component.sensor
local range = 5
local foundFluids = {}
for , entity in ipairs(sensor.searchEntities(-range, -range, -range, range, range, range)) do
if(entity.type == "neutral" and entity.moofluids ~= nil) then
foundFluids[#foundFluids+1] = entity.moofluids.fluid.name
end
end
print(serialization.serialize(foundFluids))
-- {"lava", "lava", "lava", "water", "water", "lava"}