summaryrefslogtreecommitdiffstats
path: root/src/jake2/server/SV_WORLD.java
blob: cb74e9528ca829b906ce1ef2ee5549a4d7765c06 (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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
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.

*/

// Created on 07.01.2000 by RST.
// $Id: SV_WORLD.java,v 1.5 2004-08-20 21:29:57 salomo Exp $

package jake2.server;

import jake2.game.edict_t;
import jake2.game.trace_t;

import jake2.*;
import jake2.client.*;
import jake2.game.*;
import jake2.qcommon.*;
import jake2.render.*;
import jake2.util.Vargs;

public class SV_WORLD extends SV_CCMDS
{

	// world.c -- world query functions

	//
	//
	//===============================================================================
	//
	//ENTITY AREA CHECKING
	//
	//FIXME: this use of "area" is different from the bsp file use
	//===============================================================================
	//

	public static void initNodes()
	{
		for (int n= 0; n < AREA_NODES; n++)
			sv_areanodes[n]= new areanode_t();
	}

	public static areanode_t sv_areanodes[]= new areanode_t[AREA_NODES];

	static {
		initNodes();
	}
	public static int sv_numareanodes;

	public static float area_mins[], area_maxs[];
	public static edict_t area_list[];
	public static int area_count, area_maxcount;
	public static int area_type;

	// ClearLink is used for new headnodes
	public static void ClearLink(link_t l)
	{
		l.prev= l.next= l;
	}

	public static void RemoveLink(link_t l)
	{
		l.next.prev= l.prev;
		l.prev.next= l.next;
	}

	public static void InsertLinkBefore(link_t l, link_t before)
	{
		l.next= before;
		l.prev= before.prev;
		l.prev.next= l;
		l.next.prev= l;
	}

	/*
	===============
	SV_CreateAreaNode
	
	Builds a uniformly subdivided tree for the given world size
	===============
	*/
	public static areanode_t SV_CreateAreaNode(int depth, float[] mins, float[] maxs)
	{
		areanode_t anode;
		float[] size= { 0, 0, 0 };
		float[] mins1= { 0, 0, 0 }, maxs1= { 0, 0, 0 }, mins2= { 0, 0, 0 }, maxs2= { 0, 0, 0 };

		anode= sv_areanodes[sv_numareanodes];

		// just for debugging (rst)
		VectorCopy(mins, anode.mins_rst);
		VectorCopy(maxs, anode.maxs_rst);

		sv_numareanodes++;

		ClearLink(anode.trigger_edicts);
		ClearLink(anode.solid_edicts);

		if (depth == AREA_DEPTH)
		{
			anode.axis= -1;
			anode.children[0]= anode.children[1]= null;
			return anode;
		}

		VectorSubtract(maxs, mins, size);
		if (size[0] > size[1])
			anode.axis= 0;
		else
			anode.axis= 1;

		anode.dist= 0.5f * (maxs[anode.axis] + mins[anode.axis]);
		VectorCopy(mins, mins1);
		VectorCopy(mins, mins2);
		VectorCopy(maxs, maxs1);
		VectorCopy(maxs, maxs2);

		maxs1[anode.axis]= mins2[anode.axis]= anode.dist;

		anode.children[0]= SV_CreateAreaNode(depth + 1, mins2, maxs2);
		anode.children[1]= SV_CreateAreaNode(depth + 1, mins1, maxs1);

		return anode;
	}

	/*
	===============
	SV_ClearWorld
	
	===============
	*/
	public static void SV_ClearWorld()
	{
		initNodes();
		sv_numareanodes= 0;
		SV_CreateAreaNode(0, sv.models[1].mins, sv.models[1].maxs);

		/*
		Com.p("areanodes:" + sv_numareanodes + " (sollten 32 sein).");
		for (int n = 0; n < sv_numareanodes; n++) {
			Com.Printf(
				"|%3i|%2i|%8.2f  |%8.2f|%8.2f|%8.2f|  %8.2f|%8.2f|%8.2f|\n",
				new Vargs()
					.add(n)
					.add(sv_areanodes[n].axis)
					.add(sv_areanodes[n].dist)
					.add(sv_areanodes[n].mins_rst[0])
					.add(sv_areanodes[n].mins_rst[1])
					.add(sv_areanodes[n].mins_rst[2])
					.add(sv_areanodes[n].maxs_rst[0])
					.add(sv_areanodes[n].maxs_rst[1])
					.add(sv_areanodes[n].maxs_rst[2]));
		}
		*/
	}
	/*
	===============
	SV_UnlinkEdict
	===============
	*/
	public static void SV_UnlinkEdict(edict_t ent)
	{
		if (null == ent.area.prev)
			return; // not linked in anywhere
			
		RemoveLink(ent.area);
		ent.area.prev= ent.area.next= null;
	}

	/*
	===============
	SV_LinkEdict
	===============
	*/
	public static final int MAX_TOTAL_ENT_LEAFS= 128;
	private static int leafs[]= new int[MAX_TOTAL_ENT_LEAFS];
	private static int clusters[]= new int[MAX_TOTAL_ENT_LEAFS];
		
	public static void SV_LinkEdict(edict_t ent)
	{
		areanode_t node;

		int num_leafs;
		int j, k;
		int area;
		int topnode= 0;

		if (ent.area.prev != null)
			SV_UnlinkEdict(ent); // unlink from old position

		if (ent == ge.edicts[0])
			return; // don't add the world

		if (!ent.inuse)
			return;

		// set the size
		VectorSubtract(ent.maxs, ent.mins, ent.size);

		// encode the size into the entity_state for client prediction
		if (ent.solid == SOLID_BBOX && 0 == (ent.svflags & SVF_DEADMONSTER))
		{ 
			// assume that x/y are equal and symetric
			int i= (int) (ent.maxs[0] / 8);
			if (i < 1)
				i= 1;
			if (i > 31)
				i= 31;

			// z is not symetric
			j= (int) ((-ent.mins[2]) / 8);
			if (j < 1)
				j= 1;
			if (j > 31)
				j= 31;

			// and z maxs can be negative...
			k= (int) ((ent.maxs[2] + 32) / 8);
			if (k < 1)
				k= 1;
			if (k > 63)
				k= 63;

			ent.s.solid= (k << 10) | (j << 5) | i;
		}
		else if (ent.solid == SOLID_BSP)
		{
			ent.s.solid= 31; // a solid_bbox will never create this value
		}
		else
			ent.s.solid= 0;

		// set the abs box
		if (ent.solid == SOLID_BSP && (ent.s.angles[0] != 0 || ent.s.angles[1] != 0 || ent.s.angles[2] != 0))
		{ 
			// expand for rotation
			float max, v;

			max= 0;
			for (int i= 0; i < 3; i++)
			{
				v= Math.abs(ent.mins[i]);
				if (v > max)
					max= v;
				v= Math.abs(ent.maxs[i]);
				if (v > max)
					max= v;
			}
			for (int i= 0; i < 3; i++)
			{
				ent.absmin[i]= ent.s.origin[i] - max;
				ent.absmax[i]= ent.s.origin[i] + max;
			}
		}
		else
		{ 
			// normal
			VectorAdd(ent.s.origin, ent.mins, ent.absmin);
			VectorAdd(ent.s.origin, ent.maxs, ent.absmax);
		}

		// because movement is clipped an epsilon away from an actual edge,
		// we must fully check even when bounding boxes don't quite touch

		ent.absmin[0]--;
		ent.absmin[1]--;
		ent.absmin[2]--;
		ent.absmax[0]++;
		ent.absmax[1]++;
		ent.absmax[2]++;

		// link to PVS leafs
		ent.num_clusters= 0;
		ent.areanum= 0;
		ent.areanum2= 0;

		// get all leafs, including solids

		int iw[] = {topnode};

		num_leafs= CM.CM_BoxLeafnums(ent.absmin, ent.absmax, leafs, MAX_TOTAL_ENT_LEAFS, iw);

		topnode= iw[0];

		// set areas
		for (int i= 0; i < num_leafs; i++)
		{
			clusters[i]= CM.CM_LeafCluster(leafs[i]);
			area= CM.CM_LeafArea(leafs[i]);
			if (area != 0)
			{ 
				// doors may legally straggle two areas,
				// but nothing should evern need more than that
				if (ent.areanum != 0 && ent.areanum != area)
				{
					if (ent.areanum2 != 0 && ent.areanum2 != area && sv.state == ss_loading)
						Com.DPrintf("Object touching 3 areas at " + ent.absmin[0] + " " + ent.absmin[1] + " " + ent.absmin[2] + "\n");
					ent.areanum2= area;
				}
				else
					ent.areanum= area;
			}
		}

		if (num_leafs >= MAX_TOTAL_ENT_LEAFS)
		{ 
			// assume we missed some leafs, and mark by headnode
			ent.num_clusters= -1;
			ent.headnode= topnode;
		}
		else
		{
			ent.num_clusters= 0;
			for (int i= 0; i < num_leafs; i++)
			{
				if (clusters[i] == -1)
					continue; // not a visible leaf
				for (j= 0; j < i; j++)
					if (clusters[j] == clusters[i])
						break;
				if (j == i)
				{
					if (ent.num_clusters == MAX_ENT_CLUSTERS)
					{ 
						// assume we missed some leafs, and mark by headnode
						ent.num_clusters= -1;
						ent.headnode= topnode;
						break;
					}

					ent.clusternums[ent.num_clusters++]= clusters[i];
				}
			}
		}

		// if first time, make sure old_origin is valid
		if (0 == ent.linkcount)
		{
			VectorCopy(ent.s.origin, ent.s.old_origin);
		}
		ent.linkcount++;

		if (ent.solid == SOLID_NOT)
			return;

		// find the first node that the ent's box crosses
		node= sv_areanodes[0];

		while (true)
		{
			if (node.axis == -1)
				break;
			if (ent.absmin[node.axis] > node.dist)
				node= node.children[0];
			else if (ent.absmax[node.axis] < node.dist)
				node= node.children[1];
			else
				break; // crosses the node
		}

		// link it in	
		if (ent.solid == SOLID_TRIGGER)
			InsertLinkBefore(ent.area, node.trigger_edicts);
		else
			InsertLinkBefore(ent.area, node.solid_edicts);

	}

	/*
	====================
	SV_AreaEdicts_r
	
	====================
	*/
	public static void SV_AreaEdicts_r(areanode_t node)
	{
		link_t l, next, start;
		edict_t check;
		int count;

		count= 0;

		// touch linked edicts
		if (area_type == AREA_SOLID)
			start= node.solid_edicts;
		else
			start= node.trigger_edicts;

		for (l= start.next; l != start; l= next)
		{
			next= l.next;
			check= (edict_t) l.o;

			if (check.solid == SOLID_NOT)
				continue; // deactivated
			if (check.absmin[0] > area_maxs[0]
				|| check.absmin[1] > area_maxs[1]
				|| check.absmin[2] > area_maxs[2]
				|| check.absmax[0] < area_mins[0]
				|| check.absmax[1] < area_mins[1]
				|| check.absmax[2] < area_mins[2])
				continue; // not touching

			if (area_count == area_maxcount)
			{
				Com.Printf("SV_AreaEdicts: MAXCOUNT\n");
				return;
			}

			area_list[area_count]= check;
			area_count++;
		}

		if (node.axis == -1)
			return; // terminal node

		// recurse down both sides
		if (area_maxs[node.axis] > node.dist)
			SV_AreaEdicts_r(node.children[0]);

		if (area_mins[node.axis] < node.dist)
			SV_AreaEdicts_r(node.children[1]);
	}

	/*
	================
	SV_AreaEdicts
	================
	*/
	public static int SV_AreaEdicts(float[] mins, float[] maxs, edict_t list[], int maxcount, int areatype)
	{
		area_mins= mins;
		area_maxs= maxs;
		area_list= list;
		area_count= 0;
		area_maxcount= maxcount;
		area_type= areatype;

		SV_AreaEdicts_r(sv_areanodes[0]);

		return area_count;
	}

	//===========================================================================

	private static edict_t touch[]= new edict_t[MAX_EDICTS];
	/*
	=============
	SV_PointContents
	=============
	*/

	public static int SV_PointContents(float[] p)
	{
		edict_t hit;

		int i, num;
		int contents, c2;
		int headnode;
		float angles[];

		// get base contents from world
		contents= CM.PointContents(p, sv.models[1].headnode);

		// or in contents from all the other entities
		num= SV_AreaEdicts(p, p, touch, MAX_EDICTS, AREA_SOLID);

		for (i= 0; i < num; i++)
		{
			hit= touch[i];

			// might intersect, so do an exact clip
			headnode= SV_HullForEntity(hit);
			angles= hit.s.angles;
			if (hit.solid != SOLID_BSP)
				angles= vec3_origin; // boxes don't rotate

			c2= CM.TransformedPointContents(p, headnode, hit.s.origin, hit.s.angles);

			contents |= c2;
		}

		return contents;
	}

	/*
	================
	SV_HullForEntity
	
	Returns a headnode that can be used for testing or clipping an
	object of mins/maxs size.
	Offset is filled in to contain the adjustment that must be added to the
	testing object's origin to get a point to use with the returned hull.
	================
	*/
	public static int SV_HullForEntity(edict_t ent)
	{

		cmodel_t model;

		// decide which clipping hull to use, based on the size
		if (ent.solid == SOLID_BSP)
		{ // explicit hulls in the BSP model
			model= sv.models[ent.s.modelindex];

			if (null == model)
				Com.Error(ERR_FATAL, "MOVETYPE_PUSH with a non bsp model");

			return model.headnode;
		}

		// create a temp hull from bounding box sizes

		return CM.HeadnodeForBox(ent.mins, ent.maxs);
	}

	//===========================================================================

	/*
	====================
	SV_ClipMoveToEntities
	
	====================
	*/
	private static edict_t touchlist[]= new edict_t[MAX_EDICTS];
	public static void SV_ClipMoveToEntities(moveclip_t clip)
	{
		int i, num;
		
		edict_t touch;
		trace_t trace;
		int headnode;
		float angles[];

		num= SV_AreaEdicts(clip.boxmins, clip.boxmaxs, touchlist, MAX_EDICTS, AREA_SOLID);

		// be careful, it is possible to have an entity in this
		// list removed before we get to it (killtriggered)
		for (i= 0; i < num; i++)
		{
			touch= touchlist[i];
			if (touch.solid == SOLID_NOT)
				continue;
			if (touch == clip.passedict)
				continue;
			if (clip.trace.allsolid)
				return;
			if (clip.passedict != null)
			{
				if (touch.owner == clip.passedict)
					continue; // don't clip against own missiles
				if (clip.passedict.owner == touch)
					continue; // don't clip against owner
			}

			if (0 == (clip.contentmask & CONTENTS_DEADMONSTER) && 0 != (touch.svflags & SVF_DEADMONSTER))
				continue;

			// might intersect, so do an exact clip
			headnode= SV_HullForEntity(touch);
			angles= touch.s.angles;
			if (touch.solid != SOLID_BSP)
				angles= vec3_origin; // boxes don't rotate

			if ((touch.svflags & SVF_MONSTER) != 0)
				trace=
					CM.TransformedBoxTrace(
						clip.start,
						clip.end,
						clip.mins2,
						clip.maxs2,
						headnode,
						clip.contentmask,
						touch.s.origin,
						angles);
			else
				trace=
					CM.TransformedBoxTrace(
						clip.start,
						clip.end,
						clip.mins,
						clip.maxs,
						headnode,
						clip.contentmask,
						touch.s.origin,
						angles);

			if (trace.allsolid || trace.startsolid || trace.fraction < clip.trace.fraction)
			{
				trace.ent= touch;
				if (clip.trace.startsolid)
				{
					clip.trace= trace;
					clip.trace.startsolid= true;
				}
				else
					clip.trace.set(trace);
			}
			else if (trace.startsolid)
				clip.trace.startsolid= true;
		}
	}

	/*
	==================
	SV_TraceBounds
	==================
	*/
	public static void SV_TraceBounds(float[] start, float[] mins, float[] maxs, float[] end, float[] boxmins, float[] boxmaxs)
	{
		int i;

		for (i= 0; i < 3; i++)
		{
			if (end[i] > start[i])
			{
				boxmins[i]= start[i] + mins[i] - 1;
				boxmaxs[i]= end[i] + maxs[i] + 1;
			}
			else
			{
				boxmins[i]= end[i] + mins[i] - 1;
				boxmaxs[i]= start[i] + maxs[i] + 1;
			}
		}

	}

	/*
	==================
	SV_Trace
	
	Moves the given mins/maxs volume through the world from start to end.
	
	Passedict and edicts owned by passedict are explicitly not checked.
	
	==================
	*/
	public static trace_t SV_Trace(float[] start, float[] mins, float[] maxs, float[] end, edict_t passedict, int contentmask)
	{
		moveclip_t clip= new moveclip_t();

		if (mins == null)
			mins= vec3_origin;
		if (maxs == null)
			maxs= vec3_origin;

		//memset ( clip, 0, sizeof ( moveclip_t ) );

		// clip to world
		clip.trace= CM.BoxTrace(start, end, mins, maxs, 0, contentmask);
		clip.trace.ent= ge.edicts[0];
		if (clip.trace.fraction == 0)
			return clip.trace; // blocked by the world

		clip.contentmask= contentmask;
		clip.start= start;
		clip.end= end;
		clip.mins= mins;
		clip.maxs= maxs;
		clip.passedict= passedict;

		VectorCopy(mins, clip.mins2);
		VectorCopy(maxs, clip.maxs2);

		// create the bounding box of the entire move
		SV_TraceBounds(start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs);

		// clip to other solid entities
		SV_ClipMoveToEntities(clip);

		return clip.trace;
	}
}