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