Advertisement
jaklsfjlsak

集成红网跳船

Apr 27th, 2025 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. -- Unified Ship Jump Controller
  2. --  - “3DOffsetBroadcast” → 3D-offset jump + mining-laser align
  3. --  - “HorizontalJumpBroadcast” → horizontal jump (ship position)
  4. --  - “HorizontalJumpMinerBroadcast” → horizontal jump (miner position)
  5.  
  6. -- 1) Configuration
  7. local offsetDistance = 25
  8. local frontLever     = "front"
  9.  
  10. -- 2) Wrap peripherals
  11. local ship  = peripheral.find("warpdriveShipCore")
  12. assert(ship, "No warpdriveShipCore found")
  13. local miner = peripheral.find("warpdriveMiningLaser")
  14. assert(miner, "No warpdriveMiningLaser found")
  15.  
  16. -- 3) Precompute ship dims
  17. local ship_front, ship_right, ship_up   = ship.dim_positive()
  18. local ship_back,  ship_left,  ship_down = ship.dim_negative()
  19.  
  20. -- 4) Open rednet on every modem
  21. for _, side in ipairs(peripheral.getNames()) do
  22.   if peripheral.getType(side) == "modem" then
  23.     rednet.open(side)
  24.   end
  25. end
  26.  
  27. print("Listening for 3DOffsetBroadcast, HorizontalJumpBroadcast, HorizontalJumpMinerBroadcast")
  28. print("  (front-side redstone must be ON to accept jumps)")
  29.  
  30. -- 5) Helper: perform the 3D-offset jump logic
  31. local function do3DOffsetJump(x,y,z)
  32.   local mx, my, mz = ship.getLocalPosition()
  33.   local vx, vy, vz = mx - x, my - y, mz - z
  34.   local dist = math.sqrt(vx*vx + vy*vy + vz*vz)
  35.   if dist <= 0 then
  36.     print("3DJump: target == ship; skipping")
  37.     return
  38.   end
  39.   local ux, uy, uz = vx/dist, vy/dist, vz/dist
  40.   local dx = math.floor(x + ux*offsetDistance + 0.5)
  41.   local dy = math.floor(y + uy*offsetDistance + 0.5)
  42.   local dz = math.floor(z + uz*offsetDistance + 0.5)
  43.   print(string.format("3DJump coords → X:%d Y:%d Z:%d", dx,dy,dz))
  44.  
  45.   -- orient + thresholds
  46.   local rx, _, rz = ship.getOrientation()
  47.   local minFB = math.abs(ship_front + ship_back + 1)
  48.   local minLR = math.abs(ship_left  + ship_right + 1)
  49.   local minUD = math.abs(ship_up    + ship_down + 1)
  50.  
  51.   -- world-space desired move
  52.   local jx, jy, jz = dx - mx, dy - my, dz - mz
  53.  
  54.   -- map to ship axes
  55.   local fb, lr = 0, 0
  56.   local ud      = jy
  57.   if     rx ==  1 then fb, lr = jx,  jz
  58.   elseif rx == -1 then fb, lr = -jx, -jz
  59.   elseif rz ==  1 then fb, lr =  jz, -jx
  60.   elseif rz == -1 then fb, lr = -jz,  jx
  61.   end
  62.  
  63.   if math.abs(fb) < minFB and math.abs(ud) < minUD and math.abs(lr) < minLR then
  64.     print("3DJump: Movement too small; skipping")
  65.   else
  66.     ship.movement(fb, ud, lr)
  67.     ship.rotationSteps(0)
  68.     ship.command("MANUAL", true)
  69.   end
  70. end
  71.  
  72. -- 6) Helper: perform the horizontal-only jump logic
  73. local function doHorizontalJump(x,y,z, useMiner)
  74.   local mx, my, mz
  75.   if useMiner then
  76.     mx,my,mz = miner.getLocalPosition()
  77.   else
  78.     mx,my,mz = ship.getLocalPosition()
  79.   end
  80.   print(string.format(
  81.     "%s → Target X:%d Y:%d Z:%d",
  82.     useMiner and "MinerJump" or "ShipJump", x,y,z
  83.   ))
  84.  
  85.   -- only forward/back + left/right
  86.   local dx, dz = x - mx, z - mz
  87.   local rx, _, rz = ship.getOrientation()
  88.   local minFB = math.abs(ship_front + ship_back + 1)
  89.   local minLR = math.abs(ship_left  + ship_right + 1)
  90.   local fb, lr = 0, 0
  91.   if     rx ==  1 then fb,lr =  dx,  dz
  92.   elseif rx == -1 then fb,lr = -dx, -dz
  93.   elseif rz ==  1 then fb,lr =  dz, -dx
  94.   elseif rz == -1 then fb,lr = -dz,  dx
  95.   end
  96.  
  97.   if math.abs(fb) < minFB and math.abs(lr) < minLR then
  98.     print("HorizontalJump: Movement too small; skipping")
  99.   else
  100.     ship.movement(fb, 0, lr)
  101.     ship.rotationSteps(0)
  102.     ship.command("MANUAL", true)
  103.   end
  104. end
  105.  
  106. -- 7) Main event loop
  107. while true do
  108.   if redstone.getInput(frontLever) then
  109.     -- wait 0.5s for any protocol
  110.     local sender, message, protocol = rednet.receive(nil, 0.5)
  111.     if sender and message and message.x and message.y and message.z then
  112.       local x,y,z = tonumber(message.x), tonumber(message.y), tonumber(message.z)
  113.  
  114.       if protocol == "3DOffsetBroadcast" then
  115.         do3DOffsetJump(x,y,z)
  116.  
  117.       elseif protocol == "HorizontalJumpBroadcast" then
  118.         doHorizontalJump(x,y,z, false)
  119.  
  120.       elseif protocol == "HorizontalJumpMinerBroadcast" then
  121.         doHorizontalJump(x,y,z, true)
  122.  
  123.       else
  124.         -- unknown protocol: ignore entirely
  125.       end
  126.     end
  127.   else
  128.     os.sleep(0.1)
  129.   end
  130. end
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement