Autonomous security network
Four programs work together. Three run on devices from drives, so the base stays guarded while the computer is far away. The fourth runs on the computer and summarizes what happened.
You need: a computer, repeaters, an Entity Detector gate (no drive), an Alarm Module, a Redstone Controller with a
door on its south side, a Display Module, a Control Panel panel, a Storage Server records and three drives.
A repeater holds 16 links, so one is enough here. Name the detector gate, the panel panel and the storage server
records. The devices with drives do not need names.
Part 1: the alarm decides
Section titled “Part 1: the alarm decides”Drive type alarm. It listens to the detector directly, sounds for monsters and strangers, tells the others with a
custom event and records the alert on the Storage Server.
// Advanced: autonomous security, part 1 (Alarm Module drive). No computer needs to be running.// Listens directly to the Entity Detector "gate" (which needs no drive), sounds this alarm for hostile mobs and// strangers, tells the other devices with a custom event and keeps a bounded history on the Storage Server "records".const gate = detectors.named("gate")const records = storageServers.named("records")const friends = ["Steve", "Alex"]global alerts = 0
fn stranger(entity) { if entity.hostile { return true } if not entity.player { return false } for name in friends { if name is entity.name { return false } } return true}
on gate.entityEnter(entity) { if not stranger(entity) { return } alerts++ device.activate() // Structured custom event: other programs receive the same dictionary. sent, err = events.emit("security.alert", {who: entity.name, hostile: entity.hostile, count: alerts}) if err is not nil { log.error(err) } now = device.time() ok, err = records.append("alerts", {who: entity.name, day: now.day, time: now.dayTime}) if err is not nil { log.error(err) }}
on gate.disconnected { // The detector's area unloaded or it was unlinked: warn everyone once. events.emit("security.offline", "gate")}Part 2: the door locks
Section titled “Part 2: the door locks”Drive type redstone_controller. It locks the door when an alarm reports an alert, and button four on the panel
unlocks it.
// Advanced: autonomous security, part 2 (Redstone Controller drive).// Locks the door on its south side while an alert is active and reopens it from the Control Panel "panel".const panel = controlPanels.named("panel")global locked = false
on startup { device.setMode(Direction.SOUTH, PortMode.OUTPUT) device.set(Direction.SOUTH, 0)}
on events.custom("security.alert", alert, sender) { // sender is filled in by the server, so it cannot be faked. if sender.kind is not "alarm" { return } locked = true device.set(Direction.SOUTH, 15) print("Locked because of " + alert.who)}
on panel.buttonPressed(button) { if button.id is Button.FOUR { locked = false device.set(Direction.SOUTH, 0) events.emit("security.clear", {by: button.player}) }}Part 3: the board shows the state
Section titled “Part 3: the board shows the state”Drive type display. It shows how many entities are near the gate and the last alert.
// Advanced: autonomous security, part 3 (Display Module drive).// Shows a live summary: people near the gate, the last alert and whether the system is clear.const gate = detectors.named("gate")global near = 0global last = "none"
fn redraw() { device.write("Near the gate: " + str(near) + "\nLast alert: " + last)}
on startup { redraw()}
on gate.countChanged(count, previous) { near = count redraw()}
on events.custom("security.alert", alert, sender) { last = alert.who redraw()}
on events.custom("security.clear", info, sender) { last = "cleared by " + info.by redraw()}Part 4: the computer reports
Section titled “Part 4: the computer reports”When the computer is loaded again, it reads the alerts the devices recorded and keeps the newest 100.
// Advanced: autonomous security, part 4 (computer).// The devices above run on their own. When this computer is loaded again it reads what they recorded on the// Storage Server "records" and keeps only the newest 100 alerts.const records = storageServers.named("records")
class Tally { total = 0 hostile = 0
fn add(alert) { self.total++ if alert.who is "Zombie" or alert.who is "Skeleton" { self.hostile++ } }}
fn trim(alerts) { while alerts.length > 100 { alerts.remove(0) } return alerts}
on startup { alerts, err = records.read("alerts") if err is not nil { print("No alerts yet: " + err.code) return } tally = Tally() for alert in alerts { tally.add(alert) } print(str(tally.total) + " alerts while the computer was away, " + str(tally.hostile) + " from common mobs") records.write("alerts", trim(alerts))}Install it
Section titled “Install it”- Write parts 1 to 3 onto three drives from the computer, each with the matching Type.
- Install each drive in its device and select Start in the device’s form.
- Put part 4 on the computer.
Walk away until the computer’s area unloads, then let a zombie reach the gate. The alarm, door and board keep working. Come back: the computer prints how many alerts happened while it was away.
Why it is safe
Section titled “Why it is safe”- Each drive program only controls its own device. The alarm cannot open the door; it can only announce an alert.
- The door checks
sender.kind is not "alarm"before locking, and the server fills in the sender, so no other program can pretend to be the alarm. - The detector needs no program at all: the alarm and the display listen to it through device constants, which give events but no control.