2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // world.c -- world query functions
26 entities never clip against themselves, or their owner
28 line of sight checks trace->crosscontent, but bullets don't
35 struct link_s *prev, *next;
40 void ClearLink (link_t *l);
41 void RemoveLink (link_t *l);
42 void InsertLinkBefore (link_t *l, link_t *before);
43 void InsertLinkAfter (link_t *l, link_t *after);
45 // (type *)STRUCT_FROM_LINK(link_t *link, type, member)
46 // ent = STRUCT_FROM_LINK(link,entity_t,order)
47 // FIXME: remove this mess!
48 //#define STRUCT_FROM_LINK(l,t,m) ((t *)((qbyte *)l - (int)&(((t *)0)->m)))
50 #define EDICT_FROM_AREA(l) ((edict_t *)((qbyte *)l - (int)&(((edict_t *)0)->area)))
52 //============================================================================
54 // ClearLink is used for new headnodes
55 void ClearLink (link_t *l)
57 l->prev = l->next = l;
60 void RemoveLink (link_t *l)
62 l->next->prev = l->prev;
63 l->prev->next = l->next;
66 void InsertLinkBefore (link_t *l, link_t *before)
69 l->prev = before->prev;
73 void InsertLinkAfter (link_t *l, link_t *after)
75 l->next = after->next;
84 // bounding box of entire move area
85 vec3_t boxmins, boxmaxs;
87 // size of the moving object
90 // size when clipping against monsters
93 // size when clipping against brush models
94 vec3_t hullmins, hullmaxs;
96 // start and end origin of move
102 // type of move (like ignoring monsters, or similar)
105 // the edict that is moving (if any)
112 ===============================================================================
116 ===============================================================================
119 typedef struct areanode_s
121 int axis; // -1 = leaf node
123 struct areanode_s *children[2];
124 link_t trigger_edicts;
129 #define AREA_NODES 32
131 static areanode_t sv_areanodes[AREA_NODES];
132 static int sv_numareanodes;
140 areanode_t *SV_CreateAreaNode (int depth, vec3_t mins, vec3_t maxs)
144 vec3_t mins1, maxs1, mins2, maxs2;
146 anode = &sv_areanodes[sv_numareanodes];
149 ClearLink (&anode->trigger_edicts);
150 ClearLink (&anode->solid_edicts);
152 if (depth == AREA_DEPTH)
155 anode->children[0] = anode->children[1] = NULL;
159 VectorSubtract (maxs, mins, size);
160 if (size[0] > size[1])
165 anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
166 VectorCopy (mins, mins1);
167 VectorCopy (mins, mins2);
168 VectorCopy (maxs, maxs1);
169 VectorCopy (maxs, maxs2);
171 maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
173 anode->children[0] = SV_CreateAreaNode (depth+1, mins2, maxs2);
174 anode->children[1] = SV_CreateAreaNode (depth+1, mins1, maxs1);
185 void SV_ClearWorld (void)
189 memset (sv_areanodes, 0, sizeof(sv_areanodes));
191 Mod_CheckLoaded(sv.worldmodel);
192 SV_CreateAreaNode (0, sv.worldmodel->normalmins, sv.worldmodel->normalmaxs);
202 void SV_UnlinkEdict (edict_t *ent)
205 return; // not linked in anywhere
206 RemoveLink (&ent->area);
207 ent->area.prev = ent->area.next = NULL;
216 void SV_TouchLinks ( edict_t *ent, areanode_t *node )
220 int old_self, old_other;
223 // touch linked edicts
224 for (l = node->trigger_edicts.next ; l != &node->trigger_edicts ; l = next)
227 touch = EDICT_FROM_AREA(l);
230 if (!touch->v.touch || touch->v.solid != SOLID_TRIGGER)
232 if (ent->v.absmin[0] > touch->v.absmax[0]
233 || ent->v.absmin[1] > touch->v.absmax[1]
234 || ent->v.absmin[2] > touch->v.absmax[2]
235 || ent->v.absmax[0] < touch->v.absmin[0]
236 || ent->v.absmax[1] < touch->v.absmin[1]
237 || ent->v.absmax[2] < touch->v.absmin[2])
239 old_self = pr_global_struct->self;
240 old_other = pr_global_struct->other;
242 pr_global_struct->self = EDICT_TO_PROG(touch);
243 pr_global_struct->other = EDICT_TO_PROG(ent);
244 pr_global_struct->time = sv.time;
245 PR_ExecuteProgram (touch->v.touch, "");
247 pr_global_struct->self = old_self;
248 pr_global_struct->other = old_other;
251 // recurse down both sides
252 if (node->axis == -1)
255 // LordHavoc: optimized recursion
256 // if (ent->v.absmax[node->axis] > node->dist) SV_TouchLinks (ent, node->children[0]);
257 // if (ent->v.absmin[node->axis] < node->dist) SV_TouchLinks (ent, node->children[1]);
258 if (ent->v.absmax[node->axis] > node->dist)
260 if (ent->v.absmin[node->axis] < node->dist)
261 SV_TouchLinks(ent, node->children[1]); // order reversed to reduce code
262 node = node->children[0];
267 if (ent->v.absmin[node->axis] < node->dist)
269 node = node->children[1];
282 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
288 SV_UnlinkEdict (ent); // unlink from old position
290 if (ent == sv.edicts)
291 return; // don't add the world
299 // LordHavoc: enabling rotating bmodels
300 if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
302 // expand for rotation
306 max = DotProduct(ent->v.mins, ent->v.mins);
307 v = DotProduct(ent->v.maxs, ent->v.maxs);
314 for (i=0 ; i<3 ; i++)
316 v = fabs(ent->v.mins[i]);
319 v = fabs(ent->v.maxs[i]);
325 for (i=0 ; i<3 ; i++)
327 ent->v.absmin[i] = ent->v.origin[i] - max;
328 ent->v.absmax[i] = ent->v.origin[i] + max;
333 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
334 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
338 if (ent->v.solid == SOLID_BSP)
340 if (ent->v.modelindex < 0 || ent->v.modelindex > MAX_MODELS)
341 PR_RunError("SOLID_BSP with invalid modelindex!\n");
342 model = sv.models[(int) ent->v.modelindex];
345 if (model->type != mod_brush)
346 PR_RunError("SOLID_BSP with non-BSP model\n");
348 if (ent->v.angles[0] || ent->v.angles[2] || ent->v.avelocity[0] || ent->v.avelocity[2])
350 VectorAdd(ent->v.origin, model->rotatedmins, ent->v.absmin);
351 VectorAdd(ent->v.origin, model->rotatedmaxs, ent->v.absmax);
353 else if (ent->v.angles[1] || ent->v.avelocity[1])
355 VectorAdd(ent->v.origin, model->yawmins, ent->v.absmin);
356 VectorAdd(ent->v.origin, model->yawmaxs, ent->v.absmax);
360 VectorAdd(ent->v.origin, model->normalmins, ent->v.absmin);
361 VectorAdd(ent->v.origin, model->normalmaxs, ent->v.absmax);
366 // SOLID_BSP with no model is valid, mainly because some QC setup code does so temporarily
367 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
368 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
373 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
374 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
378 // to make items easier to pick up and allow them to be grabbed off
379 // of shelves, the abs sizes are expanded
381 if ((int)ent->v.flags & FL_ITEM)
383 ent->v.absmin[0] -= 15;
384 ent->v.absmin[1] -= 15;
385 ent->v.absmin[2] -= 1;
386 ent->v.absmax[0] += 15;
387 ent->v.absmax[1] += 15;
388 ent->v.absmax[2] += 1;
392 // because movement is clipped an epsilon away from an actual edge,
393 // we must fully check even when bounding boxes don't quite touch
394 ent->v.absmin[0] -= 1;
395 ent->v.absmin[1] -= 1;
396 ent->v.absmin[2] -= 1;
397 ent->v.absmax[0] += 1;
398 ent->v.absmax[1] += 1;
399 ent->v.absmax[2] += 1;
402 if (ent->v.solid == SOLID_NOT)
405 // find the first node that the ent's box crosses
409 if (node->axis == -1)
411 if (ent->v.absmin[node->axis] > node->dist)
412 node = node->children[0];
413 else if (ent->v.absmax[node->axis] < node->dist)
414 node = node->children[1];
416 break; // crosses the node
421 if (ent->v.solid == SOLID_TRIGGER)
422 InsertLinkBefore (&ent->area, &node->trigger_edicts);
424 InsertLinkBefore (&ent->area, &node->solid_edicts);
426 // if touch_triggers, touch all entities at this node and descend for more
428 SV_TouchLinks ( ent, sv_areanodes );
434 ===============================================================================
436 POINT TESTING IN HULLS
438 ===============================================================================
447 int SV_HullPointContents (hull_t *hull, int num, vec3_t p)
450 num = hull->clipnodes[num].children[(hull->planes[hull->clipnodes[num].planenum].type < 3 ? p[hull->planes[hull->clipnodes[num].planenum].type] : DotProduct (hull->planes[hull->clipnodes[num].planenum].normal, p)) < hull->planes[hull->clipnodes[num].planenum].dist];
457 SV_TestEntityPosition
459 This could be a lot more efficient...
462 edict_t *SV_TestEntityPosition (edict_t *ent)
466 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, MOVE_NORMAL, ent);
468 if (trace.startsolid)
476 ===============================================================================
478 LINE TESTING IN HULLS
480 ===============================================================================
487 Handles selection or creation of a clipping hull, and offseting (and
488 eventually rotation) of the end points
491 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
497 i = ent->v.modelindex;
498 if ((unsigned int) i >= MAX_MODELS)
499 PR_RunError("SV_ClipMoveToEntity: invalid modelindex\n");
500 model = sv.models[i];
501 if (i != 0 && model == NULL)
502 PR_RunError("SV_ClipMoveToEntity: invalid modelindex\n");
504 if ((int) ent->v.solid == SOLID_BSP)
506 Mod_CheckLoaded(model);
507 if (model->type != mod_brush)
509 Con_Printf ("SV_ClipMoveToEntity: SOLID_BSP with a non bsp model, entity dump:\n");
511 Host_Error ("SV_ClipMoveToEntity: SOLID_BSP with a non bsp model\n");
513 if (ent->v.movetype != MOVETYPE_PUSH)
514 Host_Error ("SV_ClipMoveToEntity: SOLID_BSP without MOVETYPE_PUSH");
517 Collision_ClipTrace(&trace, ent, model, ent->v.origin, ent->v.angles, ent->v.mins, ent->v.maxs, start, mins, maxs, end);
522 //===========================================================================
528 Mins and maxs enclose the entire area swept by the move
531 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
538 if (clip->trace.allsolid)
540 // touch linked edicts
541 for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
544 touch = EDICT_FROM_AREA(l);
545 if (touch->v.solid == SOLID_NOT)
547 if (touch == clip->passedict)
549 if (touch->v.solid == SOLID_TRIGGER)
550 Host_Error ("Trigger in clipping list");
552 if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
555 if (clip->boxmins[0] > touch->v.absmax[0]
556 || clip->boxmaxs[0] < touch->v.absmin[0]
557 || clip->boxmins[1] > touch->v.absmax[1]
558 || clip->boxmaxs[1] < touch->v.absmin[1]
559 || clip->boxmins[2] > touch->v.absmax[2]
560 || clip->boxmaxs[2] < touch->v.absmin[2])
565 if (clip->passedict->v.size[0] && !touch->v.size[0])
566 continue; // points never interact
567 if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
568 continue; // don't clip against own missiles
569 if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
570 continue; // don't clip against owner
571 // LordHavoc: corpse code
572 if (clip->passedict->v.solid == SOLID_CORPSE && (touch->v.solid == SOLID_SLIDEBOX || touch->v.solid == SOLID_CORPSE))
574 if (clip->passedict->v.solid == SOLID_SLIDEBOX && touch->v.solid == SOLID_CORPSE)
578 // might interact, so do an exact clip
579 if ((int)touch->v.flags & FL_MONSTER)
580 trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
581 else if (touch->v.solid == SOLID_BSP)
582 trace = SV_ClipMoveToEntity (touch, clip->start, clip->hullmins, clip->hullmaxs, clip->end);
584 trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
585 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
587 clip->trace.allsolid = true;
588 if (trace.startsolid)
590 clip->trace.startsolid = true;
591 if (!clip->trace.ent)
592 clip->trace.ent = trace.ent;
595 clip->trace.inopen = true;
597 clip->trace.inwater = true;
598 if (trace.fraction < clip->trace.fraction)
600 clip->trace.fraction = trace.fraction;
601 VectorCopy(trace.endpos, clip->trace.endpos);
602 clip->trace.plane = trace.plane;
603 clip->trace.endcontents = trace.endcontents;
604 clip->trace.ent = trace.ent;
608 // recurse down both sides
609 if (node->axis == -1)
612 // LordHavoc: optimized recursion
613 // if (clip->boxmaxs[node->axis] > node->dist) SV_ClipToLinks(node->children[0], clip);
614 // if (clip->boxmins[node->axis] < node->dist) SV_ClipToLinks(node->children[1], clip);
615 if (clip->boxmaxs[node->axis] > node->dist)
617 if (clip->boxmins[node->axis] < node->dist)
618 SV_ClipToLinks(node->children[1], clip);
619 node = node->children[0];
622 else if (clip->boxmins[node->axis] < node->dist)
624 node = node->children[1];
635 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
638 // debug to test against everything
639 boxmins[0] = boxmins[1] = boxmins[2] = -999999999;
640 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 999999999;
644 for (i=0 ; i<3 ; i++)
646 if (end[i] > start[i])
648 boxmins[i] = start[i] + mins[i] - 1;
649 boxmaxs[i] = end[i] + maxs[i] + 1;
653 boxmins[i] = end[i] + mins[i] - 1;
654 boxmaxs[i] = start[i] + maxs[i] + 1;
665 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
668 vec3_t bigmins, bigmaxs;
671 memset ( &clip, 0, sizeof ( moveclip_t ) );
673 VectorCopy(start, clip.start);
674 VectorCopy(end, clip.end);
675 VectorCopy(mins, clip.mins);
676 VectorCopy(maxs, clip.maxs);
678 clip.passedict = passedict;
680 Collision_RoundUpToHullSize(sv.worldmodel, clip.mins, clip.maxs, clip.hullmins, clip.hullmaxs);
682 if (type == MOVE_MISSILE)
684 // LordHavoc: modified this, was = -15, now = clip.mins[i] - 15
685 for (i=0 ; i<3 ; i++)
687 clip.mins2[i] = clip.mins[i] - 15;
688 clip.maxs2[i] = clip.maxs[i] + 15;
693 VectorCopy (clip.mins, clip.mins2);
694 VectorCopy (clip.maxs, clip.maxs2);
697 bigmins[0] = min(clip.mins2[0], clip.hullmins[0]);
698 bigmaxs[0] = max(clip.maxs2[0], clip.hullmaxs[0]);
699 bigmins[1] = min(clip.mins2[1], clip.hullmins[1]);
700 bigmaxs[1] = max(clip.maxs2[1], clip.hullmaxs[1]);
701 bigmins[2] = min(clip.mins2[2], clip.hullmins[2]);
702 bigmaxs[2] = max(clip.maxs2[2], clip.hullmaxs[2]);
705 clip.trace = SV_ClipMoveToEntity (sv.edicts, start, mins, maxs, end);
708 // create the bounding box of the entire move
709 SV_MoveBounds ( start, bigmins, bigmaxs, end, clip.boxmins, clip.boxmaxs );
711 SV_ClipToLinks ( sv_areanodes, &clip );