public class TurretMonoBehaviour : MonoBehaviour
        {
            private AutoTurret turretSystem;
            private float detectionRange;
            private float lastTargetValidation;

            private void Awake()
            {
                turretSystem = GetComponent<AutoTurret>();

                turretSystem.SetPeacekeepermode(false);
                turretSystem.InitiateStartup();
                turretSystem.isLootable = false;
                turretSystem.dropFloats = false;
                turretSystem.dropsLoot = false;

                detectionRange = 30f;

                InvokeRepeating(nameof(ManageAmmo), 2f, 30f);
                InvokeRepeating(nameof(DetectEntities), 3f, 1f);
                InvokeRepeating(nameof(HandleServerOperations), Random.Range(1.2f, 2.2f), 0.015f);
            }

            private void ManageAmmo()
            {
                const int maxAmmoCapacity = 1000;

                if (turretSystem.AttachedWeapon is not BaseProjectile projectileWeapon || projectileWeapon.primaryMagazine?.ammoType == null)
                    return;

                var ammoType = projectileWeapon.primaryMagazine.ammoType;
                int currentAmmoCount = turretSystem.inventory.itemList.Where(item => item.info == ammoType).Sum(item => item.amount);

                if (currentAmmoCount < maxAmmoCapacity)
                {
                    int ammoNeeded = maxAmmoCapacity - currentAmmoCount;
                    Item additionalAmmo = ItemManager.Create(ammoType, ammoNeeded);
                    additionalAmmo?.MoveToContainer(turretSystem.inventory);

                    turretSystem.UpdateTotalAmmo();
                    turretSystem.EnsureReloaded();
                    turretSystem.SendNetworkUpdateImmediate();
                }
            }

            private void DetectEntities()
            {
                if (turretSystem.targetTrigger.entityContents == null)
                    turretSystem.targetTrigger.entityContents = new HashSet<BaseEntity>();
                else
                    turretSystem.targetTrigger.entityContents.Clear();

                int detectedEntities = BaseEntity.Query.Server.GetPlayersInSphereFast(transform.position, detectionRange, AIBrainSenses.playerQueryResults, IsEligibleTarget);

                if (detectedEntities == 0)
                    return;

                foreach (BasePlayer entity in AIBrainSenses.playerQueryResults.Take(detectedEntities))
                {
                    if (Interface.CallHook("OnEntityEnter", turretSystem.targetTrigger, entity) != null)
                        continue;

                    if (entity.IsSleeping() || (entity.InSafeZone() && !entity.IsHostile()))
                        continue;

                    turretSystem.targetTrigger.entityContents.Add(entity);
                }
            }

            private void HandleServerOperations()
            {
                if (turretSystem.isClient || turretSystem.IsDestroyed)
                    return;

                float timeElapsed = (float)turretSystem.timeSinceLastServerTick;
                turretSystem.timeSinceLastServerTick = 0;

                if (!turretSystem.IsOnline())
                {
                    turretSystem.OfflineTick();
                }
                else if (!turretSystem.IsBeingControlled)
                {
                    if (!turretSystem.HasTarget())
                    {
                        turretSystem.IdleTick(timeElapsed);
                    }
                    else
                    {
                        ManageTargetBehavior();
                    }
                }

                turretSystem.UpdateFacingToTarget(timeElapsed);

                if (turretSystem.totalAmmoDirty && Time.time > turretSystem.nextAmmoCheckTime)
                {
                    turretSystem.UpdateTotalAmmo();
                    turretSystem.totalAmmoDirty = false;
                    turretSystem.nextAmmoCheckTime = Time.time + 0.5f;
                }
            }

            private void ManageTargetBehavior()
            {
                if (Time.realtimeSinceStartup >= lastTargetValidation)
                {
                    lastTargetValidation = Time.realtimeSinceStartup + Random.Range(0.2f, 0.3f);
                    turretSystem.targetVisible = turretSystem.ObjectVisible(turretSystem.target);
                    if (turretSystem.targetVisible)
                        turretSystem.lastTargetSeenTime = Time.realtimeSinceStartup;
                }

                turretSystem.EnsureReloaded();

                BaseProjectile equippedWeapon = turretSystem.GetAttachedWeapon();

                if (
                    Time.time >= turretSystem.nextShotTime
                    && turretSystem.targetVisible
                    && Mathf.Abs(turretSystem.AngleToTarget(turretSystem.target, turretSystem.currentAmmoGravity != 0f)) < turretSystem.GetMaxAngleForEngagement()
                )
                {
                    if (equippedWeapon != null)
                    {
                        if (equippedWeapon.primaryMagazine.contents > 0)
                        {
                            turretSystem.FireAttachedGun(turretSystem.AimOffset(turretSystem.target), turretSystem.aimCone, null, turretSystem.PeacekeeperMode() ? turretSystem.target : null);

                            float fireDelay = equippedWeapon.isSemiAuto ? equippedWeapon.repeatDelay * 1.5f : equippedWeapon.repeatDelay;
                            turretSystem.nextShotTime = Time.time + equippedWeapon.ScaleRepeatDelay(fireDelay);
                        }
                        else
                        {
                            turretSystem.nextShotTime = Time.time + 5f;
                        }
                    }
                }

                ValidateCurrentTarget();
            }

            private void ValidateCurrentTarget()
            {
                BasePlayer potentialTarget = turretSystem.target as BasePlayer;

                if (
                    turretSystem.target != null
                    && (
                        potentialTarget == null
                        || potentialTarget.IsDead()
                        || Time.realtimeSinceStartup - turretSystem.lastTargetSeenTime > 3f
                        || Vector3.Distance(turretSystem.transform.position, turretSystem.target.transform.position) > turretSystem.sightRange
                        || (turretSystem.PeacekeeperMode() && !turretSystem.IsEntityHostile(turretSystem.target))
                    )
                )
                {
                    turretSystem.SetTarget(null);
                }
            }

            private bool IsEligibleTarget(BasePlayer entity)
            {
                if (!IsPlayer(entity) || entity.IsDead() || entity.IsSleeping() || entity.IsWounded() || entity._limitedNetworking)
                    return false;

                return true;
            }

            private void OnDestroy()
            {
                CancelInvoke(nameof(ManageAmmo));
                CancelInvoke(nameof(DetectEntities));
                CancelInvoke(nameof(HandleServerOperations));
            }
        }