Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Combined Shield Control & Monitor with Rednet “Toggle” control (“ztc+”)
- -- 1) Configuration
- local OffsetValue = 3
- local safedist = 15
- -- 2) Wrap peripherals
- local shield = peripheral.find("warpdriveForceFieldProjector")
- local speaker = peripheral.find("warpdriveSpeaker")
- assert(shield, "ForceFieldProjector not found!")
- -- 3) Speak helper (no-op if no speaker)
- local function trySpeak(msg)
- if speaker then speaker.speak(msg) end
- end
- -- 4) Compute offset point and distance
- local function shieldOffset(lx, ly, lz, fx, fy, fz)
- local dx, dy, dz = lx - fx, ly - fy, lz - fz
- local distance = math.sqrt(dx*dx + dy*dy + dz*dz)
- dx, dy, dz = dx/distance, dy/distance, dz/distance
- return
- lx + dx * OffsetValue,
- ly + dy * OffsetValue,
- lz + dz * OffsetValue,
- distance
- end
- -- 5) Ensure config script exists
- if not fs.exists("conf") then
- shell.run("pastebin get 38M5cNbZ conf")
- term.clear()
- end
- -- 6) Open all modems for Rednet
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- end
- end
- -- 7) Announce readiness
- print("Control System Online, Toggle via Rednet → Press C to Configure")
- -- 8) Determine shield range multiplier
- local _, upgrades = shield.getUpgrades()
- local Size = ({
- ["1/4 x Range"] = 16,
- ["2/4 x Range"] = 32,
- ["3/4 x Range"] = 48,
- ["4/4 x Range"] = 64,
- ["0/4 x Range"] = 0,
- })[upgrades:match("(%d/4 x Range)")] or 32
- -- 9) State-monitor coroutine: announce on enable/disable
- local function stateMonitor()
- local prevEnabled = shield.enable()
- while true do
- local enabled = shield.enable()
- if enabled ~= prevEnabled then
- if enabled then
- rednet.broadcast("StatON", "SMiner")
- else
- rednet.broadcast("StatOFF", "SMiner")
- end
- prevEnabled = enabled
- end
- os.sleep(0.5)
- end
- end
- -- 10) Rednet-listener coroutine: reply to “check”
- local function rednetListener()
- while true do
- local _, message = rednet.receive("SMiner")
- if message == "check" then
- local enabled = shield.enable()
- if enabled then
- rednet.broadcast("StatON", "SMiner")
- else
- rednet.broadcast("StatOFF", "SMiner")
- end
- end
- end
- end
- -- 11) Main control coroutine
- local function mainControl()
- local lastX, lastY, lastZ = 0,0,0
- local toggledOn = false
- while true do
- local event, p1, p2, p3 = os.pullEventRaw()
- if event == "rednet_message" and p3 == "SMineBroadcast" then
- lastX, lastY, lastZ = p2.x, p2.y, p2.z
- local fx, fy, fz = shield.getLocalPosition()
- local t1x, t1y, t1z, distance = shieldOffset(lastX, lastY, lastZ, fx, fy, fz)
- local tx = (t1x - fx) / Size
- local ty = (t1y - fy) / Size
- local tz = (t1z - fz) / Size
- local rd = math.floor(distance + 0.5)
- if distance < safedist then
- print("Target too close → disabling shield")
- trySpeak("Target too close, shield disabled")
- shield.enable(false)
- else
- shield.translation(tx, ty, tz)
- trySpeak("Distance " .. rd)
- end
- elseif event == "rednet_message"
- and p3 == "SMiner"
- and p2 == "Toggle" then
- -- **only flip the shield**; let stateMonitor announce it
- toggledOn = not toggledOn
- local fx, fy, fz = shield.getLocalPosition()
- local _,_,_,distance = shieldOffset(lastX, lastY, lastZ, fx, fy, fz)
- if toggledOn and distance > safedist then
- shield.enable(true)
- else
- shield.enable(false)
- end
- elseif event == "key" and p1 == keys.c then
- print("C key pressed → running 'conf'")
- shell.run("conf")
- end
- end
- end
- -- 12) Run all three loops in parallel
- parallel.waitForAny(stateMonitor, rednetListener, mainControl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement