]> icculus.org git repositories - divverent/darkplaces.git/blob - world.c
hush some texture loading warnings in dedicated servers (thanks to Biomass for report...
[divverent/darkplaces.git] / world.c
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->inopen and trace->inwater, but bullets don't
29
30 */
31
32 cvar_t sv_debugmove = {CVAR_NOTIFY, "sv_debugmove", "0"};
33 cvar_t sv_areagrid_mingridsize = {CVAR_NOTIFY, "sv_areagrid_mingridsize", "64"};
34
35 void SV_AreaStats_f(void);
36
37 void SV_World_Init(void)
38 {
39         Cvar_RegisterVariable(&sv_debugmove);
40         Cvar_RegisterVariable(&sv_areagrid_mingridsize);
41         Cmd_AddCommand("sv_areastats", SV_AreaStats_f);
42         Collision_Init();
43 }
44
45 //============================================================================
46
47 // ClearLink is used for new headnodes
48 static void ClearLink (link_t *l)
49 {
50         l->entitynumber = 0;
51         l->prev = l->next = l;
52 }
53
54 static void RemoveLink (link_t *l)
55 {
56         l->next->prev = l->prev;
57         l->prev->next = l->next;
58 }
59
60 static void InsertLinkBefore (link_t *l, link_t *before, int entitynumber)
61 {
62         l->entitynumber = entitynumber;
63         l->next = before;
64         l->prev = before->prev;
65         l->prev->next = l;
66         l->next->prev = l;
67 }
68
69
70 /*
71 ===============================================================================
72
73 ENTITY AREA CHECKING
74
75 ===============================================================================
76 */
77
78 int sv_areagrid_stats_calls = 0;
79 int sv_areagrid_stats_nodechecks = 0;
80 int sv_areagrid_stats_entitychecks = 0;
81
82 void SV_AreaStats_f(void)
83 {
84         Con_Printf("areagrid check stats: %d calls %d nodes (%f per call) %d entities (%f per call)\n", sv_areagrid_stats_calls, sv_areagrid_stats_nodechecks, (double) sv_areagrid_stats_nodechecks / (double) sv_areagrid_stats_calls, sv_areagrid_stats_entitychecks, (double) sv_areagrid_stats_entitychecks / (double) sv_areagrid_stats_calls);
85         sv_areagrid_stats_calls = 0;
86         sv_areagrid_stats_nodechecks = 0;
87         sv_areagrid_stats_entitychecks = 0;
88 }
89
90 typedef struct areagrid_s
91 {
92         link_t edicts;
93 }
94 areagrid_t;
95
96 #define AREA_GRID 512
97 #define AREA_GRIDNODES (AREA_GRID * AREA_GRID)
98
99 static areagrid_t sv_areagrid[AREA_GRIDNODES], sv_areagrid_outside;
100 static vec3_t sv_areagrid_bias, sv_areagrid_scale, sv_areagrid_mins, sv_areagrid_maxs, sv_areagrid_size;
101 static int sv_areagrid_marknumber = 1;
102
103 void SV_CreateAreaGrid (vec3_t mins, vec3_t maxs)
104 {
105         int i;
106         ClearLink (&sv_areagrid_outside.edicts);
107         // choose either the world box size, or a larger box to ensure the grid isn't too fine
108         sv_areagrid_size[0] = max(maxs[0] - mins[0], AREA_GRID * sv_areagrid_mingridsize.value);
109         sv_areagrid_size[1] = max(maxs[1] - mins[1], AREA_GRID * sv_areagrid_mingridsize.value);
110         sv_areagrid_size[2] = max(maxs[2] - mins[2], AREA_GRID * sv_areagrid_mingridsize.value);
111         // figure out the corners of such a box, centered at the center of the world box
112         sv_areagrid_mins[0] = (mins[0] + maxs[0] - sv_areagrid_size[0]) * 0.5f;
113         sv_areagrid_mins[1] = (mins[1] + maxs[1] - sv_areagrid_size[1]) * 0.5f;
114         sv_areagrid_mins[2] = (mins[2] + maxs[2] - sv_areagrid_size[2]) * 0.5f;
115         sv_areagrid_maxs[0] = (mins[0] + maxs[0] + sv_areagrid_size[0]) * 0.5f;
116         sv_areagrid_maxs[1] = (mins[1] + maxs[1] + sv_areagrid_size[1]) * 0.5f;
117         sv_areagrid_maxs[2] = (mins[2] + maxs[2] + sv_areagrid_size[2]) * 0.5f;
118         // now calculate the actual useful info from that
119         VectorNegate(sv_areagrid_mins, sv_areagrid_bias);
120         sv_areagrid_scale[0] = AREA_GRID / sv_areagrid_size[0];
121         sv_areagrid_scale[1] = AREA_GRID / sv_areagrid_size[1];
122         sv_areagrid_scale[2] = AREA_GRID / sv_areagrid_size[2];
123         for (i = 0;i < AREA_GRIDNODES;i++)
124         {
125                 ClearLink (&sv_areagrid[i].edicts);
126         }
127         Con_DPrintf("sv_areagrid settings: divisions %ix%ix1 : box %f %f %f : %f %f %f size %f %f %f grid %f %f %f (mingrid %f)\n", AREA_GRID, AREA_GRID, sv_areagrid_mins[0], sv_areagrid_mins[1], sv_areagrid_mins[2], sv_areagrid_maxs[0], sv_areagrid_maxs[1], sv_areagrid_maxs[2], sv_areagrid_size[0], sv_areagrid_size[1], sv_areagrid_size[2], 1.0f / sv_areagrid_scale[0], 1.0f / sv_areagrid_scale[1], 1.0f / sv_areagrid_scale[2], sv_areagrid_mingridsize.value);
128 }
129
130 /*
131 ===============
132 SV_ClearWorld
133
134 ===============
135 */
136 void SV_ClearWorld (void)
137 {
138         Mod_CheckLoaded(sv.worldmodel);
139         SV_CreateAreaGrid(sv.worldmodel->normalmins, sv.worldmodel->normalmaxs);
140 }
141
142
143 /*
144 ===============
145 SV_UnlinkEdict
146
147 ===============
148 */
149 void SV_UnlinkEdict (edict_t *ent)
150 {
151         int i;
152         for (i = 0;i < ENTITYGRIDAREAS;i++)
153         {
154                 if (ent->e->areagrid[i].prev)
155                 {
156                         RemoveLink (&ent->e->areagrid[i]);
157                         ent->e->areagrid[i].prev = ent->e->areagrid[i].next = NULL;
158                 }
159         }
160 }
161
162 int SV_EntitiesInBox(vec3_t mins, vec3_t maxs, int maxlist, edict_t **list)
163 {
164         int numlist;
165         areagrid_t *grid;
166         link_t *l;
167         edict_t *ent;
168         int igrid[3], igridmins[3], igridmaxs[3];
169
170         sv_areagrid_stats_calls++;
171         sv_areagrid_marknumber++;
172         igridmins[0] = (int) ((mins[0] + sv_areagrid_bias[0]) * sv_areagrid_scale[0]);
173         igridmins[1] = (int) ((mins[1] + sv_areagrid_bias[1]) * sv_areagrid_scale[1]);
174         //igridmins[2] = (int) ((mins[2] + sv_areagrid_bias[2]) * sv_areagrid_scale[2]);
175         igridmaxs[0] = (int) ((maxs[0] + sv_areagrid_bias[0]) * sv_areagrid_scale[0]) + 1;
176         igridmaxs[1] = (int) ((maxs[1] + sv_areagrid_bias[1]) * sv_areagrid_scale[1]) + 1;
177         //igridmaxs[2] = (int) ((maxs[2] + sv_areagrid_bias[2]) * sv_areagrid_scale[2]) + 1;
178         igridmins[0] = max(0, igridmins[0]);
179         igridmins[1] = max(0, igridmins[1]);
180         //igridmins[2] = max(0, igridmins[2]);
181         igridmaxs[0] = min(AREA_GRID, igridmaxs[0]);
182         igridmaxs[1] = min(AREA_GRID, igridmaxs[1]);
183         //igridmaxs[2] = min(AREA_GRID, igridmaxs[2]);
184
185         numlist = 0;
186         // add entities not linked into areagrid because they are too big or
187         // outside the grid bounds
188         if (sv_areagrid_outside.edicts.next != &sv_areagrid_outside.edicts)
189         {
190                 for (l = sv_areagrid_outside.edicts.next;l != &sv_areagrid_outside.edicts;l = l->next)
191                 {
192                         ent = EDICT_NUM_UNSIGNED(l->entitynumber);
193                         if (ent->e->areagridmarknumber != sv_areagrid_marknumber)
194                         {
195                                 ent->e->areagridmarknumber = sv_areagrid_marknumber;
196                                 if (!ent->e->free && BoxesOverlap(mins, maxs, ent->v->absmin, ent->v->absmax))
197                                 {
198                                         if (numlist < maxlist)
199                                                 list[numlist] = ent;
200                                         numlist++;
201                                 }
202                                 sv_areagrid_stats_entitychecks++;
203                         }
204                 }
205         }
206         // add grid linked entities
207         for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
208         {
209                 grid = sv_areagrid + igrid[1] * AREA_GRID + igridmins[0];
210                 for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++)
211                 {
212                         if (grid->edicts.next != &grid->edicts)
213                         {
214                                 for (l = grid->edicts.next;l != &grid->edicts;l = l->next)
215                                 {
216                                         ent = EDICT_NUM_UNSIGNED(l->entitynumber);
217                                         if (ent->e->areagridmarknumber != sv_areagrid_marknumber)
218                                         {
219                                                 ent->e->areagridmarknumber = sv_areagrid_marknumber;
220                                                 if (!ent->e->free && BoxesOverlap(mins, maxs, ent->v->absmin, ent->v->absmax))
221                                                 {
222                                                         if (numlist < maxlist)
223                                                                 list[numlist] = ent;
224                                                         numlist++;
225                                                 }
226                                         }
227                                         sv_areagrid_stats_entitychecks++;
228                                 }
229                         }
230                 }
231         }
232         return numlist;
233 }
234
235 void SV_TouchAreaGrid(edict_t *ent)
236 {
237         int i, numtouchedicts, old_self, old_other;
238         edict_t *touch, *touchedicts[MAX_EDICTS];
239
240         // build a list of edicts to touch, because the link loop can be corrupted
241         // by SV_IncreaseEdicts called during touch functions
242         numtouchedicts = SV_EntitiesInBox(ent->v->absmin, ent->v->absmax, MAX_EDICTS, touchedicts);
243         if (numtouchedicts > MAX_EDICTS)
244         {
245                 // this never happens
246                 Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
247                 numtouchedicts = MAX_EDICTS;
248         }
249
250         old_self = pr_global_struct->self;
251         old_other = pr_global_struct->other;
252         for (i = 0;i < numtouchedicts;i++)
253         {
254                 touch = touchedicts[i];
255                 if (touch != ent && (int)touch->v->solid == SOLID_TRIGGER && touch->v->touch)
256                 {
257                         pr_global_struct->self = EDICT_TO_PROG(touch);
258                         pr_global_struct->other = EDICT_TO_PROG(ent);
259                         pr_global_struct->time = sv.time;
260                         PR_ExecuteProgram (touch->v->touch, "QC function self.touch is missing");
261                 }
262         }
263         pr_global_struct->self = old_self;
264         pr_global_struct->other = old_other;
265 }
266
267 void SV_LinkEdict_AreaGrid(edict_t *ent)
268 {
269         areagrid_t *grid;
270         int igrid[3], igridmins[3], igridmaxs[3], gridnum, entitynumber = NUM_FOR_EDICT(ent);
271
272         if (entitynumber <= 0 || entitynumber >= sv.max_edicts || EDICT_NUM(entitynumber) != ent)
273                 Host_Error("SV_LinkEdict_AreaGrid: invalid edict %p (sv.edicts is %p, edict compared to sv.edicts is %i)\n", ent, sv.edicts, entitynumber);
274
275         igridmins[0] = (int) ((ent->v->absmin[0] + sv_areagrid_bias[0]) * sv_areagrid_scale[0]);
276         igridmins[1] = (int) ((ent->v->absmin[1] + sv_areagrid_bias[1]) * sv_areagrid_scale[1]);
277         //igridmins[2] = (int) ((ent->v->absmin[2] + sv_areagrid_bias[2]) * sv_areagrid_scale[2]);
278         igridmaxs[0] = (int) ((ent->v->absmax[0] + sv_areagrid_bias[0]) * sv_areagrid_scale[0]) + 1;
279         igridmaxs[1] = (int) ((ent->v->absmax[1] + sv_areagrid_bias[1]) * sv_areagrid_scale[1]) + 1;
280         //igridmaxs[2] = (int) ((ent->v->absmax[2] + sv_areagrid_bias[2]) * sv_areagrid_scale[2]) + 1;
281         if (igridmins[0] < 0 || igridmaxs[0] > AREA_GRID || igridmins[1] < 0 || igridmaxs[1] > AREA_GRID || ((igridmaxs[0] - igridmins[0]) * (igridmaxs[1] - igridmins[1])) > ENTITYGRIDAREAS)
282         {
283                 // wow, something outside the grid, store it as such
284                 InsertLinkBefore (&ent->e->areagrid[0], &sv_areagrid_outside.edicts, entitynumber);
285                 return;
286         }
287
288         gridnum = 0;
289         for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
290         {
291                 grid = sv_areagrid + igrid[1] * AREA_GRID + igridmins[0];
292                 for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++, gridnum++)
293                         InsertLinkBefore (&ent->e->areagrid[gridnum], &grid->edicts, entitynumber);
294         }
295 }
296
297 /*
298 ===============
299 SV_LinkEdict
300
301 ===============
302 */
303 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
304 {
305         model_t *model;
306
307         if (ent->e->areagrid[0].prev)
308                 SV_UnlinkEdict (ent);   // unlink from old position
309
310         if (ent == sv.edicts)
311                 return;         // don't add the world
312
313         if (ent->e->free)
314                 return;
315
316 // set the abs box
317
318         if (ent->v->solid == SOLID_BSP)
319         {
320                 int modelindex = ent->v->modelindex;
321                 if (modelindex < 0 || modelindex > MAX_MODELS)
322                 {
323                         Con_Printf("edict %i: SOLID_BSP with invalid modelindex!\n", NUM_FOR_EDICT(ent));
324                         modelindex = 0;
325                 }
326                 model = sv.models[modelindex];
327                 if (model != NULL)
328                 {
329                         Mod_CheckLoaded(model);
330                         if (!model->TraceBox)
331                                 Con_Printf("edict %i: SOLID_BSP with non-collidable model\n", NUM_FOR_EDICT(ent));
332
333                         if (ent->v->angles[0] || ent->v->angles[2] || ent->v->avelocity[0] || ent->v->avelocity[2])
334                         {
335                                 VectorAdd(ent->v->origin, model->rotatedmins, ent->v->absmin);
336                                 VectorAdd(ent->v->origin, model->rotatedmaxs, ent->v->absmax);
337                         }
338                         else if (ent->v->angles[1] || ent->v->avelocity[1])
339                         {
340                                 VectorAdd(ent->v->origin, model->yawmins, ent->v->absmin);
341                                 VectorAdd(ent->v->origin, model->yawmaxs, ent->v->absmax);
342                         }
343                         else
344                         {
345                                 VectorAdd(ent->v->origin, model->normalmins, ent->v->absmin);
346                                 VectorAdd(ent->v->origin, model->normalmaxs, ent->v->absmax);
347                         }
348                 }
349                 else
350                 {
351                         // SOLID_BSP with no model is valid, mainly because some QC setup code does so temporarily
352                         VectorAdd(ent->v->origin, ent->v->mins, ent->v->absmin);
353                         VectorAdd(ent->v->origin, ent->v->maxs, ent->v->absmax);
354                 }
355         }
356         else
357         {
358                 VectorAdd(ent->v->origin, ent->v->mins, ent->v->absmin);
359                 VectorAdd(ent->v->origin, ent->v->maxs, ent->v->absmax);
360         }
361
362 //
363 // to make items easier to pick up and allow them to be grabbed off
364 // of shelves, the abs sizes are expanded
365 //
366         if ((int)ent->v->flags & FL_ITEM)
367         {
368                 ent->v->absmin[0] -= 15;
369                 ent->v->absmin[1] -= 15;
370                 ent->v->absmin[2] -= 1;
371                 ent->v->absmax[0] += 15;
372                 ent->v->absmax[1] += 15;
373                 ent->v->absmax[2] += 1;
374         }
375         else
376         {
377                 // because movement is clipped an epsilon away from an actual edge,
378                 // we must fully check even when bounding boxes don't quite touch
379                 ent->v->absmin[0] -= 1;
380                 ent->v->absmin[1] -= 1;
381                 ent->v->absmin[2] -= 1;
382                 ent->v->absmax[0] += 1;
383                 ent->v->absmax[1] += 1;
384                 ent->v->absmax[2] += 1;
385         }
386
387         if (ent->v->solid == SOLID_NOT)
388                 return;
389
390         SV_LinkEdict_AreaGrid(ent);
391
392 // if touch_triggers, touch all entities at this node and descend for more
393         if (touch_triggers)
394                 SV_TouchAreaGrid(ent);
395 }
396
397
398
399 /*
400 ===============================================================================
401
402 POINT TESTING IN HULLS
403
404 ===============================================================================
405 */
406
407 /*
408 ============
409 SV_TestEntityPosition
410
411 This could be a lot more efficient...
412 ============
413 */
414 int SV_TestEntityPosition (edict_t *ent)
415 {
416         return SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, ent->v->origin, MOVE_NORMAL, ent).startsolid;
417 }
418
419
420 /*
421 ===============================================================================
422
423 LINE TESTING IN HULLS
424
425 ===============================================================================
426 */
427
428 /*
429 ==================
430 SV_ClipMoveToEntity
431
432 Handles selection or creation of a clipping hull, and offseting (and
433 eventually rotation) of the end points
434 ==================
435 */
436 trace_t SV_ClipMoveToEntity(edict_t *ent, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int movetype, int hitsupercontents)
437 {
438         trace_t trace;
439         model_t *model = NULL;
440         matrix4x4_t matrix, imatrix;
441         float tempnormal[3], starttransformed[3], endtransformed[3];
442         float starttransformedmins[3], starttransformedmaxs[3], endtransformedmins[3], endtransformedmaxs[3];
443
444         memset(&trace, 0, sizeof(trace));
445         trace.fraction = trace.realfraction = 1;
446         VectorCopy(end, trace.endpos);
447
448         if ((int) ent->v->solid == SOLID_BSP || movetype == MOVE_HITMODEL)
449         {
450                 unsigned int modelindex = ent->v->modelindex;
451                 // if the modelindex is 0, it shouldn't be SOLID_BSP!
452                 if (modelindex == 0)
453                 {
454                         Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP with no model\n", NUM_FOR_EDICT(ent));
455                         return trace;
456                 }
457                 if (modelindex >= MAX_MODELS)
458                 {
459                         Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP with invalid modelindex\n", NUM_FOR_EDICT(ent));
460                         return trace;
461                 }
462                 model = sv.models[modelindex];
463                 if (modelindex != 0 && model == NULL)
464                 {
465                         Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP with invalid modelindex\n", NUM_FOR_EDICT(ent));
466                         return trace;
467                 }
468
469                 Mod_CheckLoaded(model);
470                 if ((int) ent->v->solid == SOLID_BSP)
471                 {
472                         if (!model->TraceBox)
473                         {
474                                 Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP with a non-collidable model\n", NUM_FOR_EDICT(ent));
475                                 return trace;
476                         }
477                         //if (ent->v->movetype != MOVETYPE_PUSH)
478                         //{
479                         //      Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP without MOVETYPE_PUSH\n", NUM_FOR_EDICT(ent));
480                         //      return trace;
481                         //}
482                 }
483                 Matrix4x4_CreateFromQuakeEntity(&matrix, ent->v->origin[0], ent->v->origin[1], ent->v->origin[2], ent->v->angles[0], ent->v->angles[1], ent->v->angles[2], 1);
484         }
485         else
486                 Matrix4x4_CreateTranslate(&matrix, ent->v->origin[0], ent->v->origin[1], ent->v->origin[2]);
487
488         Matrix4x4_Invert_Simple(&imatrix, &matrix);
489         Matrix4x4_Transform(&imatrix, start, starttransformed);
490         Matrix4x4_Transform(&imatrix, end, endtransformed);
491 #if COLLISIONPARANOID >= 3
492         Con_Printf("trans(%f %f %f -> %f %f %f, %f %f %f -> %f %f %f)", start[0], start[1], start[2], starttransformed[0], starttransformed[1], starttransformed[2], end[0], end[1], end[2], endtransformed[0], endtransformed[1], endtransformed[2]);
493 #endif
494
495         if (model && model->TraceBox)
496         {
497                 int frame;
498                 frame = (int)ent->v->frame;
499                 frame = bound(0, frame, (model->numframes - 1));
500                 VectorAdd(starttransformed, maxs, starttransformedmaxs);
501                 VectorAdd(endtransformed, maxs, endtransformedmaxs);
502                 VectorAdd(starttransformed, mins, starttransformedmins);
503                 VectorAdd(endtransformed, mins, endtransformedmins);
504                 model->TraceBox(model, frame, &trace, starttransformedmins, starttransformedmaxs, endtransformedmins, endtransformedmaxs, hitsupercontents);
505         }
506         else
507                 Collision_ClipTrace_Box(&trace, ent->v->mins, ent->v->maxs, starttransformed, mins, maxs, endtransformed, hitsupercontents, SUPERCONTENTS_SOLID);
508         trace.fraction = bound(0, trace.fraction, 1);
509         trace.realfraction = bound(0, trace.realfraction, 1);
510
511         if (trace.fraction < 1)
512         {
513                 VectorLerp(start, trace.fraction, end, trace.endpos);
514                 VectorCopy(trace.plane.normal, tempnormal);
515                 Matrix4x4_Transform3x3(&matrix, tempnormal, trace.plane.normal);
516                 // FIXME: should recalc trace.plane.dist
517         }
518         else
519                 VectorCopy(end, trace.endpos);
520
521         return trace;
522 }
523
524 //===========================================================================
525
526 /*
527 ==================
528 SV_Move
529 ==================
530 */
531 #if COLLISIONPARANOID >= 1
532 trace_t SV_Move_(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, edict_t *passedict)
533 #else
534 trace_t SV_Move(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, edict_t *passedict)
535 #endif
536 {
537         vec3_t hullmins, hullmaxs;
538         int i;
539         int hitsupercontentsmask;
540         int passedictprog;
541         qboolean pointtrace;
542         edict_t *traceowner, *touch;
543         trace_t trace;
544         // bounding box of entire move area
545         vec3_t clipboxmins, clipboxmaxs;
546         // size of the moving object
547         vec3_t clipmins, clipmaxs;
548         // size when clipping against monsters
549         vec3_t clipmins2, clipmaxs2;
550         // start and end origin of move
551         vec3_t clipstart, clipend;
552         // trace results
553         trace_t cliptrace;
554         int numtouchedicts;
555         edict_t *touchedicts[MAX_EDICTS];
556
557         VectorCopy(start, clipstart);
558         VectorCopy(end, clipend);
559         VectorCopy(mins, clipmins);
560         VectorCopy(maxs, clipmaxs);
561         VectorCopy(mins, clipmins2);
562         VectorCopy(maxs, clipmaxs2);
563 #if COLLISIONPARANOID >= 3
564         Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
565 #endif
566
567         hitsupercontentsmask = SUPERCONTENTS_SOLID;
568         if (passedict)
569         {
570                 if (passedict->v->solid == SOLID_SLIDEBOX)
571                         hitsupercontentsmask |= SUPERCONTENTS_PLAYERCLIP;
572                 if ((int)passedict->v->flags & FL_MONSTER)
573                         hitsupercontentsmask |= SUPERCONTENTS_MONSTERCLIP;
574         }
575
576         // clip to world
577         cliptrace = SV_ClipMoveToEntity(sv.edicts, clipstart, clipmins, clipmaxs, clipend, type, hitsupercontentsmask);
578         if (cliptrace.startsolid || cliptrace.fraction < 1)
579                 cliptrace.ent = sv.edicts;
580         if (type == MOVE_WORLDONLY)
581                 return cliptrace;
582
583         if (type == MOVE_MISSILE)
584         {
585                 // LordHavoc: modified this, was = -15, now -= 15
586                 for (i = 0;i < 3;i++)
587                 {
588                         clipmins2[i] -= 15;
589                         clipmaxs2[i] += 15;
590                 }
591         }
592
593         // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
594         if (sv.worldmodel && sv.worldmodel->brush.RoundUpToHullSize)
595                 sv.worldmodel->brush.RoundUpToHullSize(sv.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
596         else
597         {
598                 VectorCopy(clipmins, hullmins);
599                 VectorCopy(clipmaxs, hullmaxs);
600         }
601
602         // create the bounding box of the entire move
603         for (i = 0;i < 3;i++)
604         {
605                 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
606                 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
607         }
608
609         // debug override to test against everything
610         if (sv_debugmove.integer)
611         {
612                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
613                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
614         }
615
616         // if the passedict is world, make it NULL (to avoid two checks each time)
617         if (passedict == sv.edicts)
618                 passedict = NULL;
619         // precalculate prog value for passedict for comparisons
620         passedictprog = EDICT_TO_PROG(passedict);
621         // figure out whether this is a point trace for comparisons
622         pointtrace = VectorCompare(clipmins, clipmaxs);
623         // precalculate passedict's owner edict pointer for comparisons
624         traceowner = passedict ? PROG_TO_EDICT(passedict->v->owner) : 0;
625
626         // clip to enttiies
627         numtouchedicts = SV_EntitiesInBox(clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
628         if (numtouchedicts > MAX_EDICTS)
629         {
630                 // this never happens
631                 Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
632                 numtouchedicts = MAX_EDICTS;
633         }
634         for (i = 0;i < numtouchedicts;i++)
635         {
636                 touch = touchedicts[i];
637
638                 if (touch->v->solid < SOLID_BBOX)
639                         continue;
640                 if (type == MOVE_NOMONSTERS && touch->v->solid != SOLID_BSP)
641                         continue;
642
643                 if (passedict)
644                 {
645                         // don't clip against self
646                         if (passedict == touch)
647                                 continue;
648                         // don't clip owned entities against owner
649                         if (traceowner == touch)
650                                 continue;
651                         // don't clip owner against owned entities
652                         if (passedictprog == touch->v->owner)
653                                 continue;
654                         // don't clip points against points (they can't collide)
655                         if (pointtrace && VectorCompare(touch->v->mins, touch->v->maxs) && (type != MOVE_MISSILE || !((int)touch->v->flags & FL_MONSTER)))
656                                 continue;
657                         // don't clip corpse against character
658                         if (passedict->v->solid == SOLID_CORPSE && (touch->v->solid == SOLID_SLIDEBOX || touch->v->solid == SOLID_CORPSE))
659                                 continue;
660                         // don't clip character against corpse
661                         if (passedict->v->solid == SOLID_SLIDEBOX && touch->v->solid == SOLID_CORPSE)
662                                 continue;
663                 }
664
665                 // might interact, so do an exact clip
666                 if ((int)touch->v->flags & FL_MONSTER)
667                         trace = SV_ClipMoveToEntity(touch, clipstart, clipmins2, clipmaxs2, clipend, type, hitsupercontentsmask);
668                 else
669                         trace = SV_ClipMoveToEntity(touch, clipstart, clipmins, clipmaxs, clipend, type, hitsupercontentsmask);
670                 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
671                 if (trace.allsolid)
672                         cliptrace.allsolid = true;
673                 if (trace.startsolid)
674                 {
675                         cliptrace.startsolid = true;
676                         if (cliptrace.realfraction == 1)
677                                 cliptrace.ent = touch;
678                 }
679                 // don't set this except on the world, because it can easily confuse
680                 // monsters underwater if there's a bmodel involved in the trace
681                 // (inopen && inwater is how they check water visibility)
682                 //if (trace.inopen)
683                 //      cliptrace.inopen = true;
684                 if (trace.inwater)
685                         cliptrace.inwater = true;
686                 if (trace.realfraction < cliptrace.realfraction)
687                 {
688                         cliptrace.fraction = trace.fraction;
689                         cliptrace.realfraction = trace.realfraction;
690                         VectorCopy(trace.endpos, cliptrace.endpos);
691                         cliptrace.plane = trace.plane;
692                         cliptrace.ent = touch;
693                 }
694                 cliptrace.startsupercontents |= trace.startsupercontents;
695         }
696
697         return cliptrace;
698 }
699
700 #if COLLISIONPARANOID >= 1
701 trace_t SV_Move(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, edict_t *passedict)
702 {
703         int endstuck;
704         trace_t trace;
705         vec3_t temp;
706         trace = SV_Move_(start, mins, maxs, end, type, passedict);
707         if (passedict)
708         {
709                 VectorCopy(trace.endpos, temp);
710                 endstuck = SV_Move_(temp, mins, maxs, temp, type, passedict).startsolid;
711 #if COLLISIONPARANOID < 3
712                 if (trace.startsolid || endstuck)
713 #endif
714                         Con_Printf("%s{e%i:%f %f %f:%f %f %f:%f:%f %f %f%s%s}\n", (trace.startsolid || endstuck) ? "\002" : "", passedict ? passedict - sv.edicts : -1, passedict->v->origin[0], passedict->v->origin[1], passedict->v->origin[2], end[0] - passedict->v->origin[0], end[1] - passedict->v->origin[1], end[2] - passedict->v->origin[2], trace.fraction, trace.endpos[0] - passedict->v->origin[0], trace.endpos[1] - passedict->v->origin[1], trace.endpos[2] - passedict->v->origin[2], trace.startsolid ? " startstuck" : "", endstuck ? " endstuck" : "");
715         }
716         return trace;
717 }
718 #endif
719
720 int SV_PointSuperContents(const vec3_t point)
721 {
722         return SV_Move(point, vec3_origin, vec3_origin, point, sv_gameplayfix_swiminbmodels.integer ? MOVE_NOMONSTERS : MOVE_WORLDONLY, NULL).startsupercontents;
723 }
724
725 int SV_PointQ1Contents(const vec3_t point)
726 {
727         return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, SV_PointSuperContents(point));
728 }
729
730