Example #1
0
        public static string GetNamedExpressId(SpecialTrain special)
        {
            string idStr = FindUnusedID(special.IDAbbrev);

            if (idStr != null)
            {
                //IdGenerator.Instance.RegisterJobId(idStr);
                return(idStr);
            }
            else
            {
                PassengerJobs.ModEntry.Logger.Warning($"Couldn't find free jobId for express special: {special.IDAbbrev}! Using 0 for jobId number!");
                return($"{special.IDAbbrev}-{0:D2}");
            }
        }
Example #2
0
        private static StaticPassengerJobDefinition CreateSavedPassengerJob(GameObject jobChainGO, PassengerJobDefinitionData jobData)
        {
            // associated station
            if (!(GetStationWithId(jobData.stationId) is Station logicStation))
            {
                PrintError($"Couldn't find corresponding Station with ID: {jobData.stationId}! Skipping load of this job chain!");
                return(null);
            }

            // bonus time limit, base payment
            if (jobData.timeLimitForJob < 0f || jobData.initialWage < 0f ||
                string.IsNullOrEmpty(jobData.originStationId) || string.IsNullOrEmpty(jobData.destinationStationId))
            {
                PrintError("Invalid data! Skipping load of this job chain!");
                return(null);
            }

            // license requirements
            if (!LicenseManager.IsValidForParsingToJobLicense(jobData.requiredLicenses))
            {
                PrintError("Undefined job licenses requirement! Skipping load of this job chain!");
                return(null);
            }

            // starting track
            if (!(GetYardTrackWithId(jobData.startingTrack) is Track startTrack))
            {
                PrintError($"Couldn't find corresponding start Track with ID: {jobData.startingTrack}! Skipping load of this job chain!");
                return(null);
            }

            // destination track
            if (!(GetYardTrackWithId(jobData.destinationTrack) is Track destTrack))
            {
                PrintError($"Couldn't find corresponding destination Track with ID: {jobData.destinationTrack}! Skipping load of this job chain!");
                return(null);
            }

            // consist
            if (!(GetCarsFromCarGuids(jobData.trainCarGuids) is List <Car> consist))
            {
                PrintError("Couldn't find all carsToTransport with transportCarGuids! Skipping load of this job chain!");
                return(null);
            }

            // loading platform
            WarehouseMachine loadMachine = null;

            if (!string.IsNullOrEmpty(jobData.loadingTrackId))
            {
                if (PlatformManager.GetPlatformByTrackId(jobData.loadingTrackId) is PlatformDefinition pd)
                {
                    loadMachine = pd.Controller.LogicMachine;
                }
            }

            // unloading platform
            WarehouseMachine unloadMachine = null;

            if (!string.IsNullOrEmpty(jobData.unloadingTrackId))
            {
                if (PlatformManager.GetPlatformByTrackId(jobData.unloadingTrackId) is PlatformDefinition pd)
                {
                    unloadMachine = pd.Controller.LogicMachine;
                }
            }

            // Named train
            SpecialTrain special = null;

            if (!string.IsNullOrWhiteSpace(jobData.specialName))
            {
                special = SpecialConsistManager.TrainDefinitions.Find(train => string.Equals(train.Name, jobData.specialName));
            }

            StaticPassengerJobDefinition jobDefinition = jobChainGO.AddComponent <StaticPassengerJobDefinition>();
            var chainData = new StationsChainData(jobData.originStationId, jobData.destinationStationId);

            jobDefinition.PopulateBaseJobDefinition(logicStation, jobData.timeLimitForJob, jobData.initialWage, chainData, (JobLicenses)jobData.requiredLicenses);

            jobDefinition.subType              = (JobType)jobData.subType;
            jobDefinition.startingTrack        = startTrack;
            jobDefinition.destinationTrack     = destTrack;
            jobDefinition.trainCarsToTransport = consist;
            jobDefinition.loadMachine          = loadMachine;
            jobDefinition.unloadMachine        = unloadMachine;
            jobDefinition.specialDefinition    = special;

            return(jobDefinition);
        }
