This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/client/cl_phys.c

366 lines
11 KiB
C
Raw Normal View History

2008-12-25 22:00:00 +01:00
//=======================================================================
// Copyright XashXT Group 2008 <20>
// cl_physics.c - client physic and prediction
//=======================================================================
2007-06-21 22:00:00 +02:00
2008-06-09 22:00:00 +02:00
#include "common.h"
2007-06-21 22:00:00 +02:00
#include "client.h"
2008-11-25 22:00:00 +01:00
#include "matrix_lib.h"
2008-08-30 22:00:00 +02:00
#include "const.h"
2007-06-21 22:00:00 +02:00
/*
===================
CL_CheckPredictionError
===================
*/
2009-01-05 22:00:00 +01:00
void CL_CheckPredictionError( void )
2007-06-21 22:00:00 +02:00
{
2009-01-05 22:00:00 +01:00
int frame;
int delta[3];
int len;
2007-06-21 22:00:00 +02:00
2009-01-05 22:00:00 +01:00
if( !cl_predict->integer ) return;
2007-06-21 22:00:00 +02:00
// calculate the last usercmd_t we sent that the server has processed
2008-07-10 22:00:00 +02:00
frame = cls.netchan.incoming_acknowledged;
frame &= (CMD_BACKUP-1);
2007-06-21 22:00:00 +02:00
// compare what the server returned with what we had predicted it to be
2008-07-10 22:00:00 +02:00
VectorSubtract (cl.frame.ps.origin, cl.predicted_origins[frame], delta);
2007-06-21 22:00:00 +02:00
// save the prediction error for interpolation
len = abs(delta[0]) + abs(delta[1]) + abs(delta[2]);
2008-07-31 22:00:00 +02:00
if( len > 640 ) // 80 world units
2007-06-21 22:00:00 +02:00
{ // a teleport or something
VectorClear (cl.prediction_error);
}
else
{
2007-10-31 22:00:00 +01:00
if (cl_showmiss->value && (delta[0] || delta[1] || delta[2]))
Msg ("prediction miss on %i: %i\n", cl.frame.serverframe, delta[0] + delta[1] + delta[2]);
2007-06-21 22:00:00 +02:00
2008-07-10 22:00:00 +02:00
VectorCopy (cl.frame.ps.origin, cl.predicted_origins[frame]);
2007-06-21 22:00:00 +02:00
// save for error itnerpolation
2008-07-31 22:00:00 +02:00
VectorCopy( delta, cl.prediction_error );
2007-06-21 22:00:00 +02:00
}
}
2008-12-25 22:00:00 +01:00
/*
===============================================================================
LINE TESTING IN HULLS
===============================================================================
*/
int CL_ContentsMask( const edict_t *passedict )
{
if( passedict )
{
if( passedict->v.flags & FL_MONSTER )
return MASK_MONSTERSOLID;
else if( passedict->v.flags & FL_CLIENT )
return MASK_PLAYERSOLID;
else if( passedict->v.solid == SOLID_TRIGGER )
return CONTENTS_SOLID|CONTENTS_BODY;
return MASK_SOLID;
}
return MASK_SOLID;
}
2007-06-21 22:00:00 +02:00
/*
2008-07-30 22:00:00 +02:00
==================
CL_Trace
==================
2007-06-21 22:00:00 +02:00
*/
2008-12-25 22:00:00 +01:00
trace_t CL_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, edict_t *passedict, int contentsmask )
2007-06-21 22:00:00 +02:00
{
2008-07-30 22:00:00 +02:00
vec3_t hullmins, hullmaxs;
int i, bodycontents;
bool pointtrace;
2008-12-25 22:00:00 +01:00
edict_t *traceowner, *touch;
2007-06-21 22:00:00 +02:00
trace_t trace;
2008-07-30 22:00:00 +02:00
vec3_t clipboxmins, clipboxmaxs; // bounding box of entire move area
vec3_t clipmins, clipmaxs; // size of the moving object
vec3_t clipmins2, clipmaxs2; // size when clipping against monsters
vec3_t clipstart, clipend; // start and end origin of move
trace_t cliptrace; // trace results
matrix4x4 matrix, imatrix; // matrices to transform into/out of other entity's space
cmodel_t *model; // model of other entity
int numtouchedicts = 0; // list of entities to test for collisions
2008-12-25 22:00:00 +01:00
edict_t *touchedicts[MAX_EDICTS];
2008-07-30 22:00:00 +02:00
VectorCopy( start, clipstart );
VectorCopy( end, clipend );
VectorCopy( mins, clipmins );
VectorCopy( maxs, clipmaxs );
VectorCopy( mins, clipmins2 );
VectorCopy( maxs, clipmaxs2 );
// clip to world
pe->ClipToWorld( &cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, contentsmask );
cliptrace.startstuck = cliptrace.startsolid;
2008-12-15 22:00:00 +01:00
if( cliptrace.startsolid || cliptrace.fraction < 1 )
2008-12-25 22:00:00 +01:00
cliptrace.ent = (edict_t *)EDICT_NUM( 0 );
2008-07-30 22:00:00 +02:00
if( type == MOVE_WORLDONLY ) return cliptrace;
if( type == MOVE_MISSILE )
2007-06-21 22:00:00 +02:00
{
2008-07-30 22:00:00 +02:00
for( i = 0; i < 3; i++ )
{
clipmins2[i] -= 15;
clipmaxs2[i] += 15;
2007-06-21 22:00:00 +02:00
}
2008-07-30 22:00:00 +02:00
}
// get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
VectorCopy( clipmins, hullmins );
VectorCopy( clipmaxs, hullmaxs );
// create the bounding box of the entire move
for( i = 0; i < 3; i++ )
{
clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
}
// if the passedict is world, make it NULL (to avoid two checks each time)
2008-12-25 22:00:00 +01:00
if( passedict == EDICT_NUM( 0 )) passedict = NULL;
2008-07-30 22:00:00 +02:00
// figure out whether this is a point trace for comparisons
pointtrace = VectorCompare( clipmins, clipmaxs );
// precalculate passedict's owner edict pointer for comparisons
2008-12-25 22:00:00 +01:00
if( passedict && passedict->v.owner )
traceowner = passedict->v.owner;
else traceowner = NULL;
2008-07-30 22:00:00 +02:00
// clip to entities
// because this uses World_EntitiestoBox, we know all entity boxes overlap
// the clip region, so we can skip culling checks in the loop below
2008-12-25 22:00:00 +01:00
numtouchedicts = 0;// FIXME: CL_AreaEdicts( clipboxmins, clipboxmaxs, touchedicts, host.max_edicts );
2008-07-30 22:00:00 +02:00
if( numtouchedicts > host.max_edicts )
{
// this never happens
MsgDev( D_WARN, "CL_AreaEdicts returned %i edicts, max was %i\n", numtouchedicts, host.max_edicts );
numtouchedicts = host.max_edicts;
}
for( i = 0; i < numtouchedicts; i++ )
{
touch = touchedicts[i];
2008-12-25 22:00:00 +01:00
if( touch->v.solid < SOLID_BBOX ) continue;
if( type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP )
2008-07-30 22:00:00 +02:00
continue;
if( passedict )
{
// don't clip against self
if( passedict == touch ) continue;
// don't clip owned entities against owner
if( traceowner == touch ) continue;
// don't clip owner against owned entities
2008-12-25 22:00:00 +01:00
if( passedict == touch->v.owner ) continue;
2008-07-30 22:00:00 +02:00
// don't clip points against points (they can't collide)
2008-12-25 22:00:00 +01:00
if( pointtrace && VectorCompare( touch->v.mins, touch->v.maxs ) && (type != MOVE_MISSILE || !(touch->v.flags & FL_MONSTER)))
2008-07-30 22:00:00 +02:00
continue;
2007-06-21 22:00:00 +02:00
}
2008-07-30 22:00:00 +02:00
bodycontents = CONTENTS_BODY;
2007-06-21 22:00:00 +02:00
2008-07-30 22:00:00 +02:00
// might interact, so do an exact clip
model = NULL;
2008-12-25 22:00:00 +01:00
if( touch->v.solid == SOLID_BSP || type == MOVE_HITMODEL )
2007-06-21 22:00:00 +02:00
{
2008-12-25 22:00:00 +01:00
uint modelindex = (uint)touch->v.modelindex;
2008-07-30 22:00:00 +02:00
// if the modelindex is 0, it shouldn't be SOLID_BSP!
if( modelindex > 0 && modelindex < MAX_MODELS )
2008-12-25 22:00:00 +01:00
model = cl.models[touch->v.modelindex];
2007-06-21 22:00:00 +02:00
}
2008-12-25 22:00:00 +01:00
if( model ) Matrix4x4_CreateFromEntity( matrix, touch->v.origin[0], touch->v.origin[1], touch->v.origin[2], touch->v.angles[0], touch->v.angles[1], touch->v.angles[2], 1 );
else Matrix4x4_CreateTranslate( matrix, touch->v.origin[0], touch->v.origin[1], touch->v.origin[2] );
2008-07-30 22:00:00 +02:00
Matrix4x4_Invert_Simple( imatrix, matrix );
2008-12-25 22:00:00 +01:00
if( touch->v.flags & FL_MONSTER )
pe->ClipToGenericEntity(&trace, model, touch->v.mins, touch->v.maxs, bodycontents, matrix, imatrix, clipstart, clipmins2, clipmaxs2, clipend, contentsmask );
else pe->ClipToGenericEntity(&trace, model, touch->v.mins, touch->v.maxs, bodycontents, matrix, imatrix, clipstart, clipmins, clipmaxs, clipend, contentsmask );
pe->CombineTraces( &cliptrace, &trace, (edict_t *)touch, touch->v.solid == SOLID_BSP );
2008-07-30 22:00:00 +02:00
}
return cliptrace;
}
2009-01-11 22:00:00 +01:00
/*
================
CL_CheckVelocity
================
*/
void CL_CheckVelocity( edict_t *ent )
{
int i;
float wishspeed;
// bound velocity
for( i = 0; i < 3; i++ )
{
if(IS_NAN(ent->v.velocity[i]))
{
MsgDev( D_INFO, "Got a NaN velocity on entity #%i (%s)\n", NUM_FOR_EDICT( ent ), STRING( ent->v.classname ));
ent->v.velocity[i] = 0;
}
if (IS_NAN(ent->v.origin[i]))
{
MsgDev( D_INFO, "Got a NaN origin on entity #%i (%s)\n", NUM_FOR_EDICT( ent ), STRING( ent->v.classname ));
ent->v.origin[i] = 0;
}
}
// LordHavoc: max velocity fix, inspired by Maddes's source fixes, but this is faster
wishspeed = DotProduct( ent->v.velocity, ent->v.velocity );
if( wishspeed > ( clgame.maxVelocity * clgame.maxVelocity ))
{
wishspeed = clgame.maxVelocity / com.sqrt( wishspeed );
ent->v.velocity[0] *= wishspeed;
ent->v.velocity[1] *= wishspeed;
ent->v.velocity[2] *= wishspeed;
}
}
2008-07-30 22:00:00 +02:00
/*
====================
CL_ClipMoveToEntities
====================
*/
void CL_ClipMoveToEntities ( vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, trace_t *tr )
{
/*
for( i = 0; i < cl.frame.num_entities; i++ )
{
num = (cl.frame.parse_entities + i)&(MAX_PARSE_ENTITIES-1);
ent = &cl_parse_entities[num];
if(!ent->solid) continue;
if(ent->number == cl.playernum + 1) continue;
if( ent->solid == SOLID_BMODEL )
{
// special value for bmodel
cmodel = cl.model_clip[ent->modelindex];
if(!cmodel) continue;
angles = ent->angles;
}
else
{ // encoded bbox
x = (ent->solid & 255);
zd = ((ent->solid>>8) & 255);
zu = ((ent->solid>>16) & 255) - 32;
bmins[0] = bmins[1] = -x;
bmaxs[0] = bmaxs[1] = x;
bmins[2] = -zd;
bmaxs[2] = zu;
angles = ent->angles;
2007-06-21 22:00:00 +02:00
}
}
2008-07-30 22:00:00 +02:00
*/
}
2007-06-21 22:00:00 +02:00
/*
================
CL_PMTrace
================
*/
2008-07-27 22:00:00 +02:00
void CL_PMTrace( vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, trace_t *tr )
2007-06-21 22:00:00 +02:00
{
2008-08-30 22:00:00 +02:00
*tr = CL_Trace( start, mins, maxs, end, MOVE_NORMAL, NULL, CONTENTS_SOLID|CONTENTS_PLAYERCLIP|CONTENTS_BODY );
2007-06-21 22:00:00 +02:00
}
2008-11-25 22:00:00 +01:00
int CL_PointContents( const vec3_t point )
2007-06-21 22:00:00 +02:00
{
2008-07-30 22:00:00 +02:00
// get world supercontents at this point
if( cl.worldmodel && cl.worldmodel->PointContents )
return cl.worldmodel->PointContents( point, cl.worldmodel );
return 0;
2007-06-21 22:00:00 +02:00
}
2008-11-25 22:00:00 +01:00
bool CL_AmbientLevel( const vec3_t point, float *volumes )
{
// get world supercontents at this point
if( cl.worldmodel && cl.worldmodel->AmbientLevel )
return cl.worldmodel->AmbientLevel( point, volumes, cl.worldmodel );
return 0;
}
2007-06-21 22:00:00 +02:00
/*
=================
CL_PredictMovement
Sets cl.predicted_origin and cl.predicted_angles
=================
*/
void CL_PredictMovement (void)
{
2008-07-03 22:00:00 +02:00
int ack, current;
int frame;
int oldframe;
2009-01-05 22:00:00 +01:00
entvars_t pmove;
2008-07-03 22:00:00 +02:00
usercmd_t *cmd;
int i;
float step;
float oldz;
2007-06-21 22:00:00 +02:00
2009-01-05 22:00:00 +01:00
if( cls.state != ca_active ) return;
if( cl_paused->value ) return;
2007-06-21 22:00:00 +02:00
2009-01-05 22:00:00 +01:00
pmove = EDICT_NUM( cl.playernum + 1 )->v;
2009-01-14 22:00:00 +01:00
if( !cl_predict->value || cl.frame.ps.ed_flags & ESF_NO_PREDICTION )
2007-11-18 22:00:00 +01:00
{
// just set angles
2009-01-05 22:00:00 +01:00
for( i = 0; i < 3; i++ )
cl.predicted_angles[i] = cl.viewangles[i] + SHORT2ANGLE( cl.frame.ps.delta_angles[i] );
2007-06-21 22:00:00 +02:00
return;
}
2008-07-10 22:00:00 +02:00
ack = cls.netchan.incoming_acknowledged;
current = cls.netchan.outgoing_sequence;
2007-06-21 22:00:00 +02:00
// if we are too far out of date, just freeze
2009-01-05 22:00:00 +01:00
if( current - ack >= CMD_BACKUP )
2007-06-21 22:00:00 +02:00
{
2009-01-05 22:00:00 +01:00
if( cl_showmiss->value )
Msg( "exceeded CMD_BACKUP\n" );
2007-06-21 22:00:00 +02:00
return;
}
2007-11-04 22:00:00 +01:00
// SCR_DebugGraph (current - ack - 1, COLOR_0);
2007-06-21 22:00:00 +02:00
frame = 0;
// run frames
2009-01-05 22:00:00 +01:00
while( ++ack < current )
2007-06-21 22:00:00 +02:00
{
frame = ack & (CMD_BACKUP-1);
cmd = &cl.cmds[frame];
2009-01-05 22:00:00 +01:00
pe->PlayerMove( &pmove, cmd, NULL, true );
2007-06-21 22:00:00 +02:00
// save for debug checking
2009-01-05 22:00:00 +01:00
VectorCopy( pmove.origin, cl.predicted_origins[frame] );
2007-06-21 22:00:00 +02:00
}
oldframe = (ack-2) & (CMD_BACKUP-1);
2008-07-03 22:00:00 +02:00
2009-01-05 22:00:00 +01:00
if( pmove.flags & FL_ONGROUND )
2007-06-21 22:00:00 +02:00
{
2008-07-03 22:00:00 +02:00
oldz = cl.predicted_origins[oldframe][2];
2009-01-05 22:00:00 +01:00
step = pmove.origin[2] - oldz;
2008-07-11 22:00:00 +02:00
if( step > 63 && step < 160 )
2008-07-03 22:00:00 +02:00
{
2008-07-31 22:00:00 +02:00
cl.predicted_step = step;
2008-07-03 22:00:00 +02:00
cl.predicted_step_time = cls.realtime - cls.frametime * 500;
}
2007-06-21 22:00:00 +02:00
}
// copy results out for rendering
2009-01-05 22:00:00 +01:00
VectorCopy( pmove.origin, cl.predicted_origin );
2009-01-06 22:00:00 +01:00
VectorCopy( pmove.viewangles, cl.predicted_angles );
2007-06-21 22:00:00 +02:00
}