Example #1
0
        public Func <IHexCell, bool> GetCaptureCivilianFilter(IUnit captor)
        {
            return(delegate(IHexCell cell) {
                var captiveCandidates = UnitPositionCanon.GetPossessionsOfOwner(cell);

                return captiveCandidates.Any() && captiveCandidates.All(
                    captiveCandidate => captiveCandidate.Type.IsCivilian() && CombatExecuter.CanPerformMeleeAttack(captor, captiveCandidate)
                    );
            });
        }
 private void TryRemoveUnit(IHexCell location)
 {
     foreach (var unit in new List <IUnit>(UnitPositionCanon.GetPossessionsOfOwner(location)))
     {
         unit.Destroy();
     }
 }
Example #3
0
        public IUnit GetNextAttackTargetOnCell(IHexCell cell)
        {
            var unitsAt = UnitPositionCanon.GetPossessionsOfOwner(cell);

            if (!unitsAt.Any())
            {
                return(null);
            }

            IUnit candidate;

            candidate = unitsAt.FirstOrDefault(unit => unit.Type == UnitType.City);

            if (candidate != null)
            {
                return(candidate);
            }

            candidate = unitsAt.FirstOrDefault(unit => unit.Type.IsWaterMilitary());

            if (candidate != null)
            {
                return(candidate);
            }

            candidate = unitsAt.FirstOrDefault(unit => unit.Type.IsLandMilitary());

            if (candidate != null)
            {
                return(candidate);
            }

            return(unitsAt.FirstOrDefault());
        }
Example #4
0
        public bool IsCityGarrisoned(ICity city)
        {
            var cityLocation = CityLocationCanon.GetOwnerOfPossession(city);

            var militaryUnitsAt = UnitPositionCanon
                                  .GetPossessionsOfOwner(cityLocation)
                                  .Where(unit => unit.Type != UnitType.Civilian && unit.Type != UnitType.City);

            return(militaryUnitsAt.Any());
        }
