Jump to content

Home

TUTORIAL: Melee Button


-=*Raz0r*=-

Recommended Posts

so, I'm not sure if I'm supposed to/allowed to do this, but I might as-well start writing little tutorials to help out beginner coders add some fun features.

 

 

 

 

 

 

 

 

What you will achieve

The purpose of this modification is to allow the player to perform a melee attack (kick) regardless of the weapon selected.

 

 

 

 

 

 

 

 

Before we get started

I would like to point out, we will be adding a new button to the game (the buttons are defined in q_shared.h), so you must make sure you have not already reached your limit of buttons.

 

 

 

 

 

 

 

 

Files edited

- bg_pmove.c (cgame and game)

- q_shared.h (global)

- g_active (game)

 

 

 

 

 

 

 

 

Step 1

Open up bg_pmove.c

Within the PM_Weapon function, do a search for this code:

 

if (PM_CanSetWeaponAnims())
{
	if (pm->ps->weapon == WP_THERMAL ||
		pm->ps->weapon == WP_TRIP_MINE ||
		pm->ps->weapon == WP_DET_PACK)

 

Before all of this, insert the following code:

//[MeleeButton]
if ((pm->cmd.buttons & BUTTON_MELEE))
{ //ok, try a kick I guess.
	if (!BG_KickingAnim(pm->ps->torsoAnim) &&
		!BG_KickingAnim(pm->ps->legsAnim))
	{
		int kickMove = LS_KICK_F;
		if (kickMove != -1)
		{
			int kickAnim = saberMoveData[kickMove].animToUse;

			if (kickAnim != -1)
			{
				PM_SetAnim(SETANIM_BOTH, kickAnim, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLD, 0);
				if (pm->ps->legsAnim == kickAnim)
				{
					pm->ps->weaponTime = pm->ps->legsTimer;
					return;
				}
			}
		}
		//if got here then no move to do so put torso into leg idle or whatever
		if (pm->ps->torsoAnim != pm->ps->legsAnim)
		{
			PM_SetAnim(SETANIM_BOTH, pm->ps->legsAnim, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLD, 0);
		}
		pm->ps->weaponTime = 0;
		return;
	}
}
//[/MeleeButton]

 

This basically says; if we can do a kick, then do it.

 

 

 

 

 

 

 

 

Step 2 (optional)

Well, we have a melee button, so why do we want another button to kick with the 'melee' weapon?

You can remove this by going into bg_pmove.c and commenting this code:

 

(1) Above PM_Weapon function

//[MeleeButton]
//extern int PM_KickMoveForConditions(void);
//[/MeleeButton]

 

(2) Inside PM_Weapon function

		//[MeleeButton]
		//Raz - may aswell remove this then
		/*
		else if (pm->debugMelee &&
			(pm->cmd.buttons & BUTTON_ALT_ATTACK))
		{ //kicks
			if (!BG_KickingAnim(pm->ps->torsoAnim) &&
				!BG_KickingAnim(pm->ps->legsAnim))
			{
				int kickMove = PM_KickMoveForConditions();
				if (kickMove == LS_HILT_BASH)
				{ //yeah.. no hilt to bash with!
					kickMove = LS_KICK_F;
				}

				if (kickMove != -1)
				{
					if ( pm->ps->groundEntityNum == ENTITYNUM_NONE )
					{//if in air, convert kick to an in-air kick
						float gDist = PM_GroundDistance();
						//let's only allow air kicks if a certain distance from the ground
						//it's silly to be able to do them right as you land.
						//also looks wrong to transition from a non-complete flip anim...
						if ((!BG_FlippingAnim( pm->ps->legsAnim ) || pm->ps->legsTimer <= 0) &&
							gDist > 64.0f && //strict minimum
							gDist > (-pm->ps->velocity[2])-64.0f //make sure we are high to ground relative to downward velocity as well
							)
						{
							switch ( kickMove )
							{
							case LS_KICK_F:
								kickMove = LS_KICK_F_AIR;
								break;
							case LS_KICK_B:
								kickMove = LS_KICK_B_AIR;
								break;
							case LS_KICK_R:
								kickMove = LS_KICK_R_AIR;
								break;
							case LS_KICK_L:
								kickMove = LS_KICK_L_AIR;
								break;
							default: //oh well, can't do any other kick move while in-air
								kickMove = -1;
								break;
							}
						}
						else
						{ //off ground, but too close to ground
							kickMove = -1;
						}
					}
				}

				if (kickMove != -1)
				{
					int kickAnim = saberMoveData[kickMove].animToUse;

					if (kickAnim != -1)
					{
						PM_SetAnim(SETANIM_BOTH, kickAnim, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLD, 0);
						if (pm->ps->legsAnim == kickAnim)
						{
							pm->ps->weaponTime = pm->ps->legsTimer;
							return;
						}
					}
				}
			}

			//if got here then no move to do so put torso into leg idle or whatever
			if (pm->ps->torsoAnim != pm->ps->legsAnim)
			{
				PM_SetAnim(SETANIM_BOTH, pm->ps->legsAnim, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLD, 0);
			}
			pm->ps->weaponTime = 0;
			return;
		}
		*/
		//[/MeleeButton]

 

 

 

 

 

 

 

 

Step 3

Time to actually make the button (BUTTON_MELEE) that the game will understand!

 

Open up q_shared.h and search for #define BUTTON_FORCE_DRAIN

Underneath this line, add #define BUTTON_MELEE 4096

 

 

 

 

 

 

 

 

Step 4

Now to make sure the game knows we're pressing an 'action' button, insert this nifty piece of code below.

 

Open up g_active.c and search for qboolean G_ActionButtonPressed(int buttons)

Inside there, insert this code:

 

//[MeleeButton]
else if (buttons & BUTTON_MELEE)
{
	return qtrue;
}
//[/MeleeButton]

 

 

 

 

 

 

 

 

You're done!

Now you can compile your code, and try it out!

type /bind x +button12 and press x to perform a kick regardless of your weapon!

 

 

 

 

 

 

 

 

!BUGS!

Yes, I'm afraid there is one small bug.

If you use a weapon and hold the 'melee button', it works fine...

However, if you use a saber and hold the 'melee button', your animations will screw up after the first time you kick.

This is due to being dependent on the 'weaponTime' value to say if we can kick or not.

If I can fix it, or if anyone can help me, I will update this tutorial.

 

 

 

 

Other information

Feel free to contact me about any bugs/glitches/etc

Xfire: friedjawa101

Email: raz0r@arbmodding.co.cc

Link to comment
Share on other sites

  • 3 months later...

Hey Raz0r, thanks for the tutorial! I've been meaning to add this functionality so this is awesome. I've always been wary of touching the q_shared.h file since you can easily break the game if you edit it but I think your tutorial irons out the kinks, thanks again!

Link to comment
Share on other sites

Hey, that's still cool. Glad to see the modding community is still alive, there's so much that can be done with this game. Just wish some of the code hidden away from us could be a bit more optimized and flexible otherwise its a great engine. Its still really helpful that you've posted this about melee, i thought oh noes another feature i want to implement that will take some time. Maybe we all could start having a repository of tutorials, that would be epic.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...