Index: src/game/g_local.h =================================================================== --- src/game/g_local.h (revision 848) +++ src/game/g_local.h (working copy) @@ -351,6 +351,14 @@ int adminLevel; } clientPersistant_t; +#define MAX_UNLAGGED_MARKERS 10 +typedef struct unlagged_s { + vec3_t origin; + vec3_t mins; + vec3_t maxs; + qboolean used; +} unlagged_t; + // this structure is cleared on each ClientSpawn(), // except for 'client->pers' and 'client->sess' struct gclient_s @@ -442,6 +450,12 @@ #define RAM_FRAMES 1 // number of frames to wait before retriggering int retriggerArmouryMenu; // frame number to retrigger the armoury menu + + unlagged_t unlaggedHist[ MAX_UNLAGGED_MARKERS ]; + unlagged_t unlaggedBackup; + unlagged_t unlaggedCalc; + int attackTime; + }; @@ -628,6 +642,9 @@ qboolean uncondHumanWin; qboolean alienTeamLocked; qboolean humanTeamLocked; + + int unlaggedIndex; + int unlaggedTimes[ MAX_UNLAGGED_MARKERS ]; } level_locals_t; // @@ -913,6 +930,11 @@ // // g_active.c // +void G_UnlaggedStore( void ); +void G_UnlaggedClear( gentity_t *ent ); +void G_UnlaggedCalc( int time, gentity_t *skipEnt ); +void G_UnlaggedOn( void ); +void G_UnlaggedOff( void ); void ClientThink( int clientNum ); void ClientEndFrame( gentity_t *ent ); void G_RunClient( gentity_t *ent ); @@ -1099,6 +1121,8 @@ extern vmCvar_t g_alienStage2Threshold; extern vmCvar_t g_alienStage3Threshold; +extern vmCvar_t g_unlagged; + extern vmCvar_t g_disabledEquipment; extern vmCvar_t g_disabledClasses; extern vmCvar_t g_disabledBuildables; Index: src/game/g_active.c =================================================================== --- src/game/g_active.c (revision 848) +++ src/game/g_active.c (working copy) @@ -954,6 +954,283 @@ /* ============== + G_UnlaggedStore + + Called on every server frame. Stores position data for the client at that + into client->unlaggedHist[] and the time into level.unlaggedTimes[]. + This data is used by G_UnlaggedCalc() +============== +*/ +void G_UnlaggedStore( void ) +{ + int i = 0; + gentity_t *ent; + unlagged_t *save; + + if( !g_unlagged.integer ) + return; + level.unlaggedIndex++; + if( level.unlaggedIndex >= MAX_UNLAGGED_MARKERS ) + level.unlaggedIndex = 0; + + level.unlaggedTimes[ level.unlaggedIndex ] = level.time; + + for( i = 0; i < level.maxclients; i++ ) + { + ent = &g_entities[ i ]; + save = &ent->client->unlaggedHist[ level.unlaggedIndex ]; + save->used = qfalse; + if( !ent->r.linked || !( ent->r.contents & CONTENTS_BODY ) ) + continue; + if( ent->client->pers.connected != CON_CONNECTED ) + continue; + VectorCopy( ent->r.mins, save->mins ); + VectorCopy( ent->r.maxs, save->maxs ); + VectorCopy( ent->s.pos.trBase, save->origin ); + SnapVector( save->origin ); + save->used = qtrue; + } +} + +/* +============== + G_UnlaggedClear + + Mark all unlaggedHist[] markers for this client invalid. Useful for + preventing teleporting and death. +============== +*/ +void G_UnlaggedClear( gentity_t *ent ) +{ + int i; + + for( i = 0; i < MAX_UNLAGGED_MARKERS; i++ ) + ent->client->unlaggedHist[ i ].used = qfalse; +} + +// FIXME: this should be defined in q_shared.h +#define VectorLerp( f, s, e, r ) (\ + (r)[0]=(s)[0]+(f)*((e)[0]-(s)[0]),\ + (r)[1]=(s)[1]+(f)*((e)[1]-(s)[1]),\ + (r)[2]=(s)[2]+(f)*((e)[2]-(s)[2])) + +/* +============== + G_UnlaggedCalc + + Loops through all active clients and calculates their predicted position + for time then stores it in client->unlaggedCalc +============== +*/ +void G_UnlaggedCalc( int time, gentity_t *rewindEnt ) +{ + int i = 0; + gentity_t *ent; + int startIndex = level.unlaggedIndex; + int stopIndex = -1; + int frameMsec = 0; + float lerp = 0.5f; + + if( !g_unlagged.integer ) + return; + + // clear any calculated values from a previous run + for( i = 0; i < level.maxclients; i++ ) + { + ent = &g_entities[ i ]; + ent->client->unlaggedCalc.used = qfalse; + } + + for( i = 0; i < MAX_UNLAGGED_MARKERS; i++ ) + { + if( level.unlaggedTimes[ startIndex ] <= time ) + break; + stopIndex = startIndex; + if( --startIndex < 0 ) + startIndex = MAX_UNLAGGED_MARKERS - 1; + } + if( i == MAX_UNLAGGED_MARKERS ) + { + // if we searched all markers and the oldest one still isn't old enough + // just use the oldest marker with no lerping + lerp = 0.0f; + } + + // client is on the current frame, no need for unlagged + if( stopIndex == -1 ) + return; + + // lerp between two markers + frameMsec = level.unlaggedTimes[ stopIndex ] - + level.unlaggedTimes[ startIndex ]; + if( frameMsec > 0 ) + { + lerp = ( ( float ) + ( time - level.unlaggedTimes[ startIndex ] ) ) + / ( float ) frameMsec; + } + + for( i = 0; i < level.maxclients; i++ ) + { + ent = &g_entities[ i ]; + if( ent->s.number == rewindEnt->s.number ) + continue; + if( !ent->r.linked || !( ent->r.contents & CONTENTS_BODY ) ) + continue; + if( ent->client->pers.connected != CON_CONNECTED ) + continue; + if( !ent->client->unlaggedHist[ startIndex ].used ) + continue; + if( !ent->client->unlaggedHist[ stopIndex ].used ) + continue; + + // between two unlagged markers + VectorLerp( lerp, ent->client->unlaggedHist[ startIndex ].mins, + ent->client->unlaggedHist[ stopIndex ].mins, + ent->client->unlaggedCalc.mins ); + VectorLerp( lerp, ent->client->unlaggedHist[ startIndex ].maxs, + ent->client->unlaggedHist[ stopIndex ].maxs, + ent->client->unlaggedCalc.maxs ); + VectorLerp( lerp, ent->client->unlaggedHist[ startIndex ].origin, + ent->client->unlaggedHist[ stopIndex ].origin, + ent->client->unlaggedCalc.origin ); + + ent->client->unlaggedCalc.used = qtrue; + } +} + +/* +============== + G_UnlaggedOff + + Reverses the changes made to all active clients by G_UnlaggedOn() +============== +*/ +void G_UnlaggedOff( void ) +{ + int i = 0; + gentity_t *ent; + + if( !g_unlagged.integer ) + return; + + for( i = 0; i < level.maxclients; i++ ) + { + ent = &g_entities[ i ]; + if( !ent->client->unlaggedBackup.used ) + continue; + VectorCopy( ent->client->unlaggedBackup.mins, ent->r.mins ); + VectorCopy( ent->client->unlaggedBackup.maxs, ent->r.maxs ); + VectorCopy( ent->client->unlaggedBackup.origin, ent->r.currentOrigin ); + ent->client->unlaggedBackup.used = qfalse; + trap_LinkEntity( ent ); + } +} + +/* +============== + G_UnlaggedOn + + Called after G_UnlaggedCalc() to apply the calculated values to all active + clients. Once finished tracing, G_UnlaggedOff() must be called to restore + the clients' position data +============== +*/ +void G_UnlaggedOn( void ) +{ + int i = 0; + gentity_t *ent; + + if( !g_unlagged.integer ) + return; + + for( i = 0; i < level.maxclients; i++ ) + { + ent = &g_entities[ i ]; + if( !ent->client->unlaggedCalc.used ) + continue; + if( ent->client->unlaggedBackup.used ) + continue; + if( !ent->r.linked || !( ent->r.contents & CONTENTS_BODY ) ) + continue; + + VectorCopy( ent->r.mins, ent->client->unlaggedBackup.mins ); + VectorCopy( ent->r.maxs, ent->client->unlaggedBackup.maxs ); + VectorCopy( ent->r.currentOrigin, ent->client->unlaggedBackup.origin ); + ent->client->unlaggedBackup.used = qtrue; + + VectorCopy( ent->client->unlaggedCalc.mins, ent->r.mins ); + VectorCopy( ent->client->unlaggedCalc.maxs, ent->r.maxs ); + VectorCopy( ent->client->unlaggedCalc.origin, ent->r.currentOrigin ); + trap_LinkEntity( ent ); + } +} + +/* +============== + G_UnlaggedCollisions + + The only case when the calculated unlagged positions to not match what + rewindEnt sees is when prediction is hampered by a client-client collision. + As a result, the attacking client may move through his victim's + calculated position causing a miss. For this reason we must disable the + use of the calculated position whenever there is any client-client + collision. +============== +*/ +static void G_UnlaggedCollisions( gentity_t *rewindEnt ) +{ + int i, j; + gentity_t *ent; + vec3_t r_absmin, r_absmax; + vec3_t u_absmin, u_absmax; + qboolean collision; + + if( !g_unlagged.integer ) + return; + + VectorAdd( rewindEnt->r.currentOrigin, rewindEnt->r.mins, r_absmin ); + VectorAdd( rewindEnt->r.currentOrigin, rewindEnt->r.maxs, r_absmax ); + r_absmax[ 0 ] += 1; + r_absmax[ 1 ] += 1; + r_absmax[ 2 ] += 1; + r_absmin[ 0 ] -= 1; + r_absmin[ 1 ] -= 1; + r_absmin[ 2 ] -= 1; + + for( i = 0; i < level.maxclients; i++ ) + { + ent = &g_entities[ i ]; + if( !ent->client->unlaggedCalc.used ) + continue; + VectorAdd( ent->client->unlaggedCalc.origin, + ent->client->unlaggedCalc.mins, u_absmin ); + VectorAdd( ent->client->unlaggedCalc.origin, + ent->client->unlaggedCalc.maxs, u_absmax ); + u_absmax[ 0 ] += 1; + u_absmax[ 1 ] += 1; + u_absmax[ 2 ] += 1; + u_absmin[ 0 ] -= 1; + u_absmin[ 1 ] -= 1; + u_absmin[ 2 ] -= 1; + + collision = qtrue; + for( j = 0; j < 3; j++ ) + { + if( r_absmin[ j ] > u_absmax[ j ] || + r_absmax[ j ] < u_absmin[ j ] ) + { + collision = qfalse; + break; + } + } + if( collision ) + ent->client->unlaggedCalc.used = qfalse; + } +} + +/* +============== ClientThink This will be called once for each client frame, which will @@ -1002,6 +1279,9 @@ if( msec > 200 ) msec = 200; + client->attackTime = ucmd->serverTime; + client->attackTime += ( level.time - level.previousTime ); + if( pmove_msec.integer < 8 ) trap_Cvar_Set( "pmove_msec", "8" ); else if( pmove_msec.integer > 33 ) @@ -1039,6 +1319,10 @@ if( !ClientInactivityTimer( client ) ) return; + // calculate where ent is currently seeing all the other active clients + G_UnlaggedCalc( ent->client->attackTime, ent ); + G_UnlaggedCollisions( ent ); + if( client->noclip ) client->ps.pm_type = PM_NOCLIP; else if( client->ps.stats[ STAT_HEALTH ] <= 0 ) @@ -1236,6 +1520,8 @@ VectorCopy( pm.mins, ent->r.mins ); VectorCopy( pm.maxs, ent->r.maxs ); + G_UnlaggedCollisions( ent ); + ent->waterlevel = pm.waterlevel; ent->watertype = pm.watertype; @@ -1278,6 +1564,7 @@ //prevent lerping client->ps.eFlags ^= EF_TELEPORT_BIT; client->ps.eFlags &= ~EF_NODRAW; + G_UnlaggedClear( ent ); //client leaves hovel client->ps.stats[ STAT_STATE ] &= ~SS_HOVELING; Index: src/game/g_buildable.c =================================================================== --- src/game/g_buildable.c (revision 848) +++ src/game/g_buildable.c (working copy) @@ -1190,6 +1190,7 @@ //prevent lerping activator->client->ps.eFlags ^= EF_TELEPORT_BIT; activator->client->ps.eFlags |= EF_NODRAW; + G_UnlaggedClear( activator ); activator->client->ps.stats[ STAT_STATE ] |= SS_HOVELING; activator->client->hovel = self; @@ -1275,6 +1276,7 @@ //prevent lerping builder->client->ps.eFlags ^= EF_TELEPORT_BIT; builder->client->ps.eFlags &= ~EF_NODRAW; + G_UnlaggedClear( builder ); G_SetOrigin( builder, newOrigin ); VectorCopy( newOrigin, builder->client->ps.origin ); Index: src/game/g_main.c =================================================================== --- src/game/g_main.c (revision 848) +++ src/game/g_main.c (working copy) @@ -103,6 +103,8 @@ vmCvar_t g_alienStage2Threshold; vmCvar_t g_alienStage3Threshold; +vmCvar_t g_unlagged; + vmCvar_t g_disabledEquipment; vmCvar_t g_disabledClasses; vmCvar_t g_disabledBuildables; @@ -210,6 +212,8 @@ { &g_alienMaxStage, "g_alienMaxStage", DEFAULT_ALIEN_MAX_STAGE, 0, 0, qfalse }, { &g_alienStage2Threshold, "g_alienStage2Threshold", DEFAULT_ALIEN_STAGE2_THRESH, 0, 0, qfalse }, { &g_alienStage3Threshold, "g_alienStage3Threshold", DEFAULT_ALIEN_STAGE3_THRESH, 0, 0, qfalse }, + + { &g_unlagged, "g_unlagged", "1", CVAR_SERVERINFO | CVAR_ARCHIVE, 0, qfalse }, { &g_disabledEquipment, "g_disabledEquipment", "", CVAR_ROM, 0, qfalse }, { &g_disabledClasses, "g_disabledClasses", "", CVAR_ROM, 0, qfalse }, @@ -2126,12 +2130,6 @@ if( !ent->r.linked && ent->neverFree ) continue; - if( ent->s.eType == ET_MISSILE ) - { - G_RunMissile( ent ); - continue; - } - if( ent->s.eType == ET_BUILDABLE ) { G_BuildableThink( ent, msec ); @@ -2171,6 +2169,25 @@ ClientEndFrame( ent ); } + // save position information for all active clients + G_UnlaggedStore( ); + + // for missle impacts, move every active client one server frame time back + // to compensate for built-in 50ms lag + G_UnlaggedCalc( level.previousTime, NULL ); + G_UnlaggedOn( ); + for( i = MAX_CLIENTS; i < level.num_entities ; i++) + { + ent = &g_entities[ i ]; + if( !ent->inuse ) + continue; + if( ent->freeAfterEvent ) + continue; + if( ent->s.eType == ET_MISSILE ) + G_RunMissile( ent ); + } + G_UnlaggedOff( ); + end = trap_Milliseconds(); //TA: Index: src/game/g_weapon.c =================================================================== --- src/game/g_weapon.c (revision 848) +++ src/game/g_weapon.c (working copy) @@ -179,7 +179,10 @@ VectorMA( muzzle, range, forward, end ); + G_UnlaggedOn( ); trap_Trace( &tr, muzzle, mins, maxs, end, ent->s.number, MASK_SHOT ); + G_UnlaggedOff( ); + if( tr.surfaceFlags & SURF_NOIMPACT ) return; @@ -223,7 +226,16 @@ VectorMA( end, r, right, end ); VectorMA( end, u, up, end ); - trap_Trace( &tr, muzzle, NULL, NULL, end, ent->s.number, MASK_SHOT ); + // don't use unlagged if this is not a client (e.g. turret) + if( ent->client ) + { + G_UnlaggedOn( ); + trap_Trace( &tr, muzzle, NULL, NULL, end, ent->s.number, MASK_SHOT ); + G_UnlaggedOff( ); + } + else + trap_Trace( &tr, muzzle, NULL, NULL, end, ent->s.number, MASK_SHOT ); + if( tr.surfaceFlags & SURF_NOIMPACT ) return; @@ -308,8 +320,9 @@ SnapVector( tent->s.origin2 ); tent->s.eventParm = rand() & 255; // seed for spread pattern tent->s.otherEntityNum = ent->s.number; - + G_UnlaggedOn(); ShotgunPattern( tent->s.pos.trBase, tent->s.origin2, tent->s.eventParm, ent ); + G_UnlaggedOff(); } /* @@ -329,7 +342,10 @@ VectorMA( muzzle, 8192 * 16, forward, end ); + G_UnlaggedOn( ); trap_Trace( &tr, muzzle, NULL, NULL, end, ent->s.number, MASK_SHOT ); + G_UnlaggedOff( ); + if( tr.surfaceFlags & SURF_NOIMPACT ) return; @@ -482,7 +498,10 @@ VectorMA( muzzle, 8192 * 16, forward, end ); + G_UnlaggedOn( ); trap_Trace( &tr, muzzle, NULL, NULL, end, ent->s.number, MASK_SHOT ); + G_UnlaggedOff( ); + if( tr.surfaceFlags & SURF_NOIMPACT ) return; @@ -534,7 +553,10 @@ VectorMA( muzzle, PAINSAW_RANGE, forward, end ); + G_UnlaggedOn( ); trap_Trace( &tr, muzzle, NULL, NULL, end, ent->s.number, MASK_SHOT ); + G_UnlaggedOff( ); + if( tr.surfaceFlags & SURF_NOIMPACT ) return; @@ -805,7 +827,9 @@ VectorMA( muzzle, LEVEL0_BITE_RANGE, forward, end ); + G_UnlaggedOn( ); trap_Trace( &tr, muzzle, mins, maxs, end, ent->s.number, MASK_SHOT ); + G_UnlaggedOff( ); if( tr.surfaceFlags & SURF_NOIMPACT ) return qfalse; @@ -939,6 +963,7 @@ VectorAdd( ent->client->ps.origin, range, maxs ); VectorSubtract( ent->client->ps.origin, range, mins ); + G_UnlaggedOn( ); num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES ); for( i = 0; i < num; i++ ) { @@ -967,6 +992,7 @@ } } } + G_UnlaggedOff( ); } @@ -1000,6 +1026,7 @@ VectorAdd( ent->s.origin, range, maxs ); VectorSubtract( ent->s.origin, range, mins ); + G_UnlaggedOn( ); num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES ); for( i = 0; i < num; i++ ) @@ -1038,11 +1065,11 @@ // enemy is already targetted if( foundOldTarget ) continue; - + G_UnlaggedOff( ); return enemy; } } - + G_UnlaggedOff( ); return NULL; } @@ -1231,7 +1258,9 @@ VectorMA( muzzle, LEVEL2_AREAZAP_RANGE, forward, end ); + G_UnlaggedOn( ); trap_Trace( &tr, muzzle, mins, maxs, end, ent->s.number, MASK_SHOT ); + G_UnlaggedOff( ); if( tr.surfaceFlags & SURF_NOIMPACT ) return; @@ -1291,7 +1320,9 @@ VectorMA( muzzle, LEVEL3_POUNCE_RANGE, forward, end ); + G_UnlaggedOn( ); trap_Trace( &tr, ent->s.origin, mins, maxs, end, ent->s.number, MASK_SHOT ); + G_UnlaggedOff( ); //miss if( tr.fraction >= 1.0 ) Index: src/game/g_misc.c =================================================================== --- src/game/g_misc.c (revision 848) +++ src/game/g_misc.c (working copy) @@ -86,6 +86,7 @@ // toggle the teleport bit so the client knows to not lerp player->client->ps.eFlags ^= EF_TELEPORT_BIT; + G_UnlaggedClear( player ); // set angles SetClientViewAngle( player, angles ); Index: src/game/g_client.c =================================================================== --- src/game/g_client.c (revision 848) +++ src/game/g_client.c (working copy) @@ -1412,6 +1412,7 @@ // toggle the teleport bit so the client knows to not lerp flags = ent->client->ps.eFlags & ( EF_TELEPORT_BIT | EF_VOTED | EF_TEAMVOTED ); flags ^= EF_TELEPORT_BIT; + G_UnlaggedClear( ent ); // clear everything but the persistant data