MegiRicky

ZTZ-99Turret

Oct 29th, 2024 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 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. cannon.assemble()
  27.  
  28. if modem then
  29. modem.open(cannonHitPosChannel)
  30. modem.open(controlChannel)
  31. end
  32.  
  33. local distance = 100
  34. local setToPitch, controls
  35.  
  36. -- Normalize a vector
  37. local function normalizeVector(v)
  38. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  39. if length == 0 then
  40. return {0, 0, 0}
  41. end
  42. return {v[1] / length, v[2] / length, v[3] / length}
  43. end
  44.  
  45. -- Normalize the rotation matrix
  46. local function normalizeRotationMatrix(rotMatrix)
  47. local normalizedMatrix = {}
  48. for i = 1, #rotMatrix do
  49. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  50. end
  51. return normalizedMatrix
  52. end
  53.  
  54. -- Get the pitch of the ship
  55. local function getPitch()
  56. local rotMatrix = ship.getTransformationMatrix()
  57. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  58. return math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  59. end
  60. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  61. -- Get the yaw of the ship
  62. local function getYaw()
  63. local rotMatrix = ship.getTransformationMatrix()
  64. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  65. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) -- Extract yaw from the matrix
  66. end
  67.  
  68. -- Get the roll of the ship
  69. local function getRoll()
  70. local rotMatrix = ship.getTransformationMatrix()
  71. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  72. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  73. end
  74.  
  75. --Main loop
  76. local function modemMessage()
  77. while true do
  78. if modem then
  79. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  80. if channel == cannonHitPosChannel then
  81. setToPitch = message
  82. elseif channel == controlChannel then
  83. controls = message
  84. end
  85. else
  86. sleep()
  87. end
  88. end
  89. end
  90.  
  91. local function findRelativeAngle(targetYaw,targetPitch)
  92. rot = ship.getQuaternion()
  93. cacheYaw = math.pi - math.rad(targetYaw)
  94. cachePitch = - math.rad(targetPitch)
  95. rotMatAdj11 = 1 - 2*(rot.x^2 + rot.y^2)
  96. rotMatAdj12 = 2*(rot.z * rot.x + rot.y * rot.w)
  97. rotMatAdj13 = 2*(rot.z * rot.y - rot.x * rot.w)
  98. rotMatAdj21 = 2*(rot.z * rot.x - rot.y * rot.w)
  99. rotMatAdj22 = 1 - 2*(rot.z^2 + rot.y^2)
  100. rotMatAdj23 = 2*(rot.x * rot.y + rot.z * rot.w)
  101. rotMatAdj31 = 2*(rot.z * rot.y + rot.x * rot.w)
  102. rotMatAdj32 = 2*(rot.x * rot.y - rot.z * rot.w)
  103. rotMatAdj33 = 1 - 2*(rot.z^2 + rot.x^2)
  104.  
  105. rotMatTGT11 = math.cos(cacheYaw) * math.cos(cachePitch)
  106. rotMatTGT21 = math.sin(cacheYaw) * math.cos(cachePitch)
  107. rotMatTGT31 = - math.sin(cachePitch)
  108.  
  109. rotMatRSLT11 = rotMatAdj11 * rotMatTGT11 + rotMatAdj12 * rotMatTGT21 + rotMatAdj13 * rotMatTGT31
  110. rotMatRSLT21 = rotMatAdj21 * rotMatTGT11 + rotMatAdj22 * rotMatTGT21 + rotMatAdj23 * rotMatTGT31
  111. rotMatRSLT31 = rotMatAdj31 * rotMatTGT11 + rotMatAdj32 * rotMatTGT21 + rotMatAdj33 * rotMatTGT31
  112.  
  113. turretYaw = math.atan2(rotMatRSLT21, rotMatRSLT11)
  114. barrelPitch = math.asin(-rotMatRSLT31)
  115. return math.deg(-turretYaw),math.deg(-barrelPitch)
  116. end
  117.  
  118.  
  119. local function setPitch()
  120. while true do
  121. if cannon and setToPitch and setToPitch.yaw and setToPitch.pitch then
  122. print(textutils.serialize(setToPitch))
  123. --[[local shipPitch = -math.deg(ship.getPitch())
  124. local shipRoll = math.deg(ship.getRoll())
  125. RequiredCannonPitch = (setToPitch - shipPitch) / math.sin(math.rad(90-shipRoll))
  126. cannon.setPitch(math.max(RequiredCannonPitch,-3))]]
  127. local requiredRelativeYaw, requiredRelativePitch = findRelativeAngle(setToPitch.yaw,setToPitch.pitch)
  128. cannon.setPitch(math.max(requiredRelativePitch,-10))
  129. cannon.setYaw(math.min(math.max(requiredRelativeYaw,-3),3))
  130. end
  131. sleep()
  132. end
  133. end
  134.  
  135. local function fire()
  136. while true do
  137. if controls then
  138. --print(textutils.serialize(controls))
  139. end
  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())
  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