Advertisement
jaklsfjlsak

4-25-25水平采矿 广播

Apr 26th, 2025
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Warp Drive Shield Control via Rednet (“ztc” replacing laserScanning with SMineBroadcast)
  2.  
  3. local OffsetValue = 3
  4. local shield      = peripheral.find("warpdriveForceFieldProjector")
  5. local speaker     = peripheral.find("warpdriveSpeaker")
  6. local lever       = "front"
  7. local safedist    = 15
  8. local lastLx, lastLy, lastLz = 0, 0, 0
  9.  
  10. -- Speak only if we have a speaker attached
  11. local function trySpeak(msg)
  12.     if speaker then speaker.speak(msg) end
  13. end
  14.  
  15. -- Compute the offset point and distance
  16. local function shieldOffset(lx, ly, lz, fx, fy, fz)
  17.     local dx = lx - fx
  18.     local dy = ly - fy
  19.     local dz = lz - fz
  20.     local distance = math.sqrt(dx*dx + dy*dy + dz*dz)
  21.     dx, dy, dz = dx/distance, dy/distance, dz/distance
  22.     local t1x = lx + dx * OffsetValue
  23.     local t1y = ly + dy * OffsetValue
  24.     local t1z = lz + dz * OffsetValue
  25.     return t1x, t1y, t1z, distance
  26. end
  27.  
  28. -- Ensure configuration script is available
  29. if not fs.exists("conf") then
  30.     shell.run("pastebin get 38M5cNbZ conf")
  31.     term.clear()
  32. end
  33.  
  34. -- Open all modems for rednet
  35. for _, side in ipairs(peripheral.getNames()) do
  36.     if peripheral.getType(side) == "modem" then
  37.         rednet.open(side)
  38.     end
  39. end
  40.  
  41. print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
  42. trySpeak("Control System Online")
  43.  
  44. -- Determine shield range multiplier
  45. local _, upgrades = shield.getUpgrades()
  46. local Size = ({
  47.     ["1/4 x Range"] = 16,
  48.     ["2/4 x Range"] = 32,
  49.     ["3/4 x Range"] = 48,
  50.     ["4/4 x Range"] = 64,
  51.     ["0/4 x Range"] = 0,
  52. })[upgrades:match("(%d/4 x Range)")] or 32  -- fallback to 32
  53.  
  54. -- Main event loop: handle rednet messages, redstone, and config key
  55. while true do
  56.     local event, a, b, c = os.pullEventRaw()
  57.  
  58.     if event == "rednet_message" and c == "SMineBroadcast" then
  59.         -- Received new target coordinates
  60.         lastLx, lastLy, lastLz = b.x, b.y, b.z
  61.  
  62.         local fx, fy, fz = shield.getLocalPosition()
  63.         local t1x, t1y, t1z, distance = shieldOffset(lastLx, lastLy, lastLz, fx, fy, fz)
  64.  
  65.         local tx = (t1x - fx) / Size
  66.         local ty = (t1y - fy) / Size
  67.         local tz = (t1z - fz) / Size
  68.  
  69.         -- Round values for speaking
  70.         local rd   = math.floor(distance + 0.5)
  71.         local rt1x = math.floor(t1x     + 0.5)
  72.         local rt1y = math.floor(t1y     + 0.5)
  73.         local rt1z = math.floor(t1z     + 0.5)
  74.  
  75.         if distance < safedist then
  76.             print("Target is too Close! Shield Disabled!")
  77.             trySpeak("Target is too Close! Shield Disabled!")
  78.             shield.enable(false)
  79.             trySpeak("Target Dis: " .. rd)
  80.             trySpeak("Offset Coordinates: x=" .. rt1x .. ", y=" .. rt1y .. ", z=" .. rt1z)
  81.             trySpeak("Target Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz)
  82.         else
  83.             shield.translation(tx, ty, tz)
  84.             trySpeak("Target Dis: " .. rd)
  85.             trySpeak("Offset Coordinates: x=" .. rt1x .. ", y=" .. rt1y .. ", z=" .. rt1z)
  86.             trySpeak("Target Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz)
  87.         end
  88.  
  89.     elseif event == "redstone" then
  90.         -- Toggle shields via redstone and safe distance
  91.         local fx, fy, fz = shield.getLocalPosition()
  92.         local _, _, _, distance = shieldOffset(lastLx, lastLy, lastLz, fx, fy, fz)
  93.         local on = redstone.getAnalogInput(lever)
  94.  
  95.         if on > 6 and distance > safedist then
  96.             shield.enable(true)
  97.         elseif on > 6 and distance < safedist then
  98.             print("Target is too Close! Shield Disabled!")
  99.             trySpeak("Target is too Close! Shield Disabled!")
  100.             shield.enable(false)
  101.         else
  102.             shield.enable(false)
  103.         end
  104.  
  105.     elseif event == "key" and a == keys.c then
  106.         -- Run configuration
  107.         print("C key pressed, running 'conf' script...")
  108.         shell.run("conf")
  109.     end
  110. end
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement