StopSound() static private method

Stops the specified sound source.
static private StopSound ( SoundSource source ) : void
source SoundSource The sound source, or a null reference.
return void
示例#1
0
 /// <summary>Called by the controls loop to stop playback of this horn</summary>
 internal void Stop()
 {
     //Reset loop control variable
     LoopStarted = false;
     if (!StartEndSounds & !Loop)
     {
         //Don't stop horns which are play-once single part sounds
         return;
     }
     if (!StartEndSounds & Loop)
     {
         //This sound is a toggle music horn sound
         return;
     }
     if (Sounds.IsPlaying(Source))
     {
         //Stop the loop sound playing
         Sounds.StopSound(Source);
     }
     if (StartEndSounds && !Sounds.IsPlaying(Source) && EndSound != null)
     {
         //If our end sound is defined and in use, play once
         Source = Sounds.PlaySound(EndSound, 1.0, 1.0, SoundPosition,
                                   TrainManager.PlayerTrain,
                                   TrainManager.PlayerTrain.DriverCar, false);
     }
 }
示例#2
0
 internal override void Trigger(int Direction, EventTriggerType TriggerType, TrainManager.Train Train, int CarIndex)
 {
     if (TriggerType == EventTriggerType.FrontCarFrontAxle)
     {
         if (Direction < 0)
         {
             Train.StationFrontCar = true;
         }
         else if (Direction > 0)
         {
             Train.StationFrontCar = false;
             if (Train == TrainManager.PlayerTrain)
             {
                 Timetable.UpdateCustomTimetable(Game.Stations[this.StationIndex].TimetableDaytimeTexture, Game.Stations[this.StationIndex].TimetableNighttimeTexture);
             }
         }
     }
     else if (TriggerType == EventTriggerType.RearCarRearAxle)
     {
         if (Direction < 0)
         {
             Train.Station        = this.StationIndex;
             Train.StationRearCar = true;
             if (Train.NextStopSkipped != TrainManager.StopSkipMode.None)
             {
                 Train.LastStation = this.StationIndex;
             }
             Train.NextStopSkipped = TrainManager.StopSkipMode.None;
         }
         else if (Direction > 0)
         {
             if (Train.Station == StationIndex)
             {
                 if (Train == TrainManager.PlayerTrain)
                 {
                     if (Game.PlayerStopsAtStation(StationIndex) & TrainManager.PlayerTrain.StationState == TrainManager.TrainStopState.Pending)
                     {
                         string s = Interface.GetInterfaceString("message_station_passed");
                         s = s.Replace("[name]", Game.Stations[StationIndex].Name);
                         Game.AddMessage(s, MessageManager.MessageDependency.None, Interface.GameMode.Normal, MessageColor.Orange, Game.SecondsSinceMidnight + 10.0, null);
                     }
                     else if (Game.PlayerStopsAtStation(StationIndex) & TrainManager.PlayerTrain.StationState == TrainManager.TrainStopState.Boarding)
                     {
                         string s = Interface.GetInterfaceString("message_station_passed_boarding");
                         s = s.Replace("[name]", Game.Stations[StationIndex].Name);
                         Game.AddMessage(s, MessageManager.MessageDependency.None, Interface.GameMode.Normal, MessageColor.Red, Game.SecondsSinceMidnight + 10.0, null);
                     }
                 }
                 Train.Station        = -1;
                 Train.StationRearCar = false;
                 Train.StationState   = TrainManager.TrainStopState.Pending;
                 int d = Train.DriverCar;
                 Sounds.StopSound(Train.Cars[d].Sounds.Halt.Source);
             }
         }
     }
 }
