Advertisement
infiniteblock

Untitled

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