MegiRicky

55aVLSOnboard

Nov 16th, 2024 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.13 KB | None | 0 0
  1. local modem = peripheral.wrap("right")
  2. local speaker = peripheral.find("speaker")
  3.  
  4. local cannons = {}
  5. local controls = {cannonControlMode = "mouseAim"}
  6. local targetInfo = {}
  7.  
  8. local k = 1
  9. local nilCount = 0
  10. local i = 0
  11. while nilCount < 200 do
  12. local cannon = peripheral.wrap("cbc_cannon_mount_"..tostring(i))
  13. if not(cannon) then
  14. cannon = peripheral.wrap("cbcmf_compact_cannon_mount_"..tostring(i))
  15. end
  16. if not(cannon) then
  17. cannon = peripheral.wrap("createbigcannons:cannon_mount_"..tostring(i))
  18. end
  19. if cannon then
  20. cannons[k] = cannon
  21. k = k + 1
  22. nilCount = 0
  23. print("found cannon")
  24. else
  25. nilCount = nilCount + 1
  26. end
  27. i = i + 1
  28. end
  29. print("Found "..#cannons.." cannons")
  30.  
  31. print("Input the controlChannel number, default: 500")
  32. local controlChannel = io.read()
  33. if controlChannel == "" then
  34. controlChannel = 500
  35. end
  36. controlChannel = tonumber(controlChannel)
  37.  
  38. print("Input the cannon channel number, default: 900")
  39. local cannonChannel = io.read()
  40. if cannonChannel == "" then
  41. cannonChannel = 900
  42. end
  43. cannonChannel = tonumber(cannonChannel)
  44.  
  45. print("Input the muzzle velocity number, default: 240")
  46. local projectileSpeed = io.read()
  47. if projectileSpeed == "" then
  48. projectileSpeed = 230
  49. end
  50. projectileSpeed = tonumber(projectileSpeed)
  51.  
  52. print("Input the gravity acceleration per tick, default: 0.05")
  53. local g = io.read()
  54. if g == "" then
  55. g = 0.05
  56. end
  57. g = tonumber(g)
  58.  
  59. print("Input the drag per tick, default: 0.99")
  60. local cd = io.read()
  61. if cd == "" then
  62. cd = 0.995
  63. end
  64. cd = tonumber(cd)
  65.  
  66. print("Enable automatic spread, yes/no, default: yes")
  67. local autoSpread = io.read()
  68. if autoSpread == "no" then
  69. autoSpread = false
  70. else
  71. autoSpread = true
  72. end
  73.  
  74. print("Enable pitch yaw limiter, yes/no, default: yes")
  75. local limitor = io.read()
  76. if limitor == "no" then
  77. limitor = false
  78. else
  79. limitor = true
  80. end
  81. local pitchUpperLimit = -6
  82.  
  83. print("Do you want to hide cannon?yes/no, default: yes")
  84. local hideCannon = io.read()
  85. if hideCannon == "no" then
  86. hideCannon = false
  87. else
  88. hideCannon = true
  89. end
  90.  
  91.  
  92. if modem then
  93. modem.open(cannonChannel)
  94. modem.open(controlChannel)
  95. end
  96.  
  97. local function setPitchYaw(targetPitch, targetYaw, useLowPitch, targetDistance)
  98. local tasks = {} -- Table to store each cannon's task
  99. local spreadMultiplier = 1
  100.  
  101. -- Increase random variation if low-pitch solution is used or the target is fast
  102. if useLowPitch then
  103. -- Scale spreadMultiplier inversely with targetDistance
  104. -- The closer the target, the higher the spread; further targets have lower spread
  105. spreadMultiplier = math.max(1, 3 / (targetDistance / 100)) -- Adjust scale factor as needed
  106. print("spreadMultiplier: "..spreadMultiplier)
  107. end
  108.  
  109. for index, cannon in pairs(cannons) do
  110. local spreadConstant = 0.3 * spreadMultiplier
  111. local yawVariation
  112.  
  113. local pitchVariation = math.random(-2, 2) * spreadConstant -- Adjust the range to control the spread
  114. if targetPitch < 20 then
  115. yawVariation = math.random(-2, 2) * spreadConstant * 6 -- Adjust the range to control the spread
  116. elseif targetPitch < 30 then
  117. yawVariation = math.random(-2, 2) * spreadConstant * 4
  118. else
  119. yawVariation = math.random(-2, 2) * spreadConstant * 2
  120. end
  121.  
  122. local adjustedPitch = targetPitch + pitchVariation
  123. local adjustedYaw = targetYaw + yawVariation
  124.  
  125. -- Define the function for this cannon's pitch/yaw adjustment
  126. local function adjustCannon()
  127. if autoSpread then
  128. cannon.setPitch(adjustedPitch)
  129. cannon.setYaw(adjustedYaw)
  130. else
  131. cannon.setPitch(targetPitch)
  132. cannon.setYaw(targetYaw)
  133. end
  134. end
  135.  
  136. -- Add this task to the tasks table
  137. table.insert(tasks, adjustCannon)
  138. end
  139.  
  140. -- Execute all tasks in parallel
  141. parallel.waitForAll(table.unpack(tasks))
  142. end
  143.  
  144.  
  145. local function findRelativeAngle(targetYaw,targetPitch)
  146. rot = ship.getQuaternion()
  147. cacheYaw = math.pi - math.rad(targetYaw)
  148. cachePitch = - math.rad(targetPitch)
  149. rotMatAdj11 = 1 - 2*(rot.x^2 + rot.y^2)
  150. rotMatAdj12 = 2*(rot.z * rot.x + rot.y * rot.w)
  151. rotMatAdj13 = 2*(rot.z * rot.y - rot.x * rot.w)
  152. rotMatAdj21 = 2*(rot.z * rot.x - rot.y * rot.w)
  153. rotMatAdj22 = 1 - 2*(rot.z^2 + rot.y^2)
  154. rotMatAdj23 = 2*(rot.x * rot.y + rot.z * rot.w)
  155. rotMatAdj31 = 2*(rot.z * rot.y + rot.x * rot.w)
  156. rotMatAdj32 = 2*(rot.x * rot.y - rot.z * rot.w)
  157. rotMatAdj33 = 1 - 2*(rot.z^2 + rot.x^2)
  158.  
  159. rotMatTGT11 = math.cos(cacheYaw) * math.cos(cachePitch)
  160. rotMatTGT21 = math.sin(cacheYaw) * math.cos(cachePitch)
  161. rotMatTGT31 = - math.sin(cachePitch)
  162.  
  163. rotMatRSLT11 = rotMatAdj11 * rotMatTGT11 + rotMatAdj12 * rotMatTGT21 + rotMatAdj13 * rotMatTGT31
  164. rotMatRSLT21 = rotMatAdj21 * rotMatTGT11 + rotMatAdj22 * rotMatTGT21 + rotMatAdj23 * rotMatTGT31
  165. rotMatRSLT31 = rotMatAdj31 * rotMatTGT11 + rotMatAdj32 * rotMatTGT21 + rotMatAdj33 * rotMatTGT31
  166.  
  167. turretYaw = math.atan2(rotMatRSLT21, rotMatRSLT11)
  168. barrelPitch = math.asin(-rotMatRSLT31)
  169. return math.deg(-turretYaw),math.deg(-barrelPitch)
  170. end
  171.  
  172. local function calculateRange(angle, u, cd, g, c_est, projectileSpeed)
  173.  
  174. local radians = math.rad(angle)
  175. local u = projectileSpeed/20
  176. local part1 = u * math.cos(radians) / math.log(cd)
  177.  
  178. local part2 = ((g * cd) / (g * cd + (1 - cd) * u * math.sin(radians))) ^ (2 + c_est * projectileSpeed * math.sin(radians)) - 1
  179.  
  180. local XR = part1 * part2
  181.  
  182. return XR
  183. end
  184.  
  185. local function findBestPitch(targetX, targetY, targetZ, sourceX, sourceY, sourceZ, initialVelocity, g, cd, c_est, projectileSpeed)
  186. local bestLowPitch = nil
  187. local bestHighPitch = nil
  188. local bestLowDistance = math.huge
  189. local bestHighDistance = math.huge
  190. local targetDistance = math.sqrt((targetX - sourceX)^2 + (targetZ - sourceZ)^2)
  191. for pitch = 0, 90, 0.05 do -- Iterate over pitch angles
  192. local calculatedRange = calculateRange(pitch, initialVelocity, cd, g, c_est, projectileSpeed)
  193. local distanceDifference = math.abs(calculatedRange - targetDistance)
  194.  
  195. -- Find the low-angle solution
  196. if pitch <= 30 then
  197. if distanceDifference < bestLowDistance then
  198. bestLowDistance = distanceDifference
  199. bestLowPitch = pitch
  200. end
  201. -- Find the high-angle solution
  202. elseif pitch > 30 then
  203. if distanceDifference < bestHighDistance then
  204. bestHighDistance = distanceDifference
  205. bestHighPitch = pitch
  206. end
  207. end
  208. end
  209.  
  210. return bestLowPitch, bestHighPitch
  211. end
  212.  
  213. local function aimCannon(targetPos, targetVel, sourceX, sourceY, sourceZ)
  214. if sourceX and sourceY and sourceZ then
  215. local dx = targetPos.x - sourceX
  216. local dy = targetPos.y - sourceY
  217. local dz = targetPos.z - sourceZ
  218. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  219.  
  220. local estimateTime = distance / (projectileSpeed) -- assuming velocity units and time units need adjustment
  221. local estimateX = targetPos.x + targetVel.x * estimateTime
  222. local estimateY = targetPos.y + targetVel.y * estimateTime
  223. local estimateZ = targetPos.z + targetVel.z * estimateTime
  224. local targetSpeed = math.sqrt(targetVel.x^2 + targetVel.y^2 + targetVel.z^2)
  225.  
  226. local dx = estimateX - sourceX
  227. local dy = estimateY - sourceY
  228. local dz = estimateZ - sourceZ
  229.  
  230. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  231. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  232.  
  233. local yaw = math.deg(math.atan2(-dx, dz))
  234. yaw = (yaw + 180) % 360
  235.  
  236. local bestLowPitch, bestHighPitch = findBestPitch(estimateX, estimateY, estimateZ, sourceX, sourceY, sourceZ, projectileSpeed, g, cd, 0.0028, projectileSpeed)
  237. print("pitch: "..pitch)
  238. print("bestLowPitch: "..bestLowPitch)
  239. local requiredRelativeYaw, requiredRelativeLowPitch = findRelativeAngle(yaw, bestLowPitch)
  240. local lowPitchAllowed = requiredRelativeLowPitch and (requiredRelativeLowPitch >= 20) -- Ensure low pitch is valid
  241. local selectedPitch = nil
  242. local useLowPitch = false
  243.  
  244. if targetSpeed > 0 and lowPitchAllowed then
  245. selectedPitch = bestLowPitch
  246. useLowPitch = true
  247. else
  248. selectedPitch = bestHighPitch or bestLowPitch
  249. useLowPitch = false
  250. end
  251.  
  252. local requiredRelativeYaw, requiredRelativePitch = findRelativeAngle(yaw, selectedPitch)
  253. local currentPitch = cannons[1].getPitch()
  254. local currentYaw = cannons[1].getYaw()
  255. print("requiredRelativePitch: "..requiredRelativePitch)
  256. -- Check if the delta yaw and pitch are greater than 0.1
  257. if math.abs(currentPitch - (90 - requiredRelativePitch)) > 0.05 or math.abs(currentYaw - (requiredRelativeYaw + 180)) > 0.05 then
  258. if limitor and controls.cannonControlMode == "mouseAim" then
  259. -- Limit pitch based on yaw
  260. if requiredRelativeYaw + 180 >= 290 or requiredRelativeYaw <= 60 + 180 then
  261. setPitchYaw(math.min(90 - requiredRelativePitch, 70), requiredRelativeYaw + 180, useLowPitch, distance)
  262. else
  263. setPitchYaw(math.min(90 - requiredRelativePitch, 18), requiredRelativeYaw + 180, useLowPitch, distance)
  264. end
  265. local pitchLimit = 18
  266. if yaw >= 290 or yaw <= 60 then
  267. pitchLimit = 70
  268. end
  269. if (90 - requiredRelativePitch) > pitchLimit then
  270. -- Play warning tone if the pitch is not achievable
  271. if speaker then
  272. speaker.playSound("minecraft:block.note_block.bass", 1.5, 1.0) -- Example warning tone
  273. end
  274. else
  275. if speaker then
  276. speaker.playSound("minecraft:block.note_block.pling", 1.5, 5.0) -- Locked tone
  277. end
  278. end
  279. else
  280. setPitchYaw(90 - requiredRelativePitch, requiredRelativeYaw + 180, useLowPitch, distance)
  281. end
  282. end
  283. end
  284. end
  285.  
  286.  
  287. local function manualCannonControl()
  288. while true do
  289. if controls.cannonControlMode == "manual" then
  290. if controls then
  291. local pitch = cannons[1].getPitch()
  292. local yaw = cannons[1].getYaw()
  293. -- Control yaw and pitch manually with arrow keys
  294. if controls.cannonUp then
  295. -- Increase pitch
  296. setPitchYaw(pitch + 2, yaw)
  297. elseif controls.cannonDown then
  298. -- Decrease pitch
  299. setPitchYaw(pitch - 2, yaw)
  300. else
  301.  
  302. end
  303. if controls.cannonLeft then
  304. -- Turn left
  305. setPitchYaw(pitch, yaw - 2)
  306. elseif controls.cannonRight then
  307. -- Turn right
  308. setPitchYaw(pitch, yaw + 2)
  309. else
  310.  
  311. end
  312. else
  313.  
  314. end
  315. sleep()
  316. else
  317. sleep() -- Sleep for a short time if not in manual mode
  318. end
  319. end
  320. end
  321.  
  322. local function setHiddenPosition()
  323. local tasks = {} -- Table to store each cannon's task for setting to hidden position
  324.  
  325. for index, cannon in pairs(cannons) do
  326. local function hideCannon()
  327. cannon.setPitch(90)
  328. cannon.setYaw(180)
  329. end
  330. table.insert(tasks, hideCannon)
  331. end
  332.  
  333. -- Execute all tasks in parallel to hide cannons simultaneously
  334. parallel.waitForAll(table.unpack(tasks))
  335. end
  336.  
  337. local function firing()
  338. while true do
  339. if controls and controls.fireCannon and controls.cannonControlMode == "mouseAim" then
  340. print("firing")
  341. local tasks = {} -- Initialize tasks table to store firing functions
  342.  
  343. -- Aim each cannon before firing wa
  344. if targetInfo and targetInfo.targetPos and targetInfo.targetVel then
  345. local source = ship.getWorldspacePosition()
  346. local sourceX = source.x + 42 * math.cos(ship.getYaw() + math.pi / 2)
  347. local sourceY = source.y + 1.5
  348. local sourceZ = source.z + 42 * math.sin(ship.getYaw() + math.pi / 2)
  349. print("sourceX: "..sourceX)
  350. print("sourceY: "..sourceY)
  351. print("sourceZ: "..sourceZ)
  352.  
  353. aimCannon(targetInfo.targetPos, targetInfo.targetVel, sourceX, sourceY, sourceZ)
  354. end
  355.  
  356. -- Create a task for each cannon to fire
  357. for index, cannon in pairs(cannons) do
  358. local function fireCannon()
  359. cannon.fire()
  360. end
  361. table.insert(tasks, fireCannon)
  362. end
  363. redstone.setOutput("top",true)
  364.  
  365. --sleep to ensure that all cannon is adjusted
  366. sleep(0.1)
  367.  
  368. -- Execute all cannon fire tasks simultaneously
  369. parallel.waitForAll(table.unpack(tasks))
  370.  
  371. --sleep again to ensure all cannon fired
  372. sleep(0.1)
  373. redstone.setOutput("top",false)
  374.  
  375. -- Set cannons back to hidden position after firing
  376. if hideCannon then
  377. setHiddenPosition()
  378. end
  379. else
  380. -- If not firing, keep all cannons in hidden position
  381. if controls.cannonControlMode == "mouseAim" and hideCannon then
  382. setHiddenPosition()
  383. end
  384. if controls and controls.cannonControlMode == "manual" and controls.fireCannon then
  385. local tasks = {}
  386. for index, cannon in pairs(cannons) do
  387. local function fireCannon()
  388. cannon.fire()
  389. end
  390. table.insert(tasks, fireCannon)
  391. end
  392. parallel.waitForAll(table.unpack(tasks))
  393. end
  394. if targetInfo and targetInfo.targetPos and targetInfo.targetVel then
  395. --print(textutils.serialize(targetInfo))
  396. local source = ship.getWorldspacePosition()
  397. local sourceX = source.x + 42 * math.cos(ship.getYaw() + math.pi / 2)
  398. local sourceY = source.y + 1.5
  399. local sourceZ = source.z + 42 * math.sin(ship.getYaw() + math.pi / 2)
  400.  
  401. local dx = targetInfo.targetPos.x - sourceX
  402. local dy = targetInfo.targetPos.y - sourceY
  403. local dz = targetInfo.targetPos.z - sourceZ
  404. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  405.  
  406. local estimateTime = distance / (projectileSpeed) -- assuming velocity units and time units need adjustment
  407. local estimateX = targetInfo.targetPos.x + targetInfo.targetVel.x * estimateTime
  408. local estimateY = targetInfo.targetPos.y + targetInfo.targetVel.y * estimateTime
  409. local estimateZ = targetInfo.targetPos.z + targetInfo.targetVel.z * estimateTime
  410.  
  411. local dx = estimateX - sourceX
  412. local dy = estimateY - sourceY
  413. local dz = estimateZ - sourceZ
  414.  
  415. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  416. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  417.  
  418. local yaw = math.deg(math.atan2(-dx, dz))
  419. yaw = (yaw + 180) % 360
  420. pitch = findBestPitch(estimateX, estimateY, estimateZ, sourceX, sourceY, sourceZ, projectileSpeed, g, cd, 0.0028, projectileSpeed)
  421.  
  422. local shipRoll = math.deg(ship.getRoll())
  423. local shipYaw = math.deg(ship.getYaw()) - 90
  424. if shipYaw < 0 then shipYaw = shipYaw + 360 end
  425. local shipPitch = math.deg(ship.getPitch())
  426.  
  427. local requiredRelativeYaw, requiredRelativePitch = findRelativeAngle(yaw, pitch)
  428. local currentPitch = cannons[1].getPitch()
  429. local currentYaw = cannons[1].getYaw()
  430.  
  431. if limitor and controls.cannonControlMode == "mouseAim" then
  432. -- Limit pitch based on yaw
  433. local pitchLimit = 18
  434. if yaw >= 290 or yaw <= 60 then
  435. pitchLimit = 70
  436. end
  437. if (90 - requiredRelativePitch) > pitchLimit then
  438. -- Play warning tone if the pitch is not achievable
  439. if speaker and targetInfo and targetInfo.targetPos and targetInfo.targetVel then
  440. speaker.playSound("minecraft:block.note_block.bass", 1.5, 1.0) -- Example warning tone
  441. end
  442. else
  443. if speaker and targetInfo and targetInfo.targetPos and targetInfo.targetVel then
  444. print("Target locked line 423")
  445. speaker.playSound("minecraft:block.note_block.pling", 1.5, 5.0) -- Locked tone
  446. end
  447. end
  448. end
  449. end
  450. end
  451. sleep()
  452. end
  453. end
  454.  
  455. local function modemMessage()
  456. while true do
  457. if modem then
  458. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  459. if channel == cannonChannel then
  460. targetInfo = message
  461. elseif channel == controlChannel then
  462. controls = message
  463. end
  464. else
  465. sleep()
  466. end
  467. end
  468. end
  469.  
  470. local function main()
  471. while true do
  472. if not(hideCannon) then
  473. local source = ship.getWorldspacePosition()
  474. local sourceX = source.x
  475. local sourceY = source.y + 1.5
  476. local sourceZ = source.z
  477.  
  478. -- Calculate cannon position offset based on ship's yaw
  479. local yawRadians = ship.getYaw() + 0.5 * math.pi
  480. local cannonOffsetX = 42 * math.cos(yawRadians) -- 84 blocks ahead in the x direction
  481. local cannonOffsetZ = 42 * math.sin(yawRadians) -- 84 blocks ahead in the z direction
  482.  
  483. -- Adjust source coordinates to the actual cannon position
  484. local adjustedSourceX = sourceX + cannonOffsetX
  485. local adjustedSourceZ = sourceZ + cannonOffsetZ
  486. --print("adjustedSourceX: "..adjustedSourceX)
  487. --print("adjustedSourceZ: "..adjustedSourceZ)
  488.  
  489. if targetInfo and targetInfo.targetPos and targetInfo.targetVel and controls.cannonControlMode == "mouseAim" then
  490. aimCannon(targetInfo.targetPos, targetInfo.targetVel, adjustedSourceX, sourceY, adjustedSourceZ)
  491. end
  492. end
  493. sleep()
  494. end
  495. end
  496.  
  497.  
  498. parallel.waitForAny(
  499. manualCannonControl,
  500. modemMessage,
  501. firing,
  502. main
  503. )
  504.  
Advertisement
Add Comment
Please, Sign In to add comment