Advertisement
infiniteblock

Untitled

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