Advertisement
Guest User

startup

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