示例#3
0
        protected override void Elapse(ElapseData data)
        {
            try {
                double            time = data.TotalTime.Milliseconds;
                Win32VehicleState win32State;
                win32State.Location    = data.Vehicle.Location;
                win32State.Speed       = (float)data.Vehicle.Speed.KilometersPerHour;
                win32State.Time        = (int)Math.Floor(time - 2073600000.0 * Math.Floor(time / 2073600000.0));
                win32State.BcPressure  = (float)data.Vehicle.BcPressure;
                win32State.MrPressure  = (float)data.Vehicle.MrPressure;
                win32State.ErPressure  = (float)data.Vehicle.ErPressure;
                win32State.BpPressure  = (float)data.Vehicle.BpPressure;
                win32State.SapPressure = (float)data.Vehicle.SapPressure;
                win32State.Current     = 0.0f;
                Win32Handles win32Handles;
                win32Handles.Brake         = data.Handles.BrakeNotch;
                win32Handles.Power         = data.Handles.PowerNotch;
                win32Handles.Reverser      = data.Handles.Reverser;
                win32Handles.ConstantSpeed = data.Handles.ConstSpeed ? ConstSpeedInstructions.Enable : ConstSpeedInstructions.Disable;
                Win32Elapse(ref win32Handles.Brake, ref win32State.Location, ref base.Panel[0], ref this.Sound[0]);
                data.Handles.Reverser   = win32Handles.Reverser;
                data.Handles.PowerNotch = win32Handles.Power;
                data.Handles.BrakeNotch = win32Handles.Brake;
                if (win32Handles.ConstantSpeed == ConstSpeedInstructions.Enable)
                {
                    data.Handles.ConstSpeed = true;
                }
                else if (win32Handles.ConstantSpeed == ConstSpeedInstructions.Disable)
                {
                    data.Handles.ConstSpeed = false;
                }
                else if (win32Handles.ConstantSpeed != ConstSpeedInstructions.Continue)
                {
                    this.PluginValid = false;
                }

                /*
                 * Process the sound instructions
                 * */
                for (int i = 0; i < this.Sound.Length; i++)
                {
                    if (this.Sound[i] != this.LastSound[i])
                    {
                        if (this.Sound[i] == SoundInstructions.Stop)
                        {
                            if (i < base.Train.Cars[base.Train.DriverCar].Sounds.Plugin.Length)
                            {
                                Sounds.StopSound(base.Train.Cars[base.Train.DriverCar].Sounds.Plugin[i].Source);
                            }
                        }
                        else if (this.Sound[i] > SoundInstructions.Stop & this.Sound[i] <= SoundInstructions.PlayLooping)
                        {
                            if (i < base.Train.Cars[base.Train.DriverCar].Sounds.Plugin.Length)
                            {
                                Sounds.SoundBuffer buffer = base.Train.Cars[base.Train.DriverCar].Sounds.Plugin[i].Buffer;
                                if (buffer != null)
                                {
                                    double volume = (double)(this.Sound[i] - SoundInstructions.Stop) / (double)(SoundInstructions.PlayLooping - SoundInstructions.Stop);
                                    if (Sounds.IsPlaying(base.Train.Cars[base.Train.DriverCar].Sounds.Plugin[i].Source))
                                    {
                                        base.Train.Cars[base.Train.DriverCar].Sounds.Plugin[i].Source.Volume = volume;
                                    }
                                    else
                                    {
                                        base.Train.Cars[base.Train.DriverCar].Sounds.Plugin[i].Source = Sounds.PlaySound(buffer, 1.0, volume, base.Train.Cars[base.Train.DriverCar].Sounds.Plugin[i].Position, base.Train, base.Train.DriverCar, true);
                                    }
                                }
                            }
                        }
                        else if (this.Sound[i] == SoundInstructions.PlayOnce)
                        {
                            if (i < base.Train.Cars[base.Train.DriverCar].Sounds.Plugin.Length)
                            {
                                Sounds.SoundBuffer buffer = base.Train.Cars[base.Train.DriverCar].Sounds.Plugin[i].Buffer;
                                if (buffer != null)
                                {
                                    base.Train.Cars[base.Train.DriverCar].Sounds.Plugin[i].Source = Sounds.PlaySound(buffer, 1.0, 1.0, base.Train.Cars[base.Train.DriverCar].Sounds.Plugin[i].Position, base.Train, base.Train.DriverCar, false);
                                }
                            }
                            this.Sound[i] = SoundInstructions.Continue;
                        }
                        else if (this.Sound[i] != SoundInstructions.Continue)
                        {
                            this.PluginValid = false;
                        }
                        this.LastSound[i] = this.Sound[i];
                    }
                    else
                    {
                        if ((this.Sound[i] < SoundInstructions.Stop | this.Sound[i] > SoundInstructions.PlayLooping) && this.Sound[i] != SoundInstructions.PlayOnce & this.Sound[i] != SoundInstructions.Continue)
                        {
                            this.PluginValid = false;
                        }
                    }
                }
            } catch (Exception ex) {
                base.LastException = ex;
                throw;
            }
        }
示例#4
0
            internal override void Update(double TimeElapsed, bool ForceUpdate)
            {
                const double extraRadius = 10.0;
                const double Radius      = 25.0;

                double pa      = currentTrackPosition + Radius - extraRadius;
                double pb      = currentTrackPosition + Radius + extraRadius;
                double ta      = World.CameraTrackFollower.TrackPosition + World.CameraCurrentAlignment.Position.Z - World.BackgroundImageDistance - World.ExtraViewingDistance;
                double tb      = World.CameraTrackFollower.TrackPosition + World.CameraCurrentAlignment.Position.Z + World.BackgroundImageDistance + World.ExtraViewingDistance;
                bool   visible = pb >= ta & pa <= tb;

                if (visible | ForceUpdate)
                {
                    if (Game.MinimalisticSimulation || TimeElapsed > 0.05)
                    {
                        return;
                    }
                    TrainManager.Train train         = null;
                    double             trainDistance = double.MaxValue;
                    for (int j = 0; j < TrainManager.Trains.Length; j++)
                    {
                        if (TrainManager.Trains[j].State == TrainState.Available)
                        {
                            double distance;
                            if (TrainManager.Trains[j].Cars[0].FrontAxle.Follower.TrackPosition < this.Follower.TrackPosition)
                            {
                                distance = this.Follower.TrackPosition - TrainManager.Trains[j].Cars[0].FrontAxle.Follower.TrackPosition;
                            }
                            else if (TrainManager.Trains[j].Cars[TrainManager.Trains[j].Cars.Length - 1].RearAxle.Follower.TrackPosition > this.Follower.TrackPosition)
                            {
                                distance = TrainManager.Trains[j].Cars[TrainManager.Trains[j].Cars.Length - 1].RearAxle.Follower.TrackPosition - this.Follower.TrackPosition;
                            }
                            else
                            {
                                distance = 0;
                            }
                            if (distance < trainDistance)
                            {
                                train         = TrainManager.Trains[j];
                                trainDistance = distance;
                            }
                        }
                    }
                    if (this.TrackFollowerFunction != null)
                    {
                        double delta = this.TrackFollowerFunction.Perform(train, train == null ? 0 : train.DriverCar, this.Position, this.Follower.TrackPosition, 0, false, TimeElapsed, 0);
                        this.Follower.Update(this.currentTrackPosition + delta, true, true);
                        this.Follower.UpdateWorldCoordinates(false);
                    }
                    if (this.VolumeFunction != null)
                    {
                        this.currentVolume = this.VolumeFunction.Perform(train, train == null ? 0 : train.DriverCar, this.Position, this.Follower.TrackPosition, 0, false, TimeElapsed, 0);
                    }
                    if (this.PitchFunction != null)
                    {
                        this.currentPitch = this.PitchFunction.Perform(train, train == null ? 0 : train.DriverCar, this.Position, this.Follower.TrackPosition, 0, false, TimeElapsed, 0);
                    }
                    if (this.Source != null)
                    {
                        this.Source.Pitch  = this.currentPitch;
                        this.Source.Volume = this.currentVolume;
                    }
                    //Buffer should never be null, but check it anyways
                    if (!Sounds.IsPlaying(Source) && this.Buffer != null)
                    {
                        Source = Sounds.PlaySound(Buffer, 1.0, 1.0, Follower.WorldPosition + Position, this, true);
                    }
                }
                else
                {
                    if (Sounds.IsPlaying(Source))
                    {
                        Sounds.StopSound(Source);
                    }
                }
            }
示例#5
0
            /// <summary>Called by the controls loop to start playback of this horn</summary>
            internal void Play()
            {
                if (StartEndSounds == true)
                {
                    //New style three-part sounds
                    if (LoopStarted == false)
                    {
                        if (!Sounds.IsPlaying(Source))
                        {
                            if (StartSound != null)
                            {
                                //The start sound is not currently playing, so start it
                                Source = Sounds.PlaySound(StartSound, 1.0, 1.0, SoundPosition,
                                                          TrainManager.PlayerTrain, TrainManager.PlayerTrain.DriverCar, false);

                                //Set the loop control variable to started
                                LoopStarted = true;
                            }
                            else
                            {
                                Source = Sounds.PlaySound(LoopSound, 1.0, 1.0, SoundPosition,
                                                          TrainManager.PlayerTrain, TrainManager.PlayerTrain.DriverCar, true);
                            }
                        }
                    }
                    else
                    {
                        if (!Sounds.IsPlaying(Source))
                        {
                            //Start our loop sound playing if the start sound is finished
                            Source = Sounds.PlaySound(LoopSound, 1.0, 1.0, SoundPosition,
                                                      TrainManager.PlayerTrain, TrainManager.PlayerTrain.DriverCar, true);
                        }
                    }
                }
                else
                {
                    //Original single part sounds
                    if (LoopSound != null)
                    {
                        //Loop is ONLY true if this is a Music Horn
                        if (Loop)
                        {
                            if (!Sounds.IsPlaying(Source) && !LoopStarted)
                            {
                                //On the first keydown event, start the sound source playing and trigger the loop control variable
                                Source = Sounds.PlaySound(LoopSound, 1.0, 1.0, SoundPosition,
                                                          TrainManager.PlayerTrain, TrainManager.PlayerTrain.DriverCar, true);
                                LoopStarted = true;
                            }
                            else
                            {
                                if (!LoopStarted)
                                {
                                    //Our loop control variable is reset by the keyup event so this code will only trigger on the
                                    //second keydown meaning our horn toggles
                                    Sounds.StopSound(Source);
                                    LoopStarted = true;
                                }
                            }
                        }
                        else
                        {
                            if (!LoopStarted)
                            {
                                Source = Sounds.PlaySound(LoopSound, 1.0, 1.0, SoundPosition, TrainManager.PlayerTrain,
                                                          TrainManager.PlayerTrain.DriverCar, false);
                            }
                            LoopStarted = true;
                        }
                    }
                }
            }
