]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics/movetypes/movetypes.qc
Some minor adjustments to the push movetype, fix QC physics entities not running...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / physics / movetypes / movetypes.qc
1 #include "movetypes.qh"
2
3 #ifdef SVQC
4 void set_movetype(entity this, int mt)
5 {
6         this.move_movetype = mt;
7         if (mt == MOVETYPE_PHYSICS) {
8                 this.move_qcphysics = false;
9         }
10         if(!IL_CONTAINS(g_moveables, this))
11                 IL_PUSH(g_moveables, this); // add it to the moveable entities list (even if it doesn't move!) logic: if an object never sets its movetype, we assume it never does anything notable
12         this.movetype = (this.move_qcphysics) ? MOVETYPE_QCENTITY : mt;
13 }
14 #elif defined(CSQC)
15 void set_movetype(entity this, int mt)
16 {
17         this.move_movetype = mt;
18 }
19 #endif
20
21 bool _Movetype_NudgeOutOfSolid_PivotIsKnownGood(entity this, vector pivot) // SV_NudgeOutOfSolid_PivotIsKnownGood
22 {
23         vector stuckorigin = this.origin;
24         vector goodmins = pivot, goodmaxs = pivot;
25         for(int bump = 0; bump < 6; bump++)
26         {
27                 int coord = 2 - (bump >> 1);
28                 bool dir = (bump & 1);
29
30                 for(int subbump = 0; ; ++subbump)
31                 {
32                         vector testorigin = stuckorigin;
33                         if(dir)
34                         {
35                                 // pushing maxs
36                                 switch(coord)
37                                 {
38                                         case 0: testorigin.x += this.maxs_x - goodmaxs.x; break;
39                                         case 1: testorigin.y += this.maxs_y - goodmaxs.y; break;
40                                         case 2: testorigin.z += this.maxs_z - goodmaxs.z; break;
41                                 }
42                         }
43                         else
44                         {
45                                 // pushing mins
46                                 switch(coord)
47                                 {
48                                         case 0: testorigin.x += this.mins_x - goodmins.x; break;
49                                         case 1: testorigin.y += this.mins_y - goodmins.y; break;
50                                         case 2: testorigin.z += this.mins_z - goodmins.z; break;
51                                 }
52                         }
53
54                         tracebox(stuckorigin, goodmins, goodmaxs, testorigin, MOVE_NOMONSTERS, this);
55                         if(trace_startsolid && trace_ent.solid == SOLID_BSP) // NOTE: this checks for bmodelstartsolid in the engine
56                         {
57                                 
58                                 // BAD BAD, can't fix that
59                                 return false;
60                         }
61
62                         if(trace_fraction >= 1)
63                                 break; // it WORKS!
64
65                         if(subbump >= 10)
66                         {
67                                 // BAD BAD, can't fix that
68                                 return false;
69                         }
70
71                         // we hit something... let's move out of it
72                         vector move = trace_endpos - testorigin;
73                         float nudge = (trace_plane_normal * move) + 0.03125f; // FIXME cvar this constant
74                         stuckorigin = stuckorigin + nudge * trace_plane_normal;
75                 }
76
77                 if(dir)
78                 {
79                         // pushing maxs
80                         switch(coord)
81                         {
82                                 case 0: goodmaxs.x = this.maxs_x; break;
83                                 case 1: goodmaxs.y = this.maxs_y; break;
84                                 case 2: goodmaxs.z = this.maxs_z; break;
85                         }
86                 }
87                 else
88                 {
89                         // pushing mins
90                         switch(coord)
91                         {
92                                 case 0: goodmins.x = this.mins_x; break;
93                                 case 1: goodmins.y = this.mins_y; break;
94                                 case 2: goodmins.z = this.mins_z; break;
95                         }
96                 }
97         }
98
99         // WE WIN
100         this.origin = stuckorigin;
101
102         return true;
103 }
104
105 void _Movetype_WallFriction(entity this, vector stepnormal)  // SV_WallFriction
106 {
107         /*float d, i;
108         vector into, side;
109         makevectors(this.v_angle);
110         d = (stepnormal * v_forward) + 0.5;
111
112         if(d < 0)
113         {
114             i = (stepnormal * this.velocity);
115             into = i * stepnormal;
116             side = this.velocity - into;
117             this.velocity_x = side.x * (1 * d);
118             this.velocity_y = side.y * (1 * d);
119         }*/
120 }
121
122 vector planes[MAX_CLIP_PLANES];
123 int _Movetype_FlyMove(entity this, float dt, bool applygravity, bool applystepnormal, float stepheight) // SV_FlyMove
124 {
125         move_stepnormal = '0 0 0';
126
127         if(dt <= 0)
128                 return 0;
129
130         int blockedflag = 0;
131         int i, j, numplanes = 0;
132         float time_left = dt, grav = 0;
133         vector push;
134         vector primal_velocity, original_velocity;
135         vector restore_velocity = this.velocity;
136
137         for(i = 0; i < MAX_CLIP_PLANES; ++i)
138                 planes[i] = '0 0 0';
139
140         if(applygravity)
141         {
142                 this.move_didgravity = 1;
143                 grav = dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
144
145                 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
146                 {
147                         if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
148                                 this.velocity_z -= grav * 0.5;
149                         else
150                                 this.velocity_z -= grav;
151                 }
152         }
153
154         original_velocity = primal_velocity = this.velocity;
155
156         for(int bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
157         {
158                 if(this.velocity == '0 0 0')
159                         break;
160
161                 push = this.velocity * time_left;
162                 if(!_Movetype_PushEntity(this, push, true, false))
163                 {
164                         // we got teleported by a touch function
165                         // let's abort the move
166                         blockedflag |= 8;
167                         break;
168                 }
169
170                 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
171                 // abort move if we're stuck in the world (and didn't make it out)
172                 if(trace_startsolid && trace_allsolid)
173                 {
174                         this.velocity = restore_velocity;
175                         return 3;
176                 }
177
178                 if(trace_fraction == 1)
179                         break;
180
181                 float my_trace_fraction = trace_fraction;
182                 vector my_trace_plane_normal = trace_plane_normal;
183
184                 if(trace_plane_normal.z)
185                 {
186                         if(trace_plane_normal.z > 0.7)
187                         {
188                                 // floor
189                                 blockedflag |= 1;
190
191                                 if(!trace_ent)
192                                 {
193                                         //dprint("_Movetype_FlyMove: !trace_ent\n");
194                                         trace_ent = NULL;
195                                 }
196
197                                 SET_ONGROUND(this);
198                                 this.groundentity = trace_ent;
199                         }
200                 }
201                 else if(stepheight)
202                 {
203                         // step - handle it immediately
204                         vector org = this.origin;
205                         vector steppush = '0 0 1' * stepheight;
206
207                         if(!_Movetype_PushEntity(this, steppush, true, false))
208                         {
209                                 blockedflag |= 8;
210                                 break;
211                         }
212                         if(!_Movetype_PushEntity(this, push, true, false))
213                         {
214                                 blockedflag |= 8;
215                                 break;
216                         }
217                         float trace2_fraction = trace_fraction;
218                         steppush = vec3(0, 0, org.z - this.origin_z);
219                         if(!_Movetype_PushEntity(this, steppush, true, false))
220                         {
221                                 blockedflag |= 8;
222                                 break;
223                         }
224
225                         // accept the new position if it made some progress...
226                         if(fabs(this.origin_x - org.x) >= 0.03125 || fabs(this.origin_y - org.y) >= 0.03125)
227                         {
228                                 trace_endpos = this.origin;
229                                 time_left *= 1 - trace2_fraction;
230                                 numplanes = 0;
231                                 continue;
232                         }
233                         else
234                                 this.origin = org;
235                 }
236                 else
237                 {
238                         // step - return it to caller
239                         blockedflag |= 2;
240                         // save the trace for player extrafriction
241                         if(applystepnormal)
242                                 move_stepnormal = trace_plane_normal;
243                 }
244
245                 if(my_trace_fraction >= 0.001)
246                 {
247                         // actually covered some distance
248                         original_velocity = this.velocity;
249                         numplanes = 0;
250                 }
251
252                 time_left *= 1 - my_trace_fraction;
253
254                 // clipped to another plane
255                 if(numplanes >= MAX_CLIP_PLANES)
256                 {
257                         // this shouldn't really happen
258                         this.velocity = '0 0 0';
259                         blockedflag = 3;
260                         break;
261                 }
262
263                 planes[numplanes] = my_trace_plane_normal;
264                 numplanes++;
265
266                 // modify original_velocity so it parallels all of the clip planes
267                 vector new_velocity = '0 0 0';
268                 for (i = 0;i < numplanes;i++)
269                 {
270                         new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
271                         for (j = 0;j < numplanes;j++)
272                         {
273                                 if(j != i)
274                                 {
275                                         // not ok
276                                         if((new_velocity * planes[j]) < 0)
277                                                 break;
278                                 }
279                         }
280                         if(j == numplanes)
281                                 break;
282                 }
283
284                 if(i != numplanes)
285                 {
286                         // go along this plane
287                         this.velocity = new_velocity;
288                 }
289                 else
290                 {
291                         // go along the crease
292                         if(numplanes != 2)
293                         {
294                                 this.velocity = '0 0 0';
295                                 blockedflag = 7;
296                                 break;
297                         }
298                         vector dir = cross(planes[0], planes[1]);
299                         // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
300                         float ilength = sqrt((dir * dir));
301                         if(ilength)
302                                 ilength = 1.0 / ilength;
303                         dir.x *= ilength;
304                         dir.y *= ilength;
305                         dir.z *= ilength;
306                         float d = (dir * this.velocity);
307                         this.velocity = dir * d;
308                 }
309
310                 // if current velocity is against the original velocity,
311                 // stop dead to avoid tiny occilations in sloping corners
312                 if((this.velocity * primal_velocity) <= 0)
313                 {
314                         this.velocity = '0 0 0';
315                         break;
316                 }
317         }
318
319         // LordHavoc: this came from QW and allows you to get out of water more easily
320         if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blockedflag & 8))
321                 this.velocity = primal_velocity;
322
323         if(PHYS_WALLCLIP(this) && this.pm_time && !(this.flags & FL_WATERJUMP) && !(blockedflag & 8))
324                 this.velocity = primal_velocity;
325
326         if(applygravity)
327         {
328                 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
329                 {
330                         if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
331                                 this.velocity_z -= grav * 0.5f;
332                 }
333         }
334
335         return blockedflag;
336 }
337
338 void _Movetype_CheckVelocity(entity this)  // SV_CheckVelocity
339 {
340         // if(vlen(this.velocity) < 0.0001)
341         // this.velocity = '0 0 0';
342 }
343
344 bool _Movetype_CheckWater(entity this)  // SV_CheckWater
345 {
346         vector point = this.origin;
347         point.z += this.mins.z + 1;
348
349         int nativecontents = pointcontents(point);
350         if(this.watertype && this.watertype != nativecontents)
351         {
352                 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
353                 if(this.contentstransition)
354                         this.contentstransition(this.watertype, nativecontents);
355         }
356
357         this.waterlevel = WATERLEVEL_NONE;
358         this.watertype = CONTENT_EMPTY;
359
360         int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
361         if(supercontents & DPCONTENTS_LIQUIDSMASK)
362         {
363                 this.watertype = nativecontents;
364                 this.waterlevel = WATERLEVEL_WETFEET;
365                 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
366                 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
367                 {
368                         this.waterlevel = WATERLEVEL_SWIMMING;
369                         point.z = this.origin.z + this.view_ofs.z;
370                         if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
371                                 this.waterlevel = WATERLEVEL_SUBMERGED;
372                 }
373         }
374
375         return this.waterlevel > 1;
376 }
377
378 void _Movetype_CheckWaterTransition(entity ent)  // SV_CheckWaterTransition
379 {
380         int contents = pointcontents(ent.origin);
381
382         if(!ent.watertype)
383         {
384                 // just spawned here
385                 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
386                 {
387                         ent.watertype = contents;
388                         ent.waterlevel = 1;
389                         return;
390                 }
391         }
392         else if(ent.watertype != contents)
393         {
394                 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
395                 if(ent.contentstransition)
396                         ent.contentstransition(ent.watertype, contents);
397         }
398
399         if(contents <= CONTENT_WATER)
400         {
401                 ent.watertype = contents;
402                 ent.waterlevel = 1;
403         }
404         else
405         {
406                 ent.watertype = CONTENT_EMPTY;
407                 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
408         }
409 }
410
411 void _Movetype_Impact(entity this, entity oth)  // SV_Impact
412 {
413         if(!this && !oth)
414                 return;
415
416         if(this.solid != SOLID_NOT && gettouch(this))
417                 gettouch(this)(this, oth);
418
419         if(oth.solid != SOLID_NOT && gettouch(oth))
420                 gettouch(oth)(oth, this);
421 }
422
423 void _Movetype_LinkEdict_TouchAreaGrid(entity this)  // SV_LinkEdict_TouchAreaGrid
424 {
425         if(this.solid == SOLID_NOT)
426                 return;
427
428     FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
429                 if (it.solid == SOLID_TRIGGER && it != this)
430                 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
431                 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
432                 {
433                         trace_allsolid = false;
434                         trace_startsolid = false;
435                         trace_fraction = 1;
436                         trace_inwater = false;
437                         trace_inopen = true;
438                         trace_endpos = it.origin;
439                         trace_plane_normal = '0 0 1';
440                         trace_plane_dist = 0;
441                         trace_ent = this;
442                         trace_dpstartcontents = 0;
443                         trace_dphitcontents = 0;
444                         trace_dphitq3surfaceflags = 0;
445                         trace_dphittexturename = string_null;
446
447                         gettouch(it)(it, this);
448                 }
449     });
450 }
451
452 bool autocvar__movetype_debug = false;
453 void _Movetype_LinkEdict(entity this, bool touch_triggers)  // SV_LinkEdict
454 {
455         if(autocvar__movetype_debug)
456         {
457                 vector mi, ma;
458                 if(this.solid == SOLID_BSP)
459                 {
460                         // TODO set the absolute bbox
461                         mi = this.mins;
462                         ma = this.maxs;
463                 }
464                 else
465                 {
466                         mi = this.mins;
467                         ma = this.maxs;
468                 }
469                 mi += this.origin;
470                 ma += this.origin;
471
472                 if(this.flags & FL_ITEM)
473                 {
474                         mi -= '15 15 1';
475                         ma += '15 15 1';
476                 }
477                 else
478                 {
479                         mi -= '1 1 1';
480                         ma += '1 1 1';
481                 }
482
483                 this.absmin = mi;
484                 this.absmax = ma;
485         }
486         else
487         {
488                 setorigin(this, this.origin); // calls SV_LinkEdict
489         #ifdef CSQC
490                 // NOTE: CSQC's version of setorigin doesn't expand
491                 this.absmin -= '1 1 1';
492                 this.absmax += '1 1 1';
493         #endif
494         }
495
496         if(touch_triggers)
497                 _Movetype_LinkEdict_TouchAreaGrid(this);
498 }
499
500 int _Movetype_ContentsMask(entity this)  // SV_GenericHitSuperContentsMask
501 {
502         if(this)
503         {
504                 if(this.dphitcontentsmask)
505                         return this.dphitcontentsmask;
506                 else if(this.solid == SOLID_SLIDEBOX)
507                 {
508                         if(this.flags & 32) // TODO: FL_MONSTER
509                                 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_MONSTERCLIP;
510                         else
511                                 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
512                 }
513                 else if(this.solid == SOLID_CORPSE)
514                         return DPCONTENTS_SOLID | DPCONTENTS_BODY;
515                 else if(this.solid == SOLID_TRIGGER)
516                         return DPCONTENTS_SOLID | DPCONTENTS_BODY;
517                 else
518                         return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
519         }
520         else
521                 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
522 }
523
524 entity _Movetype_TestEntityPosition_ent;
525 bool _Movetype_TestEntityPosition(vector ofs)  // SV_TestEntityPosition
526 {
527     entity this = _Movetype_TestEntityPosition_ent;
528         vector org = this.origin + ofs;
529
530         //int cont = this.dphitcontentsmask;
531         //this.dphitcontentsmask = DPCONTENTS_SOLID;
532         tracebox(org, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
533         //this.dphitcontentsmask = cont;
534         if(trace_dpstartcontents & _Movetype_ContentsMask(this))
535                 return true;
536
537         if(vlen2(trace_endpos - this.origin) >= 0.0001)
538         {
539                 tracebox(trace_endpos, this.mins, this.maxs, trace_endpos, MOVE_NOMONSTERS, this);
540                 if(!trace_startsolid)
541                         this.origin = trace_endpos;
542         }
543         return false;
544 }
545
546 bool _Movetype_TestEntityPosition_Offset(int offset)
547 {
548         // NOTE: expects _Movetype_TestEntityPosition_ent to be set to the correct entity
549         // returns true if stuck
550
551     // start at 2, since the first position has already been checked
552     for(int j = 2; j <= offset; ++j)
553     {
554         if(!_Movetype_TestEntityPosition('0 0 -1' * j))
555                 return false;
556         if(!_Movetype_TestEntityPosition('0 0 1' * j))
557                 return false;
558     }
559
560         return true;
561 }
562
563 int _Movetype_UnstickEntity(entity this)  // SV_UnstickEntity
564 {
565     _Movetype_TestEntityPosition_ent = this;
566         if (!_Movetype_TestEntityPosition(' 0  0  0')) {
567             return UNSTICK_FINE;
568         }
569         #define X(v) if (_Movetype_TestEntityPosition(v))
570         X('0  0  -1') X(' 0  0  1')
571         X('-1  0  0') X(' 1  0  0')
572         X(' 0 -1  0') X(' 0  1  0')
573         X('-1 -1  0') X(' 1 -1  0')
574         X('-1  1  0') X(' 1  1  0')
575         #undef X
576         {
577         if(_Movetype_TestEntityPosition_Offset(rint((this.maxs.z - this.mins.z) * 0.36)))
578         {
579             LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
580                 etof(this), this.classname, vtos(this.origin));
581             return UNSTICK_STUCK;
582         }
583         }
584         LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
585                 etof(this), this.classname, vtos(this.origin));
586         _Movetype_LinkEdict(this, false);
587         return UNSTICK_FIXED;
588 }
589
590 void _Movetype_CheckStuck(entity this)  // SV_CheckStuck
591 {
592         int unstick = _Movetype_UnstickEntity(this); // sets test position entity
593         switch(unstick)
594         {
595                 case UNSTICK_FINE:
596                         this.oldorigin = this.origin;
597                         break;
598                 case UNSTICK_FIXED:
599                         break; // already sorted
600                 case UNSTICK_STUCK:
601                         vector offset = this.oldorigin - this.origin;
602                         if(!_Movetype_TestEntityPosition(offset))
603                                 _Movetype_LinkEdict(this, false);
604                         // couldn't unstick, should we warn about this?
605                         break;
606         }
607 }
608
609 vector _Movetype_ClipVelocity(vector vel, vector norm, float f)  // SV_ClipVelocity
610 {
611         vel -= ((vel * norm) * norm) * f;
612
613         if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
614         if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
615         if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
616
617         return vel;
618 }
619
620 void _Movetype_PushEntityTrace(entity this, vector push)
621 {
622         vector end = this.origin + push;
623         int type;
624         if(this.move_nomonsters)
625                 type = max(0, this.move_nomonsters);
626         else if(this.move_movetype == MOVETYPE_FLYMISSILE)
627                 type = MOVE_MISSILE;
628         else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
629                 type = MOVE_WORLDONLY;
630         else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
631                 type = MOVE_NOMONSTERS;
632         else
633                 type = MOVE_NORMAL;
634
635         tracebox(this.origin, this.mins, this.maxs, end, type, this);
636 }
637
638 bool _Movetype_PushEntity(entity this, vector push, bool failonstartsolid, bool dolink)  // SV_PushEntity
639 {
640         _Movetype_PushEntityTrace(this, push);
641
642         // NOTE: this is a workaround for the QC's lack of a worldstartsolid trace parameter
643         if(trace_startsolid && failonstartsolid)
644         {
645                 int oldtype = this.move_nomonsters;
646                 this.move_nomonsters = MOVE_WORLDONLY;
647                 _Movetype_PushEntityTrace(this, push);
648                 this.move_nomonsters = oldtype;
649                 if(trace_startsolid)
650                         return true;
651         }
652
653         this.origin = trace_endpos;
654
655         vector last_origin = this.origin;
656
657         _Movetype_LinkEdict(this, dolink);
658
659         if((this.solid >= SOLID_TRIGGER && trace_fraction < 1 && (!IS_ONGROUND(this) || this.groundentity != trace_ent)))
660                 _Movetype_Impact(this, trace_ent);
661
662         return (this.origin == last_origin); // false if teleported by touch
663 }
664
665 void _Movetype_Physics_Frame(entity this, float movedt)
666 {
667         this.move_didgravity = -1;
668         switch (this.move_movetype)
669         {
670                 case MOVETYPE_PUSH:
671                 case MOVETYPE_FAKEPUSH:
672                         _Movetype_Physics_Push(this, movedt);
673                         break;
674                 case MOVETYPE_NONE:
675                         break;
676                 case MOVETYPE_FOLLOW:
677                         _Movetype_Physics_Follow(this);
678                         break;
679                 case MOVETYPE_NOCLIP:
680                         _Movetype_CheckWater(this);
681                         this.origin = this.origin + movedt * this.velocity;
682                         this.angles = this.angles + movedt * this.avelocity;
683                         _Movetype_LinkEdict(this, false);
684                         break;
685                 case MOVETYPE_STEP:
686                         _Movetype_Physics_Step(this, movedt);
687                         break;
688                 case MOVETYPE_WALK:
689                         _Movetype_Physics_Walk(this, movedt);
690                         break;
691                 case MOVETYPE_TOSS:
692                 case MOVETYPE_BOUNCE:
693                 case MOVETYPE_BOUNCEMISSILE:
694                 case MOVETYPE_FLYMISSILE:
695                 case MOVETYPE_FLY:
696                 case MOVETYPE_FLY_WORLDONLY:
697                         _Movetype_Physics_Toss(this, movedt);
698                         if(wasfreed(this))
699                                 return;
700                         _Movetype_LinkEdict(this, true);
701                         break;
702                 case MOVETYPE_PHYSICS:
703                         break;
704         }
705 }
706
707 void _Movetype_Physics_ClientFrame(entity this, float movedt)
708 {
709         this.move_didgravity = -1;
710         switch (this.move_movetype)
711         {
712                 case MOVETYPE_PUSH:
713                 case MOVETYPE_FAKEPUSH:
714                         LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
715                         break;
716                 case MOVETYPE_NONE:
717                         break;
718                 case MOVETYPE_FOLLOW:
719                         _Movetype_Physics_Follow(this);
720                         break;
721                 case MOVETYPE_NOCLIP:
722                         _Movetype_CheckWater(this);
723                         this.origin = this.origin + movedt * this.velocity;
724                         this.angles = this.angles + movedt * this.avelocity;
725                         break;
726                 case MOVETYPE_STEP:
727                         _Movetype_Physics_Step(this, movedt);
728                         break;
729                 case MOVETYPE_WALK:
730                 case MOVETYPE_FLY:
731                 case MOVETYPE_FLY_WORLDONLY:
732                         _Movetype_Physics_Walk(this, movedt);
733                         break;
734                 case MOVETYPE_TOSS:
735                 case MOVETYPE_BOUNCE:
736                 case MOVETYPE_BOUNCEMISSILE:
737                 case MOVETYPE_FLYMISSILE:
738                         _Movetype_Physics_Toss(this, movedt);
739                         break;
740                 case MOVETYPE_PHYSICS:
741                         break;
742         }
743
744         //_Movetype_CheckVelocity(this);
745
746         _Movetype_LinkEdict(this, true);
747
748         //_Movetype_CheckVelocity(this);
749 }
750
751 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient)  // to be run every move frame
752 {
753         this.move_time = time;
754
755         if(isclient)
756                 _Movetype_Physics_ClientFrame(this, movedt);
757         else
758                 _Movetype_Physics_Frame(this, movedt);
759         if(wasfreed(this))
760                 return;
761
762         setorigin(this, this.origin);
763 }
764
765 void Movetype_Physics_NoMatchServer(entity this)  // optimized
766 {
767         float movedt = time - this.move_time;
768         this.move_time = time;
769
770         _Movetype_Physics_Frame(this, movedt);
771         if(wasfreed(this))
772                 return;
773
774         setorigin(this, this.origin);
775 }
776
777 void Movetype_Physics_MatchServer(entity this, bool sloppy)
778 {
779         Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
780 }
781
782 // saved .move_*
783 .vector tic_origin;
784 .vector tic_velocity;
785 .int tic_flags;
786 .vector tic_avelocity;
787 .vector tic_angles;
788
789 // saved .*
790 .vector tic_saved_origin;
791 .vector tic_saved_velocity;
792 .int tic_saved_flags;
793 .vector tic_saved_avelocity;
794 .vector tic_saved_angles;
795 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy)  // SV_Physics_Entity
796 {
797         // this hack exists to contain the physics feature
798         // (so entities can place themselves in the world and not need to update .tic_* themselves)
799 #define X(s) \
800         if(this.(s) != this.tic_saved_##s) \
801                 this.tic_##s = this.(s)
802
803         X(origin);
804         X(velocity);
805         X(flags);
806         X(avelocity);
807         X(angles);
808 #undef X
809
810         this.flags = this.tic_flags;
811         this.velocity = this.tic_velocity;
812         setorigin(this, this.tic_origin);
813         this.avelocity = this.tic_avelocity;
814         this.angles = this.tic_angles;
815
816         if(tr <= 0)
817         {
818                 Movetype_Physics_NoMatchServer(this);
819
820                 this.tic_saved_flags = this.tic_flags = this.flags;
821                 this.tic_saved_velocity = this.tic_velocity = this.velocity;
822                 this.tic_saved_origin = this.tic_origin = this.origin;
823                 this.tic_saved_avelocity = this.tic_avelocity = this.avelocity;
824                 this.tic_saved_angles = this.tic_angles = this.angles;
825                 return;
826         }
827
828         float dt = time - this.move_time;
829
830         int n = bound(0, floor(dt / tr), 32); // limit the number of frames to 32 (CL_MAX_USERCMDS, using DP_SMALLMEMORY value for consideration of QC's limitations)
831         dt -= n * tr;
832         this.move_time += n * tr;
833
834         if(!this.move_didgravity)
835                 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
836
837         for (int j = 0; j < n; ++j)
838         {
839                 _Movetype_Physics_Frame(this, tr);
840                 if(wasfreed(this))
841                         return;
842         }
843
844         // update the physics fields
845         this.tic_origin = this.origin;
846         this.tic_velocity = this.velocity;
847         this.tic_avelocity = this.avelocity;
848         this.tic_angles = this.angles;
849         this.tic_flags = this.flags;
850
851         // restore their actual values
852         this.flags = this.tic_saved_flags;
853         this.velocity = this.tic_saved_velocity;
854         setorigin(this, this.tic_saved_origin);
855         //this.avelocity = this.tic_saved_avelocity;
856         this.angles = this.tic_saved_angles;
857
858         this.avelocity = this.tic_avelocity;
859
860         if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
861         {
862                 // now continue the move from move_time to time
863                 this.velocity = this.tic_velocity;
864
865                 if(this.move_didgravity > 0)
866                 {
867                         this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
868                             * dt
869                             * ((this.gravity) ? this.gravity : 1)
870                             * PHYS_GRAVITY(this);
871                 }
872
873                 this.angles = this.tic_angles + dt * this.avelocity;
874
875                 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
876                 {
877                         setorigin(this, this.tic_origin + dt * this.velocity);
878                 }
879                 else
880                 {
881                         setorigin(this, this.tic_origin);
882                         _Movetype_PushEntityTrace(this, dt * this.velocity);
883                         if(!trace_startsolid)
884                                 setorigin(this, trace_endpos);
885                         else
886                                 setorigin(this, this.tic_saved_origin);
887                 }
888
889                 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
890                         this.velocity_z -= 0.5 * dt * ((this.gravity) ? this.gravity : 1) * PHYS_GRAVITY(this);
891         }
892         else
893         {
894                 this.velocity = this.tic_velocity;
895                 this.angles = this.tic_angles;
896                 setorigin(this, this.tic_origin);
897         }
898
899         this.flags = this.tic_flags;
900
901         this.tic_saved_flags = this.flags;
902         this.tic_saved_velocity = this.velocity;
903         this.tic_saved_origin = this.origin;
904         this.tic_saved_avelocity = this.avelocity;
905         this.tic_saved_angles = this.angles;
906 }