Skip to content

Commit fdf206e

Browse files
committed
[Shared] fix s_initSound 0 crash
1 parent 9671dd7 commit fdf206e

File tree

6 files changed

+280
-284
lines changed

6 files changed

+280
-284
lines changed

code/client/snd_ambient.cpp

Lines changed: 88 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,8 @@ AS_AddPrecacheEntry
788788
-------------------------
789789
*/
790790

791-
void AS_AddPrecacheEntry( const char *name )
792-
{
793-
if (!pMap) //s_initsound 0 probably
794-
{
791+
void AS_AddPrecacheEntry( const char *name ) {
792+
if ( !pMap ) { // s_initsound 0 probably
795793
return;
796794
}
797795
if (!Q_stricmp(name,"#clear"))
@@ -816,10 +814,10 @@ Called on the client side to load and precache all the ambient sound sets
816814

817815
void AS_ParseSets( void )
818816
{
819-
cvar_t *cv = Cvar_Get ("s_initsound", "1", 0);
820-
if ( !cv->integer ) {
817+
if ( !s_initsound->integer ) {
821818
return;
822819
}
820+
823821
AS_Init();
824822

825823
//Parse all the sets
@@ -913,47 +911,47 @@ Fades volumes up or down depending on the action being taken on them.
913911
-------------------------
914912
*/
915913

916-
static void AS_UpdateSetVolumes( void )
917-
{
918-
ambientSet_t *old, *current;
919-
float scale;
920-
int deltaTime;
914+
static void AS_UpdateSetVolumes( void ) {
915+
if ( !aSets ) {
916+
return;
917+
}
921918

922919
//Get the sets and validate them
923-
current = aSets->GetSet( currentSet );
924-
925-
if ( current == NULL )
920+
ambientSet_t *current = aSets->GetSet( currentSet );
921+
if ( !current ) {
926922
return;
923+
}
927924

928-
if ( current->masterVolume < MAX_SET_VOLUME )
929-
{
925+
float scale;
926+
int deltaTime;
927+
if ( current->masterVolume < MAX_SET_VOLUME ) {
930928
deltaTime = cls.realtime - current->fadeTime;
931929
scale = ((float)(deltaTime)/(float)(crossDelay));
932930
current->masterVolume = (int)((scale) * (float)MAX_SET_VOLUME);
933931
}
934932

935-
if ( current->masterVolume > MAX_SET_VOLUME )
933+
if ( current->masterVolume > MAX_SET_VOLUME ) {
936934
current->masterVolume = MAX_SET_VOLUME;
935+
}
937936

938937
//Only update the old set if it's still valid
939-
if ( oldSet == -1 )
938+
if ( oldSet == -1 ) {
940939
return;
940+
}
941941

942-
old = aSets->GetSet( oldSet );
943-
944-
if ( old == NULL )
942+
ambientSet_t *old = aSets->GetSet( oldSet );
943+
if ( !old ) {
945944
return;
945+
}
946946

947947
//Update the volumes
948-
if ( old->masterVolume > 0 )
949-
{
948+
if ( old->masterVolume > 0 ) {
950949
deltaTime = cls.realtime - old->fadeTime;
951950
scale = ((float)(deltaTime)/(float)(crossDelay));
952951
old->masterVolume = MAX_SET_VOLUME - (int)((scale) * (float)MAX_SET_VOLUME);
953952
}
954953

955-
if ( old->masterVolume <= 0 )
956-
{
954+
if ( old->masterVolume <= 0 ) {
957955
old->masterVolume = 0;
958956
oldSet = -1;
959957
}
@@ -967,26 +965,25 @@ Does internal maintenance to keep track of changing sets.
967965
-------------------------
968966
*/
969967

970-
static void AS_UpdateCurrentSet( int id )
971-
{
972-
ambientSet_t *old, *current;
968+
static void AS_UpdateCurrentSet( int id ) {
969+
if ( !aSets ) {
970+
return;
971+
}
973972

974973
//Check for a change
975-
if ( id != currentSet )
976-
{
974+
if ( id != currentSet ) {
977975
//This is new, so start the fading
978976
oldSet = currentSet;
979977
currentSet = id;
980978

981-
old = aSets->GetSet( oldSet );
982-
current = aSets->GetSet( currentSet );
979+
ambientSet_t *current = aSets->GetSet( currentSet );
983980
// Ste, I just put this null check in for now, not sure if there's a more graceful way to exit this function - dmv
984-
if( !current )
985-
{
981+
if ( !current ) {
986982
return;
987983
}
988-
if ( old )
989-
{
984+
985+
ambientSet_t *old = aSets->GetSet( oldSet );
986+
if ( old ) {
990987
old->masterVolume = MAX_SET_VOLUME;
991988
old->fadeTime = cls.realtime;
992989
}
@@ -1010,42 +1007,42 @@ Alters lastTime to reflect the time updates.
10101007
-------------------------
10111008
*/
10121009

1013-
static void AS_PlayLocalSet( vec3_t listener_origin, vec3_t origin, ambientSet_t *set, int entID, int *lastTime )
1014-
{
1015-
unsigned char volume;
1016-
vec3_t dir;
1017-
float volScale, dist, distScale;
1018-
int time = cl.serverTime;
1019-
1010+
static void AS_PlayLocalSet( vec3_t listener_origin, vec3_t origin, const ambientSet_t *set, int entID, int *lastTime ) {
10201011
//Make sure it's valid
1021-
if ( set == NULL )
1012+
if ( !set ) {
10221013
return;
1014+
}
10231015

1016+
vec3_t dir;
10241017
VectorSubtract( origin, listener_origin, dir );
1025-
dist = VectorLength( dir );
1018+
float dist = VectorLength( dir );
10261019

10271020
//Determine the volume based on distance (NOTE: This sits on top of what SpatializeOrigin does)
1028-
distScale = ( dist < ( set->radius * 0.5f ) ) ? 1 : ( set->radius - dist ) / ( set->radius * 0.5f );
1029-
volume = ( distScale > 1.0f || distScale < 0.0f ) ? 0 : (unsigned char) ( set->masterVolume * distScale );
1021+
float distScale = ( dist < ( set->radius * 0.5f ) ) ? 1 : ( set->radius - dist ) / ( set->radius * 0.5f );
1022+
unsigned char volume = ( distScale > 1.0f || distScale < 0.0f ) ? 0 : (unsigned char) ( set->masterVolume * distScale );
10301023

10311024
//Add the looping sound
1032-
if ( set->loopedWave )
1025+
if ( set->loopedWave ) {
10331026
S_AddAmbientLoopingSound( origin, volume, set->loopedWave );
1027+
}
10341028

10351029
//Check the time to start another one-shot subwave
1036-
if ( ( time - *lastTime ) < ( ( Q_irand( set->time_start, set->time_end ) ) * 1000 ) )
1030+
int time = cl.serverTime;
1031+
if ( ( time - *lastTime ) < ( ( Q_irand( set->time_start, set->time_end ) ) * 1000 ) ) {
10371032
return;
1033+
}
10381034

10391035
//Update the time
10401036
*lastTime = time;
10411037

10421038
//Scale the volume ranges for the subwaves based on the overall master volume
1043-
volScale = (float) volume / (float) MAX_SET_VOLUME;
1039+
float volScale = (float) volume / (float) MAX_SET_VOLUME;
10441040
volume = (unsigned char) Q_irand( (int)(volScale*set->volRange_start), (int)(volScale*set->volRange_end) );
10451041

10461042
//Add the random subwave
1047-
if ( set->numSubWaves )
1043+
if ( set->numSubWaves ) {
10481044
S_StartAmbientSound( origin, entID, volume, set->subWaves[Q_irand( 0, set->numSubWaves-1)] );
1045+
}
10491046
}
10501047

10511048
/*
@@ -1057,38 +1054,39 @@ Alters lastTime to reflect the time updates.
10571054
-------------------------
10581055
*/
10591056

1060-
static void AS_PlayAmbientSet( vec3_t origin, ambientSet_t *set, int *lastTime )
1061-
{
1062-
unsigned char volume;
1063-
float volScale;
1064-
int time = cls.realtime;
1065-
1057+
static void AS_PlayAmbientSet( vec3_t origin, const ambientSet_t *set, int *lastTime ) {
10661058
//Make sure it's valid
1067-
if ( set == NULL )
1059+
if ( !set ) {
10681060
return;
1061+
}
10691062

10701063
//Add the looping sound
1071-
if ( set->loopedWave )
1064+
if ( set->loopedWave ) {
10721065
S_AddAmbientLoopingSound( origin, (unsigned char) set->masterVolume, set->loopedWave );
1066+
}
10731067

10741068
//Check the time to start another one-shot subwave
1075-
if ( ( time - *lastTime ) < ( ( Q_irand( set->time_start, set->time_end ) ) * 1000 ) )
1069+
int time = cls.realtime;
1070+
if ( ( time - *lastTime ) < ( ( Q_irand( set->time_start, set->time_end ) ) * 1000 ) ) {
10761071
return;
1072+
}
10771073

10781074
//Update the time
10791075
*lastTime = time;
10801076

10811077
//Scale the volume ranges for the subwaves based on the overall master volume
1082-
volScale = (float) set->masterVolume / (float) MAX_SET_VOLUME;
1083-
volume = Q_irand( (int)(volScale*set->volRange_start), (int)(volScale*set->volRange_end) );
1078+
float volScale = (float) set->masterVolume / (float) MAX_SET_VOLUME;
1079+
unsigned char volume = Q_irand( (int)(volScale*set->volRange_start), (int)(volScale*set->volRange_end) );
10841080

10851081
//Allow for softer noises than the masterVolume, but not louder
1086-
if ( volume > set->masterVolume )
1082+
if ( volume > set->masterVolume ) {
10871083
volume = set->masterVolume;
1084+
}
10881085

10891086
//Add the random subwave
1090-
if ( set->numSubWaves )
1087+
if ( set->numSubWaves ) {
10911088
S_StartAmbientSound( origin, 0, volume, set->subWaves[Q_irand( 0, set->numSubWaves-1)] );
1089+
}
10921090
}
10931091

10941092
/*
@@ -1099,29 +1097,28 @@ Does maintenance and plays the ambient sets (two if crossfading)
10991097
-------------------------
11001098
*/
11011099

1102-
void S_UpdateAmbientSet ( const char *name, vec3_t origin )
1103-
{
1104-
ambientSet_t *current, *old;
1105-
if (aSets == NULL)
1106-
{
1100+
void S_UpdateAmbientSet( const char *name, vec3_t origin ) {
1101+
if ( !aSets ) {
11071102
return;
11081103
}
1109-
ambientSet_t *set = aSets->GetSet( name );
11101104

1111-
if ( set == NULL )
1105+
const ambientSet_t *set = aSets->GetSet( name );
1106+
if ( !set ) {
11121107
return;
1108+
}
11131109

11141110
//Update the current and old set for crossfading
11151111
AS_UpdateCurrentSet( set->id );
11161112

1117-
current = aSets->GetSet( currentSet );
1118-
old = aSets->GetSet( oldSet );
1119-
1120-
if ( current )
1113+
const ambientSet_t *current = aSets->GetSet( currentSet );
1114+
if ( current ) {
11211115
AS_PlayAmbientSet( origin, set, &currentSetTime );
1116+
}
11221117

1123-
if ( old )
1118+
const ambientSet_t *old = aSets->GetSet( oldSet );
1119+
if ( old ) {
11241120
AS_PlayAmbientSet( origin, old, &oldSetTime );
1121+
}
11251122
}
11261123

11271124
/*
@@ -1130,20 +1127,18 @@ S_AddLocalSet
11301127
-------------------------
11311128
*/
11321129

1133-
int S_AddLocalSet( const char *name, vec3_t listener_origin, vec3_t origin, int entID, int time )
1134-
{
1135-
ambientSet_t *set;
1136-
int currentTime = 0;
1137-
1138-
set = aSets->GetSet( name );
1139-
1140-
if ( set == NULL )
1130+
int S_AddLocalSet( const char *name, vec3_t listener_origin, vec3_t origin, int entID, int time ) {
1131+
if ( !aSets ) {
11411132
return cl.serverTime;
1133+
}
11421134

1143-
currentTime = time;
1135+
const ambientSet_t *set = aSets->GetSet( name );
1136+
if ( !set ) {
1137+
return cl.serverTime;
1138+
}
11441139

1140+
int currentTime = time;
11451141
AS_PlayLocalSet( listener_origin, origin, set, entID, &currentTime );
1146-
11471142
return currentTime;
11481143
}
11491144

@@ -1153,18 +1148,16 @@ AS_GetBModelSound
11531148
-------------------------
11541149
*/
11551150

1156-
sfxHandle_t AS_GetBModelSound( const char *name, int stage )
1157-
{
1158-
ambientSet_t *set;
1159-
1160-
set = aSets->GetSet( name );
1161-
1162-
if ( set == NULL )
1151+
sfxHandle_t AS_GetBModelSound( const char *name, int stage ) {
1152+
if ( !aSets ) {
11631153
return -1;
1154+
}
11641155

11651156
//Stage must be within a valid range
1166-
if ( ( stage > ( set->numSubWaves - 1 ) ) || ( stage < 0 ) )
1157+
const ambientSet_t *set = aSets->GetSet( name );
1158+
if ( !set || stage < 0 || stage > (set->numSubWaves - 1) ) {
11671159
return -1;
1160+
}
11681161

1169-
return set->subWaves[ stage ];
1162+
return set->subWaves[stage];
11701163
}

0 commit comments

Comments
 (0)