AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [TUT] Replace Weapon Firing Sounds (https://forums.alliedmods.net/showthread.php?t=299376)

Depresie 07-12-2017 16:34

[TUT] Replace Weapon Firing Sounds
 
Today i'm gonna show you is how to replace a weapon's firing sound, without having to rewrite a lot of stuff..

I know this has been discussed around here and no one offered any solution that didn't involve rewriting a lot of the weapon's functions, so i am here to offer you an alternative

I am a lazy and inpatient person, so i'm gonna jump right to it, if anyone wants to rewrite the tutorial he is welcome..

PHP Code:


new gi_TempClip

public plugin_init()
{
    
register_plugin("Plugin""1.0""Author")

    
// Hooking weapon primary attack
    
RegisterHam(Ham_Weapon_PrimaryAttack"weapon_ak47""hook_weaponattack_pre"0)
    
RegisterHam(Ham_Weapon_PrimaryAttack"weapon_ak47""hook_weaponattack_post"1)
}

public 
hook_weaponattack_pre(iWeapon)
{
     new 
iWeaponClip get_pdata_int(iWeapon514)
     
gi_TempClip iWeaponClip
}

public 
hook_weaponattack_post(iWeapon)
{
      new 
iPlayer get_pdata_cbase(iWeapon414)
      new 
iWeaponClip get_pdata_int(iWeapon514)

      if(
gi_TempClip iWeaponClip)
      {
             switch(
random_num(01))
             {
                 case 
0emit_sound(iPlayerCHAN_WEAPON"weapons/famas-1.wav"1.0ATTN_NORM0PITCH_NORM)
                 case 
1emit_sound(iPlayerCHAN_WEAPON"weapons/famas-2.wav"1.0ATTN_NORM0PITCH_NORM)
             }
      }



OciXCrom 07-13-2017 07:57

Re: [TUT] Replace Weapon Firing Sounds
 
I don't think "replace" is the correct word since you don't actually do that. "Overlap" would be much more appropriate. This won't work correct for pistols. Try using it on one and hold down the attack button.

Depresie 07-13-2017 10:04

Re: [TUT] Replace Weapon Firing Sounds
 
Fixed it to work correctly for pistols

PRoSToTeM@ 07-13-2017 18:14

Re: [TUT] Replace Weapon Firing Sounds
 
if(pev_valid(iWeapon) >= 2) is not need here.
if(is_user_alive(iPlayer)) also.

meTaLiCroSS 07-14-2017 00:47

Re: [TUT] Replace Weapon Firing Sounds
 
You'll be reproducing both fire sounds... right?

Depresie 07-14-2017 03:40

Re: [TUT] Replace Weapon Firing Sounds
 
@Prostostem@ Done, removed the not needed checks, added one more firing sound for the sake of diversity
@Metalicross Yes, both fire sounds should be "replaced"

Natsheh 07-18-2017 06:57

Re: [TUT] Replace Weapon Firing Sounds
 
How is this suppose to block the old sound?
From player chan_weapon? Or should be from the entity?

Depresie 07-19-2017 16:31

Re: [TUT] Replace Weapon Firing Sounds
 
As far as i know, when you emit a sound on a specific channel, it will mute any other sound that is still playing on that respective channel...

meTaLiCroSS 07-29-2017 21:49

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by Depresie (Post 2536436)
As far as i know, when you emit a sound on a specific channel, it will mute any other sound that is still playing on that respective channel...

So, you haven't tested this?

Black Rose 08-01-2017 16:43

Re: [TUT] Replace Weapon Firing Sounds
 
It works pretty good in first person. But like all other methods, not when others are shooting.
When spraying you can sometimes make out the beginning of the original sound.

Natsheh 08-01-2017 17:08

Re: [TUT] Replace Weapon Firing Sounds
 
Best way is to hook emit sound forward...

klippy 08-01-2017 18:35

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by Natsheh (Post 2539137)
Best way is to hook emit sound forward...

That doesn't work for guns.

Natsheh 08-01-2017 21:18

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by KliPPy (Post 2539163)
That doesn't work for guns.

thats akward it worked with a knife ?

klippy 08-01-2017 21:20

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by Natsheh (Post 2539177)
thats akward it worked with a knife ?

It does work with the knife. But the knife ain't gun, right?

Natsheh 08-02-2017 01:18

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by KliPPy (Post 2539178)
It does work with the knife. But the knife ain't gun, right?

Its a weapon...

HamletEagle 08-02-2017 04:59

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by KliPPy (Post 2539163)
That doesn't work for guns.

He said guns, not weapons.

meTaLiCroSS 08-04-2017 21:38

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by Natsheh (Post 2539177)
thats akward it worked with a knife ?

Weapons shoot sounds are handled on client-side in exception of knife.

Depresie 08-07-2017 02:50

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by meTaLiCroSS (Post 2538617)
So, you haven't tested this?

I did test it some time ago.. it should be fine and now..

Quote:

It works pretty good in first person. But like all other methods, not when others are shooting.
When spraying you can sometimes make out the beginning of the original sound.
I didn't test when other players were shooting, but i will these days, but in theory it should work and when others are shooting too, the initial sound should be cut, but there may be some delay depending on the player's ping.. the only way to fix it would be to recreate the weapon firing function, which would be a little CPU expensive..

Ofc this could be done much better and more efficiently using Reverse Engineered Trio ( ReHLDS + ReGame + ReAPI )
but for that you should ask prostostem@ since he has better knowledge of how the game works..

Black Rose 08-07-2017 10:36

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by Depresie (Post 2540053)
I didn't test when other players were shooting, but i will these days, but in theory it should work and when others are shooting too, the initial sound should be cut, but there may be some delay depending on the player's ping.. the only way to fix it would be to recreate the weapon firing function, which would be a little CPU expensive..

I did test it using podbot so that might be the issue.

meTaLiCroSS 08-10-2017 23:36

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by Depresie (Post 2540053)
the only way to fix it would be to recreate the weapon firing function, which would be a little CPU expensive..

Yeah, you might note you did a wrong research.

Whenever you post an snippet you should be fully sure of its functionality. Also, don't test on a listen server, test with on a dedicated one.

Weapons fire sound are handled client-side, this means there's no server code associated to the fire sound. The only association you can assume, is that clients listen to the shoot sound because it's sent to them by its respective PlayBack event (you can send a fire sound after this callback is triggered to override the sound played, you can see code reference on HLSDK, cl_dlls). The player who fires the weapon predicts the sound, and as you know, prediction is what it is: A PREDICTION, it's not synchronized with the server, and this sound could be easily skippable, depending on certain factors.

The only accurate snippet to change this fire sound correctly must be the weapon prediction override.

Depresie 08-15-2017 14:15

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

The only accurate snippet to change this fire sound correctly must be the weapon prediction override
I wasn't talking about prediction, I was talking about blocking the default firing function, and emulate the firing function by hooking the firing button, doing a trace line, sending animations, sounds, handle bullets, etc

CrazY. 08-21-2017 21:25

Re: [TUT] Replace Weapon Firing Sounds
 
Probably this way don't will work.

This is the correct way:

1° Hook FM_UpdateClientData (Post) and set_cd(cd_handle, CD_flNextAttack, get_gametime() + 0.001);
2° Hook FM_PrecacheEvent/FM_PlaybackEvent, precache the gun event (cstrike/events) and then do playback_event (engine);
3° Hook weapon actions (Ham_Weapon_[PrimaryAttack|SecondaryAttack|Reload] | Ham_Item_[Deploy|Holster] etc.), play animations and custom sounds;

Too complicated to explain now without time, but it's a base.

It's a good idea to make an API :D, probrably when I finish AvP or some other developer take this job...

meTaLiCroSS 08-22-2017 08:47

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by CrazY. (Post 2543460)
Probably this way don't will work.

This is the correct way:

1° Hook FM_UpdateClientData (Post) and set_cd(cd_handle, CD_flNextAttack, get_gametime() + 0.001);
2° Hook FM_PrecacheEvent/FM_PlaybackEvent, precache the gun event (cstrike/events) and then do playback_event (engine);
3° Hook weapon actions (Ham_Weapon_[PrimaryAttack|SecondaryAttack|Reload] | Ham_Item_[Deploy|Holster] etc.), play animations and custom sounds;

Too complicated to explain now without time, but it's a base.

It's a good idea to make an API :D, probrably when I finish AvP or some other developer take this job...

Quote:

weapon prediction override

CrazY. 08-22-2017 09:50

Re: [TUT] Replace Weapon Firing Sounds
 
O.o

Depresie 08-23-2017 07:53

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by CrazY. (Post 2543460)
Probably this way don't will work.

This is the correct way:

1° Hook FM_UpdateClientData (Post) and set_cd(cd_handle, CD_flNextAttack, get_gametime() + 0.001);
2° Hook FM_PrecacheEvent/FM_PlaybackEvent, precache the gun event (cstrike/events) and then do playback_event (engine);
3° Hook weapon actions (Ham_Weapon_[PrimaryAttack|SecondaryAttack|Reload] | Ham_Item_[Deploy|Holster] etc.), play animations and custom sounds;

Too complicated to explain now without time, but it's a base.

It's a good idea to make an API :D, probrably when I finish AvP or some other developer take this job...

Doesn't make any sense...

CrazY. 08-23-2017 11:36

Re: [TUT] Replace Weapon Firing Sounds
 
Really? Take a look at my weapons.

HamletEagle 08-23-2017 12:00

Re: [TUT] Replace Weapon Firing Sounds
 
It makes sense, not a good method indeed, but yours is not that good too.

CrazY. 08-23-2017 15:07

Re: [TUT] Replace Weapon Firing Sounds
 
Well, I'm using this way, but I'm not sure if it's one of the best ways to do it.

Maybe you have some idea in mind, Hamlet...?

Depresie 08-23-2017 16:05

Re: [TUT] Replace Weapon Firing Sounds
 
Still don't understand what he is explaining there..

Why would you just hook three events and do nothing ? then proceed to emit the sound on PrimaryAttack ?

Natsheh 08-23-2017 17:25

Re: [TUT] Replace Weapon Firing Sounds
 
best way is to crack the engine :) hack it !! :) which its not allowed :)

CrazY. 08-23-2017 18:36

Re: [TUT] Replace Weapon Firing Sounds
 
Here is an example, needs some conditions to avoid problems, but is the base:

Code:
public fw_UpdateClientData_Post(id, sendweapons, cd_handle) {     // Block default sounds and animation of a weapon     set_cd(cd_handle, CD_flNextAttack, get_gametime() + 0.001) } public fw_PlaybackEvent(flags, invoker, eventid, Float:delay, Float:origin[3], Float:angles[3], Float:fparam1, Float:fparam2, iParam1, iParam2, bParam1, bParam2) {     if (((1<<eventid) != g_iEventId))         return FMRES_IGNORED;     if (!(1 <= invoker <= g_MaxPlayers))         return FMRES_IGNORED;     // PlaybackEvent (play custom sounds to all clients, don't only the owner)     playback_event(flags | FEV_HOSTONLY, invoker, eventid, delay, origin, angles, fparam1, fparam2, iParam1, iParam2, bParam1, bParam2);     return FMRES_SUPERCEDE; } public fw_PrecacheEvent_Post(type, const name[]) {     if (!equal("events/weapon_event.sc", name))         return;     // Precache gun event     g_iEventId |= (1<<get_orig_retval()) } public fw_Item_Deploy_Post(entity) {     new id = get_pdata_cbase(entity, 41, 4)     // Play 'draw' animation     Util_PlayWeaponAnim(id, 1)     // Play 'draw' sound     emit_sound(id, "weapons/draw.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM) } public fw_Weapon_PrimaryAttack_Post(entity) {     new id = get_pdata_cbase(entity, 41, 4)     // Play 'fire' animation     Util_PlayWeaponAnim(id, 3)     // Play 'fire' sound     emit_sound(id, "weapons/fire.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM) } stock Util_PlayWeaponAnim(id, sequence) {     entity_set_int(id, EV_INT_weaponanim, sequence)     message_begin(MSG_ONE_UNRELIABLE, SVC_WEAPONANIM, .player = id)     write_byte(sequence)     write_byte(entity_get_int(id, EV_INT_body))     message_end() }

meTaLiCroSS 08-23-2017 21:19

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by Depresie (Post 2543899)
Still don't understand what he is explaining there..

Why would you just hook three events and do nothing ? then proceed to emit the sound on PrimaryAttack ?

I see you didn't read what I wrote to you. Too bad

I would like to see your basis about your snippet, because you're assuming it's right.

What is @CrazY. doing is called "weapon prediction removal" and it's an accurate (and expensive, wasteful) way to make client avoid completely his gun prediction, so you can safely skip shoot sound, and it also removes firing animation and bullet holes with smokes. That's why it's not a friendly way.

HamletEagle 08-24-2017 03:56

Re: [TUT] Replace Weapon Firing Sounds
 
Crazy, it's not the body of the player but the body of weapon entity. It works both ways because the param is not used.

CrazY. 08-24-2017 11:43

Re: [TUT] Replace Weapon Firing Sounds
 
@meTaLiCroSS, ah ok, now I understand.

@HamletEagle, oh sorry, my bad. Enjoying your comment, I see that sometimes a few sprites pops up in the weapons I used this procedure. I believe the problem should be in this stock haha

PRoSToTeM@ 08-24-2017 20:09

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by CrazY. (Post 2543460)
Hook FM_UpdateClientData (Post) and set_cd(cd_handle, CD_flNextAttack, get_gametime() + 0.001);

Better just to use some value, about 2 and bigger.

Quote:

Originally Posted by CrazY. (Post 2543935)
Code:
// PlaybackEvent (play custom sounds to all clients, don't only the owner) playback_event(flags | FEV_HOSTONLY, invoker, eventid, delay, origin, angles, fparam1, fparam2, iParam1, iParam2, bParam1, bParam2); return FMRES_SUPERCEDE;

??? Why not just block it? Actually it will send event only for invoker and only if he has cl_lw 0.

Quote:

Originally Posted by HamletEagle (Post 2543989)
It works both ways because the param is not used.

It is used in svc_weaponanim and it actually works. Looks like you mean Ham_Weapon_SendWeaponAnim.

CrazY. 08-24-2017 21:08

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by PRoSToTeM@ (Post 2544186)
Better just to use some value, about 2 and bigger.

Why that statement exactly?

Quote:

Originally Posted by PRoSToTeM@ (Post 2544186)
??? Why not just block it? Actually it will send event only for invoker and only if he has cl_lw 0..

Hm, probably you're right, I'm too lazy to update the code now. :P

Quote:

Originally Posted by PRoSToTeM@ (Post 2544186)
It is used in svc_weaponanim and it actually works. Looks like you mean Ham_Weapon_SendWeaponAnim.

Ham_Weapon_SendWeaponAnim do the same thing of the stock Util_PlayWeaponAnim?

meTaLiCroSS 08-25-2017 16:11

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by CrazY. (Post 2544192)
Why that statement exactly?



Hm, probably you're right, I'm too lazy to update the code now. :P



Ham_Weapon_SendWeaponAnim do the same thing of the stock Util_PlayWeaponAnim?

Because delay between server and client can be higher than 0.1, so client would not have a real reload sensation.

You should not keep reusing code you see on internet. That playback event trick is an useless snippet.

Yeah, both are the same, but you can hook one with extra addons. That's a good coding habit.

--

Clarifying:

Quote:

// PlaybackEvent (play custom sounds to all clients, don't only the owner)
playback_event(flags | FEV_HOSTONLY, invoker, eventid, delay, origin, angles, fparam1, fparam2, iParam1, iParam2, bParam1, bParam2);
return FMRES_SUPERCEDE;
Don't know who started with this **trick** (dias maybe?), but it's some kind of HIV virus. Original flag param has the FEV_NOTHOST flag, then you're setting FEV_HOSTONLY, so I guess he was high as f*ck. Then, everybody started copying their addons with this. Sad

PRoSToTeM@ 08-25-2017 16:24

Re: [TUT] Replace Weapon Firing Sounds
 
Quote:

Originally Posted by CrazY. (Post 2544192)
Why that statement exactly?

I meant that it is not gametime based. So instead of 'get_gametime() + const' you can just put 'const', but this const should be equal or bigger than the biggest delay between client and server, so 2 seconds should be ok, but you can put any value bigger than 2 for reliability.

CrazY. 08-25-2017 17:01

Re: [TUT] Replace Weapon Firing Sounds
 
Well, I do not know exactly what plugin I extract that part of the code but it was in a time that I did not know how much haha

Oh yes, now I understand, thanks for the explanations, but anyway, I can't think of any other way to do that... what about you?

thEsp 08-31-2019 20:54

Re: [TUT] Replace Weapon Firing Sounds
 
This is a terrible way, and the title doesn't wrap the main post. It's impossible to replace gunfire sounds (in CS, don't really know about other mods) only using AMXX, probably something can be achieved by Orpheu.
Although sounds can be overridden and that's the proper term. It's 50/50%, depending per default sound. I've messed with this for quiet a while actually, the closest thing (I've considered more accurate) is by emitting sound on both weapon and player, but still is nowhere close to perfect.


All times are GMT -4. The time now is 05:11.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.