public override void Update(GameTime gameTime)
        {
            PauseTime += gameTime.ElapsedGameTime.Milliseconds;
            if (PauseTime > MaxPauseTime)
            {
                Parent.ParentController.ParentShip.GetTurretSelection();

                AiState s = Parent.GetExistingState(typeof(PlaceTurretState));
                Parent.SetState(s == null ? new PlaceTurretState() : s);
            }

            base.Update(gameTime);
        }
        public void SetState(AiState state)
        {
            if (!AllStates.Contains(state))
            {
                AllStates.AddLast(state);
            }

            if (this.CurrentState != null)
            {
                this.CurrentState.Exit();
            }
            this.CurrentState = state;
            this.CurrentState.Enter(this);
        }
        public override void Update(GameTime gameTime)
        {
            if (DodgeBullet != null && DodgeBullet.TimeAlive >= DodgeBullet.LifeTime)
            {
                DodgeBullet = null;
            }

            SearchTime += gameTime.ElapsedGameTime.Milliseconds;
            if (SearchTime > MaxSearchTime || (AttackTarget != null && !AttackTarget.CanBeTargeted()))
            {
                SearchTime = 0;
                SearchForEnemies();
            }

            if (AttackTarget == null || AttackTarget.GetType().IsSubclassOf(typeof(UnitTurret)))
            {
                UnitTurret u = (UnitTurret)AttackTarget;
                if (AttackTarget == null || (u.MyCard == null && NeutralManager.MyPattern.CurrentCard.Type.Equals("Heavy") ||
                                             (u.MyCard != null && NeutralManager.MyPattern.CurrentCard.Type.Equals(u.MyCard.StrongVs))))
                {
                    if (!isPathfinding || Vector2.Distance(Parent.ParentController.ParentShip.Position.get(), PathfindingTarget) < 300)
                    {
                        isPathfinding     = true;
                        PathfindingTarget = PathFindingManager.TraceAttackPoint(Parent.ParentController.ParentShip.Position.get(), 10);
                    }
                }
                else
                {
                    isPathfinding = false;
                }
            }
            else
            {
                isPathfinding = false;
            }

            if (Parent.ParentController.ParentShip.Dead || !WaveFSM.WaveStepState.WeaponsFree)
            {
                AiState s = Parent.GetExistingState(typeof(PlaceTurretState));
                Parent.SetState(s == null ? new PlaceTurretState() : s);
            }

            base.Update(gameTime);
        }
        public override void Update(GameTime gameTime)
        {
            PauseTime += gameTime.ElapsedGameTime.Milliseconds;
            if (PauseTime > MaxPauseTime)
            {
                float MoveAmt = gameTime.ElapsedGameTime.Milliseconds * 60 / 1000f * MoveSpeed;
                Theta = Logic.Clerp(Theta, TargetTheta, MoveAmt);
                if (MathHelper.WrapAngle(Theta - TargetTheta) < MoveAmt)
                {
                    Theta = TargetTheta;

                    if (Parent.ParentController.ParentShip != null &&
                        Parent.ParentController.ParentShip.PlacedStartingTurrets)
                    {
                        AiState s = Parent.GetExistingState(typeof(PickTurretCardsState));
                        Parent.SetState(s == null ? new PickTurretCardsState() : s);
                    }
                }
            }
            base.Update(gameTime);
        }
        public override void Update(GameTime gameTime)
        {
            PauseTime += gameTime.ElapsedGameTime.Milliseconds;
            if (PauseTime > MaxPauseTime)
            {
                Moving = true;
                if (OverCardPicker.CurrentCards[OverCardPicker.TeamSelectedNodes[Parent.ParentController.ParentShip.FactionNumber]] != SelectedCard)
                {
                    OverCardPicker.TeamMoveLeft(Parent.ParentController.ParentShip.FactionNumber);
                    PauseTime -= 1000;
                }
                else
                {
                    OverCardPicker.ReadyTeam(Parent.ParentController.ParentShip.FactionNumber);
                    AiState s = Parent.GetExistingState(typeof(PlaceTurretState));
                    Parent.SetState(s == null ? new PlaceTurretState() : s);
                }
            }

            base.Update(gameTime);
        }
        public override void Update(GameTime gameTime)
        {
            if (DodgeBullet != null && DodgeBullet.TimeAlive >= DodgeBullet.LifeTime)
            {
                DodgeBullet = null;
            }
            if (DodgeTarget != null && DodgeTarget.Dead)
            {
                DodgeTarget = null;
            }

            float MostDamage = 0;

            UrgentDefenseTarget = null;
            foreach (UnitBasic b in FactionManager.SortedUnits[Parent.ParentController.ParentShip.GetTeam()])
            {
                if (b.GetType().IsSubclassOf(typeof(MiningPlatform)))
                {
                    MiningPlatform m = (MiningPlatform)b;
                    if (FactionManager.Factions[Parent.ParentController.ParentShip.GetTeam()].MiningPlatformCount == 1 &&
                        m.HullDamage > 0 && FactionManager.CanBuildMiningPlatform(Parent.ParentController.ParentShip.FactionNumber))
                    {
                        AiState s = Parent.GetExistingState(typeof(PlaceTurretState));
                        Parent.SetState(s == null ? new PlaceTurretState() : s);
                    }
                    else if (m.SizeMult > 1)
                    {
                        float Damage = Math.Min(m.ShieldToughness, m.ShieldDamage) +
                                       Math.Min(m.HullToughness, m.HullDamage);
                        if (Damage > MostDamage)
                        {
                            MostDamage          = Damage;
                            UrgentDefenseTarget = m;
                        }
                    }
                }
            }


            SmallBombTimer -= gameTime.ElapsedGameTime.Milliseconds;

            SearchTime += gameTime.ElapsedGameTime.Milliseconds;
            if (SearchTime > MaxSearchTime || (AttackTarget != null && !AttackTarget.CanBeTargeted()))
            {
                SearchTime = 0;
                SearchForEnemies();
            }

            ResetTime += gameTime.ElapsedGameTime.Milliseconds;
            if (ResetTime > MaxResetTime)
            {
                ResetTime -= MaxResetTime;
                ResetDefenseTarget();
            }

            if (Parent.ParentController.ParentShip.Dead || !WaveFSM.WaveStepState.WeaponsFree)
            {
                AiState s = Parent.GetExistingState(typeof(PlaceTurretState));
                Parent.SetState(s == null ? new PlaceTurretState() : s);
            }

            base.Update(gameTime);
        }
        public override void Update(GameTime gameTime)
        {
            if (WaveManager.GetState() == WaveFSM.PickEnemyState.self && WaveManager.CurrentWave > 4 &&
                WaveManager.ActiveTeam != Parent.ParentController.ParentShip.GetTeam())
            {
                AiState s = Parent.GetExistingState(typeof(PickAttackerState));
                Parent.SetState(s == null ? new PickAttackerState() : s);
            }
            else
            {
                if (TargetObject == null && TargetCard == null)
                {
                    bool Damaged = false;
                    foreach (UnitBasic u in FactionManager.SortedUnits[Parent.ParentController.ParentShip.GetTeam()])
                    {
                        if (u.GetType().IsSubclassOf(typeof(MiningPlatform)))
                        {
                            if (u.HullDamage > 0)
                            {
                                Damaged = true;
                                break;
                            }
                        }
                    }

                    if (FactionManager.CanBuildMiningPlatform(Parent.ParentController.ParentShip.FactionNumber) &&
                        (WaveManager.ActiveTeam != Parent.ParentController.ParentShip.GetTeam() || FactionManager.NeutralUnitCount < 3 ||
                         (FactionManager.Factions[Parent.ParentController.ParentShip.FactionNumber].MiningPlatformCount < 2 &&
                          Damaged)))
                    {
                        float BestStrength = 100000;

                        foreach (MineralRock m in Parent.ParentController.ParentShip.ParentScene.Enumerate(typeof(MineralRock)))
                        {
                            if (m.miningPlatform == null)
                            {
                                float MineralRockStrength = 0;

                                foreach (MiningPlatform f in
                                         Parent.ParentController.ParentShip.ParentScene.Enumerate(typeof(MiningPlatform)))
                                {
                                    if (f.IsAlly(Parent.ParentController.ParentShip))
                                    {
                                        MineralRockStrength += Vector2.Distance(m.Position.get(), f.Position.get()) / 1000;
                                    }
                                }

                                foreach (UnitTurret f in
                                         Parent.ParentController.ParentShip.ParentScene.Enumerate(typeof(UnitTurret)))
                                {
                                    if (f.IsAlly(Parent.ParentController.ParentShip))
                                    {
                                        MineralRockStrength += Vector2.Distance(m.Position.get(), f.Position.get()) / 2000;
                                    }
                                }

                                if (MineralRockStrength < BestStrength)
                                {
                                    BestStrength = MineralRockStrength;
                                    TargetObject = m;
                                }
                            }
                        }
                    }
                    else if (WaveManager.ActiveTeam == Parent.ParentController.ParentShip.GetTeam() &&
                             PathFindingManager.CellJobQue.Count == 0 && WaveManager.CurrentWave > 1)
                    {
                        Faction f = FactionManager.Factions[Parent.ParentController.ParentShip.FactionNumber];

                        TurretCard BestCard     = null;
                        float      BestStrength = 0;

                        foreach (TurretCard c in f.Cards)
                        {
                            float s = c.GetPlaceStrength(f.FactionNumber);
                            if (s > BestStrength)
                            {
                                BestStrength = s;
                                BestCard     = c;
                            }
                        }

                        if (BestCard != null && (!BestCard.FactionCostIncreases.ContainsKey(f.FactionNumber) ? f.Cells >= BestCard.CardCellsCost :
                                                 f.Cells >= BestCard.CardCellsCost + BestCard.CardCellsCostIncrease * BestCard.FactionCostIncreases[f.FactionNumber]))
                        {
                            TargetCard         = BestCard;
                            TargetCardPosition = BestCard.GetPlacePosition(f.FactionNumber);
                        }
                    }
                }
                else
                {
                    if (TargetObject != null)
                    {
                        if (TargetObject.GetType().Equals(typeof(MineralRock)))
                        {
                            MineralRock m = (MineralRock)TargetObject;
                            if (m.miningPlatform != null)
                            {
                                TargetObject = null;
                            }
                        }
                        else if (TargetObject.GetType().IsSubclassOf(typeof(UnitBuilding)))
                        {
                            UnitBuilding b = (UnitBuilding)TargetObject;
                            if (b.IsUpdgraded)
                            {
                                TargetObject = null;
                            }
                        }
                    }
                    else if (Vector2.Distance(TargetCardPosition, Parent.ParentController.ParentShip.FloatingViewPosition) < 4)
                    {
                        PlayerShip ParentShip = Parent.ParentController.ParentShip;
                        int        CardCost   = TargetCard.CardCellsCost;

                        if (TargetCard.FactionCostIncreases.ContainsKey(ParentShip.FactionNumber))
                        {
                            CardCost += TargetCard.CardCellsCostIncrease *
                                        TargetCard.FactionCostIncreases[ParentShip.FactionNumber];
                        }

                        if (TargetCard.FactionCostIncreases.ContainsKey(ParentShip.FactionNumber))
                        {
                            TargetCard.FactionCostIncreases[ParentShip.FactionNumber]++;
                        }
                        else
                        {
                            TargetCard.FactionCostIncreases.Add(ParentShip.FactionNumber, 1);
                        }

                        FactionManager.AddCells(ParentShip.FactionNumber, -CardCost);
                        UnitTurret u = (UnitTurret)TargetCard.GetUnit(ParentShip.FactionNumber);
                        u.MyCard = TargetCard;
                        ParentShip.PlaceTurret(u);

                        TargetCard = null;
                    }
                }
            }
            if (WaveFSM.WaveStepState.WeaponsFree && !Parent.ParentController.ParentShip.Dead &&
                TargetObject == null && TargetCard == null)
            {
                if (WaveManager.ActiveTeam != Parent.ParentController.ParentShip.GetTeam())
                {
                    AiState s = Parent.GetExistingState(typeof(AttackState));
                    Parent.SetState(s == null ? new AttackState() : s);
                }
                else
                {
                    AiState s = Parent.GetExistingState(typeof(DefendState));
                    Parent.SetState(s == null ? new DefendState() : s);
                }
            }

            base.Update(gameTime);
        }