Advertisement
jaklsfjlsak

试制护盾采矿723 v2

Jul 25th, 2023 (edited)
870
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 -- 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. --- this function is really janky but I don't want to make it better
  18. function getRange()
  19.     if upgrades:match("1/4 x Range") then
  20.         return 1
  21.     elseif upgrades:match("2/4 x Range") then
  22.         return 2
  23.     elseif upgrades:match("3/4 x Range") then
  24.         return 3
  25.     elseif upgrades:match("4/4 x Range") then
  26.         return 4
  27.     elseif upgrades:match("0/4 x Range") then
  28.         return 0
  29.     end
  30. end
  31.  
  32. Size = getRange() * 16
  33.  
  34. while true do
  35.     os.sleep(0.5)
  36.     local event, key = os.pullEvent()
  37.  
  38.     if event == "key" then
  39.         -- keys are represented by numbers, the number for 'C' is 46
  40.         if key == 46 then
  41.             print("C key pressed, running 'conf' script...")
  42.             shell.run("conf")
  43.         end
  44.     elseif event == "laserScanning" then
  45.         -- Laser scan target position
  46.         local type, lx, ly, lz, block = laser.getScanResult()
  47.         lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz) -- Convert to numbers
  48.  
  49.         -- Shield position
  50.         local fx, fy, fz = shield.getLocalPosition()
  51.  
  52.         function shieldOffset()
  53.             -- Calculate direction vector from shield position to laser scan target position
  54.             local dx = lx - fx
  55.             local dy = ly - fy
  56.             local dz = lz - fz
  57.  
  58.             -- Calculate the length of the direction vector
  59.             local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  60.  
  61.             -- Normalize the direction vector to have a length of 1 (unit vector)
  62.             dx = dx / distance
  63.             dy = dy / distance
  64.             dz = dz / distance
  65.  
  66.             -- Calculate the final target position by extending the direction vector by OffsetValue
  67.             local t1x = lx + dx * OffsetValue
  68.             local t1y = ly + dy * OffsetValue
  69.             local t1z = lz + dz * OffsetValue
  70.  
  71.             return t1x, t1y, t1z, distance -- Return the final target position and 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) -- Disable shield if too close
  83.             elseif distance > safedist and distance > Size then
  84.             print("Target is too Far! Shield Disabled!")
  85.             shield.enable(false) -- Disable shield if too far
  86.         elseif distance > safedist and distance < Size then
  87.             shield.translation(tx, ty, tz)
  88.         end
  89.     elseif event == "redstone" then
  90.         local _, _, _, distance = shieldOffset()
  91.         local on = redstone.getAnalogInput(lever)
  92.         if on > 6 and distance > safedist and distance < Size then
  93.             shield.enable(true)
  94.         elseif on > 6 and distance > safedist and distance > Size then
  95.             print("Target is too Far! Shield Disabled!")
  96.             shield.enable(false) -- Disable shield if too far
  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