Esempio n. 1
0
        public static Airline LoadAirline(string path)
        {
            var doc = new XmlDocument();
            doc.Load(path);
            XmlElement root = doc.DocumentElement;

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

            if (profileElement != null)
            {
                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;

                var countries = sCountries.Split(';').Select(Countries.GetCountry).ToList();

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

                var routeFocus = Route.RouteType.Passenger;

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

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

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

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

                var 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);
                }

                var license = Airline.AirlineLicense.Domestic;

                if (market == AirlineFocus.Global)
                {
                    license = mentality == Airline.AirlineMentality.Aggressive ? Airline.AirlineLicense.LongHaul : Airline.AirlineLicense.ShortHaul;
                }

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

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

                var 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"))
                    {
                        var primarypurchasing =
                            (AirlineProfile.PreferedPurchasing)
                                Enum.Parse(
                                    typeof (AirlineProfile.PreferedPurchasing),
                                    preferedsElement.Attributes["primarypurchasing"].Value);
                        airline.Profile.PrimaryPurchasing = primarypurchasing;
                    }
                }

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

                if (logosList != null)
                    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);
                        var subMentality =
                            (Airline.AirlineMentality)
                                Enum.Parse(
                                    typeof (Airline.AirlineMentality),
                                    subsidiaryElement.Attributes["mentality"].Value);
                        var subMarket =
                            (AirlineFocus)
                                Enum.Parse(typeof (AirlineFocus), subsidiaryElement.Attributes["market"].Value);

                        string subLogo = AppSettings.GetDataPath() + "\\graphics\\airlinelogos\\"
                                         + subsidiaryElement.Attributes["logo"].Value + ".png";

                        var 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));
                    }
                }

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

                    XmlNodeList routesList = startDataElement.SelectNodes("routes/route");
                    if (routesList != null)
                        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);
                            }

                            var 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);
                        var 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);
                        }

                        var 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;
            }
            return null;
        }
Esempio n. 2
0
        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);
            List<StartDataRoute> startroutes =
                startData.Routes.FindAll(
                    r =>
                    r.Opened <= GameObject.GetInstance().GameTime.Year
                    && r.Closed >= GameObject.GetInstance().GameTime.Year);

            Terminal.TerminalType terminaltype = airline.AirlineRouteFocus == Route.RouteType.Cargo ? Terminal.TerminalType.Cargo : Terminal.TerminalType.Passenger;
            //creates the routes
            List<StartDataRoute> 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.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, terminaltype))
                            {
                                AirportHelpers.RentGates(dest1, airline, AirportContract.ContractType.LowService, terminaltype);
                            }

                            if (!AirportHelpers.HasFreeGates(dest2, airline, terminaltype))
                            {
                                AirportHelpers.RentGates(dest2, airline, AirportContract.ContractType.LowService, terminaltype);
                            }

                            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, bool>? 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();
                                        var 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);
                                    }
                                }
                            }

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

                            if (airliner == null)
                            {
                                airliner = AIHelpers.GetAirlinerForRoute(
                                    airline,
                                    dest2,
                                    dest1,
                                    leaseAircraft,
                                    startRoute.RouteType,
                                    true);

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

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

                                if (route != null)
                                {
                                    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); //B747-200

                        const 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);

                        if (type == null)
                        {
                            string tAirline = airline.Profile.Name;
                            string typeNull = airliners.Type;

                            Console.WriteLine(tAirline + @" " + typeNull);
                        }
                        if (type != null && 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));

                                var 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;

                                airliner.EngineType = EngineTypes.GetStandardEngineType(airliner.Type, airliner.BuiltDate.Year);

                                Airliners.AddAirliner(airliner);

                                bool 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)
                        {
                            Terminal.TerminalType terminalType = airline.AirlineRouteFocus == Route.RouteType.Cargo ? Terminal.TerminalType.Cargo : Terminal.TerminalType.Passenger;

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

                                if (!AirportHelpers.HasFreeGates(origin, airline, terminalType))
                                {
                                    AirportHelpers.RentGates(origin, airline, AirportContract.ContractType.LowService, terminalType);
                                }

                                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, terminalType))
                                {
                                    AirportHelpers.RentGates(destination, airline, AirportContract.ContractType.LowService, terminalType);
                                }

                                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));
                                }

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

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

                                if (airliner == null
                                    && airline.Profile.PrimaryPurchasing == AirlineProfile.PreferedPurchasing.Random)
                                {
                                    airliner = AIHelpers.GetAirlinerForRoute(
                                        airline,
                                        origin,
                                        destination,
                                        true,
                                        routes.RouteType,
                                        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.ToRouteStart;
                                    AirlineHelpers.HireAirlinerPilots(fAirliner);

                                    if (route != null)
                                    {
                                        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);
                                    }
                                }
                            }
                        }
                    });
        }
 public static void AddStartData(AirlineStartData data)
 {
     StartData.Add(data);
 }