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
32 cvar_t sv_useareanodes = {CVAR_NOTIFY, "sv_useareanodes", "1"};
33 cvar_t sv_polygoncollisions = {CVAR_NOTIFY, "sv_polygoncollisions", "0"};
35 void SV_World_Init(void)
37 Cvar_RegisterVariable(&sv_useareanodes);
38 Cvar_RegisterVariable(&sv_polygoncollisions);
43 void ClearLink (link_t *l);
44 void RemoveLink (link_t *l);
45 void InsertLinkBefore (link_t *l, link_t *before);
46 void InsertLinkAfter (link_t *l, link_t *after);
48 #define EDICT_FROM_AREA(l) ((edict_t *)((qbyte *)l - (int)&(((edict_t *)0)->area)))
50 //============================================================================
52 // ClearLink is used for new headnodes
53 void ClearLink (link_t *l)
55 l->prev = l->next = l;
58 void RemoveLink (link_t *l)
60 l->next->prev = l->prev;
61 l->prev->next = l->next;
64 void InsertLinkBefore (link_t *l, link_t *before)
67 l->prev = before->prev;
71 void InsertLinkAfter (link_t *l, link_t *after)
73 l->next = after->next;
82 // bounding box of entire move area
83 vec3_t boxmins, boxmaxs;
85 // size of the moving object
88 // size when clipping against monsters
91 // size when clipping against brush models
92 vec3_t hullmins, hullmaxs;
94 // start and end origin of move
100 // type of move (like ignoring monsters, or similar)
103 // the edict that is moving (if any)
110 ===============================================================================
114 ===============================================================================
117 typedef struct areanode_s
119 int axis; // -1 = leaf node
121 struct areanode_s *children[2];
122 link_t trigger_edicts;
126 #define AREA_DEPTH 10
127 #define AREA_NODES (1 << (AREA_DEPTH + 1))
129 static areanode_t sv_areanodes[AREA_NODES];
130 static int sv_numareanodes;
138 areanode_t *SV_CreateAreaNode (int depth, vec3_t mins, vec3_t maxs)
142 vec3_t mins1, maxs1, mins2, maxs2;
144 anode = &sv_areanodes[sv_numareanodes];
147 ClearLink (&anode->trigger_edicts);
148 ClearLink (&anode->solid_edicts);
150 if (depth == AREA_DEPTH)
153 anode->children[0] = anode->children[1] = NULL;
157 VectorSubtract (maxs, mins, size);
158 if (size[0] > size[1])
163 anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
164 VectorCopy (mins, mins1);
165 VectorCopy (mins, mins2);
166 VectorCopy (maxs, maxs1);
167 VectorCopy (maxs, maxs2);
169 maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
171 anode->children[0] = SV_CreateAreaNode (depth+1, mins2, maxs2);
172 anode->children[1] = SV_CreateAreaNode (depth+1, mins1, maxs1);
183 void SV_ClearWorld (void)
185 memset (sv_areanodes, 0, sizeof(sv_areanodes));
187 Mod_CheckLoaded(sv.worldmodel);
188 SV_CreateAreaNode (0, sv.worldmodel->normalmins, sv.worldmodel->normalmaxs);
198 void SV_UnlinkEdict (edict_t *ent)
201 return; // not linked in anywhere
202 RemoveLink (&ent->area);
203 ent->area.prev = ent->area.next = NULL;
212 void SV_TouchLinks ( edict_t *ent, areanode_t *node )
216 int old_self, old_other;
219 // touch linked edicts
220 for (l = node->trigger_edicts.next ; l != &node->trigger_edicts ; l = next)
223 touch = EDICT_FROM_AREA(l);
226 if (!touch->v->touch || touch->v->solid != SOLID_TRIGGER)
228 if (ent->v->absmin[0] > touch->v->absmax[0]
229 || ent->v->absmin[1] > touch->v->absmax[1]
230 || ent->v->absmin[2] > touch->v->absmax[2]
231 || ent->v->absmax[0] < touch->v->absmin[0]
232 || ent->v->absmax[1] < touch->v->absmin[1]
233 || ent->v->absmax[2] < touch->v->absmin[2])
235 old_self = pr_global_struct->self;
236 old_other = pr_global_struct->other;
238 pr_global_struct->self = EDICT_TO_PROG(touch);
239 pr_global_struct->other = EDICT_TO_PROG(ent);
240 pr_global_struct->time = sv.time;
241 PR_ExecuteProgram (touch->v->touch, "");
243 pr_global_struct->self = old_self;
244 pr_global_struct->other = old_other;
247 // recurse down both sides
248 if (node->axis == -1)
251 if (ent->v->absmax[node->axis] > node->dist)
253 if (ent->v->absmin[node->axis] < node->dist)
254 SV_TouchLinks(ent, node->children[1]); // order reversed to reduce code
255 node = node->children[0];
260 if (ent->v->absmin[node->axis] < node->dist)
262 node = node->children[1];
275 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
281 SV_UnlinkEdict (ent); // unlink from old position
283 if (ent == sv.edicts)
284 return; // don't add the world
291 if (ent->v->solid == SOLID_BSP)
293 if (ent->v->modelindex < 0 || ent->v->modelindex > MAX_MODELS)
294 Host_Error("SOLID_BSP with invalid modelindex!\n");
295 model = sv.models[(int) ent->v->modelindex];
298 if (model->type != mod_brush)
299 Host_Error("SOLID_BSP with non-BSP model\n");
301 if (ent->v->angles[0] || ent->v->angles[2] || ent->v->avelocity[0] || ent->v->avelocity[2])
303 VectorAdd(ent->v->origin, model->rotatedmins, ent->v->absmin);
304 VectorAdd(ent->v->origin, model->rotatedmaxs, ent->v->absmax);
306 else if (ent->v->angles[1] || ent->v->avelocity[1])
308 VectorAdd(ent->v->origin, model->yawmins, ent->v->absmin);
309 VectorAdd(ent->v->origin, model->yawmaxs, ent->v->absmax);
313 VectorAdd(ent->v->origin, model->normalmins, ent->v->absmin);
314 VectorAdd(ent->v->origin, model->normalmaxs, ent->v->absmax);
319 // SOLID_BSP with no model is valid, mainly because some QC setup code does so temporarily
320 VectorAdd (ent->v->origin, ent->v->mins, ent->v->absmin);
321 VectorAdd (ent->v->origin, ent->v->maxs, ent->v->absmax);
326 VectorAdd (ent->v->origin, ent->v->mins, ent->v->absmin);
327 VectorAdd (ent->v->origin, ent->v->maxs, ent->v->absmax);
331 // to make items easier to pick up and allow them to be grabbed off
332 // of shelves, the abs sizes are expanded
334 if ((int)ent->v->flags & FL_ITEM)
336 ent->v->absmin[0] -= 15;
337 ent->v->absmin[1] -= 15;
338 ent->v->absmin[2] -= 1;
339 ent->v->absmax[0] += 15;
340 ent->v->absmax[1] += 15;
341 ent->v->absmax[2] += 1;
345 // because movement is clipped an epsilon away from an actual edge,
346 // we must fully check even when bounding boxes don't quite touch
347 ent->v->absmin[0] -= 1;
348 ent->v->absmin[1] -= 1;
349 ent->v->absmin[2] -= 1;
350 ent->v->absmax[0] += 1;
351 ent->v->absmax[1] += 1;
352 ent->v->absmax[2] += 1;
355 if (ent->v->solid == SOLID_NOT)
358 // find the first node that the ent's box crosses
362 if (node->axis == -1)
364 if (ent->v->absmin[node->axis] > node->dist)
365 node = node->children[0];
366 else if (ent->v->absmax[node->axis] < node->dist)
367 node = node->children[1];
369 break; // crosses the node
374 if (ent->v->solid == SOLID_TRIGGER)
375 InsertLinkBefore (&ent->area, &node->trigger_edicts);
377 InsertLinkBefore (&ent->area, &node->solid_edicts);
379 // if touch_triggers, touch all entities at this node and descend for more
381 SV_TouchLinks ( ent, sv_areanodes );
387 ===============================================================================
389 POINT TESTING IN HULLS
391 ===============================================================================
396 SV_TestEntityPosition
398 This could be a lot more efficient...
401 int SV_TestEntityPosition (edict_t *ent)
403 return SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, ent->v->origin, MOVE_NORMAL, ent).startsolid;
408 ===============================================================================
410 LINE TESTING IN HULLS
412 ===============================================================================
419 Handles selection or creation of a clipping hull, and offseting (and
420 eventually rotation) of the end points
423 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
429 i = ent->v->modelindex;
430 if ((unsigned int) i >= MAX_MODELS)
431 Host_Error("SV_ClipMoveToEntity: invalid modelindex\n");
432 model = sv.models[i];
433 if (i != 0 && model == NULL)
434 Host_Error("SV_ClipMoveToEntity: invalid modelindex\n");
436 if ((int) ent->v->solid == SOLID_BSP)
438 Mod_CheckLoaded(model);
439 if (model->type != mod_brush)
441 Con_Printf ("SV_ClipMoveToEntity: SOLID_BSP with a non bsp model, entity dump:\n");
443 Host_Error ("SV_ClipMoveToEntity: SOLID_BSP with a non bsp model\n");
445 if (ent->v->movetype != MOVETYPE_PUSH)
446 Host_Error ("SV_ClipMoveToEntity: SOLID_BSP without MOVETYPE_PUSH");
449 if (sv_polygoncollisions.integer && (mins[0] != maxs[0] || mins[1] != maxs[1] || mins[2] != maxs[2]))
450 Collision_PolygonClipTrace(&trace, ent, model, ent->v->origin, ent->v->angles, ent->v->mins, ent->v->maxs, start, mins, maxs, end);
452 Collision_ClipTrace(&trace, ent, model, ent->v->origin, ent->v->angles, ent->v->mins, ent->v->maxs, start, mins, maxs, end);
457 //===========================================================================
463 Mins and maxs enclose the entire area swept by the move
466 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
473 if (clip->trace.allsolid)
475 // touch linked edicts
476 for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
479 touch = EDICT_FROM_AREA(l);
480 if (touch->v->solid == SOLID_NOT)
482 if (touch == clip->passedict)
484 if (touch->v->solid == SOLID_TRIGGER)
487 Host_Error ("Trigger in clipping list");
490 if (clip->type == MOVE_NOMONSTERS && touch->v->solid != SOLID_BSP)
493 if (clip->boxmins[0] > touch->v->absmax[0]
494 || clip->boxmaxs[0] < touch->v->absmin[0]
495 || clip->boxmins[1] > touch->v->absmax[1]
496 || clip->boxmaxs[1] < touch->v->absmin[1]
497 || clip->boxmins[2] > touch->v->absmax[2]
498 || clip->boxmaxs[2] < touch->v->absmin[2])
503 if (clip->passedict->v->size[0] && !touch->v->size[0])
504 continue; // points never interact
505 if (PROG_TO_EDICT(touch->v->owner) == clip->passedict)
506 continue; // don't clip against own missiles
507 if (PROG_TO_EDICT(clip->passedict->v->owner) == touch)
508 continue; // don't clip against owner
509 // LordHavoc: corpse code
510 if (clip->passedict->v->solid == SOLID_CORPSE && (touch->v->solid == SOLID_SLIDEBOX || touch->v->solid == SOLID_CORPSE))
512 if (clip->passedict->v->solid == SOLID_SLIDEBOX && touch->v->solid == SOLID_CORPSE)
516 // might interact, so do an exact clip
517 if (touch->v->solid == SOLID_BSP)
518 trace = SV_ClipMoveToEntity (touch, clip->start, clip->hullmins, clip->hullmaxs, clip->end);
519 else if ((int)touch->v->flags & FL_MONSTER)
520 trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
522 trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
523 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
525 clip->trace.allsolid = true;
526 if (trace.startsolid)
528 clip->trace.startsolid = true;
529 if (!clip->trace.ent)
530 clip->trace.ent = trace.ent;
533 clip->trace.inopen = true;
535 clip->trace.inwater = true;
536 if (trace.fraction < clip->trace.fraction)
538 clip->trace.fraction = trace.fraction;
539 VectorCopy(trace.endpos, clip->trace.endpos);
540 clip->trace.plane = trace.plane;
541 clip->trace.endcontents = trace.endcontents;
542 clip->trace.ent = trace.ent;
546 // recurse down both sides
547 if (node->axis == -1)
550 if (clip->boxmaxs[node->axis] > node->dist)
552 if (clip->boxmins[node->axis] < node->dist)
553 SV_ClipToLinks(node->children[1], clip);
554 node = node->children[0];
557 else if (clip->boxmins[node->axis] < node->dist)
559 node = node->children[1];
570 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
572 if (sv_useareanodes.integer)
576 for (i=0 ; i<3 ; i++)
578 if (end[i] > start[i])
580 boxmins[i] = start[i] + mins[i] - 1;
581 boxmaxs[i] = end[i] + maxs[i] + 1;
585 boxmins[i] = end[i] + mins[i] - 1;
586 boxmaxs[i] = start[i] + maxs[i] + 1;
592 // debug to test against everything
593 boxmins[0] = boxmins[1] = boxmins[2] = -999999999;
594 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 999999999;
603 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
606 vec3_t bigmins, bigmaxs;
609 memset ( &clip, 0, sizeof ( moveclip_t ) );
611 VectorCopy(start, clip.start);
612 VectorCopy(end, clip.end);
613 VectorCopy(mins, clip.mins);
614 VectorCopy(maxs, clip.maxs);
616 clip.passedict = passedict;
618 Collision_RoundUpToHullSize(sv.worldmodel, clip.mins, clip.maxs, clip.hullmins, clip.hullmaxs);
620 if (type == MOVE_MISSILE)
622 // LordHavoc: modified this, was = -15, now = clip.mins[i] - 15
623 for (i=0 ; i<3 ; i++)
625 clip.mins2[i] = clip.mins[i] - 15;
626 clip.maxs2[i] = clip.maxs[i] + 15;
631 VectorCopy (clip.mins, clip.mins2);
632 VectorCopy (clip.maxs, clip.maxs2);
635 bigmins[0] = min(clip.mins2[0], clip.hullmins[0]);
636 bigmaxs[0] = max(clip.maxs2[0], clip.hullmaxs[0]);
637 bigmins[1] = min(clip.mins2[1], clip.hullmins[1]);
638 bigmaxs[1] = max(clip.maxs2[1], clip.hullmaxs[1]);
639 bigmins[2] = min(clip.mins2[2], clip.hullmins[2]);
640 bigmaxs[2] = max(clip.maxs2[2], clip.hullmaxs[2]);
643 clip.trace = SV_ClipMoveToEntity (sv.edicts, start, mins, maxs, end);
646 // create the bounding box of the entire move
647 SV_MoveBounds ( start, bigmins, bigmaxs, end, clip.boxmins, clip.boxmaxs );
649 SV_ClipToLinks ( sv_areanodes, &clip );