Advertisement
infiniteblock

Untitled

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