[GENERAL] What you don't know about the code

Locked
3xistence
Posts: 90
Joined: Sun May 13, 2012 3:02 pm

[GENERAL] What you don't know about the code

Post by 3xistence »

So, i'm here from some months, sometimes inactive sometimes more active.
By the way, i wanted to create this topic for all users, like me, who comes from a different program language and, most of all, doesn't know exactly how to understand completly the source code or else.

So i suggest this topic in order to put your questions about the "dark" side of the code that you aren't able to understand.

So i propose in order to start a piece of cpma source code about hitsounds:
in cg_playerstate.c
in CG_CheckLocalSounds, change this

// hit changes
if ( ps->persistant[PERS_HITS] > ops->persistant[PERS_HITS] ) {
trap_S_StartLocalSound( cgs.media.hitSound[2], CHAN_LOCAL_SOUND );
} else if ( ps->persistant[PERS_HITS] < ops->persistant[PERS_HITS] ) {
trap_S_StartLocalSound( cgs.media.hitTeamSound, CHAN_LOCAL_SOUND );
}

to

// hit changes
// CPM: Hit tones
if (cpm_hittones)
{
int delta;

delta = ps->persistant[PERS_HITS] - ops->persistant[PERS_HITS];

if (delta > 75)
trap_S_StartLocalSound( cgs.media.hitSound[3], CHAN_LOCAL_SOUND );
else if (delta > 50)
trap_S_StartLocalSound( cgs.media.hitSound[2], CHAN_LOCAL_SOUND );
else if (delta > 25)
trap_S_StartLocalSound( cgs.media.hitSound[1], CHAN_LOCAL_SOUND );
else if (delta > 0)
trap_S_StartLocalSound( cgs.media.hitSound[0], CHAN_LOCAL_SOUND );
else if (delta < 0)
trap_S_StartLocalSound( cgs.media.hitTeamSound, CHAN_LOCAL_SOUND );
}
else
{
if ( ps->persistant[PERS_HITS] > ops->persistant[PERS_HITS] ) {
trap_S_StartLocalSound( cgs.media.hitSound[2], CHAN_LOCAL_SOUND );
} else if ( ps->persistant[PERS_HITS] < ops->persistant[PERS_HITS] ) {
trap_S_StartLocalSound( cgs.media.hitTeamSound, CHAN_LOCAL_SOUND );
}
}
// !CPM
the main function is CG_CheckLocalSounds and start with

Code: Select all

void CG_CheckLocalSounds( playerState_t *ps, playerState_t *ops ) {
	int			highScore, health, armor, reward;
	sfxHandle_t sfx;
this is the code about cpma hitsounds, but what i don't understand is that:
delta = ps->persistant[PERS_HITS] - ops->persistant[PERS_HITS];
delta is an integer and is equal to = ?????

i still not understand that ps->persistant and ops->persistant (aren't the same variable inside the playerState_t table?)

[USING THAT CODE]: i got ever the same hitsound....
themuffinman
Posts: 384
Joined: Fri Mar 05, 2010 5:29 pm

Re: [GENERAL] What you don't know about the code

Post by themuffinman »

ops = old playerstate, ie: playerstate from previous frame. That's how you check for changes in player state: by comparing old to current.

Those silly CPM guys! PERS_HITS adds/subtracts whenever you hit an opponent/teammate respectively. It doesn't factor in damage points at all so you can't consider changes in damage dealt with it (unless you modify how it is calculated, which - if you don't have some separate stats calculations of your own - is not recommended as it is used for the overall accuracy calculation). Hence delta will always be either 1, 0 or -1.

What you want to do is set delta to the ps-ops difference in PERS_DMG_GIVEN (that you added earlier on) but only consider it when the difference in PERS_HITS is positive (hit opponent).

Also: make sure that cpm_hittones is in fact not zero.
3xistence
Posts: 90
Joined: Sun May 13, 2012 3:02 pm

Re: [GENERAL] What you don't know about the code

Post by 3xistence »

that's why the hitsound still sounds the ever the same!
instead of using the PERS_DMG_GIVEN isn't more easy setting delta = take +atake ?
themuffinman
Posts: 384
Joined: Fri Mar 05, 2010 5:29 pm

Re: [GENERAL] What you don't know about the code

Post by themuffinman »

Well then you'll need to add another two entries in persEnum_t to communicate G_Damage()'s take and atake. How is that easier?
themuffinman
Posts: 384
Joined: Fri Mar 05, 2010 5:29 pm

Re: [GENERAL] What you don't know about the code

Post by themuffinman »

You can also refer to how it was done in Q3TA where it counted only armor damage using PERS_ATTACKEE_ARMOR. Here's my (slightly) modified version of it:

Code: Select all

// hit changes
	if ( ps->persistant[PERS_HITS] > ops->persistant[PERS_HITS] ) {
		armor  = ps->persistant[PERS_ATTACKEE_ARMOR] & 0xff;
		health = ps->persistant[PERS_ATTACKEE_ARMOR] >> 8;

		cg.targetTime = cg.time;
		cg.targetDmg = armor;
		if ( cg_hitBeep.integer == 1 ) {
			trap_S_StartLocalSound( cgs.media.hitSound, CHAN_LOCAL_SOUND );
		} else if ( cg_hitBeep.integer == 2 ) {
			if (armor <= 50 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundHigh2, CHAN_LOCAL_SOUND );
			} else if (armor > 50 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundHigh1, CHAN_LOCAL_SOUND );
			} else if (armor || health > 75) {
				trap_S_StartLocalSound( cgs.media.hitSoundLow1, CHAN_LOCAL_SOUND );
			} else if (armor || health > 100) {
				trap_S_StartLocalSound( cgs.media.hitSoundLow2, CHAN_LOCAL_SOUND );
			}
		} else if ( cg_hitBeep.integer == 3 ) {
			if (armor <= 50 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundLow2, CHAN_LOCAL_SOUND );
			} else if (armor > 50 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundLow1, CHAN_LOCAL_SOUND );
			} else if (armor || health > 75) {
				trap_S_StartLocalSound( cgs.media.hitSoundHigh1, CHAN_LOCAL_SOUND );
			} else if (armor || health > 100) {
				trap_S_StartLocalSound( cgs.media.hitSoundHigh2, CHAN_LOCAL_SOUND );
			}
		}
	}
3xistence
Posts: 90
Joined: Sun May 13, 2012 3:02 pm

Re: [GENERAL] What you don't know about the code

Post by 3xistence »

Your version work with bip from high to low and reverse but it's based on enemy armor value.
There's a way probably with PERS_DMG_GIVEN but using it i will stuck myself like for CA scores (i wasn't able to complete it)
for example the first thing in my mind for hit sounds was to set:
delta = PERS_DMG_GIVEN - 100
if (delta <= 0 ) it will play a sound for 100 of damage
if (delta >= 25) the other one
if (delta >= 50) etc...
if (delta >= 75) the most low tone
BUT
PERS_DMG_GIVEN doesn't reset is value so probably it will ever increase > of 100 so after the first hit probably i will got ever the sound (delta>= 75)...
That's the same trouble i got with CA individual scores.
I need to reset PERS_DMG_GIVEN after every hit or create some kind of rule in order to check only the last added value on it
themuffinman
Posts: 384
Joined: Fri Mar 05, 2010 5:29 pm

Re: [GENERAL] What you don't know about the code

Post by themuffinman »

Yeah you can probably just ignore that code I posted, isn't that relevant here I suppose.

What did I tell you about checking for changes in values? You subtract ops from ps. So what you want is something along these lines:

Code: Select all

	// hit changes
	if ( ps->persistant[PERS_HITS] > ops->persistant[PERS_HITS] ) {
		const int dmg = ps->persistant[PERS_DMG_GIVEN] - ops->persistant[PERS_DMG_GIVEN];

		cg.targetTime = cg.time;
		cg.targetDmg = dmg;
		if ( cg_hitBeep.integer == 1 ) {
			trap_S_StartLocalSound( cgs.media.hitSound, CHAN_LOCAL_SOUND );
		} else if ( cg_hitBeep.integer == 2 ) {
			if ( dmg > 75 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundLow2, CHAN_LOCAL_SOUND );
			} else if ( dmg > 50 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundLow1, CHAN_LOCAL_SOUND );
			} else if ( dmg > 25 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundHigh1, CHAN_LOCAL_SOUND );
			} else if ( dmg > 0 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundHigh2, CHAN_LOCAL_SOUND );
			}
		} else if ( cg_hitBeep.integer == 3 ) {
			if ( dmg > 75 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundHigh2, CHAN_LOCAL_SOUND );
			} else if ( dmg > 50 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundHigh1, CHAN_LOCAL_SOUND );
			} else if ( dmg > 25 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundLow1, CHAN_LOCAL_SOUND );
			} else if ( dmg > 0 ) {
				trap_S_StartLocalSound( cgs.media.hitSoundLow2, CHAN_LOCAL_SOUND );
			}
		}
	}  else if ( ps->persistant[PERS_HITS] < ops->persistant[PERS_HITS] ) {
		trap_S_StartLocalSound( cgs.media.hitTeamSound, CHAN_LOCAL_SOUND );
	}
Then if your server side code isn't working, what you need is this in G_Damage() right after PERS_ATTACKEE_ARMOR is set:

Code: Select all

attacker->client->ps.persistant[PERS_DMG_GIVEN] += damage;
You can also remove all references to PERS_ATTACKEE_ARMOR while you're at it.
Locked