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