コード例 #1
0
        private void AiTurn()
        {
            foreach (var unit in _activeCiv.Units.Where(u => !u.Dead).ToList())
            {
                var currentTile = unit.CurrentLocation;
                switch (unit.AIrole)
                {
                case AIroleType.Attack:
                    break;

                case AIroleType.Defend:
                    if (currentTile.CityHere != null)
                    {
                        if (currentTile.UnitsHere.Count(u => u != unit && u.AIrole == AIroleType.Defend) <
                            2 + currentTile.CityHere.Size / 3)
                        {
                            if (unit.Order == OrderType.Fortify || unit.Order == OrderType.Fortified)
                            {
                                unit.Order = OrderType.Fortified;
                            }
                            else
                            {
                                unit.Order = OrderType.Fortify;
                            }
                            unit.MovePointsLost = unit.MovePoints;
                        }
                    }
                    else
                    {
                    }
                    break;

                case AIroleType.NavalSuperiority:
                    break;

                case AIroleType.AirSuperiority:
                    break;

                case AIroleType.SeaTransport:
                    break;

                case AIroleType.Settle:
                    if (currentTile.Fertility == -2)
                    {
                        CityActions.AIBuildCity(unit, this);
                    }
                    var cityTile = CurrentMap.CityRadius(currentTile)
                                   .FirstOrDefault(t => t.CityHere != null);
                    if (cityTile == null)
                    {
                        var moreFertile = MovementFunctions.GetPossibleMoves(this, currentTile, unit)
                                          .Where(n => n.Fertility > currentTile.Fertility).OrderByDescending(n => n.Fertility)
                                          .FirstOrDefault();
                        if (moreFertile == null)
                        {
                            CityActions.AIBuildCity(unit, this);
                        }
                        else
                        {
                            if (MovementFunctions.UnitMoved(this, unit, moreFertile, currentTile))
                            {
                                currentTile = moreFertile;
                                if (unit.MovePoints > 0)
                                {
                                    CityActions.AIBuildCity(unit, this);
                                }
                            }
                        }
                    }

                    break;

                case AIroleType.Diplomacy:
                    break;

                case AIroleType.Trade:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                while (unit.MovePoints > 0)
                {
                    var possibleMoves = MovementFunctions.GetPossibleMoves(this, currentTile, unit).ToList();
                    if (unit.AttackBase == 0)
                    {
                        possibleMoves = possibleMoves
                                        .Where(m => m.UnitsHere.Count == 0 || m.UnitsHere[0].Owner == unit.Owner).ToList();
                    }
                    if (possibleMoves.Count == 0)
                    {
                        unit.SkipTurn();
                    }
                    else
                    {
                        var destination = Random.ChooseFrom(possibleMoves);
                        if (destination.UnitsHere.Count > 0 && destination.UnitsHere[0].Owner != unit.Owner)
                        {
                            unit.Order = OrderType.NoOrders;
                            MovementFunctions.AttackAtTile(unit, this, destination);
                        }
                        else if (MovementFunctions.UnitMoved(this, unit, destination, currentTile))
                        {
                            currentTile = destination;
                        }
                    }
                }
            }
            ChoseNextCiv();
        }