File: g_antilag.c

package info (click to toggle)
iortcw 1.51.c%2Bdfsg1-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 25,304 kB
  • sloc: ansic: 457,326; cpp: 6,507; makefile: 4,737; sh: 1,292; asm: 1,176; xml: 31
file content (285 lines) | stat: -rw-r--r-- 9,735 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
===========================================================================

Return to Castle Wolfenstein multiplayer GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. 

This file is part of the Return to Castle Wolfenstein multiplayer GPL Source Code (“RTCW MP Source Code”).  

RTCW MP Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RTCW MP Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RTCW MP Source Code.  If not, see <http://www.gnu.org/licenses/>.

In addition, the RTCW MP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW MP Source Code.  If not, please request a copy in writing from id Software at the address below.

If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.

===========================================================================
*/

#include "g_local.h"

#define IS_ACTIVE( x ) ( \
		x->r.linked == qtrue &&	\
		x->client->ps.stats[STAT_HEALTH] > 0 &&	\
		x->client->sess.sessionTeam != TEAM_SPECTATOR && \
		( x->client->ps.pm_flags & PMF_LIMBO ) == 0	\
		)

// OSP -
//	Aside from the inline edits, I also changed the client loops to poll only
//  the active client slots on the server, rather than looping through every
//  potential (and usually unused) slot.
//

void G_StoreClientPosition( gentity_t* ent ) {
	int top, currentTime;

	if ( !IS_ACTIVE( ent ) ) {
		return;
	}

	top = ent->client->topMarker;

	// new frame, mark the old marker's time as the end of the last frame
	if ( ent->client->clientMarkers[top].time < level.time ) {
		ent->client->clientMarkers[top].time = level.previousTime;
		top = ent->client->topMarker = ent->client->topMarker == MAX_CLIENT_MARKERS - 1 ? 0 : ent->client->topMarker + 1;
	}

	currentTime = level.previousTime + trap_Milliseconds() - level.frameTime;

	if ( currentTime > level.time ) {
		// owwie, we just went into the next frame... let's push them back
		currentTime = level.time;
	}

	VectorCopy( ent->r.mins,                        ent->client->clientMarkers[top].mins );
	VectorCopy( ent->r.maxs,                        ent->client->clientMarkers[top].maxs );
	VectorCopy( ent->r.currentOrigin,               ent->client->clientMarkers[top].origin );

	// OSP - these timers appear to be questionable
	ent->client->clientMarkers[top].servertime =    level.time;
	ent->client->clientMarkers[top].time =          currentTime;
}

static void G_AdjustSingleClientPosition( gentity_t* ent, int time ) {
	int i, j;

	if ( time > level.time ) {
		time = level.time;
	} // no lerping forward....

	i = j = ent->client->topMarker;
	do {
		if ( ent->client->clientMarkers[i].time <= time ) {
			break;
		}

		j = i;
		i = ( ( i > 0 ) ? ( i ) : ( MAX_CLIENT_MARKERS ) ) - 1;
	} while ( i != ent->client->topMarker );

	if ( i == j ) { // oops, no valid stored markers
		return;
	}

	// OSP - I don't trust this caching, as the "warped" player's position updates potentially
	//       wont be counted after his think until the next server frame, which will result
	//       in a bad backupMarker
//	if( ent->client->backupMarker.time != level.time ) {
//		ent->client->backupMarker.time = level.time;
	VectorCopy( ent->r.currentOrigin,    ent->client->backupMarker.origin );
	VectorCopy( ent->r.mins,             ent->client->backupMarker.mins );
	VectorCopy( ent->r.maxs,             ent->client->backupMarker.maxs );
//	}

	if ( i != ent->client->topMarker ) {
		float frac = ( (float)( ent->client->clientMarkers[j].time - time ) ) / ( ent->client->clientMarkers[j].time - ent->client->clientMarkers[i].time );

		LerpPosition( ent->client->clientMarkers[j].origin,      ent->client->clientMarkers[i].origin,   frac,   ent->r.currentOrigin );
		LerpPosition( ent->client->clientMarkers[j].mins,        ent->client->clientMarkers[i].mins,     frac,   ent->r.mins );
		LerpPosition( ent->client->clientMarkers[j].maxs,        ent->client->clientMarkers[i].maxs,     frac,   ent->r.maxs );
	} else {
		VectorCopy( ent->client->clientMarkers[j].origin,       ent->r.currentOrigin );
		VectorCopy( ent->client->clientMarkers[j].mins,         ent->r.mins );
		VectorCopy( ent->client->clientMarkers[j].maxs,         ent->r.maxs );
	}

	trap_LinkEntity( ent );
}

static void G_ReAdjustSingleClientPosition( gentity_t* ent ) {

	// OSP - I don't trust this caching, as the "warped" player's position updates potentially
	//       wont be counted after his think until the next server frame
//	if( ent->client->backupMarker.time == level.time) {
	VectorCopy( ent->client->backupMarker.origin,       ent->r.currentOrigin );
	VectorCopy( ent->client->backupMarker.mins,         ent->r.mins );
	VectorCopy( ent->client->backupMarker.maxs,         ent->r.maxs );
	ent->client->backupMarker.servertime =  0;

	trap_LinkEntity( ent );
//	}
}

void G_AdjustClientPositions( gentity_t* ent, int time, qboolean forward ) {
	int i;
	gentity_t   *list;

	for ( i = 0; i < level.numConnectedClients; i++ ) {
		list = g_entities + level.sortedClients[i];
		if ( list != ent && IS_ACTIVE( list ) ) {
			if ( forward ) {
				G_AdjustSingleClientPosition( list, time );
			} else { G_ReAdjustSingleClientPosition( list );}
		}
	}
}

/*
void G_ResetMarkers( gentity_t* ent ) {
	int i, time;
	char buffer[256];
	float period;

	trap_Cvar_VariableStringBuffer( "sv_fps", buffer, sizeof( buffer ) - 1 );

	period = atoi( buffer );
	period = ( period == 0 ) ? 50.0f : 1000.f / period;

	ent->client->topMarker = MAX_CLIENT_MARKERS - 1;
	for ( i = MAX_CLIENT_MARKERS, time = level.time; i >= 0; i--, time -= period ) {
		ent->client->clientMarkers[i].servertime =  time;
		ent->client->clientMarkers[i].time =        time;

		VectorCopy( ent->r.mins,            ent->client->clientMarkers[i].mins );
		VectorCopy( ent->r.maxs,            ent->client->clientMarkers[i].maxs );
		VectorCopy( ent->r.currentOrigin,   ent->client->clientMarkers[i].origin );
	}
}
*/

void G_ResetMarkers(gentity_t *ent)
{
	int   i, time;
	char  buffer[MAX_CVAR_VALUE_STRING];
	float period;

	trap_Cvar_VariableStringBuffer("sv_fps", buffer, sizeof(buffer) - 1);

	period = atoi(buffer);
	if (!period)
	{
		period = 50;
	}
	else
	{
		period = 1000.f / period;
	}

	ent->client->topMarker = MAX_CLIENT_MARKERS - 1;
	for (i = MAX_CLIENT_MARKERS - 1, time = level.time; i >= 0; i--, time -= period)
	{
		VectorCopy(ent->r.mins, ent->client->clientMarkers[i].mins);
		VectorCopy(ent->r.maxs, ent->client->clientMarkers[i].maxs);
		VectorCopy(ent->r.currentOrigin, ent->client->clientMarkers[i].origin);
		ent->client->clientMarkers[i].time = time;
	}
}

void G_AttachBodyParts( gentity_t* ent ) {
	int i;
	gentity_t   *list;

	for ( i = 0; i < level.numConnectedClients; i++ ) {
		list = g_entities + level.sortedClients[i];
		list->client->tempHead = ( list != ent && IS_ACTIVE( list ) ) ? G_BuildHead( list ) : NULL;
	}
}

void G_DettachBodyParts( void ) {
	int i;
	gentity_t   *list;

	for ( i = 0; i < level.numConnectedClients; i++ ) {
		list = g_entities + level.sortedClients[i];
		if ( list->client->tempHead != NULL ) {
			G_FreeEntity( list->client->tempHead );
		}
	}
}

int G_SwitchBodyPartEntity( gentity_t* ent ) {
	if ( ent->s.eType == ET_TEMPHEAD ) {
		return ent->parent - g_entities;
	}
	return ent - g_entities;
}

#define POSITION_READJUST											\
	if ( res != results->entityNum ) {							   \
		VectorSubtract( end, start, dir );						  \
		VectorNormalizeFast( dir );								  \
																	\
		VectorMA( results->endpos, -1, dir, results->endpos );	  \
		results->entityNum = res;								\
	}

// Run a trace with players in historical positions.
void G_HistoricalTrace( gentity_t* ent, trace_t *results, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int passEntityNum, int contentmask ) {
	trace_t tr;
	gentity_t *other;
	int res;
	vec3_t dir;

	if ( !g_antilag.integer || !ent->client ) {
		G_AttachBodyParts( ent ) ;

		trap_Trace( results, start, mins, maxs, end, passEntityNum, contentmask );

		res = G_SwitchBodyPartEntity( &g_entities[results->entityNum] );
		POSITION_READJUST

		G_DettachBodyParts();
		return;
	}

	G_AdjustClientPositions( ent, ent->client->pers.cmd.serverTime, qtrue );

	G_AttachBodyParts( ent ) ;

	trap_Trace( results, start, mins, maxs, end, passEntityNum, contentmask );

	res = G_SwitchBodyPartEntity( &g_entities[results->entityNum] );
	POSITION_READJUST

	G_DettachBodyParts();

	G_AdjustClientPositions( ent, 0, qfalse );

	if ( results->entityNum >= 0 && results->entityNum < MAX_CLIENTS && ( other = &g_entities[results->entityNum] )->inuse ) {
		G_AttachBodyParts( ent ) ;

		trap_Trace( &tr, start, mins, maxs, other->client->ps.origin, passEntityNum, contentmask );
		res = G_SwitchBodyPartEntity( &g_entities[results->entityNum] );
		POSITION_READJUST

		if ( tr.entityNum != results->entityNum ) {
			trap_Trace( results, start, mins, maxs, end, passEntityNum, contentmask );
			res = G_SwitchBodyPartEntity( &g_entities[results->entityNum] );
			POSITION_READJUST
		}

		G_DettachBodyParts();
	}
}