Skip to content

Commit

Permalink
AI hearing code improvement + bug fix (tgstation#16502)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmixo authored Sep 4, 2024
1 parent b90dc1f commit 6a81d85
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
2 changes: 2 additions & 0 deletions code/__DEFINES/is_helpers.dm
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ GLOBAL_VAR_INIT(refid_filter, TYPEID(filter(type="angular_blur")))

#define isAI(A) (istype(A, /mob/living/silicon/ai))

#define isAIeye(A) (istype(A, /mob/camera/aiEye))

//Simple animals
#define isanimal(A) (istype(A, /mob/living/simple_animal))

Expand Down
25 changes: 11 additions & 14 deletions code/game/sound.dm
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ A good representation is: 'byond applies a volume reduction to the sound every X
frequency = GET_RANDOM_FREQ // Same frequency for everybody
// Looping through the player list has the added bonus of working for mobs inside containers
var/sound/S = sound(get_sfx(soundin))
for(var/mob/M AS in GLOB.player_list|GLOB.aiEyes)
if(!M.client && !istype(M, /mob/camera/aiEye))
for(var/mob/listener AS in GLOB.player_list|GLOB.aiEyes)
if(!listener.client && !isAIeye(listener))
continue
if(ambient_sound && !(M.client?.prefs?.toggles_sound & SOUND_AMBIENCE))
if(ambient_sound && !(listener.client?.prefs?.toggles_sound & SOUND_AMBIENCE))
continue
var/turf/T = get_turf(M)
if(!T || T.z != turf_source.z || get_dist(M, turf_source) > sound_range)
var/turf/T = get_turf(listener)
if(!T || T.z != turf_source.z || get_dist(listener, turf_source) > sound_range)
continue
M.playsound_local(turf_source, soundin, vol, vary, frequency, falloff, is_global, channel, S, sound_reciever = M)
listener.playsound_local(turf_source, soundin, vol, vary, frequency, falloff, is_global, channel, S)

for(var/obj/vehicle/sealed/armored/armor AS in GLOB.tank_list)
if(!armor.interior || armor.z != turf_source.z || get_dist(armor.loc, turf_source) > sound_range)
Expand All @@ -83,7 +83,7 @@ A good representation is: 'byond applies a volume reduction to the sound every X
if(ambient_sound && !(crew.client.prefs.toggles_sound & SOUND_AMBIENCE))
continue
//turf source is null on purpose because it will not work properly since crew is on a different z
crew.playsound_local(null, soundin, vol*0.5, vary, frequency, falloff, is_global, channel, S, sound_reciever = crew)
crew.playsound_local(null, soundin, vol*0.5, vary, frequency, falloff, is_global, channel, S)

/**
* Plays a sound locally
Expand All @@ -99,12 +99,9 @@ A good representation is: 'byond applies a volume reduction to the sound every X
* * channel - Optional: Picks a random available channel if not set
* * sound_to_use - Optional: Will default to soundin
* * distance_multiplier - Affects x and z hearing
* * sound_reciever - Defaults to src, the thing that is hearing this sound
*/
/mob/proc/playsound_local(turf/turf_source, soundin, vol, vary, frequency, falloff, is_global, channel = 0, sound/sound_to_use, distance_multiplier = 1, mob/sound_reciever)
if(!sound_reciever)
sound_reciever = src
if(!sound_reciever.client)
/mob/proc/playsound_local(turf/turf_source, soundin, vol, vary, frequency, falloff, is_global, channel = 0, sound/sound_to_use, distance_multiplier = 1)
if(!client)
return FALSE

if(!sound_to_use)
Expand Down Expand Up @@ -139,9 +136,9 @@ A good representation is: 'byond applies a volume reduction to the sound every X
if(!is_global)
sound_to_use.environment = SOUND_ENVIRONMENT_ROOM

SEND_SOUND(sound_reciever, sound_to_use)
SEND_SOUND(src, sound_to_use)

/mob/living/playsound_local(turf/turf_source, soundin, vol, vary, frequency, falloff, is_global, channel = 0, sound/sound_to_use, distance_multiplier = 1, mob/sound_reciever)
/mob/living/playsound_local(turf/turf_source, soundin, vol, vary, frequency, falloff, is_global, channel = 0, sound/sound_to_use, distance_multiplier = 1)
if(ear_deaf > 0)
return FALSE
return ..()
Expand Down
12 changes: 7 additions & 5 deletions code/modules/instruments/songs/play_legacy.dm
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@
if(!fexists(soundfile))
return
// and play
var/turf/source = get_turf(parent)
if((world.time - MUSICIAN_HEARCHECK_MINDELAY) > last_hearcheck)
do_hearcheck()
var/sound/music_played = sound(soundfile)
for(var/i in hearing_mobs)
var/mob/M = i
if(M?.client?.prefs?.toggles_sound & SOUND_INSTRUMENTS_OFF)
for(var/mob/listener AS in hearing_mobs)
if(isAIeye(listener)) //isn't there someone you forgot to ask?
var/mob/camera/aiEye/listener_eye = listener
if(listener_eye.ai?.client?.prefs?.toggles_sound & SOUND_INSTRUMENTS_OFF)
continue
if(listener?.client?.prefs?.toggles_sound & SOUND_INSTRUMENTS_OFF)
continue
M.playsound_local(source, music_played, volume * using_instrument.volume_multiplier)
listener.playsound_local(get_turf(parent), music_played, volume * using_instrument.volume_multiplier)
// Could do environment and echo later but not for now
11 changes: 7 additions & 4 deletions code/modules/instruments/songs/play_synthesized.dm
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@
var/channel_text = num2text(channel)
channels_playing[channel_text] = 100
last_channel_played = channel_text
for(var/i in hearing_mobs)
var/mob/M = i
if(M?.client?.prefs?.toggles_sound & SOUND_INSTRUMENTS_OFF)
for(var/mob/listener AS in hearing_mobs)
if(isAIeye(listener)) //isn't there someone you forgot to ask?
var/mob/camera/aiEye/listener_eye = listener
if(listener_eye.ai?.client?.prefs?.toggles_sound & SOUND_INSTRUMENTS_OFF)
continue
if(listener?.client?.prefs?.toggles_sound & SOUND_INSTRUMENTS_OFF)
continue
M.playsound_local(get_turf(parent), null, volume, FALSE, K.frequency, null, FALSE, channel, copy)
listener.playsound_local(get_turf(parent), null, volume, FALSE, K.frequency, null, FALSE, channel, copy)
// Could do environment and echo later but not for now

/**
Expand Down
12 changes: 9 additions & 3 deletions code/modules/mob/living/silicon/ai/freelook/eye.dm
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@
/mob/camera/aiEye/proc/unregister_facedir_signals(mob/user)
UnregisterSignal(user, list(COMSIG_KB_MOB_FACENORTH_DOWN, COMSIG_KB_MOB_FACEEAST_DOWN, COMSIG_KB_MOB_FACESOUTH_DOWN, COMSIG_KB_MOB_FACEWEST_DOWN))

/mob/camera/aiEye/playsound_local(turf/turf_source, soundin, vol, vary, frequency, falloff, is_global, channel, sound/sound_to_use, distance_multiplier, mob/sound_reciever)
if(istype(parent_cameranet) && !parent_cameranet.checkTurfVis(get_turf(src)))
/mob/camera/aiEye/playsound_local(turf/turf_source, soundin, vol, vary, frequency, falloff, is_global, channel, sound/sound_to_use, distance_multiplier)
if((istype(parent_cameranet) && !parent_cameranet.checkTurfVis(get_turf(src))) || !ai)
return
return ..(turf_source, soundin, vol, vary, frequency, falloff, is_global, channel, sound_to_use, distance_multiplier, ai)
/*
What is happening here is we are looking to find the position of the sound source relative to the camera eye
and using it to get a turf in the same relative position to the AI core in order to properly simulate the
direction and volume of where the sound is coming from.
*/
var/turf/playturf = locate(ai.x + (turf_source.x - x), ai.y + (turf_source.y - y), ai.z)
ai?.playsound_local(playturf, soundin, vol, vary, frequency, falloff, is_global, channel, sound_to_use, distance_multiplier)

0 comments on commit 6a81d85

Please sign in to comment.