jaklsfjlsak

三维定位 426 v2

Apr 27th, 2025
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- 3D-Offset-Driven Ship Jump & Mining-Laser Align
  2. -- Listens for { x, y, z } on Rednet protocol "3DOffsetBroadcast"
  3. -- and computes an offset jump point back toward the ship.
  4.  
  5. -- 1) Configuration
  6. local offsetDistance = 25
  7.  
  8. -- 2) Wrap your ship core
  9. local ship = peripheral.find("warpdriveShipCore")
  10. assert(ship, "No warpdriveShipCore detected!")
  11.  
  12. -- 3) Pre-compute ship dimensions & status
  13. local ship_front, ship_right, ship_up   = ship.dim_positive()
  14. local ship_back,  ship_left,  ship_down = ship.dim_negative()
  15.  
  16. -- 4) Instructions
  17. print("Waiting for 3DOffsetBroadcast → Redstone 'front' must be ON to accept jumps")
  18.  
  19. -- 5) Open Rednet on all modems
  20. for _, side in ipairs(peripheral.getNames()) do
  21.   if peripheral.getType(side) == "modem" then
  22.     rednet.open(side)
  23.   end
  24. end
  25.  
  26. -- 6) Main loop: only react when redstone 'front' is high
  27. while true do
  28.   if redstone.getInput("front") then
  29.     -- wait up to 0.5s for a broadcast on 3DOffsetBroadcast
  30.     local sender, message = rednet.receive("3DOffsetBroadcast", 0.5)
  31.     if sender and message.x and message.y and message.z then
  32.       local lastLx, lastLy, lastLz =
  33.         tonumber(message.x),
  34.         tonumber(message.y),
  35.         tonumber(message.z)
  36.  
  37.       print(string.format(
  38.         "Received target from %d → X:%d  Y:%d  Z:%d",
  39.         sender, lastLx, lastLy, lastLz
  40.       ))
  41.  
  42.       -- ship current position
  43.       local mx, my, mz = ship.getLocalPosition()
  44.  
  45.       -- vector from laser target back toward ship
  46.       local vx, vy, vz = mx - lastLx, my - lastLy, mz - lastLz
  47.       local dist = math.sqrt(vx*vx + vy*vy + vz*vz)
  48.  
  49.       if dist > 0 then
  50.         -- unit vector
  51.         local ux, uy, uz = vx/dist, vy/dist, vz/dist
  52.  
  53.         -- offset point
  54.         local dx = math.floor(lastLx + ux*offsetDistance + 0.5)
  55.         local dy = math.floor(lastLy + uy*offsetDistance + 0.5)
  56.         local dz = math.floor(lastLz + uz*offsetDistance + 0.5)
  57.  
  58.         print(string.format(
  59.           "Jump coords → X:%d  Y:%d  Z:%d",
  60.           dx, dy, dz
  61.         ))
  62.  
  63.         -- orientation & thresholds
  64.         local rx, ry, rz    = ship.getOrientation()
  65.         local minFB         = math.abs(ship_front + ship_back + 1)
  66.         local minLR         = math.abs(ship_left  + ship_right + 1)
  67.         local minUD         = math.abs(ship_up    + ship_down + 1)
  68.  
  69.         -- desired move in world-space
  70.         local jx, jy, jz    = dx - mx, dy - my, dz - mz
  71.  
  72.         -- map to ship axes
  73.         local fb, lr = 0, 0
  74.         local ud      = jy
  75.         if     rx ==  1 then fb, lr = jx,  jz
  76.         elseif rx == -1 then fb, lr = -jx, -jz
  77.         elseif rz ==  1 then fb, lr = jz, -jx
  78.         elseif rz == -1 then fb, lr = -jz, jx
  79.         end
  80.  
  81.         -- threshold check
  82.         if math.abs(fb) < minFB
  83.            and math.abs(ud) < minUD
  84.            and math.abs(lr) < minLR then
  85.           print("Movement too small—skipping jump.")
  86.         else
  87.           ship.movement(fb, ud, lr)
  88.           ship.rotationSteps(0)
  89.           ship.command("MANUAL", true)
  90.         end
  91.       else
  92.         print("Error: ship and target coincide—no movement.")
  93.       end
  94.     end
  95.   else
  96.     -- redstone is off, pause briefly
  97.     os.sleep(0.1)
  98.   end
  99. end
  100.  
Advertisement
Add Comment
Please, Sign In to add comment