Пример #1
0
        protected override void OnCancelPanel()
        {
            //Move the Unit to the cursor position
            ActiveUnit.SetPosition(Map.LastPosition);

            Map.sndCancel.Play();
        }
        public override void OnSelect()
        {
            UnitConquest TargetSquad = null;

            //Movement initialisation.
            Map.MovementAnimation.Add(ActiveUnit.X, ActiveUnit.Y, ActiveUnit.Components);
            List <Unit> ListChoice  = new List <Unit>();
            float       DistanceMax = 99999;

            //Select the nearest enemy as a target.
            for (int P = 0; P < Map.ListPlayer.Count; P++)
            {
                //If the player is from the same team as the current player or is dead, skip it.
                if (Map.ListPlayer[P].Team == Map.ListPlayer[Map.ActivePlayerIndex].Team ||
                    !Map.ListPlayer[P].IsAlive)
                {
                    continue;
                }

                for (int U = 0; U < Map.ListPlayer[P].ListUnit.Count; U++)
                {
                    float Distance = (Math.Abs(Map.ListPlayer[P].ListUnit[U].X - ActiveUnit.X) + Math.Abs(Map.ListPlayer[P].ListUnit[U].Y - ActiveUnit.Y));
                    if (Distance < DistanceMax)
                    {
                        DistanceMax = Distance;
                        TargetSquad = Map.ListPlayer[P].ListUnit[U];
                    }
                }
            }
            DistanceMax = 99999;
            List <MovementAlgorithmTile> ListMVChoice = Map.GetMVChoice(ActiveUnit);
            int FinalMV = 0;

            //If for some reason, there's no target on to move at, don't move.
            if (TargetSquad != null)
            {
                //Remove everything that is closer then DistanceMax.
                for (int M = 0; M < ListMVChoice.Count; M++)
                {
                    float Distance = (Math.Abs(ListMVChoice[M].Position.X - TargetSquad.X) + Math.Abs(ListMVChoice[M].Position.Y - TargetSquad.Y));
                    //Remove MV choices if they are not at the furthest distance and if there is at least 1 MV(protection against bugs)
                    if (Distance < DistanceMax && ListMVChoice.Count > 1)
                    {
                        DistanceMax = Distance;
                        FinalMV     = M;
                    }
                }
                if (DistanceMax < Math.Abs(ActiveUnit.X - TargetSquad.X) + Math.Abs(ActiveUnit.Y - TargetSquad.Y))
                {
                    //Prepare the Cursor to move.
                    Map.CursorPosition.X = ListMVChoice[FinalMV].Position.X;
                    Map.CursorPosition.Y = ListMVChoice[FinalMV].Position.Y;
                    Map.CursorPosition.Y = ListMVChoice[FinalMV].Position.Z;
                    //Move the Unit to the target position;
                    ActiveUnit.SetPosition(ListMVChoice[FinalMV].Position);
                }
            }

            Map.FinalizeMovement(ActiveUnit);
        }
Пример #3
0
        public override void DoUpdate(GameTime gameTime)
        {
            Map.ListLayer[Map.ActiveLayerIndex].LayerGrid.AddDrawablePoints(ListMVChoice, Color.White);

            Map.CursorControl();

            if ((InputHelper.InputConfirmPressed() || MouseHelper.InputLeftButtonReleased()) &&
                ListMVChoice.Contains(Map.CursorPosition))//If the cursor is in the possible move list.
            {
                //Movement initialisation.
                Map.MovementAnimation.Add(ActiveUnit.X, ActiveUnit.Y, ActiveUnit.Components);
                //Move the Unit to the cursor position
                ActiveUnit.SetPosition(Map.CursorPosition);
                ListMVChoice.Clear();
                AddToPanelListAndSelect(new ActionPanelPlayerUnitSelected(Map, ActiveUnit));
            }
        }
Пример #4
0
        public override void DoUpdate(GameTime gameTime)
        {
            // Can Move, try capturing a building.
            if (ActiveUnit.CanMove)
            {
                List <Vector3> ListMVChoice = Map.GetMVChoice(ActiveUnit);

                //Remove everything that is closer then DistanceMax.
                for (int M = 0; M < ListMVChoice.Count; M++)
                {
                    TerrainConquest ActiveTerrain = Map.GetTerrain((int)ListMVChoice[M].X, (int)ListMVChoice[M].Y, ActiveUnit.Components.LayerIndex);

                    //Check if the Terrain is a building.
                    if (ActiveTerrain.CapturedPlayerIndex != Map.ActivePlayerIndex && ActiveTerrain.TerrainTypeIndex >= 13)
                    {
                        //Movement initialisation.
                        Map.MovementAnimation.Add(ActiveUnit.X, ActiveUnit.Y, ActiveUnit.Components);
                        //Prepare the Cursor to move.
                        Map.CursorPosition.X = ListMVChoice[M].X;
                        Map.CursorPosition.Y = ListMVChoice[M].Y;
                        //Move the Unit to the target position;
                        ActiveUnit.SetPosition(ListMVChoice[M]);

                        Map.FinalizeMovement(ActiveUnit);
                    }
                }
            }
            //If it didn't attacked yet.
            if (ActiveUnit.CanMove)
            {
                TerrainConquest ActiveTerrain = Map.GetTerrain(ActiveUnit.Components);

                // Can't move and on a building not owned by the current Player, try to capture it
                if (!ActiveUnit.CanMove && ActiveTerrain.CapturedPlayerIndex != Map.ActivePlayerIndex && ActiveTerrain.TerrainTypeIndex >= 13)
                {
                    ActiveTerrain.CapturePoints = Math.Max(0, ActiveTerrain.CapturePoints - ActiveUnit.HP);
                    if (ActiveTerrain.CapturePoints == 0)
                    {
                        ActiveTerrain.CapturedPlayerIndex = Map.ActivePlayerIndex;
                    }

                    ActiveUnit.EndTurn();
                }

                if (ActiveUnit.X < Map.CameraPosition.X || ActiveUnit.Y < Map.CameraPosition.Y ||
                    ActiveUnit.X >= Map.CameraPosition.X + Map.ScreenSize.X || ActiveUnit.Y >= Map.CameraPosition.Y + Map.ScreenSize.Y)
                {
                    Map.PushScreen(new CenterOnSquadCutscene(null, Map, ActiveUnit.Position));
                }

                bool AttackSuccess = false;
                //Try to attack.
                AttackSuccess = Map.AIAttackWithWeapon1(ActiveUnit) || Map.AIAttackWithWeapon2(ActiveUnit);

                //All weapon are used, if he had to attack at this point it's already done.
                if (!AttackSuccess)
                {
                    ActiveUnit.EndTurn();
                }

                RemoveFromPanelList(this);
            }

            //If the Unit can't attack at all, move toward the nearest enemy.
            else if (ActiveUnit.CanMove)
            {
                AddToPanelListAndSelect(new ActionPanelAIMoveTowardEnemy(Map, ActiveUnit));
            }
        }
Пример #5
0
        /// <summary>
        /// Try to attack with Weapon 1.
        /// </summary>
        /// <returns>Returns true if success.</returns>
        public bool AIAttackWithWeapon1(UnitConquest ActiveUnit)
        {
            int    PosX         = (int)ActiveUnit.X;
            int    PosY         = (int)ActiveUnit.Y;
            Attack ActiveWeapon = ActiveUnit.ListAttack[0];
            List <Tuple <int, int> > ListDefendingSquad = CanSquadAttackWeapon1(PosX, PosY, ActivePlayerIndex, ActiveUnit.FullName, ActiveUnit.ListAttack[1]);

            if (!ActiveUnit.CanMove && !((ActiveWeapon.Sec & WeaponSecondaryProperty.PostMovement) == WeaponSecondaryProperty.PostMovement || ActiveUnit.Boosts.PostMovementModifier.Attack))
            {
                return(false);
            }

            //Define the minimum and maximum value of the attack range.
            int   MinRange = ActiveWeapon.RangeMinimum;
            int   MaxRange = ActiveWeapon.RangeMaximum;
            float Distance;

            if (MaxRange > 1)
            {
                MaxRange += ActiveUnit.Boosts.RangeModifier;
            }

            #region Can attack

            if (ListDefendingSquad.Count > 0)
            {
                UnitConquest TargetSquad = ListPlayer[ListDefendingSquad[0].Item1].ListUnit[ListDefendingSquad[0].Item2];

                //Prepare the Cursor to move.
                CursorPosition.X = ActiveUnit.X;
                CursorPosition.Y = ActiveUnit.Y;
                GetAttackChoice(ActiveUnit, ActiveUnit.Position);
                ActiveUnit.BattleDefenseChoice = Unit.BattleDefenseChoices.Attack;

                GetAttackDamageWithWeapon1(ActiveUnit, TargetSquad, ActiveUnit.HP);
                return(true);//Exit the weapon loop.
            }

            #endregion

            #region Can't attack directly

            //If it's a post-movement weapon or you can still move.
            else if (ActiveUnit.CanMove && ((ActiveWeapon.Sec & WeaponSecondaryProperty.PostMovement) == WeaponSecondaryProperty.PostMovement || ActiveUnit.Boosts.PostMovementModifier.Attack))
            {//check if there is an enemy too close to be attacked but that could be attacked after moving.
                for (int P = 0; P < ListPlayer.Count; P++)
                {
                    //If the player is from the same team as the current player or is dead, skip it.
                    if (ListPlayer[P].Team == ListPlayer[ActivePlayerIndex].Team ||
                        !ListPlayer[P].IsAlive)
                    {
                        continue;
                    }
                    for (int TargetSelect = 0; TargetSelect < ListPlayer[P].ListUnit.Count; TargetSelect++)
                    {
                        UnitConquest TargetSquad = ListPlayer[P].ListUnit[TargetSelect];
                        Distance = Math.Abs(PosX - TargetSquad.X) + Math.Abs(PosY - TargetSquad.Y);
                        //Check if you can attack it if you moved.
                        if (Distance >= MinRange - GetSquadMaxMovement(ActiveUnit) && Distance <= MaxRange + GetSquadMaxMovement(ActiveUnit))
                        {
                            ListDefendingSquad.Add(new Tuple <int, int>(P, TargetSelect));
                        }
                    }
                }
                //If something was found.
                if (ListDefendingSquad.Count > 0)
                {
                    int RandomNumber = RandomHelper.Next(ListDefendingSquad.Count);
                    //Select a target.
                    UnitConquest TargetSquad  = ListPlayer[ListDefendingSquad[RandomNumber].Item1].ListUnit[ListDefendingSquad[RandomNumber].Item2];
                    float        DistanceUnit = Math.Abs(PosX - TargetSquad.X) + Math.Abs(PosY - TargetSquad.Y);
                    //Move to be in range.
                    List <Vector3> ListRealChoice = new List <Vector3>(GetMVChoice(ActiveUnit));
                    for (int M = 0; M < ListRealChoice.Count; M++)
                    {//Remove every MV that would make it impossible to attack.
                        Distance = Math.Abs(ListRealChoice[M].X - TargetSquad.X) + Math.Abs(ListRealChoice[M].Y - TargetSquad.Y);
                        //Remove every MV that would bring the Unit too close to use its weapon.
                        if (DistanceUnit <= MinRange)
                        {
                            if (Distance <= MinRange)
                            {
                                ListRealChoice.RemoveAt(M--);
                            }
                        }
                        //Check if you can attack it if you moved.
                        else if (Distance < MinRange || Distance > MaxRange)
                        {
                            ListRealChoice.RemoveAt(M--);
                        }
                    }
                    //Must find a spot to move if got there, just to make sure it won't crash in case of logic error.
                    if (ListRealChoice.Count != 0)
                    {
                        int Choice = RandomHelper.Next(ListRealChoice.Count);

                        //Movement initialisation.
                        MovementAnimation.Add(ActiveUnit.X, ActiveUnit.Y, ActiveUnit.Components);

                        //Prepare the Cursor to move.
                        CursorPosition.X = ListRealChoice[Choice].X;
                        CursorPosition.Y = ListRealChoice[Choice].Y;
                        ActiveUnit.SetPosition(CursorPosition);
                        ActiveUnit.SetPosition(ListRealChoice[Choice]);

                        FinalizeMovement(ActiveUnit);
                    }
                    else
                    {
                        //Something is blocking the path.
                        ActiveUnit.EndTurn();
                    }
                    //Unit should be in attack range next time the AI is called.
                    return(true);//Exit the weapon loop.
                }
            }

            #endregion

            return(false);//Can't attack at all.
        }