//adds start data to the list
 public static void AddStartData(AirlineStartData data)
 {
     startData.Add(data);
 }
示例#2
0
        /*loads an airline and returns the object
         */
        public static Airline LoadAirline(string path)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlElement root = doc.DocumentElement;

            XmlElement profileElement = (XmlElement)root.SelectSingleNode("profile");

            string name = profileElement.Attributes["name"].Value;
            string iata = profileElement.Attributes["iata"].Value;
            string color = profileElement.Attributes["color"].Value;

            string logoImage = profileElement.HasAttribute("logo") ? profileElement.Attributes["logo"].Value : "";

            string sCountries = profileElement.Attributes["country"].Value;

            List<Country> countries = new List<Country>();

            foreach (string sCountry in sCountries.Split(';'))
                countries.Add(Countries.GetCountry(sCountry));

            //Country country = Countries.GetCountry(profileElement.Attributes["country"].Value);
            string ceo = profileElement.Attributes["CEO"].Value;
            Airline.AirlineMentality mentality = (Airline.AirlineMentality)Enum.Parse(typeof(Airline.AirlineMentality), profileElement.Attributes["mentality"].Value);
            Airline.AirlineFocus market = (Airline.AirlineFocus)Enum.Parse(typeof(Airline.AirlineFocus), profileElement.Attributes["market"].Value);

            Route.RouteType routeFocus = Route.RouteType.Passenger;

            if (profileElement.HasAttribute("routefocus"))
                routeFocus = (Route.RouteType)Enum.Parse(typeof(Route.RouteType), profileElement.Attributes["routefocus"].Value);

            XmlElement narrativeElement = (XmlElement)profileElement.SelectSingleNode("narrative");

            string narrative = "";
            if (narrativeElement != null)
                narrative = narrativeElement.Attributes["narrative"].Value;

            Boolean isReal = true;
            int founded = 1950;
            int folded = 2199;

            XmlElement infoElement = (XmlElement)root.SelectSingleNode("info");
            if (infoElement != null)
            {
                isReal = Convert.ToBoolean(infoElement.Attributes["real"].Value);
                founded = Convert.ToInt16(infoElement.Attributes["from"].Value);
                folded = Convert.ToInt16(infoElement.Attributes["to"].Value);

            }

            Airline.AirlineLicense license = Airline.AirlineLicense.Domestic;

            if (market == Airline.AirlineFocus.Global)
                if (mentality == Airline.AirlineMentality.Aggressive)
                    license = Airline.AirlineLicense.Long_Haul;
                else
                    license = Airline.AirlineLicense.Short_Haul;

            if (market == Airline.AirlineFocus.Regional)
                license = Airline.AirlineLicense.Regional;

            Airline airline = new Airline(new AirlineProfile(name, iata, color, ceo, isReal, founded, folded), mentality, market, license, routeFocus);
            airline.Profile.Countries = countries;
            airline.Profile.Country = airline.Profile.Countries[0];
            airline.Profile.LogoName = logoImage;

            XmlElement preferedsElement = (XmlElement)root.SelectSingleNode("prefereds");

            if (preferedsElement != null)
            {
                if (preferedsElement.HasAttribute("aircrafts"))
                {
                    string[] preferedAircrafts = preferedsElement.Attributes["aircrafts"].Value.Split(',');

                    foreach (string preferedAircraft in preferedAircrafts)
                    {
                        AirlinerType pAircraft = AirlinerTypes.GetType(preferedAircraft);
                        airline.Profile.addPreferedAircraft(pAircraft);
                    }
                }
                if (preferedsElement.HasAttribute("primarypurchasing"))
                {
                    AirlineProfile.PreferedPurchasing primarypurchasing = (AirlineProfile.PreferedPurchasing)Enum.Parse(typeof(AirlineProfile.PreferedPurchasing), preferedsElement.Attributes["primarypurchasing"].Value);
                    airline.Profile.PrimaryPurchasing = primarypurchasing;
                }
            }

            XmlNodeList logosList = profileElement.SelectNodes("logos/logo");

            foreach (XmlElement logoElement in logosList)
            {
                int logoFromYear = Convert.ToInt16(logoElement.Attributes["from"].Value);
                int logoToYear = Convert.ToInt16(logoElement.Attributes["to"].Value);
                string logoPath = AppSettings.getDataPath() + "\\graphics\\airlinelogos\\multilogos\\" + logoElement.Attributes["path"].Value + ".png";

                airline.Profile.addLogo(new AirlineLogo(logoFromYear, logoToYear, logoPath));
            }

            if (profileElement.HasAttribute("preferedairport"))
            {
                Airport preferedAirport = Airports.GetAirport(profileElement.Attributes["preferedairport"].Value);
                airline.Profile.PreferedAirport = preferedAirport;
            }

            XmlNodeList subsidiariesList = root.SelectNodes("subsidiaries/subsidiary");
            if (subsidiariesList != null)
            {
                foreach (XmlElement subsidiaryElement in subsidiariesList)
                {
                    string subName = subsidiaryElement.Attributes["name"].Value;
                    string subIATA = subsidiaryElement.Attributes["IATA"].Value;
                    Airport subAirport = Airports.GetAirport(subsidiaryElement.Attributes["homebase"].Value);
                    Airline.AirlineMentality subMentality = (Airline.AirlineMentality)Enum.Parse(typeof(Airline.AirlineMentality), subsidiaryElement.Attributes["mentality"].Value);
                    Airline.AirlineFocus subMarket = (Airline.AirlineFocus)Enum.Parse(typeof(Airline.AirlineFocus), subsidiaryElement.Attributes["market"].Value);
                    string subLogo = AppSettings.getDataPath() + "\\graphics\\airlinelogos\\" + subsidiaryElement.Attributes["logo"].Value + ".png";

                    Route.RouteType airlineRouteFocus = Route.RouteType.Passenger;

                    if (subsidiaryElement.HasAttribute("routefocus"))
                        airlineRouteFocus = (Route.RouteType)Enum.Parse(typeof(Route.RouteType), subsidiaryElement.Attributes["routefocus"].Value);

                    airline.FutureAirlines.Add(new FutureSubsidiaryAirline(subName, subIATA, subAirport, subMentality, subMarket, airlineRouteFocus, subLogo));
                }
            }

            XmlElement startDataElement = (XmlElement)root.SelectSingleNode("startdata");
            if (startDataElement != null)
            {
                AirlineStartData startData = new AirlineStartData(airline);

                XmlNodeList routesList = startDataElement.SelectNodes("routes/route");
                foreach (XmlElement routeElement in routesList)
                {
                    string dest1 = routeElement.Attributes["destination1"].Value;
                    string dest2 = routeElement.Attributes["destination2"].Value;
                    int opened = Convert.ToInt16(routeElement.Attributes["opened"].Value);
                    int closed = Convert.ToInt16(routeElement.Attributes["closed"].Value);

                    Route.RouteType routetype = airline.AirlineRouteFocus;

                    if (routeElement.HasAttribute("routetype"))
                        routetype = (Route.RouteType)Enum.Parse(typeof(Route.RouteType), routeElement.Attributes["routetype"].Value);

                    StartDataRoute sdr = new StartDataRoute(dest1, dest2, opened, closed, routetype);
                    startData.addRoute(sdr);

                    if (routeElement.HasAttribute("airliner"))
                    {
                        AirlinerType airlinerType = AirlinerTypes.GetType(routeElement.Attributes["airliner"].Value);
                        sdr.Type = airlinerType;
                    }

                }

                XmlNodeList airlinersList = startDataElement.SelectNodes("airliners/airliner");

                foreach (XmlElement airlinerElement in airlinersList)
                {
                    string type = airlinerElement.Attributes["type"].Value;
                    int early = Convert.ToInt16(airlinerElement.Attributes["early"].Value);
                    int late = Convert.ToInt16(airlinerElement.Attributes["late"].Value);

                    startData.addAirliners(new StartDataAirliners(type, early, late));
                }

                XmlNodeList randomRoutesList = startDataElement.SelectNodes("random_routes/routes");

                foreach (XmlElement routeElement in randomRoutesList)
                {
                    string origin = routeElement.Attributes["origin"].Value;
                    int destinations = Convert.ToInt16(routeElement.Attributes["destinations"].Value);
                    GeneralHelpers.Size minimumsize = (GeneralHelpers.Size)Enum.Parse(typeof(GeneralHelpers.Size), routeElement.Attributes["minimumsize"].Value);

                    Route.RouteType routetype = airline.AirlineRouteFocus;

                    if (routeElement.HasAttribute("routetype"))
                        routetype = (Route.RouteType)Enum.Parse(typeof(Route.RouteType), routeElement.Attributes["routetype"].Value);

                    StartDataRoutes routes = new StartDataRoutes(origin, destinations, minimumsize, routetype);

                    XmlNodeList countriesList = routeElement.SelectNodes("countries/country");

                    foreach (XmlElement countryElement in countriesList)
                    {
                        Country routesCountry = Countries.GetCountry(countryElement.Attributes["value"].Value);
                        routes.addCountry(routesCountry);
                    }
                    startData.addOriginRoutes(routes);
                }

                AirlineStartDatas.AddStartData(startData);
            }

            airline.Profile.Narrative = narrative;

            Airlines.AddAirline(airline);

            return airline;
        }
