Debian Codegraph @

code/server/sv_world.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // world.c -- world query functions
23
24 #include "server.h"
25
26 /*
27 ================
28 SV_ClipHandleForEntity
29
30 Returns a headnode that can be used for testing or clipping to a
31 given entity. If the entity is a bsp model, the headnode will
32 be returned, otherwise a custom box tree will be constructed.
33 ================
34 */
35 clipHandle_t SV_ClipHandleForEntity( const sharedEntity_t *ent ) {
36 if ( ent->r.bmodel ) {
37 // explicit hulls in the BSP model
38 return CM_InlineModel( ent->s.modelindex );
39 }
40 if ( ent->r.svFlags & SVF_CAPSULE ) {
41 // create a temp capsule from bounding box sizes
42 return CM_TempBoxModel( ent->r.mins, ent->r.maxs, qtrue );
43 }
44
45 // create a temp tree from bounding box sizes
46 return CM_TempBoxModel( ent->r.mins, ent->r.maxs, qfalse );
47 }
48
49
50
51 /*
52 ===============================================================================
53
54 ENTITY CHECKING
55
56 To avoid linearly searching through lists of entities during environment testing,
57 the world is carved up with an evenly spaced, axially aligned bsp tree. Entities
58 are kept in chains either at the final leafs, or at the first node that splits
59 them, which prevents having to deal with multiple fragments of a single entity.
60
61 ===============================================================================
62 */
63
64 typedef struct worldSector_s {
65 int axis; // -1 = leaf node
66 float dist;
67 struct worldSector_s *children[2];
68 svEntity_t *entities;
69 } worldSector_t;
70
71 #define AREA_DEPTH 4
72 #define AREA_NODES 64
73
74 worldSector_t sv_worldSectors[AREA_NODES];
75 int sv_numworldSectors;
76
77
78 /*
79 ===============
80 SV_SectorList_f
81 ===============
82 */
83 void SV_SectorList_f( void ) {
84 int i, c;
85 worldSector_t *sec;
86 svEntity_t *ent;
87
88 for ( i = 0 ; i < AREA_NODES ; i++ ) {
89 sec = &sv_worldSectors[i];
90
91 c = 0;
92 for ( ent = sec->entities ; ent ; ent = ent->nextEntityInWorldSector ) {
93 c++;
94 }
95 Com_Printf( "sector %i: %i entities\n", i, c );
96 }
97 }
98
99 /*
100 ===============
101 SV_CreateworldSector
102
103 Builds a uniformly subdivided tree for the given world size
104 ===============
105 */
106 static worldSector_t *SV_CreateworldSector( int depth, vec3_t mins, vec3_t maxs ) {
107 worldSector_t *anode;
108 vec3_t size;
109 vec3_t mins1, maxs1, mins2, maxs2;
110
111 anode = &sv_worldSectors[sv_numworldSectors];
112 sv_numworldSectors++;
113
114 if (depth == AREA_DEPTH) {
115 anode->axis = -1;
116 anode->children[0] = anode->children[1] = NULL;
117 return anode;
118 }
119
120 VectorSubtract (maxs, mins, size);
121 if (size[0] > size[1]) {
122 anode->axis = 0;
123 } else {
124 anode->axis = 1;
125 }
126
127 anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
128 VectorCopy (mins, mins1);
129 VectorCopy (mins, mins2);
130 VectorCopy (maxs, maxs1);
131 VectorCopy (maxs, maxs2);
132
133 maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
134
135 anode->children[0] = SV_CreateworldSector (depth+1, mins2, maxs2);
136 anode->children[1] = SV_CreateworldSector (depth+1, mins1, maxs1);
137
138 return anode;
139 }
140
141 /*
142 ===============
143 SV_ClearWorld
144
145 ===============
146 */
147 void SV_ClearWorld( void ) {
148 clipHandle_t h;
149 vec3_t mins, maxs;
150
151 Com_Memset( sv_worldSectors, 0, sizeof(sv_worldSectors) );
152 sv_numworldSectors = 0;
153
154 // get world map bounds
155 h = CM_InlineModel( 0 );
156 CM_ModelBounds( h, mins, maxs );
157 SV_CreateworldSector( 0, mins, maxs );
158 }
159
160
161 /*
162 ===============
163 SV_UnlinkEntity
164
165 ===============
166 */
167 void SV_UnlinkEntity( sharedEntity_t *gEnt ) {
168 svEntity_t *ent;
169 svEntity_t *scan;
170 worldSector_t *ws;
171
172 ent = SV_SvEntityForGentity( gEnt );
173
174 gEnt->r.linked = qfalse;
175
176 ws = ent->worldSector;
177 if ( !ws ) {
178 return; // not linked in anywhere
179 }
180 ent->worldSector = NULL;
181
182 if ( ws->entities == ent ) {
183 ws->entities = ent->nextEntityInWorldSector;
184 return;
185 }
186
187 for ( scan = ws->entities ; scan ; scan = scan->nextEntityInWorldSector ) {
188 if ( scan->nextEntityInWorldSector == ent ) {
189 scan->nextEntityInWorldSector = ent->nextEntityInWorldSector;
190 return;
191 }
192 }
193
194 Com_Printf( "WARNING: SV_UnlinkEntity: not found in worldSector\n" );
195 }
196
197
198 /*
199 ===============
200 SV_LinkEntity
201
202 ===============
203 */
204 #define MAX_TOTAL_ENT_LEAFS 128
205 void SV_LinkEntity( sharedEntity_t *gEnt ) {
206 worldSector_t *node;
207 int leafs[MAX_TOTAL_ENT_LEAFS];
208 int cluster;
209 int num_leafs;
210 int i, j, k;
211 int area;
212 int lastLeaf;
213 float *origin, *angles;
214 svEntity_t *ent;
215
216 ent = SV_SvEntityForGentity( gEnt );
217
218 if ( ent->worldSector ) {
219 SV_UnlinkEntity( gEnt ); // unlink from old position
220 }
221
222 // encode the size into the entityState_t for client prediction
223 if ( gEnt->r.bmodel ) {
224 gEnt->s.solid = SOLID_BMODEL; // a solid_box will never create this value
225 } else if ( gEnt->r.contents & ( CONTENTS_SOLID | CONTENTS_BODY ) ) {
226 // assume that x/y are equal and symetric
227 i = gEnt->r.maxs[0];
228 if (i<1)
229 i = 1;
230 if (i>255)
231 i = 255;
232
233 // z is not symetric
234 j = (-gEnt->r.mins[2]);
235 if (j<1)
236 j = 1;
237 if (j>255)
238 j = 255;
239
240 // and z maxs can be negative...
241 k = (gEnt->r.maxs[2]+32);
242 if (k<1)
243 k = 1;
244 if (k>255)
245 k = 255;
246
247 gEnt->s.solid = (k<<16) | (j<<8) | i;
248 } else {
249 gEnt->s.solid = 0;
250 }
251
252 // get the position
253 origin = gEnt->r.currentOrigin;
254 angles = gEnt->r.currentAngles;
255
256 // set the abs box
257 if ( gEnt->r.bmodel && (angles[0] || angles[1] || angles[2]) ) {
258 // expand for rotation
259 float max;
260
261 max = RadiusFromBounds( gEnt->r.mins, gEnt->r.maxs );
262 for (i=0 ; i<3 ; i++) {
263 gEnt->r.absmin[i] = origin[i] - max;
264 gEnt->r.absmax[i] = origin[i] + max;
265 }
266 } else {
267 // normal
268 VectorAdd (origin, gEnt->r.mins, gEnt->r.absmin);
269 VectorAdd (origin, gEnt->r.maxs, gEnt->r.absmax);
270 }
271
272 // because movement is clipped an epsilon away from an actual edge,
273 // we must fully check even when bounding boxes don't quite touch
274 gEnt->r.absmin[0] -= 1;
275 gEnt->r.absmin[1] -= 1;
276 gEnt->r.absmin[2] -= 1;
277 gEnt->r.absmax[0] += 1;
278 gEnt->r.absmax[1] += 1;
279 gEnt->r.absmax[2] += 1;
280
281 // link to PVS leafs
282 ent->numClusters = 0;
283 ent->lastCluster = 0;
284 ent->areanum = -1;
285 ent->areanum2 = -1;
286
287 //get all leafs, including solids
288 num_leafs = CM_BoxLeafnums( gEnt->r.absmin, gEnt->r.absmax,
289 leafs, MAX_TOTAL_ENT_LEAFS, &lastLeaf );
290
291 // if none of the leafs were inside the map, the
292 // entity is outside the world and can be considered unlinked
293 if ( !num_leafs ) {
294 return;
295 }
296
297 // set areas, even from clusters that don't fit in the entity array
298 for (i=0 ; i<num_leafs ; i++) {
299 area = CM_LeafArea (leafs[i]);
300 if (area != -1) {
301 // doors may legally straggle two areas,
302 // but nothing should evern need more than that
303 if (ent->areanum != -1 && ent->areanum != area) {
304 if (ent->areanum2 != -1 && ent->areanum2 != area && sv.state == SS_LOADING) {
305 Com_DPrintf ("Object %i touching 3 areas at %f %f %f\n",
306 gEnt->s.number,
307 gEnt->r.absmin[0], gEnt->r.absmin[1], gEnt->r.absmin[2]);
308 }
309 ent->areanum2 = area;
310 } else {
311 ent->areanum = area;
312 }
313 }
314 }
315
316 // store as many explicit clusters as we can
317 ent->numClusters = 0;
318 for (i=0 ; i < num_leafs ; i++) {
319 cluster = CM_LeafCluster( leafs[i] );
320 if ( cluster != -1 ) {
321 ent->clusternums[ent->numClusters++] = cluster;
322 if ( ent->numClusters == MAX_ENT_CLUSTERS ) {
323 break;
324 }
325 }
326 }
327
328 // store off a last cluster if we need to
329 if ( i != num_leafs ) {
330 ent->lastCluster = CM_LeafCluster( lastLeaf );
331 }
332
333 gEnt->r.linkcount++;
334
335 // find the first world sector node that the ent's box crosses
336 node = sv_worldSectors;
337 while (1)
338 {
339 if (node->axis == -1)
340 break;
341 if ( gEnt->r.absmin[node->axis] > node->dist)
342 node = node->children[0];
343 else if ( gEnt->r.absmax[node->axis] < node->dist)
344 node = node->children[1];
345 else
346 break; // crosses the node
347 }
348
349 // link it in
350 ent->worldSector = node;
351 ent->nextEntityInWorldSector = node->entities;
352 node->entities = ent;
353
354 gEnt->r.linked = qtrue;
355 }
356
357 /*
358 ============================================================================
359
360 AREA QUERY
361
362 Fills in a list of all entities who's absmin / absmax intersects the given
363 bounds. This does NOT mean that they actually touch in the case of bmodels.
364 ============================================================================
365 */
366
367 typedef struct {
368 const float *mins;
369 const float *maxs;
370 int *list;
371 int count, maxcount;
372 } areaParms_t;
373
374
375 /*
376 ====================
377 SV_AreaEntities_r
378
379 ====================
380 */
381 static void SV_AreaEntities_r( worldSector_t *node, areaParms_t *ap ) {
382 svEntity_t *check, *next;
383 sharedEntity_t *gcheck;
384
385 for ( check = node->entities ; check ; check = next ) {
386 next = check->nextEntityInWorldSector;
387
388 gcheck = SV_GEntityForSvEntity( check );
389
390 if ( gcheck->r.absmin[0] > ap->maxs[0]
391 || gcheck->r.absmin[1] > ap->maxs[1]
392 || gcheck->r.absmin[2] > ap->maxs[2]
393 || gcheck->r.absmax[0] < ap->mins[0]
394 || gcheck->r.absmax[1] < ap->mins[1]
395 || gcheck->r.absmax[2] < ap->mins[2]) {
396 continue;
397 }
398
399 if ( ap->count == ap->maxcount ) {
400 Com_Printf ("SV_AreaEntities: MAXCOUNT\n");
401 return;
402 }
403
404 ap->list[ap->count] = check - sv.svEntities;
405 ap->count++;
406 }
407
408 if (node->axis == -1) {
409 return; // terminal node
410 }
411
412 // recurse down both sides
413 if ( ap->maxs[node->axis] > node->dist ) {
414 SV_AreaEntities_r ( node->children[0], ap );
415 }
416 if ( ap->mins[node->axis] < node->dist ) {
417 SV_AreaEntities_r ( node->children[1], ap );
418 }
419 }
420
421 /*
422 ================
423 SV_AreaEntities
424 ================
425 */
426 int SV_AreaEntities( const vec3_t mins, const vec3_t maxs, int *entityList, int maxcount ) {
427 areaParms_t ap;
428
429 ap.mins = mins;
430 ap.maxs = maxs;
431 ap.list = entityList;
432 ap.count = 0;
433 ap.maxcount = maxcount;
434
435 SV_AreaEntities_r( sv_worldSectors, &ap );
436
437 return ap.count;
438 }
439
440
441
442 //===========================================================================
443
444
445 typedef struct {
446 vec3_t boxmins, boxmaxs;// enclose the test object along entire move
447 const float *mins;
448 const float *maxs; // size of the moving object
449 const float *start;
450 vec3_t end;
451 trace_t trace;
452 int passEntityNum;
453 int contentmask;
454 int capsule;
455 } moveclip_t;
456
457
458 /*
459 ====================
460 SV_ClipToEntity
461
462 ====================
463 */
464 void SV_ClipToEntity( trace_t *trace, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int entityNum, int contentmask, int capsule ) {
465 sharedEntity_t *touch;
466 clipHandle_t clipHandle;
467 float *origin, *angles;
468
469 touch = SV_GentityNum( entityNum );
470
471 Com_Memset(trace, 0, sizeof(trace_t));
472
473 // if it doesn't have any brushes of a type we
474 // are looking for, ignore it
475 if ( ! ( contentmask & touch->r.contents ) ) {
476 trace->fraction = 1.0;
477 return;
478 }
479
480 // might intersect, so do an exact clip
481 clipHandle = SV_ClipHandleForEntity (touch);
482
483 origin = touch->r.currentOrigin;
484 angles = touch->r.currentAngles;
485
486 if ( !touch->r.bmodel ) {
487 angles = vec3_origin; // boxes don't rotate
488 }
489
490 CM_TransformedBoxTrace ( trace, (float *)start, (float *)end,
491 (float *)mins, (float *)maxs, clipHandle, contentmask,
492 origin, angles, capsule);
493
494 if ( trace->fraction < 1 ) {
495 trace->entityNum = touch->s.number;
496 }
497 }
498
499
500 /*
501 ====================
502 SV_ClipMoveToEntities
503
504 ====================
505 */
506 static void SV_ClipMoveToEntities( moveclip_t *clip ) {
507 int i, num;
508 int touchlist[MAX_GENTITIES];
509 sharedEntity_t *touch;
510 int passOwnerNum;
511 trace_t trace;
512 clipHandle_t clipHandle;
513 float *origin, *angles;
514
515 num = SV_AreaEntities( clip->boxmins, clip->boxmaxs, touchlist, MAX_GENTITIES);
516
517 if ( clip->passEntityNum != ENTITYNUM_NONE ) {
518 passOwnerNum = ( SV_GentityNum( clip->passEntityNum ) )->r.ownerNum;
519 if ( passOwnerNum == ENTITYNUM_NONE ) {
520 passOwnerNum = -1;
521 }
522 } else {
523 passOwnerNum = -1;
524 }
525
526 for ( i=0 ; i<num ; i++ ) {
527 if ( clip->trace.allsolid ) {
528 return;
529 }
530 touch = SV_GentityNum( touchlist[i] );
531
532 // see if we should ignore this entity
533 if ( clip->passEntityNum != ENTITYNUM_NONE ) {
534 if ( touchlist[i] == clip->passEntityNum ) {
535 continue; // don't clip against the pass entity
536 }
537 if ( touch->r.ownerNum == clip->passEntityNum ) {
538 continue; // don't clip against own missiles
539 }
540 if ( touch->r.ownerNum == passOwnerNum ) {
541 continue; // don't clip against other missiles from our owner
542 }
543 }
544
545 // if it doesn't have any brushes of a type we
546 // are looking for, ignore it
547 if ( ! ( clip->contentmask & touch->r.contents ) ) {
548 continue;
549 }
550
551 // might intersect, so do an exact clip
552 clipHandle = SV_ClipHandleForEntity (touch);
553
554 origin = touch->r.currentOrigin;
555 angles = touch->r.currentAngles;
556
557
558 if ( !touch->r.bmodel ) {
559 angles = vec3_origin; // boxes don't rotate
560 }
561
562 CM_TransformedBoxTrace ( &trace, (float *)clip->start, (float *)clip->end,
563 (float *)clip->mins, (float *)clip->maxs, clipHandle, clip->contentmask,
564 origin, angles, clip->capsule);
565
566 if ( trace.allsolid ) {
567 clip->trace.allsolid = qtrue;
568 trace.entityNum = touch->s.number;
569 } else if ( trace.startsolid ) {
570 clip->trace.startsolid = qtrue;
571 trace.entityNum = touch->s.number;
572 }
573
574 if ( trace.fraction < clip->trace.fraction ) {
575 qboolean oldStart;
576
577 // make sure we keep a startsolid from a previous trace
578 oldStart = clip->trace.startsolid;
579
580 trace.entityNum = touch->s.number;
581 clip->trace = trace;
582 clip->trace.startsolid |= oldStart;
583 }
584 }
585 }
586
587
588 /*
589 ==================
590 SV_Trace
591
592 Moves the given mins/maxs volume through the world from start to end.
593 passEntityNum and entities owned by passEntityNum are explicitly not checked.
594 ==================
595 */
596 void SV_Trace( trace_t *results, const vec3_t start, vec3_t mins, vec3_t maxs, const vec3_t end, int passEntityNum, int contentmask, int capsule ) {
597 moveclip_t clip;
598 int i;
599
600 if ( !mins ) {
601 mins = vec3_origin;
602 }
603 if ( !maxs ) {
604 maxs = vec3_origin;
605 }
606
607 Com_Memset ( &clip, 0, sizeof ( moveclip_t ) );
608
609 // clip to world
610 CM_BoxTrace( &clip.trace, start, end, mins, maxs, 0, contentmask, capsule );
611 clip.trace.entityNum = clip.trace.fraction != 1.0 ? ENTITYNUM_WORLD : ENTITYNUM_NONE;
612 if ( clip.trace.fraction == 0 ) {
613 *results = clip.trace;
614 return; // blocked immediately by the world
615 }
616
617 clip.contentmask = contentmask;
618 clip.start = start;
619 // VectorCopy( clip.trace.endpos, clip.end );
620 VectorCopy( end, clip.end );
621 clip.mins = mins;
622 clip.maxs = maxs;
623 clip.passEntityNum = passEntityNum;
624 clip.capsule = capsule;
625
626 // create the bounding box of the entire move
627 // we can limit it to the part of the move not
628 // already clipped off by the world, which can be
629 // a significant savings for line of sight and shot traces
630 for ( i=0 ; i<3 ; i++ ) {
631 if ( end[i] > start[i] ) {
632 clip.boxmins[i] = clip.start[i] + clip.mins[i] - 1;
633 clip.boxmaxs[i] = clip.end[i] + clip.maxs[i] + 1;
634 } else {
635 clip.boxmins[i] = clip.end[i] + clip.mins[i] - 1;
636 clip.boxmaxs[i] = clip.start[i] + clip.maxs[i] + 1;
637 }
638 }
639
640 // clip to other solid entities
641 SV_ClipMoveToEntities ( &clip );
642
643 *results = clip.trace;
644 }
645
646
647
648 /*
649 =============
650 SV_PointContents
651 =============
652 */
653 int SV_PointContents( const vec3_t p, int passEntityNum ) {
654 int touch[MAX_GENTITIES];
655 sharedEntity_t *hit;
656 int i, num;
657 int contents, c2;
658 clipHandle_t clipHandle;
659 float *angles;
660
661 // get base contents from world
662 contents = CM_PointContents( p, 0 );
663
664 // or in contents from all the other entities
665 num = SV_AreaEntities( p, p, touch, MAX_GENTITIES );
666
667 for ( i=0 ; i<num ; i++ ) {
668 if ( touch[i] == passEntityNum ) {
669 continue;
670 }
671 hit = SV_GentityNum( touch[i] );
672 // might intersect, so do an exact clip
673 clipHandle = SV_ClipHandleForEntity( hit );
674 angles = hit->r.currentAngles;
675 if ( !hit->r.bmodel ) {
676 angles = vec3_origin; // boxes don't rotate
677 }
678
679 c2 = CM_TransformedPointContents (p, clipHandle, hit->r.currentOrigin, angles);
680
681 contents |= c2;
682 }
683
684 return contents;
685 }
686
687