private void DrawForbiddenLine(
            HashSet <MapSection> unassignedSections, HashSet <MapSection> forbiddenSections,
            GridPartition partition, IMapTemplate mapTemplate
            )
        {
            int lineXCoord = Mathf.RoundToInt(Grid.CellCountX * UnityEngine.Random.Range(
                                                  mapTemplate.ContinentSeparationLineXMin, mapTemplate.ContinentSeparationLineXMax
                                                  ));

            IHexCell startCell = Grid.GetCellAtCoordinates(HexCoordinates.FromOffsetCoordinates(lineXCoord, 0));
            IHexCell endCell   = Grid.GetCellAtCoordinates(HexCoordinates.FromOffsetCoordinates(lineXCoord, Grid.CellCountZ - 1));

            var cellLine = Grid.GetCellsInLine(startCell, endCell);

            var sectionsOnLine = cellLine.Select(cell => partition.GetSectionOfCell(cell)).Distinct();

            var sectionsToForbid = sectionsOnLine.SelectMany(section => partition.GetNeighbors(section))
                                   .Concat(sectionsOnLine).Distinct();

            foreach (var section in sectionsToForbid)
            {
                unassignedSections.Remove(section);
                forbiddenSections.Add(section);
            }
        }
Example #2
0
        public void DecomposeUnits(SerializableMapData mapData)
        {
            foreach (var unitData in mapData.Units)
            {
                var unitLocation    = Grid.GetCellAtCoordinates(unitData.Location);
                var templateToBuild = AvailableUnitTemplates.Where(template => template.name.Equals(unitData.Template)).First();
                var unitOwner       = CivilizationFactory.AllCivilizations.Where(civ => civ.Template.Name.Equals(unitData.Owner)).First();
                var promotionTree   = PromotionTreeComposer.DecomposePromotionTree(unitData.PromotionTree);

                var newUnit = UnitFactory.BuildUnit(unitLocation, templateToBuild, unitOwner, promotionTree);

                newUnit.CurrentMovement  = unitData.CurrentMovement;
                newUnit.CurrentHitpoints = unitData.Hitpoints;
                newUnit.Experience       = unitData.Experience;
                newUnit.Level            = unitData.Level;

                if (unitData.CurrentPath != null)
                {
                    newUnit.CurrentPath = unitData.CurrentPath.Select(coord => Grid.GetCellAtCoordinates(coord)).ToList();
                }

                if (unitData.IsSetUpToBombard)
                {
                    newUnit.SetUpToBombard();
                }
            }
        }
        public void DecomposeBarbarians(SerializableMapData mapData)
        {
            foreach (var encampmentData in mapData.Encampments)
            {
                var location = Grid.GetCellAtCoordinates(encampmentData.Location);

                var newEncampment = EncampmentFactory.CreateEncampment(location);

                newEncampment.SpawnProgress = encampmentData.SpawnProgress;
            }
        }
        public void DecomposeCivilizations(SerializableMapData mapData)
        {
            foreach (var civData in mapData.Civilizations)
            {
                var civTemplate = AvailableCivTemplates.Where(template => template.Name.Equals(civData.TemplateName)).FirstOrDefault();

                var newCiv = CivilizationFactory.Create(civTemplate);

                newCiv.GoldStockpile    = civData.GoldStockpile;
                newCiv.CultureStockpile = civData.CultureStockpile;

                PolicyComposer.DecomposePoliciesIntoCiv(civData.SocialPolicies, newCiv);

                DecomposeTechs(civData, newCiv);

                if (civData.ExploredCells != null)
                {
                    foreach (var exploredCoords in civData.ExploredCells)
                    {
                        var exploredCell = Grid.GetCellAtCoordinates(exploredCoords);

                        ExplorationCanon.SetCellAsExploredByCiv(exploredCell, newCiv);
                    }
                }

                DecomposeFreeBuildings(civData, newCiv);

                GoldenAgeCanon.SetGoldenAgeProgressForCiv(newCiv, civData.GoldenAgeProgress);
                GoldenAgeCanon.SetPreviousGoldenAgesForCiv(newCiv, civData.PreviousGoldenAges);

                if (civData.GoldenAgeTurnsLeft > 0)
                {
                    GoldenAgeCanon.StartGoldenAgeForCiv(newCiv, civData.GoldenAgeTurnsLeft);
                }
            }

            foreach (var discoveryPair in mapData.CivDiscoveryPairs)
            {
                var civOne = CivilizationFactory.AllCivilizations.Where(civ => civ.Template.Name.Equals(discoveryPair.Item1)).FirstOrDefault();
                var civTwo = CivilizationFactory.AllCivilizations.Where(civ => civ.Template.Name.Equals(discoveryPair.Item2)).FirstOrDefault();

                if (CivDiscoveryCanon.CanEstablishDiscoveryBetweenCivs(civOne, civTwo))
                {
                    CivDiscoveryCanon.EstablishDiscoveryBetweenCivs(civOne, civTwo);
                }
                else
                {
                    throw new InvalidOperationException(
                              string.Format("Invalid discovery for civs of template names {0} and {1}", discoveryPair.Item1, discoveryPair.Item2)
                              );
                }
            }
        }
        public void DecomposeImprovements(SerializableMapData mapData)
        {
            foreach (var improvementData in mapData.Improvements)
            {
                var templateToBuild = AvailableImprovementTemplates.Where(
                    template => template.name.Equals(improvementData.Template)
                    ).First();
                var locationToBuild = Grid.GetCellAtCoordinates(improvementData.Location);

                ImprovementFactory.BuildImprovement(
                    templateToBuild, locationToBuild, improvementData.WorkInvested,
                    improvementData.IsConstructed, improvementData.IsPillaged
                    );
            }
        }
Example #6
0
        public void DecomposeCapitalCities(SerializableMapData mapData)
        {
            for (int i = 0; i < mapData.Civilizations.Count; i++)
            {
                var civ     = CivFactory.AllCivilizations[i];
                var civData = mapData.Civilizations[i];

                if (civData.CapitalLocation != null)
                {
                    var capitalLocation = Grid.GetCellAtCoordinates(civData.CapitalLocation.Value);

                    var capitalCity = CityLocationCanon.GetPossessionsOfOwner(capitalLocation).FirstOrDefault();

                    CapitalCityCanon.SetCapitalOfCiv(civ, capitalCity);
                }
            }
        }
        public void DecomposeBuildings(SerializableMapData mapData)
        {
            foreach (var buildingData in mapData.Buildings)
            {
                var templateToBuild = AllBuildingTemplates.Where(template => template.name.Equals(buildingData.Template)).First();

                var cellAtCoords   = Grid.GetCellAtCoordinates(buildingData.CityLocation);
                var cityAtLocation = CityLocationCanon.GetPossessionsOfOwner(cellAtCoords).First();

                var newBuilding = BuildingFactory.BuildBuilding(templateToBuild, cityAtLocation);

                for (int i = 0; i < newBuilding.Slots.Count; i++)
                {
                    var slot = newBuilding.Slots[i];

                    slot.IsOccupied = buildingData.IsSlotOccupied[i];
                    slot.IsLocked   = buildingData.IsSlotLocked  [i];
                }
            }
        }
        public void DecomposeCities(SerializableMapData mapData)
        {
            foreach (var cityData in mapData.Cities)
            {
                var owner    = CivilizationFactory.AllCivilizations.Where(civ => civ.Template.Name.Equals(cityData.Owner)).FirstOrDefault();
                var location = Grid.GetCellAtCoordinates(cityData.Location);

                var newCity = CityFactory.Create(location, owner, cityData.Name);

                newCity.Population       = cityData.Population;
                newCity.FoodStockpile    = cityData.FoodStockpile;
                newCity.CultureStockpile = cityData.CultureStockpile;
                newCity.YieldFocus       = cityData.YieldFocus;
                newCity.CombatFacade.CurrentHitpoints = cityData.Hitpoints;
                newCity.CombatFacade.CurrentMovement  = cityData.CurrentMovement;

                DecomposeActiveProject(cityData, newCity);
                DecomposeAppliedTemplates(cityData, newCity);
            }
        }
        public IDiplomaticExchange DecomposeExchange(SerializableDiplomaticExchangeData exchangeData)
        {
            var retval = ExchangeFactory.BuildExchangeForType(exchangeData.Type);

            retval.IntegerInput = exchangeData.IntegerInput;

            if (exchangeData.CityInputLocation != null)
            {
                var cellAtCoords = Grid.GetCellAtCoordinates(exchangeData.CityInputLocation.Value);

                if (cellAtCoords == null)
                {
                    throw new InvalidOperationException("Could not find a cell at the specified coordinates");
                }

                var cityAtLocation = CityLocationCanon.GetPossessionsOfOwner(cellAtCoords).FirstOrDefault();

                if (cityAtLocation == null)
                {
                    throw new InvalidOperationException("Could not find a city at the specified location");
                }

                retval.CityInput = cityAtLocation;
            }

            if (exchangeData.ResourceInput != null)
            {
                var resourceOfName = AvailableResources.Where(resource => resource.name.Equals(exchangeData.ResourceInput)).FirstOrDefault();

                if (resourceOfName == null)
                {
                    throw new InvalidOperationException("Could not find the resource of the specified name");
                }

                retval.ResourceInput = resourceOfName;
            }

            return(retval);
        }
        public void DecomposeCells(SerializableMapData mapData)
        {
            Grid.Build(mapData.CellCountX, mapData.CellCountZ);

            foreach (var cellData in mapData.HexCells)
            {
                var cellToModify = Grid.GetCellAtCoordinates(cellData.Coordinates);

                CellModificationLogic.ChangeTerrainOfCell(cellToModify, cellData.Terrain);
                CellModificationLogic.ChangeShapeOfCell(cellToModify, cellData.Shape);
                CellModificationLogic.ChangeVegetationOfCell(cellToModify, cellData.Vegetation);
                CellModificationLogic.ChangeFeatureOfCell(cellToModify, cellData.Feature);
                CellModificationLogic.ChangeHasRoadsOfCell(cellToModify, cellData.HasRoads);

                cellToModify.SuppressSlot = cellData.SuppressSlot;

                //Converging rivers (where two rivers combine and flow into a third) have
                //order-sensitive creation, since attempting to place both of the inflow
                //rivers before the outflow river has been created is invalid. To account for
                //this, we delay the creation of any invalid rivers (since those represent
                //an inflow being attached to another inflow) until after all other rivers
                //have been placed.
                var delayedRivers = new List <System.Tuple <IHexCell, HexDirection, RiverFlow> >();

                for (int i = 0; i < 6; i++)
                {
                    var edge = (HexDirection)i;

                    if (cellData.HasRiverAtEdge[i] && !RiverCanon.HasRiverAlongEdge(cellToModify, edge))
                    {
                        if (RiverCanon.CanAddRiverToCell(cellToModify, edge, cellData.DirectionOfRiverAtEdge[i]))
                        {
                            RiverCanon.AddRiverToCell(cellToModify, edge, cellData.DirectionOfRiverAtEdge[i]);
                        }
                        else
                        {
                            delayedRivers.Add(new System.Tuple <IHexCell, HexDirection, RiverFlow>(
                                                  cellToModify, edge, cellData.DirectionOfRiverAtEdge[i]
                                                  ));
                        }
                    }
                }

                foreach (var river in delayedRivers)
                {
                    if (RiverCanon.CanAddRiverToCell(river.Item1, river.Item2, river.Item3))
                    {
                        RiverCanon.AddRiverToCell(river.Item1, river.Item2, river.Item3);
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format(
                                                                "Failed to decompose river ({0}, {1}, {2})",
                                                                river.Item1, river.Item2, river.Item3
                                                                ));
                    }
                }

                cellToModify.WorkerSlot.IsOccupied = cellData.IsSlotOccupied;
                cellToModify.WorkerSlot.IsLocked   = cellData.IsSlotLocked;
            }

            foreach (var chunk in Grid.Chunks)
            {
                chunk.Refresh(MapRendering.TerrainRefreshType.All);
            }
        }