new cvar sys_useclockgettime (default 0) that makes DP use clock_gettime as time...
[divverent/darkplaces.git] / sv_phys.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 // sv_phys.c
21
22 #include "quakedef.h"
23
24 /*
25
26
27 pushmove objects do not obey gravity, and do not interact with each other or trigger fields, but block normal movement and push normal objects when they move.
28
29 onground is set for toss objects when they come to a complete rest.  it is set for steping or walking objects
30
31 doors, plats, etc are SOLID_BSP, and MOVETYPE_PUSH
32 bonus items are SOLID_TRIGGER touch, and MOVETYPE_TOSS
33 corpses are SOLID_NOT and MOVETYPE_TOSS
34 crates are SOLID_BBOX and MOVETYPE_TOSS
35 walking monsters are SOLID_SLIDEBOX and MOVETYPE_STEP
36 flying/floating monsters are SOLID_SLIDEBOX and MOVETYPE_FLY
37
38 solid_edge items only clip against bsp models.
39
40 */
41
42 #define MOVE_EPSILON    0.01
43
44 void SV_Physics_Toss (prvm_edict_t *ent);
45
46 /*
47 ===============================================================================
48
49 LINE TESTING IN HULLS
50
51 ===============================================================================
52 */
53
54 int SV_GenericHitSuperContentsMask(const prvm_edict_t *passedict)
55 {
56         prvm_eval_t *val;
57         if (passedict)
58         {
59                 val = PRVM_EDICTFIELDVALUE(passedict, prog->fieldoffsets.dphitcontentsmask);
60                 if (val && val->_float)
61                         return (int)val->_float;
62                 else if (passedict->fields.server->solid == SOLID_SLIDEBOX)
63                 {
64                         if ((int)passedict->fields.server->flags & FL_MONSTER)
65                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_MONSTERCLIP;
66                         else
67                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_PLAYERCLIP;
68                 }
69                 else if (passedict->fields.server->solid == SOLID_CORPSE)
70                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
71                 else if (passedict->fields.server->solid == SOLID_TRIGGER)
72                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
73                 else
74                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
75         }
76         else
77                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
78 }
79
80 /*
81 ==================
82 SV_Move
83 ==================
84 */
85 #if COLLISIONPARANOID >= 1
86 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, int hitsupercontentsmask)
87 #else
88 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, int hitsupercontentsmask)
89 #endif
90 {
91         vec3_t hullmins, hullmaxs;
92         int i, bodysupercontents;
93         int passedictprog;
94         qboolean pointtrace;
95         prvm_edict_t *traceowner, *touch;
96         trace_t trace;
97         // bounding box of entire move area
98         vec3_t clipboxmins, clipboxmaxs;
99         // size of the moving object
100         vec3_t clipmins, clipmaxs;
101         // size when clipping against monsters
102         vec3_t clipmins2, clipmaxs2;
103         // start and end origin of move
104         vec3_t clipstart, clipend;
105         // trace results
106         trace_t cliptrace;
107         // matrices to transform into/out of other entity's space
108         matrix4x4_t matrix, imatrix;
109         // model of other entity
110         dp_model_t *model;
111         // list of entities to test for collisions
112         int numtouchedicts;
113         prvm_edict_t *touchedicts[MAX_EDICTS];
114
115         VectorCopy(start, clipstart);
116         VectorCopy(end, clipend);
117         VectorCopy(mins, clipmins);
118         VectorCopy(maxs, clipmaxs);
119         VectorCopy(mins, clipmins2);
120         VectorCopy(maxs, clipmaxs2);
121 #if COLLISIONPARANOID >= 3
122         Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
123 #endif
124
125         // clip to world
126         Collision_ClipToWorld(&cliptrace, sv.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
127         cliptrace.bmodelstartsolid = cliptrace.startsolid;
128         if (cliptrace.startsolid || cliptrace.fraction < 1)
129                 cliptrace.ent = prog->edicts;
130         if (type == MOVE_WORLDONLY)
131                 return cliptrace;
132
133         if (type == MOVE_MISSILE)
134         {
135                 // LordHavoc: modified this, was = -15, now -= 15
136                 for (i = 0;i < 3;i++)
137                 {
138                         clipmins2[i] -= 15;
139                         clipmaxs2[i] += 15;
140                 }
141         }
142
143         // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
144         if (sv.worldmodel && sv.worldmodel->brush.RoundUpToHullSize)
145                 sv.worldmodel->brush.RoundUpToHullSize(sv.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
146         else
147         {
148                 VectorCopy(clipmins, hullmins);
149                 VectorCopy(clipmaxs, hullmaxs);
150         }
151
152         // create the bounding box of the entire move
153         for (i = 0;i < 3;i++)
154         {
155                 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
156                 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
157         }
158
159         // debug override to test against everything
160         if (sv_debugmove.integer)
161         {
162                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
163                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
164         }
165
166         // if the passedict is world, make it NULL (to avoid two checks each time)
167         if (passedict == prog->edicts)
168                 passedict = NULL;
169         // precalculate prog value for passedict for comparisons
170         passedictprog = PRVM_EDICT_TO_PROG(passedict);
171         // figure out whether this is a point trace for comparisons
172         pointtrace = VectorCompare(clipmins, clipmaxs);
173         // precalculate passedict's owner edict pointer for comparisons
174         traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.server->owner) : 0;
175
176         // clip to entities
177         // because this uses World_EntitiestoBox, we know all entity boxes overlap
178         // the clip region, so we can skip culling checks in the loop below
179         numtouchedicts = World_EntitiesInBox(&sv.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
180         if (numtouchedicts > MAX_EDICTS)
181         {
182                 // this never happens
183                 Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
184                 numtouchedicts = MAX_EDICTS;
185         }
186         for (i = 0;i < numtouchedicts;i++)
187         {
188                 touch = touchedicts[i];
189
190                 if (touch->fields.server->solid < SOLID_BBOX)
191                         continue;
192                 if (type == MOVE_NOMONSTERS && touch->fields.server->solid != SOLID_BSP)
193                         continue;
194
195                 if (passedict)
196                 {
197                         // don't clip against self
198                         if (passedict == touch)
199                                 continue;
200                         // don't clip owned entities against owner
201                         if (traceowner == touch)
202                                 continue;
203                         // don't clip owner against owned entities
204                         if (passedictprog == touch->fields.server->owner)
205                                 continue;
206                         // don't clip points against points (they can't collide)
207                         if (pointtrace && VectorCompare(touch->fields.server->mins, touch->fields.server->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.server->flags & FL_MONSTER)))
208                                 continue;
209                 }
210
211                 bodysupercontents = touch->fields.server->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
212
213                 // might interact, so do an exact clip
214                 model = NULL;
215                 if ((int) touch->fields.server->solid == SOLID_BSP || type == MOVE_HITMODEL)
216                 {
217                         unsigned int modelindex = (unsigned int)touch->fields.server->modelindex;
218                         // if the modelindex is 0, it shouldn't be SOLID_BSP!
219                         if (modelindex > 0 && modelindex < MAX_MODELS)
220                                 model = sv.models[(int)touch->fields.server->modelindex];
221                 }
222                 if (model)
223                         Matrix4x4_CreateFromQuakeEntity(&matrix, touch->fields.server->origin[0], touch->fields.server->origin[1], touch->fields.server->origin[2], touch->fields.server->angles[0], touch->fields.server->angles[1], touch->fields.server->angles[2], 1);
224                 else
225                         Matrix4x4_CreateTranslate(&matrix, touch->fields.server->origin[0], touch->fields.server->origin[1], touch->fields.server->origin[2]);
226                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
227                 if ((int)touch->fields.server->flags & FL_MONSTER)
228                         Collision_ClipToGenericEntity(&trace, model, touch->fields.server->frame, touch->fields.server->mins, touch->fields.server->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask);
229                 else
230                         Collision_ClipToGenericEntity(&trace, model, touch->fields.server->frame, touch->fields.server->mins, touch->fields.server->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
231
232                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.server->solid == SOLID_BSP);
233         }
234
235         return cliptrace;
236 }
237
238 #if COLLISIONPARANOID >= 1
239 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, int hitsupercontentsmask)
240 {
241         int endstuck;
242         trace_t trace;
243         vec3_t temp;
244         trace = SV_Move_(start, mins, maxs, end, type, passedict, hitsupercontentsmask);
245         if (passedict)
246         {
247                 VectorCopy(trace.endpos, temp);
248                 endstuck = SV_Move_(temp, mins, maxs, temp, type, passedict, hitsupercontentsmask).startsolid;
249 #if COLLISIONPARANOID < 3
250                 if (trace.startsolid || endstuck)
251 #endif
252                         Con_Printf("%s{e%i:%f %f %f:%f %f %f:%f:%f %f %f%s%s}\n", (trace.startsolid || endstuck) ? "^3" : "", passedict ? (int)(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" : "");
253         }
254         return trace;
255 }
256 #endif
257
258 int SV_PointSuperContents(const vec3_t point)
259 {
260         int supercontents = 0;
261         int i;
262         prvm_edict_t *touch;
263         vec3_t transformed;
264         // matrices to transform into/out of other entity's space
265         matrix4x4_t matrix, imatrix;
266         // model of other entity
267         dp_model_t *model;
268         unsigned int modelindex;
269         int frame;
270         // list of entities to test for collisions
271         int numtouchedicts;
272         prvm_edict_t *touchedicts[MAX_EDICTS];
273
274         // get world supercontents at this point
275         if (sv.worldmodel && sv.worldmodel->PointSuperContents)
276                 supercontents = sv.worldmodel->PointSuperContents(sv.worldmodel, 0, point);
277
278         // if sv_gameplayfix_swiminbmodels is off we're done
279         if (!sv_gameplayfix_swiminbmodels.integer)
280                 return supercontents;
281
282         // get list of entities at this point
283         numtouchedicts = World_EntitiesInBox(&sv.world, point, point, MAX_EDICTS, touchedicts);
284         if (numtouchedicts > MAX_EDICTS)
285         {
286                 // this never happens
287                 Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
288                 numtouchedicts = MAX_EDICTS;
289         }
290         for (i = 0;i < numtouchedicts;i++)
291         {
292                 touch = touchedicts[i];
293
294                 // we only care about SOLID_BSP for pointcontents
295                 if (touch->fields.server->solid != SOLID_BSP)
296                         continue;
297
298                 // might interact, so do an exact clip
299                 modelindex = (unsigned int)touch->fields.server->modelindex;
300                 if (modelindex >= MAX_MODELS)
301                         continue;
302                 model = sv.models[(int)touch->fields.server->modelindex];
303                 if (!model || !model->PointSuperContents)
304                         continue;
305                 Matrix4x4_CreateFromQuakeEntity(&matrix, touch->fields.server->origin[0], touch->fields.server->origin[1], touch->fields.server->origin[2], touch->fields.server->angles[0], touch->fields.server->angles[1], touch->fields.server->angles[2], 1);
306                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
307                 Matrix4x4_Transform(&imatrix, point, transformed);
308                 frame = (int)touch->fields.server->frame;
309                 supercontents |= model->PointSuperContents(model, bound(0, frame, (model->numframes - 1)), transformed);
310         }
311
312         return supercontents;
313 }
314
315 /*
316 ===============================================================================
317
318 Linking entities into the world culling system
319
320 ===============================================================================
321 */
322
323 void SV_LinkEdict_TouchAreaGrid(prvm_edict_t *ent)
324 {
325         int i, numtouchedicts, old_self, old_other;
326         prvm_edict_t *touch, *touchedicts[MAX_EDICTS];
327
328         // build a list of edicts to touch, because the link loop can be corrupted
329         // by IncreaseEdicts called during touch functions
330         numtouchedicts = World_EntitiesInBox(&sv.world, ent->priv.server->areamins, ent->priv.server->areamaxs, MAX_EDICTS, touchedicts);
331         if (numtouchedicts > MAX_EDICTS)
332         {
333                 // this never happens
334                 Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
335                 numtouchedicts = MAX_EDICTS;
336         }
337
338         old_self = prog->globals.server->self;
339         old_other = prog->globals.server->other;
340         for (i = 0;i < numtouchedicts;i++)
341         {
342                 touch = touchedicts[i];
343                 if (touch != ent && (int)touch->fields.server->solid == SOLID_TRIGGER && touch->fields.server->touch)
344                 {
345                         prvm_eval_t *val;
346                         prog->globals.server->self = PRVM_EDICT_TO_PROG(touch);
347                         prog->globals.server->other = PRVM_EDICT_TO_PROG(ent);
348                         prog->globals.server->time = sv.time;
349                         prog->globals.server->trace_allsolid = false;
350                         prog->globals.server->trace_startsolid = false;
351                         prog->globals.server->trace_fraction = 1;
352                         prog->globals.server->trace_inwater = false;
353                         prog->globals.server->trace_inopen = true;
354                         VectorCopy (touch->fields.server->origin, prog->globals.server->trace_endpos);
355                         VectorSet (prog->globals.server->trace_plane_normal, 0, 0, 1);
356                         prog->globals.server->trace_plane_dist = 0;
357                         prog->globals.server->trace_ent = PRVM_EDICT_TO_PROG(ent);
358                         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
359                                 val->_float = 0;
360                         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
361                                 val->_float = 0;
362                         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
363                                 val->_float = 0;
364                         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
365                                 val->string = 0;
366                         PRVM_ExecuteProgram (touch->fields.server->touch, "QC function self.touch is missing");
367                 }
368         }
369         prog->globals.server->self = old_self;
370         prog->globals.server->other = old_other;
371 }
372
373 /*
374 ===============
375 SV_LinkEdict
376
377 ===============
378 */
379 void SV_LinkEdict (prvm_edict_t *ent, qboolean touch_triggers)
380 {
381         dp_model_t *model;
382         vec3_t mins, maxs;
383
384         if (ent == prog->edicts)
385                 return;         // don't add the world
386
387         if (ent->priv.server->free)
388                 return;
389
390 // set the abs box
391
392         if (ent->fields.server->solid == SOLID_BSP)
393         {
394                 int modelindex = (int)ent->fields.server->modelindex;
395                 if (modelindex < 0 || modelindex > MAX_MODELS)
396                 {
397                         Con_Printf("edict %i: SOLID_BSP with invalid modelindex!\n", PRVM_NUM_FOR_EDICT(ent));
398                         modelindex = 0;
399                 }
400                 model = sv.models[modelindex];
401                 if (model != NULL)
402                 {
403                         if (!model->TraceBox && developer.integer >= 1)
404                                 Con_Printf("edict %i: SOLID_BSP with non-collidable model\n", PRVM_NUM_FOR_EDICT(ent));
405
406                         if (ent->fields.server->angles[0] || ent->fields.server->angles[2] || ent->fields.server->avelocity[0] || ent->fields.server->avelocity[2])
407                         {
408                                 VectorAdd(ent->fields.server->origin, model->rotatedmins, mins);
409                                 VectorAdd(ent->fields.server->origin, model->rotatedmaxs, maxs);
410                         }
411                         else if (ent->fields.server->angles[1] || ent->fields.server->avelocity[1])
412                         {
413                                 VectorAdd(ent->fields.server->origin, model->yawmins, mins);
414                                 VectorAdd(ent->fields.server->origin, model->yawmaxs, maxs);
415                         }
416                         else
417                         {
418                                 VectorAdd(ent->fields.server->origin, model->normalmins, mins);
419                                 VectorAdd(ent->fields.server->origin, model->normalmaxs, maxs);
420                         }
421                 }
422                 else
423                 {
424                         // SOLID_BSP with no model is valid, mainly because some QC setup code does so temporarily
425                         VectorAdd(ent->fields.server->origin, ent->fields.server->mins, mins);
426                         VectorAdd(ent->fields.server->origin, ent->fields.server->maxs, maxs);
427                 }
428         }
429         else
430         {
431                 VectorAdd(ent->fields.server->origin, ent->fields.server->mins, mins);
432                 VectorAdd(ent->fields.server->origin, ent->fields.server->maxs, maxs);
433         }
434
435 //
436 // to make items easier to pick up and allow them to be grabbed off
437 // of shelves, the abs sizes are expanded
438 //
439         if ((int)ent->fields.server->flags & FL_ITEM)
440         {
441                 mins[0] -= 15;
442                 mins[1] -= 15;
443                 mins[2] -= 1;
444                 maxs[0] += 15;
445                 maxs[1] += 15;
446                 maxs[2] += 1;
447         }
448         else
449         {
450                 // because movement is clipped an epsilon away from an actual edge,
451                 // we must fully check even when bounding boxes don't quite touch
452                 mins[0] -= 1;
453                 mins[1] -= 1;
454                 mins[2] -= 1;
455                 maxs[0] += 1;
456                 maxs[1] += 1;
457                 maxs[2] += 1;
458         }
459
460         VectorCopy(mins, ent->fields.server->absmin);
461         VectorCopy(maxs, ent->fields.server->absmax);
462
463         World_LinkEdict(&sv.world, ent, mins, maxs);
464
465         // if touch_triggers, call touch on all entities overlapping this box
466         if (touch_triggers && ent->fields.server->solid != SOLID_NOT)
467                 SV_LinkEdict_TouchAreaGrid(ent);
468 }
469
470 /*
471 ===============================================================================
472
473 Utility functions
474
475 ===============================================================================
476 */
477
478 /*
479 ============
480 SV_TestEntityPosition
481
482 returns true if the entity is in solid currently
483 ============
484 */
485 static int SV_TestEntityPosition (prvm_edict_t *ent, vec3_t offset)
486 {
487         vec3_t org;
488         trace_t trace;
489         VectorAdd(ent->fields.server->origin, offset, org);
490         trace = SV_Move (org, ent->fields.server->mins, ent->fields.server->maxs, ent->fields.server->origin, MOVE_NOMONSTERS, ent, SUPERCONTENTS_SOLID);
491         if (trace.startsupercontents & SUPERCONTENTS_SOLID)
492                 return true;
493         else
494         {
495                 if (sv.worldmodel->brushq1.numclipnodes && !VectorCompare(ent->fields.server->mins, ent->fields.server->maxs))
496                 {
497                         // q1bsp/hlbsp use hulls and if the entity does not exactly match
498                         // a hull size it is incorrectly tested, so this code tries to
499                         // 'fix' it slightly...
500                         // FIXME: this breaks entities larger than the hull size
501                         int i;
502                         vec3_t v, m1, m2, s;
503                         VectorAdd(org, ent->fields.server->mins, m1);
504                         VectorAdd(org, ent->fields.server->maxs, m2);
505                         VectorSubtract(m2, m1, s);
506 #define EPSILON (1.0f / 32.0f)
507                         if (s[0] >= EPSILON*2) {m1[0] += EPSILON;m2[0] -= EPSILON;}
508                         if (s[1] >= EPSILON*2) {m1[1] += EPSILON;m2[1] -= EPSILON;}
509                         if (s[2] >= EPSILON*2) {m1[2] += EPSILON;m2[2] -= EPSILON;}
510                         for (i = 0;i < 8;i++)
511                         {
512                                 v[0] = (i & 1) ? m2[0] : m1[0];
513                                 v[1] = (i & 2) ? m2[1] : m1[1];
514                                 v[2] = (i & 4) ? m2[2] : m1[2];
515                                 if (SV_PointSuperContents(v) & SUPERCONTENTS_SOLID)
516                                         return true;
517                         }
518                 }
519         }
520         // if the trace found a better position for the entity, move it there
521         if (VectorDistance2(trace.endpos, ent->fields.server->origin) >= 0.0001)
522                 VectorCopy(trace.endpos, ent->fields.server->origin);
523         return false;
524 }
525
526 /*
527 ================
528 SV_CheckAllEnts
529 ================
530 */
531 void SV_CheckAllEnts (void)
532 {
533         int e;
534         prvm_edict_t *check;
535
536         // see if any solid entities are inside the final position
537         check = PRVM_NEXT_EDICT(prog->edicts);
538         for (e = 1;e < prog->num_edicts;e++, check = PRVM_NEXT_EDICT(check))
539         {
540                 if (check->priv.server->free)
541                         continue;
542                 if (check->fields.server->movetype == MOVETYPE_PUSH
543                  || check->fields.server->movetype == MOVETYPE_NONE
544                  || check->fields.server->movetype == MOVETYPE_FOLLOW
545                  || check->fields.server->movetype == MOVETYPE_NOCLIP)
546                         continue;
547
548                 if (SV_TestEntityPosition (check, vec3_origin))
549                         Con_Print("entity in invalid position\n");
550         }
551 }
552
553 // DRESK - Support for Entity Contents Transition Event
554 /*
555 ================
556 SV_CheckContentsTransition
557
558 returns true if entity had a valid contentstransition function call
559 ================
560 */
561 int SV_CheckContentsTransition(prvm_edict_t *ent, const int nContents)
562 {
563         int bValidFunctionCall;
564         prvm_eval_t *contentstransition;
565
566         // Default Valid Function Call to False
567         bValidFunctionCall = false;
568
569         if(ent->fields.server->watertype != nContents)
570         { // Changed Contents
571                 // Acquire Contents Transition Function from QC
572                 contentstransition = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.contentstransition);
573
574                 if(contentstransition->function)
575                 { // Valid Function; Execute
576                         // Assign Valid Function
577                         bValidFunctionCall = true;
578                         // Prepare Parameters (Original Contents, New Contents)
579                                 // Original Contents
580                                 PRVM_G_FLOAT(OFS_PARM0) = ent->fields.server->watertype;
581                                 // New Contents
582                                 PRVM_G_FLOAT(OFS_PARM1) = nContents;
583                                 // Assign Self
584                                 prog->globals.server->self = PRVM_EDICT_TO_PROG(ent);
585                         // Execute VM Function
586                         PRVM_ExecuteProgram(contentstransition->function, "contentstransition: NULL function");
587                 }
588         }
589
590         // Return if Function Call was Valid
591         return bValidFunctionCall;
592 }
593
594
595 /*
596 ================
597 SV_CheckVelocity
598 ================
599 */
600 void SV_CheckVelocity (prvm_edict_t *ent)
601 {
602         int i;
603         float wishspeed;
604
605 //
606 // bound velocity
607 //
608         for (i=0 ; i<3 ; i++)
609         {
610                 if (IS_NAN(ent->fields.server->velocity[i]))
611                 {
612                         Con_Printf("Got a NaN velocity on entity #%i (%s)\n", PRVM_NUM_FOR_EDICT(ent), PRVM_GetString(ent->fields.server->classname));
613                         ent->fields.server->velocity[i] = 0;
614                 }
615                 if (IS_NAN(ent->fields.server->origin[i]))
616                 {
617                         Con_Printf("Got a NaN origin on entity #%i (%s)\n", PRVM_NUM_FOR_EDICT(ent), PRVM_GetString(ent->fields.server->classname));
618                         ent->fields.server->origin[i] = 0;
619                 }
620         }
621
622         // LordHavoc: max velocity fix, inspired by Maddes's source fixes, but this is faster
623         wishspeed = DotProduct(ent->fields.server->velocity, ent->fields.server->velocity);
624         if (wishspeed > sv_maxvelocity.value * sv_maxvelocity.value)
625         {
626                 wishspeed = sv_maxvelocity.value / sqrt(wishspeed);
627                 ent->fields.server->velocity[0] *= wishspeed;
628                 ent->fields.server->velocity[1] *= wishspeed;
629                 ent->fields.server->velocity[2] *= wishspeed;
630         }
631 }
632
633 /*
634 =============
635 SV_RunThink
636
637 Runs thinking code if time.  There is some play in the exact time the think
638 function will be called, because it is called before any movement is done
639 in a frame.  Not used for pushmove objects, because they must be exact.
640 Returns false if the entity removed itself.
641 =============
642 */
643 qboolean SV_RunThink (prvm_edict_t *ent)
644 {
645         int iterations;
646
647         // don't let things stay in the past.
648         // it is possible to start that way by a trigger with a local time.
649         if (ent->fields.server->nextthink <= 0 || ent->fields.server->nextthink > sv.time + sv.frametime)
650                 return true;
651
652         for (iterations = 0;iterations < 128  && !ent->priv.server->free;iterations++)
653         {
654                 prog->globals.server->time = max(sv.time, ent->fields.server->nextthink);
655                 ent->fields.server->nextthink = 0;
656                 prog->globals.server->self = PRVM_EDICT_TO_PROG(ent);
657                 prog->globals.server->other = PRVM_EDICT_TO_PROG(prog->edicts);
658                 PRVM_ExecuteProgram (ent->fields.server->think, "QC function self.think is missing");
659                 // mods often set nextthink to time to cause a think every frame,
660                 // we don't want to loop in that case, so exit if the new nextthink is
661                 // <= the time the qc was told, also exit if it is past the end of the
662                 // frame
663                 if (ent->fields.server->nextthink <= prog->globals.server->time || ent->fields.server->nextthink > sv.time + sv.frametime || !sv_gameplayfix_multiplethinksperframe.integer)
664                         break;
665         }
666         return !ent->priv.server->free;
667 }
668
669 /*
670 ==================
671 SV_Impact
672
673 Two entities have touched, so run their touch functions
674 ==================
675 */
676 extern void VM_SetTraceGlobals(const trace_t *trace);
677 extern sizebuf_t vm_tempstringsbuf;
678 void SV_Impact (prvm_edict_t *e1, trace_t *trace)
679 {
680         int restorevm_tempstringsbuf_cursize;
681         int old_self, old_other;
682         prvm_edict_t *e2 = (prvm_edict_t *)trace->ent;
683         prvm_eval_t *val;
684
685         old_self = prog->globals.server->self;
686         old_other = prog->globals.server->other;
687         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
688
689         VM_SetTraceGlobals(trace);
690
691         prog->globals.server->time = sv.time;
692         if (!e1->priv.server->free && !e2->priv.server->free && e1->fields.server->touch && e1->fields.server->solid != SOLID_NOT)
693         {
694                 prog->globals.server->self = PRVM_EDICT_TO_PROG(e1);
695                 prog->globals.server->other = PRVM_EDICT_TO_PROG(e2);
696                 PRVM_ExecuteProgram (e1->fields.server->touch, "QC function self.touch is missing");
697         }
698
699         if (!e1->priv.server->free && !e2->priv.server->free && e2->fields.server->touch && e2->fields.server->solid != SOLID_NOT)
700         {
701                 prog->globals.server->self = PRVM_EDICT_TO_PROG(e2);
702                 prog->globals.server->other = PRVM_EDICT_TO_PROG(e1);
703                 VectorCopy(e2->fields.server->origin, prog->globals.server->trace_endpos);
704                 VectorNegate(trace->plane.normal, prog->globals.server->trace_plane_normal);
705                 prog->globals.server->trace_plane_dist = -trace->plane.dist;
706                 prog->globals.server->trace_ent = PRVM_EDICT_TO_PROG(e1);
707                 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
708                         val->_float = 0;
709                 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
710                         val->_float = 0;
711                 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
712                         val->_float = 0;
713                 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
714                         val->string = 0;
715                 PRVM_ExecuteProgram (e2->fields.server->touch, "QC function self.touch is missing");
716         }
717
718         prog->globals.server->self = old_self;
719         prog->globals.server->other = old_other;
720         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
721 }
722
723
724 /*
725 ==================
726 ClipVelocity
727
728 Slide off of the impacting object
729 returns the blocked flags (1 = floor, 2 = step / wall)
730 ==================
731 */
732 #define STOP_EPSILON 0.1
733 void ClipVelocity (vec3_t in, vec3_t normal, vec3_t out, float overbounce)
734 {
735         int i;
736         float backoff;
737
738         backoff = -DotProduct (in, normal) * overbounce;
739         VectorMA(in, backoff, normal, out);
740
741         for (i = 0;i < 3;i++)
742                 if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
743                         out[i] = 0;
744 }
745
746
747 /*
748 ============
749 SV_FlyMove
750
751 The basic solid body movement clip that slides along multiple planes
752 Returns the clipflags if the velocity was modified (hit something solid)
753 1 = floor
754 2 = wall / step
755 4 = dead stop
756 If stepnormal is not NULL, the plane normal of any vertical wall hit will be stored
757 ============
758 */
759 // LordHavoc: increased from 5 to 32
760 #define MAX_CLIP_PLANES 32
761 int SV_FlyMove (prvm_edict_t *ent, float time, float *stepnormal, int hitsupercontentsmask)
762 {
763         int blocked, bumpcount;
764         int i, j, impact, numplanes;
765         float d, time_left;
766         vec3_t dir, end, planes[MAX_CLIP_PLANES], primal_velocity, original_velocity, new_velocity;
767         trace_t trace;
768         if (time <= 0)
769                 return 0;
770         blocked = 0;
771         VectorCopy(ent->fields.server->velocity, original_velocity);
772         VectorCopy(ent->fields.server->velocity, primal_velocity);
773         numplanes = 0;
774         time_left = time;
775         for (bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
776         {
777                 if (!ent->fields.server->velocity[0] && !ent->fields.server->velocity[1] && !ent->fields.server->velocity[2])
778                         break;
779
780                 VectorMA(ent->fields.server->origin, time_left, ent->fields.server->velocity, end);
781                 trace = SV_Move(ent->fields.server->origin, ent->fields.server->mins, ent->fields.server->maxs, end, MOVE_NORMAL, ent, hitsupercontentsmask);
782 #if 0
783                 //if (trace.fraction < 0.002)
784                 {
785 #if 1
786                         vec3_t start;
787                         trace_t testtrace;
788                         VectorCopy(ent->fields.server->origin, start);
789                         start[2] += 3;//0.03125;
790                         VectorMA(ent->fields.server->origin, time_left, ent->fields.server->velocity, end);
791                         end[2] += 3;//0.03125;
792                         testtrace = SV_Move(start, ent->fields.server->mins, ent->fields.server->maxs, end, MOVE_NORMAL, ent, hitsupercontentsmask);
793                         if (trace.fraction < testtrace.fraction && !testtrace.startsolid && (testtrace.fraction == 1 || DotProduct(trace.plane.normal, ent->fields.server->velocity) < DotProduct(testtrace.plane.normal, ent->fields.server->velocity)))
794                         {
795                                 Con_Printf("got further (new %f > old %f)\n", testtrace.fraction, trace.fraction);
796                                 trace = testtrace;
797                         }
798 #endif
799 #if 0
800                         //j = -1;
801                         for (i = 0;i < numplanes;i++)
802                         {
803                                 VectorCopy(ent->fields.server->origin, start);
804                                 VectorMA(ent->fields.server->origin, time_left, ent->fields.server->velocity, end);
805                                 VectorMA(start, 3, planes[i], start);
806                                 VectorMA(end, 3, planes[i], end);
807                                 testtrace = SV_Move(start, ent->fields.server->mins, ent->fields.server->maxs, end, MOVE_NORMAL, ent, hitsupercontentsmask);
808                                 if (trace.fraction < testtrace.fraction)
809                                 {
810                                         trace = testtrace;
811                                         VectorCopy(start, ent->fields.server->origin);
812                                         //j = i;
813                                 }
814                         }
815                         //if (j >= 0)
816                         //      VectorAdd(ent->fields.server->origin, planes[j], start);
817 #endif
818                 }
819 #endif
820
821 #if 0
822                 Con_Printf("entity %i bump %i: velocity %f %f %f trace %f", ent - prog->edicts, bumpcount, ent->fields.server->velocity[0], ent->fields.server->velocity[1], ent->fields.server->velocity[2], trace.fraction);
823                 if (trace.fraction < 1)
824                         Con_Printf(" : %f %f %f", trace.plane.normal[0], trace.plane.normal[1], trace.plane.normal[2]);
825                 Con_Print("\n");
826 #endif
827
828 #if 0
829                 if (trace.bmodelstartsolid)
830                 {
831                         // LordHavoc: note: this code is what makes entities stick in place
832                         // if embedded in world only (you can walk through other objects if
833                         // stuck)
834                         // entity is trapped in another solid
835                         VectorClear(ent->fields.server->velocity);
836                         return 3;
837                 }
838 #endif
839
840                 // break if it moved the entire distance
841                 if (trace.fraction == 1)
842                 {
843                         VectorCopy(trace.endpos, ent->fields.server->origin);
844                         break;
845                 }
846
847                 if (!trace.ent)
848                 {
849                         Con_Printf ("SV_FlyMove: !trace.ent");
850                         trace.ent = prog->edicts;
851                 }
852
853                 impact = !((int) ent->fields.server->flags & FL_ONGROUND) || ent->fields.server->groundentity != PRVM_EDICT_TO_PROG(trace.ent);
854
855                 if (trace.plane.normal[2])
856                 {
857                         if (trace.plane.normal[2] > 0.7)
858                         {
859                                 // floor
860                                 blocked |= 1;
861                                 ent->fields.server->flags = (int)ent->fields.server->flags | FL_ONGROUND;
862                                 ent->fields.server->groundentity = PRVM_EDICT_TO_PROG(trace.ent);
863                         }
864                 }
865                 else
866                 {
867                         // step
868                         blocked |= 2;
869                         // save the trace for player extrafriction
870                         if (stepnormal)
871                                 VectorCopy(trace.plane.normal, stepnormal);
872                 }
873
874                 if (trace.fraction >= 0.001)
875                 {
876                         // actually covered some distance
877                         VectorCopy(trace.endpos, ent->fields.server->origin);
878                         VectorCopy(ent->fields.server->velocity, original_velocity);
879                         numplanes = 0;
880                 }
881
882                 // run the impact function
883                 if (impact)
884                 {
885                         SV_Impact(ent, &trace);
886
887                         // break if removed by the impact function
888                         if (ent->priv.server->free)
889                                 break;
890                 }
891
892                 time_left *= 1 - trace.fraction;
893
894                 // clipped to another plane
895                 if (numplanes >= MAX_CLIP_PLANES)
896                 {
897                         // this shouldn't really happen
898                         VectorClear(ent->fields.server->velocity);
899                         blocked = 3;
900                         break;
901                 }
902
903                 /*
904                 for (i = 0;i < numplanes;i++)
905                         if (DotProduct(trace.plane.normal, planes[i]) > 0.99)
906                                 break;
907                 if (i < numplanes)
908                 {
909                         VectorAdd(ent->fields.server->velocity, trace.plane.normal, ent->fields.server->velocity);
910                         continue;
911                 }
912                 */
913
914                 VectorCopy(trace.plane.normal, planes[numplanes]);
915                 numplanes++;
916
917                 if (sv_newflymove.integer)
918                         ClipVelocity(ent->fields.server->velocity, trace.plane.normal, ent->fields.server->velocity, 1);
919                 else
920                 {
921                         // modify original_velocity so it parallels all of the clip planes
922                         for (i = 0;i < numplanes;i++)
923                         {
924                                 ClipVelocity(original_velocity, planes[i], new_velocity, 1);
925                                 for (j = 0;j < numplanes;j++)
926                                 {
927                                         if (j != i)
928                                         {
929                                                 // not ok
930                                                 if (DotProduct(new_velocity, planes[j]) < 0)
931                                                         break;
932                                         }
933                                 }
934                                 if (j == numplanes)
935                                         break;
936                         }
937
938                         if (i != numplanes)
939                         {
940                                 // go along this plane
941                                 VectorCopy(new_velocity, ent->fields.server->velocity);
942                         }
943                         else
944                         {
945                                 // go along the crease
946                                 if (numplanes != 2)
947                                 {
948                                         VectorClear(ent->fields.server->velocity);
949                                         blocked = 7;
950                                         break;
951                                 }
952                                 CrossProduct(planes[0], planes[1], dir);
953                                 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
954                                 VectorNormalize(dir);
955                                 d = DotProduct(dir, ent->fields.server->velocity);
956                                 VectorScale(dir, d, ent->fields.server->velocity);
957                         }
958                 }
959
960                 // if current velocity is against the original velocity,
961                 // stop dead to avoid tiny occilations in sloping corners
962                 if (DotProduct(ent->fields.server->velocity, primal_velocity) <= 0)
963                 {
964                         VectorClear(ent->fields.server->velocity);
965                         break;
966                 }
967         }
968
969         //Con_Printf("entity %i final: blocked %i velocity %f %f %f\n", ent - prog->edicts, blocked, ent->fields.server->velocity[0], ent->fields.server->velocity[1], ent->fields.server->velocity[2]);
970
971         /*
972         if ((blocked & 1) == 0 && bumpcount > 1)
973         {
974                 // LordHavoc: fix the 'fall to your death in a wedge corner' glitch
975                 // flag ONGROUND if there's ground under it
976                 trace = SV_Move(ent->fields.server->origin, ent->fields.server->mins, ent->fields.server->maxs, end, MOVE_NORMAL, ent, hitsupercontentsmask);
977         }
978         */
979
980         // LordHavoc: this came from QW and allows you to get out of water more easily
981         if (sv_gameplayfix_easierwaterjump.integer && ((int)ent->fields.server->flags & FL_WATERJUMP))
982                 VectorCopy(primal_velocity, ent->fields.server->velocity);
983         return blocked;
984 }
985
986 /*
987 ============
988 SV_AddGravity
989
990 ============
991 */
992 void SV_AddGravity (prvm_edict_t *ent)
993 {
994         float ent_gravity;
995         prvm_eval_t *val;
996
997         val = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.gravity);
998         if (val!=0 && val->_float)
999                 ent_gravity = val->_float;
1000         else
1001                 ent_gravity = 1.0;
1002         ent->fields.server->velocity[2] -= ent_gravity * sv_gravity.value * sv.frametime;
1003 }
1004
1005
1006 /*
1007 ===============================================================================
1008
1009 PUSHMOVE
1010
1011 ===============================================================================
1012 */
1013
1014 /*
1015 ============
1016 SV_PushEntity
1017
1018 Does not change the entities velocity at all
1019 ============
1020 */
1021 static trace_t SV_PushEntity (prvm_edict_t *ent, vec3_t push, qboolean failonbmodelstartsolid)
1022 {
1023         int type;
1024         trace_t trace;
1025         vec3_t end;
1026
1027         VectorAdd (ent->fields.server->origin, push, end);
1028
1029         if (ent->fields.server->movetype == MOVETYPE_FLYMISSILE)
1030                 type = MOVE_MISSILE;
1031         else if (ent->fields.server->solid == SOLID_TRIGGER || ent->fields.server->solid == SOLID_NOT)
1032                 type = MOVE_NOMONSTERS; // only clip against bmodels
1033         else
1034                 type = MOVE_NORMAL;
1035
1036         trace = SV_Move (ent->fields.server->origin, ent->fields.server->mins, ent->fields.server->maxs, end, type, ent, SV_GenericHitSuperContentsMask(ent));
1037         if (trace.bmodelstartsolid && failonbmodelstartsolid)
1038                 return trace;
1039
1040         VectorCopy (trace.endpos, ent->fields.server->origin);
1041         SV_LinkEdict (ent, true);
1042
1043         if (ent->fields.server->solid >= SOLID_TRIGGER && trace.ent && (!((int)ent->fields.server->flags & FL_ONGROUND) || ent->fields.server->groundentity != PRVM_EDICT_TO_PROG(trace.ent)))
1044                 SV_Impact (ent, &trace);
1045         return trace;
1046 }
1047
1048
1049 /*
1050 ============
1051 SV_PushMove
1052
1053 ============
1054 */
1055 void SV_PushMove (prvm_edict_t *pusher, float movetime)
1056 {
1057         int i, e, index;
1058         int checkcontents;
1059         qboolean rotated;
1060         float savesolid, movetime2, pushltime;
1061         vec3_t mins, maxs, move, move1, moveangle, pushorig, pushang, a, forward, left, up, org;
1062         int num_moved;
1063         int numcheckentities;
1064         static prvm_edict_t *checkentities[MAX_EDICTS];
1065         dp_model_t *pushermodel;
1066         trace_t trace;
1067         matrix4x4_t pusherfinalmatrix, pusherfinalimatrix;
1068         unsigned short moved_edicts[MAX_EDICTS];
1069
1070         if (!pusher->fields.server->velocity[0] && !pusher->fields.server->velocity[1] && !pusher->fields.server->velocity[2] && !pusher->fields.server->avelocity[0] && !pusher->fields.server->avelocity[1] && !pusher->fields.server->avelocity[2])
1071         {
1072                 pusher->fields.server->ltime += movetime;
1073                 return;
1074         }
1075
1076         switch ((int) pusher->fields.server->solid)
1077         {
1078         // LordHavoc: valid pusher types
1079         case SOLID_BSP:
1080         case SOLID_BBOX:
1081         case SOLID_SLIDEBOX:
1082         case SOLID_CORPSE: // LordHavoc: this would be weird...
1083                 break;
1084         // LordHavoc: no collisions
1085         case SOLID_NOT:
1086         case SOLID_TRIGGER:
1087                 VectorMA (pusher->fields.server->origin, movetime, pusher->fields.server->velocity, pusher->fields.server->origin);
1088                 VectorMA (pusher->fields.server->angles, movetime, pusher->fields.server->avelocity, pusher->fields.server->angles);
1089                 pusher->fields.server->angles[0] -= 360.0 * floor(pusher->fields.server->angles[0] * (1.0 / 360.0));
1090                 pusher->fields.server->angles[1] -= 360.0 * floor(pusher->fields.server->angles[1] * (1.0 / 360.0));
1091                 pusher->fields.server->angles[2] -= 360.0 * floor(pusher->fields.server->angles[2] * (1.0 / 360.0));
1092                 pusher->fields.server->ltime += movetime;
1093                 SV_LinkEdict (pusher, false);
1094                 return;
1095         default:
1096                 Con_Printf("SV_PushMove: entity #%i, unrecognized solid type %f\n", PRVM_NUM_FOR_EDICT(pusher), pusher->fields.server->solid);
1097                 return;
1098         }
1099         index = (int) pusher->fields.server->modelindex;
1100         if (index < 1 || index >= MAX_MODELS)
1101         {
1102                 Con_Printf("SV_PushMove: entity #%i has an invalid modelindex %f\n", PRVM_NUM_FOR_EDICT(pusher), pusher->fields.server->modelindex);
1103                 return;
1104         }
1105         pushermodel = sv.models[index];
1106
1107         rotated = VectorLength2(pusher->fields.server->angles) + VectorLength2(pusher->fields.server->avelocity) > 0;
1108
1109         movetime2 = movetime;
1110         VectorScale(pusher->fields.server->velocity, movetime2, move1);
1111         VectorScale(pusher->fields.server->avelocity, movetime2, moveangle);
1112         if (moveangle[0] || moveangle[2])
1113         {
1114                 for (i = 0;i < 3;i++)
1115                 {
1116                         if (move1[i] > 0)
1117                         {
1118                                 mins[i] = pushermodel->rotatedmins[i] + pusher->fields.server->origin[i] - 1;
1119                                 maxs[i] = pushermodel->rotatedmaxs[i] + move1[i] + pusher->fields.server->origin[i] + 1;
1120                         }
1121                         else
1122                         {
1123                                 mins[i] = pushermodel->rotatedmins[i] + move1[i] + pusher->fields.server->origin[i] - 1;
1124                                 maxs[i] = pushermodel->rotatedmaxs[i] + pusher->fields.server->origin[i] + 1;
1125                         }
1126                 }
1127         }
1128         else if (moveangle[1])
1129         {
1130                 for (i = 0;i < 3;i++)
1131                 {
1132                         if (move1[i] > 0)
1133                         {
1134                                 mins[i] = pushermodel->yawmins[i] + pusher->fields.server->origin[i] - 1;
1135                                 maxs[i] = pushermodel->yawmaxs[i] + move1[i] + pusher->fields.server->origin[i] + 1;
1136                         }
1137                         else
1138                         {
1139                                 mins[i] = pushermodel->yawmins[i] + move1[i] + pusher->fields.server->origin[i] - 1;
1140                                 maxs[i] = pushermodel->yawmaxs[i] + pusher->fields.server->origin[i] + 1;
1141                         }
1142                 }
1143         }
1144         else
1145         {
1146                 for (i = 0;i < 3;i++)
1147                 {
1148                         if (move1[i] > 0)
1149                         {
1150                                 mins[i] = pushermodel->normalmins[i] + pusher->fields.server->origin[i] - 1;
1151                                 maxs[i] = pushermodel->normalmaxs[i] + move1[i] + pusher->fields.server->origin[i] + 1;
1152                         }
1153                         else
1154                         {
1155                                 mins[i] = pushermodel->normalmins[i] + move1[i] + pusher->fields.server->origin[i] - 1;
1156                                 maxs[i] = pushermodel->normalmaxs[i] + pusher->fields.server->origin[i] + 1;
1157                         }
1158                 }
1159         }
1160
1161         VectorNegate (moveangle, a);
1162         AngleVectorsFLU (a, forward, left, up);
1163
1164         VectorCopy (pusher->fields.server->origin, pushorig);
1165         VectorCopy (pusher->fields.server->angles, pushang);
1166         pushltime = pusher->fields.server->ltime;
1167
1168 // move the pusher to its final position
1169
1170         VectorMA (pusher->fields.server->origin, movetime, pusher->fields.server->velocity, pusher->fields.server->origin);
1171         VectorMA (pusher->fields.server->angles, movetime, pusher->fields.server->avelocity, pusher->fields.server->angles);
1172         pusher->fields.server->ltime += movetime;
1173         SV_LinkEdict (pusher, false);
1174
1175         pushermodel = NULL;
1176         if (pusher->fields.server->modelindex >= 1 && pusher->fields.server->modelindex < MAX_MODELS)
1177                 pushermodel = sv.models[(int)pusher->fields.server->modelindex];
1178         Matrix4x4_CreateFromQuakeEntity(&pusherfinalmatrix, pusher->fields.server->origin[0], pusher->fields.server->origin[1], pusher->fields.server->origin[2], pusher->fields.server->angles[0], pusher->fields.server->angles[1], pusher->fields.server->angles[2], 1);
1179         Matrix4x4_Invert_Simple(&pusherfinalimatrix, &pusherfinalmatrix);
1180
1181         savesolid = pusher->fields.server->solid;
1182
1183 // see if any solid entities are inside the final position
1184         num_moved = 0;
1185
1186         numcheckentities = World_EntitiesInBox(&sv.world, mins, maxs, MAX_EDICTS, checkentities);
1187         for (e = 0;e < numcheckentities;e++)
1188         {
1189                 prvm_edict_t *check = checkentities[e];
1190                 if (check->fields.server->movetype == MOVETYPE_NONE
1191                  || check->fields.server->movetype == MOVETYPE_PUSH
1192                  || check->fields.server->movetype == MOVETYPE_FOLLOW
1193                  || check->fields.server->movetype == MOVETYPE_NOCLIP
1194                  || check->fields.server->movetype == MOVETYPE_FAKEPUSH)
1195                         continue;
1196
1197                 //Con_Printf("%i %s ", PRVM_NUM_FOR_EDICT(check), PRVM_GetString(check->fields.server->classname));
1198
1199                 // tell any MOVETYPE_STEP entity that it may need to check for water transitions
1200                 check->priv.server->waterposition_forceupdate = true;
1201
1202                 checkcontents = SV_GenericHitSuperContentsMask(check);
1203
1204                 // if the entity is standing on the pusher, it will definitely be moved
1205                 // if the entity is not standing on the pusher, but is in the pusher's
1206                 // final position, move it
1207                 if (!((int)check->fields.server->flags & FL_ONGROUND) || PRVM_PROG_TO_EDICT(check->fields.server->groundentity) != pusher)
1208                 {
1209                         Collision_ClipToGenericEntity(&trace, pushermodel, pusher->fields.server->frame, pusher->fields.server->mins, pusher->fields.server->maxs, SUPERCONTENTS_BODY, &pusherfinalmatrix, &pusherfinalimatrix, check->fields.server->origin, check->fields.server->mins, check->fields.server->maxs, check->fields.server->origin, checkcontents);
1210                         //trace = SV_Move(check->fields.server->origin, check->fields.server->mins, check->fields.server->maxs, check->fields.server->origin, MOVE_NOMONSTERS, check, checkcontents);
1211                         if (!trace.startsolid)
1212                         {
1213                                 //Con_Printf("- not in solid\n");
1214                                 continue;
1215                         }
1216                 }
1217
1218                 if (rotated)
1219                 {
1220                         vec3_t org2;
1221                         VectorSubtract (check->fields.server->origin, pusher->fields.server->origin, org);
1222                         org2[0] = DotProduct (org, forward);
1223                         org2[1] = DotProduct (org, left);
1224                         org2[2] = DotProduct (org, up);
1225                         VectorSubtract (org2, org, move);
1226                         VectorAdd (move, move1, move);
1227                 }
1228                 else
1229                         VectorCopy (move1, move);
1230
1231                 //Con_Printf("- pushing %f %f %f\n", move[0], move[1], move[2]);
1232
1233                 VectorCopy (check->fields.server->origin, check->priv.server->moved_from);
1234                 VectorCopy (check->fields.server->angles, check->priv.server->moved_fromangles);
1235                 moved_edicts[num_moved++] = PRVM_NUM_FOR_EDICT(check);
1236
1237                 // try moving the contacted entity
1238                 pusher->fields.server->solid = SOLID_NOT;
1239                 trace = SV_PushEntity (check, move, true);
1240                 // FIXME: turn players specially
1241                 check->fields.server->angles[1] += trace.fraction * moveangle[1];
1242                 pusher->fields.server->solid = savesolid; // was SOLID_BSP
1243                 //Con_Printf("%s:%d frac %f startsolid %d bmodelstartsolid %d allsolid %d\n", __FILE__, __LINE__, trace.fraction, trace.startsolid, trace.bmodelstartsolid, trace.allsolid);
1244
1245                 // this trace.fraction < 1 check causes items to fall off of pushers
1246                 // if they pass under or through a wall
1247                 // the groundentity check causes items to fall off of ledges
1248                 if (check->fields.server->movetype != MOVETYPE_WALK && (trace.fraction < 1 || PRVM_PROG_TO_EDICT(check->fields.server->groundentity) != pusher))
1249                         check->fields.server->flags = (int)check->fields.server->flags & ~FL_ONGROUND;
1250
1251                 // if it is still inside the pusher, block
1252                 Collision_ClipToGenericEntity(&trace, pushermodel, pusher->fields.server->frame, pusher->fields.server->mins, pusher->fields.server->maxs, SUPERCONTENTS_BODY, &pusherfinalmatrix, &pusherfinalimatrix, check->fields.server->origin, check->fields.server->mins, check->fields.server->maxs, check->fields.server->origin, checkcontents);
1253                 if (trace.startsolid)
1254                 {
1255                         // try moving the contacted entity a tiny bit further to account for precision errors
1256                         vec3_t move2;
1257                         pusher->fields.server->solid = SOLID_NOT;
1258                         VectorScale(move, 1.1, move2);
1259                         VectorCopy (check->priv.server->moved_from, check->fields.server->origin);
1260                         VectorCopy (check->priv.server->moved_fromangles, check->fields.server->angles);
1261                         SV_PushEntity (check, move2, true);
1262                         pusher->fields.server->solid = savesolid;
1263                         Collision_ClipToGenericEntity(&trace, pushermodel, pusher->fields.server->frame, pusher->fields.server->mins, pusher->fields.server->maxs, SUPERCONTENTS_BODY, &pusherfinalmatrix, &pusherfinalimatrix, check->fields.server->origin, check->fields.server->mins, check->fields.server->maxs, check->fields.server->origin, checkcontents);
1264                         if (trace.startsolid)
1265                         {
1266                                 // try moving the contacted entity a tiny bit less to account for precision errors
1267                                 pusher->fields.server->solid = SOLID_NOT;
1268                                 VectorScale(move, 0.9, move2);
1269                                 VectorCopy (check->priv.server->moved_from, check->fields.server->origin);
1270                                 VectorCopy (check->priv.server->moved_fromangles, check->fields.server->angles);
1271                                 SV_PushEntity (check, move2, true);
1272                                 pusher->fields.server->solid = savesolid;
1273                                 Collision_ClipToGenericEntity(&trace, pushermodel, pusher->fields.server->frame, pusher->fields.server->mins, pusher->fields.server->maxs, SUPERCONTENTS_BODY, &pusherfinalmatrix, &pusherfinalimatrix, check->fields.server->origin, check->fields.server->mins, check->fields.server->maxs, check->fields.server->origin, checkcontents);
1274                                 if (trace.startsolid)
1275                                 {
1276                                         // still inside pusher, so it's really blocked
1277
1278                                         // fail the move
1279                                         if (check->fields.server->mins[0] == check->fields.server->maxs[0])
1280                                                 continue;
1281                                         if (check->fields.server->solid == SOLID_NOT || check->fields.server->solid == SOLID_TRIGGER)
1282                                         {
1283                                                 // corpse
1284                                                 check->fields.server->mins[0] = check->fields.server->mins[1] = 0;
1285                                                 VectorCopy (check->fields.server->mins, check->fields.server->maxs);
1286                                                 continue;
1287                                         }
1288
1289                                         VectorCopy (pushorig, pusher->fields.server->origin);
1290                                         VectorCopy (pushang, pusher->fields.server->angles);
1291                                         pusher->fields.server->ltime = pushltime;
1292                                         SV_LinkEdict (pusher, false);
1293
1294                                         // move back any entities we already moved
1295                                         for (i = 0;i < num_moved;i++)
1296                                         {
1297                                                 prvm_edict_t *ed = PRVM_EDICT_NUM(moved_edicts[i]);
1298                                                 VectorCopy (ed->priv.server->moved_from, ed->fields.server->origin);
1299                                                 VectorCopy (ed->priv.server->moved_fromangles, ed->fields.server->angles);
1300                                                 SV_LinkEdict (ed, false);
1301                                         }
1302
1303                                         // if the pusher has a "blocked" function, call it, otherwise just stay in place until the obstacle is gone
1304                                         if (pusher->fields.server->blocked)
1305                                         {
1306                                                 prog->globals.server->self = PRVM_EDICT_TO_PROG(pusher);
1307                                                 prog->globals.server->other = PRVM_EDICT_TO_PROG(check);
1308                                                 PRVM_ExecuteProgram (pusher->fields.server->blocked, "QC function self.blocked is missing");
1309                                         }
1310                                         break;
1311                                 }
1312                         }
1313                 }
1314         }
1315         pusher->fields.server->angles[0] -= 360.0 * floor(pusher->fields.server->angles[0] * (1.0 / 360.0));
1316         pusher->fields.server->angles[1] -= 360.0 * floor(pusher->fields.server->angles[1] * (1.0 / 360.0));
1317         pusher->fields.server->angles[2] -= 360.0 * floor(pusher->fields.server->angles[2] * (1.0 / 360.0));
1318 }
1319
1320 /*
1321 ================
1322 SV_Physics_Pusher
1323
1324 ================
1325 */
1326 void SV_Physics_Pusher (prvm_edict_t *ent)
1327 {
1328         float thinktime, oldltime, movetime;
1329
1330         oldltime = ent->fields.server->ltime;
1331
1332         thinktime = ent->fields.server->nextthink;
1333         if (thinktime < ent->fields.server->ltime + sv.frametime)
1334         {
1335                 movetime = thinktime - ent->fields.server->ltime;
1336                 if (movetime < 0)
1337                         movetime = 0;
1338         }
1339         else
1340                 movetime = sv.frametime;
1341
1342         if (movetime)
1343                 // advances ent->fields.server->ltime if not blocked
1344                 SV_PushMove (ent, movetime);
1345
1346         if (thinktime > oldltime && thinktime <= ent->fields.server->ltime)
1347         {
1348                 ent->fields.server->nextthink = 0;
1349                 prog->globals.server->time = sv.time;
1350                 prog->globals.server->self = PRVM_EDICT_TO_PROG(ent);
1351                 prog->globals.server->other = PRVM_EDICT_TO_PROG(prog->edicts);
1352                 PRVM_ExecuteProgram (ent->fields.server->think, "QC function self.think is missing");
1353         }
1354 }
1355
1356
1357 /*
1358 ===============================================================================
1359
1360 CLIENT MOVEMENT
1361
1362 ===============================================================================
1363 */
1364
1365 static float unstickoffsets[] =
1366 {
1367         -1,  0,  0,
1368          1,  0,  0,
1369          0, -1,  0,
1370          0,  1,  0,
1371         -1, -1,  0,
1372          1, -1,  0,
1373         -1,  1,  0,
1374          1,  1,  0,
1375          0,  0,  -1,
1376          0,  0,  1,
1377          0,  0,  -2,
1378          0,  0,  2,
1379          0,  0,  -3,
1380          0,  0,  3,
1381          0,  0,  -4,
1382          0,  0,  4,
1383          0,  0,  -5,
1384          0,  0,  5,
1385          0,  0,  -6,
1386          0,  0,  6,
1387          0,  0,  -7,
1388          0,  0,  7,
1389          0,  0,  -8,
1390          0,  0,  8,
1391          0,  0,  -9,
1392          0,  0,  9,
1393          0,  0,  -10,
1394          0,  0,  10,
1395          0,  0,  -11,
1396          0,  0,  11,
1397          0,  0,  -12,
1398          0,  0,  12,
1399          0,  0,  -13,
1400          0,  0,  13,
1401          0,  0,  -14,
1402          0,  0,  14,
1403          0,  0,  -15,
1404          0,  0,  15,
1405          0,  0,  -16,
1406          0,  0,  16,
1407          0,  0,  -17,
1408          0,  0,  17,
1409 };
1410
1411 /*
1412 =============
1413 SV_CheckStuck
1414
1415 This is a big hack to try and fix the rare case of getting stuck in the world
1416 clipping hull.
1417 =============
1418 */
1419 void SV_CheckStuck (prvm_edict_t *ent)
1420 {
1421         int i;
1422         vec3_t offset;
1423
1424         if (!SV_TestEntityPosition(ent, vec3_origin))
1425         {
1426                 VectorCopy (ent->fields.server->origin, ent->fields.server->oldorigin);
1427                 return;
1428         }
1429
1430         for (i = 0;i < (int)(sizeof(unstickoffsets) / sizeof(unstickoffsets[0]));i += 3)
1431         {
1432                 if (!SV_TestEntityPosition(ent, unstickoffsets + i))
1433                 {
1434                         Con_DPrintf("Unstuck player entity %i (classname \"%s\") with offset %f %f %f.\n", (int)PRVM_EDICT_TO_PROG(ent), PRVM_GetString(ent->fields.server->classname), unstickoffsets[i+0], unstickoffsets[i+1], unstickoffsets[i+2]);
1435                         SV_LinkEdict (ent, true);
1436                         return;
1437                 }
1438         }
1439
1440         VectorSubtract(ent->fields.server->oldorigin, ent->fields.server->origin, offset);
1441         if (!SV_TestEntityPosition(ent, offset))
1442         {
1443                 Con_DPrintf("Unstuck player entity %i (classname \"%s\") by restoring oldorigin.\n", (int)PRVM_EDICT_TO_PROG(ent), PRVM_GetString(ent->fields.server->classname));
1444                 SV_LinkEdict (ent, true);
1445                 return;
1446         }
1447
1448         Con_DPrintf("Stuck player entity %i (classname \"%s\").\n", (int)PRVM_EDICT_TO_PROG(ent), PRVM_GetString(ent->fields.server->classname));
1449 }
1450
1451 qboolean SV_UnstickEntity (prvm_edict_t *ent)
1452 {
1453         int i;
1454
1455         // if not stuck in a bmodel, just return
1456         if (!SV_TestEntityPosition(ent, vec3_origin))
1457                 return true;
1458
1459         for (i = 0;i < (int)(sizeof(unstickoffsets) / sizeof(unstickoffsets[0]));i += 3)
1460         {
1461                 if (!SV_TestEntityPosition(ent, unstickoffsets + i))
1462                 {
1463                         Con_DPrintf("Unstuck entity %i (classname \"%s\") with offset %f %f %f.\n", (int)PRVM_EDICT_TO_PROG(ent), PRVM_GetString(ent->fields.server->classname), unstickoffsets[i+0], unstickoffsets[i+1], unstickoffsets[i+2]);
1464                         SV_LinkEdict (ent, true);
1465                         return true;
1466                 }
1467         }
1468
1469         if (developer.integer >= 100)
1470                 Con_Printf("Stuck entity %i (classname \"%s\").\n", (int)PRVM_EDICT_TO_PROG(ent), PRVM_GetString(ent->fields.server->classname));
1471         return false;
1472 }
1473
1474
1475 /*
1476 =============
1477 SV_CheckWater
1478 =============
1479 */
1480 qboolean SV_CheckWater (prvm_edict_t *ent)
1481 {
1482         int cont;
1483         int nNativeContents;
1484         vec3_t point;
1485
1486         point[0] = ent->fields.server->origin[0];
1487         point[1] = ent->fields.server->origin[1];
1488         point[2] = ent->fields.server->origin[2] + ent->fields.server->mins[2] + 1;
1489
1490         // DRESK - Support for Entity Contents Transition Event
1491         // NOTE: Some logic needed to be slightly re-ordered
1492         // to not affect performance and allow for the feature.
1493
1494         // Acquire Super Contents Prior to Resets
1495         cont = SV_PointSuperContents(point);
1496         // Acquire Native Contents Here
1497         nNativeContents = Mod_Q1BSP_NativeContentsFromSuperContents(NULL, cont);
1498
1499         // DRESK - Support for Entity Contents Transition Event
1500         if(ent->fields.server->watertype)
1501                 // Entity did NOT Spawn; Check
1502                 SV_CheckContentsTransition(ent, nNativeContents);
1503
1504
1505         ent->fields.server->waterlevel = 0;
1506         ent->fields.server->watertype = CONTENTS_EMPTY;
1507         cont = SV_PointSuperContents(point);
1508         if (cont & (SUPERCONTENTS_LIQUIDSMASK))
1509         {
1510                 ent->fields.server->watertype = nNativeContents;
1511                 ent->fields.server->waterlevel = 1;
1512                 point[2] = ent->fields.server->origin[2] + (ent->fields.server->mins[2] + ent->fields.server->maxs[2])*0.5;
1513                 if (SV_PointSuperContents(point) & (SUPERCONTENTS_LIQUIDSMASK))
1514                 {
1515                         ent->fields.server->waterlevel = 2;
1516                         point[2] = ent->fields.server->origin[2] + ent->fields.server->view_ofs[2];
1517                         if (SV_PointSuperContents(point) & (SUPERCONTENTS_LIQUIDSMASK))
1518                                 ent->fields.server->waterlevel = 3;
1519                 }
1520         }
1521
1522         return ent->fields.server->waterlevel > 1;
1523 }
1524
1525 /*
1526 ============
1527 SV_WallFriction
1528
1529 ============
1530 */
1531 void SV_WallFriction (prvm_edict_t *ent, float *stepnormal)
1532 {
1533         float d, i;
1534         vec3_t forward, into, side;
1535
1536         AngleVectors (ent->fields.server->v_angle, forward, NULL, NULL);
1537         if ((d = DotProduct (stepnormal, forward) + 0.5) < 0)
1538         {
1539                 // cut the tangential velocity
1540                 i = DotProduct (stepnormal, ent->fields.server->velocity);
1541                 VectorScale (stepnormal, i, into);
1542                 VectorSubtract (ent->fields.server->velocity, into, side);
1543                 ent->fields.server->velocity[0] = side[0] * (1 + d);
1544                 ent->fields.server->velocity[1] = side[1] * (1 + d);
1545         }
1546 }
1547
1548 #if 0
1549 /*
1550 =====================
1551 SV_TryUnstick
1552
1553 Player has come to a dead stop, possibly due to the problem with limited
1554 float precision at some angle joins in the BSP hull.
1555
1556 Try fixing by pushing one pixel in each direction.
1557
1558 This is a hack, but in the interest of good gameplay...
1559 ======================
1560 */
1561 int SV_TryUnstick (prvm_edict_t *ent, vec3_t oldvel)
1562 {
1563         int i, clip;
1564         vec3_t oldorg, dir;
1565
1566         VectorCopy (ent->fields.server->origin, oldorg);
1567         VectorClear (dir);
1568
1569         for (i=0 ; i<8 ; i++)
1570         {
1571                 // try pushing a little in an axial direction
1572                 switch (i)
1573                 {
1574                         case 0: dir[0] = 2; dir[1] = 0; break;
1575                         case 1: dir[0] = 0; dir[1] = 2; break;
1576                         case 2: dir[0] = -2; dir[1] = 0; break;
1577                         case 3: dir[0] = 0; dir[1] = -2; break;
1578                         case 4: dir[0] = 2; dir[1] = 2; break;
1579                         case 5: dir[0] = -2; dir[1] = 2; break;
1580                         case 6: dir[0] = 2; dir[1] = -2; break;
1581                         case 7: dir[0] = -2; dir[1] = -2; break;
1582                 }
1583
1584                 SV_PushEntity (ent, dir, false);
1585
1586                 // retry the original move
1587                 ent->fields.server->velocity[0] = oldvel[0];
1588                 ent->fields.server->velocity[1] = oldvel[1];
1589                 ent->fields.server->velocity[2] = 0;
1590                 clip = SV_FlyMove (ent, 0.1, NULL, SV_GenericHitSuperContentsMask(ent));
1591
1592                 if (fabs(oldorg[1] - ent->fields.server->origin[1]) > 4
1593                  || fabs(oldorg[0] - ent->fields.server->origin[0]) > 4)
1594                 {
1595                         Con_DPrint("TryUnstick - success.\n");
1596                         return clip;
1597                 }
1598
1599                 // go back to the original pos and try again
1600                 VectorCopy (oldorg, ent->fields.server->origin);
1601         }
1602
1603         // still not moving
1604         VectorClear (ent->fields.server->velocity);
1605         Con_DPrint("TryUnstick - failure.\n");
1606         return 7;
1607 }
1608 #endif
1609
1610 /*
1611 =====================
1612 SV_WalkMove
1613
1614 Only used by players
1615 ======================
1616 */
1617 void SV_WalkMove (prvm_edict_t *ent)
1618 {
1619         int clip, oldonground, originalmove_clip, originalmove_flags, originalmove_groundentity, hitsupercontentsmask;
1620         vec3_t upmove, downmove, start_origin, start_velocity, stepnormal, originalmove_origin, originalmove_velocity;
1621         trace_t downtrace;
1622
1623         // if frametime is 0 (due to client sending the same timestamp twice),
1624         // don't move
1625         if (sv.frametime <= 0)
1626                 return;
1627
1628         hitsupercontentsmask = SV_GenericHitSuperContentsMask(ent);
1629
1630         SV_CheckVelocity(ent);
1631
1632         // do a regular slide move unless it looks like you ran into a step
1633         oldonground = (int)ent->fields.server->flags & FL_ONGROUND;
1634
1635         VectorCopy (ent->fields.server->origin, start_origin);
1636         VectorCopy (ent->fields.server->velocity, start_velocity);
1637
1638         clip = SV_FlyMove (ent, sv.frametime, NULL, hitsupercontentsmask);
1639
1640         // if the move did not hit the ground at any point, we're not on ground
1641         if (!(clip & 1))
1642                 ent->fields.server->flags = (int)ent->fields.server->flags & ~FL_ONGROUND;
1643
1644         SV_CheckVelocity(ent);
1645
1646         VectorCopy(ent->fields.server->origin, originalmove_origin);
1647         VectorCopy(ent->fields.server->velocity, originalmove_velocity);
1648         originalmove_clip = clip;
1649         originalmove_flags = (int)ent->fields.server->flags;
1650         originalmove_groundentity = ent->fields.server->groundentity;
1651
1652         if ((int)ent->fields.server->flags & FL_WATERJUMP)
1653                 return;
1654
1655         if (sv_nostep.integer)
1656                 return;
1657
1658         // if move didn't block on a step, return
1659         if (clip & 2)
1660         {
1661                 // if move was not trying to move into the step, return
1662                 if (fabs(start_velocity[0]) < 0.03125 && fabs(start_velocity[1]) < 0.03125)
1663                         return;
1664
1665                 if (ent->fields.server->movetype != MOVETYPE_FLY)
1666                 {
1667                         // return if gibbed by a trigger
1668                         if (ent->fields.server->movetype != MOVETYPE_WALK)
1669                                 return;
1670
1671                         // only step up while jumping if that is enabled
1672                         if (!(sv_jumpstep.integer && sv_gameplayfix_stepwhilejumping.integer))
1673                                 if (!oldonground && ent->fields.server->waterlevel == 0)
1674                                         return;
1675                 }
1676
1677                 // try moving up and forward to go up a step
1678                 // back to start pos
1679                 VectorCopy (start_origin, ent->fields.server->origin);
1680                 VectorCopy (start_velocity, ent->fields.server->velocity);
1681
1682                 // move up
1683                 VectorClear (upmove);
1684                 upmove[2] = sv_stepheight.value;
1685                 // FIXME: don't link?
1686                 SV_PushEntity(ent, upmove, false);
1687
1688                 // move forward
1689                 ent->fields.server->velocity[2] = 0;
1690                 clip = SV_FlyMove (ent, sv.frametime, stepnormal, hitsupercontentsmask);
1691                 ent->fields.server->velocity[2] += start_velocity[2];
1692
1693                 SV_CheckVelocity(ent);
1694
1695                 // check for stuckness, possibly due to the limited precision of floats
1696                 // in the clipping hulls
1697                 if (clip
1698                  && fabs(originalmove_origin[1] - ent->fields.server->origin[1]) < 0.03125
1699                  && fabs(originalmove_origin[0] - ent->fields.server->origin[0]) < 0.03125)
1700                 {
1701                         //Con_Printf("wall\n");
1702                         // stepping up didn't make any progress, revert to original move
1703                         VectorCopy(originalmove_origin, ent->fields.server->origin);
1704                         VectorCopy(originalmove_velocity, ent->fields.server->velocity);
1705                         //clip = originalmove_clip;
1706                         ent->fields.server->flags = originalmove_flags;
1707                         ent->fields.server->groundentity = originalmove_groundentity;
1708                         // now try to unstick if needed
1709                         //clip = SV_TryUnstick (ent, oldvel);
1710                         return;
1711                 }
1712
1713                 //Con_Printf("step - ");
1714
1715                 // extra friction based on view angle
1716                 if (clip & 2 && sv_wallfriction.integer)
1717                         SV_WallFriction (ent, stepnormal);
1718         }
1719         // don't do the down move if stepdown is disabled, moving upward, not in water, or the move started offground or ended onground
1720         else if (!sv_gameplayfix_stepdown.integer || ent->fields.server->waterlevel >= 3 || start_velocity[2] >= (1.0 / 32.0) || !oldonground || ((int)ent->fields.server->flags & FL_ONGROUND))
1721                 return;
1722
1723         // move down
1724         VectorClear (downmove);
1725         downmove[2] = -sv_stepheight.value + start_velocity[2]*sv.frametime;
1726         // FIXME: don't link?
1727         downtrace = SV_PushEntity (ent, downmove, false);
1728
1729         if (downtrace.fraction < 1 && downtrace.plane.normal[2] > 0.7)
1730         {
1731                 // this has been disabled so that you can't jump when you are stepping
1732                 // up while already jumping (also known as the Quake2 double jump bug)
1733 #if 0
1734                 // LordHavoc: disabled this check so you can walk on monsters/players
1735                 //if (ent->fields.server->solid == SOLID_BSP)
1736                 {
1737                         //Con_Printf("onground\n");
1738                         ent->fields.server->flags =     (int)ent->fields.server->flags | FL_ONGROUND;
1739                         ent->fields.server->groundentity = PRVM_EDICT_TO_PROG(downtrace.ent);
1740                 }
1741 #endif
1742         }
1743         else
1744         {
1745                 //Con_Printf("slope\n");
1746                 // if the push down didn't end up on good ground, use the move without
1747                 // the step up.  This happens near wall / slope combinations, and can
1748                 // cause the player to hop up higher on a slope too steep to climb
1749                 VectorCopy(originalmove_origin, ent->fields.server->origin);
1750                 VectorCopy(originalmove_velocity, ent->fields.server->velocity);
1751                 //clip = originalmove_clip;
1752                 ent->fields.server->flags = originalmove_flags;
1753                 ent->fields.server->groundentity = originalmove_groundentity;
1754         }
1755
1756         SV_CheckVelocity(ent);
1757 }
1758
1759 //============================================================================
1760
1761 /*
1762 =============
1763 SV_Physics_Follow
1764
1765 Entities that are "stuck" to another entity
1766 =============
1767 */
1768 void SV_Physics_Follow (prvm_edict_t *ent)
1769 {
1770         vec3_t vf, vr, vu, angles, v;
1771         prvm_edict_t *e;
1772
1773         // regular thinking
1774         if (!SV_RunThink (ent))
1775                 return;
1776
1777         // LordHavoc: implemented rotation on MOVETYPE_FOLLOW objects
1778         e = PRVM_PROG_TO_EDICT(ent->fields.server->aiment);
1779         if (e->fields.server->angles[0] == ent->fields.server->punchangle[0] && e->fields.server->angles[1] == ent->fields.server->punchangle[1] && e->fields.server->angles[2] == ent->fields.server->punchangle[2])
1780         {
1781                 // quick case for no rotation
1782                 VectorAdd(e->fields.server->origin, ent->fields.server->view_ofs, ent->fields.server->origin);
1783         }
1784         else
1785         {
1786                 angles[0] = -ent->fields.server->punchangle[0];
1787                 angles[1] =  ent->fields.server->punchangle[1];
1788                 angles[2] =  ent->fields.server->punchangle[2];
1789                 AngleVectors (angles, vf, vr, vu);
1790                 v[0] = ent->fields.server->view_ofs[0] * vf[0] + ent->fields.server->view_ofs[1] * vr[0] + ent->fields.server->view_ofs[2] * vu[0];
1791                 v[1] = ent->fields.server->view_ofs[0] * vf[1] + ent->fields.server->view_ofs[1] * vr[1] + ent->fields.server->view_ofs[2] * vu[1];
1792                 v[2] = ent->fields.server->view_ofs[0] * vf[2] + ent->fields.server->view_ofs[1] * vr[2] + ent->fields.server->view_ofs[2] * vu[2];
1793                 angles[0] = -e->fields.server->angles[0];
1794                 angles[1] =  e->fields.server->angles[1];
1795                 angles[2] =  e->fields.server->angles[2];
1796                 AngleVectors (angles, vf, vr, vu);
1797                 ent->fields.server->origin[0] = v[0] * vf[0] + v[1] * vf[1] + v[2] * vf[2] + e->fields.server->origin[0];
1798                 ent->fields.server->origin[1] = v[0] * vr[0] + v[1] * vr[1] + v[2] * vr[2] + e->fields.server->origin[1];
1799                 ent->fields.server->origin[2] = v[0] * vu[0] + v[1] * vu[1] + v[2] * vu[2] + e->fields.server->origin[2];
1800         }
1801         VectorAdd (e->fields.server->angles, ent->fields.server->v_angle, ent->fields.server->angles);
1802         SV_LinkEdict (ent, true);
1803 }
1804
1805 /*
1806 ==============================================================================
1807
1808 TOSS / BOUNCE
1809
1810 ==============================================================================
1811 */
1812
1813 /*
1814 =============
1815 SV_CheckWaterTransition
1816
1817 =============
1818 */
1819 void SV_CheckWaterTransition (prvm_edict_t *ent)
1820 {
1821         int cont;
1822         cont = Mod_Q1BSP_NativeContentsFromSuperContents(NULL, SV_PointSuperContents(ent->fields.server->origin));
1823         if (!ent->fields.server->watertype)
1824         {
1825                 // just spawned here
1826                 ent->fields.server->watertype = cont;
1827                 ent->fields.server->waterlevel = 1;
1828                 return;
1829         }
1830
1831         // DRESK - Support for Entity Contents Transition Event
1832         // NOTE: Call here BEFORE updating the watertype below,
1833         // and suppress watersplash sound if a valid function
1834         // call was made to allow for custom "splash" sounds.
1835         if( !SV_CheckContentsTransition(ent, cont) )
1836         { // Contents Transition Function Invalid; Potentially Play Water Sound
1837                 // check if the entity crossed into or out of water
1838                 if (sv_sound_watersplash.string && ((ent->fields.server->watertype == CONTENTS_WATER || ent->fields.server->watertype == CONTENTS_SLIME) != (cont == CONTENTS_WATER || cont == CONTENTS_SLIME)))
1839                         SV_StartSound (ent, 0, sv_sound_watersplash.string, 255, 1);
1840         }
1841
1842         if (cont <= CONTENTS_WATER)
1843         {
1844                 ent->fields.server->watertype = cont;
1845                 ent->fields.server->waterlevel = 1;
1846         }
1847         else
1848         {
1849                 ent->fields.server->watertype = CONTENTS_EMPTY;
1850                 ent->fields.server->waterlevel = 0;
1851         }
1852 }
1853
1854 /*
1855 =============
1856 SV_Physics_Toss
1857
1858 Toss, bounce, and fly movement.  When onground, do nothing.
1859 =============
1860 */
1861 void SV_Physics_Toss (prvm_edict_t *ent)
1862 {
1863         trace_t trace;
1864         vec3_t move;
1865
1866 // if onground, return without moving
1867         if ((int)ent->fields.server->flags & FL_ONGROUND)
1868         {
1869                 if (ent->fields.server->velocity[2] >= (1.0 / 32.0) && sv_gameplayfix_upwardvelocityclearsongroundflag.integer)
1870                 {
1871                         // don't stick to ground if onground and moving upward
1872                         ent->fields.server->flags -= FL_ONGROUND;
1873                 }
1874                 else if (!ent->fields.server->groundentity || !sv_gameplayfix_noairborncorpse.integer)
1875                 {
1876                         // we can trust FL_ONGROUND if groundentity is world because it never moves
1877                         return;
1878                 }
1879                 else if (ent->priv.server->suspendedinairflag && PRVM_PROG_TO_EDICT(ent->fields.server->groundentity)->priv.server->free)
1880                 {
1881                         // if ent was supported by a brush model on previous frame,
1882                         // and groundentity is now freed, set groundentity to 0 (world)
1883                         // which leaves it suspended in the air
1884                         ent->fields.server->groundentity = 0;
1885                         return;
1886                 }
1887         }
1888         ent->priv.server->suspendedinairflag = false;
1889
1890         SV_CheckVelocity (ent);
1891
1892 // add gravity
1893         if (ent->fields.server->movetype == MOVETYPE_TOSS || ent->fields.server->movetype == MOVETYPE_BOUNCE)
1894                 SV_AddGravity (ent);
1895
1896 // move angles
1897         VectorMA (ent->fields.server->angles, sv.frametime, ent->fields.server->avelocity, ent->fields.server->angles);
1898
1899 // move origin
1900         VectorScale (ent->fields.server->velocity, sv.frametime, move);
1901         trace = SV_PushEntity (ent, move, true);
1902         if (ent->priv.server->free)
1903                 return;
1904         if (trace.bmodelstartsolid)
1905         {
1906                 // try to unstick the entity
1907                 SV_UnstickEntity(ent);
1908                 trace = SV_PushEntity (ent, move, false);
1909                 if (ent->priv.server->free)
1910                         return;
1911         }
1912
1913         if (trace.fraction < 1)
1914         {
1915                 if (ent->fields.server->movetype == MOVETYPE_BOUNCEMISSILE)
1916                 {
1917                         ClipVelocity (ent->fields.server->velocity, trace.plane.normal, ent->fields.server->velocity, 2.0);
1918                         ent->fields.server->flags = (int)ent->fields.server->flags & ~FL_ONGROUND;
1919                 }
1920                 else if (ent->fields.server->movetype == MOVETYPE_BOUNCE)
1921                 {
1922                         float d;
1923                         ClipVelocity (ent->fields.server->velocity, trace.plane.normal, ent->fields.server->velocity, 1.5);
1924                         // LordHavoc: fixed grenades not bouncing when fired down a slope
1925                         if (sv_gameplayfix_grenadebouncedownslopes.integer)
1926                         {
1927                                 d = DotProduct(trace.plane.normal, ent->fields.server->velocity);
1928                                 if (trace.plane.normal[2] > 0.7 && fabs(d) < 60)
1929                                 {
1930                                         ent->fields.server->flags = (int)ent->fields.server->flags | FL_ONGROUND;
1931                                         ent->fields.server->groundentity = PRVM_EDICT_TO_PROG(trace.ent);
1932                                         VectorClear (ent->fields.server->velocity);
1933                                         VectorClear (ent->fields.server->avelocity);
1934                                 }
1935                                 else
1936                                         ent->fields.server->flags = (int)ent->fields.server->flags & ~FL_ONGROUND;
1937                         }
1938                         else
1939                         {
1940                                 if (trace.plane.normal[2] > 0.7 && ent->fields.server->velocity[2] < 60)
1941                                 {
1942                                         ent->fields.server->flags = (int)ent->fields.server->flags | FL_ONGROUND;
1943                                         ent->fields.server->groundentity = PRVM_EDICT_TO_PROG(trace.ent);
1944                                         VectorClear (ent->fields.server->velocity);
1945                                         VectorClear (ent->fields.server->avelocity);
1946                                 }
1947                                 else
1948                                         ent->fields.server->flags = (int)ent->fields.server->flags & ~FL_ONGROUND;
1949                         }
1950                 }
1951                 else
1952                 {
1953                         ClipVelocity (ent->fields.server->velocity, trace.plane.normal, ent->fields.server->velocity, 1.0);
1954                         if (trace.plane.normal[2] > 0.7)
1955                         {
1956                                 ent->fields.server->flags = (int)ent->fields.server->flags | FL_ONGROUND;
1957                                 ent->fields.server->groundentity = PRVM_EDICT_TO_PROG(trace.ent);
1958                                 if (((prvm_edict_t *)trace.ent)->fields.server->solid == SOLID_BSP)
1959                                         ent->priv.server->suspendedinairflag = true;
1960                                 VectorClear (ent->fields.server->velocity);
1961                                 VectorClear (ent->fields.server->avelocity);
1962                         }
1963                         else
1964                                 ent->fields.server->flags = (int)ent->fields.server->flags & ~FL_ONGROUND;
1965                 }
1966         }
1967
1968 // check for in water
1969         SV_CheckWaterTransition (ent);
1970 }
1971
1972 /*
1973 ===============================================================================
1974
1975 STEPPING MOVEMENT
1976
1977 ===============================================================================
1978 */
1979
1980 /*
1981 =============
1982 SV_Physics_Step
1983
1984 Monsters freefall when they don't have a ground entity, otherwise
1985 all movement is done with discrete steps.
1986
1987 This is also used for objects that have become still on the ground, but
1988 will fall if the floor is pulled out from under them.
1989 =============
1990 */
1991 void SV_Physics_Step (prvm_edict_t *ent)
1992 {
1993         int flags = (int)ent->fields.server->flags;
1994
1995         // DRESK
1996         // Backup Velocity in the event that movetypesteplandevent is called,
1997         // to provide a parameter with the entity's velocity at impact.
1998         prvm_eval_t *movetypesteplandevent;
1999         vec3_t backupVelocity;
2000         VectorCopy(ent->fields.server->velocity, backupVelocity);
2001         // don't fall at all if fly/swim
2002         if (!(flags & (FL_FLY | FL_SWIM)))
2003         {
2004                 if (flags & FL_ONGROUND)
2005                 {
2006                         // freefall if onground and moving upward
2007                         // freefall if not standing on a world surface (it may be a lift or trap door)
2008                         if ((ent->fields.server->velocity[2] >= (1.0 / 32.0) && sv_gameplayfix_upwardvelocityclearsongroundflag.integer) || ent->fields.server->groundentity)
2009                         {
2010                                 ent->fields.server->flags -= FL_ONGROUND;
2011                                 SV_AddGravity(ent);
2012                                 SV_CheckVelocity(ent);
2013                                 SV_FlyMove(ent, sv.frametime, NULL, SV_GenericHitSuperContentsMask(ent));
2014                                 SV_LinkEdict(ent, true);
2015                                 ent->priv.server->waterposition_forceupdate = true;
2016                         }
2017                 }
2018                 else
2019                 {
2020                         // freefall if not onground
2021                         int hitsound = ent->fields.server->velocity[2] < sv_gravity.value * -0.1;
2022
2023                         SV_AddGravity(ent);
2024                         SV_CheckVelocity(ent);
2025                         SV_FlyMove(ent, sv.frametime, NULL, SV_GenericHitSuperContentsMask(ent));
2026                         SV_LinkEdict(ent, true);
2027
2028                         // just hit ground
2029                         if (hitsound && (int)ent->fields.server->flags & FL_ONGROUND)
2030                         {
2031                                 // DRESK - Check for Entity Land Event Function
2032                                 movetypesteplandevent = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.movetypesteplandevent);
2033
2034                                 if(movetypesteplandevent->function)
2035                                 { // Valid Function; Execute
2036                                         // Prepare Parameters
2037                                                 // Assign Velocity at Impact
2038                                                 PRVM_G_VECTOR(OFS_PARM0)[0] = backupVelocity[0];
2039                                                 PRVM_G_VECTOR(OFS_PARM0)[1] = backupVelocity[1];
2040                                                 PRVM_G_VECTOR(OFS_PARM0)[2] = backupVelocity[2];
2041                                                 // Assign Self
2042                                                 prog->globals.server->self = PRVM_EDICT_TO_PROG(ent);
2043                                         // Execute VM Function
2044                                         PRVM_ExecuteProgram(movetypesteplandevent->function, "movetypesteplandevent: NULL function");
2045                                 }
2046                                 else
2047                                 // Check for Engine Landing Sound
2048                                 if(sv_sound_land.string)
2049                                         SV_StartSound(ent, 0, sv_sound_land.string, 255, 1);
2050                         }
2051                         ent->priv.server->waterposition_forceupdate = true;
2052                 }
2053         }
2054
2055 // regular thinking
2056         if (!SV_RunThink(ent))
2057                 return;
2058
2059         if (ent->priv.server->waterposition_forceupdate || !VectorCompare(ent->fields.server->origin, ent->priv.server->waterposition_origin))
2060         {
2061                 ent->priv.server->waterposition_forceupdate = false;
2062                 VectorCopy(ent->fields.server->origin, ent->priv.server->waterposition_origin);
2063                 SV_CheckWaterTransition(ent);
2064         }
2065 }
2066
2067 //============================================================================
2068
2069 static void SV_Physics_Entity (prvm_edict_t *ent)
2070 {
2071         // don't run a move on newly spawned projectiles as it messes up movement
2072         // interpolation and rocket trails
2073         qboolean runmove = ent->priv.server->move;
2074         ent->priv.server->move = true;
2075         switch ((int) ent->fields.server->movetype)
2076         {
2077         case MOVETYPE_PUSH:
2078         case MOVETYPE_FAKEPUSH:
2079                 SV_Physics_Pusher (ent);
2080                 break;
2081         case MOVETYPE_NONE:
2082                 // LordHavoc: manually inlined the thinktime check here because MOVETYPE_NONE is used on so many objects
2083                 if (ent->fields.server->nextthink > 0 && ent->fields.server->nextthink <= sv.time + sv.frametime)
2084                         SV_RunThink (ent);
2085                 break;
2086         case MOVETYPE_FOLLOW:
2087                 SV_Physics_Follow (ent);
2088                 break;
2089         case MOVETYPE_NOCLIP:
2090                 if (SV_RunThink(ent))
2091                 {
2092                         SV_CheckWater(ent);
2093                         VectorMA(ent->fields.server->origin, sv.frametime, ent->fields.server->velocity, ent->fields.server->origin);
2094                         VectorMA(ent->fields.server->angles, sv.frametime, ent->fields.server->avelocity, ent->fields.server->angles);
2095                 }
2096                 SV_LinkEdict(ent, false);
2097                 break;
2098         case MOVETYPE_STEP:
2099                 SV_Physics_Step (ent);
2100                 break;
2101         case MOVETYPE_WALK:
2102                 if (SV_RunThink (ent))
2103                 {
2104                         if (!SV_CheckWater (ent) && ! ((int)ent->fields.server->flags & FL_WATERJUMP) )
2105                                 SV_AddGravity (ent);
2106                         SV_CheckStuck (ent);
2107                         SV_WalkMove (ent);
2108                         SV_LinkEdict (ent, true);
2109                 }
2110                 break;
2111         case MOVETYPE_TOSS:
2112         case MOVETYPE_BOUNCE:
2113         case MOVETYPE_BOUNCEMISSILE:
2114         case MOVETYPE_FLYMISSILE:
2115         case MOVETYPE_FLY:
2116                 // regular thinking
2117                 if (SV_RunThink (ent) && (runmove || !sv_gameplayfix_delayprojectiles.integer))
2118                         SV_Physics_Toss (ent);
2119                 break;
2120         default:
2121                 Con_Printf ("SV_Physics: bad movetype %i\n", (int)ent->fields.server->movetype);
2122                 break;
2123         }
2124 }
2125
2126 void SV_Physics_ClientMove(void)
2127 {
2128         prvm_edict_t *ent;
2129         ent = host_client->edict;
2130
2131         // call player physics, this needs the proper frametime
2132         prog->globals.server->frametime = sv.frametime;
2133         SV_ClientThink();
2134
2135         // call standard client pre-think, with frametime = 0
2136         prog->globals.server->time = sv.time;
2137         prog->globals.server->frametime = 0;
2138         prog->globals.server->self = PRVM_EDICT_TO_PROG(ent);
2139         PRVM_ExecuteProgram (prog->globals.server->PlayerPreThink, "QC function PlayerPreThink is missing");
2140         prog->globals.server->frametime = sv.frametime;
2141
2142         // make sure the velocity is sane (not a NaN)
2143         SV_CheckVelocity(ent);
2144         // LordHavoc: a hack to ensure that the (rather silly) id1 quakec
2145         // player_run/player_stand1 does not horribly malfunction if the
2146         // velocity becomes a number that is both == 0 and != 0
2147         // (sounds to me like NaN but to be absolutely safe...)
2148         if (DotProduct(ent->fields.server->velocity, ent->fields.server->velocity) < 0.0001)
2149                 VectorClear(ent->fields.server->velocity);
2150
2151         // perform MOVETYPE_WALK behavior
2152         if (!SV_CheckWater (ent) && ! ((int)ent->fields.server->flags & FL_WATERJUMP) )
2153                 SV_AddGravity (ent);
2154         SV_CheckStuck (ent);
2155         SV_WalkMove (ent);
2156
2157         SV_CheckVelocity (ent);
2158
2159         SV_LinkEdict (ent, true);
2160
2161         SV_CheckVelocity (ent);
2162
2163         // call standard player post-think, with frametime = 0
2164         prog->globals.server->time = sv.time;
2165         prog->globals.server->frametime = 0;
2166         prog->globals.server->self = PRVM_EDICT_TO_PROG(ent);
2167         PRVM_ExecuteProgram (prog->globals.server->PlayerPostThink, "QC function PlayerPostThink is missing");
2168         prog->globals.server->frametime = sv.frametime;
2169
2170         if(ent->fields.server->fixangle)
2171         {
2172                 // angle fixing was requested by physics code...
2173                 // so store the current angles for later use
2174                 memcpy(host_client->fixangle_angles, ent->fields.server->angles, sizeof(host_client->fixangle_angles));
2175                 host_client->fixangle_angles_set = TRUE;
2176
2177                 // and clear fixangle for the next frame
2178                 ent->fields.server->fixangle = 0;
2179         }
2180 }
2181
2182 void SV_Physics_ClientEntity(prvm_edict_t *ent)
2183 {
2184         // don't do physics on disconnected clients, FrikBot relies on this
2185         if (!host_client->spawned)
2186         {
2187                 memset(&host_client->cmd, 0, sizeof(host_client->cmd));
2188                 return;
2189         }
2190
2191         // don't run physics here if running asynchronously
2192         if (host_client->clmovement_skipphysicsframes <= 0)
2193                 SV_ClientThink();
2194
2195         // make sure the velocity is sane (not a NaN)
2196         SV_CheckVelocity(ent);
2197         // LordHavoc: a hack to ensure that the (rather silly) id1 quakec
2198         // player_run/player_stand1 does not horribly malfunction if the
2199         // velocity becomes a number that is both == 0 and != 0
2200         // (sounds to me like NaN but to be absolutely safe...)
2201         if (DotProduct(ent->fields.server->velocity, ent->fields.server->velocity) < 0.0001)
2202                 VectorClear(ent->fields.server->velocity);
2203
2204         // call standard client pre-think
2205         prog->globals.server->time = sv.time;
2206         prog->globals.server->self = PRVM_EDICT_TO_PROG(ent);
2207         PRVM_ExecuteProgram (prog->globals.server->PlayerPreThink, "QC function PlayerPreThink is missing");
2208         SV_CheckVelocity (ent);
2209
2210         switch ((int) ent->fields.server->movetype)
2211         {
2212         case MOVETYPE_PUSH:
2213         case MOVETYPE_FAKEPUSH:
2214                 SV_Physics_Pusher (ent);
2215                 break;
2216         case MOVETYPE_NONE:
2217                 // LordHavoc: manually inlined the thinktime check here because MOVETYPE_NONE is used on so many objects
2218                 if (ent->fields.server->nextthink > 0 && ent->fields.server->nextthink <= sv.time + sv.frametime)
2219                         SV_RunThink (ent);
2220                 break;
2221         case MOVETYPE_FOLLOW:
2222                 SV_Physics_Follow (ent);
2223                 break;
2224         case MOVETYPE_NOCLIP:
2225                 SV_RunThink(ent);
2226                 SV_CheckWater(ent);
2227                 VectorMA(ent->fields.server->origin, sv.frametime, ent->fields.server->velocity, ent->fields.server->origin);
2228                 VectorMA(ent->fields.server->angles, sv.frametime, ent->fields.server->avelocity, ent->fields.server->angles);
2229                 break;
2230         case MOVETYPE_STEP:
2231                 SV_Physics_Step (ent);
2232                 break;
2233         case MOVETYPE_WALK:
2234                 SV_RunThink (ent);
2235                 // don't run physics here if running asynchronously
2236                 if (host_client->clmovement_skipphysicsframes <= 0)
2237                 {
2238                         if (!SV_CheckWater (ent) && ! ((int)ent->fields.server->flags & FL_WATERJUMP) )
2239                                 SV_AddGravity (ent);
2240                         SV_CheckStuck (ent);
2241                         SV_WalkMove (ent);
2242                 }
2243                 break;
2244         case MOVETYPE_TOSS:
2245         case MOVETYPE_BOUNCE:
2246         case MOVETYPE_BOUNCEMISSILE:
2247         case MOVETYPE_FLYMISSILE:
2248                 // regular thinking
2249                 SV_RunThink (ent);
2250                 SV_Physics_Toss (ent);
2251                 break;
2252         case MOVETYPE_FLY:
2253                 SV_RunThink (ent);
2254                 SV_CheckWater (ent);
2255                 SV_WalkMove (ent);
2256                 break;
2257         default:
2258                 Con_Printf ("SV_Physics_ClientEntity: bad movetype %i\n", (int)ent->fields.server->movetype);
2259                 break;
2260         }
2261
2262         // decrement the countdown variable used to decide when to go back to
2263         // synchronous physics
2264         if (host_client->clmovement_skipphysicsframes > 0)
2265                 host_client->clmovement_skipphysicsframes--;
2266
2267         SV_CheckVelocity (ent);
2268
2269         SV_LinkEdict (ent, true);
2270
2271         SV_CheckVelocity (ent);
2272
2273         // call standard player post-think
2274         prog->globals.server->time = sv.time;
2275         prog->globals.server->self = PRVM_EDICT_TO_PROG(ent);
2276         PRVM_ExecuteProgram (prog->globals.server->PlayerPostThink, "QC function PlayerPostThink is missing");
2277
2278         if(ent->fields.server->fixangle)
2279         {
2280                 // angle fixing was requested by physics code...
2281                 // so store the current angles for later use
2282                 memcpy(host_client->fixangle_angles, ent->fields.server->angles, sizeof(host_client->fixangle_angles));
2283                 host_client->fixangle_angles_set = TRUE;
2284
2285                 // and clear fixangle for the next frame
2286                 ent->fields.server->fixangle = 0;
2287         }
2288 }
2289
2290 /*
2291 ================
2292 SV_Physics
2293
2294 ================
2295 */
2296 void SV_Physics (void)
2297 {
2298         int i;
2299         prvm_edict_t *ent;
2300
2301 // let the progs know that a new frame has started
2302         prog->globals.server->self = PRVM_EDICT_TO_PROG(prog->edicts);
2303         prog->globals.server->other = PRVM_EDICT_TO_PROG(prog->edicts);
2304         prog->globals.server->time = sv.time;
2305         prog->globals.server->frametime = sv.frametime;
2306         PRVM_ExecuteProgram (prog->globals.server->StartFrame, "QC function StartFrame is missing");
2307
2308 //
2309 // treat each object in turn
2310 //
2311
2312         // if force_retouch, relink all the entities
2313         if (prog->globals.server->force_retouch > 0)
2314                 for (i = 1, ent = PRVM_EDICT_NUM(i);i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
2315                         if (!ent->priv.server->free)
2316                                 SV_LinkEdict (ent, true);       // force retouch even for stationary
2317
2318         // run physics on the client entities
2319         for (i = 1, ent = PRVM_EDICT_NUM(i), host_client = svs.clients;i <= svs.maxclients;i++, ent = PRVM_NEXT_EDICT(ent), host_client++)
2320                 if (!ent->priv.server->free)
2321                                 SV_Physics_ClientEntity(ent);
2322
2323         // run physics on all the non-client entities
2324         if (!sv_freezenonclients.integer)
2325                 for (;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
2326                         if (!ent->priv.server->free)
2327                                 SV_Physics_Entity(ent);
2328
2329         if (prog->globals.server->force_retouch > 0)
2330                 prog->globals.server->force_retouch = max(0, prog->globals.server->force_retouch - 1);
2331
2332         // LordHavoc: endframe support
2333         if (prog->funcoffsets.EndFrame)
2334         {
2335                 prog->globals.server->self = PRVM_EDICT_TO_PROG(prog->edicts);
2336                 prog->globals.server->other = PRVM_EDICT_TO_PROG(prog->edicts);
2337                 prog->globals.server->time = sv.time;
2338                 PRVM_ExecuteProgram (prog->funcoffsets.EndFrame, "QC function EndFrame is missing");
2339         }
2340
2341         // decrement prog->num_edicts if the highest number entities died
2342         for (;PRVM_EDICT_NUM(prog->num_edicts - 1)->priv.server->free;prog->num_edicts--);
2343
2344         if (!sv_freezenonclients.integer)
2345                 sv.time += sv.frametime;
2346 }