Example #5
0
        private bool DoesCellHaveUnitsForeignTo(IHexCell cell, ICivilization domesticCiv)
        {
            foreach (var unit in UnitPositionCanon.GetPossessionsOfOwner(cell))
            {
                if (UnitPossessionCanon.GetOwnerOfPossession(unit) != domesticCiv)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #6
0
 private void OnChunkFinishedRefreshing(IMapChunk chunk)
 {
     foreach (var cell in chunk.CenteredCells)
     {
         foreach (var unit in UnitPositionCanon.GetPossessionsOfOwner(cell))
         {
             if (!RepositioningCoroutineForUnit.ContainsKey(unit))
             {
                 RepositioningCoroutineForUnit[unit] = CoroutineInvoker.StartCoroutine(
                     RefreshAfterChunksDone(unit, cell.OverlappingChunks)
                     );
             }
         }
     }
 }
Example #7
0
        private void OnCellBecameExploredByCiv(Tuple <IHexCell, ICivilization> data)
        {
            var cell = data.Item1;
            var civ  = data.Item2;

            foreach (var unit in UnitPositionCanon.GetPossessionsOfOwner(cell))
            {
                var unitOwner = UnitPossessionCanon.GetOwnerOfPossession(unit);

                if (CivDiscoveryCanon.CanEstablishDiscoveryBetweenCivs(civ, unitOwner))
                {
                    CivDiscoveryCanon.EstablishDiscoveryBetweenCivs(civ, unitOwner);
                }
            }
        }
        private bool HasRangedAttackTarget()
        {
            var cityLocation = CityLocationCanon.GetOwnerOfPossession(ObjectToDisplay);

            foreach (var cell in Grid.GetCellsInRadius(cityLocation, Config.CityAttackRange))
            {
                foreach (var unit in UnitPositionCanon.GetPossessionsOfOwner(cell))
                {
                    if (CombatExecuter.CanPerformRangedAttack(ObjectToDisplay.CombatFacade, unit))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        private bool IsConditionMetByAdjacentUnits(IUnit centerUnit)
        {
            var unitOwner    = UnitPossessionCanon.GetOwnerOfPossession(centerUnit);
            var unitLocation = UnitPositionCanon.GetOwnerOfPossession(centerUnit);

            foreach (var adjacentCell in Grid.GetNeighbors(unitLocation))
            {
                foreach (var adjacentUnit in UnitPositionCanon.GetPossessionsOfOwner(adjacentCell))
                {
                    var adjacentOwner = UnitPossessionCanon.GetOwnerOfPossession(adjacentUnit);

                    if (adjacentOwner == unitOwner && UnitTypeArguments.Contains(adjacentUnit.Type))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #10
0
        public void PlaceStartingUnitsInRegion(
            MapRegion region, ICivilization owner, IMapTemplate mapTemplate
            )
        {
            if (owner.Template.IsBarbaric)
            {
                return;
            }

            var centralLocation = GetBestStartingCell(region);

            if (centralLocation == null)
            {
                throw new InvalidOperationException("Failed to find an appropriate central location");
            }

            UnitFactory.BuildUnit(centralLocation, mapTemplate.StartingUnits[0], owner);

            for (int i = 1; i < mapTemplate.StartingUnits.Count; i++)
            {
                var unitTemplate = mapTemplate.StartingUnits[i];

                var location = Grid.GetCellsInRadius(centralLocation, 2).Where(
                    cell => !UnitPositionCanon.GetPossessionsOfOwner(cell).Any() &&
                    UnitFactory.CanBuildUnit(cell, unitTemplate, owner) &&
                    cell != null
                    ).FirstOrDefault();

                if (location == null)
                {
                    throw new InvalidOperationException(
                              string.Format("Failed to place starting unit {0} for civ {1}", unitTemplate, owner)
                              );
                }
                else
                {
                    UnitFactory.BuildUnit(location, unitTemplate, owner);
                }
            }
        }
        private IEnumerable <IUnit> GetNearbyAllies(IUnit unit, int maxRange)
        {
            var unitLocation = UnitPositionCanon.GetOwnerOfPossession(unit);
            var unitOwner    = UnitPossessionCanon.GetOwnerOfPossession(unit);

            var retval = new List <IUnit>();

            if (unitLocation != null && unitOwner != null)
            {
                var cellsInRange = Grid.GetCellsInRadius(unitLocation, maxRange);

                foreach (var nearbyUnit in cellsInRange.SelectMany(cell => UnitPositionCanon.GetPossessionsOfOwner(cell)))
                {
                    if (UnitPossessionCanon.GetOwnerOfPossession(nearbyUnit) == unitOwner)
                    {
                        retval.Add(nearbyUnit);
                    }
                }
            }

            return(retval);
        }
        private IEnumerable <IUnit> GetFriendlyUnitsNeighboringUnit(IUnit unit)
        {
            var retval = new List <IUnit>();

            var unitLocation = UnitPositionCanon.GetOwnerOfPossession(unit);
            var unitOwner    = UnitPossessionCanon.GetOwnerOfPossession(unit);

            if (unitLocation != null)
            {
                foreach (var neighboringCell in Grid.GetNeighbors(unitLocation))
                {
                    foreach (var neighboringUnit in UnitPositionCanon.GetPossessionsOfOwner(neighboringCell))
                    {
                        if (unitOwner == UnitPossessionCanon.GetOwnerOfPossession(neighboringUnit))
                        {
                            retval.Add(neighboringUnit);
                        }
                    }
                }
            }

            return(retval);
        }
        private IEnumerator ResetCall_Coroutine(IHexCell cell)
        {
            yield return(new WaitForEndOfFrame());

            var unitsAt = UnitPositionCanon.GetPossessionsOfOwner(cell);

            foreach (var unit in unitsAt)
            {
                if (IsUnitValidForIcon(unit))
                {
                    if (!IconOfUnit.ContainsKey(unit))
                    {
                        BuildIconForUnit(unit);
                    }
                }
                else
                {
                    DestroyIconOfUnit(unit);
                }
            }

            CellResetRoroutines.Remove(cell);
        }
        public void HandleCommandOnUnit(AbilityCommandRequest command, IUnit unit)
        {
            if (CanHandleCommandOnUnit(command, unit))
            {
                var unitPosition = UnitPositionCanon.GetOwnerOfPossession(unit);
                var unitOwner    = UnitPossessionCanon.GetOwnerOfPossession(unit);

                var nearbyCells = Grid.GetCellsInRadius(unitPosition, 1);

                foreach (var nearbyUnit in nearbyCells.SelectMany(cell => UnitPositionCanon.GetPossessionsOfOwner(cell)))
                {
                    var nearbyOwner = UnitPossessionCanon.GetOwnerOfPossession(nearbyUnit);

                    if (nearbyUnit.Type.IsWaterMilitary() && nearbyOwner == unitOwner)
                    {
                        nearbyUnit.CurrentHitpoints = nearbyUnit.MaxHitpoints;
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("Cannot handle command");
            }
        }
Example #15
0
        private void OnCellPointerEnter(IHexCell cell)
        {
            var cityOnCell = CityLocationCanon.GetPossessionsOfOwner(cell).FirstOrDefault();

            if (cityOnCell != null && CombatExecuter.CanPerformRangedAttack(SelectedUnit, cityOnCell.CombatFacade))
            {
                SetCityToAttack(cityOnCell);
                return;
            }

            var unitsOnTile = UnitPositionCanon.GetPossessionsOfOwner(cell).Where(unit => unit.Type != UnitType.City);

            if (unitsOnTile.Count() > 0)
            {
                var attackCandidate = GetUnitToAttackFromStack(unitsOnTile);
                if (attackCandidate != null && CombatExecuter.CanPerformRangedAttack(SelectedUnit, attackCandidate))
                {
                    SetUnitToAttack(attackCandidate);
                    return;
                }
            }

            Clear();
        }
 private bool HasUndefendedEncampment(IHexCell cell)
 {
     return(EncampmentLocationCanon.GetPossessionsOfOwner(cell).Any() &&
            !UnitPositionCanon.GetPossessionsOfOwner(cell).Any());
 }