Advertisement
jaklsfjlsak

试制护盾采矿726修复4

Jul 26th, 2023 (edited)
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. if fs.exists("conf") == false then
  2.     shell.run("pastebin get 38M5cNbZ conf")
  3.     term.clear()
  4. end
  5.  
  6. local OffsetValue = 3
  7. local shield = peripheral.find("warpdriveForceFieldProjector")
  8. local speaker = peripheral.find("warpdriveSpeaker")
  9. local lever = "front"
  10. local safedist = 15
  11.  
  12. -- Get a list of all laser names
  13. local lasers = {}
  14. for _, name in ipairs(peripheral.getNames()) do
  15.     if peripheral.getType(name) == "warpdriveLaserCamera" then
  16.         print("Laser found: "..name)
  17.         lasers[name] = peripheral.wrap(name)
  18.         lasers[name].beamFrequency(1420)
  19.     end
  20. end
  21.  
  22. print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
  23.  
  24. local _, upgrades = shield.getUpgrades()
  25.  
  26. function getRange()
  27.     if upgrades:match("1/4 x Range") then
  28.         return 1
  29.     elseif upgrades:match("2/4 x Range") then
  30.         return 2
  31.     elseif upgrades:match("3/4 x Range") then
  32.         return 3
  33.     elseif upgrades:match("4/4 x Range") then
  34.         return 4
  35.     elseif upgrades:match("0/4 x Range") then
  36.         return 0
  37.     end
  38. end
  39.  
  40. Size = getRange() * 16
  41. local lastLaser -- added to keep track of the last laser that fired a scan
  42.  
  43. while true do
  44.     print("Waiting for an event...")
  45.     local event, peripheralName, lx, ly, lz = os.pullEvent()
  46.     print("Event received: "..event)
  47.  
  48.     if event == "key" and peripheralName == 46 then
  49.         print("C key pressed, running 'conf' script...")
  50.         shell.run("conf")
  51.     end
  52.  
  53.     if event == "laserScanning" and lasers[peripheralName] then
  54.         print("Received laser scanning event from: "..peripheralName)
  55.         lastLaser = lasers[peripheralName] -- update lastLaser to the laser that fired the scan
  56.         local type, lx, ly, lz, block = lastLaser.getScanResult() -- use lastLaser to collect scan results
  57.         lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz)
  58.         local fx, fy, fz = shield.getLocalPosition()
  59.  
  60.         function shieldOffset()
  61.             local dx = lx - fx
  62.             local dy = ly - fy
  63.             local dz = lz - fz
  64.             local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  65.             dx = dx / distance
  66.             dy = dy / distance
  67.             dz = dz / distance
  68.             local t1x = lx + dx * OffsetValue
  69.             local t1y = ly + dy * OffsetValue
  70.             local t1z = lz + dz * OffsetValue
  71.             return t1x, t1y, t1z, distance
  72.         end
  73.  
  74.         local t1x, t1y, t1z, distance = shieldOffset()
  75.  
  76.         tx = (t1x - fx) / Size
  77.         ty = (t1y - fy) / Size
  78.         tz = (t1z - fz) / Size
  79.  
  80.         if distance < safedist then
  81.             print("Target is too Close! Shield Disabled!")
  82.             shield.enable(false)
  83.             speaker.speak("Target Dis: " .. tostring(distance))
  84.             speaker.speak("Offset Coordinates: x=" .. tostring(t1x) .. ", y=" .. tostring(t1y) .. ", z=" .. tostring(t1z))
  85.             speaker.speak("Laser Coordinates: x=" .. tostring(lx) .. ", y=" .. tostring(ly) .. ", z=" .. tostring(lz))
  86.         elseif distance > safedist then
  87.             shield.translation(tx, ty, tz)
  88.             speaker.speak("Target Dis: " .. tostring(distance))
  89.             speaker.speak("Offset Coordinates: x=" .. tostring(t1x) .. ", y=" .. tostring(t1y) .. ", z=" .. tostring(t1z))
  90.             speaker.speak("Laser Coordinates: x=" .. tostring(lx) .. ", y=" .. tostring(ly) .. ", z=" .. tostring(lz))
  91.         end
  92.     end
  93.  
  94.     if event == "redstone" and lastLaser then -- make sure lastLaser is set
  95.         print("Received redstone event")
  96.         local _, _, _, distance = shieldOffset()
  97.         local on = redstone.getAnalogInput(lever)
  98.         if on > 6 and distance > safedist then
  99.             shield.enable(true)
  100.         elseif on > 6 and distance < safedist then
  101.             shield.enable(false)
  102.             print("Target is too Close! Shield Disabled!")
  103.         elseif on < 5 then
  104.             shield.enable(false)
  105.         end
  106.     end
  107. end
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement