Advertisement
jaklsfjlsak

精简自动驾驶 带追踪器

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