Guest User

turret2

a guest
Dec 23rd, 2024
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.06 KB | None | 0 0
  1. -- Constants for cannon and base positions
  2. local cannon = {x = 157.5, y = 74.5, z = -13.5}
  3. local defaultYawOffset = 0 -- Adjust this value based on the cannon's rotation (e.g., 90° for West)
  4. local overshootTolerance = 1 -- Allowed overshoot tolerance for yaw and pitch
  5. local detectionRange = 30 -- Detection range for player detector
  6.  
  7. -- Peripheral assignments
  8. local yaw = peripheral.wrap("left") -- Rotation Speed Controller for Yaw
  9. local pitch = peripheral.wrap("right") -- Rotation Speed Controller for Pitch
  10. local entDet = peripheral.wrap("playerDetector_0") -- Player Detector
  11. local blockReader = peripheral.wrap("blockReader_2") -- Block Reader
  12. local mon = peripheral.find("monitor")
  13.  
  14. -- Relative angles for target
  15. local relYaw = 0
  16. local relPitch = 0
  17.  
  18. -- Helper function: Dynamically adjust speed for pitch based on distance to target
  19. local function adjustSpeedPitch(current, target)
  20.     local difference = target - current
  21.     local speed = math.min(math.abs(difference) * 5, 60) -- Scales speed dynamically
  22.     return speed * (difference > 0 and -1 or 1) -- Adjust direction
  23. end
  24.  
  25. -- Helper function: Dynamically adjust speed for yaw based on distance to target
  26. local function adjustSpeedYaw(current, target)
  27.     local difference = target - current
  28.     print("Target "..target.." Current "..current)
  29.     local speed = math.min(math.abs(difference) * 5, 60) -- Scales speed dynamically
  30.     print("Diff "..(difference > 0 and -1 or 1))
  31.     print("Speed "..speed)
  32.     return speed * (difference > 0 and -1 or 1) -- Adjust direction
  33. end
  34.  
  35. -- Function to handle Yaw adjustment
  36. local function setYaw()
  37.     local canYaw = blockReader.getBlockData().CannonYaw
  38.     print("relYaw "..relYaw)
  39.     print("canYaw "..canYaw)
  40.     while math.abs(canYaw - relYaw) >= overshootTolerance do
  41.         local speed = adjustSpeedYaw(canYaw, relYaw)
  42.         yaw.setTargetSpeed(speed)
  43.         sleep(0.1) -- Prevent CPU overload
  44.         canYaw = blockReader.getBlockData().CannonYaw
  45.     end
  46.     yaw.setTargetSpeed(0) -- Stop rotation
  47. end
  48.  
  49. -- Function to handle Pitch adjustment
  50. local function setPitch()
  51.     local canPitch = blockReader.getBlockData().CannonPitch
  52.     while math.abs(canPitch - relPitch) >= overshootTolerance do
  53.         local speed = adjustSpeedPitch(canPitch, relPitch)
  54.         pitch.setTargetSpeed(speed)
  55.         sleep(0.1)
  56.         canPitch = blockReader.getBlockData().CannonPitch
  57.     end
  58.     pitch.setTargetSpeed(0)
  59. end
  60.  
  61. -- Function to detect and assign a target
  62. local function checkForNewTarget()
  63.     for _, entity in ipairs(entDet.getPlayersInRange(detectionRange)) do
  64.         if entity ~= "NeuGoga1" then
  65.             return entity
  66.         end
  67.     end
  68.     return nil
  69. end
  70.  
  71. -- Function to calculate angles for target
  72. local function calculateAngles(targetX, targetY, targetZ)
  73.     local dx, dy, dz = targetX - cannon.x, targetY - cannon.y, targetZ - cannon.z
  74.     local distance = math.sqrt(dx^2 + dy^2 + dz^2)
  75.  
  76.     relPitch = math.deg(math.asin(dy / distance)) -- Calculate relative pitch
  77.     local rawYaw = math.deg(math.atan2(dz, dx)) -- Calculate relative yaw
  78.     -- Adjust for the cannon's default orientation (facing south)
  79.     relYaw = (rawYaw - 90 + 360) % 360
  80.    
  81.     -- Normalize the yaw to [-180, 180]
  82.     if relYaw > 180 then relYaw = relYaw - 360 end
  83.     if relYaw < -180 then relYaw = relYaw + 360 end
  84.    
  85.     print("Calculated Yaw: " .. relYaw)
  86.     print("Calculated Pitch: " .. relPitch)
  87. end
  88.  
  89. -- Main loop to track and move turret
  90. while true do
  91.     local target = checkForNewTarget() -- Check for a new target
  92.     if target then
  93.         local pos = entDet.getPlayerPos(target) -- Get target position
  94.         print(pos.x, pos.y)
  95.         if pos then
  96.             calculateAngles(pos.x, pos.y, pos.z) -- Update angles
  97.             parallel.waitForAll(setYaw, setPitch) -- Move both pitch and yaw simultaneously
  98.         end
  99.     else
  100.         pitch.setTargetSpeed(0)
  101.         yaw.setTargetSpeed(0)
  102.         print("No targets detected.") -- Log when no target is found
  103.         sleep(1) -- Add delay to reduce polling frequency
  104.     end
  105. end
  106.  
Advertisement
Add Comment
Please, Sign In to add comment