jaklsfjlsak

自 驾 屏

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