Advertisement
Guest User

startup

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