private TerritoryIDType?MoveTowardsNearestBorder(TerritoryIDType id) { var neighborDistances = new List <KeyValuePair <TerritoryIDType, int> >(); foreach (var immediateNeighbor in Map.Territories[id].ConnectedTo) { FindNearestBorderResult nearestBorder = FindNearestBorder(immediateNeighbor, new Nullable <TerritoryIDType>(id)); if (nearestBorder != null) { neighborDistances.Add(new KeyValuePair <TerritoryIDType, int>(immediateNeighbor, nearestBorder.Depth)); } } if (neighborDistances.Count == 0) { return(new Nullable <TerritoryIDType>()); } var ret = neighborDistances[0].Key; int minValue = neighborDistances[0].Value; for (int i = 1; i < neighborDistances.Count; i++) { if (neighborDistances[i].Value < minValue) { ret = neighborDistances[i].Key; minValue = neighborDistances[i].Value; } } return(new Nullable <TerritoryIDType>(ret)); }
private FindNearestBorderResult FindNearestBorder(TerritoryIDType id, TerritoryIDType?exclude) { var queue = new Queue <TerritoryIDType>(); queue.Enqueue(id); var visited = new HashSet <TerritoryIDType>(); if (exclude.HasValue) { visited.Add(exclude.Value); } int depth = 0; while (true) { TerritoryIDType?r = FindNearestBorderRecurse(queue, visited); if (r.HasValue) { FindNearestBorderResult ret = new FindNearestBorderResult(); ret.NearestBorder = r.Value; ret.Depth = depth; return(ret); } depth++; if (queue.Count == 0) { return(null); //No border } } #if CS2HX || CSSCALA throw new Exception("Never"); #endif }