示例#6
0
        /// <summary>Is called once a frame to update the station state for the given train</summary>
        /// <param name="Train">The train</param>
        /// <param name="TimeElapsed">The frame time elapsed</param>
        private static void UpdateTrainStation(Train Train, double TimeElapsed)
        {
            if (Train.Station >= 0)
            {
                int    i = Train.Station;
                int    n = Game.Stations[Train.Station].GetStopIndex(Train.Cars.Length);
                double tf, tb;
                if (n >= 0)
                {
                    double p0 = Train.Cars[0].FrontAxle.Follower.TrackPosition - Train.Cars[0].FrontAxle.Position + 0.5 * Train.Cars[0].Length;
                    double p1 = Game.Stations[i].Stops[n].TrackPosition;
                    tf = Game.Stations[i].Stops[n].ForwardTolerance;
                    tb = Game.Stations[i].Stops[n].BackwardTolerance;
                    Train.StationDistanceToStopPoint = p1 - p0;
                }
                else
                {
                    Train.StationDistanceToStopPoint = 0.0;
                    tf = 5.0;
                    tb = 5.0;
                }
                if (Train.StationState == TrainStopState.Pending)
                {
                    Train.StationDepartureSoundPlayed = false;
                    if (Game.StopsAtStation(i, Train))
                    {
                        Train.StationDepartureSoundPlayed = false;
                        //Check whether all doors are controlled by the driver
                        if (Train.Specs.DoorOpenMode != DoorMode.Manual)
                        {
                            //Check that we are not moving
                            if (Math.Abs(Train.Specs.CurrentAverageSpeed) < 0.1 / 3.6 &
                                Math.Abs(Train.Specs.CurrentAverageAcceleration) < 0.1 / 3.6)
                            {
                                //Check the interlock state for the doors
                                switch (Train.Specs.DoorInterlockState)
                                {
                                case DoorInterlockStates.Unlocked:
                                    if (Game.Stations[i].OpenLeftDoors || Game.Stations[i].OpenRightDoors)
                                    {
                                        AttemptToOpenDoors(Train, i, tb, tf);
                                    }
                                    break;

                                case DoorInterlockStates.Left:
                                    if (Game.Stations[i].OpenLeftDoors && !Game.Stations[i].OpenRightDoors)
                                    {
                                        AttemptToOpenDoors(Train, i, tb, tf);
                                    }
                                    break;

                                case DoorInterlockStates.Right:
                                    if (!Game.Stations[i].OpenLeftDoors && Game.Stations[i].OpenRightDoors)
                                    {
                                        AttemptToOpenDoors(Train, i, tb, tf);
                                    }
                                    break;

                                case DoorInterlockStates.Locked:
                                    //All doors are currently locked, do nothing
                                    break;
                                }
                            }
                        }
                        // detect arrival
                        if (Train.Specs.CurrentAverageSpeed > -0.277777777777778 & Train.Specs.CurrentAverageSpeed < 0.277777777777778)
                        {
                            bool left, right;
                            if (Game.Stations[i].OpenLeftDoors)
                            {
                                left = false;
                                for (int j = 0; j < Train.Cars.Length; j++)
                                {
                                    if (Train.Cars[j].Doors[0].AnticipatedOpen)
                                    {
                                        left = true; break;
                                    }
                                }
                            }
                            else
                            {
                                left = true;
                            }
                            if (Game.Stations[i].OpenRightDoors)
                            {
                                right = false;
                                for (int j = 0; j < Train.Cars.Length; j++)
                                {
                                    if (Train.Cars[j].Doors[1].AnticipatedOpen)
                                    {
                                        right = true; break;
                                    }
                                }
                            }
                            else
                            {
                                right = true;
                            }
                            if (left & right)
                            {
                                // arrival
                                Train.StationState  = TrainStopState.Boarding;
                                Train.StationAdjust = false;
                                Train.Specs.DoorClosureAttempted = false;
                                Sounds.StopSound(Train.Cars[Train.DriverCar].Sounds.Halt.Source);
                                Sounds.SoundBuffer buffer = Game.Stations[i].ArrivalSoundBuffer;
                                if (buffer != null)
                                {
                                    OpenBveApi.Math.Vector3 pos = Game.Stations[i].SoundOrigin;
                                    Sounds.PlaySound(buffer, 1.0, 1.0, pos, false);
                                }
                                Train.StationArrivalTime   = Game.SecondsSinceMidnight;
                                Train.StationDepartureTime = Game.Stations[i].DepartureTime - Train.TimetableDelta;
                                if (Train.StationDepartureTime - Game.SecondsSinceMidnight < Game.Stations[i].StopTime)
                                {
                                    Train.StationDepartureTime = Game.SecondsSinceMidnight + Game.Stations[i].StopTime;
                                }
                                Train.Passengers.PassengerRatio = Game.Stations[i].PassengerRatio;
                                UpdateTrainMassFromPassengerRatio(Train);
                                if (Train == PlayerTrain)
                                {
                                    double early = 0.0;
                                    if (Game.Stations[i].ArrivalTime >= 0.0)
                                    {
                                        early = (Game.Stations[i].ArrivalTime - Train.TimetableDelta) - Train.StationArrivalTime;
                                    }
                                    string s;
                                    if (early < -1.0)
                                    {
                                        s = Translations.GetInterfaceString("message_station_arrival_late");
                                    }
                                    else if (early > 1.0)
                                    {
                                        s = Translations.GetInterfaceString("message_station_arrival_early");
                                    }
                                    else
                                    {
                                        s = Translations.GetInterfaceString("message_station_arrival");
                                    }
                                    System.Globalization.CultureInfo Culture = System.Globalization.CultureInfo.InvariantCulture;
                                    TimeSpan a = TimeSpan.FromSeconds(Math.Abs(early));
                                    string   b = a.Hours.ToString("00", Culture) + ":" + a.Minutes.ToString("00", Culture) + ":" + a.Seconds.ToString("00", Culture);
                                    if (Train.StationDistanceToStopPoint < -0.1)
                                    {
                                        s += Translations.GetInterfaceString("message_delimiter") + Translations.GetInterfaceString("message_station_overrun");
                                    }
                                    else if (Train.StationDistanceToStopPoint > 0.1)
                                    {
                                        s += Translations.GetInterfaceString("message_delimiter") + Translations.GetInterfaceString("message_station_underrun");
                                    }
                                    double d = Math.Abs(Train.StationDistanceToStopPoint);
                                    string c = d.ToString("0.0", Culture);
                                    if (Game.Stations[i].Type == StationType.Terminal)
                                    {
                                        s += Translations.GetInterfaceString("message_delimiter") + Translations.GetInterfaceString("message_station_terminal");
                                    }
                                    s = s.Replace("[name]", Game.Stations[i].Name);
                                    s = s.Replace("[time]", b);
                                    s = s.Replace("[difference]", c);
                                    Game.AddMessage(s, MessageManager.MessageDependency.StationArrival, Interface.GameMode.Normal, MessageColor.White, Game.SecondsSinceMidnight + 10.0, null);
                                    if (Game.Stations[i].Type == StationType.Normal)
                                    {
                                        s = Translations.GetInterfaceString("message_station_deadline");
                                        Game.AddMessage(s, MessageManager.MessageDependency.StationDeparture, Interface.GameMode.Normal, MessageColor.White, double.PositiveInfinity, null);
                                    }
                                    Timetable.UpdateCustomTimetable(Game.Stations[i].TimetableDaytimeTexture, Game.Stations[i].TimetableNighttimeTexture);
                                }
                                // schedule door locks (passengers stuck between the doors)
                                for (int j = 0; j < Train.Cars.Length; j++)
                                {
                                    for (int k = 0; k < Train.Cars[j].Doors.Length; k++)
                                    {
                                        Train.Cars[j].Doors[k].DoorLockDuration = 0.0;
                                        if (Game.Stations[i].OpenLeftDoors & Train.Cars[j].Doors[k].Direction == -1 | Game.Stations[i].OpenRightDoors & Train.Cars[j].Doors[k].Direction == 1)
                                        {
                                            double p = 0.005 * Game.Stations[i].PassengerRatio * Game.Stations[i].PassengerRatio * Game.Stations[i].PassengerRatio * Game.Stations[i].PassengerRatio;
                                            if (Program.RandomNumberGenerator.NextDouble() < p)
                                            {
                                                /*
                                                 * -- door lock at state --
                                                 * minimum: 0.2 (nearly closed)
                                                 * maximum: 0.8 (nearly opened)
                                                 * */
                                                Train.Cars[j].Doors[k].DoorLockState = 0.2 + 0.6 * Program.RandomNumberGenerator.NextDouble();

                                                /* -- waiting time --
                                                 * minimum: 2.9 s
                                                 * maximum: 40.0 s
                                                 * average: 7.6 s
                                                 * */
                                                p = Program.RandomNumberGenerator.NextDouble();
                                                Train.Cars[j].Doors[k].DoorLockDuration = (50.0 - 10.0 * p) / (17.0 - 16.0 * p);
                                            }
                                        }
                                    }
                                }
                            }
                            else if (Train.Specs.CurrentAverageSpeed > -0.277777777777778 & Train.Specs.CurrentAverageSpeed < 0.277777777777778)
                            {
                                // correct stop position
                                if (!Train.StationAdjust & (Train.StationDistanceToStopPoint > tb | Train.StationDistanceToStopPoint < -tf))
                                {
                                    Sounds.SoundBuffer buffer = Train.Cars[Train.DriverCar].Sounds.Adjust.Buffer;
                                    if (buffer != null)
                                    {
                                        OpenBveApi.Math.Vector3 pos = Train.Cars[Train.DriverCar].Sounds.Adjust.Position;
                                        Sounds.PlaySound(buffer, 1.0, 1.0, pos, Train, Train.DriverCar, false);
                                    }
                                    if (Train == TrainManager.PlayerTrain)
                                    {
                                        Game.AddMessage(Translations.GetInterfaceString("message_station_correct"), MessageManager.MessageDependency.None, Interface.GameMode.Normal, MessageColor.Orange, Game.SecondsSinceMidnight + 5.0, null);
                                    }
                                    Train.StationAdjust = true;
                                }
                            }
                            else
                            {
                                Train.StationAdjust = false;
                            }
                        }
                    }
                }
                else if (Train.StationState == TrainStopState.Boarding)
                {
                    for (int j = 0; j < Train.Cars.Length; j++)
                    {
                        if (GetDoorsState(Train, j, Game.Stations[i].OpenLeftDoors, Game.Stations[i].OpenRightDoors) == (TrainDoorState.Opened | TrainDoorState.AllOpened))
                        {
                            //Check whether all doors are controlled by the driver, and whether this is a non-standard station type
                            //e.g. Change ends
                            if (Train.Specs.DoorCloseMode != DoorMode.Manual & Game.Stations[i].Type == StationType.Normal)
                            {
                                //Check the interlock state for the doors
                                switch (Train.Specs.DoorInterlockState)
                                {
                                case DoorInterlockStates.Unlocked:
                                    AttemptToCloseDoors(Train);
                                    break;

                                case DoorInterlockStates.Left:
                                    if (Game.Stations[i].OpenLeftDoors)
                                    {
                                        AttemptToCloseDoors(Train);
                                    }
                                    break;

                                case DoorInterlockStates.Right:
                                    if (Game.Stations[i].OpenRightDoors)
                                    {
                                        AttemptToCloseDoors(Train);
                                    }
                                    break;

                                case DoorInterlockStates.Locked:
                                    //All doors are currently locked, do nothing
                                    break;
                                }

                                if (Train.Specs.DoorInterlockState != DoorInterlockStates.Locked & Train.Specs.DoorClosureAttempted)
                                {
                                    if (Game.Stations[i].OpenLeftDoors && !Train.Cars[j].Doors[0].AnticipatedReopen && Program.RandomNumberGenerator.NextDouble() < Game.Stations[i].ReopenDoor)
                                    {
                                        Train.Cars[j].Doors[0].ReopenLimit           = Program.RandomNumberGenerator.Next(1, Game.Stations[i].ReopenStationLimit);
                                        Train.Cars[j].Doors[0].ReopenCounter         = 0;
                                        Train.Cars[j].Doors[0].InterferingObjectRate = Program.RandomNumberGenerator.Next(1, Game.Stations[i].MaxInterferingObjectRate) * 0.01;
                                        if (Train.Cars[j].Doors[0].InterferingObjectRate * Train.Specs.DoorWidth >= Train.Specs.DoorMaxTolerance)
                                        {
                                            Train.Cars[j].Doors[0].AnticipatedReopen = true;
                                        }
                                    }
                                    if (Game.Stations[i].OpenRightDoors && !Train.Cars[j].Doors[1].AnticipatedReopen && Program.RandomNumberGenerator.NextDouble() < Game.Stations[i].ReopenDoor)
                                    {
                                        Train.Cars[j].Doors[1].ReopenLimit           = Program.RandomNumberGenerator.Next(1, Game.Stations[i].ReopenStationLimit);
                                        Train.Cars[j].Doors[1].ReopenCounter         = 0;
                                        Train.Cars[j].Doors[1].InterferingObjectRate = Program.RandomNumberGenerator.Next(1, Game.Stations[i].MaxInterferingObjectRate) * 0.01;
                                        if (Train.Cars[j].Doors[1].InterferingObjectRate * Train.Specs.DoorWidth >= Train.Specs.DoorMaxTolerance)
                                        {
                                            Train.Cars[j].Doors[1].AnticipatedReopen = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // detect departure
                    bool left, right;
                    if (!Game.Stations[i].OpenLeftDoors & !Game.Stations[i].OpenRightDoors)
                    {
                        left  = true;
                        right = true;
                    }
                    else
                    {
                        if (Game.Stations[i].OpenLeftDoors)
                        {
                            left = false;
                            for (int j = 0; j < Train.Cars.Length; j++)
                            {
                                for (int k = 0; k < Train.Cars[j].Doors.Length; k++)
                                {
                                    if (Train.Cars[j].Doors[k].State != 0.0)
                                    {
                                        left = true; break;
                                    }
                                }
                                if (left)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            left = false;
                        }
                        if (Game.Stations[i].OpenRightDoors)
                        {
                            right = false;
                            for (int j = 0; j < Train.Cars.Length; j++)
                            {
                                for (int k = 0; k < Train.Cars[j].Doors.Length; k++)
                                {
                                    if (Train.Cars[j].Doors[k].State != 0.0)
                                    {
                                        right = true; break;
                                    }
                                }
                                if (right)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            right = false;
                        }
                    }
                    // departure sound
                    if (!Train.StationDepartureSoundPlayed)
                    {
                        Sounds.SoundBuffer buffer = Game.Stations[i].DepartureSoundBuffer;
                        if (buffer != null)
                        {
                            double dur = Sounds.GetDuration(buffer);
                            if (Game.SecondsSinceMidnight >= Train.StationDepartureTime - dur)
                            {
                                Sounds.PlaySound(buffer, 1.0, 1.0, Game.Stations[i].SoundOrigin, false);
                                Train.StationDepartureSoundPlayed = true;
                            }
                        }
                    }
                    for (int j = 0; j < Train.Cars.Length; j++)
                    {
                        if (Train.Cars[j].Doors[0].AnticipatedReopen && Train.Cars[j].Doors[0].State == Train.Cars[j].Doors[0].InterferingObjectRate)
                        {
                            if (Train.Cars[j].Doors[0].NextReopenTime == 0.0)
                            {
                                Train.Cars[j].Doors[0].NextReopenTime = Game.SecondsSinceMidnight + Game.Stations[i].InterferenceInDoor;
                            }
                            else if (Train.Cars[j].Doors[0].ReopenCounter < Train.Cars[j].Doors[0].ReopenLimit)
                            {
                                if (Game.SecondsSinceMidnight >= Train.Cars[j].Doors[0].NextReopenTime)
                                {
                                    OpenTrainDoors(Train, j, true, false);
                                }
                            }
                            else
                            {
                                Train.Cars[j].Doors[0].AnticipatedReopen = false;
                            }
                        }
                        if (Train.Cars[j].Doors[1].AnticipatedReopen && Train.Cars[j].Doors[1].State == Train.Cars[j].Doors[1].InterferingObjectRate)
                        {
                            if (Train.Cars[j].Doors[1].NextReopenTime == 0.0)
                            {
                                Train.Cars[j].Doors[1].NextReopenTime = Game.SecondsSinceMidnight + Game.Stations[i].InterferenceInDoor;
                            }
                            else if (Train.Cars[j].Doors[1].ReopenCounter < Train.Cars[j].Doors[1].ReopenLimit)
                            {
                                if (Game.SecondsSinceMidnight >= Train.Cars[j].Doors[1].NextReopenTime)
                                {
                                    OpenTrainDoors(Train, j, false, true);
                                }
                            }
                            else
                            {
                                Train.Cars[j].Doors[1].AnticipatedReopen = false;
                            }
                        }
                    }
                    TrainDoorState doorState = GetDoorsState(Train, Game.Stations[i].OpenLeftDoors, Game.Stations[i].OpenRightDoors);
                    if (Train.Specs.DoorCloseMode == DoorMode.Manual || doorState == TrainDoorState.None || doorState == (TrainDoorState.Closed | TrainDoorState.AllClosed))
                    {
                        if (left | right)
                        {
                            // departure message
                            if (Game.SecondsSinceMidnight > Train.StationDepartureTime && (Game.Stations[i].Type != StationType.Terminal || Train != PlayerTrain))
                            {
                                Train.StationState = TrainStopState.Completed;
                                if (Train == PlayerTrain & Game.Stations[i].Type == StationType.Normal)
                                {
                                    if (!Game.Stations[i].OpenLeftDoors & !Game.Stations[i].OpenRightDoors | Train.Specs.DoorCloseMode != DoorMode.Manual)
                                    {
                                        Game.AddMessage(Translations.GetInterfaceString("message_station_depart"), MessageManager.MessageDependency.None, Interface.GameMode.Normal, MessageColor.White, Game.SecondsSinceMidnight + 5.0, null);
                                    }
                                    else
                                    {
                                        Game.AddMessage(Translations.GetInterfaceString("message_station_depart_closedoors"), MessageManager.MessageDependency.None, Interface.GameMode.Normal, MessageColor.White, Game.SecondsSinceMidnight + 5.0, null);
                                    }
                                }
                                else if (Game.Stations[i].Type == StationType.ChangeEnds)
                                {
                                    JumpTrain(Train, i + 1);
                                }
                            }
                            if (Interface.CurrentOptions.LoadingSway)
                            {
                                // passengers boarding
                                for (int j = 0; j < Train.Cars.Length; j++)
                                {
                                    double r = 2.0 * Game.Stations[i].PassengerRatio * TimeElapsed;
                                    if (r >= Program.RandomNumberGenerator.NextDouble())
                                    {
                                        int d =
                                            (int)Math.Floor(Program.RandomNumberGenerator.NextDouble() * (double)Train.Cars[j].Doors.Length);
                                        if (Train.Cars[j].Doors[d].State == 1.0)
                                        {
                                            Train.Cars[j].Specs.CurrentRollShakeDirection += (double)Train.Cars[j].Doors[d].Direction;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            Train.StationState = TrainStopState.Completed;
                            if (Train == PlayerTrain & Game.Stations[i].Type == StationType.Normal)
                            {
                                Game.AddMessage(Translations.GetInterfaceString("message_station_depart"), MessageManager.MessageDependency.None, Interface.GameMode.Normal, MessageColor.White, Game.SecondsSinceMidnight + 5.0, null);
                            }
                        }
                    }
                }
            }
            else
            {
                if (Train.StationState != TrainStopState.Jumping)
                {
                    Train.StationState = TrainStopState.Pending;
                }
            }
            // automatically close doors
            if (Train.Specs.DoorCloseMode != DoorMode.Manual & Train.Specs.DoorInterlockState != DoorInterlockStates.Locked & !Train.Specs.DoorClosureAttempted)
            {
                if (Train.Station == -1 | Train.StationState == TrainStopState.Completed)
                {
                    if ((GetDoorsState(Train, true, true) & TrainDoorState.AllClosed) == 0)
                    {
                        CloseTrainDoors(Train, true, true);
                        Train.Specs.DoorClosureAttempted = true;
                    }
                }
            }
        }
示例#7
0
            /// <summary>Updates the brake system for a car within this train</summary>
            /// <remarks>This must remain a property of the train, for easy access to various base properties</remarks>
            /// <param name="CarIndex">The induvidual car</param>
            /// <param name="TimeElapsed">The frame time elapsed</param>
            /// <param name="DecelerationDueToBrake">The total brake deceleration this car provides</param>
            /// <param name="DecelerationDueToMotor">The total motor deceleration this car provides</param>
            private void UpdateBrakeSystem(int CarIndex, double TimeElapsed, out double DecelerationDueToBrake, out double DecelerationDueToMotor)
            {
                DecelerationDueToBrake = 0.0;
                // air compressor
                if (Cars[CarIndex].CarBrake.brakeType == BrakeType.Main)
                {
                    if (Cars[CarIndex].CarBrake.airCompressor.Enabled)
                    {
                        if (Cars[CarIndex].CarBrake.mainReservoir.CurrentPressure > Cars[CarIndex].CarBrake.mainReservoir.MaximumPressure)
                        {
                            Cars[CarIndex].CarBrake.airCompressor.Enabled = false;
                            Cars[CarIndex].Sounds.CpLoopStarted           = false;
                            Sounds.SoundBuffer buffer = Cars[CarIndex].Sounds.CpEnd.Buffer;
                            if (buffer != null)
                            {
                                OpenBveApi.Math.Vector3 pos = Cars[CarIndex].Sounds.CpEnd.Position;
                                Sounds.PlaySound(buffer, 1.0, 1.0, pos, this, CarIndex, false);
                            }

                            buffer = Cars[CarIndex].Sounds.CpLoop.Buffer;
                            if (buffer != null)
                            {
                                Sounds.StopSound(Cars[CarIndex].Sounds.CpLoop.Source);
                            }
                        }
                        else
                        {
                            Cars[CarIndex].CarBrake.mainReservoir.CurrentPressure += Cars[CarIndex].CarBrake.airCompressor.Rate * TimeElapsed;
                            if (!Cars[CarIndex].Sounds.CpLoopStarted && Game.SecondsSinceMidnight > Cars[CarIndex].Sounds.CpStartTimeStarted + 5.0)
                            {
                                Cars[CarIndex].Sounds.CpLoopStarted = true;
                                Sounds.SoundBuffer buffer = Cars[CarIndex].Sounds.CpLoop.Buffer;
                                if (buffer != null)
                                {
                                    OpenBveApi.Math.Vector3 pos = Cars[CarIndex].Sounds.CpLoop.Position;
                                    Cars[CarIndex].Sounds.CpLoop.Source = Sounds.PlaySound(buffer, 1.0, 1.0, pos, this, CarIndex, true);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (Cars[CarIndex].CarBrake.mainReservoir.CurrentPressure < Cars[CarIndex].CarBrake.mainReservoir.MinimumPressure)
                        {
                            Cars[CarIndex].CarBrake.airCompressor.Enabled = true;
                            Cars[CarIndex].Sounds.CpStartTimeStarted      = Game.SecondsSinceMidnight;
                            Sounds.SoundBuffer buffer = Cars[CarIndex].Sounds.CpStart.Buffer;
                            if (buffer != null)
                            {
                                OpenBveApi.Math.Vector3 pos = Cars[CarIndex].Sounds.CpStart.Position;
                                Sounds.PlaySound(buffer, 1.0, 1.0, pos, this, CarIndex, false);
                            }
                        }
                    }
                }

                if (CarIndex == DriverCar && Handles.HasLocoBrake)
                {
                    switch (Handles.LocoBrakeType)
                    {
                    case LocoBrakeType.Independant:
                        //With an independant Loco brake, we always want to use this handle
                        Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.LocoBrake, out DecelerationDueToBrake);
                        break;

                    case LocoBrakeType.Combined:
                        if (Handles.LocoBrake is LocoBrakeHandle && Handles.Brake is NotchedHandle)
                        {
                            //Both handles are of the notched type
                            if (Handles.Brake.MaximumNotch == Handles.LocoBrake.MaximumNotch)
                            {
                                //Identical number of notches, so return the handle with the higher setting
                                if (Handles.LocoBrake.Actual >= Handles.Brake.Actual)
                                {
                                    Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.LocoBrake, out DecelerationDueToBrake);
                                }
                                else
                                {
                                    Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.Brake, out DecelerationDueToBrake);
                                }
                            }
                            else if (Handles.Brake.MaximumNotch > Handles.LocoBrake.MaximumNotch)
                            {
                                double nc = ((double)Handles.LocoBrake.Actual / Handles.LocoBrake.MaximumNotch) * Handles.Brake.MaximumNotch;
                                if (nc > Handles.Brake.Actual)
                                {
                                    Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.LocoBrake, out DecelerationDueToBrake);
                                }
                                else
                                {
                                    Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.Brake, out DecelerationDueToBrake);
                                }
                            }
                            else
                            {
                                double nc = ((double)Handles.Brake.Actual / Handles.Brake.MaximumNotch) * Handles.LocoBrake.MaximumNotch;
                                if (nc > Handles.LocoBrake.Actual)
                                {
                                    Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.Brake, out DecelerationDueToBrake);
                                }
                                else
                                {
                                    Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.LocoBrake, out DecelerationDueToBrake);
                                }
                            }
                        }
                        else if (Handles.LocoBrake is LocoAirBrakeHandle && Handles.Brake is AirBrakeHandle)
                        {
                            if (Handles.LocoBrake.Actual < Handles.Brake.Actual)
                            {
                                Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.Brake, out DecelerationDueToBrake);
                            }
                            else
                            {
                                Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.LocoBrake, out DecelerationDueToBrake);
                            }
                        }
                        else
                        {
                            double p, tp;
                            //Calculate the pressure differentials for the two handles
                            if (Handles.LocoBrake is LocoAirBrakeHandle)
                            {
                                //Air brake handle
                                p  = Cars[CarIndex].CarBrake.brakeCylinder.CurrentPressure / Cars[CarIndex].CarBrake.brakeCylinder.ServiceMaximumPressure;
                                tp = (Cars[CarIndex].CarBrake.brakeCylinder.ServiceMaximumPressure / Handles.Brake.MaximumNotch) * Handles.Brake.Actual;
                            }
                            else
                            {
                                //Notched handle
                                p  = Cars[CarIndex].CarBrake.brakeCylinder.CurrentPressure / Cars[CarIndex].CarBrake.brakeCylinder.ServiceMaximumPressure;
                                tp = (Cars[CarIndex].CarBrake.brakeCylinder.ServiceMaximumPressure / Handles.LocoBrake.MaximumNotch) * Handles.LocoBrake.Actual;
                            }

                            if (p < tp)
                            {
                                Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.Brake, out DecelerationDueToBrake);
                            }
                            else
                            {
                                Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.LocoBrake, out DecelerationDueToBrake);
                            }
                        }
                        break;

                    case LocoBrakeType.Blocking:
                        if (Handles.LocoBrake.Actual != 0)
                        {
                            Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.LocoBrake, out DecelerationDueToBrake);
                        }
                        else
                        {
                            Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.Brake, out DecelerationDueToBrake);
                        }

                        break;
                    }
                }
                else
                {
                    Cars[CarIndex].CarBrake.Update(TimeElapsed, Cars[DriverCar].Specs.CurrentSpeed, Handles.Brake, out DecelerationDueToBrake);
                }


                switch (Cars[CarIndex].CarBrake.airSound)
                {
                case AirSound.AirZero:
                {
                    Sounds.SoundBuffer buffer = Cars[CarIndex].Sounds.AirZero.Buffer;
                    if (buffer != null)
                    {
                        OpenBveApi.Math.Vector3 pos = Cars[CarIndex].Sounds.AirZero.Position;
                        Sounds.PlaySound(buffer, 1.0, 1.0, pos, this, CarIndex, false);
                    }

                    break;
                }

                case AirSound.Air:
                {
                    Sounds.SoundBuffer buffer = Cars[CarIndex].Sounds.Air.Buffer;
                    if (buffer != null)
                    {
                        OpenBveApi.Math.Vector3 pos = Cars[CarIndex].Sounds.Air.Position;
                        Sounds.PlaySound(buffer, 1.0, 1.0, pos, this, CarIndex, false);
                    }
                    break;
                }

                case AirSound.AirHigh:
                {
                    Sounds.SoundBuffer buffer = Cars[CarIndex].Sounds.AirHigh.Buffer;
                    if (buffer != null)
                    {
                        OpenBveApi.Math.Vector3 pos = Cars[CarIndex].Sounds.AirHigh.Position;
                        Sounds.PlaySound(buffer, 1.0, 1.0, pos, this, CarIndex, false);
                    }
                    break;
                }
                }

                // deceleration provided by motor
                if (!(Cars[CarIndex].CarBrake is AutomaticAirBrake) && Math.Abs(Cars[CarIndex].Specs.CurrentSpeed) >= Cars[CarIndex].CarBrake.brakeControlSpeed & Handles.Reverser.Actual != 0 & !Handles.EmergencyBrake.Actual)
                {
                    double f;
                    if (Handles.LocoBrake.Actual != 0 && CarIndex == DriverCar)
                    {
                        f = (double)Handles.LocoBrake.Actual / (double)Handles.Brake.MaximumNotch;
                    }
                    else
                    {
                        f = (double)Handles.Brake.Actual / (double)Handles.Brake.MaximumNotch;
                    }

                    double a = Cars[CarIndex].Specs.MotorDeceleration;
                    DecelerationDueToMotor = f * a;
                }
                else
                {
                    DecelerationDueToMotor = 0.0;
                }

                // hold brake
                Cars[CarIndex].Specs.HoldBrake.Update(ref DecelerationDueToMotor, Handles.HoldBrake.Actual);
                {
                    // rub sound
                    Sounds.SoundBuffer buffer = Cars[CarIndex].Sounds.Rub.Buffer;
                    if (buffer != null)
                    {
                        double spd   = Math.Abs(Cars[CarIndex].Specs.CurrentSpeed);
                        double pitch = 1.0 / (spd + 1.0) + 1.0;
                        double gain  = Cars[CarIndex].Derailed ? 0.0 : Cars[CarIndex].CarBrake.brakeCylinder.CurrentPressure / Cars[CarIndex].CarBrake.brakeCylinder.ServiceMaximumPressure;
                        if (spd < 1.38888888888889)
                        {
                            double t = spd * spd;
                            gain *= 1.5552 * t - 0.746496 * spd * t;
                        }
                        else if (spd > 12.5)
                        {
                            double       t          = spd - 12.5;
                            const double fadefactor = 0.1;
                            gain *= 1.0 / (fadefactor * t * t + 1.0);
                        }

                        if (Sounds.IsPlaying(Cars[CarIndex].Sounds.Rub.Source))
                        {
                            if (pitch > 0.01 & gain > 0.001)
                            {
                                Cars[CarIndex].Sounds.Rub.Source.Pitch  = pitch;
                                Cars[CarIndex].Sounds.Rub.Source.Volume = gain;
                            }
                            else
                            {
                                Sounds.StopSound(Cars[CarIndex].Sounds.Rub.Source);
                            }
                        }
                        else if (pitch > 0.02 & gain > 0.01)
                        {
                            OpenBveApi.Math.Vector3 pos = Cars[CarIndex].Sounds.Rub.Position;
                            Cars[CarIndex].Sounds.Rub.Source = Sounds.PlaySound(buffer, pitch, gain, pos, this, CarIndex, true);
                        }
                    }
                }
            }