Advertisement
jaklsfjlsak

护盾炮台

Mar 29th, 2025 (edited)
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | None | 0 0
  1. local allowed = {"Ares_Xiao_Hu", "Destiney"}
  2.  
  3. if not fs.exists("conf") then
  4.   shell.run("pastebin get 38M5cNbZ conf")
  5.   term.clear()
  6. end
  7.  
  8. -- Check if a player is whitelisted.
  9. local function isWhitelisted(playerName)
  10.   for i = 1, #allowed do
  11.     if playerName == allowed[i] then
  12.       return true
  13.     end
  14.   end
  15.   return false
  16. end
  17.  
  18. -- Find the shield peripheral.
  19. local shield = peripheral.find("warpdriveForceFieldProjector")
  20. if not shield then
  21.   error("Shield not found!")
  22. end
  23.  
  24. -- Find and wrap all warpdrive cameras.
  25. local allPeripherals = peripheral.getNames()
  26. local cameras = {}
  27. for _, side in pairs(allPeripherals) do
  28.   if peripheral.getType(side) == "warpdriveCamera" then
  29.     print("Camera found on " .. side)
  30.     table.insert(cameras, peripheral.wrap(side))
  31.   end
  32. end
  33.  
  34. print("Control System Online. Press C to Configure.")
  35.  
  36. -- Determine shield range based on upgrades.
  37. local _, upgrades = shield.getUpgrades()
  38. local Size
  39. if upgrades:match("1/4 x Range") then
  40.     Size = 16
  41. elseif upgrades:match("2/4 x Range") then
  42.     Size = 32
  43. elseif upgrades:match("3/4 x Range") then
  44.     Size = 48
  45. elseif upgrades:match("4/4 x Range") then
  46.     Size = 64
  47. elseif upgrades:match("0/4 x Range") then
  48.     Size = 0
  49. else
  50.     Size = 32  -- default fallback
  51. end
  52.  
  53. -- An offset value to adjust the target coordinates.
  54. local OffsetValue = -1
  55.  
  56. -- Calculate the adjusted target coordinates.
  57. local function shieldOffset(lx, ly, lz, fx, fy, fz)
  58.     local dx = lx - fx
  59.     local dy = ly - fy
  60.     local dz = lz - fz
  61.     local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  62.     -- Normalize the vector.
  63.     dx = dx / distance
  64.     dy = dy / distance
  65.     dz = dz / distance
  66.     local t1x = lx + dx * OffsetValue
  67.     local t1y = ly + dy * OffsetValue
  68.     local t1z = lz + dz * OffsetValue
  69.     return t1x, t1y, t1z, distance
  70. end
  71.  
  72. -- Main shield translation loop.
  73. local function shieldLoop()
  74.   while true do
  75.     if not redstone.getInput("bottom") then
  76.             print("Shield Disabled")
  77.             shield.enable(false)
  78.      else
  79.       shield.enable(true)
  80.       for _, camera in ipairs(cameras) do
  81.         local count = camera.getResultsCount()
  82.         if count and count > 0 then
  83.           for i = 0, count - 1 do
  84.             local success, entityType, playerName, x, y, z = camera.getResult(i)
  85.             if success and entityType == "minecraft:player" and not isWhitelisted(playerName) then
  86.               -- Round the coordinates.
  87.               x = math.floor(x * 10) / 10
  88.               y = math.floor((y * 10) / 10) - 1
  89.               z = math.floor(z * 10) / 10
  90.               print(playerName .. " detected at (" .. x .. ", " .. y .. ", " .. z .. ")")
  91.              
  92.               -- Get the shield's current local position.
  93.               local fx, fy, fz = shield.getLocalPosition()
  94.               -- Compute the adjusted target position.
  95.               local t1x, t1y, t1z, distance = shieldOffset(x, y, z, fx, fy, fz)
  96.              
  97.               -- Scale the translation by the shield's Size.
  98.               local tx = (t1x - fx) / Size
  99.               local ty = (t1y - fy) / Size
  100.               local tz = (t1z - fz) / Size
  101.              
  102.               shield.translation(tx, ty, tz)
  103.               print("Shield translated to (" .. tx .. ", " .. ty .. ", " .. tz .. ")")
  104.             end
  105.           end
  106.         end
  107.       end
  108.     end
  109.     os.sleep(0.1)
  110.   end
  111. end
  112.  
  113. -- Key listener loop for configuration.
  114. local function keyListener()
  115.   while true do
  116.     local event, key = os.pullEvent("key")
  117.     if key == 46 then  -- Key code 46 corresponds to C.
  118.       print("C key pressed, launching 'conf' script...")
  119.       parallel.waitForAny(
  120.         function() shell.run("conf") end,
  121.         function() while true do os.sleep(0.1) end end
  122.       )
  123.     end
  124.   end
  125. end
  126.  
  127. -- Run both loops concurrently.
  128. parallel.waitForAny(shieldLoop, keyListener)
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement