Example #1
0
        /// <summary>
        /// Moves the unit to the desired cell.  Takes the appropriate
        /// number of turns.
        /// </summary>
        /// <param name="destination">The coordinates on the <see cref="Grid"/>
        /// the <see cref="Unit"/> is trying to move to.</param>
        /// <returns></returns>
        public virtual MoveResult MoveTo(Point destination)
        {
            GameRoot root = GameRoot.Instance;
            Grid     grid = root.Grid;

            //create a journey to the destination
            Journey journey = CreateJourney(destination);
            ReadOnlyCollection <Point> path = journey.CalculateExistingPath();

            if (journey == null)
            {
                return(MoveResult.UnreachableTerrain);
            }

            GridCell currentCell = grid.GetCell(this.Coordinates);
            GridCell nextCell    = grid.GetCell(this.Journey.PeekNextPoint());
            int      roadBonus   = root.Ruleset.RoadMovementBonus;

            if (roadBonus == 0)
            {
                throw new InvalidOperationException(ServerResources.RulesetHasInvalidRoadBonus);
            }

            if (this.MovesLeft == 0)
            {
                OnTurnFinished();
                return(MoveResult.NoMovesLeft);
            }

            //fight any enemies at the destination
            MoveResult result = CombatUnits(nextCell);

            if (result == MoveResult.MoveSuccess)
            {
                //we're still alive, so we must have taken the new cell.
                int moveCost = nextCell.Terrain.MovementCost;

                if (currentCell.HasRoad && nextCell.HasRoad)
                {
                    if (currentCell.HasRailroad && nextCell.HasRailroad)
                    {
                        moveCost = 0;
                    }
                    else
                    {
                        moveCost = moveCost / roadBonus;
                    }
                }

                //we can now actually move to the new cell.
                this.Journey = journey;
                ContinueJourney();

                if (this.MovesLeft >= moveCost)
                {
                    this.MovesLeft -= moveCost;
                }
                else
                {
                    this.MovesLeft = 0;
                }

                //check for villages
                if (nextCell.Village != null)
                {
                    VillageGoody     goody = nextCell.Village.Discover(this);
                    VillageEventArgs e     = new VillageEventArgs(nextCell.Village, goody);
                    OnVillageEncountered(e);
                    nextCell.Village = null;
                }

                // Check to see if we just invaded a foreign city.
                if (nextCell.City != null && nextCell.City.ParentCountry != ParentCountry)
                {
                    nextCell.City.Capture(this);
                }

                OnMoved();

                if (this.MovesLeft <= 0)
                {
                    OnTurnFinished();
                }
            }

            return(result);
        }