problem making hitsounds


(ZeAvIs) #1

Im making hitsounds for this mod im doing and im having a little trouble with it. The problem is that when a person gets shot, everyone hears the hitsound not just the person who was shooting. Here is the code i used.

In function g_damage in g_combat i have this code right above where it does damage distance falloff

if( headShot)

	{
		if( OnSameTeam( attacker, targ ) )
			G_AddEvent( attacker, EV_TEAMHITSOUND, 0 );

		else G_AddEvent( attacker, EV_HEADSHOTSOUND, 0 );
	}

	else
	{
		if ( OnSameTeam(attacker, targ) )
			G_AddEvent( attacker, EV_TEAMHITSOUND, 0 );

		else G_AddEvent( attacker, EV_HITSOUND, 0 );
	}

Then in function cg_entityevent in cg_event i have this.

case EV_HEADSHOTSOUND:
DEBUGNAME(“EV_HEADSHOTSOUND”);
trap_S_StartLocalSound( trap_S_RegisterSound( “sound/headshot.wav”, qtrue ), CHAN_LOCAL_SOUND );
break;

case EV_HITSOUND:
	DEBUGNAME("EV_HITSOUND");
	trap_S_StartLocalSound( trap_S_RegisterSound( "sound/hit.wav", qtrue ), CHAN_LOCAL_SOUND );
	break;

case EV_TEAMHITSOUND:
	DEBUGNAME("EV_TEAMHITSOUND");
	trap_S_StartLocalSound( trap_S_RegisterSound( "sound/hitteammate.wav", qtrue ), CHAN_LOCAL_SOUND );
	break;

At first i thought the CHAN thing was wrong but i tried other ones and still had the same problem. Any help would be appreciated.


(Jaquboss) #2

use search for hitsound code, BTW if you want hitsounds like this look at airstrike/artillery messages ( eg. 'Aborting, cant see target ’ )
However since game already count hits you just need to count headshots as well…
I suggest to search…


(Mr.Mxyzptlk) #3

Nicely done with the event types. But there’s one catch – events are sent to all clients,
are they not? So if you want to do this with events, you’ll have to make it conditional
on the client-side to process the event.

Try someting like this:

  1. Add slot number to the event paramater on server side:
    G_AddEvent( attacker, EV_TEAMHITSOUND, attacker->client - level.clients );

  2. Make conditional your sound on client side…
    if ( cg.snap->ps.clientNum == es->eventParm ) {
    trap_S_…
    }

It might be more efficient to send commands from the server to the specific client
instead of using events. This way the network traffic isn’t spoked out to each and every
client, and only processed by one.


(ZeAvIs) #4

I just dont understand because when calling G_AddEvent, im sending in the attacker as the client parameter, so shouldnt that event only activate for that client? I dont understand why that doesnt work.


(Mr.Mxyzptlk) #5

The first arg specifices which entity is the owner/primary of the event. Also, entity is
a superset of clients. So you probably don’t need to use eventparm as I suggested,
and you could compare the entity number to the client, but I’m fairly certain the event still
goes to all clients. Events are typically broadcast. Everyone listens to the game channel
and then processes each event.


(jaybird) #6

Yep.


(ZeAvIs) #7

is it possible to define a function in the gclient_s struct that will perform the hitsounds?


(ensiform) #8

why would you do that when the actual sounds are played on client-side. i would make a cvar on the client-side instead to control if u want them.


(jaybird) #9

Thanks to this thread, I got around to it and moved all the main hitsound stuff completely client side in Jaymod (no events and such). Pretty easy to do.


(Mr.Mxyzptlk) #10

But you’re still issuing sounds * ONLY UPON* server-side detection of hit, right?
Did you end up implementing as servercmd on client?


(jaybird) #11

Well, yeah, serverside detection. Basically I used Quake 3’s version of hitsound: ps->persistant[PERS_HITS]. That var pretty much isn’t used in ET (and probably should have been freed), but it was still incrementing in g_damage. So I used it. Also, I used the PERS_BLEH2 slot to increment/decrement a headshot count, and let the client handle tracking when hits had been made. Pretty cut and dry.


(Mr.Mxyzptlk) #12

Nice! This nicely preserves network bandwidth usage at probably the most important
time – when people are shooting at eachother.


(jaybird) #13

Exactly. I’m quite happy with it. I have a couple different “hitsound” type events (not in the sdk sense) that I’m just doing a variation of G_Sound for, but the frequency of them playing is quite low, so I think it’s a good alternative to keep just about every feature of the hitsound system I had but reduce overall bandwidth usage.


(forty) #14

Clever.


(jaybird) #15

Thanx ;]


(kamikazee) #16

I once implemented these by using a userinfo cvar.
The cvar would be sent when the client changed it or on connecting.

However, this is ok as well, as long it works and doesn’t need extra/constant traffic, things are ok.


(Pytox) #17

Yea, u imported quake sounds :stuck_out_tongue:


(ZeAvIs) #18

Im just not too sure how to make it so the client that was shooting gets the hitsound and not anyone else. I don’t know where I execute the play sound function. I’m still kinda new to the SDK, and i figure asking you guys would be quicker than spending hours trying to understand it.


(jaybird) #19

I’ll throw up a very simple solution later how to do it.


(jaybird) #20

As promised, here’s how I did my hitsounds. This isn’t the whole thing, but it gives you your basic functionality for body and head hitsounds (the other stuff is just sizzle on the steak).
<pre>
diff -urN et-sdk/src/cgame/cg_local.h hitsounds/src/cgame/cg_local.h
— et-sdk/src/cgame/cg_local.h 2005-07-10 02:17:34.000000000 -0400
+++ hitsounds/src/cgame/cg_local.h 2005-08-12 09:11:20.000000000 -0400
@@ -1570,6 +1570,12 @@

sfxHandle_t sndMedicCall[2];
  • // Jaybird - hitsounds

  • sfxHandle_t hitsound_hit;

  • sfxHandle_t hitsound_headshot;

  • sfxHandle_t hitsound_team_hit;

  • sfxHandle_t hitsound_team_head;

  • qhandle_t ccStamps[2];
    qhandle_t ccFilterPics[10];
    qhandle_t ccFilterBackOn;
    diff -urN et-sdk/src/cgame/cg_main.c hitsounds/src/cgame/cg_main.c
    — et-sdk/src/cgame/cg_main.c 2005-07-10 02:17:34.000000000 -0400
    +++ hitsounds/src/cgame/cg_main.c 2005-08-12 09:13:51.000000000 -0400
    @@ -1230,6 +1230,12 @@

    cgs.media.sndMedicCall[0] = trap_S_RegisterSound (“sound/chat/axis/medic.wav”, qfalse );
    cgs.media.sndMedicCall[1] = trap_S_RegisterSound (“sound/chat/allies/medic.wav”, qfalse );

  • // Jaybird - register hitsounds

  • cgs.media.hitsound_hit = trap_S_RegisterSound(“yoursoundhere.wav”, qfalse );

  • cgs.media.hitsound_headshot = trap_S_RegisterSound(“yoursoundhere.wav”, qfalse );

  • cgs.media.hitsound_team_hit = trap_S_RegisterSound(“yoursoundhere.wav”, qfalse );

  • cgs.media.hitsound_team_headshot = trap_S_RegisterSound(“yoursoundhere.wav”, qfalse);

    // FIXME: send as a special event
    diff -urN et-sdk/src/cgame/cg_playerstate.c hitsounds/src/cgame/cg_playerstate.c
    — et-sdk/src/cgame/cg_playerstate.c 2005-07-10 02:17:34.000000000 -0400
    +++ hitsounds/src/cgame/cg_playerstate.c 2005-08-12 09:17:27.000000000 -0400
    @@ -369,6 +369,20 @@
    ==================
    */
    void CG_CheckLocalSounds( playerState_t *ps, playerState_t *ops ) {

  • // Jaybird - Hitsounds

  • if ( ps->persistant[PERS_HITS] > ops->persistant[PERS_HITS]) {

  •   if (ps-&gt;persistant[PERS_HEADSHOTS] &gt; ops-&gt;persistant[PERS_HEADSHOTS] )
    
  •   	trap_S_StartLocalSound( cgs.media.hitsound_headshot, CHAN_LOCAL_SOUND );
    
  •   else
    
  •   	trap_S_StartLocalSound( cgs.media.hitsound_hit, CHAN_LOCAL_SOUND );
    
  • } else if ( ps->persistant[PERS_HITS] < ops->persistant[PERS_HITS] ) {

  •   if ( ps-&gt;persistant[PERS_HEADSHOTS] &lt; ops-&gt;persistant[PERS_HEADSHOTS] )
    
  •   	trap_S_StartLocalSound( cgs.media.hitsound_team_head, CHAN_LOCAL_SOUND );
    
  •   else
    
  •   	trap_S_StartLocalSound( cgs.media.hitsound_team_hit, CHAN_LOCAL_SOUND );
    
  • }

  • // health changes of more than -1 should make pain sounds
    if ( ps->stats[STAT_HEALTH] < ops->stats[STAT_HEALTH] - 1 ) {
    if ( ps->stats[STAT_HEALTH] > 0 ) {
    diff -urN et-sdk/src/game/bg_public.h hitsounds/src/game/bg_public.h
    — et-sdk/src/game/bg_public.h 2005-07-10 02:17:34.000000000 -0400
    +++ hitsounds/src/game/bg_public.h 2005-08-12 09:07:45.000000000 -0400
    @@ -608,7 +608,7 @@
    PERS_RESPAWNS_PENALTY, // how many respawns you have to sit through before respawning again

    PERS_REVIVE_COUNT,

  • PERS_BLEH_2,
  • PERS_HEADSHOTS,
    PERS_BLEH_3,

    // Rafael - mg42 // (SA) I don’t understand these here. can someone explain?
    diff -urN et-sdk/src/game/g_combat.c hitsounds/src/game/g_combat.c
    — et-sdk/src/game/g_combat.c 2005-07-10 02:17:34.000000000 -0400
    +++ hitsounds/src/game/g_combat.c 2005-08-12 09:20:17.000000000 -0400
    @@ -1354,13 +1354,22 @@
    }
    }
    }

  • // Jaybird - move this here to (possibly) increment headshots counter

  • headShot = IsHeadShot(targ, dir, point, mod);

  • // add to the attacker’s hit counter
  • if ( attacker->client && targ != attacker && targ->health > 0 ) {
  • // Jaybird - add to counters for hitsounds.
  • // The addition/subtraction of damage dealt was useless,
  • // and doing a simple increment/decrement to work better for me.
  • if ( attacker->client && targ->client && targ != attacker && targ->health > 0 ) {
    if ( OnSameTeam( targ, attacker ) ) {
  •   	attacker-&gt;client-&gt;ps.persistant[PERS_HITS] -= damage;
    
  •   	attacker-&gt;client-&gt;ps.persistant[PERS_HITS]--;
    
  •   	if (headShot)
    
  •   		attacker-&gt;client-&gt;ps.persistant[PERS_HEADSHOTS]--;
      } else {
    
  •   	attacker-&gt;client-&gt;ps.persistant[PERS_HITS] += damage;
    
  •   	attacker-&gt;client-&gt;ps.persistant[PERS_HITS]++;
    
  •   	if (headShot)
    
  •   		attacker-&gt;client-&gt;ps.persistant[PERS_HEADSHOTS]++;
      }
    
    }

@@ -1398,7 +1407,6 @@
}
}

  • headShot = IsHeadShot(targ, dir, point, mod);
    if ( headShot ) {
    if( take * 2 < 50 ) // head shots, all weapons, do minimum 50 points damage
    take = 50;
    </pre>

If you don’t know how to read the patch format, the lines starting with - mean delete that line, and the lines starting with + mean add that line. Lines without either you should not touch.

Hope someone finds this helpful :smiley: