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