Пример #1
0
 public Settlement GetSettlement(int id)
 {
     //Check if we have this location id
     if (WorldLocations.TryGetValue(id, out WorldLocation loc))
     {
         //Cast to settlement, will return null if this isn't a settlement
         return(loc as Settlement);
     }
     return(null);
 }
Пример #2
0
 public void AddLocationRange(List <WorldLocation> locations)
 {
     lock (LocationAddLock)
     {
         foreach (WorldLocation wl in locations)
         {
             WorldLocations.Add(wl.LocationID, wl);
         }
     }
 }
Пример #3
0
    public void AddLocation(WorldLocation location)
    {
        int id = location.LocationID;

        lock (LocationAddLock)
        {
            WorldLocations.Add(id, location);
            if (location is Settlement)
            {
                (location as Settlement).GetKingdom().AddSettlement(location as Settlement);
            }
        }
    }
        public static void Init()
        {
            Flights  = new List <FlightInfo>();
            Airports = new List <WorldCity>();

            var fullCityList = WorldLocations.GetAll();
            var cities       = new List <WorldCity>();

            var countries = new Dictionary <string, int>();

            foreach (var city in fullCityList)
            {
                if (countries.ContainsKey(city.Country))
                {
                    if (countries[city.Country] < 2)
                    {
                        cities.Add(city);
                        countries[city.Country]++;
                    }
                }
                else
                {
                    cities.Add(city);
                    countries.Add(city.Country, 1);
                }
            }

            fullCityList.Sort(new Comparison <WorldCity>(ComparePopulation));

            int count        = cities.Count;
            int flightsCount = 0;

            double minDistance  = 200;
            double maxDistance  = 10000;
            double flightsLimit = 250;

            for (int i = 0; i < count; i++)
            {
                WorldCity origin           = cities[i];
                int       connectionsCount = 0;
                double    connectionsMax   = Math.Min(20, Math.Round(origin.Pop * 4));

                for (int j = 0; j < count; j++)
                {
                    WorldCity dest = cities[j];

                    GeoLocation originGeo = new GeoLocation()
                    {
                        Lat = origin.Lat, Lon = origin.Lon
                    };
                    GeoLocation destGeo = new GeoLocation()
                    {
                        Lat = dest.Lat, Lon = dest.Lon
                    };

                    if (origin.Name != dest.Name)
                    {
                        string route        = origin.Name + "-" + dest.Name;
                        bool   routeIsValid = !FlightsLookup.ContainsKey(route);

                        double distance        = Math.Round(WorldUtils.CalcDistance(originGeo, destGeo));
                        bool   distanceIsValid = distance > minDistance && distance < maxDistance;

                        double pass           = Math.Round(rand.NextDouble() * 200) + 150;
                        double time           = distance / 800;
                        bool   trafficIsValid = origin.Pop > 3 && dest.Pop > 1.0;

                        if (routeIsValid && distanceIsValid && trafficIsValid)
                        {
                            FlightsLookup.Add(route, route);

                            List <List <Point> > paths = WorldUtils.CalcPaths(originGeo, destGeo);
                            flightsCount++;
                            connectionsCount++;

                            string     id     = origin.Name.Substring(0, 3).ToUpper() + "-" + flightsCount;
                            FlightInfo flight = new FlightInfo()
                            {
                                ID = id, Origin = origin, Dest = dest, Time = time, Passengers = pass, Distance = distance, Points = paths
                            };
                            Flights.Add(flight);
                        }

                        if (connectionsCount > connectionsMax)
                        {
                            //Console.WriteLine("Count: " + connectionsCount + ", Origin Name: " + origin.Name);
                            break;
                        }
                        if (flightsCount > flightsLimit)
                        {
                            break;
                        }
                    }
                }
            }

            foreach (FlightInfo flight in Flights)
            {
                AddAirport(flight.Origin);
                AddAirport(flight.Dest);
            }

            Airports = AirportsLookup.Values.ToList();
            Console.WriteLine("Cities= " + cities.Count + " Airports=" + Airports.Count + " Flights=" + Flights.Count);
        }