public bool InitComponent(MyCubeGrid shipGrid)
        {
            if (shipGrid.GridSizeEnum == MyCubeSize.Small && MyFakes.ENABLE_NEW_SMALL_SHIP_SOUNDS == false)
            {
                return(false);
            }
            if (shipGrid.GridSizeEnum == MyCubeSize.Large && MyFakes.ENABLE_NEW_LARGE_SHIP_SOUNDS == false)
            {
                return(false);
            }
            m_shipGrid      = shipGrid;
            m_shipThrusters = m_shipGrid.Components.Get <MyEntityThrustComponent>();
            m_shipWheels    = m_shipGrid.GridSystems.WheelSystem;

            m_thrusterVolumes       = new float[(Enum.GetNames(typeof(ShipThrusters)).Length)];
            m_thrusterVolumeTargets = new float[(Enum.GetNames(typeof(ShipThrusters)).Length)];
            for (int i = 1; i < m_thrusterVolumes.Length; i++)
            {
                m_thrusterVolumes[i]       = 0f;
                m_thrusterVolumeTargets[i] = 0f;
            }
            m_thrusterVolumes[0]       = 1f;
            m_thrusterVolumeTargets[0] = 1f;

            for (int i = 0; i < m_emitters.Length; i++)
            {
                m_emitters[i]         = new MyEntity3DSoundEmitter(m_shipGrid, true);
                m_emitters[i].Force2D = m_shouldPlay2D;
                m_emitters[i].Force3D = !m_shouldPlay2D;
            }

            m_initialized = true;

            return(true);
        }
예제 #2
0
        public virtual void Init(MyObjectBuilder_CubeGrid builder)
        {
            MyEntityThrustComponent component = this.CubeGrid.Components.Get <MyEntityThrustComponent>();

            if (component != null)
            {
                component.DampenersEnabled = builder.DampenersEnabled;
            }
            if (this.WheelSystem != null)
            {
                this.m_cubeGrid.SetHandbrakeRequest(builder.Handbrake);
            }
            if (((this.GasSystem != null) && MySession.Static.Settings.EnableOxygen) && MySession.Static.Settings.EnableOxygenPressurization)
            {
                this.GasSystem.Init(builder.OxygenRooms);
            }
            if ((this.ShipSoundComponent != null) && !this.ShipSoundComponent.InitComponent(this.m_cubeGrid))
            {
                this.ShipSoundComponent.DestroyComponent();
                this.ShipSoundComponent = null;
            }
            if (MyPerGameSettings.EnableJumpDrive)
            {
                this.JumpSystem.Init(builder.JumpDriveDirection, builder.JumpRemainingTime);
            }
            MyEntityThrustComponent component2 = this.CubeGrid.Components.Get <MyEntityThrustComponent>();

            if (component2 != null)
            {
                component2.MergeAllGroupsDirty();
            }
        }
예제 #3
0
 public void DestroyComponent()
 {
     for (int i = 0; i < m_emitters.Length; i++)
     {
         if (m_emitters[i] != null)
         {
             m_emitters[i].StopSound(true);
             m_emitters[i] = null;
         }
     }
     m_shipGrid      = null;
     m_shipThrusters = null;
     m_shipWheels    = null;
 }
        //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;
                        }
                    }
                }
            }
        }
예제 #5
0
        public override void OnRegisteredToGridSystems()
        {
            base.OnRegisteredToGridSystems();

            MyEntityThrustComponent entityThrustComponent;

            if (!CubeGrid.Components.TryGet(out entityThrustComponent))
            {
                entityThrustComponent = new MyThrusterBlockThrustComponent();

                entityThrustComponent.Init();
                CubeGrid.Components.Add <MyEntityThrustComponent>(entityThrustComponent);
            }
            m_thrustComponent = entityThrustComponent;
            m_thrustComponent.Register(this, ThrustForwardVector, OnRegisteredToThrustComponent);
        }
예제 #6
0
        public void Update100()
        {
            m_distanceToShip = m_initialized && m_shipGrid.Physics != null ? (m_shouldPlay2D ? 0 : (float)Vector3D.DistanceSquared(MySector.MainCamera.Position, m_shipGrid.PositionComp.GetPosition())) : MAX_UPDATE_RANGE_SQ + 1;
            if (m_initialized && m_shipGrid.Physics != null && m_shipGrid.IsStatic == false && m_distanceToShip < MAX_UPDATE_RANGE_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_shipCategory != ShipTypeEnum.Debris && 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(), LARGE_SHIP_DETECTION_RADIUS);
                    HashSet <MySlimBlock> detectedBlocks = new HashSet <MySlimBlock>();
                    m_shipGrid.GetBlocksInsideSphere(ref playerSphere, detectedBlocks);
                    m_insideShip = detectedBlocks.Count > 0;
                }
            }
            for (int i = 0; i < m_emitters.Length; i++)
            {
                m_emitters[i].Entity = m_shipSoundSource;
                m_emitters[i].Update();
            }
        }
예제 #7
0
        public virtual void GetObjectBuilder(MyObjectBuilder_CubeGrid ob)
        {
            MyEntityThrustComponent component = this.CubeGrid.Components.Get <MyEntityThrustComponent>();

            ob.DampenersEnabled = (component == null) || component.DampenersEnabled;
            this.ConveyorSystem.SerializeLines(ob.ConveyorLines);
            if (ob.ConveyorLines.Count == 0)
            {
                ob.ConveyorLines = null;
            }
            if (this.WheelSystem != null)
            {
                ob.Handbrake = this.WheelSystem.HandBrake;
            }
            if (((this.GasSystem != null) && MySession.Static.Settings.EnableOxygen) && MySession.Static.Settings.EnableOxygenPressurization)
            {
                ob.OxygenRooms = this.GasSystem.GetOxygenAmount();
            }
            if (MyPerGameSettings.EnableJumpDrive)
            {
                ob.JumpDriveDirection = this.JumpSystem.GetJumpDriveDirection();
                ob.JumpRemainingTime  = this.JumpSystem.GetRemainingJumpTime();
            }
        }
        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;
                    }
                }
            }
        }
 public void DestroyComponent()
 {
     for (int i = 0; i < m_emitters.Length; i++)
     {
         if (m_emitters[i] != null)
         {
             m_emitters[i].StopSound(true);
             m_emitters[i] = null;
         }
     }
     m_shipGrid = null;
     m_shipThrusters = null;
     m_shipWheels = null;
 }
        public bool InitComponent(MyCubeGrid shipGrid)
        {
            if (shipGrid.GridSizeEnum == MyCubeSize.Small && MyFakes.ENABLE_NEW_SMALL_SHIP_SOUNDS == false)
                return false;
            if (shipGrid.GridSizeEnum == MyCubeSize.Large && MyFakes.ENABLE_NEW_LARGE_SHIP_SOUNDS == false)
                return false;
            m_shipGrid = shipGrid;
            m_shipThrusters = m_shipGrid.Components.Get<MyEntityThrustComponent>();
            m_shipWheels = m_shipGrid.GridSystems.WheelSystem;

            m_thrusterVolumes = new float[(Enum.GetNames(typeof(ShipThrusters)).Length)];
            m_thrusterVolumeTargets = new float[(Enum.GetNames(typeof(ShipThrusters)).Length)];
            for (int i = 1; i < m_thrusterVolumes.Length; i++)
            {
                m_thrusterVolumes[i] = 0f;
                m_thrusterVolumeTargets[i] = 0f;
            }
            m_thrusterVolumes[0] = 1f;
            m_thrusterVolumeTargets[0] = 1f;

            for (int i = 0; i < m_emitters.Length; i++)
            {
                m_emitters[i] = new MyEntity3DSoundEmitter(m_shipGrid);
                m_emitters[i].Force2D = m_shouldPlay2D;
                m_emitters[i].Force3D = !m_shouldPlay2D;
            }

            m_initialized = true;

            return true;
        }
        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
                bool driving = ((DateTime.UtcNow - m_lastContactWithGround).TotalSeconds <= 0.2f);
                float shipSpeed = 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.ControlledEntity.Entity is MyRemoteControl) && MySession.Static.LocalCharacter != null
                            && MySession.Static.LocalCharacter.IsUsing is MyCockpit && (MySession.Static.LocalCharacter.IsUsing as MyCockpit).Parent == m_shipGrid)

                        || (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 (driving)
                {
                    m_wheelVolumeModifierEngine = Math.Min(m_wheelVolumeModifierEngine + 0.01f, 1f);
                    m_wheelVolumeModifierWheels = Math.Min(m_wheelVolumeModifierWheels + 0.03f, 1f);
                }
                else
                {
                    m_wheelVolumeModifierEngine = Math.Max(m_wheelVolumeModifierEngine - 0.005f, 0f);
                    m_wheelVolumeModifierWheels = Math.Max(m_wheelVolumeModifierWheels - 0.03f, 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);
                        PlayShipSound(ShipEmitters.ShipIdle, 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);
                            else if (m_shipState == ShipStateEnum.Medium)
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSystemSoundsEnum.AtmoThrustersMedium);
                            else if (m_shipState == ShipStateEnum.Fast)
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSystemSoundsEnum.AtmoThrustersFast);
                            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_wheelVolumeModifierEngine * 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.ShipIdle].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.MainSound].VolumeMultiplier = Math.Max(shipSpeedVolume - m_wheelVolumeModifierEngine * m_groupData.WheelsLowerThrusterVolumeBy, 0f);
                        m_emitters[(int)ShipEmitters.WheelsMain].VolumeMultiplier = shipThrusterRatio * m_wheelVolumeModifierEngine * m_singleSoundsModifier;
                        m_emitters[(int)ShipEmitters.WheelsMain].Sound.FrequencyRatio = thrusterPitch;
                        m_emitters[(int)ShipEmitters.WheelsSecondary].VolumeMultiplier = CalculateVolumeFromSpeed(shipSpeedRatio, ref m_groupData.WheelsVolumes) * m_shipEngineModifier * m_wheelVolumeModifierWheels * 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_wheelVolumeModifierEngine <= 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_wheelVolumeModifierEngine <= 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;
            }
        }
예제 #12
0
        public override void Init(MyObjectBuilder_CubeBlock objectBuilder, MyCubeGrid cubeGrid)
        {
            MyEntityThrustComponent entityThrustComponent;

            if (!cubeGrid.Components.TryGet(out entityThrustComponent))
            {
                entityThrustComponent = new MyThrusterBlockThrustComponent();

                entityThrustComponent.Init();
                cubeGrid.Components.Add <MyEntityThrustComponent>(entityThrustComponent);
            }

            m_thrustComponent = entityThrustComponent;

            BlockDefinition = (MyThrustDefinition)base.BlockDefinition;

            MyDefinitionId fuelId = new MyDefinitionId();

            if (!BlockDefinition.FuelConverter.FuelId.IsNull())
            {
                fuelId = BlockDefinition.FuelConverter.FuelId;
            }

            MyGasProperties fuelDef = null;

            if (MyFakes.ENABLE_HYDROGEN_FUEL)
            {
                MyDefinitionManager.Static.TryGetDefinition(fuelId, out fuelDef);
            }

            FuelDefinition = fuelDef ?? new MyGasProperties // Use electricity by default
            {
                Id            = MyResourceDistributorComponent.ElectricityId,
                EnergyDensity = 1f,
            };

            base.Init(objectBuilder, cubeGrid);



            var builder = (MyObjectBuilder_Thrust)objectBuilder;

            ThrustColor = BlockDefinition.FlameIdleColor;

            m_thrustOverride.Value = (builder.ThrustOverride * 100f) / BlockDefinition.ForceMagnitude;

            LoadDummies();
            NeedsUpdate |= MyEntityUpdateEnum.EACH_100TH_FRAME;

            m_light = MyLights.AddLight();
            m_light.ReflectorDirection = WorldMatrix.Forward;
            m_light.ReflectorUp        = WorldMatrix.Up;
            m_light.ReflectorRange     = CubeGrid.GridScale;
            m_light.Color          = ThrustColor;
            m_light.GlareMaterial  = BlockDefinition.FlameGlareMaterial;
            m_light.GlareQuerySize = BlockDefinition.FlameGlareQuerySize * CubeGrid.GridScale;

            m_glareSize = BlockDefinition.FlameGlareSize * CubeGrid.GridScale;
            m_maxBillboardDistanceSquared = BlockDefinition.FlameVisibilityDistance * BlockDefinition.FlameVisibilityDistance;
            m_maxLightDistanceSquared     = m_maxBillboardDistanceSquared;

            m_light.Start(MyLight.LightTypeEnum.PointLight, 1);

            UpdateDetailedInfo();

            FuelConverterDefinition = !MyFakes.ENABLE_HYDROGEN_FUEL ? new MyFuelConverterInfo {
                Efficiency = 1.0f
            } : BlockDefinition.FuelConverter;

            SlimBlock.ComponentStack.IsFunctionalChanged += ComponentStack_IsFunctionalChanged;
        }
        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
                bool  driving   = ((DateTime.UtcNow - m_lastContactWithGround).TotalSeconds <= 0.2f);
                float shipSpeed = driving == false?m_shipGrid.Physics.LinearVelocity.Length() : (m_shipGrid.Physics.LinearVelocity * m_groupData.WheelsSpeedCompensation).Length();

                float originalSpeed = Math.Min(shipSpeed / m_definition.FullSpeed, 1f);
                if (!MySandboxGame.Config.ShipSoundsAreBasedOnSpeed)
                {
                    shipSpeed = m_shipCurrentPower * m_definition.FullSpeed;
                }
                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;
                    }
                }

                //alternate speed calculation (based on acceleration/decceleration)
                if (!MySandboxGame.Config.ShipSoundsAreBasedOnSpeed)
                {
                    m_shipCurrentPowerTarget = 0f;
                    if (driving)
                    {
                        if (m_shipWheels != null && m_shipWheels.WheelCount > 0)
                        {
                            if (m_shipWheels.AngularVelocity.LengthSquared() >= 1f)
                            {
                                m_shipCurrentPowerTarget = 1f;//accelerating/deccelerating
                            }
                            else if (m_shipGrid.Physics.LinearVelocity.LengthSquared() > 5f)
                            {
                                m_shipCurrentPowerTarget = 0.33f;//cruising
                            }
                        }
                    }
                    else
                    {
                        if (m_shipThrusters != null)
                        {
                            if (m_shipThrusters.FinalThrust.LengthSquared() >= 100f)
                            {
                                m_shipCurrentPowerTarget = 1f;//accelerating/deccelerating
                            }
                            else if (m_shipGrid.Physics.Gravity != Vector3.Zero && m_shipThrusters.DampenersEnabled && m_shipGrid.Physics.LinearVelocity.LengthSquared() < 4f)
                            {
                                m_shipCurrentPowerTarget = 0.33f;//hovering
                            }
                            else
                            {
                                m_shipCurrentPowerTarget = 0;//complete stop
                            }
                        }
                    }

                    if (m_shipCurrentPower < m_shipCurrentPowerTarget)
                    {
                        m_shipCurrentPower = Math.Min(m_shipCurrentPower + POWER_CHANGE_SPEED_UP, m_shipCurrentPowerTarget);
                    }
                    else if (m_shipCurrentPower > m_shipCurrentPowerTarget)
                    {
                        m_shipCurrentPower = Math.Max(m_shipCurrentPower - POWER_CHANGE_SPEED_DOWN, m_shipCurrentPowerTarget);
                    }
                }

                //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.ControlledEntity.Entity is MyRemoteControl) && MySession.Static.LocalCharacter != null &&
                                         MySession.Static.LocalCharacter.IsUsing is MyCockpit && (MySession.Static.LocalCharacter.IsUsing as MyCockpit).Parent == m_shipGrid)

                                     || (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 (driving)
                {
                    m_wheelVolumeModifierEngine = Math.Min(m_wheelVolumeModifierEngine + 0.01f, 1f);
                    m_wheelVolumeModifierWheels = Math.Min(m_wheelVolumeModifierWheels + 0.03f, 1f);
                }
                else
                {
                    m_wheelVolumeModifierEngine = Math.Max(m_wheelVolumeModifierEngine - 0.005f, 0f);
                    m_wheelVolumeModifierWheels = Math.Max(m_wheelVolumeModifierWheels - 0.03f, 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);
                        PlayShipSound(ShipEmitters.ShipIdle, 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);
                            }
                            else if (m_shipState == ShipStateEnum.Medium)
                            {
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSystemSoundsEnum.AtmoThrustersMedium);
                            }
                            else if (m_shipState == ShipStateEnum.Fast)
                            {
                                PlayShipSound(ShipEmitters.AtmosphericThrusters, ShipSystemSoundsEnum.AtmoThrustersFast);
                            }
                            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_wheelVolumeModifierEngine * 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 = MySandboxGame.Config.ShipSoundsAreBasedOnSpeed ? Math.Max(0f, shipSpeedVolume - shipThrusterIdleRatio) : originalSpeed;
                    m_emitters[(int)ShipEmitters.ShipIdle].VolumeMultiplier   = (MySandboxGame.Config.ShipSoundsAreBasedOnSpeed ? shipThrusterIdleRatio : MyMath.Clamp(1.2f - originalSpeed * 3f, 0f, 1f)) * 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.MainSound].VolumeMultiplier       = Math.Max(shipSpeedVolume - m_wheelVolumeModifierEngine * m_groupData.WheelsLowerThrusterVolumeBy, 0f);
                        m_emitters[(int)ShipEmitters.WheelsMain].VolumeMultiplier      = shipThrusterRatio * m_wheelVolumeModifierEngine * m_singleSoundsModifier;
                        m_emitters[(int)ShipEmitters.WheelsMain].Sound.FrequencyRatio  = thrusterPitch;
                        m_emitters[(int)ShipEmitters.WheelsSecondary].VolumeMultiplier = CalculateVolumeFromSpeed(shipSpeedRatio, ref m_groupData.WheelsVolumes) * m_shipEngineModifier * m_wheelVolumeModifierWheels * 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_wheelVolumeModifierEngine <= 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_wheelVolumeModifierEngine <= 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;
            }
        }
예제 #14
0
        /// <summary>
        /// Apply the calculated force ratios to the controller.
        /// </summary>
        public void MoveAndRotate()
        {
            CheckGrid();

            //Log.DebugLog("moveForceRatio: " + moveForceRatio + ", rotateForceRatio: " + rotateForceRatio + ", move length: " + moveForceRatio.Length(), "MoveAndRotate()");

            // if all the force ratio values are 0, Autopilot has to stop the ship, MoveAndRotate will not
            if (m_moveForceRatio == Vector3.Zero && m_rotateTargetVelocity == Vector3.Zero)
            {
                //Log.DebugLog("Stopping the ship, move: " + m_moveForceRatio + ", rotate: " + m_rotateTargetVelocity);
                // should not toggle dampeners, grid may just have landed
                MoveAndRotateStop(false);
                return;
            }

            if (m_moveForceRatio != Vector3.Zero && CheckStuck(WriggleAfter))
            {
                ulong upWoMove = Globals.UpdateCount - m_lastAccel;

                if (m_rotateForceRatio != Vector3.Zero)
                {
                    // wriggle
                    float wriggle = (upWoMove - WriggleAfter) * 0.0001f;

                    //Log.DebugLog("wriggle: " + wriggle + ", updates w/o moving: " + upWoMove);

                    m_rotateForceRatio.X += (0.5f - (float)Globals.Random.NextDouble()) * wriggle;
                    m_rotateForceRatio.Y += (0.5f - (float)Globals.Random.NextDouble()) * wriggle;
                    m_rotateForceRatio.Z += (0.5f - (float)Globals.Random.NextDouble()) * wriggle;
                }

                // increase force
                m_moveForceRatio *= 1f + (upWoMove - WriggleAfter) * 0.1f;
            }

            // clamp values
            Vector3 moveControl;

            moveControl.X = MathHelper.Clamp(m_moveForceRatio.X, -1f, 1f);
            moveControl.Y = MathHelper.Clamp(m_moveForceRatio.Y, -1f, 1f);
            moveControl.Z = MathHelper.Clamp(m_moveForceRatio.Z, -1f, 1f);

            Vector3 rotateControl = -m_rotateForceRatio;             // control torque is opposite of move indicator

            Vector3.ClampToSphere(ref rotateControl, 1f);

            m_stopped = false;
            MyShipController controller = Block.Controller;

            MyAPIGateway.Utilities.TryInvokeOnGameThread(() => {
                //Log.DebugLog("rotate control: " + rotateControl + ", previous: " + m_prevRotateControl + ", delta: " + (rotateControl - m_prevRotateControl), "MoveAndRotate()");

                if (Block.Controller.GridGyroSystem != null)
                {
                    DirectionGrid gridRotate = ((DirectionBlock)rotateControl).ToGrid(Block.CubeBlock);
                    Block.Controller.GridGyroSystem.ControlTorque = gridRotate;
                }
                else
                {
                    Log.DebugLog("No gyro system");
                }

                MyEntityThrustComponent thrustComponent = Block.CubeGrid.Components.Get <MyEntityThrustComponent>();
                if (thrustComponent != null)
                {
                    DirectionGrid gridMove        = ((DirectionBlock)moveControl).ToGrid(Block.CubeBlock);
                    thrustComponent.ControlThrust = gridMove;
                }
                else
                {
                    Log.DebugLog("No thrust component");
                }
            });
        }
예제 #15
0
        public override void Init(MyObjectBuilder_CubeBlock objectBuilder, MyCubeGrid cubeGrid)
        {
            base.Init(objectBuilder, cubeGrid);

            BlockDefinition = (MyThrustDefinition)base.BlockDefinition;

            var builder = (MyObjectBuilder_Thrust)objectBuilder;

            ThrustColor = BlockDefinition.FlameIdleColor;

            ThrustOverride = builder.ThrustOverride;

            LoadDummies();

            m_light = MyLights.AddLight();
            m_light.ReflectorDirection = WorldMatrix.Forward;
            m_light.ReflectorUp = WorldMatrix.Up;
            m_light.ReflectorRange = 1;
            m_light.Color = ThrustColor;
            m_light.GlareMaterial = BlockDefinition.FlameGlareMaterial;
            m_light.GlareQuerySize = BlockDefinition.FlameGlareQuerySize;

            m_glareSize = BlockDefinition.FlameGlareSize;
            m_maxBillboardDistanceSquared = BlockDefinition.FlameVisibilityDistance*BlockDefinition.FlameVisibilityDistance;
            m_maxLightDistanceSquared = m_maxBillboardDistanceSquared / 100;

            m_light.Start(MyLight.LightTypeEnum.PointLight, 1);
            SyncObject = new MySyncThruster(this);

            UpdateDetailedInfo();

            FuelConverterDefinition = !MyFakes.ENABLE_HYDROGEN_FUEL ? new MyFuelConverterInfo { Efficiency = 1.0f } : BlockDefinition.FuelConverter;

            MyDefinitionId fuelId = new MyDefinitionId();
            if (!BlockDefinition.FuelConverter.FuelId.IsNull())
                fuelId = BlockDefinition.FuelConverter.FuelId;

            MyGasProperties fuelDef = null;
            if (MyFakes.ENABLE_HYDROGEN_FUEL)
                MyDefinitionManager.Static.TryGetDefinition(fuelId, out fuelDef);

            FuelDefinition = fuelDef ?? new MyGasProperties // Use electricity by default
            {
                Id = MyResourceDistributorComponent.ElectricityId,
                EnergyDensity = 1f,
            };

	        MyEntityThrustComponent entityThrustComponent;
	        if (!cubeGrid.Components.TryGet(out entityThrustComponent))
	        {
		        entityThrustComponent = new MyThrusterBlockThrustComponent();

                entityThrustComponent.Init();
		        cubeGrid.Components.Add<MyEntityThrustComponent>(entityThrustComponent);
	        }

            m_thrustComponent = entityThrustComponent;
        }
 protected override void AddToGroup(MyEntity thrustEntity, MyEntityThrustComponent.MyConveyorConnectedGroup group)
 {
 }
예제 #17
0
        //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;
                    }
                }
            }
        }
예제 #18
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;
            }
        }
        protected override void RemoveFromGroup(MyEntity thrustEntity, MyEntityThrustComponent.MyConveyorConnectedGroup group)
        {
            var thruster = thrustEntity as MyThrust;
            if (thruster == null)
                return;

            group.ResourceSink.IsPoweredChanged -= thruster.Sink_IsPoweredChanged;
        }
예제 #20
0
        private void DrawJetpackThrusts(bool updateCalled)
        {
            MyCharacter skinnedEntity = base.m_skinnedEntity as MyCharacter;

            if ((skinnedEntity != null) && (skinnedEntity.GetCurrentMovementState() != MyCharacterMovementEnum.Died))
            {
                MyCharacterJetpackComponent jetpackComp = skinnedEntity.JetpackComp;
                if ((jetpackComp != null) && jetpackComp.CanDrawThrusts)
                {
                    MyEntityThrustComponent component2 = base.Container.Get <MyEntityThrustComponent>();
                    if (component2 != null)
                    {
                        MatrixD worldToLocal = MatrixD.Invert(base.Container.Entity.PositionComp.WorldMatrix);
                        foreach (MyJetpackThrust thrust in this.m_jetpackThrusts)
                        {
                            Vector3D zero = Vector3D.Zero;
                            if ((!jetpackComp.TurnedOn || !jetpackComp.IsPowered) || ((skinnedEntity.IsInFirstPersonView && ReferenceEquals(skinnedEntity, MySession.Static.LocalCharacter)) && !MyAnsel.IsAnselSessionRunning))
                            {
                                if (updateCalled || (skinnedEntity.IsUsing != null))
                                {
                                    thrust.ThrustRadius = 0f;
                                }
                            }
                            else
                            {
                                MatrixD  matrix   = thrust.ThrustMatrix * base.Container.Entity.PositionComp.WorldMatrix;
                                Vector3D vectord2 = Vector3D.TransformNormal(thrust.Forward, matrix);
                                zero = matrix.Translation + (vectord2 * thrust.Offset);
                                float num = 0.05f;
                                if (updateCalled)
                                {
                                    thrust.ThrustRadius = MyUtils.GetRandomFloat(0.9f, 1.1f) * num;
                                }
                                float   num2    = MathHelper.Clamp((float)(Vector3.Dot((Vector3)vectord2, (Vector3)(-Vector3.Transform(component2.FinalThrust, base.Entity.WorldMatrix.GetOrientation()) / ((double)skinnedEntity.BaseMass))) * 0.09f), (float)0.1f, (float)1f);
                                Vector4 color   = Vector4.Zero;
                                Vector4 vector2 = Vector4.Zero;
                                if ((num2 > 0f) && (thrust.ThrustRadius > 0f))
                                {
                                    float num4 = (1f - ((float)Math.Pow((double)(1f - (1f - Math.Abs(Vector3.Dot((Vector3)MyUtils.Normalize(MySector.MainCamera.Position - zero), (Vector3)vectord2)))), 30.0))) * 0.5f;
                                    if (updateCalled)
                                    {
                                        thrust.ThrustLength    = ((num2 * 12f) * MyUtils.GetRandomFloat(1.6f, 2f)) * num;
                                        thrust.ThrustThickness = MyUtils.GetRandomFloat(thrust.ThrustRadius * 1.9f, thrust.ThrustRadius);
                                    }
                                    color = thrust.Light.Color.ToVector4() * new Vector4(1f, 1f, 1f, 0.4f);
                                    MyTransparentGeometry.AddLineBillboard(thrust.ThrustLengthMaterial, color, zero + (vectord2 * JETPACK_THRUST_OFFSET), base.GetRenderObjectID(), ref worldToLocal, (Vector3)vectord2, thrust.ThrustLength * JETPACK_THRUST_LENGTH, thrust.ThrustThickness * JETPACK_THRUST_THICKNESS, MyBillboard.BlendTypeEnum.Standard, -1, num4 * (JETPACK_THRUST_INTENSITY_BASE + (num2 * JETPACK_THRUST_INTENSITY)), null);
                                }
                                if (thrust.ThrustRadius > 0f)
                                {
                                    vector2 = thrust.Light.Color.ToVector4() * new Vector4(1f, 1f, 1f, 0.4f);
                                    MyTransparentGeometry.AddPointBillboard(thrust.ThrustPointMaterial, vector2, zero, base.GetRenderObjectID(), ref worldToLocal, thrust.ThrustRadius * JETPACK_THRUST_THICKNESS, 0f, -1, MyBillboard.BlendTypeEnum.Standard, JETPACK_THRUST_INTENSITY_BASE + (num2 * JETPACK_THRUST_INTENSITY), null);
                                }
                            }
                            if (thrust.Light != null)
                            {
                                if (thrust.ThrustRadius <= 0f)
                                {
                                    thrust.Light.GlareOn = false;
                                    thrust.Light.LightOn = false;
                                    thrust.Light.UpdateLight();
                                }
                                else
                                {
                                    thrust.Light.LightOn        = true;
                                    thrust.Light.Intensity      = JETPACK_LIGHT_INTENSITY_BASE + (thrust.ThrustLength * JETPACK_LIGHT_INTENSITY_LENGTH);
                                    thrust.Light.Range          = (thrust.ThrustRadius * JETPACK_LIGHT_RANGE_RADIUS) + (thrust.ThrustLength * JETPACK_LIGHT_RANGE_LENGTH);
                                    thrust.Light.Position       = Vector3D.Transform(zero, base.Container.Entity.PositionComp.WorldMatrixNormalizedInv);
                                    thrust.Light.ParentID       = this.m_cullRenderId;
                                    thrust.Light.GlareOn        = true;
                                    thrust.Light.GlareIntensity = (JETPACK_GLARE_INTENSITY_BASE + (thrust.ThrustLength * JETPACK_GLARE_INTENSITY_LENGTH)) * this.m_flareJetpack.Intensity;
                                    thrust.Light.GlareType      = MyGlareTypeEnum.Normal;
                                    thrust.Light.GlareSize      = (this.m_flareJetpack.Size * ((thrust.ThrustRadius * JETPACK_GLARE_SIZE_RADIUS) + (thrust.ThrustLength * JETPACK_GLARE_SIZE_LENGTH))) * thrust.ThrustGlareSize;
                                    thrust.Light.SubGlares      = this.m_flareJetpack.SubGlares;
                                    thrust.Light.GlareQuerySize = 0.1f;
                                    thrust.Light.UpdateLight();
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #21
0
        public override void OnRegisteredToGridSystems()
        {
            base.OnRegisteredToGridSystems();

            MyEntityThrustComponent entityThrustComponent;
            if (!CubeGrid.Components.TryGet(out entityThrustComponent))
            {
                entityThrustComponent = new MyThrusterBlockThrustComponent();

                entityThrustComponent.Init();
                CubeGrid.Components.Add<MyEntityThrustComponent>(entityThrustComponent);
            }
	        m_thrustComponent = entityThrustComponent;
			m_thrustComponent.Register(this, ThrustForwardVector);
            m_thrustComponent.ResourceSink.IsPoweredChanged += Sink_IsPoweredChanged;
        }
예제 #22
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;
                    }
                }
            }
        }