Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Warp Drive Shield Control via Rednet (“ztc” replacing laserScanning with SMineBroadcast)
- local OffsetValue = 3
- local shield = peripheral.find("warpdriveForceFieldProjector")
- local speaker = peripheral.find("warpdriveSpeaker")
- local lever = "front"
- local safedist = 15
- local lastLx, lastLy, lastLz = 0, 0, 0
- -- Speak only if we have a speaker attached
- local function trySpeak(msg)
- if speaker then speaker.speak(msg) end
- end
- -- Compute the offset point and distance
- local function shieldOffset(lx, ly, lz, fx, fy, fz)
- local dx = lx - fx
- local dy = ly - fy
- local dz = lz - fz
- local distance = math.sqrt(dx*dx + dy*dy + dz*dz)
- dx, dy, dz = dx/distance, dy/distance, dz/distance
- local t1x = lx + dx * OffsetValue
- local t1y = ly + dy * OffsetValue
- local t1z = lz + dz * OffsetValue
- return t1x, t1y, t1z, distance
- end
- -- Ensure configuration script is available
- if not fs.exists("conf") then
- shell.run("pastebin get 38M5cNbZ conf")
- term.clear()
- end
- -- Open all modems for rednet
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- end
- end
- print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
- trySpeak("Control System Online")
- -- 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 -- fallback to 32
- -- Main event loop: handle rednet messages, redstone, and config key
- while true do
- local event, a, b, c = os.pullEventRaw()
- if event == "rednet_message" and c == "SMineBroadcast" then
- -- Received new target coordinates
- lastLx, lastLy, lastLz = b.x, b.y, b.z
- local fx, fy, fz = shield.getLocalPosition()
- local t1x, t1y, t1z, distance = shieldOffset(lastLx, lastLy, lastLz, fx, fy, fz)
- local tx = (t1x - fx) / Size
- local ty = (t1y - fy) / Size
- local tz = (t1z - fz) / Size
- -- Round values for speaking
- 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 Dis: " .. rd)
- trySpeak("Offset Coordinates: x=" .. rt1x .. ", y=" .. rt1y .. ", z=" .. rt1z)
- trySpeak("Target Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz)
- else
- shield.translation(tx, ty, tz)
- trySpeak("Target Dis: " .. rd)
- trySpeak("Offset Coordinates: x=" .. rt1x .. ", y=" .. rt1y .. ", z=" .. rt1z)
- trySpeak("Target Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz)
- end
- elseif event == "redstone" then
- -- Toggle shields via redstone and safe distance
- local fx, fy, fz = shield.getLocalPosition()
- local _, _, _, distance = shieldOffset(lastLx, lastLy, lastLz, fx, fy, fz)
- local on = redstone.getAnalogInput(lever)
- if on > 6 and distance > safedist then
- shield.enable(true)
- elseif on > 6 and distance < safedist 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 a == keys.c then
- -- Run configuration
- print("C key pressed, running 'conf' script...")
- shell.run("conf")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement