jaklsfjlsak

核心跳船 信号接收器 v2

Apr 27th, 2025 (edited)
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local ship  = peripheral.find("warpdriveShipCore")
  2. local miner = peripheral.find("warpdriveMiningLaser")
  3.  
  4. local ship_front, ship_right, ship_up   = ship.dim_positive()
  5. local ship_back,  ship_left,  ship_down = ship.dim_negative()
  6.  
  7. -- Open rednet on the modem side (adjust "top" as needed)
  8. rednet.open("top")
  9. print("Waiting for coordinate broadcasts when redstone is active on the front...")
  10.  
  11. while true do
  12.   if redstone.getInput("front") then
  13.     -- listen for any protocol, 0.5s timeout
  14.     local senderID, message, protocol = rednet.receive(nil, 0.5)
  15.     if senderID and message and message.x and message.y and message.z then
  16.       local lastLx = tonumber(message.x)
  17.       local lastLy = tonumber(message.y)
  18.       local lastLz = tonumber(message.z)
  19.  
  20.       -- choose reference point based on protocol
  21.       if protocol == "HorizontalJumpBroadcast"
  22.       or protocol == "HorizontalJumpMinerBroadcast" then
  23.         local mx, my, mz
  24.         if protocol == "HorizontalJumpBroadcast" then
  25.           mx, my, mz = ship.getLocalPosition()
  26.         else
  27.           mx, my, mz = miner.getLocalPosition()
  28.         end
  29.  
  30.         print(("Received %s → X:%d, Y:%d, Z:%d")
  31.           :format(protocol, lastLx, lastLy, lastLz))
  32.  
  33.         -- compute movement vector (X/Z plane only)
  34.         local dx = lastLx - mx
  35.         local dz = lastLz - mz
  36.  
  37.         local rx, ry, rz     = ship.getOrientation()
  38.         local minFB          = math.abs(ship_front + ship_back + 1)
  39.         local minLR          = math.abs(ship_left  + ship_right + 1)
  40.         local forwardBackMov = 0
  41.         local leftRightMov   = 0
  42.  
  43.         if     rx ==  1 then forwardBackMov, leftRightMov =  dx,  dz
  44.         elseif rx == -1 then forwardBackMov, leftRightMov = -dx, -dz
  45.         elseif rz ==  1 then forwardBackMov, leftRightMov =  dz, -dx
  46.         elseif rz == -1 then forwardBackMov, leftRightMov = -dz,  dx
  47.         end
  48.  
  49.         if math.abs(forwardBackMov) < minFB and math.abs(leftRightMov) < minLR then
  50.           print("The movement is too small!")
  51.         else
  52.           ship.movement(forwardBackMov, 0, leftRightMov)
  53.           ship.rotationSteps(0)
  54.           ship.command("MANUAL", true)
  55.         end
  56.       end
  57.     end
  58.   else
  59.     -- no redstone signal: wait before retrying
  60.     os.sleep(0.1)
  61.   end
  62. end
  63.  
Advertisement
Add Comment
Please, Sign In to add comment