MegiRicky

turretOnboardNGDW

Jul 31st, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2. local reader = peripheral.find("blockReader")
  3.  
  4. local cannonHitPosChannel = 700
  5. local cannonSourceChannel = 701
  6. if modem then
  7. modem.open(cannonHitPosChannel)
  8. end
  9.  
  10. if not reader then
  11. print("No blockReader found.")
  12. end
  13.  
  14. local distance = 100
  15.  
  16. --Main loop
  17. while true do
  18. local hitPos = {}
  19. local sourceData = {}
  20.  
  21. cannonPitch = 0
  22.  
  23. local source = ship.getWorldspacePosition()
  24. local sourceX, sourceY, sourceZ = source.x, source.y, source.z
  25.  
  26. local yaw = math.deg(ship.getYaw())
  27. if yaw < 0 then yaw = yaw + 360 end
  28. local matrix = ship.getRotationMatrix()
  29. local shipPitchCos = matrix[2][2] -- Cosine of the ship's pitch angle
  30. local shipPitch = math.acos(shipPitchCos) -- Ship's pitch in radians
  31.  
  32. print(" shipPitch: "..shipPitch)
  33. local pitch = shipPitch + cannonPitch
  34. local yawRad = math.rad(yaw)
  35. local pitchRad = math.rad(pitch)
  36.  
  37. local dx = distance * math.cos(pitchRad) * math.sin(yawRad)
  38. local dz = -distance * math.cos(pitchRad) * math.cos(yawRad)
  39. local dy = distance * math.sin(pitchRad)
  40.  
  41. local targetX = sourceX + dx
  42. local targetY = sourceY + dy
  43. local targetZ = sourceZ + dz
  44.  
  45. print("targetX: "..targetX.." targetY: "..targetY.." targetZ: "..targetZ)
  46.  
  47. print(" pitch: "..pitch)
  48. print(" yaw: "..yaw)
  49.  
  50. table.insert(sourceData, 1, targetX)
  51. table.insert(sourceData, 2, targetY)
  52. table.insert(sourceData, 3, targetZ)
  53. table.insert(sourceData, 4, "coordinate")
  54. table.insert(sourceData, 5, source)
  55. table.insert(sourceData, 6, pitch)
  56. table.insert(sourceData, 7, yaw)
  57. modem.transmit(cannonHitPosChannel,0,sourceData)
  58.  
  59. print(textutils.serialize(sourceData))
  60. sleep()
  61. end
Advertisement
Add Comment
Please, Sign In to add comment