示例#3
0
        /*!creates the start data for an airline
         */
        private static void CreateAirlineStartData(Airline airline, AirlineStartData startData)
        {
            AirportFacility checkinFacility = AirportFacilities.GetFacilities(AirportFacility.FacilityType.CheckIn).Find(f => f.TypeLevel == 1);
            AirportFacility cargoTerminal = AirportFacilities.GetFacilities(AirportFacility.FacilityType.Cargo).Find(f => f.TypeLevel > 0);

            //  int difficultyFactor = GameObject.GetInstance().Difficulty.AILevel > 1 ? 2 : 1; //level easy

            int startDataFactor = Convert.ToInt16(GameObject.GetInstance().Difficulty.StartDataLevel);
            var startroutes = startData.Routes.FindAll(r => r.Opened <= GameObject.GetInstance().GameTime.Year && r.Closed >= GameObject.GetInstance().GameTime.Year);

            //creates the routes
            var sRoutes = startroutes.GetRange(0, startroutes.Count / startDataFactor);
            Parallel.ForEach(sRoutes, startRoute =>
            {
                Airport dest1 = Airports.GetAirport(startRoute.Destination1);
                Airport dest2 = Airports.GetAirport(startRoute.Destination2);

                if (dest1 != null && dest2 != null)
                {
                    //if (dest1.getAirportFacility(airline, AirportFacility.FacilityType.CheckIn).TypeLevel == 0)
                    //dest1.addAirportFacility(airline, checkinFacility, GameObject.GetInstance().GameTime);

                    //if (dest2.getAirportFacility(airline, AirportFacility.FacilityType.CheckIn).TypeLevel == 0)
                    //dest2.addAirportFacility(airline, checkinFacility, GameObject.GetInstance().GameTime);

                    if (dest1.getAirportFacility(airline, AirportFacility.FacilityType.Cargo).TypeLevel == 0 && dest1.getAirportFacility(null, AirportFacility.FacilityType.Cargo).TypeLevel == 0 && airline.AirlineRouteFocus == Route.RouteType.Cargo)
                        dest1.addAirportFacility(airline, cargoTerminal, GameObject.GetInstance().GameTime);

                    if (dest2.getAirportFacility(airline, AirportFacility.FacilityType.Cargo).TypeLevel == 0 && dest2.getAirportFacility(null, AirportFacility.FacilityType.Cargo).TypeLevel == 0 && airline.AirlineRouteFocus == Route.RouteType.Cargo)
                        dest2.addAirportFacility(airline, cargoTerminal, GameObject.GetInstance().GameTime);

                    if (!AirportHelpers.HasFreeGates(dest1, airline))
                        AirportHelpers.RentGates(dest1, airline, AirportContract.ContractType.Low_Service);

                    if (!AirportHelpers.HasFreeGates(dest2, airline))
                        AirportHelpers.RentGates(dest2, airline, AirportContract.ContractType.Low_Service);

                    Guid id = Guid.NewGuid();

                    Route route = null;

                    double price = PassengerHelpers.GetPassengerPrice(dest1, dest2);

                    if (startRoute.RouteType == Route.RouteType.Mixed || startRoute.RouteType == Route.RouteType.Passenger)
                    {
                        route = new PassengerRoute(id.ToString(), dest1, dest2, GameObject.GetInstance().GameTime, price);
                    }

                    if (startRoute.RouteType == Route.RouteType.Cargo)
                        route = new CargoRoute(id.ToString(), dest1, dest2, GameObject.GetInstance().GameTime, PassengerHelpers.GetCargoPrice(dest1, dest2));

                    KeyValuePair<Airliner, Boolean>? airliner = null;
                    if (startRoute.Type != null)
                    {
                        double distance = MathHelpers.GetDistance(dest1, dest2);

                        if (startRoute.Type.Range > distance)
                        {
                            airliner = new KeyValuePair<Airliner, bool>(Airliners.GetAirlinersForSale(a => a.Type == startRoute.Type).FirstOrDefault(), true);

                            if (airliner.Value.Key == null)
                            {
                                id = Guid.NewGuid();
                                Airliner nAirliner = new Airliner(id.ToString(), startRoute.Type, airline.Profile.Country.TailNumbers.getNextTailNumber(), GameObject.GetInstance().GameTime);
                                Airliners.AddAirliner(nAirliner);

                                nAirliner.clearAirlinerClasses();

                                AirlinerHelpers.CreateAirlinerClasses(nAirliner);

                                airliner = new KeyValuePair<Airliner, bool>(nAirliner, true);
                            }
                        }

                    }

                    Boolean leaseAircraft = airline.Profile.PrimaryPurchasing == AirlineProfile.PreferedPurchasing.Leasing;

                    if (airliner == null)
                    {

                        airliner = AIHelpers.GetAirlinerForRoute(airline, dest2, dest1, leaseAircraft, startRoute.RouteType == Route.RouteType.Cargo, true);

                        if (airliner == null && airline.Profile.PrimaryPurchasing == AirlineProfile.PreferedPurchasing.Random)
                            AIHelpers.GetAirlinerForRoute(airline, dest2, dest1, true, startRoute.RouteType == Route.RouteType.Cargo, true);

                    }

                    if (airliner != null)
                    {
                        FleetAirliner fAirliner = AirlineHelpers.AddAirliner(airline, airliner.Value.Key, airline.Airports[0],leaseAircraft);
                        fAirliner.addRoute(route);
                        fAirliner.Status = FleetAirliner.AirlinerStatus.To_route_start;
                        AirlineHelpers.HireAirlinerPilots(fAirliner);

                        route.LastUpdated = GameObject.GetInstance().GameTime;

                        if (startRoute.RouteType == Route.RouteType.Mixed || startRoute.RouteType == Route.RouteType.Passenger)
                        {
                            AirlinerHelpers.CreateAirlinerClasses(fAirliner.Airliner);

                            RouteClassesConfiguration configuration = AIHelpers.GetRouteConfiguration((PassengerRoute)route);

                            foreach (RouteClassConfiguration classConfiguration in configuration.getClasses())
                            {
                                ((PassengerRoute)route).getRouteAirlinerClass(classConfiguration.Type).FarePrice = price * GeneralHelpers.ClassToPriceFactor(classConfiguration.Type);

                                foreach (RouteFacility rFacility in classConfiguration.getFacilities())
                                    ((PassengerRoute)route).getRouteAirlinerClass(classConfiguration.Type).addFacility(rFacility);
                            }

                            AIHelpers.CreateRouteTimeTable(route, fAirliner);
                        }
                        if (startRoute.RouteType == Route.RouteType.Cargo)
                        {
                            AIHelpers.CreateCargoRouteTimeTable(route, fAirliner);
                        }
                    }
                    airline.addRoute(route);
                }

            });

            //adds the airliners
            Parallel.ForEach(startData.Airliners, airliners =>
            {
                AirlinerType type = AirlinerTypes.GetType(airliners.Type);

                int totalSpan = 2010 - 1960;
                int yearSpan = GameObject.GetInstance().GameTime.Year - 1960;
                double valueSpan = Convert.ToDouble(airliners.AirlinersLate - airliners.AirlinersEarly);
                double span = valueSpan / Convert.ToDouble(totalSpan);

                int numbers = Math.Max(1, Convert.ToInt16(span * yearSpan) / startDataFactor);
                //type==null
                if (type.Produced.From <= GameObject.GetInstance().GameTime)
                {
                    for (int i = 0; i < Math.Max(numbers, airliners.AirlinersEarly); i++)
                    {
                        Guid id = Guid.NewGuid();

                        int countryNumber = rnd.Next(Countries.GetCountries().Count() - 1);
                        Country country = Countries.GetCountries()[countryNumber];

                        int builtYear = rnd.Next(type.Produced.From.Year, Math.Min(GameObject.GetInstance().GameTime.Year - 1, type.Produced.To.Year));

                        Airliner airliner = new Airliner(id.ToString(), type, country.TailNumbers.getNextTailNumber(), new DateTime(builtYear, 1, 1));

                        int age = MathHelpers.CalculateAge(airliner.BuiltDate, GameObject.GetInstance().GameTime);

                        long kmPerYear = rnd.Next(100000, 1000000);
                        long km = kmPerYear * age;

                        airliner.Flown = km;

                        Airliners.AddAirliner(airliner);

                        Boolean leaseAircraft = airline.Profile.PrimaryPurchasing == AirlineProfile.PreferedPurchasing.Leasing;

                        FleetAirliner fAirliner = AirlineHelpers.AddAirliner(airline, airliner, airline.Airports[0],leaseAircraft);
                        fAirliner.Status = FleetAirliner.AirlinerStatus.Stopped;
                        AirlineHelpers.HireAirlinerPilots(fAirliner);

                        AirlinerHelpers.CreateAirlinerClasses(fAirliner.Airliner);

                    }
                }

            });

            //the origin routes
            Parallel.ForEach(startData.OriginRoutes, routes =>
            {
                Airport origin = Airports.GetAirport(routes.Origin);

                if (origin != null)
                {
                    for (int i = 0; i < Math.Min(routes.Destinations / startDataFactor, origin.Terminals.getFreeGates()); i++)
                    {
                        //if (origin.getAirportFacility(airline, AirportFacility.FacilityType.CheckIn).TypeLevel == 0)
                        //origin.addAirportFacility(airline, checkinFacility, GameObject.GetInstance().GameTime);

                        if (!AirportHelpers.HasFreeGates(origin, airline))
                            AirportHelpers.RentGates(origin, airline, AirportContract.ContractType.Low_Service);

                        Airport destination = GetStartDataRoutesDestination(routes);

                        //if (destination.getAirportFacility(airline, AirportFacility.FacilityType.CheckIn).TypeLevel == 0)
                        //destination.addAirportFacility(airline, checkinFacility, GameObject.GetInstance().GameTime);

                        if (!AirportHelpers.HasFreeGates(destination, airline))
                            AirportHelpers.RentGates(destination, airline, AirportContract.ContractType.Low_Service);

                        Guid id = Guid.NewGuid();

                        Route route = null;

                        double price = PassengerHelpers.GetPassengerPrice(origin, destination);

                        if (routes.RouteType == Route.RouteType.Mixed || routes.RouteType == Route.RouteType.Passenger)
                            route = new PassengerRoute(id.ToString(), origin, destination, GameObject.GetInstance().GameTime, price);

                        if (routes.RouteType == Route.RouteType.Cargo)
                            route = new CargoRoute(id.ToString(), origin, destination, GameObject.GetInstance().GameTime, PassengerHelpers.GetCargoPrice(origin, destination));

                        Boolean leaseAircraft = airline.Profile.PrimaryPurchasing == AirlineProfile.PreferedPurchasing.Leasing;

                        KeyValuePair<Airliner, Boolean>? airliner = AIHelpers.GetAirlinerForRoute(airline, origin, destination, leaseAircraft, routes.RouteType == Route.RouteType.Cargo, true);

                        if (airliner == null && airline.Profile.PrimaryPurchasing == AirlineProfile.PreferedPurchasing.Random)
                            airliner = AIHelpers.GetAirlinerForRoute(airline, origin, destination, true, routes.RouteType == Route.RouteType.Cargo, true);

                        double distance = MathHelpers.GetDistance(origin, destination);

                        if (airliner != null)
                        {
                            FleetAirliner fAirliner = AirlineHelpers.AddAirliner(airline, airliner.Value.Key, airline.Airports[0], leaseAircraft);
                            fAirliner.addRoute(route);
                            fAirliner.Status = FleetAirliner.AirlinerStatus.To_route_start;
                            AirlineHelpers.HireAirlinerPilots(fAirliner);

                            route.LastUpdated = GameObject.GetInstance().GameTime;

                            if (routes.RouteType == Route.RouteType.Passenger || routes.RouteType == Route.RouteType.Mixed)
                            {
                                AirlinerHelpers.CreateAirlinerClasses(fAirliner.Airliner);

                                RouteClassesConfiguration configuration = AIHelpers.GetRouteConfiguration((PassengerRoute)route);

                                foreach (RouteClassConfiguration classConfiguration in configuration.getClasses())
                                {
                                    ((PassengerRoute)route).getRouteAirlinerClass(classConfiguration.Type).FarePrice = price * GeneralHelpers.ClassToPriceFactor(classConfiguration.Type);

                                    foreach (RouteFacility rFacility in classConfiguration.getFacilities())
                                        ((PassengerRoute)route).getRouteAirlinerClass(classConfiguration.Type).addFacility(rFacility);
                                }

                                AIHelpers.CreateRouteTimeTable(route, fAirliner);
                            }
                            if (routes.RouteType == Route.RouteType.Cargo)
                                AIHelpers.CreateCargoRouteTimeTable(route, fAirliner);

                            airline.addRoute(route);

                        }
                    }
                }
            });
        }
 //adds start data to the list
 public static void AddStartData(AirlineStartData data)
 {
     startData.Add(data);
 }