Пример #1
0
        protected override void AISearch(GameTime gameTime)
        {
            CurrentAttackTarget = null;
            float BestDistance = 1000000;

            foreach (Basic2DObject o in FactionManager.SortedUnits[WaveManager.ActiveTeam])
            {
                if (o != this && o.GetType().IsSubclassOf(typeof(UnitTurret)))
                {
                    float d = Vector2.Distance(getPosition(), o.getPosition());
                    if (d < BestDistance)
                    {
                        UnitTurret s = (UnitTurret)o;
                        if (s.GetTeam() == WaveManager.ActiveTeam && s.ShutDownTime < 1 && !s.Dead && !s.IsAlly(this) && s.Resistence != AttackType.Green)
                        {
                            if (d / s.ThreatLevel < BestDistance && !PathFindingManager.CollisionLine(Position.get(), o.Position.get()))
                            {
                                BestDistance        = d / s.ThreatLevel;
                                CurrentAttackTarget = s;
                            }
                        }
                    }
                }
            }
        }
        protected override void AISearch(GameTime gameTime)
        {
            CurrentAttackTarget = null;
            float BestDistance = 1000000;

            foreach (Basic2DObject o in FactionManager.SortedUnits[WaveManager.ActiveTeam])
            {
                if (o != this)
                {
                    float d = Vector2.Distance(getPosition(), o.getPosition());
                    if (d < BestDistance && o.GetType().IsSubclassOf(typeof(BasicShipGameObject)) && !o.GetType().Equals(typeof(PlayerShip)))
                    {
                        BasicShipGameObject s = (BasicShipGameObject)o;
                        if (s.GetTeam() == WaveManager.ActiveTeam && !s.Dead && !s.IsAlly(this) && s.CanBeTargeted())
                        {
                            if (d / s.ThreatLevel < BestDistance && !PathFindingManager.CollisionLine(Position.get(), s.Position.get()))
                            {
                                BestDistance        = d / s.ThreatLevel;
                                CurrentAttackTarget = s;
                            }
                        }
                    }
                }
            }
        }
        protected virtual void AISearch(GameTime gameTime)
        {
            AngerTime -= gameTime.ElapsedGameTime.Milliseconds;
            if (AngerTime > 0 && CurrentAttackTarget != null && CurrentAttackTarget.CanBeTargeted())
            {
                return;
            }

            CurrentAttackTarget = null;
            float BestDistance = 1000000;

            StarshipScene scene = (StarshipScene)Parent2DScene;

            if (GetTeam() != WaveManager.ActiveTeam)
            {
                foreach (Basic2DObject o in FactionManager.SortedUnits[WaveManager.ActiveTeam])
                {
                    if (o != this)
                    {
                        float d = Vector2.Distance(Position.get(), o.getPosition());
                        if (d < BestDistance && o.GetType().IsSubclassOf(typeof(UnitBasic)))
                        {
                            UnitBasic s = (UnitBasic)o;
                            if (s.GetTeam() == WaveManager.ActiveTeam && s.CanBeTargeted())
                            {
                                if (d / s.ThreatLevel < BestDistance && !PathFindingManager.CollisionLine(Position.get(), s.Position.get()))
                                {
                                    BestDistance        = d / s.ThreatLevel;
                                    CurrentAttackTarget = s;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (Basic2DObject o in Parent2DScene.GetList(GameObjectTag._2DSolid))
                {
                    if (o != this && o.GetType().IsSubclassOf(typeof(UnitBasic)) && !o.GetType().IsSubclassOf(typeof(UnitBuilding)))
                    {
                        float d = Vector2.Distance(Position.get(), o.getPosition());

                        if (d < BestDistance)
                        {
                            UnitBasic s = (UnitBasic)o;
                            if (!IsAlly(s) && s.CanBeTargeted() &&
                                d / s.ThreatLevel < BestDistance && !PathFindingManager.CollisionLine(Position.get(), s.Position.get()))
                            {
                                BestDistance        = d / s.ThreatLevel;
                                CurrentAttackTarget = s;
                            }
                        }
                    }
                }
            }
        }
        public override void Damage(float damage, float pushTime, Vector2 pushSpeed, BasicShipGameObject Damager, AttackType attackType)
        {
            if ((InvTime < 1 && (FieldStateTime < 1 || fieldState != FieldState.Cloaked)) || attackType == AttackType.Explosion || attackType == AttackType.Melee)
            {
                if (CanCloak)
                {
                    SoundManager.Play3DSound("UnitCloak", new Vector3(Position.X(), Y, Position.Y()), 0.25f, 800, 2);

                    fieldState     = FieldState.Cloaked;
                    HasCloaked     = true;
                    CanCloak       = false;
                    FieldStateTime = GetCloakTime();
                }
                else
                {
                    if (attackType != AttackType.White)
                    {
                        SummonUnits();
                    }

                    NoShootTime = MaxNoShootTime;
                    if (attackType == AttackType.White)
                    {
                        ThreatLevel *= 1.5f;
                    }

                    if (!PathFindingManager.CollisionLine(Position.get(), Damager.Position.get()))
                    {
                        AngerTime           = MaxAngerTime;
                        CurrentAttackTarget = Damager;
                    }

                    base.Damage(damage, pushTime, pushSpeed, Damager, attackType);

                    if (HullDamage >= HullToughness && Damager.FactionNumber != NeutralManager.NeutralFaction && ScoreToGive > 0)
                    {
                        if (CanDeathSound)
                        {
                            SoundManager.Play3DSound(DeathSound, new Vector3(Position.X(), Y, Position.Y()), DeathVolume * 0.5f, DeathDistance, DeathExponenent * 2);
                        }

                        ScoreToGive = (int)(ScoreToGive * (0.75f + 0.25f * UnitLevel) * (IsHuge ? 1.25f : 1) *
                                            (HasCloaked || CanCloak ? 1.5f : 1) * (CanSummon || HasSummoned ? 4 : 1));
                        TextParticleSystem.AddParticle(new Vector3(Position.X(), Y, Position.Y()), ScoreToGive.ToString(), (byte)Damager.GetTeam());
                        FactionManager.AddScore(Damager.FactionNumber, ScoreToGive);
                        FactionManager.Factions[Damager.FactionNumber].roundReport.UnitKills++;
                        ScoreToGive = 0;
                    }
                }
            }
        }
        public static PathFindingNode GetBestNode(Vector2 Position)
        {
            foreach (PathFindingNode node in AllNodes)
            {
                if (!PathFindingManager.CollisionLine(Position, node.Position.get()))
                {
                    return(node.Get());
                }
            }

            PathFindingNode Node = new PathFindingNode(Position);

            GameManager.GetLevel().AddObject(Node);
            return(Node.Get());
        }
        protected virtual PlayerShip AISearchForPlayers()
        {
            PlayerShip ReturnPlayer = null;
            float      BestDistance = 1000000;

            QuadGrid grid = Parent2DScene.quadGrids.First.Value;

            foreach (Basic2DObject o in grid.Enumerate(getPosition(), new Vector2(BestDistance) * 2))
            {
                if (o.GetType().IsSubclassOf(typeof(PlayerShip)))
                {
                    PlayerShip s = (PlayerShip)o;
                    if (s.GetTeam() == WaveManager.ActiveTeam && !s.Dead && !PathFindingManager.CollisionLine(Position.get(), o.Position.get()))
                    {
                        ReturnPlayer = s;
                    }
                }
            }

            return(ReturnPlayer);
        }
        public virtual Vector2 GetPlacePosition(int FactionNumber)
        {
            float   BestScore    = -500000;
            Vector2 BestPosition = Vector2.Zero;

            float Distance = 1000;
            float JumpSize = 100;

            Vector2 MinPos = new Vector2(10000);
            Vector2 MaxPos = new Vector2(-10000);

            foreach (MiningPlatform m in GameManager.GetLevel().getCurrentScene().Enumerate(typeof(MiningPlatform)))
            {
                if (m.GetTeam() == FactionManager.GetTeam(FactionNumber) && !m.Dead)
                {
                    MinPos = Vector2.Min(MinPos, m.Position.get() - new Vector2(Distance));
                    MaxPos = Vector2.Max(MaxPos, m.Position.get() + new Vector2(Distance));
                }
            }

            Basic2DScene b = (Basic2DScene)GameManager.GetLevel().getCurrentScene();

            MinPos = Vector2.Max(MinPos, b.MinBoundary.get());
            MaxPos = Vector2.Min(MaxPos, b.MaxBoundary.get());

            for (float x = MinPos.X; x < MaxPos.X; x += JumpSize)
            {
                for (float y = MinPos.Y; y < MaxPos.Y; y += JumpSize)
                {
                    if (TestFree(new Vector2(x, y), TurretSize, FactionNumber) &&
                        PathFindingManager.GetCellValue(new Vector2(x, y)) != PathFindingManager.DeadCell)
                    {
                        float score = 5000;

                        if (GetTurretFragility() != 0)
                        {
                            if (PathFindingManager.CollisionLine(new Vector2(x, y), 1, 0, 3))
                            {
                                score += 100 * GetTurretFragility();
                            }
                            if (PathFindingManager.CollisionLine(new Vector2(x, y), -1, 0, 3))
                            {
                                score += 100 * GetTurretFragility();
                            }
                            if (PathFindingManager.CollisionLine(new Vector2(x, y), 0, 1, 3))
                            {
                                score += 100 * GetTurretFragility();
                            }
                            if (PathFindingManager.CollisionLine(new Vector2(x, y), 0, -1, 3))
                            {
                                score += 100 * GetTurretFragility();
                            }

                            if (PathFindingManager.CollisionLine(new Vector2(x, y), -1, -1, 2))
                            {
                                score += 100 * GetTurretFragility();
                            }
                            if (PathFindingManager.CollisionLine(new Vector2(x, y), 1, -1, 2))
                            {
                                score += 100 * GetTurretFragility();
                            }
                            if (PathFindingManager.CollisionLine(new Vector2(x, y), -1, 1, 2))
                            {
                                score += 100 * GetTurretFragility();
                            }
                            if (PathFindingManager.CollisionLine(new Vector2(x, y), 1, 1, 2))
                            {
                                score += 100 * GetTurretFragility();
                            }
                        }

                        foreach (NeutralSpawn spawn in NeutralManager.SpawnList)
                        {
                            if (PathFindingManager.GetCellValue(spawn.Position.get()) > PathFindingManager.StartingCell - 50 &&
                                PathFindingManager.GetCellValue(spawn.Position.get()) < PathFindingManager.StartingCell - 20 &&
                                PathFindingManager.GetAreaClear(spawn.Position.get()))
                            {
                                if (GetTurretFragility() != 0 && PathFindingManager.CollisionLine(new Vector2(x, y), spawn.Position.get()))
                                {
                                    score += 500 * GetTurretFragility();
                                }

                                if (score > BestScore)
                                {
                                    MiningPlatform NearestMiningPlatform = null;
                                    float          NearestDistance       = 10000;

                                    foreach (MiningPlatform m2 in GameManager.GetLevel().getCurrentScene().Enumerate(typeof(MiningPlatform)))
                                    {
                                        if (m2.GetTeam() == FactionManager.GetTeam(FactionNumber) && !m2.Dead)
                                        {
                                            float d = Vector2.Distance(m2.Position.get(), spawn.Position.get());
                                            if (d < NearestDistance)
                                            {
                                                NearestDistance       = d;
                                                NearestMiningPlatform = m2;
                                            }
                                        }
                                    }

                                    score -= Logic.DistanceLineSegmentToPoint(spawn.Position.get(),
                                                                              NearestMiningPlatform.Position.get(), new Vector2(x, y)) * GetTurretAgression();
                                }
                            }
                        }

                        if (score > BestScore)
                        {
                            Basic2DScene Parent2DScene = (Basic2DScene)GameManager.GetLevel().getCurrentScene();
                            QuadGrid     quad          = Parent2DScene.quadGrids.First.Value;

                            foreach (Basic2DObject o in quad.Enumerate(new Vector2(x, y), new Vector2(200)))
                            {
                                if (o.GetType().IsSubclassOf(typeof(UnitBuilding)))
                                {
                                    UnitBuilding s = (UnitBuilding)o;
                                    if (s.GetTeam() == FactionManager.GetTeam(FactionNumber))
                                    {
                                        float d = Vector2.Distance(o.Position.get(), new Vector2(x, y));
                                        if (d < 2000)
                                        {
                                            score -= (2000 - d) * GetBuildingAvoidence();
                                            if (s.GetType().IsSubclassOf(typeof(MiningPlatform)))
                                            {
                                                score -= (2000 - d) * GetBaseAvoidence();
                                            }
                                            else if (s.GetType().IsSubclassOf(typeof(UnitTurret)))
                                            {
                                                UnitTurret t = (UnitTurret)s;
                                                if (t.MyCard != null)
                                                {
                                                    if (t.MyCard.StrongVs.Equals(StrongVs))
                                                    {
                                                        score -= (2000 - d) * GetTurretAvoidence();
                                                    }
                                                }
                                                else
                                                {
                                                    if (StrongVs.Equals("Heavy"))
                                                    {
                                                        score -= (2000 - d) * GetTurretAvoidence();
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            if (score > BestScore)
                            {
                                BestScore    = score;
                                BestPosition = new Vector2(x, y);
                            }
                        }
                    }
                }
            }

            return(BestPosition);
        }
        private void AIMove(GameTime gameTime)
        {
            MoveChangeTime += gameTime.ElapsedGameTime.Milliseconds;
            if (MoveChangeTime > MaxMoveChangeTime)
            {
                MoveChangeTime -= MaxMoveChangeTime;

                TargetPosition = Position.get();

                if (!DodgesBullets || BulletToDodge == null || BulletToDodge.TimeAlive > BulletToDodge.LifeTime)
                {
                    if (BulletToDodge != null)
                    {
                        BulletToDodge = null;
                    }

                    if (CurrentAttackTarget != null)
                    {
                        float d = Vector2.Distance(CurrentAttackTarget.getPosition(), getPosition());

                        if (!RunningAway)
                        {
                            if (d > MinEngagementDistance)
                            {
                                TargetPosition = CurrentAttackTarget.getPosition();
                            }
                            else if (d < MinEngagementDistance)
                            {
                                TargetPosition = Position.get();
                            }
                        }
                        else
                        {
                            TargetPosition = Position.get() + (Position.get() - CurrentAttackTarget.getPosition());
                        }

                        DisplacedFromPath = true;
                    }
                    else
                    {
                        if (TargetNode == null)
                        {
                            TargetNode        = PathFindingNode.GetBestNode(Position.get());
                            DisplacedFromPath = false;
                        }
                        else
                        {
                            DisplaceTime += gameTime.ElapsedGameTime.Milliseconds;
                            if (DisplaceTime > MaxDisplaceTime)
                            {
                                DisplaceTime     -= MaxDisplaceTime;
                                DisplacedFromPath = true;
                            }

                            if (DisplacedFromPath)
                            {
                                if (PathFindingManager.CollisionLine(Position.get(), TargetNode.Position.get()))
                                {
                                    TargetNode = PathFindingNode.GetBestNode(Position.get());
                                }
                                DisplacedFromPath = false;
                            }
                        }

                        TargetPosition = TargetNode.Position.get();

                        if (TargetNode.GetNext() != null && !PathFindingManager.CollisionLine(Position.get(), TargetNode.GetNext().Position.get()) ||
                            (Vector2.Distance(getPosition(), TargetPosition) < 16))
                        {
                            TargetNode = TargetNode.GetNext();
                        }
                    }
                }
                else
                {
                    Vector2 bPos = BulletToDodge.getPosition();
                    bPos += BulletToDodge.Speed / BulletToDodge.Speed.Length() * Vector2.Distance(bPos, Position.get());

                    TargetPosition = (Position.get() - (bPos - Position.get()));

                    BulletDodgeTime -= gameTime.ElapsedGameTime.Milliseconds;

                    if (BulletDodgeTime < 0)
                    {
                        TargetPosition = CurrentMoveTarget == null ? TargetPosition : CurrentMoveTarget.getPosition();
                        BulletToDodge  = null;
                    }

                    DisplacedFromPath = true;
                }
            }

            if (Vector2.Distance(getPosition(), TargetPosition) > 16)
            {
                Accelerate(gameTime, TargetPosition - getPosition());
            }
            else if (CurrentAttackTarget != null)
            {
                Rotation.set(MathHelper.ToDegrees(Logic.Clerp(Rotation.getAsRadians(), Logic.ToAngle(CurrentAttackTarget.Position.get() - Position.get()), RotationSpeed * gameTime.ElapsedGameTime.Milliseconds * 60.0f / 1000.0f)));
                RotationMatrix = Matrix.CreateFromYawPitchRoll(Rotation.getAsRadians() + RotationOffset.X, RotationOffset.Y, RotationOffset.Z);
            }
        }
        public override void Update2(GameTime gameTime)
        {
            base.Update2(gameTime);
            Position.set(Vector2.Clamp(Position.get(), Parent2DScene.MinBoundary.get(), Parent2DScene.MaxBoundary.get()));

            if (CrystalWall.SortedWalls.ContainsKey(WaveManager.ActiveTeam))
            {
                foreach (CrystalWall n in CrystalWall.SortedWalls[WaveManager.ActiveTeam])
                {
                    if (!n.Dead)
                    {
                        foreach (CrystalWallConnection n2 in n.ConnectedWalls)
                        {
                            if (!n2.wall.Dead)
                            {
                                float MoveAmount = (Size.X() + n.getSize().X * 0.75f) / 2 - Logic.DistanceLineSegmentToPoint(n.Position.get(), n2.wall.Position.get(), Position.get());

                                if (MoveAmount > 0)
                                {
                                    Vector2 MoveVector;

                                    if (!n2.LineIsVertical)
                                    {
                                        if (n2.LineSlope == 0)
                                        {
                                            MoveVector = new Vector2(0, -1);
                                            if (Position.Y() > n2.LineSlope * Position.X() + n2.LineIntercept)
                                            {
                                                MoveAmount = -MoveAmount;
                                            }
                                        }
                                        else
                                        {
                                            MoveVector = new Vector2(1, -1 / n2.LineSlope);
                                            if (!(Position.Y() > n2.LineSlope * Position.X() + n2.LineIntercept ^ n2.LineSlope > 0))
                                            {
                                                MoveAmount = -MoveAmount;
                                            }
                                        }

                                        MoveVector.Normalize();
                                    }
                                    else
                                    {
                                        MoveVector = new Vector2(Position.X() > n.Position.X() ? 1 : -1, 0);
                                    }

                                    Position.set(Position.get() + MoveVector * MoveAmount);


                                    float d1 = Vector2.Distance(Position.get(), n.Position.get());
                                    float d2 = 0;
                                    if (!PathFindingManager.CollisionLine(Position.get(), n.Position.get()))
                                    {
                                        Vector2.Distance(Position.get(), n2.wall.Position.get());
                                    }

                                    CurrentAttackTarget = d1 > d2 ? n : n2.wall;
                                    AngerTime           = MaxAngerTime;
                                }
                            }
                        }
                    }
                }
            }
        }