Advertisement
jaklsfjlsak

ap2

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