summaryrefslogtreecommitdiff
path: root/src/cgame
diff options
context:
space:
mode:
authorTim Angus <tim@ngus.net>2009-10-03 11:37:35 +0000
committerTim Angus <tim@ngus.net>2013-01-03 00:15:11 +0000
commitbac576514472b4f1d70a53078182d29e93c34d59 (patch)
treeacb6f0056003701d26d9d1ed3fb48bb195187be3 /src/cgame
parentbcc79996c4c254a528e7b00f260d8ebd09e2a820 (diff)
* First person weapon animation
Diffstat (limited to 'src/cgame')
-rw-r--r--src/cgame/cg_animation.c4
-rw-r--r--src/cgame/cg_animmapobj.c4
-rw-r--r--src/cgame/cg_buildable.c78
-rw-r--r--src/cgame/cg_local.h7
-rw-r--r--src/cgame/cg_particles.c2
-rw-r--r--src/cgame/cg_players.c81
-rw-r--r--src/cgame/cg_weapons.c164
7 files changed, 175 insertions, 165 deletions
diff --git a/src/cgame/cg_animation.c b/src/cgame/cg_animation.c
index c370c534..693cf6a7 100644
--- a/src/cgame/cg_animation.c
+++ b/src/cgame/cg_animation.c
@@ -31,7 +31,7 @@ Sets cg.snap, cg.oldFrame, and cg.backlerp
cg.time should be between oldFrameTime and frameTime after exit
===============
*/
-void CG_RunLerpFrame( lerpFrame_t *lf )
+void CG_RunLerpFrame( lerpFrame_t *lf, float scale )
{
int f, numFrames;
animation_t *anim;
@@ -61,7 +61,9 @@ void CG_RunLerpFrame( lerpFrame_t *lf )
lf->frameTime = lf->oldFrameTime + anim->frameLerp;
f = ( lf->frameTime - lf->animationTime ) / anim->frameLerp;
+ f *= scale;
numFrames = anim->numFrames;
+
if( anim->flipflop )
numFrames *= 2;
diff --git a/src/cgame/cg_animmapobj.c b/src/cgame/cg_animmapobj.c
index 12253149..ae2309dd 100644
--- a/src/cgame/cg_animmapobj.c
+++ b/src/cgame/cg_animmapobj.c
@@ -31,7 +31,7 @@ CG_DoorAnimation
*/
static void CG_DoorAnimation( centity_t *cent, int *old, int *now, float *backLerp )
{
- CG_RunLerpFrame( &cent->lerpFrame );
+ CG_RunLerpFrame( &cent->lerpFrame, 1.0f );
*old = cent->lerpFrame.oldFrame;
*now = cent->lerpFrame.frame;
@@ -117,7 +117,7 @@ static void CG_AMOAnimation( centity_t *cent, int *old, int *now, float *backLer
cent->lerpFrame.frameTime += delta;
}
- CG_RunLerpFrame( &cent->lerpFrame );
+ CG_RunLerpFrame( &cent->lerpFrame, 1.0f );
cent->miscTime = cg.time;
}
diff --git a/src/cgame/cg_buildable.c b/src/cgame/cg_buildable.c
index 45f1e8dd..7b20b8b8 100644
--- a/src/cgame/cg_buildable.c
+++ b/src/cgame/cg_buildable.c
@@ -427,19 +427,10 @@ cg.time should be between oldFrameTime and frameTime after exit
*/
static void CG_RunBuildableLerpFrame( centity_t *cent )
{
- int f, numFrames;
buildable_t buildable = cent->currentState.modelindex;
lerpFrame_t *lf = &cent->lerpFrame;
- animation_t *anim;
buildableAnimNumber_t newAnimation = cent->buildableAnim & ~( ANIM_TOGGLEBIT|ANIM_FORCEBIT );
- // debugging tool to get no animations
- if( cg_animSpeed.integer == 0 )
- {
- lf->oldFrame = lf->frame = lf->backlerp = 0;
- return;
- }
-
// see if the animation sequence is switching
if( newAnimation != lf->animationNumber || !lf->animation )
{
@@ -466,72 +457,11 @@ static void CG_RunBuildableLerpFrame( centity_t *cent )
trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin,
cg_buildables[ buildable ].sounds[ lf->animationNumber ].sound );
- // if we have passed the current frame, move it to
- // oldFrame and calculate a new frame
- if( cg.time >= lf->frameTime )
- {
- lf->oldFrame = lf->frame;
- lf->oldFrameTime = lf->frameTime;
-
- // get the next frame based on the animation
- anim = lf->animation;
- if( !anim->frameLerp )
- return; // shouldn't happen
-
- if ( cg.time < lf->animationTime )
- lf->frameTime = lf->animationTime; // initial lerp
- else
- lf->frameTime = lf->oldFrameTime + anim->frameLerp;
-
- f = ( lf->frameTime - lf->animationTime ) / anim->frameLerp;
- numFrames = anim->numFrames;
- if(anim->flipflop)
- numFrames *= 2;
+ CG_RunLerpFrame( lf, 1.0f );
- if( f >= numFrames )
- {
- f -= numFrames;
- if( anim->loopFrames )
- {
- f %= anim->loopFrames;
- f += anim->numFrames - anim->loopFrames;
- }
- else
- {
- f = numFrames - 1;
- // the animation is stuck at the end, so it
- // can immediately transition to another sequence
- lf->frameTime = cg.time;
- cent->buildableAnim = cent->currentState.torsoAnim;
- }
- }
-
- if( anim->reversed )
- lf->frame = anim->firstFrame + anim->numFrames - 1 - f;
- else if( anim->flipflop && f >= anim->numFrames )
- lf->frame = anim->firstFrame + anim->numFrames - 1 - ( f % anim->numFrames );
- else
- lf->frame = anim->firstFrame + f;
-
- if( cg.time > lf->frameTime )
- {
- lf->frameTime = cg.time;
- if( cg_debugAnim.integer )
- CG_Printf( "Clamp lf->frameTime\n");
- }
- }
-
- if( lf->frameTime > cg.time + 200 )
- lf->frameTime = cg.time;
-
- if( lf->oldFrameTime > cg.time )
- lf->oldFrameTime = cg.time;
-
- // calculate current lerp value
- if( lf->frameTime == lf->oldFrameTime )
- lf->backlerp = 0;
- else
- lf->backlerp = 1.0 - (float)( cg.time - lf->oldFrameTime ) / ( lf->frameTime - lf->oldFrameTime );
+ // animation ended
+ if( lf->frameTime == cg.time )
+ cent->buildableAnim = cent->currentState.torsoAnim;
}
/*
diff --git a/src/cgame/cg_local.h b/src/cgame/cg_local.h
index 4b375d43..470069c1 100644
--- a/src/cgame/cg_local.h
+++ b/src/cgame/cg_local.h
@@ -569,7 +569,7 @@ typedef struct
typedef struct
{
- lerpFrame_t legs, torso, flag, nonseg;
+ lerpFrame_t legs, torso, nonseg, weapon;
int painTime;
int painDirection; // flip from 0 to 1
@@ -825,6 +825,9 @@ typedef struct weaponInfo_s
qhandle_t barrelModel;
qhandle_t flashModel;
+ animation_t animations[ MAX_WEAPON_ANIMATIONS ];
+ qboolean noDrift;
+
vec3_t weaponMidpoint; // so it will rotate centered instead of by tag
qhandle_t weaponIcon;
@@ -1669,7 +1672,7 @@ void CG_AlienBuildableExplosion( vec3_t origin, vec3_t dir );
//
// cg_animation.c
//
-void CG_RunLerpFrame( lerpFrame_t *lf );
+void CG_RunLerpFrame( lerpFrame_t *lf, float scale );
//
// cg_animmapobj.c
diff --git a/src/cgame/cg_particles.c b/src/cgame/cg_particles.c
index 195c5ff3..c7023dbf 100644
--- a/src/cgame/cg_particles.c
+++ b/src/cgame/cg_particles.c
@@ -2416,7 +2416,7 @@ static void CG_RenderParticle( particle_t *p )
p->lf.animation = &bp->modelAnimation;
//run animation
- CG_RunLerpFrame( &p->lf );
+ CG_RunLerpFrame( &p->lf, 1.0f );
re.oldframe = p->lf.oldFrame;
re.frame = p->lf.frame;
diff --git a/src/cgame/cg_players.c b/src/cgame/cg_players.c
index 78cbe8d8..f729164a 100644
--- a/src/cgame/cg_players.c
+++ b/src/cgame/cg_players.c
@@ -841,90 +841,11 @@ cg.time should be between oldFrameTime and frameTime after exit
*/
static void CG_RunPlayerLerpFrame( clientInfo_t *ci, lerpFrame_t *lf, int newAnimation, float speedScale )
{
- int f, numFrames;
- animation_t *anim;
-
- // debugging tool to get no animations
- if( cg_animSpeed.integer == 0 )
- {
- lf->oldFrame = lf->frame = lf->backlerp = 0;
- return;
- }
-
// see if the animation sequence is switching
if( newAnimation != lf->animationNumber || !lf->animation )
- {
CG_SetLerpFrameAnimation( ci, lf, newAnimation );
- }
- // if we have passed the current frame, move it to
- // oldFrame and calculate a new frame
- if( cg.time >= lf->frameTime )
- {
- lf->oldFrame = lf->frame;
- lf->oldFrameTime = lf->frameTime;
-
- // get the next frame based on the animation
- anim = lf->animation;
- if( !anim->frameLerp )
- return; // shouldn't happen
-
- if( cg.time < lf->animationTime )
- lf->frameTime = lf->animationTime; // initial lerp
- else
- lf->frameTime = lf->oldFrameTime + anim->frameLerp;
-
- f = ( lf->frameTime - lf->animationTime ) / anim->frameLerp;
- f *= speedScale; // adjust for haste, etc
- numFrames = anim->numFrames;
-
- if( anim->flipflop )
- numFrames *= 2;
-
- if( f >= numFrames )
- {
- f -= numFrames;
- if( anim->loopFrames )
- {
- f %= anim->loopFrames;
- f += anim->numFrames - anim->loopFrames;
- }
- else
- {
- f = numFrames - 1;
- // the animation is stuck at the end, so it
- // can immediately transition to another sequence
- lf->frameTime = cg.time;
- }
- }
-
- if( anim->reversed )
- lf->frame = anim->firstFrame + anim->numFrames - 1 - f;
- else if( anim->flipflop && f>=anim->numFrames )
- lf->frame = anim->firstFrame + anim->numFrames - 1 - ( f % anim->numFrames );
- else
- lf->frame = anim->firstFrame + f;
-
- if( cg.time > lf->frameTime )
- {
- lf->frameTime = cg.time;
-
- if( cg_debugAnim.integer )
- CG_Printf( "Clamp lf->frameTime\n" );
- }
- }
-
- if( lf->frameTime > cg.time + 200 )
- lf->frameTime = cg.time;
-
- if( lf->oldFrameTime > cg.time )
- lf->oldFrameTime = cg.time;
-
- // calculate current lerp value
- if( lf->frameTime == lf->oldFrameTime )
- lf->backlerp = 0;
- else
- lf->backlerp = 1.0 - (float)( cg.time - lf->oldFrameTime ) / ( lf->frameTime - lf->oldFrameTime );
+ CG_RunLerpFrame( lf, speedScale );
}
diff --git a/src/cgame/cg_weapons.c b/src/cgame/cg_weapons.c
index e3776779..f9ea2985 100644
--- a/src/cgame/cg_weapons.c
+++ b/src/cgame/cg_weapons.c
@@ -80,6 +80,104 @@ void CG_InitUpgrades( void )
/*
+======================
+CG_ParseWeaponAnimationFile
+
+Read a configuration file containing animation counts and rates
+models/weapons/rifle/animation.cfg, etc
+======================
+*/
+static qboolean CG_ParseWeaponAnimationFile( const char *filename, weaponInfo_t *weapon )
+{
+ char *text_p;
+ int len;
+ int i;
+ char *token;
+ float fps;
+ char text[ 20000 ];
+ fileHandle_t f;
+ animation_t *animations;
+
+ animations = weapon->animations;
+
+ // load the file
+ len = trap_FS_FOpenFile( filename, &f, FS_READ );
+ if( len <= 0 )
+ return qfalse;
+
+ if( len >= sizeof( text ) - 1 )
+ {
+ CG_Printf( "File %s too long\n", filename );
+ return qfalse;
+ }
+
+ trap_FS_Read( text, len, f );
+ text[ len ] = 0;
+ trap_FS_FCloseFile( f );
+
+ // parse the text
+ text_p = text;
+
+ // read information for each frame
+ for( i = WANIM_NONE + 1; i < MAX_WEAPON_ANIMATIONS; i++ )
+ {
+
+ token = COM_Parse( &text_p );
+ if( !*token )
+ break;
+
+ if( !Q_stricmp( token, "noDrift" ) )
+ {
+ weapon->noDrift = qtrue;
+ continue;
+ }
+
+ animations[ i ].firstFrame = atoi( token );
+
+ token = COM_Parse( &text_p );
+ if( !*token )
+ break;
+
+ animations[ i ].numFrames = atoi( token );
+ animations[ i ].reversed = qfalse;
+ animations[ i ].flipflop = qfalse;
+
+ // if numFrames is negative the animation is reversed
+ if( animations[ i ].numFrames < 0 )
+ {
+ animations[ i ].numFrames = -animations[ i ].numFrames;
+ animations[ i ].reversed = qtrue;
+ }
+
+ token = COM_Parse( &text_p );
+ if ( !*token )
+ break;
+
+ animations[i].loopFrames = atoi( token );
+
+ token = COM_Parse( &text_p );
+ if( !*token )
+ break;
+
+ fps = atof( token );
+ if( fps == 0 )
+ fps = 1;
+
+ animations[ i ].frameLerp = 1000 / fps;
+ animations[ i ].initialLerp = 1000 / fps;
+ }
+
+ if( i != MAX_WEAPON_ANIMATIONS )
+ {
+ CG_Printf( "Error parsing animation file: %s\n", filename );
+ return qfalse;
+ }
+
+ return qtrue;
+}
+
+
+/*
===============
CG_ParseWeaponModeSection
@@ -627,6 +725,11 @@ void CG_RegisterWeapon( int weaponNum )
if( !CG_ParseWeaponFile( path, weaponInfo ) )
Com_Printf( S_COLOR_RED "ERROR: failed to parse %s\n", path );
+ Com_sprintf( path, MAX_QPATH, "models/weapons/%s/animation.cfg", BG_FindNameForWeapon( weaponNum ) );
+
+ if( !CG_ParseWeaponAnimationFile( path, weaponInfo ) )
+ Com_Printf( S_COLOR_RED "ERROR: failed to parse %s\n", path );
+
// calc midpoint for rotation
trap_R_ModelBounds( weaponInfo->weaponModel, mins, maxs );
for( i = 0 ; i < 3 ; i++ )
@@ -666,6 +769,53 @@ VIEW WEAPON
*/
/*
+===============
+CG_SetWeaponLerpFrameAnimation
+
+may include ANIM_TOGGLEBIT
+===============
+*/
+static void CG_SetWeaponLerpFrameAnimation( weapon_t weapon, lerpFrame_t *lf, int newAnimation )
+{
+ animation_t *anim;
+
+ lf->animationNumber = newAnimation;
+ newAnimation &= ~ANIM_TOGGLEBIT;
+
+ if( newAnimation < 0 || newAnimation >= MAX_WEAPON_ANIMATIONS )
+ CG_Error( "Bad animation number: %i", newAnimation );
+
+ anim = &cg_weapons[ weapon ].animations[ newAnimation ];
+
+ lf->animation = anim;
+ lf->animationTime = lf->frameTime + anim->initialLerp;
+
+ if( cg_debugAnim.integer )
+ CG_Printf( "Anim: %i\n", newAnimation );
+}
+
+/*
+===============
+CG_WeaponAnimation
+===============
+*/
+static void CG_WeaponAnimation( centity_t *cent, int *old, int *now, float *backLerp )
+{
+ lerpFrame_t *lf = &cent->pe.weapon;
+ entityState_t *es = &cent->currentState;
+
+ // see if the animation sequence is switching
+ if( es->weaponAnim != lf->animationNumber || !lf->animation )
+ CG_SetWeaponLerpFrameAnimation( es->weapon, lf, es->weaponAnim );
+
+ CG_RunLerpFrame( lf, 1.0f );
+
+ *old = lf->oldFrame;
+ *now = lf->frame;
+ *backLerp = lf->backlerp;
+}
+
+/*
=================
CG_MapTorsoToWeaponFrame
@@ -700,10 +850,13 @@ CG_CalculateWeaponPosition
*/
static void CG_CalculateWeaponPosition( vec3_t origin, vec3_t angles )
{
- float scale;
- int delta;
- float fracsin;
- float bob;
+ float scale;
+ int delta;
+ float fracsin;
+ float bob;
+ weaponInfo_t *weapon;
+
+ weapon = &cg_weapons[ cg.predictedPlayerState.weapon ];
VectorCopy( cg.refdef.vieworg, origin );
VectorCopy( cg.refdefViewAngles, angles );
@@ -726,7 +879,7 @@ static void CG_CalculateWeaponPosition( vec3_t origin, vec3_t angles )
}
// drop the weapon when landing
- if( !BG_ClassHasAbility( cg.predictedPlayerState.stats[ STAT_PCLASS ], SCA_NOWEAPONDRIFT ) )
+ if( weapon->noDrift )
{
delta = cg.time - cg.landTime;
if( delta < LAND_DEFLECT_TIME )
@@ -880,6 +1033,7 @@ void CG_AddPlayerWeapon( refEntity_t *parent, playerState_t *ps, centity_t *cent
if( !noGunModel )
{
+ CG_WeaponAnimation( cent, &gun.oldframe, &gun.frame, &gun.backlerp );
CG_PositionEntityOnTag( &gun, parent, parent->hModel, "tag_weapon" );
trap_R_AddRefEntityToScene( &gun );