Advertisement
jaklsfjlsak

试制护盾采矿723 v3 多激光 debug 报点

Jul 26th, 2023 (edited)
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 lasers = peripheral.getNames() -- get all peripherals
  9. local speaker = peripheral.find("warpdriveSpeaker")
  10. local lever = "front"
  11. local safedist = 15
  12.  
  13. -- filter only warpdriveLaserCamera peripherals
  14. for i=#lasers, 1, -1 do
  15.     if peripheral.getType(lasers[i]) ~= "warpdriveLaserCamera" then
  16.         table.remove(lasers, i)
  17.     else
  18.         peripheral.wrap(lasers[i]).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.  
  42. while true do
  43.     for _, laserName in ipairs(lasers) do
  44.         local laser = peripheral.wrap(laserName)
  45.         local event, key = os.pullEvent()
  46.         os.sleep(0.1)
  47.  
  48.         if event == "key" then
  49.             if key == 46 then
  50.                 print("C key pressed, running 'conf' script...")
  51.                 shell.run("conf")
  52.             end
  53.         end
  54.  
  55.         if event == "laserScanning" then
  56.             local type, lx, ly, lz, block = laser.getScanResult()
  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.  
  87.  
  88.             elseif distance > safedist then
  89.                 shield.translation(tx, ty, tz)
  90.                 speaker.speak("Target Dis: " .. tostring(distance))
  91.                 speaker.speak("Offset Coordinates: x=" .. tostring(t1x) .. ", y=" .. tostring(t1y) .. ", z=" .. tostring(t1z))
  92.                 speaker.speak("Laser Coordinates: x=" .. tostring(lx) .. ", y=" .. tostring(ly) .. ", z=" .. tostring(lz))
  93.             end
  94.         elseif event == "redstone" then
  95.             local _, _, _, distance = shieldOffset()
  96.             local on = redstone.getAnalogInput(lever)
  97.             if on > 6 and distance > safedist then
  98.                 shield.enable(true)
  99.             elseif on > 6 and distance < safedist then
  100.                 shield.enable(false)
  101.                 print("Target is too Close! Shield Disabled!")
  102.             elseif on < 5 then
  103.                 shield.enable(false)
  104.             end
  105.         end
  106.     end
  107. end
  108.  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement