Advertisement
infiniteblock

Untitled

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