View Single Post
Old 01-11-2009, 10:30 PM   #1
-=*Raz0r*=-
Rookie
 
-=*Raz0r*=-'s Avatar
 
Join Date: Oct 2006
Location: Australia
Posts: 243
Lightbulb TUTORIAL: Melee Button

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:

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:
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
Code:
//[MeleeButton]
//extern int PM_KickMoveForConditions(void);
//[/MeleeButton]
(2) Inside PM_Weapon function
Code:
			//[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:

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


Last edited by -=*Raz0r*=-; 06-23-2009 at 03:51 AM. Reason: Updated
-=*Raz0r*=- is offline   you may: quote & reply,