MegiRicky

EMBTCoax

Mar 8th, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2. local cannon = peripheral.find("cbcmodernwarfare:compact_mount")
  3.  
  4. local cannonHitPosChannel = nil
  5.  
  6. print("Input the EMBT control channel, default: 2000")
  7. controlChannel = io.read()
  8. if controlChannel == "" then
  9. controlChannel = 2000
  10. end
  11. controlChannel = tonumber(controlChannel)
  12. cannonHitPosChannel = controlChannel + 10
  13.  
  14. print("Do you want to disassemble cannon, yes or no, press enter to skip")
  15. disassemble = io.read()
  16. if disassemble == "yes" then
  17. cannon.disassemble()
  18. error("disassebled")
  19. end
  20.  
  21. cannon.assemble()
  22.  
  23. if modem then
  24. modem.open(cannonHitPosChannel)
  25. modem.open(controlChannel)
  26. end
  27.  
  28. local distance = 100
  29. local requiredAngle, controls
  30.  
  31. -- Normalize a vector
  32. local function normalizeVector(v)
  33. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  34. if length == 0 then
  35. return {0, 0, 0}
  36. end
  37. return {v[1] / length, v[2] / length, v[3] / length}
  38. end
  39.  
  40. -- Normalize the rotation matrix
  41. local function normalizeRotationMatrix(rotMatrix)
  42. local normalizedMatrix = {}
  43. for i = 1, #rotMatrix do
  44. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  45. end
  46. return normalizedMatrix
  47. end
  48.  
  49. -- Get the pitch of the ship
  50. local function getPitch()
  51. local rotMatrix = ship.getTransformationMatrix()
  52. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  53. return math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  54. end
  55. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  56. -- Get the yaw of the ship
  57. local function getYaw()
  58. local rotMatrix = ship.getTransformationMatrix()
  59. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  60. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) -- Extract yaw from the matrix
  61. end
  62.  
  63. -- Get the roll of the ship
  64. local function getRoll()
  65. local rotMatrix = ship.getTransformationMatrix()
  66. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  67. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  68. end
  69.  
  70. --Main loop
  71. local function modemMessage()
  72. while true do
  73. if modem then
  74. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  75. if channel == cannonHitPosChannel then
  76. requiredAngle = message
  77. elseif channel == controlChannel then
  78. controls = message
  79. end
  80. else
  81. sleep()
  82. end
  83. end
  84. end
  85.  
  86. local function findRelativeAngle(targetYaw,targetPitch)
  87. rot = ship.getQuaternion()
  88. cacheYaw = math.pi - math.rad(targetYaw)
  89. cachePitch = - math.rad(targetPitch)
  90. rotMatAdj11 = 1 - 2*(rot.x^2 + rot.y^2)
  91. rotMatAdj12 = 2*(rot.z * rot.x + rot.y * rot.w)
  92. rotMatAdj13 = 2*(rot.z * rot.y - rot.x * rot.w)
  93. rotMatAdj21 = 2*(rot.z * rot.x - rot.y * rot.w)
  94. rotMatAdj22 = 1 - 2*(rot.z^2 + rot.y^2)
  95. rotMatAdj23 = 2*(rot.x * rot.y + rot.z * rot.w)
  96. rotMatAdj31 = 2*(rot.z * rot.y + rot.x * rot.w)
  97. rotMatAdj32 = 2*(rot.x * rot.y - rot.z * rot.w)
  98. rotMatAdj33 = 1 - 2*(rot.z^2 + rot.x^2)
  99.  
  100. rotMatTGT11 = math.cos(cacheYaw) * math.cos(cachePitch)
  101. rotMatTGT21 = math.sin(cacheYaw) * math.cos(cachePitch)
  102. rotMatTGT31 = - math.sin(cachePitch)
  103.  
  104. rotMatRSLT11 = rotMatAdj11 * rotMatTGT11 + rotMatAdj12 * rotMatTGT21 + rotMatAdj13 * rotMatTGT31
  105. rotMatRSLT21 = rotMatAdj21 * rotMatTGT11 + rotMatAdj22 * rotMatTGT21 + rotMatAdj23 * rotMatTGT31
  106. rotMatRSLT31 = rotMatAdj31 * rotMatTGT11 + rotMatAdj32 * rotMatTGT21 + rotMatAdj33 * rotMatTGT31
  107.  
  108. turretYaw = math.atan2(rotMatRSLT21, rotMatRSLT11)
  109. barrelPitch = math.asin(-rotMatRSLT31)
  110. return math.deg(-turretYaw),math.deg(-barrelPitch)
  111. end
  112.  
  113.  
  114. local function setPitch()
  115. while true do
  116. if cannon and requiredAngle and requiredAngle.requiredRelativePitch then
  117. print("setting cannon")
  118. cannon.setPitch(requiredAngle.requiredRelativePitch)
  119. end
  120. print(textutils.serialize(requiredAngle))
  121. sleep()
  122. end
  123. end
  124.  
  125. local function fire()
  126. while true do
  127. if controls and controls.fireCOAX then
  128. print("firing autocannon")
  129. cannon.fire()
  130. cannon.fire()
  131. cannon.fire()
  132. cannon.fire()
  133. end
  134. sleep()
  135. end
  136. end
  137.  
  138. parallel.waitForAny(
  139. setPitch,
  140. modemMessage,
  141. fire
  142. )
  143.  
Advertisement
Add Comment
Please, Sign In to add comment