Ejemplo n.º 1
0
        public void GenerateTopologyAndEcology(HomelandData homelandData, IMapTemplate mapTemplate)
        {
            var regions = homelandData.OtherRegions.ToList();

            regions.Add(homelandData.StartingRegion);

            int riveredCells = 0;

            foreach (var region in regions)
            {
                var regionData = homelandData.GetDataOfRegion(region);

                RegionGenerator.GenerateTopology(region, regionData.Topology);
                RegionGenerator.PaintTerrain(region, regionData.Biome);

                riveredCells += Mathf.CeilToInt(region.LandCells.Count * regionData.Biome.RiverPercentage * 0.01f);
            }

            var allLandCells  = regions.SelectMany(region => region.LandCells);
            var allWaterCells = regions.SelectMany(region => region.WaterCells);

            RiverGenerator.CreateRivers(allLandCells, allWaterCells, riveredCells);

            RegionGenerator.AssignFloodPlains(allLandCells);

            foreach (var region in regions)
            {
                var regionData = homelandData.GetDataOfRegion(region);

                VegetationPainter.PaintVegetation(region, regionData.Biome);
            }
        }
Ejemplo n.º 2
0
        private void KeepScoreWithinBounds(HomelandData homelandData, float minScore, float maxScore)
        {
            var currentScore = homelandData.Cells.Select(cell => CellScorer.GetScoreOfCell(cell)).Sum();

            float scoreChange;

            var strategyWeightsByRegion = new Dictionary <MapRegion, Dictionary <IBalanceStrategy, int> >();

            foreach (var region in homelandData.AllRegions)
            {
                strategyWeightsByRegion[region] = homelandData.GetDataOfRegion(region).GetBalanceStrategyWeights();
            }

            int iterations = homelandData.AllRegions.Count() * 50;

            var regions = homelandData.AllRegions.ToList();

            while ((currentScore < minScore || currentScore > maxScore) && iterations-- > 0)
            {
                if (regions.Count == 0)
                {
                    regions.AddRange(homelandData.AllRegions);
                }

                var region = regions.Random();
                regions.Remove(region);

                var regionData = homelandData.GetDataOfRegion(region);

                var strategyToAttempt = GetStrategy(strategyWeightsByRegion[region]);

                if (strategyToAttempt == null)
                {
                    break;
                }

                if (currentScore < minScore && strategyToAttempt.TryIncreaseScore(region, regionData, out scoreChange))
                {
                    currentScore += scoreChange;
                }

                if (currentScore > maxScore && strategyToAttempt.TryDecreaseScore(region, regionData, out scoreChange))
                {
                    currentScore -= scoreChange;
                }
            }
        }
Ejemplo n.º 3
0
        private void BringYieldTypeToMin(
            YieldType type, float minYield, HomelandData homelandData,
            ref YieldSummary currentYield
            )
        {
            float yieldDeficit = minYield - currentYield[type];

            var strategyWeightsByRegion = new Dictionary <MapRegion, Dictionary <IBalanceStrategy, int> >();

            foreach (var region in homelandData.AllRegions)
            {
                strategyWeightsByRegion[region] = homelandData.GetDataOfRegion(region).GetBalanceStrategyWeights();
            }

            int iterations = homelandData.AllRegions.Count() * 10;

            List <MapRegion> regions = homelandData.AllRegions.ToList();

            while (yieldDeficit > 0 && iterations-- > 0)
            {
                if (regions.Count == 0)
                {
                    regions = homelandData.AllRegions.ToList();
                }

                var region = regions.Random();
                regions.Remove(region);

                var strategyWeights = strategyWeightsByRegion[region];

                var strategy = GetStrategy(strategyWeights);

                YieldSummary yieldAdded;
                if (strategy.TryIncreaseYield(region, homelandData.GetDataOfRegion(region), type, out yieldAdded))
                {
                    yieldDeficit -= yieldAdded[type];
                    currentYield += yieldAdded;
                }
            }

            if (currentYield[type] < minYield)
            {
                Debug.LogWarningFormat("Failed to bring yield type {0} to min yield {1}", type, minYield);
            }
        }
Ejemplo n.º 4
0
        public void DistributeStrategicsAcrossHomeland(HomelandData homelandData)
        {
            int nodesLeft  = Mathf.CeilToInt(homelandData.YieldAndResources.StrategicNodesPerCell * homelandData.Cells.Count());
            int copiesLeft = Mathf.CeilToInt(homelandData.YieldAndResources.StrategicCopiesPerCell * homelandData.Cells.Count());

            var validStrategics = new List <IResourceDefinition>(StrategicResources);

            var resourceWeightsByRegion = new Dictionary <MapRegion, Dictionary <IResourceDefinition, int> >();

            foreach (var region in homelandData.AllRegions)
            {
                resourceWeightsByRegion[region] = homelandData.GetDataOfRegion(region).GetResourceWeights();
            }

            var regions    = homelandData.AllRegions.ToList();
            int iterations = regions.Count * 10;

            while (nodesLeft > 0 && copiesLeft > 0 && iterations-- > 0)
            {
                if (regions.Count == 0)
                {
                    regions.AddRange(homelandData.AllRegions);
                }

                var region = regions.Random();
                regions.Remove(region);

                var resourceWeights = resourceWeightsByRegion[region];

                var strategic = ResourceRandomSampler.SampleElementsFromSet(
                    validStrategics, 1,
                    resource => resourceWeights.ContainsKey(resource) ? resourceWeights[resource] : 0
                    ).FirstOrDefault();

                if (strategic == null)
                {
                    continue;
                }

                var location = CellRandomSampler.SampleElementsFromSet(
                    region.Cells, 1, cell => ResourceRestrictionCanon.GetPlacementWeightOnCell(strategic, cell)
                    ).FirstOrDefault();

                if (location != null && NodeFactory.CanBuildNode(location, strategic))
                {
                    int copies = StrategicCopiesLogic.GetWeightedRandomCopies();

                    copies = Math.Min(copies, copiesLeft);

                    NodeFactory.BuildNode(location, strategic, copies);

                    nodesLeft--;
                    copiesLeft -= copies;
                }
            }
        }