Advertisement
infiniteblock

Untitled

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