Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 3dc3e1e

Browse files
committed
w
1 parent e94cba9 commit 3dc3e1e

File tree

1 file changed

+33
-41
lines changed

1 file changed

+33
-41
lines changed

code/game/sound.dm

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,22 @@ falloff_distance - Distance at which falloff begins. Sound is at peak volume (in
5353
CRASH("playsound(): source is an area")
5454

5555
if(islist(soundin))
56-
CRASH("playsound(): soundin attempted to pass a list! Consider using pick() source: [source.name]")
56+
CRASH("playsound(): soundin attempted to pass a list! Consider using pick()")
5757

5858
var/turf/turf_source = get_turf(source)
5959

60-
if (!turf_source)
60+
if (!turf_source || !soundin || !vol)
6161
return
6262

6363
//allocate a channel if necessary now so its the same for everyone
6464
channel = channel || SSsounds.random_available_channel()
6565

66-
// Looping through the player list has the added bonus of working for mobs inside containers
67-
var/sound/S = sound(get_sfx(soundin))
66+
var/sound/S = isdatum(soundin) ? soundin : sound(get_sfx(soundin))
6867
var/maxdistance = SOUND_RANGE + extrarange
6968
var/source_z = turf_source.z
7069
var/list/listeners = SSmobs.clients_by_zlevel[source_z].Copy()
7170

72-
. = list() //output everything that successfully heard the sound
71+
. = list()//output everything that successfully heard the sound
7372

7473
var/turf/above_turf = GET_TURF_ABOVE(turf_source)
7574
var/turf/below_turf = GET_TURF_BELOW(turf_source)
@@ -91,12 +90,10 @@ falloff_distance - Distance at which falloff begins. Sound is at peak volume (in
9190
if(below_turf && istransparentturf(turf_source))
9291
listeners += get_hearers_in_view(maxdistance, below_turf)
9392

94-
for(var/mob/listening_mob as anything in listeners)
95-
if(get_dist(listening_mob, turf_source) <= maxdistance)
96-
listening_mob.playsound_local(turf_source, soundin, vol, vary, frequency, falloff_exponent, channel, pressure_affected, S, maxdistance, falloff_distance, 1, use_reverb)
97-
for(var/mob/listening_mob as anything in SSmobs.dead_players_by_zlevel[source_z])
93+
for(var/mob/listening_mob in listeners | SSmobs.dead_players_by_zlevel[source_z])//observers always hear through walls
9894
if(get_dist(listening_mob, turf_source) <= maxdistance)
9995
listening_mob.playsound_local(turf_source, soundin, vol, vary, frequency, falloff_exponent, channel, pressure_affected, S, maxdistance, falloff_distance, 1, use_reverb)
96+
. += listening_mob
10097

10198
/*! playsound
10299
playsound_local is a proc used to play a sound directly on a mob from a specific turf.
@@ -114,42 +111,37 @@ falloff_distance - Distance at which falloff begins, if this is a 3D sound
114111
distance_multiplier - Can be used to multiply the distance at which the sound is heard
115112
*/
116113

117-
/mob/proc/playsound_local(turf/turf_source, soundin, vol as num, vary, frequency, falloff_exponent = SOUND_FALLOFF_EXPONENT, channel = 0, pressure_affected = TRUE, sound/S, max_distance, falloff_distance = SOUND_DEFAULT_FALLOFF_DISTANCE, distance_multiplier = 1, use_reverb = TRUE)
114+
/mob/proc/playsound_local(turf/turf_source, soundin, vol as num, vary, frequency, falloff_exponent = SOUND_FALLOFF_EXPONENT, channel = 0, pressure_affected = TRUE, sound/sound_to_use, max_distance, falloff_distance = SOUND_DEFAULT_FALLOFF_DISTANCE, distance_multiplier = 1, use_reverb = TRUE)
118115
if(!client || !can_hear())
119116
return
120117

121-
if(!S)
122-
S = sound(get_sfx(soundin))
118+
if(!sound_to_use)
119+
sound_to_use = sound(get_sfx(soundin))
123120

124-
S.wait = 0 //No queue
125-
S.channel = channel || SSsounds.random_available_channel()
126-
S.volume = vol
127-
128-
if (client?.prefs && !(client.prefs.toggles & SOUND_ALT) && (S.file in GLOB.alt_sound_overrides))
129-
S.file = GLOB.alt_sound_overrides[S.file]
121+
sound_to_use.wait = 0 //No queue
122+
sound_to_use.channel = channel || SSsounds.random_available_channel()
123+
sound_to_use.volume = vol
130124

131125
if(vary)
132126
if(frequency)
133-
S.frequency = frequency
127+
sound_to_use.frequency = frequency
134128
else
135-
S.frequency = get_rand_frequency()
129+
sound_to_use.frequency = get_rand_frequency()
136130

137131
if(isturf(turf_source))
138-
var/turf/T = get_turf(src)
132+
var/turf/turf_loc = get_turf(src)
139133

140134
//sound volume falloff with distance
141-
var/distance = get_dist(T, turf_source)
142-
143-
distance *= distance_multiplier
135+
var/distance = get_dist(turf_loc, turf_source) * distance_multiplier
144136

145137
if(max_distance) //If theres no max_distance we're not a 3D sound, so no falloff.
146-
S.volume -= (max(distance - falloff_distance, 0) ** (1 / falloff_exponent)) / ((max(max_distance, distance) - falloff_distance) ** (1 / falloff_exponent)) * S.volume
138+
sound_to_use.volume -= (max(distance - falloff_distance, 0) ** (1 / falloff_exponent)) / ((max(max_distance, distance) - falloff_distance) ** (1 / falloff_exponent)) * sound_to_use.volume
147139
//https://www.desmos.com/calculator/sqdfl8ipgf
148140

149141
if(pressure_affected)
150142
//Atmosphere affects sound
151143
var/pressure_factor = 1
152-
var/datum/gas_mixture/hearer_env = T.return_air()
144+
var/datum/gas_mixture/hearer_env = turf_loc.return_air()
153145
var/datum/gas_mixture/source_env = turf_source.return_air()
154146

155147
if(hearer_env && source_env)
@@ -162,35 +154,35 @@ distance_multiplier - Can be used to multiply the distance at which the sound is
162154
if(distance <= 1)
163155
pressure_factor = max(pressure_factor, 0.15) //touching the source of the sound
164156

165-
S.volume *= pressure_factor
157+
sound_to_use.volume *= pressure_factor
166158
//End Atmosphere affecting sound
167159

168-
if(S.volume <= 0)
160+
if(sound_to_use.volume <= 0)
169161
return //No sound
170162

171-
var/dx = turf_source.x - T.x // Hearing from the right/left
172-
S.x = dx * distance_multiplier
173-
var/dz = turf_source.y - T.y // Hearing from infront/behind
174-
S.z = dz * distance_multiplier
175-
var/dy = (turf_source.z - T.z) * 5 * distance_multiplier // Hearing from above / below, multiplied by 5 because we assume height is further along coords.
176-
S.y = dy
163+
var/dx = turf_source.x - turf_loc.x // Hearing from the right/left
164+
sound_to_use.x = dx * distance_multiplier
165+
var/dz = turf_source.y - turf_loc.y // Hearing from infront/behind
166+
sound_to_use.z = dz * distance_multiplier
167+
var/dy = (turf_source.z - turf_loc.z) * 5 * distance_multiplier // Hearing from above / below, multiplied by 5 because we assume height is further along coords.
168+
sound_to_use.y = dy
177169

178-
S.falloff = max_distance || 1 //use max_distance, else just use 1 as we are a direct sound so falloff isnt relevant.
170+
sound_to_use.falloff = max_distance || 1 //use max_distance, else just use 1 as we are a direct sound so falloff isnt relevant.
179171

180172
// Sounds can't have their own environment. A sound's environment will be:
181173
// 1. the mob's
182174
// 2. the area's (defaults to SOUND_ENVRIONMENT_NONE)
183175
if(sound_environment_override != SOUND_ENVIRONMENT_NONE)
184-
S.environment = sound_environment_override
176+
sound_to_use.environment = sound_environment_override
185177
else
186178
var/area/A = get_area(src)
187-
S.environment = A.sound_environment
179+
sound_to_use.environment = A.sound_environment
188180

189-
if(use_reverb && S.environment != SOUND_ENVIRONMENT_NONE) //We have reverb, reset our echo setting
190-
S.echo[3] = 0 //Room setting, 0 means normal reverb
191-
S.echo[4] = 0 //RoomHF setting, 0 means normal reverb.
181+
if(use_reverb && sound_to_use.environment != SOUND_ENVIRONMENT_NONE) //We have reverb, reset our echo setting
182+
sound_to_use.echo[3] = 0 //Room setting, 0 means normal reverb
183+
sound_to_use.echo[4] = 0 //RoomHF setting, 0 means normal reverb.
192184

193-
SEND_SOUND(src, S)
185+
SEND_SOUND(src, sound_to_use)
194186

195187
/proc/sound_to_playing_players(soundin, volume = 100, vary = FALSE, frequency = 0, channel = 0, pressure_affected = FALSE, sound/S)
196188
if(!S)

0 commit comments

Comments
 (0)