Advertisement
jaklsfjlsak

自 驾

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