Advertisement
jaklsfjlsak

三维定位 可用

Apr 15th, 2025 (edited)
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.00 KB | None | 0 0
  1. local ship = peripheral.find("warpdriveShipCore")
  2. local lasers = peripheral.getNames()
  3.  
  4. local offsetDistance = 25
  5.  
  6. -- Filter for warpdriveLaserCamera peripherals and set beam frequency.
  7. for i = #lasers, 1, -1 do
  8.     if peripheral.getType(lasers[i]) ~= "warpdriveLaserCamera" then
  9.         table.remove(lasers, i)
  10.     else
  11.         peripheral.wrap(lasers[i]).beamFrequency(1420)
  12.     end
  13. end
  14.  
  15. -- Get ship dimensions and status.
  16. ship_front, ship_right, ship_up = ship.dim_positive()
  17. ship_back, ship_left, ship_down = ship.dim_negative()
  18. ship_isInHyper = ship.isInHyperspace()
  19. ship_movement = { ship.movement() }
  20. ship_rotationSteps = ship.rotationSteps()
  21.  
  22. print("Emit Scanning Laser to Jump Ship and Aligning the Mining Laser")
  23.  
  24. while true do
  25.     local event, laserName, lx, ly, lz, block, _, _, _, type, metadata, resistance = os.pullEvent()
  26.    
  27.     if event == "laserScanning" then
  28.         -- Convert laser scanning coordinates to numbers.
  29.         local lastLx = tonumber(lx)
  30.         local lastLy = tonumber(ly)
  31.         local lastLz = tonumber(lz)
  32.        
  33.         -- Get current ship position.
  34.         local mx, my, mz = ship.getLocalPosition()
  35.        
  36.         print("Laser target: X:" .. lastLx .. " Y:" .. lastLy .. " Z:" .. lastLz)
  37.        
  38.         -- Calculate vector from laser target (L) to ship (M)
  39.         local vx = mx - lastLx
  40.         local vy = my - lastLy
  41.         local vz = mz - lastLz
  42.        
  43.         local dist = math.sqrt(vx*vx + vy*vy + vz*vz)
  44.        
  45.         if dist > 0 then
  46.             -- Normalize the vector.
  47.             local ux = vx / dist
  48.             local uy = vy / dist
  49.             local uz = vz / dist
  50.  
  51.             -- Calculate dx, dy, dz as the point offsetDistance away from the laser target along the vector toward the ship.
  52.             local dx = lastLx + ux * offsetDistance
  53.             local dy = lastLy + uy * offsetDistance
  54.             local dz = lastLz + uz * offsetDistance
  55.            
  56.             -- Round the coordinates to whole numbers.
  57.             dx = math.floor(dx + 0.5)
  58.             dy = math.floor(dy + 0.5)
  59.             dz = math.floor(dz + 0.5)
  60.            
  61.             print("Calculated jump coordinates: (" .. dx .. ", " .. dy .. ", " .. dz .. ")")
  62.            
  63.             -- Retrieve ship orientation and compute minimum movement thresholds.
  64.             local rx, ry, rz = ship.getOrientation()
  65.             local minForwardBack = math.abs(ship_front + ship_back + 1)
  66.             local minLeftRight = math.abs(ship_left + ship_right + 1)
  67.             local minUpDown = math.abs(ship_up + ship_down + 1)
  68.            
  69.             -- Compute the movement difference between target jump point and ship.
  70.             local jx = dx - mx
  71.             local jy = dy - my
  72.             local jz = dz - mz
  73.            
  74.             -- For horizontal movement, adjust based on ship orientation.
  75.             local forwardBackMov = 0
  76.             local leftRightMov = 0
  77.             -- For vertical movement, use the raw difference.
  78.             local upDownMov = jy
  79.            
  80.             if rx == 1 then
  81.                 forwardBackMov = jx
  82.                 leftRightMov = jz
  83.             elseif rx == -1 then
  84.                 forwardBackMov = -jx
  85.                 leftRightMov = -jz
  86.             elseif rz == 1 then
  87.                 forwardBackMov = jz
  88.                 leftRightMov = -jx
  89.             elseif rz == -1 then
  90.                 forwardBackMov = -jz
  91.                 leftRightMov = jx
  92.             end
  93.  
  94.             -- Check if movement is large enough in each axis.
  95.             if math.abs(forwardBackMov) < minForwardBack and math.abs(upDownMov) < minUpDown and math.abs(leftRightMov) < minLeftRight then
  96.                 print("The movement is too small!")
  97.             else
  98.                 ship.movement(forwardBackMov, upDownMov, leftRightMov)
  99.                 ship.rotationSteps(0)
  100.                 ship.command("MANUAL", true)
  101.             end
  102.         else
  103.             print("Error: Ship and target are at the same location!")
  104.         end
  105.     end
  106. end
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement