OSP Kit MP [Code + Sound] Making Guns Sound Different at a Distance

Users who are viewing this thread

Nord Champion

Grandmaster Knight
When I'm playing a mod that has guns, I usually get disappointed when I can see guns going off in the visible distance, but I can't hear them. I understand the fact that the guns shouldn't sound the same from a distance, which is why I have come up with this. This script will allow players to shoot a gun at a distance and players in the server will hear a "pop" noise, instead of the actual gun or nothing at all.

Note: This script requires following this tutorial first!

Files we will be editing are: module_scripts.py and module_sounds.py.

After that tutorial, you should have an item that looks something like this:
Code:
["flintlock_pistol", "Flintlock Pistol", [("flintlock_pistol",0)], itp_type_pistol |itp_merchandise|itp_primary ,itcf_shoot_pistol|itcf_reload_pistol, 230 , weight(1.5)|difficulty(0)|spd_rtng(38) | shoot_speed(160) | thrust_damage(45 ,pierce)|max_ammo(1)|accuracy(65),imodbits_none,
 [(ti_on_weapon_attack, [(multiplayer_send_int_to_server,multiplayer_event_sound_made_by_player,"snd_pistol_shot"),(position_move_x, pos1,27),(position_move_y, pos1,36),(particle_system_burst, "psys_pistol_smoke", pos1, 15)])]],
You do not need to modify that item for this tutorial, but keep it in mind later.

At this point, we need to find a sound that will be used as our distant shooting. I used this sound (it doesn't sound like a realistic "gun pop" at all, so I would suggest you look for a better one, but this one is close enough).
Open up module_sounds.py.
We need to make a sound for each distance we want to hear it. The shorter the distance, the louder the volume. The longer the distance, the quieter the volume. So put this:
Code:
 ("distant_bang_1",sf_priority_10|sf_vol_10, ["distant_bang.wav"]),
 ("distant_bang_2",sf_priority_10|sf_vol_9, ["distant_bang.wav"]),
 ("distant_bang_3",sf_priority_10|sf_vol_8, ["distant_bang.wav"]),
 ("distant_bang_4",sf_priority_10|sf_vol_7, ["distant_bang.wav"]),
 ("distant_bang_5",sf_priority_10|sf_vol_6, ["distant_bang.wav"]),
 ("distant_bang_6",sf_priority_10|sf_vol_5, ["distant_bang.wav"]),
 ("distant_bang_7",sf_priority_10|sf_vol_4, ["distant_bang.wav"]),
 ("distant_bang_8",sf_priority_10|sf_vol_3, ["distant_bang.wav"]),
Note: You can add as many or as few as you would like.

Now we just need to fix up the "multiplayer_event_sound_at_player" script.
Open up module_scripts.py
Search for multiplayer_event_sound_at_player.
You should get something that looks like this:
Code:
                  (else_try),
			(eq, ":event_type", multiplayer_event_sound_at_player),
			(store_script_param, ":sound", 3),
			(store_script_param, ":player", 4),
			
			(player_get_agent_id,":agent",":player"),

			(try_begin),
			(neq,":agent",-1),
				(agent_play_sound,":agent",":sound"),
			(try_end),

Change it to this:
Code:
                   (else_try),
			(eq, ":event_type", multiplayer_event_sound_at_player),
			(store_script_param, ":sound", 3),
			(store_script_param, ":player", 4),
			
			(multiplayer_get_my_player, ":my_player_no"),
			(player_get_agent_id,":my_agent", ":my_player_no"),
			
			(player_get_agent_id,":agent",":player"),
			(agent_get_position,pos1,":agent"), #Position of shooter.
			(agent_get_position,pos2,":my_agent"), #Position of bystander(s).
			(get_distance_between_positions_in_meters,":distance",pos1,pos2), #Distance of the shooter to the bystander(s) in meters.
			
			(assign, ":sound_1", "snd_distant_bang_1"), #For some reason you have to do these or it doesn't recognize the sounds.
			(assign, ":sound_2", "snd_distant_bang_2"),
			(assign, ":sound_3", "snd_distant_bang_3"),
			(assign, ":sound_4", "snd_distant_bang_4"),
			(assign, ":sound_5", "snd_distant_bang_5"),
			(assign, ":sound_6", "snd_distant_bang_6"),
			(assign, ":sound_7", "snd_distant_bang_7"),
                        (assign, ":sound_8", "snd_distant_bang_8"),
			
			(try_begin),
			(neq,":agent",-1),
			    (assign, reg0, ":distance"), #Debug message, not necessary.
			    (display_message, "@Distance_from_sound:_{reg0}"), #Debug message, not necessary.
				(agent_play_sound,":agent",":sound"), #This should be audible to about 200 Meters.
				(try_begin),
					(is_between,":distance",200,300), #From 200 Meters to 299 Meters
					(play_sound, ":sound_1"),
				(else_try),
					(is_between,":distance",300,400),#From 300 Meters to 399 Meters
					(play_sound, ":sound_2"),
				(else_try),
					(is_between,":distance",400,500),#From 400 Meters to 499 Meters
					(play_sound, ":sound_3"),
				(else_try),
					(is_between,":distance",500,600),#From 500 Meters to 599 Meters
					(play_sound, ":sound_4"),
				(else_try),
					(is_between,":distance",600,700),#From 600 Meters to 699 Meters
					(play_sound, ":sound_5"),
				(else_try),
					(is_between,":distance",700,800),#From 700 Meters to 799 Meters
					(play_sound, ":sound_6"),
				(else_try),
					(is_between,":distance",800,900),#From 800 Meters to 899 Meters
					(play_sound, ":sound_7"),
                                (else_try),
 					(is_between,":distance",900,999),#From 900 Meters to 999 Meters
					(play_sound, ":sound_8"),
				(try_end), #Beyond 999 meters you can't hear the gunshots.
			(try_end),
Note: If you have more sounds, make sure you add the necessary codes. Also, the distances are up to you.

And there you go! Load up your server, get some people shooting guns and test it out!

If you have a problem where it does not work for bots, change this:
Code:
	  (eq,":event_type", multiplayer_event_sound_made_by_player),
	  (neq,":player_no",0),
	  (store_script_param, ":sound", 3),
	  
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 0, ":num_players"),
            (player_is_active,":cur_player"),
            (multiplayer_send_2_int_to_player, ":cur_player", multiplayer_event_sound_at_player, ":sound",":player_no"),
          (try_end),
	  
	  (else_try),
to this:
Code:
	  (eq,":event_type", multiplayer_event_sound_made_by_player),
	  (store_script_param, ":sound", 3),
	  
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 0, ":num_players"),
            (player_is_active,":cur_player"),
            (multiplayer_send_2_int_to_player, ":cur_player", multiplayer_event_sound_at_player, ":sound",":player_no"),
          (try_end),
	  
	  (else_try),
 
Have you tried using the sound option flags to play them at different volumes? I don't think you need to have different local variables to hold the sound reference - since they are all grouped together you can do the following
Code:
(store_sub, ":sound", ":distance", 200),
(val_clamp, ":sound", 0, 800),
(val_div, ":sound", 100),
(val_add, ":sound", "snd_distant_bang_1"),
(play_sound, ":sound"),

Also, WFaS has this code using the sf_use_next_for_far not found in Warband
Code:
 ("release_crossbow",sf_vol_15|sf_priority_14|sf_use_next_for_far, ["musket_fire_close_1.ogg","musket_fire_close_2.ogg","musket_fire_close_3.ogg","musket_fire_close_5.ogg","musket_fire_close_6.ogg"]),
 ("release_crossbow_medium",sf_vol_15|sf_priority_13|sf_use_next_for_far, ["musket_fire_medium_2.ogg","musket_fire_medium_4.ogg","musket_fire_medium_5.ogg","musket_fire_medium_6.ogg"]),
 ("release_crossbow_far",sf_vol_15|sf_priority_12, ["musket_fire_far_4.ogg","musket_fire_far_4b.ogg"]),
Which seems to do the same thing, although I'm not quite sure how far the engine considers to be each threshold.
 
Somebody said:
Have you tried using the sound option flags to play them at different volumes? I don't think you need to have different local variables to hold the sound reference - since they are all grouped together you can do the following
Code:
(store_sub, ":sound", ":distance", 200),
(val_clamp, ":sound", 0, 800),
(val_div, ":sound", 100),
(val_add, ":sound", "snd_distant_bang_1"),
(play_sound, ":sound"),

Also, WFaS has this code using the sf_use_next_for_far not found in Warband
Code:
 ("release_crossbow",sf_vol_15|sf_priority_14|sf_use_next_for_far, ["musket_fire_close_1.ogg","musket_fire_close_2.ogg","musket_fire_close_3.ogg","musket_fire_close_5.ogg","musket_fire_close_6.ogg"]),
 ("release_crossbow_medium",sf_vol_15|sf_priority_13|sf_use_next_for_far, ["musket_fire_medium_2.ogg","musket_fire_medium_4.ogg","musket_fire_medium_5.ogg","musket_fire_medium_6.ogg"]),
 ("release_crossbow_far",sf_vol_15|sf_priority_12, ["musket_fire_far_4.ogg","musket_fire_far_4b.ogg"]),
Which seems to do the same thing, although I'm not quite sure how far the engine considers to be each threshold.

Ah, that will help, thank you :grin:

And this is for Warband only. Its a round about way to do what WFAS does.
 
Back
Top Bottom