Advertisement
jaklsfjlsak

核心跳船信号接收 福瑞导航

May 28th, 2025 (edited)
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.69 KB | None | 0 0
  1. -- autonav.lua
  2. -- Put this in /startup (or run it directly).  Assumes single warpdriveShipCore.
  3.  
  4. local ship = peripheral.find("warpdriveShipCore")
  5. assert(ship, "No warpdriveShipCore found!")
  6.  
  7. local lever       = "front"
  8. local navfile     = ".navdata"
  9. local backupfile  = ".navbackup"
  10.  
  11. -- read/write helpers
  12. local function loadNav()
  13.   if fs.exists(navfile) then
  14.     local f = fs.open(navfile,"r")
  15.     local t = textutils.unserialize(f.readAll())
  16.     f.close()
  17.     return t
  18.   end
  19. end
  20.  
  21. local function saveNav(t)
  22.   local f = fs.open(navfile,"w")
  23.   f.write(textutils.serialize(t))
  24.   f.close()
  25. end
  26.  
  27. local function backupNav()
  28.   if fs.exists(navfile) then
  29.     if fs.exists(backupfile) then fs.delete(backupfile) end
  30.     fs.copy(navfile,backupfile)
  31.   end
  32. end
  33.  
  34. local function clearNav()
  35.   if fs.exists(navfile) then fs.delete(navfile) end
  36. end
  37.  
  38. -- execute one hop and reboot
  39. local function runOneHop(nav)
  40.   local move = nav[1]
  41.   -- remove first, persist rest (or delete if none)
  42.   if #nav > 1 then
  43.     table.remove(nav,1)
  44.     saveNav(nav)
  45.   else
  46.     clearNav()
  47.   end
  48.   backupNav()
  49.  
  50.   -- fire the jump
  51.   ship.command("MANUAL", false)
  52.   ship.movement(move[1], move[2], move[3])
  53.   ship.rotationSteps(0)
  54.   ship.command("MANUAL", true)
  55.  
  56.   print(("→ Executed hop %d/%d   FB:%d  UD:%d  LR:%d")
  57.         :format(1, #nav+1, move[1], move[2], move[3]))
  58.   sleep(5)        -- give warpdrive time
  59.   os.reboot()     -- next boot will pick up the next hop
  60. end
  61.  
  62. -- if there's a pending nav list, resume it
  63. local pending = loadNav()
  64. if pending then
  65.   print("Resuming auto-nav with "..#pending.." hops remaining…")
  66.   runOneHop(pending)
  67. end
  68.  
  69. -- otherwise, listen for a broadcast
  70. for _,side in ipairs(peripheral.getNames()) do
  71.   if peripheral.getType(side)=="modem" then rednet.open(side) end
  72. end
  73. print("Awaiting ONJMcoordBroadcast (lever '"..lever.."’ HIGH)…")
  74.  
  75. while true do
  76.   if redstone.getInput(lever) then
  77.     local sender,msg = rednet.receive("ONJMcoordBroadcast", 0.5)
  78.     if sender and type(msg)=="table" and msg.x and msg.y and msg.z then
  79.       local tx,ty,tz = tonumber(msg.x), tonumber(msg.y), tonumber(msg.z)
  80.       print(("Received target from %d → X=%d Y=%d Z=%d")
  81.             :format(sender,tx,ty,tz))
  82.  
  83.       -- fetch dims & orientation
  84.       local front, right, up    = ship.dim_positive()
  85.       local back,  left,  down  = ship.dim_negative()
  86.       local orix,_,oriz          = ship.getOrientation()
  87.       local mx,my,mz            = ship.getLocalPosition()
  88.  
  89.       -- world deltas
  90.       local dx, dy, dz = tx-mx, ty-my, tz-mz
  91.       -- ship-local axes
  92.       local shipdeltafront, shipdeltaright
  93.       if     orix== 1 then shipdeltafront, shipdeltaright = dx,   dz
  94.       elseif orix==-1 then shipdeltafront, shipdeltaright = -dx, -dz
  95.       elseif oriz== 1 then shipdeltafront, shipdeltaright = dz,  -dx
  96.       elseif oriz==-1 then shipdeltafront, shipdeltaright = -dz,  dx
  97.       else shipdeltafront, shipdeltaright = 0,0 end
  98.       local shipdeltaup = dy
  99.  
  100.       -- minimal clearance
  101.       local minFB = front+back
  102.       local minUD = up+down
  103.       local minLR = right+left
  104.  
  105.       -- how far we can jump
  106.       local ok, maxJ = ship.getMaxJumpDistance()
  107.       if not ok or maxJ<=0 then
  108.         maxJ = math.sqrt(shipdeltafront^2 + shipdeltaup^2 + shipdeltaright^2)
  109.       end
  110.  
  111.       -- compute one-axis move, like main.lua’s computeMove()
  112.       local function computeMove(mindist, rem, unconstrained)
  113.         if rem == 0 then return 0 end
  114.         local s = rem<0 and -1 or 1
  115.         if math.abs(rem)<mindist and not unconstrained then
  116.           return -s * mindist
  117.         end
  118.         return s * math.min(math.abs(rem), maxJ)
  119.       end
  120.  
  121.       -- build the full navdata
  122.       local nav = {}
  123.       repeat
  124.         local m = {}
  125.         -- Y first
  126.         m[2] = computeMove(minUD+1, shipdeltaup, true)
  127.         shipdeltaup = shipdeltaup - m[2]
  128.         -- X (forward/back)
  129.         m[1] = computeMove(minFB+1, shipdeltafront,
  130.                    math.abs(m[2])>minUD)
  131.         if not (math.abs(m[2])>minUD)
  132.            and shipdeltafront==0 and shipdeltaup~=0 and shipdeltaright==0
  133.            and m[1]==0
  134.         then
  135.           m[1] = minFB+1
  136.         end
  137.         shipdeltafront = shipdeltafront - m[1]
  138.         -- Z (left/right)
  139.         m[3] = computeMove(minLR+1, shipdeltaright,
  140.                    math.abs(m[2])>minUD or math.abs(m[1])>minFB)
  141.         shipdeltaright = shipdeltaright - m[3]
  142.  
  143.         table.insert(nav, m)
  144.       until shipdeltafront==0 and shipdeltaup==0 and shipdeltaright==0
  145.  
  146.       -- persist and run first hop
  147.       saveNav(nav)
  148.       print("Computed "..#nav.." hop(s); starting…")
  149.       runOneHop(nav)
  150.     end
  151.   else
  152.     sleep(0.1)
  153.   end
  154. end
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement