Advertisement
Guest User

startup

a guest
Apr 2nd, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.57 KB | None | 0 0
  1. local name = "<@340978223896723456>" -- Discord ID
  2. local uri = "https://discordapp.com/api/webhooks/6702977392651/Tdn8VmaT5tyJwsI52EN5w32iAuYsXR7D9JzhpC8-nUVAq_reyXmducwUQfJad4Pjmo8R" -- Discord Uri
  3. local lever = "left" -- format: front,back,left,right,top,bottom ONLY
  4. local failping = 1 -- 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. local txs, tys, tzs = ...
  34.     print("'"..txs.."'")
  35.     print("'"..tys.."'")
  36.     print("'"..tzs.."'")
  37.     local tx = txs
  38.     local ty = tys
  39.     local tz = 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 tys-ship_len_down <= 0 or tys+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. --  print("Do you want to stay in hyperspace after jumping? (Y/n)")
  58. --  local input2 = read()
  59. --  local stayInHyper = input2~="n" and input2~="N"
  60.     print("----------------------------------")
  61.     print("Is this information correct?")
  62.     print("Target coords: ", tx,ty,tz)
  63.     print("Target dimension: "..(stayInHyper and "HYPERSPACE" or "SPACE"))
  64.     print("Total distance: "..math.floor(dst(initialx,initialy,initialz,tx,ty,tz)).." blocks")
  65.     print("----------------------------------")
  66.     for i=1,0,-1 do    
  67.         term.setCursorPos(1,select(2,term.getCursorPos()))
  68.         term.write("Please double check the info..."..i.."   ")
  69.         sleep(1)
  70.     end
  71.     term.clearLine()
  72.     print()
  73.     print("Enter 'Y' or press ENTER to engage navigation.")
  74. --  local input3 = read()
  75. --  if input3 ~= "Y" and input3 ~="y" and input3 ~="" then
  76. --      print("Aborting nagivation.")
  77. --      return
  78. --  end
  79.     print("Computing navigation steps...")
  80.  
  81.     local shipdeltafront, shipdeltaright
  82.     local shipdeltaup = ty-initialy
  83.     local orix,_,oriz = ship.getOrientation()
  84.  
  85.     if orix == 1 then
  86.         shipdeltafront = tx-initialx
  87.         shipdeltaright = tz-initialz
  88.     elseif orix == -1 then
  89.         shipdeltafront = -tx+initialx
  90.         shipdeltaright = -tz+initialz
  91.     elseif oriz == 1 then
  92.         shipdeltafront = tz-initialz
  93.         shipdeltaright = -tx+initialx
  94.     elseif oriz == -1 then
  95.         shipdeltafront = -tz+initialz
  96.         shipdeltaright = tx-initialx
  97.     else
  98.         error("Unexpected ship orientation!")
  99.     end
  100.  
  101.     print("We need to move "..shipdeltafront.." block(s) forward, "..shipdeltaup.." block(s) upward and "..shipdeltaright.." block(s) to the right.")
  102.     local minforward = ship_len_front+ship_len_back
  103.     local minup = ship_len_up+ship_len_down
  104.     local minright = ship_len_right+ship_len_left
  105.     ship.command("MANUAL",false)
  106.     local success, maxdist = ship.getMaxJumpDistance()
  107.     if not success then error("unable to get the ships max jump distance: "..maxdist) end
  108.     if maxdist <= 0 then error("max jump distance is zero") end
  109.     print("Max jump length = "..maxdist)
  110.     function computeMove(mindist,remaining,ignoreconstraint) -- returns a move to make along that axis
  111.         if remaining == 0 then return 0 end
  112.        
  113.         local remsign = (remaining < 0) and -1 or 1
  114.            
  115.         if math.abs(remaining) < mindist and not ignoreconstraint then -- if the move would be impossible because it is too short, move further away
  116.             return -remsign * mindist
  117.         end
  118.         return remsign * math.min(math.abs(remaining),maxdist) 
  119.     end
  120.  
  121.     repeat
  122.         local move = {}
  123.         move[2] = computeMove(minup+1,shipdeltaup,true) --y    
  124.         shipdeltaup = shipdeltaup - move[2]
  125.        
  126.         move[1] = computeMove(minforward+1,shipdeltafront,math.abs(move[2]) > minup) --x
  127.        
  128.         if not (math.abs(move[2]) > minup) and  shipdeltafront == 0 and shipdeltaright == 0 and shipdeltaup ~= 0 and move[1] == 0 then
  129.             move[1] = minforward+1
  130.         end
  131.        
  132.         shipdeltafront = shipdeltafront - move[1]
  133.         move[3] = computeMove(minright+1,shipdeltaright, math.abs(move[2]) > minup or math.abs(move[1]) > minforward) --z
  134.         shipdeltaright = shipdeltaright - move[3]
  135.         navdata[#navdata+1] = move
  136.         print("Computed move: #"..#navdata..": "..move[1].." block(s) forward, "..move[2].." block(s) upward and "..move[3].." block(s) to the right.")
  137.         print("Remaining: "..shipdeltafront..":"..shipdeltaup..":"..shipdeltaright)
  138.        
  139.     until shipdeltafront == 0 and shipdeltaup == 0 and shipdeltaright == 0
  140.     print("Navigational path plotted using "..#navdata.." jump(s).")
  141. end
  142. for i=1,#navdata do
  143.     local move = navdata[i]
  144.        
  145.     print("Executing next node... There are "..#navdata.." node(s) left to execute.")
  146.  http.post(uri,"{\"content\":\"```yaml\\nAutopilot: Executing next node There are "..#navdata.." node(s) left to execute.```\"}",{['content-type']="application/json"})
  147.     table.remove(navdata,1)
  148.     if fs.exists(navbackup) then
  149.     fs.delete(navbackup)
  150. end
  151.  if #navdata > 0 then
  152.   local text = textutils.serialize(navdata)
  153.         local h = fs.open(navdatafile, "w")
  154.         h.write(text)
  155.         h.close()
  156.         fs.copy(navdatafile, navbackup)
  157.     else
  158.         fs.delete(navdatafile)
  159.     end
  160.     ship.command("MANUAL", false)
  161.     ship.movement(move[1],move[2],move[3])
  162.     ship.rotationSteps(0)
  163.     ship.command("MANUAL", true)
  164.    
  165.     for i=60,0,-1 do    
  166.         term.setCursorPos(1,select(2,term.getCursorPos()))
  167.         term.write("Waiting for the ship to jump..."..i.."   ")
  168.         sleep(1)
  169.     end
  170. number = #navdata + 1
  171. http.post(uri,"{\"content\":\"```yaml\\nAutoPilot: Failed Executing node "..number..".```\"}",{['content-type']="application/json"})
  172. fs.delete(navdatafile)
  173. fs.copy(navbackup, navdatafile)
  174. print("The ship did not jump.")
  175. if failping >= 0 then
  176. http.post(uri,"{\"content\":\""..name.."\"}",{['content-type']="application/json"})
  177. end
  178. sleep(3)
  179. os.reboot()
  180. end
  181. end
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement