Advertisement
jaklsfjlsak

护盾投射7/24

Jul 25th, 2023
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 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 = 5 -- How much you want your shield to be configured forward
  7. local shield = peripheral.find("warpdriveForceFieldProjector")
  8. local laser = peripheral.find("warpdriveLaserCamera")
  9. local lever = "front" -- format: front,back,left,right,top,bottom ONLY
  10. local safedist = 15 -- Set Safe Distance
  11. laser.beamFrequency(1420) -- Sets the frequency of the laser to 1420
  12.  
  13. -- initial message
  14. print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
  15.  
  16. local _, upgrades = shield.getUpgrades()
  17.  
  18. local lx, ly, lz = nil, nil, nil
  19.  
  20. -- Function definitions moved out of the loop
  21.  
  22. function getRange()
  23.     if upgrades:match("1/4 x Range") then
  24.         return 1
  25.     elseif upgrades:match("2/4 x Range") then
  26.         return 2
  27.     elseif upgrades:match("3/4 x Range") then
  28.         return 3
  29.     elseif upgrades:match("4/4 x Range") then
  30.         return 4
  31.     else -- Added else clause to handle no match case
  32.         return 0
  33.     end
  34. end
  35.  
  36. function shieldOffset()
  37.     if lx and ly and lz then
  38.         -- Shield position
  39.         local fx, fy, fz = shield.getLocalPosition()
  40.  
  41.         -- Calculate direction vector from shield position to laser scan target position
  42.         local dx = lx - fx
  43.         local dy = ly - fy
  44.         local dz = lz - fz
  45.  
  46.         -- Calculate the length of the direction vector
  47.         local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  48.  
  49.         -- Normalize the direction vector to have a length of 1 (unit vector)
  50.         dx = dx / distance
  51.         dy = dy / distance
  52.         dz = dz / distance
  53.  
  54.         -- Calculate the final target position by extending the direction vector by OffsetValue
  55.         local t1x = lx + dx * OffsetValue
  56.         local t1y = ly + dy * OffsetValue
  57.         local t1z = lz + dz * OffsetValue
  58.  
  59.         return t1x, t1y, t1z, distance -- Return the final target position and distance
  60.     end
  61. end
  62.  
  63. Size = getRange() * 16
  64.  
  65. while true do
  66.     os.sleep(0.5)
  67.     local event, key = os.pullEvent()
  68.  
  69.     if event == "key" then
  70.         -- keys are represented by numbers, the number for 'C' is 46
  71.         if key == 46 then
  72.             print("C key pressed, running 'conf' script...")
  73.             shell.run("conf")
  74.         end
  75.     elseif event == "laserScanning" then
  76.         -- Laser scan target position
  77.         local type, lx, ly, lz, block = laser.getScanResult()
  78.         lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz) -- Convert to numbers
  79.  
  80.         local t1x, t1y, t1z, distance = shieldOffset()
  81.  
  82.         tx = (t1x - fx) / Size
  83.         ty = (t1y - fy) / Size
  84.         tz = (t1z - fz) / Size
  85.  
  86.         if distance < safedist then
  87.             print("Target is too Close! Shield Disabled!")
  88.             shield.enable(false) -- Disable shield if too close
  89.         elseif distance > safedist then
  90.             shield.translation(tx, ty, tz)
  91.         end
  92.     elseif event == "redstone" then
  93.         local _, _, _, distance = shieldOffset()
  94.         local on = redstone.getAnalogInput(lever)
  95.         if on > 6 and distance > safedist then
  96.             shield.enable(true)
  97.         elseif on > 6 and distance < safedist then
  98.             shield.enable(false)
  99.             print("Target is too Close! Shield Disabled!")
  100.         elseif on < 5 then
  101.             shield.enable(false)
  102.         end
  103.     end
  104. end
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement