Example #1
0
        public void UpdatePosition(Coordinates coord)
        {
            DebugUtils.Assert(ThreadDispatcher.IsMainThread);

            int NextTile = FindTile(coord, CurrentTile);

            //if (NextTile == CurrentTile)
            //	return;
            CurrentTile = NextTile;

            lock (this)
                NextPosition = coord;

            ThreadDispatcher.QueueToWorker(() => UpdatePositionWorker());

            if (tileOverlayObj == null)
            {
                tileOverlayObj     = new GameObject();
                tileOverlay        = tileOverlayObj.AddComponent <MapOverlayTiles>();
                tileOverlay.Body   = Body;
                tileOverlay.planet = this;
            }

            var tileColors = new Dictionary <int, Color32>();

            /*int city = -1;
             * if (data.ContainsKey(CurrentTile))
             *      city = data[CurrentTile].ClosestCity;*/

            foreach (var i in data)
            {
                byte r, g, b;
                //if (i.Value.Color(out r, out g, out b, city))
                if (i.Value.Color(out r, out g, out b))
                {
                    tileColors[i.Key] = new Color32(r, g, b, 255);
                }
            }

            if (data.ContainsKey(CurrentTile) && IsNodeAllowed(CurrentTile))
            {
                var knownCities = data.Where(x => x.Value.HasCity.GetValueOrDefault()).Select(x => x.Key).ToArray();

                if (knownCities.Length > 0)
                {
                    var pf   = new Pathfinding(this, knownCities, CurrentTile);
                    var path = pf.GetPath(CurrentTile).ToArray();
                    int city = data[CurrentTile].ClosestCity;

                    if (city >= 0)
                    {
                        foreach (var i in data)
                        {
                            if (IsNodeAllowed(i.Key) && data[i.Key].Level >= TileData.ComputationLevel.CITY_BORDER_FOUND && city == data[i.Key].ClosestCity && !data[i.Key].HasCity.GetValueOrDefault())
                            {
                                tileColors[i.Key] = XKCDColors.Yellow;
                            }
                        }

                        for (int i = 0; i < path.Length - 1; i++)
                        {
                            tileColors[path[i]] = XKCDColors.Orange;
                        }

                        tileColors[CurrentTile] = XKCDColors.Pink;

                        var tmp = TileData._frontier;
                        if (tmp != null)
                        {
                            foreach (var i in tmp)
                            {
                                tileColors[i] = XKCDColors.BabyPukeGreen;
                                //DebugUtils.Assert(data[i].ClosestCity != city);
                            }
                        }
                    }
                }
            }

            tileOverlay.UpdateTiles(tileColors);
        }
Example #2
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);
            }
        }
Example #3
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);
            }
        }