Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Combined Shield Control & Monitor (“ztc+”)
- -- 1) Configuration
- local OffsetValue = 3
- local safedist = 15
- local lever = "front"
- -- 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 the 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 Redstone To Toggle Shields, Press C to Configure")
- trySpeak("Control System Online")
- -- 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: watch for shield enable/disable changes
- local function stateMonitor()
- local prevEnabled = shield.enable()
- while true do
- local enabled = shield.enable()
- if enabled ~= prevEnabled then
- if enabled then
- print("Shield state changed → ENABLED")
- trySpeak("Shield enabled")
- rednet.broadcast("StatON", "SMiner")
- else
- print("Shield state changed → DISABLED")
- trySpeak("Shield disabled")
- rednet.broadcast("StatOFF", "SMiner")
- end
- prevEnabled = enabled
- end
- os.sleep(0.5)
- end
- end
- -- 10) Rednet‐listener coroutine: respond to "check" queries
- local function rednetListener()
- while true do
- local _, message = rednet.receive("SMiner")
- if message == "check" then
- local enabled = shield.enable()
- if enabled then
- print("Received check → Shield currently ENABLED")
- trySpeak("Shield is enabled")
- rednet.broadcast("StatON", "SMiner")
- else
- print("Received check → Shield currently DISABLED")
- trySpeak("Shield is disabled")
- rednet.broadcast("StatOFF", "SMiner")
- end
- end
- end
- end
- -- 11) Main control coroutine: handle incoming target coords, redstone, config
- local function mainControl()
- local lastX, lastY, lastZ = 0,0,0
- while true do
- local event, p1, p2, p3 = os.pullEventRaw()
- if event == "rednet_message" and p3 == "SMineBroadcast" then
- -- unpack broadcast as p1=sender, p2=table{x,y,z}, p3=protocol
- 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)
- -- normalize for shield.translation
- local tx = (t1x - fx) / Size
- local ty = (t1y - fy) / Size
- local tz = (t1z - fz) / Size
- -- rounded for speech
- local rd = math.floor(distance + 0.5)
- local rt1x = math.floor(t1x + 0.5)
- local rt1y = math.floor(t1y + 0.5)
- local rt1z = math.floor(t1z + 0.5)
- if distance < safedist then
- print("Target is too Close! Shield Disabled!")
- trySpeak("Target is too close! Shield disabled!")
- shield.enable(false)
- trySpeak("Target distance " .. rd)
- trySpeak(("Offset coords x=%d, y=%d, z=%d"):format(rt1x,rt1y,rt1z))
- trySpeak(("Target coords x=%d, y=%d, z=%d"):format(lastX,lastY,lastZ))
- else
- shield.translation(tx, ty, tz)
- trySpeak("Target distance " .. rd)
- trySpeak(("Offset coords x=%d, y=%d, z=%d"):format(rt1x,rt1y,rt1z))
- trySpeak(("Target coords x=%d, y=%d, z=%d"):format(lastX,lastY,lastZ))
- end
- elseif event == "redstone" then
- -- front lever toggles shield (with safedist guard)
- local fx, fy, fz = shield.getLocalPosition()
- local _,_,_,distance = shieldOffset(lastX, lastY, lastZ, fx, fy, fz)
- local on = redstone.getAnalogInput(lever)
- if on > 6 and distance > safedist then
- shield.enable(true)
- elseif on > 6 then
- print("Target is too Close! Shield Disabled!")
- trySpeak("Target is too close! Shield disabled!")
- shield.enable(false)
- 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) Spin up all three coroutines in parallel
- parallel.waitForAny(stateMonitor, rednetListener, mainControl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement