Advertisement
infiniteblock

Untitled

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