Created
June 16, 2019 00:30
-
-
Save dshook/f3572d9c394691cc8abfb6cc967961ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Find each cities area of influence that flood fills out from each city to a max distance based on the city pop | |
public Dictionary<HexCell, HexCity> FindCityInfluencedTiles(List<HexCity> cities) { | |
var frontier = new List<HexCell>(); | |
var costSoFar = new Dictionary<HexCell, int>(); | |
var seed = new Dictionary<HexCell, HexCity>(); | |
//Set the distance to 0 at all start poitns and each start point its own seed | |
foreach(var city in cities){ | |
frontier.Add(city.Cell); | |
costSoFar[city.Cell] = 0; | |
seed[city.Cell] = city; | |
} | |
while (frontier.Count > 0) { | |
var current = frontier[0]; | |
frontier.RemoveAt(0); | |
var maxDistance = seed[current].AreaOfInfluence; | |
for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { | |
HexCell next = current.GetNeighbor(d); | |
if(!costSoFar.ContainsKey(next) && costSoFar[current] < maxDistance){ | |
costSoFar[next] = costSoFar[current] + 1; | |
seed[next] = seed[current]; | |
frontier.Add(next); | |
} | |
} | |
} | |
return seed; | |
} | |
void UpdateCityInfluencedTiles(){ | |
var cityInfluencedTiles = grid.FindCityInfluencedTiles(cityList); | |
var grouped = cityInfluencedTiles.GroupBy(x => x.Value); | |
foreach(var groupKey in grouped){ | |
groupKey.Key.influencedCells = groupKey.Select(x => x.Key).ToList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment