MegiRicky

stationary Artillery

Aug 9th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.27 KB | None | 0 0
  1. local reader = peripheral.find("blockReader")
  2. local monitor = peripheral.find("monitor")
  3. local router = peripheral.find("redrouter")
  4. local bal = peripheral.find("ballistic_accelerator")
  5. local radar = peripheral.find("sp_radar")
  6. local controller = peripheral.find("tweaked_controller")
  7.  
  8. local function parseCoordinates(input)
  9. local x, y, z = input:match("([^%s]+)%s+([^%s]+)%s+([^%s]+)")
  10. return { x = tonumber(x), y = tonumber(y), z = tonumber(z) }
  11. end
  12.  
  13. local function directionToBearing(direction)
  14. local bearings = { N = 0, E = 90, S = 180, W = 270 }
  15. return bearings[direction]
  16. end
  17.  
  18. print("Input the cannon mount's coordinate (x y z)(space between)")
  19. local input = read()
  20. local source = parseCoordinates(input)
  21. print(source.x)
  22. print(source.y)
  23. print(source.z)
  24.  
  25. print("Input the cannon mount's facing (N or E or S or W)")
  26. local facing = read():upper()
  27. local bearing = directionToBearing(facing)
  28.  
  29. if bearing then
  30. print("True Bearing: " .. bearing .. " degrees")
  31. else
  32. print("Invalid direction. Please input N, E, S, or W.")
  33. end
  34.  
  35. -- Initialize source coordinates
  36. local sourceX = source.x
  37. local sourceY = source.y
  38. local sourceZ = source.z
  39.  
  40. local currentPitch = 0
  41. local currentYaw = 0
  42. local projectileSpeed = 160 / 20
  43. local yaw_tolerance = 0.5
  44. local pitch_tolerance = 0.12
  45. local yawCompensate = 0
  46. local pitchCompensate = 0
  47. local x, y, z = nil, nil, nil
  48. local hostileIDs = {}
  49.  
  50. local function toDegrees(radians)
  51. return radians * (180 / math.pi)
  52. end
  53.  
  54. local function toBearing(degrees)
  55. local bearing = degrees % 360
  56. if bearing < 0 then
  57. bearing = 360 + bearing
  58. end
  59. return bearing
  60. end
  61.  
  62. function table.contains(table, element)
  63. for _, value in ipairs(table) do
  64. if value == element then
  65. return true
  66. end
  67. end
  68. return false
  69. end
  70.  
  71. local function round(num)
  72. return math.floor(num * 100 + 0.5) / 100
  73. end
  74.  
  75. -- Function to convert a table of numbers into a comma-separated string
  76. local function tableToString(t)
  77. local list = {}
  78. for _, v in ipairs(t) do
  79. table.insert(list, tostring(v))
  80. end
  81. return table.concat(list, ", ")
  82. end
  83.  
  84. local function writeWithBackground(text, color)
  85. monitor.setBackgroundColor(color)
  86. monitor.write(text)
  87. monitor.setBackgroundColor(colors.black) -- Reset to default or another desired color
  88. end
  89.  
  90. function getTimeInAir(pitch, projectileSpeed, sourceY, targetY)
  91. -- Convert pitch from degrees to radians for trigonometric calculations
  92. -- Resolve the initial velocity into its vertical component
  93. local v_y = projectileSpeed * math.sin(pitch)
  94.  
  95. -- Acceleration due to gravity (m/s^2)
  96. local g = 9.81
  97.  
  98. local time = (v_y + math.sqrt(v_y * v_y + 2 * g * (sourceY - targetY))) / g
  99.  
  100. return time
  101. end
  102.  
  103. local lockedTarget = {}
  104. local selectedIndex = 1
  105. local displayData = {}
  106.  
  107. local function selectTarget()
  108. local results = radar.scanForShips(2000)
  109. local possibleTargets = {}
  110.  
  111. if not results or #results == 0 then
  112. table.insert(possibleTargets, "No objects detected.")
  113. else
  114. for i, object in ipairs(results) do
  115. local x = object.pos.x - source.x
  116. local y = object.pos.y - source.y
  117. local z = object.pos.z - source.z
  118. local distance = math.sqrt(x^2 + y^2 + z^2)
  119.  
  120. -- Check if the object's mass is greater than 10,000 before adding it to the display
  121. if object.mass > 20000 and distance > 10 then
  122.  
  123. local objectInfo = {
  124. id = object.id,
  125. mass = object.mass,
  126. pos = object.pos,
  127. velocity = object.velocity,
  128. distance = distance
  129. }
  130. table.insert(possibleTargets, objectInfo)
  131. end
  132. end
  133. -- Check if any objects passed the mass filter, if not, modify the display message
  134. end
  135.  
  136. local maxIndex = #possibleTargets
  137.  
  138. local yAxis = controller.getAxis(2)
  139. if yAxis < 0 then
  140. selectedIndex = selectedIndex - 1
  141.  
  142. elseif yAxis > 0 then
  143. selectedIndex = selectedIndex + 1
  144.  
  145. elseif controller.getButton(11) then
  146.  
  147. end
  148. if selectedIndex < 0 then
  149. selectedIndex = 0
  150. elseif selectedIndex > maxIndex then
  151. selectedIndex = maxIndex
  152. end
  153.  
  154. if selectedIndex == 0 then
  155.  
  156.  
  157. if not(x and y and z) or controller.getButton(1) then
  158. monitor.clear()
  159. monitor.setCursorPos(1,1)
  160. monitor.write("Enter Coordinates by clicking the computer")
  161. print("Enter X and press enter , leave if blank and press enter 3 times if you want to quit )")
  162. x = read()
  163. print("Enter Y and press enter (you need to restart if you accidentally press enter last time)")
  164. y = read()
  165. print("Enter Z and press enter (you need to restart if you accidentally press enter last time)")
  166. z = read()
  167. x = tonumber(x)
  168. y = tonumber(y)
  169. z = tonumber(z)
  170. end
  171. if x and y and z then
  172. local objectInfo = {
  173. id = "manual",
  174. mass = "N/A",
  175. pos = {x = x, y = y, z = z},
  176. velocity = {x = 0, y = 0, z = 0},
  177. distance = "N/A"
  178. }
  179. lockedTarget = objectInfo
  180. print(lockedTarget)
  181. print(lockedTarget.pos.x,lockedTarget.pos.y,lockedTarget.pos.z)
  182. print("Coordinates locked:", x, y, z)
  183. stored = true
  184. else
  185. selectedIndex = 1
  186. stored = false
  187. end
  188. else
  189. lockedTarget = possibleTargets[selectedIndex]
  190. end
  191.  
  192. -- Update the monitor to highlight the selected target
  193. monitor.clear()
  194. monitor.setCursorPos(1, 1)
  195. monitor.write("Position(X,Y,Z): " .. tostring(round(sourceX)) .. ", " .. tostring(round(sourceY)) .. ", " .. tostring(round(sourceZ)))
  196.  
  197. for i, line in ipairs(displayData) do
  198. monitor.setCursorPos(1, i + 3)
  199. if i == selectedIndex + 1 then
  200. monitor.setBackgroundColor(colors.blue)
  201. end
  202. monitor.write(line)
  203. monitor.setBackgroundColor(colors.black)
  204. end
  205. end
  206.  
  207. router.setOutput("front", false)
  208. router.setOutput("back", false)
  209. router.setOutput("right", false)
  210. router.setOutput("left", false)
  211. router.setOutput("top",false)
  212.  
  213. while true do
  214. currentPitch = reader.getBlockData().CannonPitch
  215. currentYaw = bearing + reader.getBlockData().CannonYaw
  216. monitor.setTextScale(0.5)
  217. results = radar.scanForShips(1000) -- Scan within a range of 1000; adjust as needed
  218. displayData = {} -- Initialize an empty table to store display information
  219.  
  220. if not results or #results == 0 then
  221. table.insert(displayData, "No objects detected.")
  222. else
  223. table.insert(displayData, "Input coordinate(; to re-enter):")
  224. for i, object in ipairs(results) do
  225. local x = object.pos.x - source.x
  226. local y = object.pos.y - source.y
  227. local z = object.pos.z - source.z
  228. local distance = math.sqrt(x^2 + y^2 + z^2)
  229.  
  230. -- Check if the object's mass is greater than 10,000 before adding it to the display
  231. if object.mass > 20000 and distance > 10 then
  232. local objectInfo = string.format("ID:%d M:%d X:%.1f Y:%.2f Z:%.1f D:%.1f",
  233. object.id, object.mass, object.pos.x, object.pos.y, object.pos.z, distance)
  234. table.insert(displayData, objectInfo)
  235. end
  236.  
  237. end
  238. -- Check if any objects passed the mass filter, if not, modify the display message
  239. if #displayData == 1 then -- Only the title has been added
  240. displayData[2] = "No objects above mass threshold detected."
  241. end
  242. end
  243. selectTarget()
  244.  
  245. if lockedTarget.id and redstone.getInput("right") then
  246. print("locked id: ",lockedTarget.id)
  247. print("target position: ",round(lockedTarget.pos.x), round(lockedTarget.pos.y), round(lockedTarget.pos.z))
  248. print("cannon position: ",round(source.x), round(source.y), round(source.z))
  249. print("distance: ",lockedTarget.distance)
  250.  
  251. cannonPos = {source.x, source.y, source.z}
  252. targetTemp = {lockedTarget.pos.x, lockedTarget.pos.y, lockedTarget.pos.z}
  253. local preCompensatedPitchTable = bal.calculatePitch(cannonPos, targetTemp, projectileSpeed, 8, -30, 90, 0.05, 0.99, 1, 1000000, 5, 20, false)
  254. print(textutils.serialize(preCompensatedPitchTable))
  255. local preCompensatedPitch = preCompensatedPitchTable[2][2]
  256. print(preCompensatedPitch)
  257. local ProjectileYSpeed = projectileSpeed * math.sin(preCompensatedPitch)
  258. if preCompensatedPitch < 0 then
  259. ProjectileYSpeed = ProjectileYSpeed * -1
  260. end
  261.  
  262. local estimateTime = getTimeInAir(preCompensatedPitch, projectileSpeed, source.y, lockedTarget.pos.y)
  263. print("estimate time: ",estimateTime)
  264.  
  265. local estimateX = lockedTarget.pos.x + lockedTarget.velocity.x * 2
  266. local estimateY = lockedTarget.pos.y + lockedTarget.velocity.y * 2
  267. local estimateZ = lockedTarget.pos.z + lockedTarget.velocity.z * 2
  268. estimate = {estimateX, estimateY, estimateZ}
  269.  
  270. local dx = estimateX - source.x
  271. local dy = estimateY - source.y
  272. local dz = estimateZ - source.z
  273.  
  274. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  275. --Manual Compesation
  276. local epsilon = 0.0001
  277. if controller.getButton(12) then
  278. pitchCompensate = pitchCompensate + 0.1
  279. end
  280. if controller.getButton(13) then
  281. yawCompensate = yawCompensate + 0.1
  282. end
  283. if controller.getButton(14) then
  284. pitchCompensate = pitchCompensate - 0.1
  285. end
  286. if controller.getButton(15) then
  287. yawCompensate = yawCompensate - 0.1
  288. end
  289. if not (math.abs(yawCompensate % 0.1) < epsilon or math.abs(yawCompensate % 0.1 - 0.1) < epsilon) then
  290. yawCompensate = 0
  291. end
  292. if not (math.abs(pitchCompensate % 0.1) < epsilon or math.abs(pitchCompensate % 0.1 - 0.1) < epsilon) then
  293. pitchCompensate = 0
  294. end
  295. monitor.setCursorPos(26, 2)
  296. monitor.write("Compensate yaw:"..yawCompensate.." Pitch:"..pitchCompensate)
  297. --CALCULATING YAW
  298. local yaw = math.deg(math.atan2(-dx, dz))
  299. yaw = yaw + 180 + yawCompensate
  300. local pitch = preCompensatedPitch + pitchCompensate
  301. print("non compesated pitch: ",pitch)
  302. print("current",currentPitch,currentYaw)
  303. print("pitch,yaw",pitch,yaw)
  304. local deltaYaw = (yaw - currentYaw + 180) % 360 - 180
  305. print("deltaYaw",deltaYaw)
  306.  
  307. -- Adjust yaw
  308. if math.abs(deltaYaw) > yaw_tolerance then
  309. if deltaYaw > 0 then
  310. print("Turning right")
  311. router.setOutput("left", false)
  312. if math.abs(deltaYaw) < 5 then
  313. local turnTime = math.abs(deltaYaw) / 6 * 0.99
  314. router.setOutput("right", true)
  315. sleep(turnTime)
  316. router.setOutput("right",false)
  317. else
  318. router.setOutput("right", true)
  319. end
  320.  
  321. else
  322. print("Turning left")
  323.  
  324. router.setOutput("right", false)
  325. if math.abs(deltaYaw) < 5 then
  326. local turnTime = math.abs(deltaYaw) / 6 * 0.99
  327. router.setOutput("left", true)
  328. sleep(turnTime)
  329. router.setOutput("left",false)
  330. else
  331. router.setOutput("left", true)
  332. end
  333. end
  334. monitor.setCursorPos(1, 2)
  335. writeWithBackground("Yaw: " .. tostring(round(currentYaw)), colors.red)
  336. else
  337. -- Target within tolerance, stop rotation
  338. monitor.setCursorPos(1, 2)
  339. writeWithBackground("Yaw: " .. tostring(round(currentYaw)), colors.green)
  340. router.setOutput("right", false)
  341. router.setOutput("left", false)
  342. end
  343.  
  344. local deltaPitch = pitch - currentPitch
  345. if math.abs(deltaPitch) > pitch_tolerance then
  346. if deltaPitch > 0 then
  347. print("Adjusting pitch up")
  348. router.setOutput("front", false)
  349. if math.abs(deltaPitch) < 3 then
  350. local turnTime = math.abs(deltaPitch) / 6 * 0.99
  351. router.setOutput("top", true)
  352. sleep(turnTime)
  353. router.setOutput("top",false)
  354. else
  355. router.setOutput("top", true)
  356. end
  357. else
  358. print("Adjusting pitch down")
  359. router.setOutput("top", false)
  360. if math.abs(deltaPitch) < 3 then
  361. local turnTime = math.abs(deltaPitch) / 6 * 0.99
  362. router.setOutput("front", true)
  363. sleep(turnTime)
  364. router.setOutput("front",false)
  365. else
  366. router.setOutput("front", true)
  367. end
  368. end
  369. monitor.setCursorPos(13, 2)
  370. writeWithBackground(" Pitch: " .. tostring(round(currentPitch)), colors.red)
  371. else
  372. monitor.setCursorPos(13, 2)
  373. writeWithBackground(" Pitch: " .. tostring(round(currentPitch)), colors.green)
  374. router.setOutput("top", false)
  375. router.setOutput("back", false)
  376. end
  377. local status = reader.getBlockData().Running
  378. if status == 1 then
  379. monitor.setCursorPos(19, 3)
  380. writeWithBackground("Assembled", colors.green)
  381. else
  382. monitor.setCursorPos(19, 3)
  383. writeWithBackground("Not Assembled", colors.red)
  384. end
  385. sleep(0.0001)
  386.  
  387. else
  388. router.setOutput("front", false)
  389. router.setOutput("back", false)
  390. router.setOutput("right", false)
  391. router.setOutput("left", false)
  392. router.setOutput("top",false)
  393. end
  394. sleep(0.01)
  395. end
Add Comment
Please, Sign In to add comment