Advertisement
infiniteblock

Untitled

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