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