Beispiel #1
0
        public void Update()
        {
            if (m_initialized && m_shipGrid.Physics != null && m_shipGrid.IsStatic == false && (m_shipThrusters != null || m_shipWheels != null) && m_distanceToShip < MAX_UPDATE_RANGE_SQ)
            {
                //calculate current ship state
                m_driving = m_shipWheels.HasWorkingWheels(true);
                float shipSpeedSquered = m_driving == false?m_shipGrid.Physics.LinearVelocity.LengthSquared() : (m_shipGrid.Physics.LinearVelocity * WHEELS_SPEED_COMPENSATION).LengthSquared();

                ShipStateEnum lastState = m_shipState;
                if (m_shipGrid.GridSystems.ResourceDistributor.ResourceState == MyResourceStateEnum.NoPower || m_shipCategory == ShipTypeEnum.Debris ||
                    ((m_shipThrusters == null || m_shipThrusters.ThrustCount <= 0) && (m_shipWheels == null || m_shipWheels.WheelCount <= 0)))
                {
                    m_shipState = ShipStateEnum.NoPower;
                }
                else
                {
                    if (shipSpeedSquered < ENGINES_SPEED_THRESHOLD_1_SQ)
                    {
                        m_shipState = ShipStateEnum.Slow;
                    }
                    else if (shipSpeedSquered < ENGINES_SPEED_THRESHOLD_2_SQ)
                    {
                        m_shipState = ShipStateEnum.Medium;
                    }
                    else
                    {
                        m_shipState = ShipStateEnum.Fast;
                    }
                }

                //in first person change
                bool orig = m_shouldPlay2D;
                if (m_shipGrid.GridSizeEnum == MyCubeSize.Large)
                {
                    m_shouldPlay2D = m_insideShip;
                }
                else if (MySession.Static.ControlledEntity != null && MySession.Static.IsCameraUserControlledSpectator() == false && MySession.Static.ControlledEntity.Entity != null && MySession.Static.ControlledEntity.Entity.Parent == m_shipGrid)
                {
                    m_shouldPlay2D = ((MySession.Static.ControlledEntity.Entity is MyCockpit) && (MySession.Static.ControlledEntity.Entity as MyCockpit).IsInFirstPersonView) ||
                                     (MySession.Static.CameraController is MyCameraBlock && (MySession.Static.CameraController as MyCameraBlock).Parent == m_shipGrid);
                }
                else
                {
                    m_shouldPlay2D = false;
                }

                m_shouldPlay2DChanged = (orig != m_shouldPlay2D);

                //thruster volume corrections
                for (int i = 0; i < m_thrusterVolumes.Length; i++)
                {
                    if (m_thrusterVolumes[i] < m_thrusterVolumeTargets[i])
                    {
                        m_thrusterVolumes[i] = Math.Min(m_thrusterVolumes[i] + THRUSTER_COMPOSITION_CHANGE_SPEED, m_thrusterVolumeTargets[i]);
                    }
                    else if (m_thrusterVolumes[i] > m_thrusterVolumeTargets[i])
                    {
                        m_thrusterVolumes[i] = Math.Max(m_thrusterVolumes[i] - THRUSTER_COMPOSITION_CHANGE_SPEED, m_thrusterVolumeTargets[i]);
                    }
                }

                if (m_driving)
                {
                    m_wheelVolumeModifier = Math.Min(m_wheelVolumeModifier + 0.04f, 1f);
                }
                else
                {
                    m_wheelVolumeModifier = Math.Max(m_wheelVolumeModifier - 0.005f, 0f);
                }

                //play sounds if there was change in state, ship type or thruster composition
                if (m_shipState != lastState || m_categoryChange || m_forceSoundCheck)
                {
                    if (m_shipState == ShipStateEnum.NoPower)
                    {
                        if (m_shipState != lastState)
                        {
                            for (int i = 0; i < m_emitters.Length; i++)
                            {
                                m_emitters[i].StopSound(false);
                            }
                            PlayShipSound(ShipEmitters.SingleSounds, ShipSounds.EnginesEnd);
                        }
                    }
                    else
                    {
                        if (m_shipState == ShipStateEnum.Slow)
                        {
                            PlayShipSound(ShipEmitters.MainSound, ShipSounds.MainLoopSlow);
                        }
                        else if (m_shipState == ShipStateEnum.Medium)
                        {
                            PlayShipSound(ShipEmitters.MainSound, ShipSounds.MainLoopMedium);
                        }
                        else if (m_shipState == ShipStateEnum.Fast)
                        {
                            PlayShipSound(ShipEmitters.MainSound, ShipSounds.MainLoopFast);
                        }

                        PlayShipSound(ShipEmitters.ShipEngine, ShipSounds.ShipEngine);
                        if (m_shipGrid.GridSizeEnum == MyCubeSize.Large)
                        {
                            PlayShipSound(ShipEmitters.LargeShipIdle, ShipSounds.LargeShipIdle);
                        }

                        if (m_thrusterVolumes[(int)ShipThrusters.Ion] > 0f)
                        {
                            PlayShipSound(ShipEmitters.IonThrusters, ShipSounds.IonThrusters);
                            PlayShipSound(ShipEmitters.IonThrustersIdle, ShipSounds.IonThrustersIdle);
                        }

                        if (m_thrusterVolumes[(int)ShipThrusters.Hydrogen] > 0f)
                        {
                            PlayShipSound(ShipEmitters.HydrogenThrusters, ShipSounds.HydrogenThrusters);
                            PlayShipSound(ShipEmitters.HydrogenThrustersIdle, ShipSounds.HydrogenThrustersIdle);
                        }

                        if (m_thrusterVolumes[(int)ShipThrusters.Atmospheric] > 0f)
                        {
                            if (m_shipState == ShipStateEnum.Slow)
                            {
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSounds.AtmoThrustersSlow, useFadeOut: true);
                            }
                            else if (m_shipState == ShipStateEnum.Medium)
                            {
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSounds.AtmoThrustersMedium, useFadeOut: true);
                            }
                            else if (m_shipState == ShipStateEnum.Fast)
                            {
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSounds.AtmoThrustersFast, useFadeOut: true);
                            }
                            PlayShipSound(ShipEmitters.AtmosphericThrustersIdle, ShipSounds.AtmoThrustersIdle);
                        }

                        if (m_shipWheels.WheelCount > 0)
                        {
                            PlayShipSound(ShipEmitters.WheelsMain, ShipSounds.WheelsEngineRun);
                            PlayShipSound(ShipEmitters.WheelsSecondary, ShipSounds.WheelsSecondary);
                        }

                        if (lastState == ShipStateEnum.NoPower)
                        {
                            PlayShipSound(ShipEmitters.SingleSounds, ShipSounds.EnginesStart);
                        }
                    }
                    m_categoryChange  = false;
                    m_forceSoundCheck = false;
                }

                //there was change in camera - sound should change from 2d to 3d or vice versa
                if (m_shouldPlay2DChanged)
                {
                    for (int i = 0; i < m_emitters.Length; i++)
                    {
                        m_emitters[i].Force2D = m_shouldPlay2D;
                        m_emitters[i].Force3D = !m_shouldPlay2D;
                        if (m_emitters[i].IsPlaying && m_emitters[i].Plays2D != m_shouldPlay2D && m_emitters[i].Loop)
                        {
                            m_emitters[i].StopSound(true);
                            m_emitters[i].PlaySound(m_emitters[i].SoundPair, true, true, m_shouldPlay2D);
                        }
                    }
                    m_shouldPlay2DChanged = false;
                }

                //update emitter volumes
                if (m_shipState != ShipStateEnum.NoPower)
                {
                    if (m_shipEngineModifier < 1f)
                    {
                        m_shipEngineModifier = Math.Min(1f, m_shipEngineModifier + MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS / ENGINES_TIME_TURN_ON);
                    }
                    float shipSpeedRatio  = Math.Min(shipSpeedSquered / FULL_SPEED_SQ, 1f);
                    float shipSpeedVolume = (ENGINES_MIN_VOLUME + (1f - ENGINES_MIN_VOLUME) * shipSpeedRatio) * m_shipEngineModifier;
                    float shipThrusterRatio;
                    float shipThrusterIdleRatio = 1f;

                    //main sound - engines
                    if (m_emitters[(int)ShipEmitters.MainSound].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.MainSound].VolumeMultiplier = shipSpeedVolume;
                        float semitones = -ENGINES_PITCH_RANGE_HALF + ENGINES_PITCH_RANGE * shipSpeedRatio;
                        m_emitters[(int)ShipEmitters.MainSound].Sound.FrequencyRatio = MyAudio.Static.SemitonesToFrequencyRatio(semitones);
                    }

                    //thruster base volume ratio
                    if (shipSpeedSquered <= THRUSTER_RATIO_SPEED_THRESHOLD_1_SQ)
                    {
                        shipThrusterRatio = THRUSTER_RATIO_VOLUME_1 * shipSpeedSquered / THRUSTER_RATIO_SPEED_THRESHOLD_1_SQ;
                    }
                    else if (shipSpeedSquered <= THRUSTER_RATIO_SPEED_THRESHOLD_2_SQ)
                    {
                        shipThrusterRatio = THRUSTER_RATIO_VOLUME_1 + THRUSTER_RATIO_VOLUME_2_DIFF * (shipSpeedSquered - THRUSTER_RATIO_SPEED_THRESHOLD_1_SQ) / THRUSTER_RATIO_SPEED_THRESHOLD_2_DIFF;
                    }
                    else if (shipSpeedSquered <= THRUSTER_RATIO_SPEED_THRESHOLD_3_SQ)
                    {
                        shipThrusterRatio = THRUSTER_RATIO_VOLUME_2 + THRUSTER_RATIO_VOLUME_3_DIFF * (shipSpeedSquered - THRUSTER_RATIO_SPEED_THRESHOLD_2_SQ) / THRUSTER_RATIO_SPEED_THRESHOLD_3_DIFF;
                    }
                    else
                    {
                        shipThrusterRatio = THRUSTER_RATIO_VOLUME_3 + THRUSTER_RATIO_VOLUME_4_DIFF * (shipSpeedSquered - THRUSTER_RATIO_SPEED_THRESHOLD_3_SQ) / THRUSTER_RATIO_SPEED_THRESHOLD_4_DIFF;
                    }
                    shipThrusterRatio     = Math.Max(Math.Min(shipThrusterRatio, 1f) - m_wheelVolumeModifier * WHEELS_LOWER_THRUSTERS_BY_RATIO, 0f);
                    shipThrusterIdleRatio = MyMath.Clamp(1.2f - shipThrusterRatio * 3f, 0f, 1f);

                    //large ship special emitters
                    m_emitters[(int)ShipEmitters.ShipEngine].VolumeMultiplier = Math.Max(0f, shipSpeedVolume - shipThrusterIdleRatio);
                    if (m_shipGrid.GridSizeEnum == MyCubeSize.Large)
                    {
                        m_emitters[(int)ShipEmitters.LargeShipIdle].VolumeMultiplier = shipThrusterIdleRatio * m_shipEngineModifier;
                    }

                    //ion thruster run/idle sounds volumes + pitch
                    float thrusterPitch = MyAudio.Static.SemitonesToFrequencyRatio(-ENGINES_PITCH_RANGE_HALF + ENGINES_PITCH_RANGE * shipThrusterRatio);
                    if (m_emitters[(int)ShipEmitters.IonThrusters].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.IonThrusters].VolumeMultiplier     = shipThrusterRatio * m_shipEngineModifier * m_thrusterVolumes[(int)ShipThrusters.Ion];
                        m_emitters[(int)ShipEmitters.IonThrusters].Sound.FrequencyRatio = thrusterPitch;
                    }
                    if (m_emitters[(int)ShipEmitters.IonThrustersIdle].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.IonThrustersIdle].VolumeMultiplier = shipThrusterIdleRatio * m_shipEngineModifier * m_thrusterVolumes[(int)ShipThrusters.Ion];
                    }

                    //hydrogen thruster run/idle sounds volumes + pitch
                    if (m_emitters[(int)ShipEmitters.HydrogenThrusters].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.HydrogenThrusters].VolumeMultiplier     = shipThrusterRatio * m_shipEngineModifier * m_thrusterVolumes[(int)ShipThrusters.Hydrogen];
                        m_emitters[(int)ShipEmitters.HydrogenThrusters].Sound.FrequencyRatio = thrusterPitch;
                    }
                    if (m_emitters[(int)ShipEmitters.HydrogenThrustersIdle].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.HydrogenThrustersIdle].VolumeMultiplier = shipThrusterIdleRatio * m_shipEngineModifier * m_thrusterVolumes[(int)ShipThrusters.Hydrogen];
                    }

                    //atmospheric thruster run/idle sounds volumes + pitch
                    if (m_emitters[(int)ShipEmitters.AtmosphericThrusters].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.AtmosphericThrusters].VolumeMultiplier     = shipThrusterRatio * m_shipEngineModifier * m_thrusterVolumes[(int)ShipThrusters.Atmospheric];
                        m_emitters[(int)ShipEmitters.AtmosphericThrusters].Sound.FrequencyRatio = thrusterPitch;
                    }
                    if (m_emitters[(int)ShipEmitters.AtmosphericThrustersIdle].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.AtmosphericThrustersIdle].VolumeMultiplier = shipThrusterIdleRatio * m_shipEngineModifier * m_thrusterVolumes[(int)ShipThrusters.Atmospheric];
                    }

                    //wheels volume + pitch
                    if (m_emitters[(int)ShipEmitters.WheelsMain].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.WheelsMain].VolumeMultiplier      = shipThrusterRatio * m_shipEngineModifier * m_wheelVolumeModifier;
                        m_emitters[(int)ShipEmitters.WheelsMain].Sound.FrequencyRatio  = thrusterPitch;
                        m_emitters[(int)ShipEmitters.WheelsSecondary].VolumeMultiplier = (WHEELS_GROUND_MIN_VOLUME + (1f - WHEELS_GROUND_MIN_VOLUME) * shipThrusterRatio) * m_shipEngineModifier * m_wheelVolumeModifier;
                    }

                    //speed up/down sounds
                    if (m_speedChange >= 20 && m_timers[(int)ShipTimers.SpeedUp] <= 0f && m_wheelVolumeModifier <= 0f)
                    {
                        m_timers[(int)ShipTimers.SpeedUp] = (m_shipGrid.GridSizeEnum == MyCubeSize.Large ? 8f : 1f);
                        if (m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSounds.EnginesSpeedDown)))
                        {
                            m_emitters[(int)ShipEmitters.SingleSounds].StopSound(false);
                        }
                        PlayShipSound(ShipEmitters.SingleSounds, ShipSounds.EnginesSpeedUp, false, false);
                    }
                    else if (m_speedChange <= 15 && m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSounds.EnginesSpeedUp)))
                    {
                        m_emitters[(int)ShipEmitters.SingleSounds].StopSound(false);
                    }
                    if (m_speedChange <= 10 && m_timers[(int)ShipTimers.SpeedDown] <= 0f && m_wheelVolumeModifier <= 0f)
                    {
                        m_timers[(int)ShipTimers.SpeedDown] = (m_shipGrid.GridSizeEnum == MyCubeSize.Large ? 8f : 1f);
                        if (m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSounds.EnginesSpeedUp)))
                        {
                            m_emitters[(int)ShipEmitters.SingleSounds].StopSound(false);
                        }
                        PlayShipSound(ShipEmitters.SingleSounds, ShipSounds.EnginesSpeedDown, false, false);
                    }
                    else if (m_speedChange >= 15 && m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSounds.EnginesSpeedDown)))
                    {
                        m_emitters[(int)ShipEmitters.SingleSounds].StopSound(false);
                    }
                }
                else
                {
                    if (m_shipEngineModifier > 0f)
                    {
                        m_shipEngineModifier = Math.Max(0f, m_shipEngineModifier - MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS / ENGINES_TIME_TURN_OFF);
                    }
                }

                if (m_shipThrusters != null && m_shipThrusters.ThrustCount <= 0)
                {
                    m_shipThrusters = null;
                }

                //speed up / speed down variable
                if (Math.Abs(shipSpeedSquered - m_lastFrameShipSpeed) > 0.5f && shipSpeedSquered >= 9f)
                {
                    m_speedChange = (int)MyMath.Clamp(m_speedChange + (shipSpeedSquered > m_lastFrameShipSpeed ? 1 : -1), 0, 30);
                }
                else if (m_speedChange != 15)
                {
                    m_speedChange += m_speedChange > 15 ? -1 : 1;
                }

                //speed up / speed down timers
                if (shipSpeedSquered >= m_lastFrameShipSpeed && m_timers[(int)ShipTimers.SpeedDown] > 0f)
                {
                    m_timers[(int)ShipTimers.SpeedDown] -= MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS;
                }
                if (shipSpeedSquered <= m_lastFrameShipSpeed && m_timers[(int)ShipTimers.SpeedUp] > 0f)
                {
                    m_timers[(int)ShipTimers.SpeedUp] -= MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS;
                }

                m_lastFrameShipSpeed = shipSpeedSquered;
            }
        }
Beispiel #2
0
        public void Update()
        {
            if (m_initialized && m_shipGrid.Physics != null && m_shipGrid.IsStatic == false && (m_shipThrusters != null || m_shipWheels != null) && m_distanceToShip < m_definition.MaxUpdateRange_sq && m_groupData != null)
            {
                //calculate current ship state
                m_driving = m_shipWheels.HasWorkingWheels(true);
                float shipSpeed = m_driving == false?m_shipGrid.Physics.LinearVelocity.Length() : (m_shipGrid.Physics.LinearVelocity * m_groupData.WheelsSpeedCompensation).Length();

                ShipStateEnum lastState = m_shipState;
                if (m_shipGrid.GridSystems.ResourceDistributor.ResourceState == MyResourceStateEnum.NoPower || m_isDebris ||
                    ((m_shipThrusters == null || m_shipThrusters.ThrustCount <= 0) && (m_shipWheels == null || m_shipWheels.WheelCount <= 0)))
                {
                    m_shipState = ShipStateEnum.NoPower;
                }
                else
                {
                    if (shipSpeed < m_definition.SpeedThreshold1)
                    {
                        m_shipState = ShipStateEnum.Slow;
                    }
                    else if (shipSpeed < m_definition.SpeedThreshold2)
                    {
                        m_shipState = ShipStateEnum.Medium;
                    }
                    else
                    {
                        m_shipState = ShipStateEnum.Fast;
                    }
                }

                //in first person change
                bool orig = m_shouldPlay2D;
                if (m_shipGrid.GridSizeEnum == MyCubeSize.Large)
                {
                    m_shouldPlay2D = m_insideShip;
                }
                else if (MySession.Static.ControlledEntity != null && MySession.Static.IsCameraUserControlledSpectator() == false && MySession.Static.ControlledEntity.Entity != null && MySession.Static.ControlledEntity.Entity.Parent == m_shipGrid)
                {
                    m_shouldPlay2D = ((MySession.Static.ControlledEntity.Entity is MyCockpit) && (MySession.Static.ControlledEntity.Entity as MyCockpit).IsInFirstPersonView) ||
                                     (MySession.Static.CameraController is MyCameraBlock && (MySession.Static.CameraController as MyCameraBlock).Parent == m_shipGrid);
                }
                else
                {
                    m_shouldPlay2D = false;
                }

                m_shouldPlay2DChanged = (orig != m_shouldPlay2D);

                //thruster volume corrections
                for (int i = 0; i < m_thrusterVolumes.Length; i++)
                {
                    if (m_thrusterVolumes[i] < m_thrusterVolumeTargets[i])
                    {
                        m_thrusterVolumes[i] = Math.Min(m_thrusterVolumes[i] + m_groupData.ThrusterCompositionChangeSpeed, m_thrusterVolumeTargets[i]);
                    }
                    else if (m_thrusterVolumes[i] > m_thrusterVolumeTargets[i])
                    {
                        m_thrusterVolumes[i] = Math.Max(m_thrusterVolumes[i] - m_groupData.ThrusterCompositionChangeSpeed, m_thrusterVolumeTargets[i]);
                    }
                }

                if (m_driving)
                {
                    m_wheelVolumeModifier = Math.Min(m_wheelVolumeModifier + 0.04f, 1f);
                }
                else
                {
                    m_wheelVolumeModifier = Math.Max(m_wheelVolumeModifier - 0.005f, 0f);
                }

                //play sounds if there was change in state, ship type or thruster composition
                if (m_shipState != lastState || m_categoryChange || m_forceSoundCheck)
                {
                    if (m_shipState == ShipStateEnum.NoPower)
                    {
                        if (m_shipState != lastState)
                        {
                            for (int i = 0; i < m_emitters.Length; i++)
                            {
                                m_emitters[i].StopSound(false);
                            }
                            m_emitters[(int)ShipEmitters.SingleSounds].VolumeMultiplier = 1f;
                            PlayShipSound(ShipEmitters.SingleSounds, ShipSystemSoundsEnum.EnginesEnd);
                        }
                    }
                    else
                    {
                        if (m_shipState == ShipStateEnum.Slow)
                        {
                            PlayShipSound(ShipEmitters.MainSound, ShipSystemSoundsEnum.MainLoopSlow);
                        }
                        else if (m_shipState == ShipStateEnum.Medium)
                        {
                            PlayShipSound(ShipEmitters.MainSound, ShipSystemSoundsEnum.MainLoopMedium);
                        }
                        else if (m_shipState == ShipStateEnum.Fast)
                        {
                            PlayShipSound(ShipEmitters.MainSound, ShipSystemSoundsEnum.MainLoopFast);
                        }

                        PlayShipSound(ShipEmitters.ShipEngine, ShipSystemSoundsEnum.ShipEngine);
                        if (m_shipGrid.GridSizeEnum == MyCubeSize.Large)
                        {
                            PlayShipSound(ShipEmitters.LargeShipIdle, ShipSystemSoundsEnum.ShipIdle);
                        }

                        if (m_thrusterVolumes[(int)ShipThrusters.Ion] > 0f)
                        {
                            PlayShipSound(ShipEmitters.IonThrusters, ShipSystemSoundsEnum.IonThrusters);
                            PlayShipSound(ShipEmitters.IonThrustersIdle, ShipSystemSoundsEnum.IonThrustersIdle);
                        }

                        if (m_thrusterVolumes[(int)ShipThrusters.Hydrogen] > 0f)
                        {
                            PlayShipSound(ShipEmitters.HydrogenThrusters, ShipSystemSoundsEnum.HydrogenThrusters);
                            PlayShipSound(ShipEmitters.HydrogenThrustersIdle, ShipSystemSoundsEnum.HydrogenThrustersIdle);
                        }

                        if (m_thrusterVolumes[(int)ShipThrusters.Atmospheric] > 0f)
                        {
                            if (m_shipState == ShipStateEnum.Slow)
                            {
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSystemSoundsEnum.AtmoThrustersSlow, useFadeOut: true);
                            }
                            else if (m_shipState == ShipStateEnum.Medium)
                            {
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSystemSoundsEnum.AtmoThrustersMedium, useFadeOut: true);
                            }
                            else if (m_shipState == ShipStateEnum.Fast)
                            {
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSystemSoundsEnum.AtmoThrustersFast, useFadeOut: true);
                            }
                            PlayShipSound(ShipEmitters.AtmosphericThrustersIdle, ShipSystemSoundsEnum.AtmoThrustersIdle);
                        }

                        if (m_shipWheels.WheelCount > 0)
                        {
                            PlayShipSound(ShipEmitters.WheelsMain, ShipSystemSoundsEnum.WheelsEngineRun);
                            PlayShipSound(ShipEmitters.WheelsSecondary, ShipSystemSoundsEnum.WheelsSecondary);
                        }

                        if (lastState == ShipStateEnum.NoPower)
                        {
                            m_emitters[(int)ShipEmitters.SingleSounds].VolumeMultiplier = 1f;
                            PlayShipSound(ShipEmitters.SingleSounds, ShipSystemSoundsEnum.EnginesStart);
                        }
                    }
                    m_categoryChange  = false;
                    m_forceSoundCheck = false;
                }

                //there was change in camera - sound should change from 2d to 3d or vice versa
                if (m_shouldPlay2DChanged)
                {
                    for (int i = 0; i < m_emitters.Length; i++)
                    {
                        m_emitters[i].Force2D = m_shouldPlay2D;
                        m_emitters[i].Force3D = !m_shouldPlay2D;
                        if (m_emitters[i].IsPlaying && m_emitters[i].Plays2D != m_shouldPlay2D && m_emitters[i].Loop)
                        {
                            m_emitters[i].StopSound(true);
                            m_emitters[i].PlaySound(m_emitters[i].SoundPair, true, true, m_shouldPlay2D);
                        }
                    }
                    m_shouldPlay2DChanged = false;
                }

                //update emitter volumes
                if (m_shipState != ShipStateEnum.NoPower)
                {
                    if (m_shipEngineModifier < 1f)
                    {
                        m_shipEngineModifier = Math.Min(1f, m_shipEngineModifier + MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS / m_groupData.EngineTimeToTurnOn);
                    }
                    float shipSpeedRatio  = Math.Min(shipSpeed / m_definition.FullSpeed, 1f);
                    float shipSpeedVolume = CalculateVolumeFromSpeed(shipSpeedRatio, ref m_groupData.EngineVolumes) * m_shipEngineModifier * m_singleSoundsModifier;
                    float shipThrusterRatio;
                    float shipThrusterIdleRatio = 1f;

                    //main sound - engines
                    if (m_emitters[(int)ShipEmitters.MainSound].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.MainSound].VolumeMultiplier = shipSpeedVolume;
                        float semitones = m_groupData.EnginePitchRangeInSemitones_h + m_groupData.EnginePitchRangeInSemitones * shipSpeedRatio;
                        m_emitters[(int)ShipEmitters.MainSound].Sound.FrequencyRatio = MyAudio.Static.SemitonesToFrequencyRatio(semitones);
                    }

                    //thruster base volume ratio
                    shipThrusterRatio     = CalculateVolumeFromSpeed(shipSpeedRatio, ref m_groupData.ThrusterVolumes);
                    shipThrusterRatio     = Math.Max(Math.Min(shipThrusterRatio, 1f) - m_wheelVolumeModifier * m_groupData.WheelsLowerThrusterVolumeBy, 0f);
                    shipThrusterIdleRatio = MyMath.Clamp(1.2f - shipThrusterRatio * 3f, 0f, 1f) * m_shipEngineModifier * m_singleSoundsModifier;
                    shipThrusterRatio    *= m_shipEngineModifier * m_singleSoundsModifier;

                    //large ship special emitters
                    m_emitters[(int)ShipEmitters.ShipEngine].VolumeMultiplier    = Math.Max(0f, shipSpeedVolume - shipThrusterIdleRatio);
                    m_emitters[(int)ShipEmitters.LargeShipIdle].VolumeMultiplier = shipThrusterIdleRatio * m_shipEngineModifier * m_singleSoundsModifier;

                    //ion thruster run/idle sounds volumes + pitch
                    float thrusterPitch = MyAudio.Static.SemitonesToFrequencyRatio(m_groupData.ThrusterPitchRangeInSemitones_h + m_groupData.ThrusterPitchRangeInSemitones * shipThrusterRatio);
                    if (m_emitters[(int)ShipEmitters.IonThrusters].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.IonThrusters].VolumeMultiplier     = shipThrusterRatio * m_thrusterVolumes[(int)ShipThrusters.Ion];
                        m_emitters[(int)ShipEmitters.IonThrusters].Sound.FrequencyRatio = thrusterPitch;
                    }
                    if (m_emitters[(int)ShipEmitters.IonThrustersIdle].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.IonThrustersIdle].VolumeMultiplier = shipThrusterIdleRatio * m_thrusterVolumes[(int)ShipThrusters.Ion];
                    }

                    //hydrogen thruster run/idle sounds volumes + pitch
                    if (m_emitters[(int)ShipEmitters.HydrogenThrusters].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.HydrogenThrusters].VolumeMultiplier     = shipThrusterRatio * m_thrusterVolumes[(int)ShipThrusters.Hydrogen];
                        m_emitters[(int)ShipEmitters.HydrogenThrusters].Sound.FrequencyRatio = thrusterPitch;
                    }
                    if (m_emitters[(int)ShipEmitters.HydrogenThrustersIdle].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.HydrogenThrustersIdle].VolumeMultiplier = shipThrusterIdleRatio * m_thrusterVolumes[(int)ShipThrusters.Hydrogen];
                    }

                    //atmospheric thruster run/idle sounds volumes + pitch
                    if (m_emitters[(int)ShipEmitters.AtmosphericThrusters].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.AtmosphericThrusters].VolumeMultiplier     = shipThrusterRatio * m_thrusterVolumes[(int)ShipThrusters.Atmospheric];
                        m_emitters[(int)ShipEmitters.AtmosphericThrusters].Sound.FrequencyRatio = thrusterPitch;
                    }
                    if (m_emitters[(int)ShipEmitters.AtmosphericThrustersIdle].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.AtmosphericThrustersIdle].VolumeMultiplier = shipThrusterIdleRatio * m_thrusterVolumes[(int)ShipThrusters.Atmospheric];
                    }

                    //wheels volume + pitch
                    if (m_emitters[(int)ShipEmitters.WheelsMain].IsPlaying)
                    {
                        m_emitters[(int)ShipEmitters.WheelsMain].VolumeMultiplier      = shipThrusterRatio * m_wheelVolumeModifier;
                        m_emitters[(int)ShipEmitters.WheelsMain].Sound.FrequencyRatio  = thrusterPitch;
                        m_emitters[(int)ShipEmitters.WheelsSecondary].VolumeMultiplier = CalculateVolumeFromSpeed(shipSpeedRatio, ref m_groupData.WheelsVolumes) * m_shipEngineModifier * m_wheelVolumeModifier * m_singleSoundsModifier;
                    }

                    //speed up/down sounds
                    float speedUpDownVolume = 0.5f + shipThrusterRatio / 2f;
                    m_playingSpeedUpOrDown = m_playingSpeedUpOrDown && m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying;

                    //speed up
                    if (m_speedChange >= 20 && m_timers[(int)ShipTimers.SpeedUp] <= 0f && m_wheelVolumeModifier <= 0f)
                    {
                        m_timers[(int)ShipTimers.SpeedUp] = (m_shipGrid.GridSizeEnum == MyCubeSize.Large ? 8f : 1f);
                        if (m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSystemSoundsEnum.EnginesSpeedDown)))
                        {
                            FadeOutSound(duration: 1000);
                        }
                        m_emitters[(int)ShipEmitters.SingleSounds].VolumeMultiplier = speedUpDownVolume;
                        PlayShipSound(ShipEmitters.SingleSounds, ShipSystemSoundsEnum.EnginesSpeedUp, false, false);
                        m_playingSpeedUpOrDown = true;
                    }
                    else if (m_speedChange <= 15 && m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSystemSoundsEnum.EnginesSpeedUp)))
                    {
                        FadeOutSound(duration: 1000);
                    }

                    //speed down
                    if (m_speedChange <= 10 && m_timers[(int)ShipTimers.SpeedDown] <= 0f && m_wheelVolumeModifier <= 0f)
                    {
                        m_timers[(int)ShipTimers.SpeedDown] = (m_shipGrid.GridSizeEnum == MyCubeSize.Large ? 8f : 2f);
                        if (m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSystemSoundsEnum.EnginesSpeedUp)))
                        {
                            FadeOutSound(duration: 1000);
                        }
                        m_emitters[(int)ShipEmitters.SingleSounds].VolumeMultiplier = speedUpDownVolume;
                        PlayShipSound(ShipEmitters.SingleSounds, ShipSystemSoundsEnum.EnginesSpeedDown, false, false);
                        m_playingSpeedUpOrDown = true;
                    }
                    else if (m_speedChange >= 15 && m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSystemSoundsEnum.EnginesSpeedDown)))
                    {
                        FadeOutSound(duration: 1000);
                    }

                    //volume change for all sound if speed up/down is playing
                    float singleSoundVolumeTarget = 1f;
                    if (m_playingSpeedUpOrDown && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSystemSoundsEnum.EnginesSpeedDown)))
                    {
                        singleSoundVolumeTarget = m_groupData.SpeedDownSoundChangeVolumeTo;
                    }
                    if (m_playingSpeedUpOrDown && m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSystemSoundsEnum.EnginesSpeedUp)))
                    {
                        singleSoundVolumeTarget = m_groupData.SpeedUpSoundChangeVolumeTo;
                    }
                    if (m_singleSoundsModifier < singleSoundVolumeTarget)
                    {
                        m_singleSoundsModifier = Math.Min(m_singleSoundsModifier + m_groupData.SpeedUpDownChangeSpeed, singleSoundVolumeTarget);
                    }
                    else if (m_singleSoundsModifier > singleSoundVolumeTarget)
                    {
                        m_singleSoundsModifier = Math.Max(m_singleSoundsModifier - m_groupData.SpeedUpDownChangeSpeed, singleSoundVolumeTarget);
                    }

                    //speed down volume
                    if (m_emitters[(int)ShipEmitters.SingleSounds].IsPlaying && (m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSystemSoundsEnum.EnginesSpeedDown)) || m_emitters[(int)ShipEmitters.SingleSounds].SoundPair.Equals(GetShipSound(ShipSystemSoundsEnum.EnginesSpeedUp))))
                    {
                        m_emitters[(int)ShipEmitters.SingleSounds].VolumeMultiplier = speedUpDownVolume;
                    }
                }
                else
                {
                    if (m_shipEngineModifier > 0f)
                    {
                        m_shipEngineModifier = Math.Max(0f, m_shipEngineModifier - MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS / m_groupData.EngineTimeToTurnOff);
                    }
                }

                if (m_shipThrusters != null && m_shipThrusters.ThrustCount <= 0)
                {
                    m_shipThrusters = null;
                }

                //speed up / speed down variable
                if (Math.Abs(shipSpeed - m_lastFrameShipSpeed) > 0.01f && shipSpeed >= 3f)
                {
                    m_speedChange = (int)MyMath.Clamp(m_speedChange + (shipSpeed > m_lastFrameShipSpeed ? 1 : -1), 0, 30);
                }
                else if (m_speedChange != 15)
                {
                    m_speedChange += m_speedChange > 15 ? -1 : 1;
                }

                //speed up / speed down timers
                if (shipSpeed >= m_lastFrameShipSpeed && m_timers[(int)ShipTimers.SpeedDown] > 0f)
                {
                    m_timers[(int)ShipTimers.SpeedDown] -= MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS;
                }
                if (shipSpeed <= m_lastFrameShipSpeed && m_timers[(int)ShipTimers.SpeedUp] > 0f)
                {
                    m_timers[(int)ShipTimers.SpeedUp] -= MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS;
                }

                m_lastFrameShipSpeed = shipSpeed;
            }
        }
        //thruster composition + ship category
        private void UpdateCategory()
        {
            if (m_initialized && m_shipGrid != null && m_shipGrid.Physics != null && m_shipGrid.IsStatic == false && m_definition != null)
            {
                if (m_distanceToShip < m_definition.MaxUpdateRange_sq)
                {
                    if (m_shipThrusters == null)
                    {
                        m_shipThrusters = m_shipGrid.Components.Get <MyEntityThrustComponent>();
                    }
                    if (m_shipWheels == null)
                    {
                        m_shipWheels = m_shipGrid.GridSystems.WheelSystem;
                    }

                    CalculateShipCategory();
                    if (m_isDebris == false && m_shipState != ShipStateEnum.NoPower && (m_singleThrusterTypeShip == false || ShipHasChanged ||
                                                                                        m_shipThrusters == null || m_shipThrusters.FinalThrust == Vector3.Zero || (m_shipWheels != null && m_shipWheels.HasWorkingWheels(false))))
                    {
                        CalculateThrusterComposition();
                    }

                    if (m_shipSoundSource == null)
                    {
                        m_shipSoundSource = m_shipGrid;
                    }
                    if (m_shipGrid.MainCockpit != null && m_shipGrid.GridSizeEnum == MyCubeSize.Small)
                    {
                        m_shipSoundSource = m_shipGrid.MainCockpit;
                    }

                    if (m_shipGrid.GridSizeEnum == MyCubeSize.Large && MySession.Static != null && MySession.Static.LocalCharacter != null)
                    {
                        if (MySession.Static.Settings.RealisticSound == false ||
                            (MySession.Static.LocalCharacter.AtmosphereDetectorComp != null && (MySession.Static.LocalCharacter.AtmosphereDetectorComp.InAtmosphere || MySession.Static.LocalCharacter.AtmosphereDetectorComp.InShipOrStation)))
                        {
                            BoundingSphereD playerSphere = new BoundingSphereD(MySession.Static.LocalCharacter.PositionComp.GetPosition(), m_definition.LargeShipDetectionRadius);
                            m_shipGrid.GetBlocksInsideSphere(ref playerSphere, m_detectedBlocks);
                            m_insideShip = m_detectedBlocks.Count > 0;
                        }
                        else
                        {
                            m_insideShip = false;
                        }
                    }
                }
            }
        }
Beispiel #4
0
        public void Update100()
        {
            //thruster composition + ship category
            m_distanceToShip = m_initialized && m_shipGrid.Physics != null ? (m_shouldPlay2D ? 0 : (float)Vector3D.DistanceSquared(MySector.MainCamera.Position, m_shipGrid.PositionComp.GetPosition())) : float.MaxValue;
            if (m_initialized && m_shipGrid.Physics != null && m_shipGrid.IsStatic == false)
            {
                if (m_distanceToShip < m_definition.MaxUpdateRange_sq)
                {
                    if (m_shipThrusters == null)
                    {
                        m_shipThrusters = m_shipGrid.Components.Get <MyEntityThrustComponent>();
                    }
                    if (m_shipWheels == null)
                    {
                        m_shipWheels = m_shipGrid.GridSystems.WheelSystem;
                    }

                    CalculateShipCategory();
                    if (m_isDebris == false && m_shipState != ShipStateEnum.NoPower && (m_singleThrusterTypeShip == false || ShipHasChanged ||
                                                                                        m_shipThrusters == null || m_shipThrusters.FinalThrust == Vector3.Zero || m_shipWheels.HasWorkingWheels(false)))
                    {
                        CalculateThrusterComposition();
                    }

                    if (m_shipSoundSource == null)
                    {
                        m_shipSoundSource = m_shipGrid;
                    }
                    if (m_shipGrid.MainCockpit != null && m_shipGrid.GridSizeEnum == MyCubeSize.Small)
                    {
                        m_shipSoundSource = m_shipGrid.MainCockpit;
                    }

                    if (m_shipGrid.GridSizeEnum == MyCubeSize.Large && MySession.Static.LocalCharacter != null)
                    {
                        BoundingSphereD playerSphere = new BoundingSphereD(MySession.Static.LocalCharacter.PositionComp.GetPosition(), m_definition.LargeShipDetectionRadius);
                        m_shipGrid.GetBlocksInsideSphere(ref playerSphere, m_detectedBlocks);
                        m_insideShip = m_detectedBlocks.Count > 0;
                    }
                }
            }

            //sound emitter update
            for (int i = 0; i < m_emitters.Length; i++)
            {
                m_emitters[i].Entity = m_shipSoundSource;
                m_emitters[i].Update();
            }

            //wheel contact point callbacks
            if (m_shipGrid.Physics != null && m_shipWheels != null && m_shipWheels.WheelCount > 0)
            {
                bool start = m_distanceToShip < m_definition.WheelsCallbackRangeCreate_sq && m_isDebris == false;
                bool stop  = m_distanceToShip > m_definition.WheelsCallbackRangeRemove_sq || m_isDebris;
                if ((start || stop) && (m_lastWheelUpdateStart != start || m_lastWheelUpdateStop != stop))
                {
                    foreach (var motor in m_shipWheels.Wheels)
                    {
                        if (motor.RotorGrid == null || motor.RotorGrid.Physics == null)
                        {
                            continue;
                        }
                        if (motor.RotorGrid.HasShipSoundEvents == false && start)
                        {
                            motor.RotorGrid.Physics.RigidBody.ContactPointCallback += RigidBody_ContactPointCallback;
                            motor.RotorGrid.Physics.RigidBody.CallbackLimit         = 1;
                            motor.RotorGrid.OnClosing         += RotorGrid_OnClosing;
                            motor.RotorGrid.HasShipSoundEvents = true;
                        }
                        else if (motor.RotorGrid.HasShipSoundEvents && stop)
                        {
                            motor.RotorGrid.HasShipSoundEvents = false;
                            motor.RotorGrid.Physics.RigidBody.ContactPointCallback -= RigidBody_ContactPointCallback;
                            motor.RotorGrid.OnClosing -= RotorGrid_OnClosing;
                        }
                    }
                    m_lastWheelUpdateStart = start;
                    m_lastWheelUpdateStop  = stop;
                    if (start && m_shipWheelsAction == false)
                    {
                        m_shipWheels.OnMotorUnregister += m_shipWheels_OnMotorUnregister;
                        m_shipWheelsAction              = true;
                    }
                    else if (stop && m_shipWheelsAction)
                    {
                        m_shipWheels.OnMotorUnregister -= m_shipWheels_OnMotorUnregister;
                        m_shipWheelsAction              = false;
                    }
                }
            }
        }