1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 // world.c -- world query functions
21 
22 #include "quakedef.h"
23 
24 /*
25 
26 entities never clip against themselves, or their owner
27 
28 line of sight checks trace->crosscontent, but bullets don't
29 
30 */
31 
32 
33 typedef struct
34 {
35 	vec3_t		boxmins, boxmaxs;// enclose the test object along entire move
36 	float		*mins, *maxs;	// size of the moving object
37 	vec3_t		mins2, maxs2;	// size when clipping against mosnters
38 	float		*start, *end;
39 	trace_t		trace;
40 	int			type;
41 	edict_t		*passedict;
42 } moveclip_t;
43 
44 
45 int SV_HullPointContents (hull_t *hull, int num, vec3_t p);
46 
47 /*
48 ===============================================================================
49 
50 HULL BOXES
51 
52 ===============================================================================
53 */
54 
55 
56 static	hull_t		box_hull;
57 static	dclipnode_t	box_clipnodes[6];
58 static	mplane_t	box_planes[6];
59 
60 /*
61 ===================
62 SV_InitBoxHull
63 
64 Set up the planes and clipnodes so that the six floats of a bounding box
65 can just be stored out and get a proper hull_t structure.
66 ===================
67 */
SV_InitBoxHull(void)68 void SV_InitBoxHull (void)
69 {
70 	int		i;
71 	int		side;
72 
73 	box_hull.clipnodes = box_clipnodes;
74 	box_hull.planes = box_planes;
75 	box_hull.firstclipnode = 0;
76 	box_hull.lastclipnode = 5;
77 
78 	for (i=0 ; i<6 ; i++)
79 	{
80 		box_clipnodes[i].planenum = i;
81 
82 		side = i&1;
83 
84 		box_clipnodes[i].children[side] = CONTENTS_EMPTY;
85 		if (i != 5)
86 			box_clipnodes[i].children[side^1] = i + 1;
87 		else
88 			box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
89 
90 		box_planes[i].type = i>>1;
91 		box_planes[i].normal[i>>1] = 1;
92 	}
93 
94 }
95 
96 
97 /*
98 ===================
99 SV_HullForBox
100 
101 To keep everything totally uniform, bounding boxes are turned into small
102 BSP trees instead of being compared directly.
103 ===================
104 */
SV_HullForBox(vec3_t mins,vec3_t maxs)105 hull_t	*SV_HullForBox (vec3_t mins, vec3_t maxs)
106 {
107 	box_planes[0].dist = maxs[0];
108 	box_planes[1].dist = mins[0];
109 	box_planes[2].dist = maxs[1];
110 	box_planes[3].dist = mins[1];
111 	box_planes[4].dist = maxs[2];
112 	box_planes[5].dist = mins[2];
113 
114 	return &box_hull;
115 }
116 
117 
118 
119 /*
120 ================
121 SV_HullForEntity
122 
123 Returns a hull that can be used for testing or clipping an object of mins/maxs
124 size.
125 Offset is filled in to contain the adjustment that must be added to the
126 testing object's origin to get a point to use with the returned hull.
127 ================
128 */
SV_HullForEntity(edict_t * ent,vec3_t mins,vec3_t maxs,vec3_t offset)129 hull_t *SV_HullForEntity (edict_t *ent, vec3_t mins, vec3_t maxs, vec3_t offset)
130 {
131 	model_t		*model;
132 	vec3_t		size;
133 	vec3_t		hullmins, hullmaxs;
134 	hull_t		*hull;
135 
136 // decide which clipping hull to use, based on the size
137 	if (ent->v.solid == SOLID_BSP)
138 	{	// explicit hulls in the BSP model
139 		if (ent->v.movetype != MOVETYPE_PUSH)
140 		{
141 			ED_Print (ent);
142 			Sys_Error ("SOLID_BSP without MOVETYPE_PUSH (%s)", ED_DbgEdict (&ent->v));
143 		}
144 
145 		model = sv.models[ (int)ent->v.modelindex ];
146 
147 		if (!model || model->type != mod_brush)
148 		{
149 			ED_Print (ent);
150 
151 			if (!model)
152 				Sys_Error ("SOLID_BSP with a missing model (%s)", ED_DbgEdict (&ent->v));
153 			else
154 				Sys_Error ("SOLID_BSP with a non bsp (%d) model (%s)", model->type, ED_DbgEdict (&ent->v));
155 		}
156 
157 		VectorSubtract (maxs, mins, size);
158 		if (size[0] < 3)
159 			hull = &model->hulls[0];
160 		else if (size[0] <= 32)
161 			hull = &model->hulls[1];
162 		else
163 			hull = &model->hulls[2];
164 
165 // calculate an offset value to center the origin
166 		VectorSubtract (hull->clip_mins, mins, offset);
167 		VectorAdd (offset, ent->v.origin, offset);
168 	}
169 	else
170 	{	// create a temp hull from bounding box sizes
171 
172 		VectorSubtract (ent->v.mins, maxs, hullmins);
173 		VectorSubtract (ent->v.maxs, mins, hullmaxs);
174 		hull = SV_HullForBox (hullmins, hullmaxs);
175 
176 		VectorCopy (ent->v.origin, offset);
177 	}
178 
179 
180 	return hull;
181 }
182 
183 /*
184 ===============================================================================
185 
186 ENTITY AREA CHECKING
187 
188 ===============================================================================
189 */
190 
191 typedef struct areanode_s
192 {
193 	int		axis;		// -1 = leaf node
194 	float	dist;
195 	struct areanode_s	*children[2];
196 	link_t	trigger_edicts;
197 	link_t	solid_edicts;
198 } areanode_t;
199 
200 #define	AREA_DEPTH	4
201 #define	AREA_NODES	32
202 
203 static	areanode_t	sv_areanodes[AREA_NODES];
204 static	int			sv_numareanodes;
205 
206 /*
207 ===============
208 SV_CreateAreaNode
209 
210 ===============
211 */
SV_CreateAreaNode(int depth,vec3_t mins,vec3_t maxs)212 areanode_t *SV_CreateAreaNode (int depth, vec3_t mins, vec3_t maxs)
213 {
214 	areanode_t	*anode;
215 	vec3_t		size;
216 	vec3_t		mins1, maxs1, mins2, maxs2;
217 
218 	anode = &sv_areanodes[sv_numareanodes];
219 	sv_numareanodes++;
220 
221 	ClearLink (&anode->trigger_edicts);
222 	ClearLink (&anode->solid_edicts);
223 
224 	if (depth == AREA_DEPTH)
225 	{
226 		anode->axis = -1;
227 		anode->children[0] = anode->children[1] = NULL;
228 		return anode;
229 	}
230 
231 	VectorSubtract (maxs, mins, size);
232 	if (size[0] > size[1])
233 		anode->axis = 0;
234 	else
235 		anode->axis = 1;
236 
237 	anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
238 	VectorCopy (mins, mins1);
239 	VectorCopy (mins, mins2);
240 	VectorCopy (maxs, maxs1);
241 	VectorCopy (maxs, maxs2);
242 
243 	maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
244 
245 	anode->children[0] = SV_CreateAreaNode (depth+1, mins2, maxs2);
246 	anode->children[1] = SV_CreateAreaNode (depth+1, mins1, maxs1);
247 
248 	return anode;
249 }
250 
251 /*
252 ===============
253 SV_ClearWorld
254 
255 ===============
256 */
SV_ClearWorld(void)257 void SV_ClearWorld (void)
258 {
259 	SV_InitBoxHull ();
260 
261 	memset (sv_areanodes, 0, sizeof(sv_areanodes));
262 	sv_numareanodes = 0;
263 	SV_CreateAreaNode (0, sv.worldmodel->mins, sv.worldmodel->maxs);
264 }
265 
266 
267 /*
268 ===============
269 SV_UnlinkEdict
270 
271 ===============
272 */
SV_UnlinkEdict(edict_t * ent)273 void SV_UnlinkEdict (edict_t *ent)
274 {
275 	if (!ent->area.prev)
276 		return;		// not linked in anywhere
277 	RemoveLink (&ent->area);
278 	ent->area.prev = ent->area.next = NULL;
279 }
280 
281 /*
282 ====================
283 SV_TouchLinks
284 ====================
285 */
SV_TouchLinks(edict_t * ent,areanode_t * node)286 void SV_TouchLinks ( edict_t *ent, areanode_t *node )
287 {
288 	link_t	*l, *next;
289 	edict_t	*touch;
290 	int	       old_self, old_other, touched = 0, i;
291 	static edict_t *list[MAX_EDICTS]; // Static due to recursive function
292 
293 // Build a list of touched edicts since linked list may change during touch
294 	for (l = node->trigger_edicts.next; l != &node->trigger_edicts; l = l->next)
295 	{
296 		touch = EDICT_FROM_AREA(l);
297 		if (touch == ent)
298 			continue;
299 		if (!touch->v.touch || touch->v.solid != SOLID_TRIGGER)
300 			continue;
301 		if (ent->v.absmin[0] > touch->v.absmax[0]
302 		|| ent->v.absmin[1] > touch->v.absmax[1]
303 		|| ent->v.absmin[2] > touch->v.absmax[2]
304 		|| ent->v.absmax[0] < touch->v.absmin[0]
305 		|| ent->v.absmax[1] < touch->v.absmin[1]
306 		|| ent->v.absmax[2] < touch->v.absmin[2] )
307 			continue;
308 
309 		list[touched++] = touch;
310 
311 		if (touched == MAX_EDICTS)
312 		{
313 			Con_DPrintf ("\x02SV_TouchLinks: ");
314 			Con_DPrintf ("too many touched trigger_edicts (max = %d)\n", MAX_EDICTS);
315 			break;
316 		}
317 	}
318 
319 // touch linked edicts
320 	for (i = 0; i < touched; ++i)
321 	{
322 		touch = list[i];
323 
324 		if (touch->free)
325 		{
326 			Con_DPrintf ("\x02SV_TouchLinks: ");
327 			Con_DPrintf ("free touched trigger_edict (%s)\n", ED_DbgEdict (&touch->v));
328 			continue;
329 		}
330 
331 		old_self = pr_global_struct->self;
332 		old_other = pr_global_struct->other;
333 
334 		pr_global_struct->self = EDICT_TO_PROG(touch);
335 		pr_global_struct->other = EDICT_TO_PROG(ent);
336 		pr_global_struct->time = sv.time;
337 		PR_ExecuteProgram (touch->v.touch);
338 
339 		pr_global_struct->self = old_self;
340 		pr_global_struct->other = old_other;
341 	}
342 
343 // recurse down both sides
344 	if (node->axis == -1)
345 		return;
346 
347 	if ( ent->v.absmax[node->axis] > node->dist )
348 		SV_TouchLinks ( ent, node->children[0] );
349 	if ( ent->v.absmin[node->axis] < node->dist )
350 		SV_TouchLinks ( ent, node->children[1] );
351 }
352 
353 
354 /*
355 ===============
356 SV_FindTouchedLeafs
357 
358 ===============
359 */
SV_FindTouchedLeafs(edict_t * ent,mnode_t * node)360 void SV_FindTouchedLeafs (edict_t *ent, mnode_t *node)
361 {
362 	mplane_t	*splitplane;
363 	mleaf_t		*leaf;
364 	int			sides;
365 	int			leafnum;
366 
367 	if (node->contents == CONTENTS_SOLID)
368 		return;
369 
370 // add an efrag if the node is a leaf
371 
372 	if ( node->contents < 0)
373 	{
374 		if (ent->num_leafs == MAX_ENT_LEAFS)
375 			return;
376 
377 		leaf = (mleaf_t *)node;
378 		leafnum = leaf - sv.worldmodel->leafs - 1;
379 
380 		ent->leafnums[ent->num_leafs] = leafnum;
381 		ent->num_leafs++;
382 		return;
383 	}
384 
385 // NODE_MIXED
386 
387 	splitplane = node->plane;
388 	sides = BOX_ON_PLANE_SIDE(ent->v.absmin, ent->v.absmax, splitplane);
389 
390 // recurse down the contacted sides
391 	if (sides & 1)
392 		SV_FindTouchedLeafs (ent, node->children[0]);
393 
394 	if (sides & 2)
395 		SV_FindTouchedLeafs (ent, node->children[1]);
396 }
397 
398 /*
399 ===============
400 SV_LinkEdict
401 
402 ===============
403 */
SV_LinkEdict(edict_t * ent,qboolean touch_triggers)404 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
405 {
406 	areanode_t	*node;
407 
408 	if (ent->area.prev)
409 		SV_UnlinkEdict (ent);	// unlink from old position
410 
411 	if (ent == sv.edicts)
412 		return;		// don't add the world
413 
414 	if (ent->free)
415 		return;
416 
417 // set the abs box
418 
419 #ifdef QUAKE2
420 	if (ent->v.solid == SOLID_BSP &&
421 	(ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
422 	{	// expand for rotation
423 		float		max, v;
424 		int			i;
425 
426 		max = 0;
427 		for (i=0 ; i<3 ; i++)
428 		{
429 			v =fabs( ent->v.mins[i]);
430 			if (v > max)
431 				max = v;
432 			v =fabs( ent->v.maxs[i]);
433 			if (v > max)
434 				max = v;
435 		}
436 		for (i=0 ; i<3 ; i++)
437 		{
438 			ent->v.absmin[i] = ent->v.origin[i] - max;
439 			ent->v.absmax[i] = ent->v.origin[i] + max;
440 		}
441 	}
442 	else
443 #endif
444 	{
445 		VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
446 		VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
447 	}
448 
449 //
450 // to make items easier to pick up and allow them to be grabbed off
451 // of shelves, the abs sizes are expanded
452 //
453 	if ((int)ent->v.flags & FL_ITEM)
454 	{
455 		ent->v.absmin[0] -= 15;
456 		ent->v.absmin[1] -= 15;
457 		ent->v.absmax[0] += 15;
458 		ent->v.absmax[1] += 15;
459 	}
460 	else
461 	{	// because movement is clipped an epsilon away from an actual edge,
462 		// we must fully check even when bounding boxes don't quite touch
463 		ent->v.absmin[0] -= 1;
464 		ent->v.absmin[1] -= 1;
465 		ent->v.absmin[2] -= 1;
466 		ent->v.absmax[0] += 1;
467 		ent->v.absmax[1] += 1;
468 		ent->v.absmax[2] += 1;
469 	}
470 
471 // link to PVS leafs
472 	ent->num_leafs = 0;
473 	if (ent->v.modelindex)
474 		SV_FindTouchedLeafs (ent, sv.worldmodel->nodes);
475 
476 	if (ent->v.solid == SOLID_NOT)
477 		return;
478 
479 // find the first node that the ent's box crosses
480 	node = sv_areanodes;
481 	while (1)
482 	{
483 		if (node->axis == -1)
484 			break;
485 		if (ent->v.absmin[node->axis] > node->dist)
486 			node = node->children[0];
487 		else if (ent->v.absmax[node->axis] < node->dist)
488 			node = node->children[1];
489 		else
490 			break;		// crosses the node
491 	}
492 
493 // link it in
494 
495 	if (ent->v.solid == SOLID_TRIGGER)
496 		InsertLinkBefore (&ent->area, &node->trigger_edicts);
497 	else
498 		InsertLinkBefore (&ent->area, &node->solid_edicts);
499 
500 // if touch_triggers, touch all entities at this node and decend for more
501 	if (touch_triggers)
502 		SV_TouchLinks ( ent, sv_areanodes );
503 }
504 
505 
506 
507 /*
508 ===============================================================================
509 
510 POINT TESTING IN HULLS
511 
512 ===============================================================================
513 */
514 
515 #if	!id386
516 
517 /*
518 ==================
519 SV_HullPointContents
520 
521 ==================
522 */
SV_HullPointContents(hull_t * hull,int num,vec3_t p)523 int SV_HullPointContents (hull_t *hull, int num, vec3_t p)
524 {
525 	float		d;
526 	dclipnode_t	*node;
527 	mplane_t	*plane;
528 
529 	while (num >= 0)
530 	{
531 		if (num < hull->firstclipnode || num > hull->lastclipnode)
532 			Sys_Error ("SV_HullPointContents: bad node number");
533 
534 		node = hull->clipnodes + num;
535 		plane = hull->planes + node->planenum;
536 
537 		if (plane->type < 3)
538 			d = p[plane->type] - plane->dist;
539 		else
540 			d = DotProduct (plane->normal, p) - plane->dist;
541 		if (d < 0)
542 			num = node->children[1];
543 		else
544 			num = node->children[0];
545 	}
546 
547 	return num;
548 }
549 
550 #endif	// !id386
551 
552 
553 /*
554 ==================
555 SV_PointContents
556 
557 ==================
558 int SV_PointContents (vec3_t p)
559 {
560 	int		cont;
561 
562 	cont = SV_HullPointContents (&sv.worldmodel->hulls[0], 0, p);
563 	if (cont <= CONTENTS_CURRENT_0 && cont >= CONTENTS_CURRENT_DOWN)
564 		cont = CONTENTS_WATER;
565 	return cont;
566 }
567 */ // Nehahra - Lordhavoc
568 
SV_TruePointContents(vec3_t p)569 int SV_TruePointContents (vec3_t p)
570 {
571 	return SV_HullPointContents (&sv.worldmodel->hulls[0], 0, p);
572 }
573 
574 //===========================================================================
575 
576 /*
577 ============
578 SV_TestEntityPosition
579 
580 This could be a lot more efficient...
581 ============
582 */
SV_TestEntityPosition(edict_t * ent)583 edict_t	*SV_TestEntityPosition (edict_t *ent)
584 {
585 	trace_t	trace;
586 
587 	trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, 0, ent);
588 
589 	if (trace.startsolid)
590 		return sv.edicts;
591 
592 	return NULL;
593 }
594 
595 
596 /*
597 ===============================================================================
598 
599 LINE TESTING IN HULLS
600 
601 ===============================================================================
602 */
603 
604 // 1/32 epsilon to keep floating point happy
605 #define	DIST_EPSILON	(0.03125)
606 
607 /*
608 ==================
609 SV_RecursiveHullCheck
610 
611 ==================
612 */
SV_RecursiveHullCheck(hull_t * hull,int num,float p1f,float p2f,vec3_t p1,vec3_t p2,trace_t * trace)613 qboolean SV_RecursiveHullCheck (hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, trace_t *trace)
614 {
615 	dclipnode_t	*node;
616 	mplane_t	*plane;
617 	float		t1, t2;
618 	float		frac;
619 	int		i;
620 	vec3_t		mid;
621 	int		side;
622 	float		midf;
623 	static float	lastmsg = 0;
624 
625 // check for empty
626 	if (num < 0)
627 	{
628 		if (num != CONTENTS_SOLID)
629 		{
630 			trace->allsolid = false;
631 			if (num == CONTENTS_EMPTY)
632 				trace->inopen = true;
633 			else
634 				trace->inwater = true;
635 		}
636 		else
637 			trace->startsolid = true;
638 		return true;		// empty
639 	}
640 
641 	if (num < hull->firstclipnode || num > hull->lastclipnode)
642 		Sys_Error ("SV_RecursiveHullCheck: node number %d outside %d - %d", num, hull->firstclipnode, hull->lastclipnode);
643 
644 //
645 // find the point distances
646 //
647 	node = hull->clipnodes + num;
648 	plane = hull->planes + node->planenum;
649 
650 	if (plane->type < 3)
651 	{
652 		t1 = p1[plane->type] - plane->dist;
653 		t2 = p2[plane->type] - plane->dist;
654 	}
655 	else
656 	{
657 		t1 = DotProduct (plane->normal, p1) - plane->dist;
658 		t2 = DotProduct (plane->normal, p2) - plane->dist;
659 	}
660 
661 #if 1
662 	if (t1 >= 0 && t2 >= 0)
663 		return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
664 	if (t1 < 0 && t2 < 0)
665 		return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
666 #else
667 	if ( (t1 >= DIST_EPSILON && t2 >= DIST_EPSILON) || (t2 > t1 && t1 >= 0) )
668 		return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
669 	if ( (t1 <= -DIST_EPSILON && t2 <= -DIST_EPSILON) || (t2 < t1 && t1 <= 0) )
670 		return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
671 #endif
672 
673 // put the crosspoint DIST_EPSILON pixels on the near side
674 	if (t1 < 0)
675 		frac = (t1 + DIST_EPSILON)/(t1-t2);
676 	else
677 		frac = (t1 - DIST_EPSILON)/(t1-t2);
678 	if (frac < 0)
679 		frac = 0;
680 	if (frac > 1)
681 		frac = 1;
682 
683 	midf = p1f + (p2f - p1f)*frac;
684 	for (i=0 ; i<3 ; i++)
685 		mid[i] = p1[i] + frac*(p2[i] - p1[i]);
686 
687 	side = (t1 < 0);
688 
689 // move up to the node
690 	if (!SV_RecursiveHullCheck (hull, node->children[side], p1f, midf, p1, mid, trace) )
691 		return false;
692 
693 #ifdef PARANOID
694 	if (SV_HullPointContents (sv_hullmodel, node->children[side], mid)
695 	== CONTENTS_SOLID)
696 	{
697 		Con_Printf ("mid PointInHullSolid\n");
698 		return false;
699 	}
700 #endif
701 
702 	if (SV_HullPointContents (hull, node->children[side^1], mid)
703 	!= CONTENTS_SOLID)
704 // go past the node
705 		return SV_RecursiveHullCheck (hull, node->children[side^1], midf, p2f, mid, p2, trace);
706 
707 	if (trace->allsolid)
708 		return false;		// never got out of the solid area
709 
710 //==================
711 // the other side of the node is solid, this is the impact point
712 //==================
713 	if (!side)
714 	{
715 		VectorCopy (plane->normal, trace->plane.normal);
716 		trace->plane.dist = plane->dist;
717 	}
718 	else
719 	{
720 		VectorSubtract (vec3_origin, plane->normal, trace->plane.normal);
721 		trace->plane.dist = -plane->dist;
722 	}
723 
724 	while (SV_HullPointContents (hull, hull->firstclipnode, mid)
725 	== CONTENTS_SOLID)
726 	{ // shouldn't really happen, but does occasionally
727 		frac -= 0.1;
728 		if (frac < 0)
729 		{
730 			trace->fraction = midf;
731 			VectorCopy (mid, trace->endpos);
732 
733 			if (IsTimeout (&lastmsg, 2))
734 				Con_DPrintf ("backup past 0\n");
735 
736 			return false;
737 		}
738 		midf = p1f + (p2f - p1f)*frac;
739 		for (i=0 ; i<3 ; i++)
740 			mid[i] = p1[i] + frac*(p2[i] - p1[i]);
741 	}
742 
743 	trace->fraction = midf;
744 	VectorCopy (mid, trace->endpos);
745 
746 	return false;
747 }
748 
749 
750 /*
751 ==================
752 SV_ClipMoveToEntity
753 
754 Handles selection or creation of a clipping hull, and offseting (and
755 eventually rotation) of the end points
756 ==================
757 */
SV_ClipMoveToEntity(edict_t * ent,vec3_t start,vec3_t mins,vec3_t maxs,vec3_t end)758 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
759 {
760 	trace_t		trace;
761 	vec3_t		offset;
762 	vec3_t		start_l, end_l;
763 	hull_t		*hull;
764 
765 // fill in a default trace
766 	memset (&trace, 0, sizeof(trace_t));
767 	trace.fraction = 1;
768 	trace.allsolid = true;
769 	VectorCopy (end, trace.endpos);
770 
771 // get the clipping hull
772 	hull = SV_HullForEntity (ent, mins, maxs, offset);
773 
774 	VectorSubtract (start, offset, start_l);
775 	VectorSubtract (end, offset, end_l);
776 
777 #ifdef QUAKE2
778 	// rotate start and end into the models frame of reference
779 	if (ent->v.solid == SOLID_BSP &&
780 	(ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
781 	{
782 		vec3_t	a;
783 		vec3_t	forward, right, up;
784 		vec3_t	temp;
785 
786 		AngleVectors (ent->v.angles, forward, right, up);
787 
788 		VectorCopy (start_l, temp);
789 		start_l[0] = DotProduct (temp, forward);
790 		start_l[1] = -DotProduct (temp, right);
791 		start_l[2] = DotProduct (temp, up);
792 
793 		VectorCopy (end_l, temp);
794 		end_l[0] = DotProduct (temp, forward);
795 		end_l[1] = -DotProduct (temp, right);
796 		end_l[2] = DotProduct (temp, up);
797 	}
798 #endif
799 
800 // trace a line through the apropriate clipping hull
801 	SV_RecursiveHullCheck (hull, hull->firstclipnode, 0, 1, start_l, end_l, &trace);
802 
803 #ifdef QUAKE2
804 	// rotate endpos back to world frame of reference
805 	if (ent->v.solid == SOLID_BSP &&
806 	(ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
807 	{
808 		vec3_t	a;
809 		vec3_t	forward, right, up;
810 		vec3_t	temp;
811 
812 		if (trace.fraction != 1)
813 		{
814 			VectorSubtract (vec3_origin, ent->v.angles, a);
815 			AngleVectors (a, forward, right, up);
816 
817 			VectorCopy (trace.endpos, temp);
818 			trace.endpos[0] = DotProduct (temp, forward);
819 			trace.endpos[1] = -DotProduct (temp, right);
820 			trace.endpos[2] = DotProduct (temp, up);
821 
822 			VectorCopy (trace.plane.normal, temp);
823 			trace.plane.normal[0] = DotProduct (temp, forward);
824 			trace.plane.normal[1] = -DotProduct (temp, right);
825 			trace.plane.normal[2] = DotProduct (temp, up);
826 		}
827 	}
828 #endif
829 
830 // fix trace up by the offset
831 	if (trace.fraction != 1)
832 		VectorAdd (trace.endpos, offset, trace.endpos);
833 
834 // did we clip the move?
835 	if (trace.fraction < 1 || trace.startsolid  )
836 		trace.ent = ent;
837 
838 	return trace;
839 }
840 
841 //===========================================================================
842 
843 /*
844 ====================
845 SV_ClipToLinks
846 
847 Mins and maxs enclose the entire area swept by the move
848 ====================
849 */
SV_ClipToLinks(areanode_t * node,moveclip_t * clip)850 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
851 {
852 	link_t	     *l, *next;
853 	edict_t	     *touch;
854 	trace_t	     trace;
855 	static float lastmsg = 0;
856 
857 // touch linked edicts
858 	for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
859 	{
860 		next = l->next;
861 		touch = EDICT_FROM_AREA(l);
862 		if (touch->v.solid == SOLID_NOT)
863 			continue;
864 
865 		if (touch != clip->passedict && touch->v.solid == SOLID_TRIGGER)
866 		{
867 			if (IsTimeout (&lastmsg, 2))
868 			{
869 				// Still valid warning?
870 				Con_DPrintf ("\x02SV_ClipToLinks: ");
871 				Con_DPrintf ("trigger (%s) in clipping list\n", ED_DbgEdict (&touch->v));
872 			}
873 		}
874 
875 		if (clip->passedict)
876 		{
877 			if (touch == clip->passedict)
878 				continue;
879 			if (clip->passedict->v.size[0] && !touch->v.size[0])
880 				continue;	// points never interact
881 			if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
882 				continue;	// don't clip against own missiles
883 			if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
884 				continue;	// don't clip against owner
885 		}
886 
887 		if (touch->v.solid == SOLID_TRIGGER)
888 		{
889 			ED_Print (touch);
890 			Sys_Error ("Trigger (%s) in clipping list", ED_DbgEdict (&touch->v));
891 		}
892 
893 		if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
894 			continue;
895 
896 		if (clip->boxmins[0] > touch->v.absmax[0]
897 		|| clip->boxmins[1] > touch->v.absmax[1]
898 		|| clip->boxmins[2] > touch->v.absmax[2]
899 		|| clip->boxmaxs[0] < touch->v.absmin[0]
900 		|| clip->boxmaxs[1] < touch->v.absmin[1]
901 		|| clip->boxmaxs[2] < touch->v.absmin[2] )
902 			continue;
903 
904 	// might intersect, so do an exact clip
905 		if (clip->trace.allsolid)
906 			return;
907 
908 		if ((int)touch->v.flags & FL_MONSTER)
909 			trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
910 		else
911 			trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
912 		if (trace.allsolid || trace.startsolid ||
913 		trace.fraction < clip->trace.fraction)
914 		{
915 			trace.ent = touch;
916 		 	if (clip->trace.startsolid)
917 			{
918 				clip->trace = trace;
919 				clip->trace.startsolid = true;
920 			}
921 			else
922 				clip->trace = trace;
923 		}
924 		else if (trace.startsolid)
925 			clip->trace.startsolid = true;
926 	}
927 
928 // recurse down both sides
929 	if (node->axis == -1)
930 		return;
931 
932 	if ( clip->boxmaxs[node->axis] > node->dist )
933 		SV_ClipToLinks ( node->children[0], clip );
934 	if ( clip->boxmins[node->axis] < node->dist )
935 		SV_ClipToLinks ( node->children[1], clip );
936 }
937 
938 
939 /*
940 ==================
941 SV_MoveBounds
942 ==================
943 */
SV_MoveBounds(vec3_t start,vec3_t mins,vec3_t maxs,vec3_t end,vec3_t boxmins,vec3_t boxmaxs)944 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
945 {
946 #if 0
947 // debug to test against everything
948 boxmins[0] = boxmins[1] = boxmins[2] = -9999;
949 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 9999;
950 #else
951 	int		i;
952 
953 	for (i=0 ; i<3 ; i++)
954 	{
955 		if (end[i] > start[i])
956 		{
957 			boxmins[i] = start[i] + mins[i] - 1;
958 			boxmaxs[i] = end[i] + maxs[i] + 1;
959 		}
960 		else
961 		{
962 			boxmins[i] = end[i] + mins[i] - 1;
963 			boxmaxs[i] = start[i] + maxs[i] + 1;
964 		}
965 	}
966 #endif
967 }
968 
969 /*
970 ==================
971 SV_Move
972 ==================
973 */
SV_Move(vec3_t start,vec3_t mins,vec3_t maxs,vec3_t end,int type,edict_t * passedict)974 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
975 {
976 	moveclip_t	clip;
977 	int			i;
978 
979 	memset ( &clip, 0, sizeof ( moveclip_t ) );
980 
981 // clip to world
982 	clip.trace = SV_ClipMoveToEntity ( sv.edicts, start, mins, maxs, end );
983 
984 	clip.start = start;
985 	clip.end = end;
986 	clip.mins = mins;
987 	clip.maxs = maxs;
988 	clip.type = type;
989 	clip.passedict = passedict;
990 
991 	if (type == MOVE_MISSILE)
992 	{
993 		for (i=0 ; i<3 ; i++)
994 		{
995 			clip.mins2[i] = -15;
996 			clip.maxs2[i] = 15;
997 		}
998 	}
999 	else
1000 	{
1001 		VectorCopy (mins, clip.mins2);
1002 		VectorCopy (maxs, clip.maxs2);
1003 	}
1004 
1005 // create the bounding box of the entire move
1006 	SV_MoveBounds ( start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs );
1007 
1008 // clip to entities
1009 	SV_ClipToLinks ( sv_areanodes, &clip );
1010 
1011 	return clip.trace;
1012 }
1013 
1014