Advertisement
jaklsfjlsak

福瑞信号导航 autonav.lua

May 28th, 2025
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.26 KB | None | 0 0
  1. -- autonav.lua
  2. -- Place this as /startup (or make sure it's run on boot).
  3.  
  4. local ship = peripheral.find("warpdriveShipCore")
  5. assert(ship, "No warpdriveShipCore found!")
  6.  
  7. local navfile    = ".navdata"
  8. local backupfile = ".navbackup"
  9.  
  10. -- load / save helpers
  11. local function loadNav()
  12.   if fs.exists(navfile) then
  13.     local f = fs.open(navfile, "r")
  14.     local t = textutils.unserialize(f.readAll())
  15.     f.close()
  16.     return t
  17.   end
  18. end
  19.  
  20. local function saveNav(t)
  21.   local f = fs.open(navfile, "w")
  22.   f.write(textutils.serialize(t))
  23.   f.close()
  24. end
  25.  
  26. local function backupNav()
  27.   if fs.exists(navfile) then
  28.     if fs.exists(backupfile) then fs.delete(backupfile) end
  29.     fs.copy(navfile, backupfile)
  30.   end
  31. end
  32.  
  33. local function clearNav()
  34.   if fs.exists(navfile) then fs.delete(navfile) end
  35. end
  36.  
  37. -- execute just one hop then reboot
  38. local function runOneHop(nav)
  39.   local move = table.remove(nav, 1)
  40.   if #nav > 0 then
  41.     saveNav(nav)
  42.   else
  43.     clearNav()
  44.   end
  45.   backupNav()
  46.  
  47.   -- fire the jump
  48.   ship.command("MANUAL", false)
  49.   ship.movement(move[1], 0, move[2])  -- [1]=forward/back, [2]=left/right
  50.   ship.rotationSteps(0)
  51.   ship.command("MANUAL", true)
  52.  
  53.   print(string.format(
  54.     "→ Executed hop. Remaining: %d", #nav
  55.   ))
  56.   sleep(5)
  57.   os.reboot()
  58. end
  59.  
  60. -- if there’s a pending nav list, resume it
  61. local pending = loadNav()
  62. if pending then
  63.   print("Resuming auto-nav, hops left:", #pending)
  64.   runOneHop(pending)
  65. end
  66.  
  67. -- otherwise wait for a broadcast
  68. for _, side in ipairs(peripheral.getNames()) do
  69.   if peripheral.getType(side) == "modem" then
  70.     rednet.open(side)
  71.   end
  72. end
  73. print("Waiting for ONJMcoordBroadcast…")
  74. local _, msg = rednet.receive("ONJMcoordBroadcast")
  75.  
  76. assert(type(msg)=="table" and msg.x and msg.z,
  77.        "Invalid broadcast payload")
  78.  
  79. local tx, tz = tonumber(msg.x), tonumber(msg.z)
  80. print(("Target received → X:%d  Z:%d"):format(tx, tz))
  81.  
  82. -- fetch dims & orientation
  83. local front, right = ship.dim_positive()
  84. local back,  left  = ship.dim_negative()
  85. local rx,_,rz    = ship.getOrientation()
  86. local mx,_,mz    = ship.getLocalPosition()
  87.  
  88. -- world deltas
  89. local dx, dz = tx-mx, tz-mz
  90.  
  91. -- convert into ship-local axes
  92. local fb, lr = 0,0
  93. if     rx== 1 then fb, lr = dx,  dz
  94. elseif rx==-1 then fb, lr = -dx, -dz
  95. elseif rz== 1 then fb, lr = dz,  -dx
  96. elseif rz==-1 then fb, lr = -dz,  dx
  97. end
  98.  
  99. -- clearance + max jump
  100. local minFB = front+back
  101. local minLR = right+left
  102. local ok, maxJ = ship.getMaxJumpDistance()
  103. if not ok or maxJ<=0 then
  104.   error("Could not get max jump distance")
  105. end
  106.  
  107. -- one-axis “computeMove” from your main.lua
  108. local function computeMove(mindist, rem, unconstrained)
  109.   if rem==0 then return 0 end
  110.   local s = rem<0 and -1 or 1
  111.   if math.abs(rem)<mindist and not unconstrained then
  112.     return -s*mindist
  113.   end
  114.   return s * math.min(math.abs(rem), maxJ)
  115. end
  116.  
  117. -- build the full list of (forward/back, left/right) hops
  118. local nav = {}
  119. repeat
  120.   local m_fb = computeMove(minFB+1, fb, false)
  121.   fb = fb - m_fb
  122.   local m_lr = computeMove(minLR+1, lr, math.abs(m_fb)>minFB)
  123.   lr = lr - m_lr
  124.   table.insert(nav, { m_fb, m_lr })
  125. until fb==0 and lr==0
  126.  
  127. assert(#nav>0, "Computed zero hops?")
  128.  
  129. -- save & run first hop
  130. saveNav(nav)
  131. print("Computed "..#nav.." hops; starting…")
  132. runOneHop(nav)
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement