Advertisement
jaklsfjlsak

4-25-25水平采矿 广播 状态监控

Apr 26th, 2025
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Combined Shield Control & Monitor (“ztc+”)
  2.  
  3. -- 1) Configuration
  4. local OffsetValue = 3
  5. local safedist    = 15
  6. local lever       = "front"
  7.  
  8. -- 2) Wrap peripherals
  9. local shield  = peripheral.find("warpdriveForceFieldProjector")
  10. local speaker = peripheral.find("warpdriveSpeaker")
  11. assert(shield, "ForceFieldProjector not found!")
  12.  
  13. -- 3) Speak helper (no-op if no speaker)
  14. local function trySpeak(msg)
  15.   if speaker then speaker.speak(msg) end
  16. end
  17.  
  18. -- 4) Compute the offset point and distance
  19. local function shieldOffset(lx, ly, lz, fx, fy, fz)
  20.   local dx, dy, dz = lx - fx, ly - fy, lz - fz
  21.   local distance = math.sqrt(dx*dx + dy*dy + dz*dz)
  22.   dx, dy, dz = dx/distance, dy/distance, dz/distance
  23.   return
  24.     lx + dx * OffsetValue,
  25.     ly + dy * OffsetValue,
  26.     lz + dz * OffsetValue,
  27.     distance
  28. end
  29.  
  30. -- 5) Ensure config script exists
  31. if not fs.exists("conf") then
  32.   shell.run("pastebin get 38M5cNbZ conf")
  33.   term.clear()
  34. end
  35.  
  36. -- 6) Open all modems for Rednet
  37. for _, side in ipairs(peripheral.getNames()) do
  38.   if peripheral.getType(side) == "modem" then
  39.     rednet.open(side)
  40.   end
  41. end
  42.  
  43. -- 7) Announce readiness
  44. print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
  45. trySpeak("Control System Online")
  46.  
  47. -- 8) Determine shield range multiplier
  48. local _, upgrades = shield.getUpgrades()
  49. local Size = ({
  50.   ["1/4 x Range"] = 16,
  51.   ["2/4 x Range"] = 32,
  52.   ["3/4 x Range"] = 48,
  53.   ["4/4 x Range"] = 64,
  54.   ["0/4 x Range"] = 0,
  55. })[upgrades:match("(%d/4 x Range)")] or 32
  56.  
  57. -- 9) State‐monitor coroutine: watch for shield enable/disable changes
  58. local function stateMonitor()
  59.   local prevEnabled = shield.enable()
  60.   while true do
  61.     local enabled = shield.enable()
  62.     if enabled ~= prevEnabled then
  63.       if enabled then
  64.         print("Shield state changed → ENABLED")
  65.         trySpeak("Shield enabled")
  66.         rednet.broadcast("StatON", "SMiner")
  67.       else
  68.         print("Shield state changed → DISABLED")
  69.         trySpeak("Shield disabled")
  70.         rednet.broadcast("StatOFF", "SMiner")
  71.       end
  72.       prevEnabled = enabled
  73.     end
  74.     os.sleep(0.5)
  75.   end
  76. end
  77.  
  78. -- 10) Rednet‐listener coroutine: respond to "check" queries
  79. local function rednetListener()
  80.   while true do
  81.     local _, message = rednet.receive("SMiner")
  82.     if message == "check" then
  83.       local enabled = shield.enable()
  84.       if enabled then
  85.         print("Received check → Shield currently ENABLED")
  86.         trySpeak("Shield is enabled")
  87.         rednet.broadcast("StatON", "SMiner")
  88.       else
  89.         print("Received check → Shield currently DISABLED")
  90.         trySpeak("Shield is disabled")
  91.         rednet.broadcast("StatOFF", "SMiner")
  92.       end
  93.     end
  94.   end
  95. end
  96.  
  97. -- 11) Main control coroutine: handle incoming target coords, redstone, config
  98. local function mainControl()
  99.   local lastX, lastY, lastZ = 0,0,0
  100.   while true do
  101.     local event, p1, p2, p3 = os.pullEventRaw()
  102.     if event == "rednet_message" and p3 == "SMineBroadcast" then
  103.       -- unpack broadcast as p1=sender, p2=table{x,y,z}, p3=protocol
  104.       lastX, lastY, lastZ = p2.x, p2.y, p2.z
  105.  
  106.       local fx, fy, fz = shield.getLocalPosition()
  107.       local t1x, t1y, t1z, distance = shieldOffset(lastX, lastY, lastZ, fx, fy, fz)
  108.  
  109.       -- normalize for shield.translation
  110.       local tx = (t1x - fx) / Size
  111.       local ty = (t1y - fy) / Size
  112.       local tz = (t1z - fz) / Size
  113.  
  114.       -- rounded for speech
  115.       local rd   = math.floor(distance + 0.5)
  116.       local rt1x = math.floor(t1x     + 0.5)
  117.       local rt1y = math.floor(t1y     + 0.5)
  118.       local rt1z = math.floor(t1z     + 0.5)
  119.  
  120.       if distance < safedist then
  121.         print("Target is too Close! Shield Disabled!")
  122.         trySpeak("Target is too close! Shield disabled!")
  123.         shield.enable(false)
  124.         trySpeak("Target distance " .. rd)
  125.         trySpeak(("Offset coords x=%d, y=%d, z=%d"):format(rt1x,rt1y,rt1z))
  126.         trySpeak(("Target coords x=%d, y=%d, z=%d"):format(lastX,lastY,lastZ))
  127.       else
  128.         shield.translation(tx, ty, tz)
  129.         trySpeak("Target distance " .. rd)
  130.         trySpeak(("Offset coords x=%d, y=%d, z=%d"):format(rt1x,rt1y,rt1z))
  131.         trySpeak(("Target coords x=%d, y=%d, z=%d"):format(lastX,lastY,lastZ))
  132.       end
  133.  
  134.     elseif event == "redstone" then
  135.       -- front lever toggles shield (with safedist guard)
  136.       local fx, fy, fz = shield.getLocalPosition()
  137.       local _,_,_,distance = shieldOffset(lastX, lastY, lastZ, fx, fy, fz)
  138.       local on = redstone.getAnalogInput(lever)
  139.  
  140.       if on > 6 and distance > safedist then
  141.         shield.enable(true)
  142.       elseif on > 6 then
  143.         print("Target is too Close! Shield Disabled!")
  144.         trySpeak("Target is too close! Shield disabled!")
  145.         shield.enable(false)
  146.       else
  147.         shield.enable(false)
  148.       end
  149.  
  150.     elseif event == "key" and p1 == keys.c then
  151.       print("C key pressed → running 'conf'…")
  152.       shell.run("conf")
  153.     end
  154.   end
  155. end
  156.  
  157. -- 12) Spin up all three coroutines in parallel
  158. parallel.waitForAny(stateMonitor, rednetListener, mainControl)
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement