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; } } }
public bool TryIncreaseScore( MapRegion region, RegionData regionData, out float scoreAdded ) { var candidateResources = new List <IResourceDefinition>(ScoreIncreasingCandidates); while (candidateResources.Any()) { var chosenResource = ResourceRandomSampler.SampleElementsFromSet( candidateResources, 1, GetResourceSelectionWeightFunction(region, regionData) ).FirstOrDefault(); if (chosenResource == null) { break; } var cell = CellRandomSampler.SampleElementsFromSet( region.Cells, 1, GetCellPlacementWeightFunction(chosenResource) ).FirstOrDefault(); if (cell != null) { float oldScore = CellScorer.GetScoreOfCell(cell); int copies = chosenResource.Type == ResourceType.Strategic ? StrategicCopiesLogic.GetWeightedRandomCopies() : 0; ResourceNodeFactory.BuildNode(cell, chosenResource, copies); scoreAdded = CellScorer.GetScoreOfCell(cell) - oldScore; return(true); } else { candidateResources.Remove(chosenResource); } } scoreAdded = 0f; return(false); }
public bool TryIncreaseScore(MapRegion region, RegionData regionData, out float scoreAdded) { var addJungleCandidates = region.Cells.Where(IncreaseScoreFilter); if (addJungleCandidates.Any()) { var newJungle = addJungleCandidates.Random(); var oldScore = CellScorer.GetScoreOfCell(newJungle); ModLogic.ChangeVegetationOfCell(newJungle, CellVegetation.Jungle); scoreAdded = CellScorer.GetScoreOfCell(newJungle) - oldScore; return(true); } else { scoreAdded = 0f; return(false); } }
public bool TryDecreaseScore(MapRegion region, RegionData regionData, out float scoreRemoved) { var allCandidates = region.LandCells.Where(CandidateFilter).ToList(); allCandidates.Sort(CandidateComparer); var bestCandidate = allCandidates.FirstOrDefault(); if (bestCandidate != null) { var oldScore = CellScorer.GetScoreOfCell(bestCandidate); ModLogic.ChangeShapeOfCell(bestCandidate, CellShape.Mountains); scoreRemoved = oldScore - CellScorer.GetScoreOfCell(bestCandidate); return(true); } else { scoreRemoved = 0f; return(false); } }
private Comparison <IHexCell> GetScoreComparisonFunction(MapRegion region) { var cellScores = new Dictionary <IHexCell, float>(); var cellsInRange = new Dictionary <IHexCell, IHexCell[]>(); foreach (var cell in region.Cells) { cellScores[cell] = CellScorer.GetScoreOfCell(cell); cellsInRange[cell] = Grid.GetCellsInRadius(cell, CityConfig.MaxBorderRange) .Where(nearby => region.Cells.Contains(nearby)) .ToArray(); } var cityPlacementScore = new Dictionary <IHexCell, float>(); return(delegate(IHexCell cellOne, IHexCell cellTwo) { float cellOneScore, cellTwoScore; if (!cityPlacementScore.TryGetValue(cellOne, out cellOneScore)) { cellOneScore = cellsInRange[cellOne].Select(neighbor => cellScores[neighbor]).Sum(); cityPlacementScore[cellOne] = cellOneScore; } if (!cityPlacementScore.TryGetValue(cellTwo, out cellTwoScore)) { cellTwoScore = cellsInRange[cellTwo].Select(neighbor => cellScores[neighbor]).Sum(); cityPlacementScore[cellTwo] = cellTwoScore; } return cellTwoScore.CompareTo(cellOneScore); }); }