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