Example #3
0
        public PassengerTransportChainController GenerateNewTransportJob(TrainCarsPerLogicTrack consistInfo = null, SpecialTrain prevSpecial = null)
        {
            int nTotalCars;
            List <TrainCarType> jobCarTypes = null;
            float trainLength;
            Track startSiding;

            // Establish the starting consist and its storage location
            if (consistInfo == null)
            {
                // generate a consist
                nTotalCars = Rand.Next(MIN_CARS_EXPRESS, MAX_CARS_EXPRESS + 1);

                float singleCarLength = TrackOrg.GetCarTypeLength(TrainCarType.PassengerRed);
                trainLength = (singleCarLength * nTotalCars) + TrackOrg.GetSeparationLengthBetweenCars(nTotalCars);

                // pick start storage track
                var emptyTracks = TrackOrg.FilterOutOccupiedTracks(StorageTracks);
                startSiding = TrackOrg.GetTrackWithEnoughFreeSpace(emptyTracks, trainLength);

                if (startSiding == null)
                {
                    startSiding = TrackOrg.GetTrackWithEnoughFreeSpace(StorageTracks, trainLength);
                }
            }
            else
            {
                // Use existing consist
                nTotalCars  = consistInfo.cars.Count;
                jobCarTypes = consistInfo.cars.Select(car => car.carType).ToList();
                trainLength = TrackOrg.GetTotalCarTypesLength(jobCarTypes) + TrackOrg.GetSeparationLengthBetweenCars(nTotalCars);
                startSiding = consistInfo.track;
            }

            if (startSiding == null)
            {
                //PassengerJobs.ModEntry.Logger.Log($"No available starting siding for express job at {Controller.stationInfo.Name}");
                return(null);
            }

            // Try to find a loading platform to use
            PlatformDefinition loadingPlatform = PlatformManager.PickPlatform(Controller.stationInfo.YardID);

            // Choose a route
            var               destPool    = PassDestinations.Values.ToList();
            Track             destSiding  = null;
            StationController destStation = null;

            // this prevents generating jobs like "ChainJob[Passenger]: FF - FF (FF-PE-47)"
            destPool.Remove(Controller);

            while ((destSiding == null) && (destPool.Count > 0))
            {
                // search the possible destinations 1 by 1 until we find an opening (or we don't)
                destStation = destPool.ChooseOne(Rand);

                // pick ending platform
                PassengerJobGenerator destGenerator = LinkedGenerators[destStation];
                destSiding = TrackOrg.GetTrackWithEnoughFreeSpace(destGenerator.StorageTracks, trainLength);

                // remove this station from the pool
                destPool.Remove(destStation);
            }

            if (destSiding == null)
            {
                //PassengerJobs.ModEntry.Logger.Log($"No available destination siding for new job at {Controller.stationInfo.Name}");
                return(null);
            }

            // we found a route :D
            // if we're creating a new consist, check if it can be a special train
            // let's try 2/3 chance of special train, 1/3 normal gen
            SpecialTrain  specialInfo = null;
            List <string> skinList    = null;

            if (consistInfo == null)
            {
                // default 67% chance, or as configured (if there is a special available)
                double choice = Rand.NextDouble();

                if ((choice <= PassengerJobs.Settings.NamedTrainProbability) &&
                    (SpecialConsistManager.GetTrainForRoute(Controller.stationInfo.YardID, destStation.stationInfo.YardID) is SpecialTrain special))
                {
                    specialInfo = special;
                    IEnumerable <SpecialTrainSkin> consistSkins = special.Skins.ChooseMany(Rand, nTotalCars);
                    jobCarTypes = consistSkins.Select(s => s.CarType).ToList();
                    skinList    = consistSkins.Select(s => s.Name).ToList();
                }
                else
                {
                    // normal consist generation
                    if (PassengerJobs.Settings.UniformConsists)
                    {
                        TrainCarType carType = PassCarTypes.ChooseOne(Rand);
                        jobCarTypes = Enumerable.Repeat(carType, nTotalCars).ToList();
                    }
                    else
                    {
                        jobCarTypes = PassCarTypes.ChooseMany(Rand, nTotalCars);
                    }
                }
            }
            else
            {
                // extant consist, use existing special (if it exists)
                specialInfo = prevSpecial;
            }

            // Try to find an unloading platform
            PlatformDefinition unloadingPlatform = PlatformManager.PickPlatform(destStation.stationInfo.YardID);

            // create job chain controller
            var chainJobObject = new GameObject($"ChainJob[Passenger]: {Controller.logicStation.ID} - {destStation.logicStation.ID}");

            chainJobObject.transform.SetParent(Controller.transform);
            var chainController = new PassengerTransportChainController(chainJobObject);

            StaticPassengerJobDefinition jobDefinition;

            //--------------------------------------------------------------------------------------------------------------------------------
            // Create transport leg job
            var chainData = new StationsChainData(Controller.stationInfo.YardID, destStation.stationInfo.YardID);
            PaymentCalculationData transportPaymentData = GetJobPaymentData(jobCarTypes);

            // calculate haul payment
            float haulDistance     = JobPaymentCalculator.GetDistanceBetweenStations(Controller, destStation);
            float bonusLimit       = JobPaymentCalculator.CalculateHaulBonusTimeLimit(haulDistance, false);
            float transportPayment = JobPaymentCalculator.CalculateJobPayment(JobType.Transport, haulDistance, transportPaymentData);

            // calculate additional payment for shunting
            const float            shuntDistance    = 500f;
            PaymentCalculationData emptyPaymentData = GetJobPaymentData(jobCarTypes, true);
            float platformBonusTime = JobPaymentCalculator.CalculateShuntingBonusTimeLimit(1) * 0.7f + PlatformController.START_XFER_DELAY;

            if (loadingPlatform?.Initialized == true)
            {
                float loadPayment = JobPaymentCalculator.CalculateJobPayment(JobType.ShuntingLoad, shuntDistance, emptyPaymentData);
                transportPayment += loadPayment;
                bonusLimit       += platformBonusTime;
            }

            if (unloadingPlatform?.Initialized == true)
            {
                float unloadPayment = JobPaymentCalculator.CalculateJobPayment(JobType.ShuntingUnload, shuntDistance, emptyPaymentData);
                transportPayment += unloadPayment;
                bonusLimit       += platformBonusTime;
            }

            // scale job payment depending on settings
            float wageScale = PassengerJobs.Settings.UseCustomWages ? BASE_WAGE_SCALE : 1;

            transportPayment = Mathf.Round(transportPayment * wageScale);

            if (consistInfo == null)
            {
                jobDefinition = PopulateTransportJobAndSpawn(
                    chainController, Controller.logicStation, startSiding, destSiding,
                    jobCarTypes, chainData, bonusLimit, transportPayment, true, skinList);
            }
            else
            {
                chainController.trainCarsForJobChain = consistInfo.cars;

                jobDefinition = PopulateTransportJobExistingCars(
                    chainController, Controller.logicStation, startSiding, destSiding,
                    consistInfo.LogicCars, chainData, bonusLimit, transportPayment);
            }

            if (jobDefinition == null)
            {
                PassengerJobs.ModEntry.Logger.Warning($"Failed to generate transport job definition for {chainController.jobChainGO.name}");
                chainController.DestroyChain();
                return(null);
            }
            jobDefinition.subType           = PassJobType.Express;
            jobDefinition.specialDefinition = specialInfo;

            // Setup any warehouse tasks
            if (loadingPlatform?.Initialized == true)
            {
                jobDefinition.loadMachine = loadingPlatform.Controller.LogicMachine;
            }

            if (unloadingPlatform?.Initialized == true)
            {
                jobDefinition.unloadMachine = unloadingPlatform.Controller.LogicMachine;
            }

            chainController.AddJobDefinitionToChain(jobDefinition);

            // Finalize job
            chainController.FinalizeSetupAndGenerateFirstJob();
            PassengerJobs.ModEntry.Logger.Log($"Generated new passenger haul job: {chainJobObject.name} ({chainController.currentJobInChain.ID})");

            return(chainController);
        }