private void SetProspectiveTravelGoal(IHexCell unitLocation, IHexCell goal)
        {
            Clear();

            ProspectiveTravelGoal = goal;
            ProspectivePath       = HexPathfinder.GetShortestPathBetween(
                unitLocation, goal,
                delegate(IHexCell currentCell, IHexCell nextCell) {
                return(UnitPositionCanon.GetTraversalCostForUnit(
                           SelectedUnit, currentCell, nextCell, false
                           ));
            }
                );

            PathDrawer.ClearPath();

            if (ProspectivePath != null)
            {
                PathDrawer.DrawPath(ProspectivePath);
            }
            else
            {
                OverlayManager.ShowOverlayOfCell(ProspectiveTravelGoal, CellOverlayType.UnreachableIndicator);
            }
        }
        public bool IsCityConnectedToCapital(ICity city)
        {
            var cityOwner = CityPossessionCanon.GetOwnerOfPossession(city);

            var capitalOfOwner = CapitalCityCanon.GetCapitalOfCiv(cityOwner);

            if (capitalOfOwner == city)
            {
                return(true);
            }

            if (capitalOfOwner == null)
            {
                return(false);
            }

            var cityLocation    = CityLocationCanon.GetOwnerOfPossession(city);
            var capitalLocation = CityLocationCanon.GetOwnerOfPossession(capitalOfOwner);

            var pathTo = HexPathfinder.GetShortestPathBetween(
                cityLocation, capitalLocation, ConnectionPathCostLogic.BuildCapitalConnectionPathCostFunction(cityOwner)
                );

            return(pathTo != null);
        }
        public bool AreCivilizationsConnected(ICivilization civOne, ICivilization civTwo)
        {
            var capitalOne = CapitalCityCanon.GetCapitalOfCiv(civOne);
            var capitalTwo = CapitalCityCanon.GetCapitalOfCiv(civTwo);

            var capitalOneLocation = CityLocationCanon.GetOwnerOfPossession(capitalOne);
            var capitalTwoLocation = CityLocationCanon.GetOwnerOfPossession(capitalTwo);

            return(HexPathfinder.GetShortestPathBetween(
                       capitalOneLocation, capitalTwoLocation, ConnectionPathCostLogic.BuildCivConnectionPathCostFunction(civOne, civTwo)
                       ) != null);
        }
        public void StartExecution()
        {
            if (DesiredLocation == null)
            {
                throw new InvalidOperationException("Cannot execute while DesiredLocation is null");
            }

            Status = CommandStatus.Running;

            UnitToMove.CurrentPath = null;

            var unitLocation = UnitPositionCanon.GetOwnerOfPossession(UnitToMove);

            if (unitLocation == null)
            {
                throw new InvalidOperationException("Cannot move a unit with a null location");
            }

            if (unitLocation == DesiredLocation)
            {
                Status = CommandStatus.Succeeded;
            }
            else
            {
                var pathTo = HexPathfinder.GetShortestPathBetween(
                    unitLocation, DesiredLocation,
                    (current, next) => UnitPositionCanon.GetTraversalCostForUnit(UnitToMove, current, next, false)
                    );

                if (pathTo != null)
                {
                    UnitToMove.CurrentPath = pathTo;

                    UnitToMove.PerformMovement(false, OnUnitFinishedMovement);
                }
                else
                {
                    Status = CommandStatus.Failed;
                }
            }
        }
        public bool IsMeleeAttackValid(IUnit attacker, IUnit defender)
        {
            if (!CommonAttackValidityLogic.DoesAttackMeetCommonConditions(attacker, defender))
            {
                return(false);
            }

            var attackerLocation = UnitPositionCanon.GetOwnerOfPossession(attacker);
            var defenderLocation = UnitPositionCanon.GetOwnerOfPossession(defender);

            if (defenderLocation == null)
            {
                return(false);
            }

            var shortestPath = HexPathfinder.GetShortestPathBetween(
                attackerLocation, defenderLocation, attacker.CurrentMovement,
                (current, next) => UnitPositionCanon.GetTraversalCostForUnit(attacker, current, next, true),
                Grid.Cells
                );

            if (shortestPath == null)
            {
                return(false);
            }

            if (!LineOfSightLogic.GetCellsVisibleToUnit(attacker).Contains(defenderLocation))
            {
                return(false);
            }

            if (attacker.CombatStrength <= 0)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 6
0
        public void PerformMeleeAttack(IUnit attacker, IUnit defender, Action successAction, Action failAction)
        {
            if (!CanPerformMeleeAttack(attacker, defender))
            {
                throw new InvalidOperationException("CanPerformMeleeCombat must return true");
            }

            var attackerLocation = UnitPositionCanon.GetOwnerOfPossession(attacker);
            var defenderLocation = UnitPositionCanon.GetOwnerOfPossession(defender);

            var pathToDefender = HexPathfinder.GetShortestPathBetween(
                attackerLocation, defenderLocation, attacker.CurrentMovement,
                (current, next) => UnitPositionCanon.GetTraversalCostForUnit(attacker, current, next, true),
                Grid.Cells
                );

            attacker.CurrentPath = pathToDefender.GetRange(0, pathToDefender.Count - 1);

            attacker.PerformMovement(
                false, PerformMeleeAttack_GetPostMovementCallback(
                    attacker, defender, defenderLocation, successAction, failAction
                    )
                );
        }
Exemplo n.º 7
0
        private bool TryBuildNewRiver(
            IEnumerable <IHexCell> landCells, IEnumerable <IHexCell> waterCells,
            IHexCell start, int maxRiveredCellCount, ref HashSet <IHexCell> riveredCells
            )
        {
            riveredCells.Clear();

            IHexCell end;

            int maxRiverLength = Math.Max(2, Mathf.RoundToInt(maxRiveredCellCount / 3f));

            if (TryGetRiverEnd(landCells, start, waterCells, maxRiverLength, out end))
            {
                riverPath.Clear();

                riverPath.Add(start);

                var weightFunction = BuildRiverWeightFunction(waterCells);

                var pathFrom = HexPathfinder.GetShortestPathBetween(start, end, weightFunction, landCells);

                if (pathFrom == null)
                {
                    return(false);
                }

                riverPath.AddRange(pathFrom);

                CreateRiverStartPoint(riverPath[0], riverPath[1], riveredCells);

                RiverPathResults pathSectionResults = new RiverPathResults(false, false);

                int i = 1;
                for (; i < riverPath.Count - 1; i++)
                {
                    if (RiverCanon.HasRiver(riverPath[i]))
                    {
                        pathSectionResults = CreateRiverEndpoint(
                            riverPath[i - 1], riverPath[i], waterCells, riveredCells
                            );

                        if (pathSectionResults.Completed)
                        {
                            pathSectionResults.FoundWater = true;
                        }

                        break;
                    }
                    else
                    {
                        pathSectionResults = CreateRiverAlongCell(
                            riverPath[i - 1], riverPath[i], riverPath[i + 1],
                            waterCells, riveredCells
                            );
                    }

                    if (!pathSectionResults.Completed)
                    {
                        Debug.LogWarning("Failed to complete river");
                        break;
                    }
                    else if (pathSectionResults.FoundWater)
                    {
                        break;
                    }
                }

                if (!pathSectionResults.FoundWater)
                {
                    pathSectionResults = CreateRiverEndpoint(
                        riverPath[i - 1], riverPath[i], waterCells, riveredCells
                        );

                    if (!pathSectionResults.Completed)
                    {
                        Debug.LogWarning("Failed to resolve river endpoint");
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }