forked from QW-Group/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_pred.c
425 lines (351 loc) · 10.8 KB
/
cl_pred.c
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "quakedef.h"
#include "pmove.h"
cvar_t cl_nopred = {"cl_nopred", "0"};
cvar_t cl_pushlatency = {"pushlatency", "-999"};
extern cvar_t cl_independentPhysics;
#ifdef JSS_CAM
cvar_t cam_thirdperson = {"cam_thirdperson", "0"};
cvar_t cam_dist = {"cam_dist", "100"};
cvar_t cam_lockdir = {"cam_lockdir", "0"};
cvar_t cam_lockpos = {"cam_lockpos", "0"};
static vec3_t saved_angles;
qbool clpred_newpos = false;
#endif
void CL_PredictUsercmd (player_state_t *from, player_state_t *to, usercmd_t *u) {
// split up very long moves
if (u->msec > 50) {
player_state_t temp;
usercmd_t split;
split = *u;
split.msec /= 2;
CL_PredictUsercmd (from, &temp, &split);
CL_PredictUsercmd (&temp, to, &split);
return;
}
VectorCopy (from->origin, pmove.origin);
VectorCopy (u->angles, pmove.angles);
VectorCopy (from->velocity, pmove.velocity);
pmove.jump_msec = (cl.z_ext & Z_EXT_PM_TYPE) ? 0 : from->jump_msec;
pmove.jump_held = from->jump_held;
pmove.waterjumptime = from->waterjumptime;
pmove.pm_type = from->pm_type;
pmove.onground = from->onground;
pmove.cmd = *u;
#ifdef JSS_CAM
if (cam_lockdir.value) {
VectorCopy (saved_angles, pmove.cmd.angles);
VectorCopy (saved_angles, pmove.angles);
}
else
VectorCopy (pmove.cmd.angles, saved_angles);
#endif
movevars.entgravity = cl.entgravity;
movevars.maxspeed = cl.maxspeed;
movevars.bunnyspeedcap = cl.bunnyspeedcap;
PM_PlayerMove();
to->waterjumptime = pmove.waterjumptime;
to->pm_type = pmove.pm_type;
to->jump_held = pmove.jump_held;
to->jump_msec = pmove.jump_msec;
pmove.jump_msec = 0;
VectorCopy (pmove.origin, to->origin);
VectorCopy (pmove.angles, to->viewangles);
VectorCopy (pmove.velocity, to->velocity);
to->onground = pmove.onground;
to->weaponframe = from->weaponframe;
}
//Used when cl_nopred is 1 to determine whether we are on ground, otherwise stepup smoothing code produces ugly jump physics
void CL_CategorizePosition (void) {
if (cl.spectator && cl.playernum == cl.viewplayernum) {
cl.onground = false; // in air
return;
}
VectorClear (pmove.velocity);
VectorCopy (cl.simorg, pmove.origin);
pmove.numtouch = 0;
PM_CategorizePosition ();
cl.onground = pmove.onground;
}
//Smooth out stair step ups.
//Called before CL_EmitEntities so that the player's lightning model origin is updated properly
void CL_CalcCrouch (void) {
qbool teleported;
static vec3_t oldorigin = {0, 0, 0};
static float oldz = 0, extracrouch = 0, crouchspeed = 100;
teleported = !VectorL2Compare(cl.simorg, oldorigin, 48);
VectorCopy (cl.simorg, oldorigin);
if (teleported) {
// possibly teleported or respawned
oldz = cl.simorg[2];
extracrouch = 0;
crouchspeed = 100;
cl.crouch = 0;
VectorCopy (cl.simorg, oldorigin);
return;
}
if (cl.onground && cl.simorg[2] - oldz > 0) {
if (cl.simorg[2] - oldz > 20) {
// if on steep stairs, increase speed
if (crouchspeed < 160) {
extracrouch = cl.simorg[2] - oldz - cls.frametime * 200 - 15;
extracrouch = min(extracrouch, 5);
}
crouchspeed = 160;
}
oldz += cls.frametime * crouchspeed;
if (oldz > cl.simorg[2])
oldz = cl.simorg[2];
if (cl.simorg[2] - oldz > 15 + extracrouch)
oldz = cl.simorg[2] - 15 - extracrouch;
extracrouch -= cls.frametime * 200;
extracrouch = max(extracrouch, 0);
cl.crouch = oldz - cl.simorg[2];
} else {
// in air or moving down
oldz = cl.simorg[2];
cl.crouch += cls.frametime * 150;
if (cl.crouch > 0)
cl.crouch = 0;
crouchspeed = 100;
extracrouch = 0;
}
}
static void CL_LerpMove (qbool angles_lerp)
{
static int lastsequence = 0;
static vec3_t lerp_angles[3];
static vec3_t lerp_origin[3];
static double lerp_times[3];
static qbool nolerp[2];
static double demo_latency = 0.01;
float frac;
double simtime;
int i;
int from, to;
extern cvar_t cl_nolerp;
extern int cmdtime_msec;
extern double physframetime;
extern qbool cl_nolerp_on_entity_flag;
double current_time = cls.demoplayback ? cls.demotime : cls.realtime;
double current_lerp_time = cls.demoplayback ? cls.demopackettime : (cmdtime_msec * 0.001);
qbool physframe = cls.netchan.outgoing_sequence != lastsequence;
if ((cl_nolerp.value || cl_nolerp_on_entity_flag))
{
lastsequence = ((unsigned)-1) >> 1; //reset
return;
}
if (cls.netchan.outgoing_sequence < lastsequence)
{
// reset
lastsequence = -1;
lerp_times[0] = -1;
demo_latency = 0.01;
}
// Independent physics.
if (physframe)
{
lastsequence = cls.netchan.outgoing_sequence;
// move along
lerp_times[2] = lerp_times[1];
lerp_times[1] = lerp_times[0];
lerp_times[0] = current_lerp_time;
VectorCopy (lerp_origin[1], lerp_origin[2]);
VectorCopy (lerp_origin[0], lerp_origin[1]);
VectorCopy (cl.simorg, lerp_origin[0]);
VectorCopy (lerp_angles[1], lerp_angles[2]);
VectorCopy (lerp_angles[0], lerp_angles[1]);
VectorCopy (cl.simangles, lerp_angles[0]);
nolerp[1] = nolerp[0];
nolerp[0] = false;
for (i = 0; i < 3; i++)
{
if (fabs(lerp_origin[0][i] - lerp_origin[1][i]) > 100)
{
break;
}
}
if (i < 3)
{
// a teleport or something
nolerp[0] = true;
}
}
simtime = current_time - demo_latency;
// Adjust latency
if (simtime > lerp_times[0])
{
// High clamp
demo_latency = current_time - lerp_times[0];
}
else if (simtime < lerp_times[2])
{
// Low clamp
demo_latency = current_time - lerp_times[2];
}
else
{
// slowly drift down till corrected
if (physframe)
demo_latency -= min(physframetime * 0.005, 0.001);
}
// decide where to lerp from
if (simtime > lerp_times[1])
{
from = 1;
to = 0;
}
else
{
from = 2;
to = 1;
}
if (nolerp[to])
{
return;
}
frac = (simtime - lerp_times[from]) / (lerp_times[to] - lerp_times[from]);
frac = bound (0, frac, 1);
if ((cl.spectator && cl.viewplayernum != cl.playernum) || angles_lerp)
{
// we track someone, so lerp angles
AngleInterpolate(lerp_angles[from], frac, lerp_angles[to], cl.simangles);
}
for (i = 0; i < 3; i++)
{
cl.simorg[i] = lerp_origin[from][i] + (lerp_origin[to][i] - lerp_origin[from][i]) * frac;
}
}
qbool cl_nolerp_on_entity_flag = false;
// function check_standing_on_entity(void)
// raises flag cl_nolerp_on_entity_flag if standing on entity
// and cl_nolerp_on_entity.value is 1
static void check_standing_on_entity(void)
{
extern cvar_t cl_nolerp;
extern cvar_t cl_nolerp_on_entity;
extern cvar_t cl_independentPhysics;
cl_nolerp_on_entity_flag =
(pmove.onground && pmove.groundent > 0 &&
cl_nolerp_on_entity.value &&
cl_independentPhysics.value);
}
void CL_PredictMove (qbool physframe) {
int i, oldphysent;
frame_t *from = NULL, *to;
qbool angles_lerp = false;
if (cl.paused && !CL_MultiviewEnabled())
return;
if (cl.intermission) {
cl.crouch = 0;
return;
}
if (cls.nqdemoplayback)
return;
if (!cl.validsequence)
return;
if (cls.netchan.outgoing_sequence - cl.validsequence >= UPDATE_BACKUP - 1)
return;
VectorCopy (cl.viewangles, cl.simangles);
// this is the last valid frame received from the server
to = &cl.frames[cl.validsequence & UPDATE_MASK];
#ifdef JSS_CAM
if (clpred_newpos && cls.demoplayback && cl.spectator)
VectorCopy (cl.simorg, to->playerstate[cl.playernum].origin);
clpred_newpos = false;
#endif
// FIXME...
if (cls.demoplayback && cl.spectator && cl.viewplayernum != cl.playernum) {
VectorCopy (to->playerstate[cl.viewplayernum].velocity, cl.simvel);
#ifdef JSS_CAM
if (!(cam_thirdperson.value && cam_lockpos.value) && !clpred_newpos)
VectorCopy (to->playerstate[cl.viewplayernum].origin, cl.simorg);
if (!cam_thirdperson.value)
VectorCopy (to->playerstate[cl.viewplayernum].viewangles, cl.simangles);
#endif
CL_CategorizePosition ();
}
else if (to->playerstate[cl.playernum].pm_type == PM_LOCK)
{
angles_lerp = true;
VectorCopy (to->playerstate[cl.playernum].velocity, cl.simvel);
VectorCopy (to->playerstate[cl.playernum].origin, cl.simorg);
VectorCopy (to->playerstate[cl.playernum].command.angles, cl.simangles);
cl.onground = false;
}
else if ((cl_nopred.value && !cls.mvdplayback) || cl.validsequence + 1 >= cls.netchan.outgoing_sequence) {
VectorCopy (to->playerstate[cl.playernum].velocity, cl.simvel);
VectorCopy (to->playerstate[cl.playernum].origin, cl.simorg);
CL_CategorizePosition ();
}
else if (physframe || !cl_independentPhysics.value)
{
oldphysent = pmove.numphysent;
CL_SetSolidPlayers (cl.playernum);
// run frames
for (i = 1; i < UPDATE_BACKUP - 1 && cl.validsequence + i < cls.netchan.outgoing_sequence; i++) {
from = to;
to = &cl.frames[(cl.validsequence + i) & UPDATE_MASK];
CL_PredictUsercmd (&from->playerstate[cl.playernum], &to->playerstate[cl.playernum], &to->cmd);
}
pmove.numphysent = oldphysent;
// save results
VectorCopy (to->playerstate[cl.playernum].velocity, cl.simvel);
VectorCopy (to->playerstate[cl.playernum].origin, cl.simorg);
cl.onground = pmove.onground;
cl.waterlevel = pmove.waterlevel;
check_standing_on_entity();
}
if (!cls.mvdplayback && cl_independentPhysics.value != 0) {
CL_LerpMove (angles_lerp || cls.demoplayback);
}
CL_CalcCrouch ();
#ifdef JSS_CAM
if (cam_thirdperson.value && cam_lockpos.value && cl.viewplayernum != cl.playernum) {
vec3_t v;
player_state_t *pstate;
int i;
i = cl.viewplayernum;
pstate = &cl.frames[cl.parsecount & UPDATE_MASK].playerstate[i];
VectorSubtract (pstate->origin, cl.simorg, v);
vectoangles (v, cl.simangles);
cl.simangles[PITCH] = -cl.simangles[PITCH];
}
else if (cam_thirdperson.value && cl.viewplayernum != cl.playernum) {
int i;
player_state_t *pstate;
vec3_t fw, rt, up;
i = cl.viewplayernum;
pstate = &cl.frames[cl.parsecount & UPDATE_MASK].playerstate[i];
AngleVectors (cl.simangles, fw, rt, up);
VectorMA (pstate->origin, -cam_dist.value, fw, cl.simorg);
}
#endif // JSS_CAM
}
void CL_InitPrediction (void) {
Cvar_SetCurrentGroup(CVAR_GROUP_NETWORK);
Cvar_Register(&cl_nopred);
Cvar_Register(&cl_pushlatency);
Cvar_ResetCurrentGroup();
#ifdef JSS_CAM
Cvar_SetCurrentGroup(CVAR_GROUP_SPECTATOR);
Cvar_Register (&cam_thirdperson);
Cvar_Register (&cam_dist);
Cvar_Register (&cam_lockdir);
Cvar_Register (&cam_lockpos);
Cvar_ResetCurrentGroup();
#endif
}