MegiRicky

BradleyTurret

Oct 17th, 2024 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2. local cannon = peripheral.find("cbcmodernwarfare:compact_mount")
  3.  
  4. local cannonHitPosChannel = nil
  5. print("Input the channel you want to use, default: 700")
  6. cannonHitPosChannel = io.read()
  7. if cannonHitPosChannel == "" then
  8. cannonHitPosChannel = 700
  9. end
  10. cannonHitPosChannel = tonumber(cannonHitPosChannel)
  11.  
  12. print("Input the channel you want to use, default: 500")
  13. controlChannel = io.read()
  14. if controlChannel == "" then
  15. controlChannel = 500
  16. end
  17. controlChannel = tonumber(controlChannel)
  18.  
  19. print("Do you want to disassemble cannon, yes or no, press enter to skip")
  20. disassemble = io.read()
  21. if disassemble == "yes" then
  22. cannon.disassemble()
  23. error("disassebled")
  24. end
  25.  
  26. print("Input the yaw compensation you want to use, default: 270")
  27. yawCompensation = io.read()
  28. if yawCompensation == "" then
  29. yawCompensation = 270
  30. end
  31. yawCompensation = tonumber(yawCompensation)
  32.  
  33. cannon.assemble()
  34.  
  35. if modem then
  36. modem.open(cannonHitPosChannel)
  37. modem.open(controlChannel)
  38. end
  39.  
  40. local distance = 100
  41. local setToPitch, controls
  42.  
  43. -- Normalize a vector
  44. local function normalizeVector(v)
  45. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  46. if length == 0 then
  47. return {0, 0, 0}
  48. end
  49. return {v[1] / length, v[2] / length, v[3] / length}
  50. end
  51.  
  52. -- Normalize the rotation matrix
  53. local function normalizeRotationMatrix(rotMatrix)
  54. local normalizedMatrix = {}
  55. for i = 1, #rotMatrix do
  56. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  57. end
  58. return normalizedMatrix
  59. end
  60.  
  61. -- Get the pitch of the ship
  62. local function getPitch()
  63. local rotMatrix = ship.getTransformationMatrix()
  64. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  65. return math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  66. end
  67. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  68. -- Get the yaw of the ship
  69. local function getYaw()
  70. local rotMatrix = ship.getTransformationMatrix()
  71. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  72. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) -- Extract yaw from the matrix
  73. end
  74.  
  75. -- Get the roll of the ship
  76. local function getRoll()
  77. local rotMatrix = ship.getTransformationMatrix()
  78. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  79. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  80. end
  81.  
  82. --Main loop
  83. local function modemMessage()
  84. while true do
  85. if modem then
  86. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  87. if channel == cannonHitPosChannel then
  88. setToPitch = message
  89. elseif channel == controlChannel then
  90. controls = message
  91. end
  92. else
  93. sleep()
  94. end
  95. end
  96. end
  97.  
  98. local function findRelativeAngle(targetYaw,targetPitch)
  99. rot = ship.getQuaternion()
  100. cacheYaw = math.pi - math.rad(targetYaw)
  101. cachePitch = - math.rad(targetPitch)
  102. rotMatAdj11 = 1 - 2*(rot.x^2 + rot.y^2)
  103. rotMatAdj12 = 2*(rot.z * rot.x + rot.y * rot.w)
  104. rotMatAdj13 = 2*(rot.z * rot.y - rot.x * rot.w)
  105. rotMatAdj21 = 2*(rot.z * rot.x - rot.y * rot.w)
  106. rotMatAdj22 = 1 - 2*(rot.z^2 + rot.y^2)
  107. rotMatAdj23 = 2*(rot.x * rot.y + rot.z * rot.w)
  108. rotMatAdj31 = 2*(rot.z * rot.y + rot.x * rot.w)
  109. rotMatAdj32 = 2*(rot.x * rot.y - rot.z * rot.w)
  110. rotMatAdj33 = 1 - 2*(rot.z^2 + rot.x^2)
  111.  
  112. rotMatTGT11 = math.cos(cacheYaw) * math.cos(cachePitch)
  113. rotMatTGT21 = math.sin(cacheYaw) * math.cos(cachePitch)
  114. rotMatTGT31 = - math.sin(cachePitch)
  115.  
  116. rotMatRSLT11 = rotMatAdj11 * rotMatTGT11 + rotMatAdj12 * rotMatTGT21 + rotMatAdj13 * rotMatTGT31
  117. rotMatRSLT21 = rotMatAdj21 * rotMatTGT11 + rotMatAdj22 * rotMatTGT21 + rotMatAdj23 * rotMatTGT31
  118. rotMatRSLT31 = rotMatAdj31 * rotMatTGT11 + rotMatAdj32 * rotMatTGT21 + rotMatAdj33 * rotMatTGT31
  119.  
  120. turretYaw = math.atan2(rotMatRSLT21, rotMatRSLT11)
  121. barrelPitch = math.asin(-rotMatRSLT31)
  122. return math.deg(-turretYaw),math.deg(-barrelPitch)
  123. end
  124.  
  125.  
  126. local function setPitch()
  127. while true do
  128. if cannon and setToPitch and setToPitch.yaw and setToPitch.pitch then
  129. print(textutils.serialize(setToPitch))
  130. local requiredRelativeYaw, requiredRelativePitch = findRelativeAngle(setToPitch.yaw,setToPitch.pitch)
  131. cannon.setPitch(math.max(requiredRelativePitch,-10))
  132. cannon.setYaw(math.min(math.max(requiredRelativeYaw,yawCompensation-360-3),yawCompensation-360+3))
  133. end
  134. sleep()
  135. end
  136. end
  137.  
  138. local function fire()
  139. while true do
  140. if controls and controls.fireCannon then
  141. print("firing")
  142. cannon.fire()
  143. redstone.setOutput("back",true)
  144. else
  145. redstone.setOutput("back",false)
  146. end
  147. if controls and controls.fireAutocannon then
  148. print("firing autocannon")
  149. redstone.setOutput("bottom",true)
  150. else
  151. redstone.setOutput("bottom",false)
  152. end
  153. sleep()
  154. end
  155. end
  156.  
  157. parallel.waitForAny(
  158. function()
  159. while true do
  160. local hitPos = {}
  161. local sourceData = {}
  162.  
  163. cannonPitch = cannon.getPitch()
  164.  
  165. local source = ship.getWorldspacePosition()
  166. local sourceX, sourceY, sourceZ = source.x, source.y, source.z
  167.  
  168. local yaw = math.deg(getYaw()) + yawCompensation
  169.  
  170. local shipPitch = math.deg(getRoll())
  171. local shipRoll = math.deg(getPitch())
  172. --print("cannonPitch: "..cannonPitch)
  173. --print(" shipPitch: "..shipPitch)
  174. --print("shipRoll: "..shipRoll)
  175. local pitch = cannonPitch * math.sin(math.rad(90-shipRoll)) + shipPitch
  176. local yawAdjust = cannonPitch * math.cos(math.rad(90 - shipRoll))
  177. --print("yawAd: "..yawAdjust)
  178. if shipRoll < 0 then
  179. yaw = yaw + math.abs(yawAdjust)
  180. else
  181. yaw = yaw - math.abs(yawAdjust)
  182. end
  183. if yaw > 360 then yaw = yaw - 360 end
  184. if yaw < 0 then yaw = yaw + 360 end
  185. local yawRad = math.rad(yaw)
  186. local pitchRad = math.rad(pitch)
  187.  
  188. local dx = distance * math.cos(pitchRad) * math.sin(yawRad)
  189. local dz = -distance * math.cos(pitchRad) * math.cos(yawRad)
  190. local dy = distance * math.sin(pitchRad)
  191.  
  192. local targetX = sourceX + dx
  193. local targetY = sourceY + dy
  194. local targetZ = sourceZ + dz
  195.  
  196. --print("targetX: "..targetX.." targetY: "..targetY.." targetZ: "..targetZ)
  197.  
  198. --print(" pitch: "..pitch)
  199. --print(" yaw: "..yaw)
  200.  
  201. table.insert(sourceData, 1, targetX)
  202. table.insert(sourceData, 2, targetY)
  203. table.insert(sourceData, 3, targetZ)
  204. table.insert(sourceData, 4, "coordinate")
  205. table.insert(sourceData, 5, source)
  206. table.insert(sourceData, 6, pitch)
  207. table.insert(sourceData, 7, yaw)
  208. modem.transmit(cannonHitPosChannel,0,sourceData)
  209.  
  210. sleep()
  211. end
  212. end,
  213. setPitch,
  214. modemMessage,
  215. fire
  216. )
  217.  
Advertisement
Add Comment
Please, Sign In to add comment