Advertisement
jaklsfjlsak

试制护盾采矿723 v1

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