aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/client/CL_pred.java
blob: cc8061fd356b3f30f45168a81beaebbd4884e3ff (plain)
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
/*
 * CL_pred.java
 * Copyright (C) 2004
 * 
 * $Id: CL_pred.java,v 1.1 2004-07-07 19:58:39 hzi Exp $
 */
/*
Copyright (C) 1997-2001 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.

*/
package jake2.client;

import jake2.game.*;
import jake2.qcommon.*;

/**
 * CL_pred
 */
public class CL_pred extends CL_parse
{

	/*
	===================
	CL_CheckPredictionError
	===================
	*/
	static void CheckPredictionError()
	{
		int frame;
		int[] delta = new int[3];
		int i;
		int len;

		if (cl_predict.value == 0.0f
			|| (cl.frame.playerstate.pmove.pm_flags & PMF_NO_PREDICTION) != 0)
			return;

		// calculate the last usercmd_t we sent that the server has processed
		frame = cls.netchan.incoming_acknowledged;
		frame &= (CMD_BACKUP - 1);

		// compare what the server returned with what we had predicted it to be
		VectorSubtract(
			cl.frame.playerstate.pmove.origin,
			cl.predicted_origins[frame],
			delta);

		// save the prediction error for interpolation
		len = Math.abs(delta[0]) + Math.abs(delta[1]) + Math.abs(delta[2]);
		if (len > 640) // 80 world units
		{ // a teleport or something
			VectorClear(cl.prediction_error);
		}
		else
		{
			if (cl_showmiss.value != 0.0f
				&& (delta[0] != 0 || delta[1] != 0 || delta[2] != 0))
				Com.Printf(
					"prediction miss on "
						+ cl.frame.serverframe
						+ ": "
						+ (delta[0] + delta[1] + delta[2])
						+ "\n");

			VectorCopy(
				cl.frame.playerstate.pmove.origin,
				cl.predicted_origins[frame]);

			// save for error itnerpolation
			for (i = 0; i < 3; i++)
				cl.prediction_error[i] = delta[i] * 0.125f;
		}
	}

	/*
	====================
	CL_ClipMoveToEntities
	
	====================
	*/
	static void ClipMoveToEntities(
		float[] start,
		float[] mins,
		float[] maxs,
		float[] end,
		trace_t tr)
	{
		int i, x, zd, zu;
		trace_t trace;
		int headnode;
		float[] angles;
		entity_state_t ent;
		int num;
		cmodel_t cmodel;
		float[] bmins = new float[3];
		float[] bmaxs = new float[3];

		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 == 0)
				continue;

			if (ent.number == cl.playernum + 1)
				continue;

			if (ent.solid == 31)
			{ // special value for bmodel
				cmodel = cl.model_clip[ent.modelindex];
				if (cmodel == null)
					continue;
				headnode = cmodel.headnode;
				angles = ent.angles;
			}
			else
			{ // encoded bbox
				x = 8 * (ent.solid & 31);
				zd = 8 * ((ent.solid >>> 5) & 31);
				zu = 8 * ((ent.solid >>> 10) & 63) - 32;

				bmins[0] = bmins[1] = -x;
				bmaxs[0] = bmaxs[1] = x;
				bmins[2] = -zd;
				bmaxs[2] = zu;

				headnode = CM.HeadnodeForBox(bmins, bmaxs);
				angles = vec3_origin; // boxes don't rotate
			}

			if (tr.allsolid)
				return;

			trace =
				CM.TransformedBoxTrace(
					start,
					end,
					mins,
					maxs,
					headnode,
					MASK_PLAYERSOLID,
					ent.origin,
					angles);

			if (trace.allsolid
				|| trace.startsolid
				|| trace.fraction < tr.fraction)
			{
				// TODO bugfix cwei
				//if (trace.ent == null) trace.ent = new edict_t(0);
				trace.ent = ent.surrounding_ent;
				if (tr.startsolid)
				{
					tr = trace;
					tr.startsolid = true;
				}
				else
					tr = trace;
			}
			else if (trace.startsolid)
				tr.startsolid = true;
		}
	}

	/*
	================
	CL_PMTrace
	================
	*/
	
	static edict_t DUMMY_ENT = new edict_t(-1);
	
	static trace_t PMTrace(float[] start, float[] mins, float[] maxs, float[] end) {
		trace_t t;

		// check against world
		t = CM.BoxTrace(start, end, mins, maxs, 0, MASK_PLAYERSOLID);

		if (t.fraction < 1.0f) {
			t.ent = DUMMY_ENT;
		}

		// check all other solid models
		CL.ClipMoveToEntities(start, mins, maxs, end, t);

		return t;
	}

	static int PMpointcontents(float[] point)
	{
		int i;
		entity_state_t ent;
		int num;
		cmodel_t cmodel;
		int contents;

		contents = CM.PointContents(point, 0);

		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 != 31) // special value for bmodel
				continue;

			cmodel = cl.model_clip[ent.modelindex];
			if (cmodel == null)
				continue;

			contents
				|= CM.TransformedPointContents(
					point,
					cmodel.headnode,
					ent.origin,
					ent.angles);
		}

		return contents;
	}

	/*
	=================
	CL_PredictMovement
	
	Sets cl.predicted_origin and cl.predicted_angles
	=================
	*/
	static void PredictMovement()
	{
		int ack, current;
		int frame;
		int oldframe;
		usercmd_t cmd;
		pmove_t pm;
		int i;
		int step;
		int oldz;

		if (cls.state != ca_active)
			return;

		if (cl_paused.value != 0.0f)
			return;

		if (cl_predict.value == 0.0f
			|| (cl.frame.playerstate.pmove.pm_flags & PMF_NO_PREDICTION) != 0)
		{ // just set angles
			for (i = 0; i < 3; i++)
			{
				cl.predicted_angles[i] =
					cl.viewangles[i]
						+ SHORT2ANGLE(cl.frame.playerstate.pmove.delta_angles[i]);
			}
			return;
		}

		ack = cls.netchan.incoming_acknowledged;
		current = cls.netchan.outgoing_sequence;

		// if we are too far out of date, just freeze
		if (current - ack >= CMD_BACKUP)
		{
			if (cl_showmiss.value != 0.0f)
				Com.Printf("exceeded CMD_BACKUP\n");
			return;
		}

		// copy current state to pmove
		//memset (pm, 0, sizeof(pm));
		pm = new pmove_t();

		pm.trace = new pmove_t.TraceAdapter()
		{
			public trace_t trace(
				float[] start,
				float[] mins,
				float[] maxs,
				float[] end)
			{
				return CL.PMTrace(start, mins, maxs, end);
			}
		};
		pm.pointcontents = new pmove_t.PointContentsAdapter()
		{
			public int pointcontents(float[] point)
			{
				return CL.PMpointcontents(point);
			}
		};

		PMove.pm_airaccelerate = atof(cl.configstrings[CS_AIRACCEL]);

		// bugfix (rst) yeah !!!!!!!!  found the B E W E G U N G S P R O B L E M.  
		pm.s.set(cl.frame.playerstate.pmove);

		// SCR_DebugGraph (current - ack - 1, 0);
		frame = 0;

		// run frames
		while (++ack < current)
		{
			frame = ack & (CMD_BACKUP - 1);
			cmd = cl.cmds[frame];
			
			pm.cmd.set(cmd);
			
			PMove.Pmove(pm);

			// save for debug checking
			VectorCopy(pm.s.origin, cl.predicted_origins[frame]);
		}

		oldframe = (ack - 2) & (CMD_BACKUP - 1);
		oldz = cl.predicted_origins[oldframe][2];
		step = pm.s.origin[2] - oldz;
		if (step > 63 && step < 160 && (pm.s.pm_flags & PMF_ON_GROUND) != 0)
		{
			cl.predicted_step = step * 0.125f;
			cl.predicted_step_time = (int) (cls.realtime - cls.frametime * 500);
		}

		// copy results out for rendering
		cl.predicted_origin[0] = pm.s.origin[0] * 0.125f;
		cl.predicted_origin[1] = pm.s.origin[1] * 0.125f;
		cl.predicted_origin[2] = pm.s.origin[2] * 0.125f;

		VectorCopy(pm.viewangles, cl.predicted_angles);
	}

}