public void GenerateMap(IMapTemplate template, IMapGenerationVariables variables)
 {
     if (GenerateMapCoroutine == null)
     {
         GenerateMapCoroutine = CoroutineInvoker.StartCoroutine(GenerateMap_Perform(template, variables));
     }
 }
        private void GeneratePlayers(IMapGenerationVariables variables)
        {
            CivFactory.Clear();

            foreach (var civTemplate in variables.Civilizations)
            {
                var newCiv = CivFactory.Create(civTemplate, variables.StartingTechs);

                PlayerFactory.CreatePlayer(newCiv, BrainPile.HumanBrain);
            }

            var barbarianCiv = CivFactory.Create(CivConfig.BarbarianTemplate);

            PlayerFactory.CreatePlayer(barbarianCiv, BrainPile.BarbarianBrain);
        }
        private IEnumerator GenerateMap_Perform(IMapTemplate template, IMapGenerationVariables variables)
        {
            MapComposer.ClearRuntime(false);

            while (MapComposer.IsProcessing)
            {
                yield return(new WaitForEndOfFrame());
            }

            Profiler.BeginSample("GenerateMap()");

            CellClimateLogic.Reset(template);

            var oldRandomState = SetRandomState();

            Grid.Build(variables.CellCountX, variables.CellCountZ);

            GeneratePlayers(variables);

            var oceansAndContinents = GenerateOceansAndContinents(template, variables);

            PaintMap(oceansAndContinents, template);

            foreach (var civ in oceansAndContinents.HomelandDataForCiv.Keys)
            {
                var civHomeland = oceansAndContinents.HomelandDataForCiv[civ];

                StartingUnitPlacementLogic.PlaceStartingUnitsInRegion(
                    civHomeland.StartingRegion, civ, template
                    );
            }

            UnityEngine.Random.state = oldRandomState;

            GenerateMapCoroutine = null;

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

            Profiler.EndSample();
        }
        private OceanAndContinentData GenerateOceansAndContinents(IMapTemplate mapTemplate, IMapGenerationVariables variables)
        {
            var nonBarbarianCivs = CivFactory.AllCivilizations.Where(civ => !civ.Template.IsBarbaric).ToList();

            var partition = GridPartitionLogic.GetPartitionOfGrid(Grid, mapTemplate);

            int totalLandCells  = Mathf.RoundToInt(Grid.Cells.Count * variables.ContinentalLandPercentage * 0.01f);
            int landCellsPerCiv = Mathf.RoundToInt((float)totalLandCells / nonBarbarianCivs.Count);

            HashSet <MapSection> unassignedSections = new HashSet <MapSection>(partition.Sections);

            List <List <MapSection> > homelandChunks = SubdivisionLogic.DivideSectionsIntoChunks(
                unassignedSections, partition, nonBarbarianCivs.Count,
                landCellsPerCiv, mapTemplate.MinStartingLocationDistance,
                GetExpansionWeightFunction(partition, mapTemplate), mapTemplate
                );

            var homelandOfCivs = new Dictionary <ICivilization, HomelandData>();

            for (int i = 0; i < nonBarbarianCivs.Count; i++)
            {
                var civ = nonBarbarianCivs[i];

                var landSections  = homelandChunks[i];
                var waterSections = GetCoastForLandSection(landSections, unassignedSections, partition);

                var homelandTemplate = TemplateSelectionLogic.GetHomelandTemplateForCiv(civ, mapTemplate);

                homelandOfCivs[civ] = HomelandGenerator.GetHomelandData(
                    civ, landSections, waterSections, partition, homelandTemplate, mapTemplate
                    );
            }

            var oceanTemplate = mapTemplate.OceanTemplates.Random();

            var oceanData = OceanGenerator.GetOceanData(unassignedSections, oceanTemplate, mapTemplate, partition);

            return(new OceanAndContinentData()
            {
                HomelandDataForCiv = homelandOfCivs,
                OceanData = oceanData
            });
        }