Skip to content

Instantly share code, notes, and snippets.

@dshook
Created June 16, 2019 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dshook/f3572d9c394691cc8abfb6cc967961ed to your computer and use it in GitHub Desktop.
Save dshook/f3572d9c394691cc8abfb6cc967961ed to your computer and use it in GitHub Desktop.
//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