示例#1
0
        public void GenerateTopology(MapRegion region, IRegionTopologyTemplate template)
        {
            var landCells = region.LandCells;

            int desiredMountainCount = Mathf.RoundToInt(template.MountainsPercentage * landCells.Count() * 0.01f);
            int desiredHillsCount    = Mathf.RoundToInt(template.HillsPercentage * landCells.Count() * 0.01f);

            var elevatedCells = CellRandomSampler.SampleElementsFromSet(
                landCells, desiredHillsCount + desiredMountainCount,
                HillsStartingWeightFunction, HillsDynamicWeightFunction, cell => Grid.GetNeighbors(cell)
                );

            foreach (var cell in elevatedCells)
            {
                ModLogic.ChangeShapeOfCell(cell, CellShape.Hills);
            }

            var mountainousCells = CellRandomSampler.SampleElementsFromSet(
                elevatedCells, desiredMountainCount, MountainWeightFunction
                );

            foreach (var cell in mountainousCells)
            {
                ModLogic.ChangeShapeOfCell(cell, CellShape.Mountains);
            }
        }
示例#2
0
        private Func <IHexCell, bool> GetLakeCandidateFilter(MapRegion region)
        {
            return(delegate(IHexCell cell) {
                if (cell.Terrain != CellTerrain.Snow && cell.Terrain != CellTerrain.Desert &&
                    !cell.Terrain.IsWater() && ModLogic.CanChangeTerrainOfCell(cell, CellTerrain.FreshWater)
                    )
                {
                    bool surroundedByLandOrLakes = Grid.GetNeighbors(cell).All(
                        neighbor => neighbor.Terrain != CellTerrain.ShallowWater &&
                        neighbor.Terrain != CellTerrain.DeepWater
                        );

                    bool hasResourceNode = NodePositionCanon.GetPossessionsOfOwner(cell).Any();

                    bool fewNearbylakes = Grid.GetCellsInRadius(cell, 2).Count(
                        nearby => nearby.Terrain == CellTerrain.FreshWater
                        ) < MaxNearbyLakes;

                    bool noAdjacentDesert = !Grid.GetNeighbors(cell).Any(neighbor => neighbor.Terrain == CellTerrain.Desert);

                    return surroundedByLandOrLakes && !hasResourceNode && fewNearbylakes && noAdjacentDesert;
                }
                else
                {
                    return false;
                }
            });
        }
 private bool CandidateFilter(IHexCell cell)
 {
     return(!RiverCanon.HasRiver(cell) &&
            !NodeLocationCanon.GetPossessionsOfOwner(cell).Any() &&
            ModLogic.CanChangeTerrainOfCell(cell, CellTerrain.ShallowWater) &&
            Grid.GetNeighbors(cell).Any(neighbor => neighbor.Terrain == CellTerrain.ShallowWater) &&
            !Grid.GetNeighbors(cell).Any(neighbor => neighbor.Terrain == CellTerrain.FreshWater));
 }
