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