Advertisement
jaklsfjlsak

精简自动驾驶 无Discord

Aug 10th, 2023 (edited)
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local failping = 0 -- format: Will ping on Failed node execution 0 = Dont Ping, 1 = Ping
  2. local ship = peripheral.find("warpdriveShipCore")
  3. local initialx, initialy, initialz = ship.getLocalPosition()
  4. local navdatafile = ".navdata"
  5. local navbackup = ".navbackup"
  6. local navdata = {} -- format: {{dx,dy,dz},{dx,dy,dz},...} -- these are ship coords (forward, up, right)
  7.  
  8. if not (ship.isInHyperspace() or ship.isInSpace()) then
  9.     error("Ship needs to be in space or hyperspace!")
  10. end
  11.  
  12. while true do
  13.         print("We are currently in " .. (ship.isInHyperspace() and "HYPERSPACE" or "SPACE") .. " at " .. initialx .. " " .. initialy .. " " .. initialz)
  14.  
  15.         if fs.exists(navdatafile) then
  16.             local h = fs.open(navdatafile, "r")
  17.             local text = h.readAll()
  18.             navdata = textutils.unserialize(text)
  19.             h.close()
  20.             print("Waiting for ship core cooldown to be done before starting node execution...")
  21.             os.pullEvent("shipCoreCooldownDone")
  22.         end
  23.  
  24.         if #navdata <= 0 then -- if navdata wasn't loaded
  25.             print("Enter target pos: x y z")
  26.             local input = read()
  27. local txs, tys, tzs = input:gmatch("(%-?%d+) (%-?%d+) (%-?%d+)")()
  28. local tx = tonumber(txs)
  29. local ty = tonumber(tys)
  30. local tz = tonumber(tzs)
  31. if tx == nil or ty == nil or tz == "" then
  32.     error("Please use the proper number format.")
  33. end
  34. local ship_len_back, ship_len_left, ship_len_down = ship.dim_negative()
  35. local ship_len_front, ship_len_right, ship_len_up = ship.dim_positive()
  36.  
  37. function dst(x1, y1, z1, x2, y2, z2)
  38.     local dx = x2 - x1
  39.     local dy = y2 - y1
  40.     local dz = z2 - z1
  41.     return math.sqrt(dx * dx + dy * dy + dz * dz)
  42. end --dst
  43.  
  44. if ty - ship_len_down <= 0 or ty + ship_len_up >= 256 then
  45.     error("Target y position needs to be inside " .. ship_len_down .. " and " .. 256 - ship_len_up .. ".")
  46. end
  47.  
  48. local stayInHyper = true -- Assuming you always want to stay in hyperspace
  49. print("----------------------------------")
  50. print("Is this information correct?")
  51. print("Target coords: ", tx, ty, tz)
  52. print("Target dimension: " .. (stayInHyper and "HYPERSPACE" or "SPACE"))
  53. print("Total distance: " .. math.floor(dst(initialx, initialy, initialz, tx, ty, tz)) .. " blocks")
  54. print("----------------------------------")
  55.  
  56. print("Computing navigation steps...")
  57.  
  58.     local shipdeltafront, shipdeltaright
  59.     local shipdeltaup = ty-initialy
  60.     local orix,_,oriz = ship.getOrientation()
  61.  
  62.     if orix == 1 then
  63.         shipdeltafront = tx-initialx
  64.         shipdeltaright = tz-initialz
  65.     elseif orix == -1 then
  66.         shipdeltafront = -tx+initialx
  67.         shipdeltaright = -tz+initialz
  68.     elseif oriz == 1 then
  69.         shipdeltafront = tz-initialz
  70.         shipdeltaright = -tx+initialx
  71.     elseif oriz == -1 then
  72.         shipdeltafront = -tz+initialz
  73.         shipdeltaright = tx-initialx
  74.     else
  75.         error("Unexpected ship orientation!")
  76.     end
  77.  
  78.     print("We need to move "..shipdeltafront.." block(s) forward, "..shipdeltaup.." block(s) upward and "..shipdeltaright.." block(s) to the right.")
  79.     local minforward = ship_len_front+ship_len_back
  80.     local minup = ship_len_up+ship_len_down
  81.     local minright = ship_len_right+ship_len_left
  82.     ship.command("MANUAL",false)
  83.     local success, maxdist = ship.getMaxJumpDistance()
  84.     if not success then error("unable to get the ships max jump distance: "..maxdist) end
  85.     if maxdist <= 0 then error("max jump distance is zero") end
  86.     print("Max jump length = "..maxdist)
  87.     function computeMove(mindist,remaining,ignoreconstraint) -- returns a move to make along that axis
  88.         if remaining == 0 then return 0 end
  89.        
  90.         local remsign = (remaining < 0) and -1 or 1
  91.            
  92.         if math.abs(remaining) < mindist and not ignoreconstraint then -- if the move would be impossible because it is too short, move further away
  93.             return -remsign * mindist
  94.         end
  95.         return remsign * math.min(math.abs(remaining),maxdist)  
  96.     end
  97.  
  98.     repeat
  99.         local move = {}
  100.         move[2] = computeMove(minup+1,shipdeltaup,true) --y    
  101.         shipdeltaup = shipdeltaup - move[2]
  102.        
  103.         move[1] = computeMove(minforward+1,shipdeltafront,math.abs(move[2]) > minup) --x
  104.        
  105.         if not (math.abs(move[2]) > minup) and  shipdeltafront == 0 and shipdeltaright == 0 and shipdeltaup ~= 0 and move[1] == 0 then
  106.             move[1] = minforward+1
  107.         end
  108.        
  109.         shipdeltafront = shipdeltafront - move[1]
  110.         move[3] = computeMove(minright+1,shipdeltaright, math.abs(move[2]) > minup or math.abs(move[1]) > minforward) --z
  111.         shipdeltaright = shipdeltaright - move[3]
  112.         navdata[#navdata+1] = move
  113.         print("Computed move: #"..#navdata..": "..move[1].." block(s) forward, "..move[2].." block(s) upward and "..move[3].." block(s) to the right.")
  114.         print("Remaining: "..shipdeltafront..":"..shipdeltaup..":"..shipdeltaright)
  115.        
  116.     until shipdeltafront == 0 and shipdeltaup == 0 and shipdeltaright == 0
  117.     print("Navigational path plotted using "..#navdata.." jump(s).")
  118. end
  119. for i=1,#navdata do
  120.     local move = navdata[i]
  121.        
  122.     print("Executing next node... There are "..#navdata.." node(s) left to execute.")
  123.     table.remove(navdata,1)
  124.     if fs.exists(navbackup) then
  125.     fs.delete(navbackup)
  126. end
  127.  if #navdata > 0 then
  128.   local text = textutils.serialize(navdata)
  129.         local h = fs.open(navdatafile, "w")
  130.         h.write(text)
  131.         h.close()
  132.         fs.copy(navdatafile, navbackup)
  133.     else
  134.         fs.delete(navdatafile)
  135.     end
  136.     ship.command("MANUAL", false)
  137.     ship.movement(move[1],move[2],move[3])
  138.     ship.rotationSteps(0)
  139.     ship.command("MANUAL", true)
  140.    
  141.     for i=60,0,-1 do    
  142.         term.setCursorPos(1,select(2,term.getCursorPos()))
  143.         term.write("Waiting for the ship to jump..."..i.."   ")
  144.         sleep(1)
  145.     end
  146. number = #navdata + 1
  147. fs.delete(navdatafile)
  148. fs.copy(navbackup, navdatafile)
  149. print("The ship did not jump.")
  150. if failping >= 0 then
  151. end
  152.        
  153. sleep(3)
  154. os.reboot()
  155. end
  156. end
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement