public override void DoUpdate(GameTime gameTime)
        {
            if (Map.GetTerrain((int)Map.CursorPosition.X, (int)Map.CursorPosition.Y, 0).CapturedPlayerIndex == Map.ActivePlayerIndex &&
                Map.GetTerrain((int)Map.CursorPosition.X, (int)Map.CursorPosition.Y, 0).CapturePoints == 0)
            {
                if (InputHelper.InputUpPressed())
                {
                    Map.BuildingMenuCursor -= (Map.BuildingMenuCursor > 0) ? 1 : 0;
                }
                else if (InputHelper.InputDownPressed())
                {
                    Map.BuildingMenuCursor += (Map.BuildingMenuCursor < Map.ListCurrentBuildingChoice.Count - 1) ? 1 : 0;
                }
                else if (InputHelper.InputConfirmPressed() || MouseHelper.InputLeftButtonReleased())
                {
                    UnitConquest NewUnit = new UnitConquest(Map.ListCurrentBuildingChoice[Map.BuildingMenuCursor], Map.Content, Map.DicRequirement, Map.DicEffect);
                    //Make sure to give the Squad an unused ID.
                    uint NewUnitID = Map.GetNextUnusedUnitID();

                    NewUnit.EndTurn();
                    NewUnit.ID = NewUnitID;
                    Map.SpawnUnit(Map.ActivePlayerIndex, NewUnit, Map.CursorPosition);
                }
                else if (InputHelper.InputCancelPressed() || MouseHelper.InputRightButtonReleased())
                {
                    //Reset the cursor.
                    Map.sndCancel.Play();
                }
            }
        }
예제 #2
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));
            }
        }
예제 #3
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.
        }