Exemple #1
0
        void FindCityBorder()
        {
            HashSet<int> loadedTiles = new HashSet<int>();
            HashSet<int> frontier = new HashSet<int>();

            loadedTiles.Add(index);

            bool borderfound = false;
            Pathfinding pf = null;
            int city = -1;

            while (!borderfound)
            {
                frontier = new HashSet<int>();
                foreach (var i in loadedTiles)
                {
                    if (!planet.IsNodeAllowed(i))
                        continue;

                    foreach (var j in Planet.Tiles[i].Neighbours)
                    {
                        if (loadedTiles.Contains(j))
                            continue;

                        frontier.Add(j);
                    }
                }

                if (frontier.Count == 0)
                    break;

                Load(ComputationLevel.CITIES_PLACED, frontier.ToArray());
                foreach (var i in frontier)
                {
                    //planet.data[i].Load(ComputationLevel.CITIES_PLACED);
                    loadedTiles.Add(i);
                }

                var knownCities = loadedTiles.Where(x => planet.data[x].HasCity.GetValueOrDefault()).ToArray();

                if (knownCities.Length > 0)
                {
                    pf = new Pathfinding(planet, knownCities);
                    city = pf.GetPath(index).Last();
                    borderfound = true;
                    foreach (var i in frontier.Where(i => planet.IsNodeAllowed(i)))
                    {
                        if (pf.GetPath(i).Last() == city)
                        {
                            borderfound = false;
                            break;
                        }
                    }
                }
                else
                {
                    borderfound = false;
                }
            }

            if (pf != null)
            {
                city = pf.GetPath(index).Last();
                foreach (var i in loadedTiles.Where(i => planet.IsNodeAllowed(i)))
                {
                    if (city == pf.GetPath(i).Last())
                    {
                        planet.data[i].ClosestCity = city;
                        planet.data[i].Level = ComputationLevel.CITY_BORDER_FOUND;
                    }
                }
            }

            _frontier = frontier;

            Level = ComputationLevel.CITY_BORDER_FOUND;

            foreach (var i in frontier)
            {
                DebugUtils.Assert(planet.data[i].ClosestCity != city);
            }
        }