LucasForums

LucasForums (http://www.lucasforums.com/index.php)
-   JA Coding (http://www.lucasforums.com/forumdisplay.php?f=539)
-   -   TUTORIAL: Melee Button (http://www.lucasforums.com/showthread.php?t=195087)

-=*Raz0r*=- 01-11-2009 10:30 PM

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

-=*Raz0r*=- 01-12-2009 01:22 PM

EDIT: Merged with first post.

-=*Raz0r*=- 04-18-2009 10:03 AM

I know it seems like a triple post, but I've just updated this tutorial and stamped out some bugs.

Also, take note of the dates. ;o

Tinny 04-18-2009 02:26 PM

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!

-=*Raz0r*=- 04-18-2009 02:45 PM

TINNY GIVING ME PRAISE?!?! o_o...

Man, you're like a legend to me.
Playing OldSchool is like what got me into modding. o_O!
Seriously, you're awesome and thanks for the feedback!

Tinny 04-18-2009 03:13 PM

Hey dude, I was just watching the videos of Jedi Knight Galaxies and it looks sweeet!! You're motivating me to complete my mod!

-=*Raz0r*=- 04-18-2009 03:15 PM

I'm not actually a part of JKG dev-team, just a community member. o_O
I offer my help if they ever need it (including promoting in my sig :P) as they have helped me too.

But yeah, that will be an epic mod once it's made. o_O

Tinny 04-18-2009 03:29 PM

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.

-=*Raz0r*=- 04-18-2009 03:47 PM

That's what I plan to do, create tutorials for all the fun features like these!

Of-course each mod should be unique, but this should help those newbies that want to learn to be cool like us! (heh)

-=*Raz0r*=- 04-24-2009 06:43 PM

Updated.

My bad, I said search for 'if (PM_CanSetWeaponAnims())' and it turns out there was 4 instances of that in bg_pmove.c, all in the PM_Weapon() function.
So I included some more code so you know where to look now. ;o


All times are GMT -4. The time now is 01:56 PM.

Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
LFNetwork, LLC ©2002-2011 - All rights reserved.