Advertisement
jaklsfjlsak

三维 激光制导跳船 gpt

Apr 14th, 2025 (edited)
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. local ship = peripheral.find("warpdriveShipCore")
  2. local lasers = peripheral.getNames()
  3.  
  4. -- Filter for warpdriveLaserCameras and set their beam frequency.
  5. for i = #lasers, 1, -1 do
  6.     if peripheral.getType(lasers[i]) ~= "warpdriveLaserCamera" then
  7.         table.remove(lasers, i)
  8.     else
  9.         peripheral.wrap(lasers[i]).beamFrequency(1420)
  10.     end
  11. end
  12.  
  13. -- Define the offset distance (in blocks).
  14. local offsetDistance = 18
  15. -- Set invert to true if your testing shows the ship moves in the opposite direction
  16. -- than expected along the X and Z axes.
  17. local invert = true
  18.  
  19. print("Auto Navigation System Online")
  20. print("Offset Distance = " .. offsetDistance)
  21. if invert then
  22.     print("Using inverted offset calculation (moving to the opposite side from the ship).")
  23. else
  24.     print("Using standard offset calculation (moving from target toward ship).")
  25. end
  26.  
  27. while true do
  28.     local event, laserName, lx, ly, lz = os.pullEvent("laserScanning")
  29.    
  30.     -- Convert target coordinates to numbers.
  31.     local targetX = tonumber(lx)
  32.     local targetY = tonumber(ly)
  33.     local targetZ = tonumber(lz)
  34.     print("LaserScanning event received:")
  35.     print("Target: (" .. targetX .. ", " .. targetY .. ", " .. targetZ .. ")")
  36.    
  37.     -- Get current ship core position.
  38.     local currentX, currentY, currentZ = ship.getLocalPosition()
  39.    
  40.     -- Calculate the vector from the target to the ship.
  41.     local dx = currentX - targetX
  42.     local dy = currentY - targetY
  43.     local dz = currentZ - targetZ
  44.    
  45.     local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  46.     if distance == 0 then
  47.       print("Warning: The ship and target are at the same location. Skipping movement.")
  48.     else
  49.       -- Compute the unit vector from target to ship.
  50.       local ux = dx / distance
  51.       local uy = dy / distance
  52.       local uz = dz / distance
  53.  
  54.       local newX, newY, newZ
  55.       if invert then
  56.          -- If invert is true, choose the point on the opposite side of the target.
  57.          newX = targetX - ux * offsetDistance
  58.          newY = targetY - uy * offsetDistance
  59.          newZ = targetZ - uz * offsetDistance
  60.       else
  61.          -- Otherwise, choose the point from the target toward the ship.
  62.          newX = targetX + ux * offsetDistance
  63.          newY = targetY + uy * offsetDistance
  64.          newZ = targetZ + uz * offsetDistance
  65.       end
  66.  
  67.       -- Round the new coordinates to whole numbers.
  68.       newX = math.floor(newX + 0.5)
  69.       newY = math.floor(newY + 0.5)
  70.       newZ = math.floor(newZ + 0.5)
  71.  
  72.       print("New ship target position (rounded): (" .. newX .. ", " .. newY .. ", " .. newZ .. ")")
  73.  
  74.       -- Determine the movement delta.
  75.       local moveX = newX - currentX
  76.       local moveY = newY - currentY
  77.       local moveZ = newZ - currentZ
  78.  
  79.       ship.movement(moveX, moveY, moveZ)
  80.       ship.rotationSteps(0)
  81.       ship.command("MANUAL", true)
  82.       print("Ship commanded to move by (" .. moveX .. ", " .. moveY .. ", " .. moveZ .. ")")
  83.     end
  84. end
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement