Guest User

autocannon

a guest
May 4th, 2024
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | Gaming | 0 0
  1. local entDet = peripheral.wrap("playerDetector_1")
  2. local yaw = peripheral.wrap("left")
  3. local pitch = peripheral.wrap("right")
  4. local mon = peripheral.find("monitor")
  5. local blockReader = peripheral.wrap("blockReader_1")
  6.  
  7. local relYaw, relPitch = blockReader.getBlockData().CannonYaw, blockReader.getBlockData().CannonPitch
  8. local isTarget = ""
  9.  
  10. function setYaw()
  11. local canYaw = blockReader.getBlockData().CannonYaw
  12. local rev = 1
  13. if relYaw < 0 then
  14. rev = -1
  15. end
  16. while math.abs(canYaw - relYaw) >= 2 do
  17. canYaw = blockReader.getBlockData().CannonYaw
  18. yaw.setTargetSpeed(60*rev)
  19. end
  20. yaw.setTargetSpeed(0)
  21. end
  22.  
  23. function setPitch()
  24. local canPitch = blockReader.getBlockData().CannonPitch
  25. local rev = 1
  26. if relPitch < 0 then
  27. rev = -1
  28. end
  29. while math.abs(canPitch - relPitch) >= 2 do
  30. canPitch = blockReader.getBlockData().CannonPitch
  31. pitch.setTargetSpeed(60*rev)
  32. end
  33. pitch.setTargetSpeed(0)
  34. end
  35.  
  36. function checkTarget(name)
  37. local buffer = name
  38. for _, entity in ipairs(entDet.getPlayersInRange(30)) do
  39. if buffer == entity then
  40. return buffer
  41. end
  42. end
  43. return ""
  44. end
  45.  
  46. function calculateAngles(x, y, z)
  47. local cannonPitch = blockReader.getBlockData().CannonPitch
  48. local cannonYaw = blockReader.getBlockData().CannonYaw
  49. local distance = math.sqrt((x - 8.5)^2 + (y - 67)^2 + (z - 363.5)^2)
  50. relPitch = (math.asin((y - 67) / distance)) * 57.296
  51. local playerYaw = entDet.getPlayerPos(isTarget).yaw
  52. relYaw = relYaw + playerYaw
  53. print(relYaw.. "yaw")
  54. print(relPitch.. "pitch")
  55.  
  56.  
  57. local yawDifference = math.abs(cannonYaw - relYaw)
  58. local pitchDifference = math.abs(cannonPitch - relPitch)
  59.  
  60. if yawDifference <= 2 and pitchDifference <= 2 then
  61. print("Target")
  62. else
  63. parallel.waitForAll(setYaw, setPitch)
  64. end
  65. end
  66.  
  67. while true do
  68. if checkTarget(isTarget) ~= "" then
  69. local table = entDet.getPlayerPos(isTarget)
  70. calculateAngles(table.x, table.y, table.z)
  71. else
  72. for _, entity in ipairs(entDet.getPlayersInRange(30)) do
  73. if entity.name ~= "MyName" then
  74. isTarget = entity
  75. end
  76. end
  77. end
  78. end
Advertisement
Add Comment
Please, Sign In to add comment