示例#4
0
 private bool IncreaseScoreFilter(IHexCell cell)
 {
     if (ModLogic.CanChangeVegetationOfCell(cell, CellVegetation.Jungle))
     {
         return(Grid.GetNeighbors(cell).Count(neighbor => neighbor.Vegetation == CellVegetation.Jungle) >= 3);
     }
     else
     {
         return(false);
     }
 }
        public bool HasAccessToFreshWater(IHexCell cell)
        {
            bool isFreshWater = cell.Terrain == CellTerrain.FreshWater;
            bool isSaltWater  = cell.Terrain.IsWater() && !isFreshWater;
            bool hasOasis     = cell.Feature == CellFeature.Oasis;
            bool hasRiver     = RiverCanon.HasRiver(cell);

            if (!isSaltWater)
            {
                if (isFreshWater || hasOasis || hasRiver)
                {
                    return(true);
                }

                foreach (var neighbor in Grid.GetNeighbors(cell))
                {
                    if (neighbor.Terrain == CellTerrain.FreshWater || neighbor.Feature == CellFeature.Oasis)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
示例#6
0
        public IHexCell GetCellAtPoint(Vector3 point)
        {
            if (!Grid.HasCellAtLocation(point))
            {
                return(null);
            }

            var gridCell = Grid.GetCellAtLocation(point);

            foreach (var direction in EnumUtil.GetValues <HexDirection>())
            {
                if (CellContourCanon.IsPointWithinContour(point.ToXZ(), gridCell, direction))
                {
                    return(gridCell);
                }
            }

            foreach (var neighbor in Grid.GetNeighbors(gridCell))
            {
                foreach (var direction in EnumUtil.GetValues <HexDirection>())
                {
                    if (CellContourCanon.IsPointWithinContour(point.ToXZ(), neighbor, direction.Opposite()))
                    {
                        return(neighbor);
                    }
                }
            }

            return(null);
        }
        /// <inheritdoc/>
        public IEnumerable <IHexCell> GetAllCellsAvailableToCity(ICity city)
        {
            var retval = new HashSet <IHexCell>();

            foreach (var tile in PossessionCanon.GetPossessionsOfOwner(city))
            {
                foreach (var neighbor in HexGrid.GetNeighbors(tile))
                {
                    if (IsCellAvailable(city, neighbor))
                    {
                        retval.Add(neighbor);
                    }
                }
            }

            return(retval);
        }
示例#8
0
        public void CreateRivers(
            IEnumerable <IHexCell> landCells, IEnumerable <IHexCell> waterCells,
            int desiredRiveredCells
            )
        {
            var riveredCells = new HashSet <IHexCell>();

            var riverStartCandidates = landCells.Where(GetRiverStartFilter(waterCells)).ToList();

            HashSet <IHexCell> cellsAdjacentToNewRiver = new HashSet <IHexCell>();

            int iterations = landCells.Count() * 10;

            while (riveredCells.Count < desiredRiveredCells && riverStartCandidates.Count > 0 && iterations-- > 0)
            {
                var start = CellRandomSampler.SampleElementsFromSet(
                    riverStartCandidates, 1, RiverStartWeightFunction
                    ).FirstOrDefault();

                riverStartCandidates.Remove(start);

                if (TryBuildNewRiver(
                        landCells, waterCells, start, desiredRiveredCells - riveredCells.Count,
                        ref cellsAdjacentToNewRiver
                        ))
                {
                    foreach (var cell in cellsAdjacentToNewRiver)
                    {
                        riveredCells.Add(cell);
                        riverStartCandidates.Remove(cell);

                        foreach (var startingNeighbor in Grid.GetNeighbors(start))
                        {
                            riverStartCandidates.Remove(startingNeighbor);
                        }
                    }
                }
            }
        }
示例#9
0
        public IEnumerable <IHexCell> GetCellsVisibleToCity(ICity city)
        {
            var retval = new HashSet <IHexCell>();

            foreach (var cellInCityBorders in CellPossessionCanon.GetPossessionsOfOwner(city))
            {
                retval.Add(cellInCityBorders);
                foreach (var neighbor in Grid.GetNeighbors(cellInCityBorders))
                {
                    retval.Add(neighbor);
                }
            }

            return(retval);
        }
        private bool OasisCandidateFilter(IHexCell cell, MapRegion region)
        {
            if (cell.Terrain == CellTerrain.Desert && ModLogic.CanChangeFeatureOfCell(cell, CellFeature.Oasis))
            {
                bool surroundedByLand = Grid.GetNeighbors(cell).All(
                    neighbor => !neighbor.Terrain.IsWater()
                    );

                bool hasResourceNode = NodePositionCanon.GetPossessionsOfOwner(cell).Any();

                return(surroundedByLand && !hasResourceNode);
            }
            else
            {
                return(false);
            }
        }
        private void ExpandFrontier(
            IHexCell center, IEnumerable <IHexCell> availableCells, PriorityQueue <IHexCell> searchFrontier,
            IEnumerable <IHexCell> acceptedCells, IHexCell seed, CrawlingWeightFunction weightFunction
            )
        {
            foreach (var neighbor in Grid.GetNeighbors(center))
            {
                if (availableCells.Contains(neighbor) && !searchFrontier.Contains(neighbor))
                {
                    int candidateWeight = weightFunction(neighbor, seed, acceptedCells);

                    if (candidateWeight >= 0)
                    {
                        searchFrontier.Add(neighbor, candidateWeight);
                    }
                }
            }
        }
示例#12
0
        public bool IsTemplateValidForCity(IBuildingTemplate template, ICity city, ICivilization cityOwner)
        {
            var cityLocation = CityLocationCanon.GetOwnerOfPossession(city);

            if (template.RequiresAdjacentRiver && !RiverCanon.HasRiver(cityLocation))
            {
                return(false);
            }

            if (template.RequiresCoastalCity &&
                !Grid.GetNeighbors(cityLocation).Any(neighbor => neighbor.Terrain.IsWater())
                )
            {
                return(false);
            }

            return(true);
        }
        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);
        }
        private bool ShouldBeMarsh(IHexCell cell, IRegionBiomeTemplate template)
        {
            if (cell.Terrain != CellTerrain.Grassland || cell.Shape != CellShape.Flatlands)
            {
                return(false);
            }

            int adjacentWater = Grid.GetNeighbors(cell).Where(
                neighbor => neighbor.Terrain.IsWater()
                ).Count();

            int adjacentRivers = EnumUtil.GetValues <HexDirection>().Where(
                direction => RiverCanon.HasRiverAlongEdge(cell, direction)
                ).Count();

            float chanceOfMarsh = template.MarshChanceBase
                                  + adjacentWater * template.MarshChancePerAdjacentWater
                                  + adjacentRivers * template.MarshChancePerAdjacentRiver;

            return(UnityEngine.Random.value < chanceOfMarsh);
        }
示例#15
0
        private bool HasObstructionsBetween(IHexCell fromCell, IHexCell toCell)
        {
            if (fromCell != toCell && !Grid.GetNeighbors(fromCell).Contains(toCell))
            {
                var cellLine = Grid.GetCellsInLine(fromCell, toCell).Where(cell => cell != fromCell);

                if (fromCell.Shape == CellShape.Flatlands)
                {
                    return(cellLine.Any(cell => cell.Shape != CellShape.Flatlands || cell.Vegetation.HasTrees()));
                }
                else if (fromCell.Shape == CellShape.Hills || fromCell.Shape == CellShape.Mountains)
                {
                    return(cellLine.Any(
                               cell => cell.Shape == CellShape.Mountains ||
                               (cell.Shape == CellShape.Hills && cell.Vegetation.HasTrees())
                               ));
                }
            }

            return(false);
        }
示例#16
0
        public void HandleCommandOnUnit(AbilityCommandRequest command, IUnit unit)
        {
            if (CanHandleCommandOnUnit(command, unit))
            {
                var unitOwner = UnitPossessionCanon.GetOwnerOfPossession(unit);

                var unitLocation = UnitPositionCanon.GetOwnerOfPossession(unit);

                var nearestCity = GetNearestDomesticCity(unitLocation, unitOwner);

                foreach (var nearbyCell in Grid.GetNeighbors(unitLocation))
                {
                    if (CellPossessionCanon.CanChangeOwnerOfPossession(nearbyCell, nearestCity))
                    {
                        CellPossessionCanon.ChangeOwnerOfPossession(nearbyCell, nearestCity);
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("Command cannot be handled");
            }
        }
        public IEnumerable <MapSection> GetNeighbors(MapSection center)
        {
            HashSet <MapSection> retval;

            if (!NeighborsOfSection.TryGetValue(center, out retval))
            {
                retval = new HashSet <MapSection>();

                foreach (var neighboringCell in center.Cells.SelectMany(cell => Grid.GetNeighbors(cell)))
                {
                    var sectionOfNeighbor = GetSectionOfCell(neighboringCell);

                    if (sectionOfNeighbor != center)
                    {
                        retval.Add(sectionOfNeighbor);
                    }
                }

                NeighborsOfSection[center] = retval;
            }

            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);
        }
示例#19
0
        public ICity Create(IHexCell location, ICivilization owner, string name)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }
            else if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            var newCityGameObject = GameObject.Instantiate(CityPrefab);

            Container.InjectGameObject(newCityGameObject);

            newCityGameObject.transform.position = location.AbsolutePosition;
            newCityGameObject.name = name;
            newCityGameObject.transform.SetParent(CityContainer, true);

            var newCity = newCityGameObject.GetComponent <City>();

            CityLocationCanon.ChangeOwnerOfPossession(newCity, location);

            location.SuppressSlot = true;

            CellModificationLogic.ChangeVegetationOfCell(location, CellVegetation.None);

            if (CityPossessionCanon.CanChangeOwnerOfPossession(newCity, owner))
            {
                CityPossessionCanon.ChangeOwnerOfPossession(newCity, owner);
            }
            else
            {
                throw new CityCreationException("Cannot assign the newly created city to its intended civilization");
            }

            var combatantTemplate = Container.Instantiate <CityCombatantTemplate>(new object[] { newCity });

            newCity.CombatFacade = UnitFactory.BuildUnit(location, combatantTemplate, owner);

            if (CellPossessionCanon.CanChangeOwnerOfPossession(location, newCity))
            {
                CellPossessionCanon.ChangeOwnerOfPossession(location, newCity);
            }
            else
            {
                throw new CityCreationException("Cannot assign the given location to the newly created city");
            }

            foreach (var neighbor in Grid.GetNeighbors(location))
            {
                if (CellPossessionCanon.CanChangeOwnerOfPossession(neighbor, newCity))
                {
                    CellPossessionCanon.ChangeOwnerOfPossession(neighbor, newCity);
                }
            }

            newCity.YieldFocus = YieldFocusType.TotalYield;

            newCity.Population = 1;

            allCities.Add(newCity);

            return(newCity);
        }
示例#20
0
 public bool IsCellCompletelyWithin(IHexCell cell)
 {
     return(Grid.GetNeighbors(cell).All(neighbor => cells.Contains(neighbor)));
 }