SHOW:
|
|
- or go back to the newest paste.
| 1 | - | -- Script B: Listen for "pulse" broadcasts and drive front-side redstone |
| 1 | + | -- Script B+: Listen for "SMiner" broadcasts and watch shield state changes |
| 2 | ||
| 3 | - | -- 1) Open your wireless modem on the top side |
| 3 | + | -- 1) Find peripherals |
| 4 | - | rednet.open("top") -- Open Rednet on "top" :contentReference[oaicite:7]{index=7}
|
| 4 | + | local speaker = peripheral.find("warpdriveSpeaker")
|
| 5 | local shield = peripheral.find("warpdriveForceFieldProjector")
| |
| 6 | - | -- 2) Loop, receiving commands and setting the front output |
| 6 | + | assert(shield, "ForceFieldProjector not found!") |
| 7 | - | while true do |
| 7 | + | |
| 8 | - | local senderID, message = rednet.receive("pulse") -- Receive on protocol "pulse" :contentReference[oaicite:8]{index=8}
|
| 8 | + | -- 2) Open all modems for rednet |
| 9 | for _, side in ipairs(peripheral.getNames()) do | |
| 10 | - | -- 3) Update front-side redstone based on the message |
| 10 | + | if peripheral.getType(side) == "modem" then |
| 11 | - | if message == "ACTIVATE" then |
| 11 | + | rednet.open(side) |
| 12 | - | redstone.setOutput("front", true) -- Turn on front redstone :contentReference[oaicite:9]{index=9}
|
| 12 | + | |
| 13 | - | elseif message == "DEACTIVATE" then |
| 13 | + | |
| 14 | - | redstone.setOutput("front", false) -- Turn off front redstone :contentReference[oaicite:10]{index=10}
|
| 14 | + | |
| 15 | -- 3) Speak helper | |
| 16 | local function trySpeak(msg) | |
| 17 | if speaker then speaker.speak(msg) end | |
| 18 | end | |
| 19 | ||
| 20 | ||
| 21 | -- 5) Rednet listener coroutine | |
| 22 | local function rednetListener() | |
| 23 | while true do | |
| 24 | local senderID, message = rednet.receive("SMiner")
| |
| 25 | if message == "check" then | |
| 26 | local enabled = shield.enable() -- query current state | |
| 27 | if enabled then | |
| 28 | print("Received check → Shield currently ENABLED")
| |
| 29 | trySpeak("Shield is enabled")
| |
| 30 | rednet.broadcast("StatON", "SMiner")
| |
| 31 | else | |
| 32 | print("Received check → Shield currently DISABLED")
| |
| 33 | trySpeak("Shield is disabled")
| |
| 34 | rednet.broadcast("StatOFF", "SMiner")
| |
| 35 | end | |
| 36 | end | |
| 37 | end | |
| 38 | end | |
| 39 | ||
| 40 | -- 6) Shield-state monitor coroutine | |
| 41 | local function stateMonitor() | |
| 42 | local prevEnabled = shield.enable() | |
| 43 | while true do | |
| 44 | local enabled = shield.enable() | |
| 45 | if enabled ~= prevEnabled then | |
| 46 | if enabled then | |
| 47 | print("Shield state changed → ENABLED")
| |
| 48 | trySpeak("Shield enabled")
| |
| 49 | rednet.broadcast("StatON", "SMiner")
| |
| 50 | else | |
| 51 | print("Shield state changed → DISABLED")
| |
| 52 | trySpeak("Shield disabled")
| |
| 53 | rednet.broadcast("StatOFF", "SMiner")
| |
| 54 | end | |
| 55 | prevEnabled = enabled | |
| 56 | end | |
| 57 | os.sleep(0.5) | |
| 58 | end | |
| 59 | end | |
| 60 | ||
| 61 | -- 7) Run both loops in parallel | |
| 62 | parallel.waitForAny(rednetListener, stateMonitor) | |
| 63 |